aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2026-11-23 11:29:55 +1100
committerYuchen Pei <id@ypei.org>2026-11-23 11:29:55 +1100
commit7e3fb4e798ed73f097d607fec9c65609ad399df9 (patch)
tree5054c464fc24af9292516adbb505aa802fa87358
parentd67ab96ad83f005c0ec944b2fdca7f07ee2c54ad (diff)
[emacs] chatbot-mode move messages up and down
-rw-r--r--emacs/.emacs.d/lisp/my/chatbot-mode.el38
1 files changed, 36 insertions, 2 deletions
diff --git a/emacs/.emacs.d/lisp/my/chatbot-mode.el b/emacs/.emacs.d/lisp/my/chatbot-mode.el
index eeaace8..0880da5 100644
--- a/emacs/.emacs.d/lisp/my/chatbot-mode.el
+++ b/emacs/.emacs.d/lisp/my/chatbot-mode.el
@@ -62,10 +62,13 @@
;;
;; M-n/M-p Go to the next / previous message. The plain n and p
;; keys keep their markdown-view-mode heading navigation.
+;; M-<up>, M-<down>
+;; Move the message at point up or down past its neighbour,
+;; and save.
;; e Edit the message at point. At the end of the buffer
;; (reached with M->), before the first message, or with
;; C-u, write a new "# User" message instead.
-;; a Write a new "# User" message, wherever point is.
+;; m Write a new "# User" message, wherever point is.
;; >, M-; Quote the region, or the whole message at point, into the
;; compose buffer, starting a new "# User" message if there
;; is none. Every line gets "> ", with a blank line before
@@ -172,6 +175,35 @@ Repeat to undo further. ARG is passed to `undo'."
(goto-char (point-min))))
(recenter 0))
+(defun chatbot-move-message-down (&optional up)
+ "Swap the message at point with the next one, or the previous if UP.
+Save the buffer and leave point on the moved message."
+ (interactive)
+ (let* ((cur (or (chatbot--message-at-point) (user-error "No message here")))
+ (other (save-excursion
+ (goto-char (if up (max (point-min) (1- (nth 1 cur)))
+ (nth 2 cur)))
+ (unless (if up (= (nth 1 cur) (point-min)) (eobp))
+ (chatbot--message-at-point))))
+ (a (if up other cur))
+ (b (if up cur other))
+ (fmt (lambda (m) (concat "# " (nth 0 m) "\n\n" (nth 3 m) "\n")))
+ (inhibit-read-only t))
+ (unless other (user-error "No message %s this one" (if up "above" "below")))
+ (let ((tail (if (= (nth 2 b) (point-max)) "" "\n"))
+ (moved-b (funcall fmt b)))
+ (undo-boundary)
+ (delete-region (nth 1 a) (nth 2 b))
+ (goto-char (nth 1 a))
+ (insert moved-b "\n" (funcall fmt a) tail)
+ (goto-char (if up (nth 1 a) (+ (nth 1 a) (length moved-b) 1))))
+ (when buffer-file-name (save-buffer))))
+
+(defun chatbot-move-message-up ()
+ "Swap the message at point with the previous one."
+ (interactive)
+ (chatbot-move-message-down t))
+
(define-minor-mode chatbot-catch-up-mode
"Catch up on turns you forgot to copy over, by alternating roles.
While on, the clipboard is pasted as User after an Assistant message,
@@ -315,8 +347,10 @@ Start a new User message if this log has no compose buffer."
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-n") #'chatbot-next-message)
(define-key map (kbd "M-p") #'chatbot-previous-message)
+ (define-key map (kbd "M-<up>") #'chatbot-move-message-up)
+ (define-key map (kbd "M-<down>") #'chatbot-move-message-down)
(define-key map (kbd "e") #'chatbot-compose)
- (define-key map (kbd "a") #'chatbot-append)
+ (define-key map (kbd "m") #'chatbot-append)
(define-key map (kbd ">") #'chatbot-quote)
(define-key map (kbd "M-;") #'chatbot-quote)
(define-key map (kbd "M-y") #'chatbot-paste-reply)