Skip to content

Commit cf105d7

Browse files
author
Phil Sainty
committed
Add compose buffer history navigation commands bound to M-p and M-n
Navigate `ement-room-message-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.
1 parent 307307b commit cf105d7

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

ement-room.el

+57-5
Original file line numberDiff line numberDiff line change
@@ -4211,12 +4211,14 @@ See also `ement-room-compose-send'."
42114211
(ement--original-event-for
42124212
replying-to-event session)))))))
42134213

4214-
(defun ement-room-compose-abort ()
4215-
"Kill the compose buffer and window."
4216-
(interactive)
4214+
(defun ement-room-compose-abort (&optional no-history)
4215+
"Kill the compose buffer and window.
4216+
With prefix arg NO-HISTORY, do not add to `ement-room-message-history'."
4217+
(interactive "P")
42174218
(let ((body (ement-room-compose-buffer-string-trimmed))
42184219
(room ement-room))
4219-
(add-to-history 'ement-room-message-history body)
4220+
(unless no-history
4221+
(add-to-history 'ement-room-message-history body))
42204222
(kill-buffer)
42214223
(delete-window)
42224224
;; Make sure we end up with the associated room buffer selected.
@@ -4230,6 +4232,11 @@ See also `ement-room-compose-send'."
42304232
(throw 'room-win win))))))))
42314233
(select-window win))))
42324234

4235+
(defun ement-room-compose-abort-no-history ()
4236+
"Kill the compose buffer and window without adding to the history."
4237+
(interactive)
4238+
(ement-room-compose-abort t))
4239+
42334240
(defun ement-room-init-compose-buffer (room session)
42344241
"Set up the current buffer as a compose buffer.
42354242
Sets ROOM and SESSION buffer-locally, binds `save-buffer' in
@@ -4252,15 +4259,19 @@ a copy of the local keymap, and sets `header-line-format'."
42524259
(use-local-map (if (current-local-map)
42534260
(copy-keymap (current-local-map))
42544261
(make-sparse-keymap)))
4262+
;; When `ement-room-self-insert-mode' is enabled, deleting the final character of the
4263+
;; message aborts and kills the compose buffer.
42554264
(local-set-key [remap delete-backward-char]
4256-
`(menu-item "" ement-room-compose-abort
4265+
`(menu-item "" ement-room-compose-abort-no-history
42574266
:filter ,(lambda (cmd)
42584267
(and ement-room-self-insert-mode
42594268
(<= (buffer-size) 1)
42604269
(save-restriction (widen) (eobp))
42614270
cmd))))
42624271
(local-set-key [remap save-buffer] #'ement-room-dispatch-send-message)
42634272
(local-set-key (kbd "C-c C-k") #'ement-room-compose-abort)
4273+
(local-set-key (kbd "M-n") #'ement-room-compose-history-next-message)
4274+
(local-set-key (kbd "M-p") #'ement-room-compose-history-prev-message)
42644275
(setq header-line-format
42654276
(concat (substitute-command-keys
42664277
(format " Press \\[save-buffer] to send message to room (%s), or \\[ement-room-compose-abort] to cancel."
@@ -4347,6 +4358,47 @@ Used with `dabbrev-friend-buffer-function'."
43474358
(with-current-buffer buffer
43484359
(derived-mode-p 'ement-room-mode)))
43494360

4361+
;;; Message history for compose buffers.
4362+
4363+
(defvar-local ement-room--compose-message-history-index -1)
4364+
(defvar-local ement-room--compose-message-history-initial "")
4365+
4366+
(defun ement-room-compose-history-prev-message (arg)
4367+
"Cycle backward through message history, after saving current message.
4368+
With a numeric prefix ARG, go back ARG messages."
4369+
(interactive "*p")
4370+
(let ((len (length ement-room-message-history)))
4371+
(cond ((<= len 0)
4372+
(user-error "Empty message history"))
4373+
((eql arg 0)) ;; No-op.
4374+
((and (> arg 0) (>= ement-room--compose-message-history-index (1- len)))
4375+
(user-error "Beginning of history; no preceding item"))
4376+
((and (< arg 0) (< ement-room--compose-message-history-index 0))
4377+
(user-error "End of history; no next item"))
4378+
(t
4379+
;; Store the not-from-history buffer message.
4380+
(when (< ement-room--compose-message-history-index 0)
4381+
(setq ement-room--compose-message-history-initial
4382+
(ement-room-compose-buffer-string-trimmed)))
4383+
;; Update the index.
4384+
(setq ement-room--compose-message-history-index
4385+
(let ((newidx (+ arg ement-room--compose-message-history-index)))
4386+
(cond ((>= newidx len) (1- len))
4387+
((< newidx -1) -1)
4388+
(t newidx))))
4389+
;; Update the buffer.
4390+
(erase-buffer)
4391+
(insert (if (< ement-room--compose-message-history-index 0)
4392+
ement-room--compose-message-history-initial
4393+
(nth ement-room--compose-message-history-index
4394+
ement-room-message-history)))))))
4395+
4396+
(defun ement-room-compose-history-next-message (arg)
4397+
"Cycle forward through message history, after saving current message.
4398+
With a numeric prefix ARG, go forward ARG messages."
4399+
(interactive "*p")
4400+
(ement-room-compose-history-prev-message (- arg)))
4401+
43504402
;;;;; Widgets
43514403

43524404
(require 'widget)

0 commit comments

Comments
 (0)