@@ -4211,12 +4211,14 @@ See also `ement-room-compose-send'."
4211
4211
(ement--original-event-for
4212
4212
replying-to-event session)))))))
4213
4213
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")
4217
4218
(let ((body (ement-room-compose-buffer-string-trimmed))
4218
4219
(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))
4220
4222
(kill-buffer)
4221
4223
(delete-window)
4222
4224
;; Make sure we end up with the associated room buffer selected.
@@ -4230,6 +4232,11 @@ See also `ement-room-compose-send'."
4230
4232
(throw 'room-win win))))))))
4231
4233
(select-window win))))
4232
4234
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
+
4233
4240
(defun ement-room-init-compose-buffer (room session)
4234
4241
"Set up the current buffer as a compose buffer.
4235
4242
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'."
4252
4259
(use-local-map (if (current-local-map)
4253
4260
(copy-keymap (current-local-map))
4254
4261
(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.
4255
4264
(local-set-key [remap delete-backward-char]
4256
- `(menu-item "" ement-room-compose-abort
4265
+ `(menu-item "" ement-room-compose-abort-no-history
4257
4266
:filter ,(lambda (cmd)
4258
4267
(and ement-room-self-insert-mode
4259
4268
(<= (buffer-size) 1)
4260
4269
(save-restriction (widen) (eobp))
4261
4270
cmd))))
4262
4271
(local-set-key [remap save-buffer] #'ement-room-dispatch-send-message)
4263
4272
(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)
4264
4275
(setq header-line-format
4265
4276
(concat (substitute-command-keys
4266
4277
(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'."
4347
4358
(with-current-buffer buffer
4348
4359
(derived-mode-p 'ement-room-mode)))
4349
4360
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
+
4350
4402
;;;;; Widgets
4351
4403
4352
4404
(require 'widget)
0 commit comments