Skip to content

Commit

Permalink
Add compose buffer message history commands bound to M-p and M-n
Browse files Browse the repository at this point in the history
New commands:
- ement-room-compose-message-history-prev-message
- ement-room-compose-message-history-next-message
- ement-room-compose-abort-no-history

(ement-room-init-compose-buffer) Update key bindings.
  • Loading branch information
Phil Sainty committed Dec 9, 2023
1 parent 307307b commit 3b8ffa2
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions ement-room.el
Original file line number Diff line number Diff line change
Expand Up @@ -4211,12 +4211,14 @@ See also `ement-room-compose-send'."
(ement--original-event-for
replying-to-event session)))))))

(defun ement-room-compose-abort ()
"Kill the compose buffer and window."
(interactive)
(defun ement-room-compose-abort (&optional no-history)
"Kill the compose buffer and window.
With prefix arg NO-HISTORY, do not add to `ement-room-message-history'."
(interactive "P")
(let ((body (ement-room-compose-buffer-string-trimmed))
(room ement-room))
(add-to-history 'ement-room-message-history body)
(unless no-history
(add-to-history 'ement-room-message-history body))
(kill-buffer)
(delete-window)
;; Make sure we end up with the associated room buffer selected.
Expand All @@ -4230,6 +4232,11 @@ See also `ement-room-compose-send'."
(throw 'room-win win))))))))
(select-window win))))

(defun ement-room-compose-abort-no-history ()
"Kill the compose buffer and window without adding to the history."
(interactive)
(ement-room-compose-abort t))

(defun ement-room-init-compose-buffer (room session)
"Set up the current buffer as a compose buffer.
Sets ROOM and SESSION buffer-locally, binds `save-buffer' in
Expand All @@ -4252,15 +4259,19 @@ a copy of the local keymap, and sets `header-line-format'."
(use-local-map (if (current-local-map)
(copy-keymap (current-local-map))
(make-sparse-keymap)))
;; When `ement-room-self-insert-mode' is enabled, deleting the final character of the
;; message aborts and kills the compose buffer.
(local-set-key [remap delete-backward-char]
`(menu-item "" ement-room-compose-abort
`(menu-item "" ement-room-compose-abort-no-history
:filter ,(lambda (cmd)
(and ement-room-self-insert-mode
(<= (buffer-size) 1)
(save-restriction (widen) (eobp))
cmd))))
(local-set-key [remap save-buffer] #'ement-room-dispatch-send-message)
(local-set-key (kbd "C-c C-k") #'ement-room-compose-abort)
(local-set-key (kbd "M-n") #'ement-room-compose-message-history-next-message)
(local-set-key (kbd "M-p") #'ement-room-compose-message-history-prev-message)
(setq header-line-format
(concat (substitute-command-keys
(format " Press \\[save-buffer] to send message to room (%s), or \\[ement-room-compose-abort] to cancel."
Expand Down Expand Up @@ -4347,6 +4358,47 @@ Used with `dabbrev-friend-buffer-function'."
(with-current-buffer buffer
(derived-mode-p 'ement-room-mode)))

;;; Message history for compose buffers.

(defvar-local ement-room--compose-message-history-index -1)
(defvar-local ement-room--compose-message-history-initial "")

(defun ement-room-compose-message-history-prev-message (arg)
"Cycle backward through message history, after saving current message.
With a numeric prefix ARG, go back ARG messages."
(interactive "*p")
(let ((len (length ement-room-message-history)))
(cond ((<= len 0)
(user-error "Empty message history"))
((eql arg 0)) ;; No-op.
((and (> arg 0) (> ement-room--compose-message-history-index len))
(user-error "Beginning of history; no preceding item"))
((and (< arg 0) (< ement-room--compose-message-history-index 0))
(user-error "End of history; no next item"))
(t
;; Store the not-from-history buffer message.
(when (< ement-room--compose-message-history-index 0)
(setq ement-room--compose-message-history-initial
(ement-room-compose-buffer-string-trimmed)))
;; Update the index.
(setq ement-room--compose-message-history-index
(let ((newidx (+ arg ement-room--compose-message-history-index)))
(cond ((> newidx len) len)
((< newidx -1) -1)
(t newidx))))
;; Update the buffer.
(erase-buffer)
(insert (if (< ement-room--compose-message-history-index 0)
ement-room--compose-message-history-initial
(nth ement-room--compose-message-history-index
ement-room-message-history)))))))

(defun ement-room-compose-message-history-next-message (arg)
"Cycle forward through message history, after saving current message.
With a numeric prefix ARG, go forward ARG messages."
(interactive "*p")
(ement-room-compose-message-history-prev-message (- arg)))

;;;;; Widgets

(require 'widget)
Expand Down

0 comments on commit 3b8ffa2

Please sign in to comment.