Skip to content

Commit

Permalink
defvar rather than defcustom pgmacs-header-line
Browse files Browse the repository at this point in the history
  • Loading branch information
emarsden committed Sep 28, 2024
1 parent 8a123d9 commit 215fc1d
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions pgmacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ concerning a specific table, rather than the entire database."
:type 'boolean
:group 'pgmacs)

(defcustom pgmacs-header-line
(defvar pgmacs-header-line
"Header-line to use in PGmacs buffers. Nil to disable."
(list (when (char-displayable-p ?🐘) " 🐘")
(propertize " PGmacs " 'face 'bold)
'(:eval (when pgmacs--con
Expand All @@ -164,9 +165,7 @@ concerning a specific table, rather than the entire database."
(format "%s as %s%s on Unix socket"
(propertize (cl-fourth ci) 'face 'bold)
maybe-icon
(cl-fifth ci))))))))
"Header-line to use in PGmacs buffers. Nil to disable."
:group 'pgmacs)
(cl-fifth ci)))))))))

(defcustom pgmacs-mode-hook nil
"Mode hook for `pgmacs-mode'."
Expand Down Expand Up @@ -523,10 +522,7 @@ Use PROMPT in the minibuffer and show the current value CURRENT-VALUE."
(defun pgmacs-funcall-cell (function)
"Call FUNCTION on the content of current cell. Does not modify database.
FUNCTION takes a single argument which is the value of the cell at point."
(let* ((pgmacstbl (or (pgmacstbl-current-table)
(error "Cursor is not in a pgmacstbl")))
(cols (pgmacstbl-columns pgmacstbl))
(col-id (or (pgmacstbl-current-column)
(let* ((col-id (or (pgmacstbl-current-column)
(error "Not on a pgmacstbl column")))
(current-row (or (pgmacstbl-current-object)
(error "Cursor is not on a pgmacstbl row")))
Expand Down Expand Up @@ -554,28 +550,27 @@ FUNCTION is called on (old-value col-name col-type) and returns the new value."
(let* ((current (funcall (pgmacstbl-column-formatter col)
(nth col-id current-row)))
(new-value (funcall function current col-name col-type)))
(when update-db
(when (null primary-keys)
(error "Can't edit content of a table that has no PRIMARY KEY"))
(unless pk-value
(error "Can't find value for primary key %s" pk))
(let* ((sql (format "UPDATE %s SET %s = $1 WHERE %s = $2"
(pg-escape-identifier pgmacs--table)
(pg-escape-identifier col-name)
(pg-escape-identifier pk)))
(res (pg-exec-prepared pgmacs--con sql
`((,new-value . ,col-type)
(,pk-value . ,pk-col-type)))))
(pgmacs--notify "%s" (pg-result res :status)))
(let ((new-row (copy-sequence current-row)))
(setf (nth col-id new-row) new-value)
;; pgmacstbl-update-object doesn't work, so insert then delete old row
(pgmacstbl-insert-object pgmacstbl new-row current-row)
(pgmacstbl-remove-object pgmacstbl current-row)
;; redrawing is necessary to ensure that all keybindings are present for the newly inserted
;; row.
(forward-line -1)
(pgmacs--redraw-pgmacstbl)))
(when (null primary-keys)
(error "Can't edit content of a table that has no PRIMARY KEY"))
(unless pk-value
(error "Can't find value for primary key %s" pk))
(let* ((sql (format "UPDATE %s SET %s = $1 WHERE %s = $2"
(pg-escape-identifier pgmacs--table)
(pg-escape-identifier col-name)
(pg-escape-identifier pk)))
(res (pg-exec-prepared pgmacs--con sql
`((,new-value . ,col-type)
(,pk-value . ,pk-col-type)))))
(pgmacs--notify "%s" (pg-result res :status)))
(let ((new-row (copy-sequence current-row)))
(setf (nth col-id new-row) new-value)
;; pgmacstbl-update-object doesn't work, so insert then delete old row
(pgmacstbl-insert-object pgmacstbl new-row current-row)
(pgmacstbl-remove-object pgmacstbl current-row)
;; redrawing is necessary to ensure that all keybindings are present for the newly inserted
;; row.
(forward-line -1)
(pgmacs--redraw-pgmacstbl))
new-value)))

(defun pgmacs--edit-value-minibuffer (row primary-keys)
Expand Down

0 comments on commit 215fc1d

Please sign in to comment.