Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lisp/ghostel-line-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 11 additions & 1 deletion test/ghostel-line-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))))

Expand Down
Loading