aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavide Masserut2023-08-29 22:33:48 +0200
committerJim Porter2023-09-02 15:40:04 -0700
commit3d08d0dd80a02419f8301b63bb5663710f4f8ee2 (patch)
treead7a0223d01f0c183926c52c6fe27bd2bff1f6fd
parent6ae2b74ed20f7c96384ed67981405212d7e2bea2 (diff)
downloademacs-3d08d0dd80a02419f8301b63bb5663710f4f8ee2.tar.gz
emacs-3d08d0dd80a02419f8301b63bb5663710f4f8ee2.zip
Display the exit code if the last command failed in Eshell
* lisp/eshell/esh-io.el (eshell-last-command-status): Make buffer-local. * lisp/eshell/em-prompt.el (eshell-prompt-function): Insert the exit code if last command failed. * test/lisp/eshell/em-prompt-tests.el (em-prompt-test/after-failure): New test. (em-prompt-test/next-previous-prompt-1) (em-prompt-test/forward-backward-matching-input-1): Add a failing command to tests. * doc/misc/eshell.texi (Invocation): Document change. * etc/NEWS: Announce change (bug#65604).
-rw-r--r--doc/misc/eshell.texi3
-rw-r--r--etc/NEWS3
-rw-r--r--lisp/eshell/em-prompt.el2
-rw-r--r--lisp/eshell/esh-io.el2
-rw-r--r--test/lisp/eshell/em-prompt-tests.el44
5 files changed, 43 insertions, 11 deletions
diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi
index 7a563bc794c..0ec90d0c159 100644
--- a/doc/misc/eshell.texi
+++ b/doc/misc/eshell.texi
@@ -234,6 +234,9 @@ the foreground. That said, background processes invoked from Eshell
234can be controlled the same way as any other background process in 234can be controlled the same way as any other background process in
235Emacs. 235Emacs.
236 236
237If a command exits abnormally, Eshell will display its exit code
238in the next prompt.
239
237@subsection Command form 240@subsection Command form
238Command form looks much the same as in other shells. A command 241Command form looks much the same as in other shells. A command
239consists of arguments separated by spaces; the first argument is the 242consists of arguments separated by spaces; the first argument is the
diff --git a/etc/NEWS b/etc/NEWS
index 5c11b6b9ac7..81ef0d6cedb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -370,6 +370,9 @@ to load the edited aliases.
370Running 'rgrep' in Eshell now uses the Emacs grep facility instead of 370Running 'rgrep' in Eshell now uses the Emacs grep facility instead of
371calling external rgrep. 371calling external rgrep.
372 372
373+++
374*** If a command exits abnormally, the Eshell prompt now shows its exit code.
375
373** Pcomplete 376** Pcomplete
374 377
375--- 378---
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index a5abb75bccc..15b849a4d37 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -50,6 +50,8 @@ as is common with most shells."
50(defcustom eshell-prompt-function 50(defcustom eshell-prompt-function
51 (lambda () 51 (lambda ()
52 (concat (abbreviate-file-name (eshell/pwd)) 52 (concat (abbreviate-file-name (eshell/pwd))
53 (unless (eshell-exit-success-p)
54 (format " [%d]" eshell-last-command-status))
53 (if (= (file-user-uid) 0) " # " " $ "))) 55 (if (= (file-user-uid) 0) " # " " $ ")))
54 "A function that returns the Eshell prompt string." 56 "A function that returns the Eshell prompt string."
55 :type 'function 57 :type 'function
diff --git a/lisp/eshell/esh-io.el b/lisp/eshell/esh-io.el
index c07f871dd37..cd0cee6e21d 100644
--- a/lisp/eshell/esh-io.el
+++ b/lisp/eshell/esh-io.el
@@ -170,7 +170,7 @@ describing the mode, e.g. for using with `eshell-get-target'.")
170 170
171(defvar eshell-current-handles nil) 171(defvar eshell-current-handles nil)
172 172
173(defvar eshell-last-command-status 0 173(defvar-local eshell-last-command-status 0
174 "The exit code from the last command. 0 if successful.") 174 "The exit code from the last command. 0 if successful.")
175 175
176(defvar eshell-last-command-result nil 176(defvar eshell-last-command-result nil
diff --git a/test/lisp/eshell/em-prompt-tests.el b/test/lisp/eshell/em-prompt-tests.el
index f6a63ac0db5..46e74e64983 100644
--- a/test/lisp/eshell/em-prompt-tests.el
+++ b/test/lisp/eshell/em-prompt-tests.el
@@ -85,20 +85,41 @@ This tests the case when `eshell-highlight-prompt' is nil."
85 (apply #'propertize "hello\n" 85 (apply #'propertize "hello\n"
86 eshell-command-output-properties))))))) 86 eshell-command-output-properties)))))))
87 87
88(ert-deftest em-prompt-test/after-failure ()
89 "Check that current prompt shows the exit code of the last failed command."
90 (with-temp-eshell
91 (let ((debug-on-error nil))
92 (eshell-insert-command "(zerop \"foo\")"))
93 (let ((current-prompt (field-string (1- (point)))))
94 (should (equal-including-properties
95 current-prompt
96 (propertize
97 (concat (directory-file-name default-directory)
98 (unless (eshell-exit-success-p)
99 (format " [%d]" eshell-last-command-status))
100 (if (= (file-user-uid) 0) " # " " $ "))
101 'read-only t
102 'field 'prompt
103 'font-lock-face 'eshell-prompt
104 'front-sticky '(read-only field font-lock-face)
105 'rear-nonsticky '(read-only field font-lock-face)))))))
106
88(defun em-prompt-test/next-previous-prompt-1 () 107(defun em-prompt-test/next-previous-prompt-1 ()
89 "Helper for checking forward/backward navigation of old prompts." 108 "Helper for checking forward/backward navigation of old prompts."
90 (with-temp-eshell 109 (with-temp-eshell
91 (eshell-insert-command "echo one") 110 (eshell-insert-command "echo one")
92 (eshell-insert-command "echo two") 111 (eshell-insert-command "echo two")
93 (eshell-insert-command "echo three") 112 (eshell-insert-command "echo three")
113 (let ((debug-on-error nil)) ; A failed command.
114 (eshell-insert-command "(zerop \"foo\")"))
94 (insert "echo fou") ; A partially-entered command. 115 (insert "echo fou") ; A partially-entered command.
95 (ert-info ("Go back one prompt") 116 (ert-info ("Go back one prompt")
96 (eshell-previous-prompt) 117 (eshell-previous-prompt)
97 (should (equal (point) (field-beginning))) 118 (should (equal (point) (field-beginning)))
98 (should (equal (field-string) "echo three\n"))) 119 (should (equal (field-string) "(zerop \"foo\")\n")))
99 (ert-info ("Go back two prompts, starting from the end of the input") 120 (ert-info ("Go back three prompts, starting from the end of the input")
100 (end-of-line) 121 (end-of-line)
101 (eshell-previous-prompt 2) 122 (eshell-previous-prompt 3)
102 (should (equal (point) (field-beginning))) 123 (should (equal (point) (field-beginning)))
103 (should (equal (field-string) "echo one\n"))) 124 (should (equal (field-string) "echo one\n")))
104 (ert-info ("Go to the current prompt, starting from the end of the input") 125 (ert-info ("Go to the current prompt, starting from the end of the input")
@@ -110,20 +131,20 @@ This tests the case when `eshell-highlight-prompt' is nil."
110 (eshell-next-prompt) 131 (eshell-next-prompt)
111 (should (equal (point) (field-beginning))) 132 (should (equal (point) (field-beginning)))
112 (should (equal (field-string) "echo two\n"))) 133 (should (equal (field-string) "echo two\n")))
113 (ert-info ("Go forward two prompts") 134 (ert-info ("Go forward three prompts")
114 (eshell-next-prompt 2) 135 (eshell-next-prompt 3)
115 (should (equal (point) (field-beginning))) 136 (should (equal (point) (field-beginning)))
116 (should (equal (field-string) "echo fou"))) 137 (should (equal (field-string) "echo fou")))
117 (ert-info ("Go back one prompt, starting from the beginning of the line") 138 (ert-info ("Go back one prompt, starting from the beginning of the line")
118 (forward-line 0) 139 (forward-line 0)
119 (eshell-previous-prompt 1) 140 (eshell-previous-prompt 1)
120 (should (equal (point) (field-beginning))) 141 (should (equal (point) (field-beginning)))
121 (should (equal (field-string) "echo three\n"))) 142 (should (equal (field-string) "(zerop \"foo\")\n")))
122 (ert-info ("Go back one prompt, starting from the previous prompt's output") 143 (ert-info ("Go back one prompt, starting from the previous prompt's output")
123 (forward-line -1) 144 (forward-line -1)
124 (eshell-previous-prompt 1) 145 (eshell-previous-prompt 1)
125 (should (equal (point) (field-beginning))) 146 (should (equal (point) (field-beginning)))
126 (should (equal (field-string) "echo two\n"))))) 147 (should (equal (field-string) "echo three\n")))))
127 148
128(ert-deftest em-prompt-test/next-previous-prompt () 149(ert-deftest em-prompt-test/next-previous-prompt ()
129 "Check that navigating forward/backward through old prompts works correctly." 150 "Check that navigating forward/backward through old prompts works correctly."
@@ -141,17 +162,20 @@ This tests the case when `eshell-highlight-prompt' is nil."
141 (eshell-insert-command "printnl something else") 162 (eshell-insert-command "printnl something else")
142 (eshell-insert-command "echo two") 163 (eshell-insert-command "echo two")
143 (eshell-insert-command "echo three") 164 (eshell-insert-command "echo three")
165 (let ((debug-on-error nil)) ; A failed command.
166 (eshell-insert-command "(zerop \"foo\")"))
144 (insert "echo fou") ; A partially-entered command. 167 (insert "echo fou") ; A partially-entered command.
145 (ert-info ("Go back one prompt") 168 (ert-info ("Search for \"echo\", back one prompt")
146 (eshell-backward-matching-input "echo" 1) 169 (eshell-backward-matching-input "echo" 1)
147 (should (equal (point) (field-beginning))) 170 (should (equal (point) (field-beginning)))
148 (should (equal (field-string) "echo three\n"))) 171 (should (equal (field-string) "echo three\n")))
149 (ert-info ("Go back two prompts, starting from the end of this line") 172 (ert-info ((concat "Search for \"echo\", back two prompts, "
173 "starting from the end of this line"))
150 (end-of-line) 174 (end-of-line)
151 (eshell-backward-matching-input "echo" 2) 175 (eshell-backward-matching-input "echo" 2)
152 (should (equal (point) (field-beginning))) 176 (should (equal (point) (field-beginning)))
153 (should (equal (field-string) "echo one\n"))) 177 (should (equal (field-string) "echo one\n")))
154 (ert-info ("Go forward three prompts") 178 (ert-info ("Search for \"echo\", forward three prompts")
155 (eshell-forward-matching-input "echo" 3) 179 (eshell-forward-matching-input "echo" 3)
156 (should (equal (point) (field-beginning))) 180 (should (equal (point) (field-beginning)))
157 (should (equal (field-string) "echo fou"))))) 181 (should (equal (field-string) "echo fou")))))