diff --git a/lisp/ghostel-line-mode.el b/lisp/ghostel-line-mode.el index d5f630f5..0b368e72 100644 --- a/lisp/ghostel-line-mode.el +++ b/lisp/ghostel-line-mode.el @@ -147,6 +147,9 @@ Most recent first.") "Current index into `ghostel--line-mode-history' while browsing history. Nil when not browsing.") +(defvar-local ghostel--line-mode-history-draft nil + "Input that was being composed when history browsing started.") + (defvar-local ghostel--line-mode-saved-cursor-type nil "Saved `cursor-type' from before line mode entry, as a singleton list. A list (VALUE) when saved, nil when not — distinguishes \"not @@ -930,26 +933,29 @@ Stays in line mode for the next prompt." (goto-char (marker-position ghostel--line-input-end))))) (defun ghostel-line-mode-history-previous () - "Replace the input with the previous entry from history." + "Replace the input with the previous entry from history. +The input being composed is kept for `ghostel-line-mode-history-next'." (interactive) (when ghostel--line-mode-history (let* ((len (length ghostel--line-mode-history)) (idx (if ghostel--line-mode-history-index (min (1- len) (1+ ghostel--line-mode-history-index)) + (setq ghostel--line-mode-history-draft + (ghostel--line-mode-input-text)) 0))) (setq ghostel--line-mode-history-index idx) (ghostel--line-mode-replace-input (nth idx ghostel--line-mode-history))))) (defun ghostel-line-mode-history-next () - "Replace the input with the next entry from history." + "Replace the input with the next entry from history. +Past the newest entry, restore the input that was being composed." (interactive) (cond - ((null ghostel--line-mode-history-index) - (ghostel--line-mode-replace-input "")) + ((null ghostel--line-mode-history-index)) ((zerop ghostel--line-mode-history-index) (setq ghostel--line-mode-history-index nil) - (ghostel--line-mode-replace-input "")) + (ghostel--line-mode-replace-input ghostel--line-mode-history-draft)) (t (setq ghostel--line-mode-history-index (1- ghostel--line-mode-history-index)) diff --git a/test/ghostel-line-mode-test.el b/test/ghostel-line-mode-test.el index 2ae4dd87..43780e26 100644 --- a/test/ghostel-line-mode-test.el +++ b/test/ghostel-line-mode-test.el @@ -1141,7 +1141,9 @@ a duplicated line." (kill-buffer buf)))) (ert-deftest ghostel-test-line-mode-history () - "Line mode \\`M-p' / \\`M-n' cycle through the history ring." + "Line mode \\`M-p' / \\`M-n' cycle through the history ring. +The input composed before browsing comes back past the newest entry, +and \\`M-n' outside of browsing leaves it alone." (let ((buf (generate-new-buffer " *ghostel-test-line-history*"))) (unwind-protect (with-current-buffer buf @@ -1157,6 +1159,9 @@ a duplicated line." ((symbol-function 'ghostel--invalidate) #'ignore)) (ghostel-line-mode) (setq ghostel--line-mode-history '("third" "second" "first")) + (ghostel--line-mode-replace-input "draft") + (ghostel-line-mode-history-next) + (should (equal (ghostel--line-mode-input-text) "draft")) (ghostel-line-mode-history-previous) (should (equal (ghostel--line-mode-input-text) "third")) (ghostel-line-mode-history-previous) @@ -1168,6 +1173,11 @@ a duplicated line." (ghostel-line-mode-history-next) (should (equal (ghostel--line-mode-input-text) "third")) (ghostel-line-mode-history-next) + (should (equal (ghostel--line-mode-input-text) "draft")) + ;; Browsing again from an emptied input restores that. + (ghostel--line-mode-replace-input "") + (ghostel-line-mode-history-previous) + (ghostel-line-mode-history-next) (should (equal (ghostel--line-mode-input-text) ""))))) (kill-buffer buf))))