aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Porter2024-06-13 21:26:53 -0700
committerJim Porter2024-06-20 19:01:20 -0700
commite22b072423a6764632328d4e0fecc06a6e7efe9b (patch)
treee1280eda53f6731f0c6af09580cbef6cd5d928f4
parent1a55e957ae57ec32ae960eabdb170b5b427392d4 (diff)
downloademacs-e22b072423a6764632328d4e0fecc06a6e7efe9b.tar.gz
emacs-e22b072423a6764632328d4e0fecc06a6e7efe9b.zip
Ensure navigating by paragraphs in Eshell stops at prompts and paragraphs
The previous implementation in 6ae2b74ed20 only stopped at prompts, which isn't the right behavior (bug#61545). * lisp/eshell/em-prompt.el (eshell-forward-paragraph) (eshell-backward-paragraph): Reimplement to handle prompts and paragraphs (the latter by calling the original 'forward-paragraph'). * test/lisp/eshell/em-prompt-tests.el (em-prompt-test/next-previous-prompt/multiline): Rename. (em-prompt-test/forward-backward-paragraph-1): New function. (em-prompt-test/forward-backward-paragraph) (em-prompt-test/forward-backward-paragraph/multiline): New tests.
-rw-r--r--lisp/eshell/em-prompt.el32
-rw-r--r--test/lisp/eshell/em-prompt-tests.el49
2 files changed, 75 insertions, 6 deletions
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index b6556d29544..7de2bd4dc21 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -167,17 +167,39 @@ negative, find the Nth next match."
167 167
168(defun eshell-forward-paragraph (&optional n) 168(defun eshell-forward-paragraph (&optional n)
169 "Move to the beginning of the Nth next prompt in the buffer. 169 "Move to the beginning of the Nth next prompt in the buffer.
170Like `forward-paragraph', but navigates using fields." 170Like `forward-paragraph', but also stops at the beginning of each prompt."
171 (interactive "p") 171 (interactive "p")
172 (eshell-next-prompt n) 172 (unless n (setq n 1))
173 (goto-char (field-beginning (point) t))) 173 (let (;; We'll handle the "paragraph" starts ourselves.
174 (paragraph-start regexp-unmatchable)
175 (inhibit-field-text-motion t))
176 (cond
177 ((> n 0)
178 (while (and (> n 0) (< (point) (point-max)))
179 (let ((next-paragraph (save-excursion (forward-paragraph) (point)))
180 (next-prompt (save-excursion
181 (if-let ((match (text-property-search-forward
182 'field 'prompt t t)))
183 (prop-match-beginning match)
184 (point-max)))))
185 (goto-char (min next-paragraph next-prompt)))
186 (setq n (1- n))))
187 ((< n 0)
188 (while (and (< n 0) (> (point) (point-min)))
189 (let ((prev-paragraph (save-excursion (backward-paragraph) (point)))
190 (prev-prompt (save-excursion
191 (if (text-property-search-backward
192 'field 'prompt t)
193 (point)
194 (point-min)))))
195 (goto-char (max prev-paragraph prev-prompt)))
196 (setq n (1+ n)))))))
174 197
175(defun eshell-backward-paragraph (&optional n) 198(defun eshell-backward-paragraph (&optional n)
176 "Move to the beginning of the Nth previous prompt in the buffer. 199 "Move to the beginning of the Nth previous prompt in the buffer.
177Like `backward-paragraph', but navigates using fields." 200Like `backward-paragraph', but navigates using fields."
178 (interactive "p") 201 (interactive "p")
179 (eshell-previous-prompt n) 202 (eshell-forward-paragraph (- (or n 1))))
180 (goto-char (field-beginning (point) t)))
181 203
182(defun eshell-next-prompt (&optional n) 204(defun eshell-next-prompt (&optional n)
183 "Move to end of Nth next prompt in the buffer." 205 "Move to end of Nth next prompt in the buffer."
diff --git a/test/lisp/eshell/em-prompt-tests.el b/test/lisp/eshell/em-prompt-tests.el
index 964609e6410..fbadade061f 100644
--- a/test/lisp/eshell/em-prompt-tests.el
+++ b/test/lisp/eshell/em-prompt-tests.el
@@ -39,6 +39,9 @@
39 39
40;;; Tests: 40;;; Tests:
41 41
42
43;; Prompt output
44
42(ert-deftest em-prompt-test/field-properties () 45(ert-deftest em-prompt-test/field-properties ()
43 "Check that field properties are properly set on Eshell output/prompts." 46 "Check that field properties are properly set on Eshell output/prompts."
44 (with-temp-eshell 47 (with-temp-eshell
@@ -104,6 +107,9 @@ This tests the case when `eshell-highlight-prompt' is nil."
104 'front-sticky '(read-only field font-lock-face) 107 'front-sticky '(read-only field font-lock-face)
105 'rear-nonsticky '(read-only field font-lock-face))))))) 108 'rear-nonsticky '(read-only field font-lock-face)))))))
106 109
110
111;; Prompt navigation
112
107(defun em-prompt-test/next-previous-prompt-1 () 113(defun em-prompt-test/next-previous-prompt-1 ()
108 "Helper for checking forward/backward navigation of old prompts." 114 "Helper for checking forward/backward navigation of old prompts."
109 (with-temp-eshell 115 (with-temp-eshell
@@ -150,11 +156,52 @@ This tests the case when `eshell-highlight-prompt' is nil."
150 "Check that navigating forward/backward through old prompts works correctly." 156 "Check that navigating forward/backward through old prompts works correctly."
151 (em-prompt-test/next-previous-prompt-1)) 157 (em-prompt-test/next-previous-prompt-1))
152 158
153(ert-deftest em-prompt-test/next-previous-prompt-multiline () 159(ert-deftest em-prompt-test/next-previous-prompt/multiline ()
154 "Check old prompt forward/backward navigation for multiline prompts." 160 "Check old prompt forward/backward navigation for multiline prompts."
155 (em-prompt-test--with-multiline 161 (em-prompt-test--with-multiline
156 (em-prompt-test/next-previous-prompt-1))) 162 (em-prompt-test/next-previous-prompt-1)))
157 163
164(defun em-prompt-test/forward-backward-paragraph-1 ()
165 "Helper for checking forward/backward navigation by paragraphs."
166 (with-temp-eshell
167 (cl-flet ((at-prompt-for-command-p (command)
168 (and (equal (point) (field-beginning))
169 (equal (get-text-property (point) 'field) 'prompt)
170 (save-excursion
171 (goto-char (field-end))
172 (equal (field-string) command)))))
173 (eshell-insert-command "echo 'high five'")
174 (eshell-insert-command "echo 'up high\n\ndown low'")
175 (eshell-insert-command "echo 'too slow'")
176 (insert "echo goodby") ; A partially-entered command.
177 (ert-info ("Go back to the last prompt")
178 (eshell-backward-paragraph)
179 (should (at-prompt-for-command-p "echo goodby")))
180 (ert-info ("Go back to the paragraph break")
181 (eshell-backward-paragraph 2)
182 (should (looking-at "\ndown low\n")))
183 (ert-info ("Go forward to the third prompt")
184 (eshell-forward-paragraph)
185 (should (at-prompt-for-command-p "echo 'too slow'\n")))
186 (ert-info ("Go backward to before the first prompt")
187 (eshell-backward-paragraph 5)
188 (should (looking-back "Welcome to the Emacs shell\n")))
189 (ert-info ("Go backward to the beginning of the buffer")
190 (eshell-backward-paragraph)
191 (should (bobp)))
192 (ert-info ("Go forward to the second prompt")
193 (eshell-forward-paragraph 3)
194 (should (at-prompt-for-command-p "echo 'up high\n\ndown low'\n"))))))
195
196(ert-deftest em-prompt-test/forward-backward-paragraph ()
197 "Check that navigating forward/backward through paragraphs works correctly."
198 (em-prompt-test/forward-backward-paragraph-1))
199
200(ert-deftest em-prompt-test/forward-backward-paragraph/multiline ()
201 "Check paragraph forward/backward navigation for multiline prompts."
202 (em-prompt-test--with-multiline
203 (em-prompt-test/forward-backward-paragraph-1)))
204
158(defun em-prompt-test/forward-backward-matching-input-1 () 205(defun em-prompt-test/forward-backward-matching-input-1 ()
159 "Helper for checking forward/backward navigation via regexps." 206 "Helper for checking forward/backward navigation via regexps."
160 (with-temp-eshell 207 (with-temp-eshell