Subject: bug#73722: 30.0.91; error calling eshell/clear with no argument

From: Jim Porter <[email-masked]>
Date: Tue, 15 Oct 2024 10:00:40 -0700

On 10/9/2024 10:33 AM, Christopher Howard wrote:
> 
> (same with or without emacs -Q)
> 1. M-x eshell
> 2. clear <return>
> 
> Gives error "wrong-type-argument listp progn".

I think this is probably the result of some improvements I've made to 
Eshell's command evaluator that revealed a latent bug in 'eshell/clear' 
(it actually runs a *second* Eshell command consisting only of newlines, 
which shouldn't be necessary).

The actual error reported here is fixed in Emacs 31; in Emacs 30, 
'eshell-do-eval' mis-evaluates an empty '(progn)', but that generally 
shouldn't occur except in odd cases like this. The "clear" command still 
doesn't *work* correctly in Emacs 31 though.

Attached is the smallest fix I could come up with for this. This is made 
somewhat more complex due to 'eshell/clear' also being used as an 
interactive Emacs-level command. The two forms ("clear" at an Eshell 
prompt and "M-x eshell/clear" in Emacs) should probably be separate 
functions entirely since they behave differently regarding the current 
prompt, but for the sake of compatibility, let's keep them fused 
together for now...

Eli, does this look ok for the release branch? It fixes a regression 
from Emacs 29 and adds a pair of regression tests to prevent future 
breakage. (If and when this merges, I'll see about a more-thorough fix 
for the master branch too.)

From 4c868dbad546b604076ab7c33839b4d3b5919f32 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Thu, 10 Oct 2024 21:03:45 -0700
Subject: [PATCH] Fix error when calling "clear" in Eshell.

* lisp/eshell/esh-mode.el (eshell/clear): Fix error, and improve
handling for using as an interactive Emacs command.

* test/lisp/eshell/esh-mode-tests.el
(esh-mode-test/clear/eshell-command, esh-mode-test/clear/emacs-command):
New tests.
---
 lisp/eshell/esh-mode.el            |  7 +++++--
 test/lisp/eshell/esh-mode-tests.el | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/lisp/eshell/esh-mode.el b/lisp/eshell/esh-mode.el
index 34ce82cfbc4..78e43e84173 100644
--- a/lisp/eshell/esh-mode.el
+++ b/lisp/eshell/esh-mode.el
@@ -869,8 +869,11 @@ eshell/clear
   (if scrollback
       (eshell/clear-scrollback)
     (let ((eshell-input-filter-functions nil))
-      (insert (make-string (window-size) ?\n))
-      (eshell-send-input))))
+      (eshell-interactive-print (make-string (window-size) ?\n))
+      (when (and (null eshell-current-handles)
+                 (eshell-using-module 'eshell-prompt))
+        (declare-function eshell-emit-prompt "em-prompt" ())
+        (eshell-emit-prompt)))))
 
 (defun eshell/clear-scrollback ()
   "Clear the scrollback content of the eshell window."
diff --git a/test/lisp/eshell/esh-mode-tests.el b/test/lisp/eshell/esh-mode-tests.el
index 306e11ce445..896931c47f6 100644
--- a/test/lisp/eshell/esh-mode-tests.el
+++ b/test/lisp/eshell/esh-mode-tests.el
@@ -26,6 +26,8 @@
 (require 'ert)
 (require 'esh-mode)
 (require 'eshell)
+(require 'em-banner)
+(require 'em-prompt)
 
 (require 'eshell-tests-helpers
          (expand-file-name "eshell-tests-helpers"
@@ -59,4 +61,25 @@ esh-mode-test/handle-control-codes/backspace
     (eshell-match-command-output (format "(format \"hello%c%cp\")" ?\C-h ?\C-h)
                                  "\\`help\n")))
 
+(ert-deftest esh-mode-test/clear/eshell-command ()
+  "Test that `eshell/clear' works as an Eshell command."
+  (let ((eshell-banner-message "")
+        (eshell-prompt-function (lambda () "$ ")))
+    (with-temp-eshell
+      (eshell-insert-command "echo hi")
+      (eshell-insert-command "clear")
+      (should (string-match "\\$ echo hi\nhi\n\\$ clear\n+\\$ "
+                            (buffer-string))))))
+
+(ert-deftest esh-mode-test/clear/emacs-command ()
+  "Test that `eshell/clear' works as an interactive Emacs command."
+  (let ((eshell-banner-message "")
+        (eshell-prompt-function (lambda () "$ ")))
+    (with-temp-eshell
+      (eshell-insert-command "echo hi")
+      (insert "echo b")
+      (eshell/clear)
+      (should (string-match "\\$ echo hi\nhi\n\\$ \n+\\$ echo b"
+                            (buffer-string))))))
+
 ;; esh-mode-tests.el ends here
-- 
2.25.1