all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jim Porter <jporterbugs@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: christopher@librehacker.com, 73722@debbugs.gnu.org
Subject: bug#73722: 30.0.91; error calling eshell/clear with no argument
Date: Wed, 16 Oct 2024 21:47:58 -0700	[thread overview]
Message-ID: <700c0daa-0b38-a43f-2705-493c5ca61c37@gmail.com> (raw)
In-Reply-To: <b16f3880-519d-ac20-e543-db4d61069051@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 547 bytes --]

On 10/16/2024 11:04 AM, Jim Porter wrote:
> Ok, pushed the one-liner fix to the release branch as d3c94102266. I'll 
> leave this open to work on a better fix for the master branch.

And here's a patch for the master branch. I split out 'eshell/clear' 
into two versions: one for interactive (from M-x) use, and one for use 
as "clear" at the Eshell prompt. For compatibility, you can still call 
'eshell/clear' interactively from M-x, but 99% of the time the 
'eshell/FOO' commands aren't meant to be used that way; hence the new 
'eshell-clear'.

[-- Attachment #2: 0001-Fix-and-improve-behavior-of-eshell-clear.patch --]
[-- Type: text/plain, Size: 6909 bytes --]

From 2e13a01708c4681a51e0bbd16a94d2f052ce53f4 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Thu, 10 Oct 2024 21:03:45 -0700
Subject: [PATCH] Fix and improve behavior of 'eshell/clear'

* lisp/eshell/esh-mode.el (eshell-clear): New function.
(eshell/clear): Fix incorrect behavior, and do the right thing when
'eshell-scroll-show-maximum-output' is nil.
(eshell/clear-scrollback): Call 'eshell/clear'.

* test/lisp/eshell/esh-mode-tests.el
(esh-mode-test/clear/eshell-command)
(esh-mode-test/clear/eshell-command/erase)
(esh-mode-test/clear/emacs-command)
(esh-mode-test/clear/emacs-command/erase): New tests.

* etc/NEWS: Mention the new 'eshell-command' (bug#73722).
---
 etc/NEWS                           |  8 ++++
 lisp/eshell/esh-mode.el            | 61 +++++++++++++++++++++++++-----
 test/lisp/eshell/esh-mode-tests.el | 42 ++++++++++++++++++++
 3 files changed, 101 insertions(+), 10 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 4346fb4aedd..b8aff8cc76f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -236,6 +236,14 @@ It removes all the buttons in the specified region.
 
 ** Eshell
 
+---
+*** New interactive command 'eshell-clear'.
+This command scrolls the screen so that only the current prompt is
+visible, optionally erasing all the previous input/output as well.
+Previously, the Eshell built-in command 'eshell/clear' supported this
+(e.g. to call it via 'M-x'), but this new command behaves more
+consistently if you have a partially-typed command at the Eshell prompt.
+
 ---
 *** New user option 'eshell-command-async-buffer'.
 This option lets you tell 'eshell-command' how to respond if its output
diff --git a/lisp/eshell/esh-mode.el b/lisp/eshell/esh-mode.el
index 34ce82cfbc4..c5bcd28ef51 100644
--- a/lisp/eshell/esh-mode.el
+++ b/lisp/eshell/esh-mode.el
@@ -862,20 +862,61 @@ eshell-show-maximum-output
   (goto-char (point-max))
   (recenter -1))
 
-(defun eshell/clear (&optional scrollback)
-  "Scroll contents of eshell window out of sight, leaving a blank window.
-If SCROLLBACK is non-nil, clear the scrollback contents."
+(defun eshell-clear (&optional clear-scrollback)
+  "Scroll contents of the Eshell window out of sight, leaving a blank window.
+If CLEAR-SCROLLBACK is non-nil (interactively, with the prefix
+argument), clear the scrollback contents.
+
+Otherwise, the behavior depends on `eshell-scroll-show-maximum-output'.
+If non-nil, fill newlines before the current prompt so that the prompt
+is the last line in the window; if nil, just scroll the window so that
+the prompt is the first line in the window."
+  (interactive "P")
+  (cond
+   (clear-scrollback
+    (let ((inhibit-read-only t))
+      (widen)
+      (delete-region (point-min) (eshell-end-of-output))))
+   (eshell-scroll-show-maximum-output
+    (save-excursion
+      (goto-char (eshell-end-of-output))
+      (let ((inhibit-read-only t))
+        (insert-and-inherit (make-string (window-size) ?\n))))
+    (when (< (point) eshell-last-output-end)
+      (goto-char eshell-last-output-end)))
+  (t
+   (when (< (point) eshell-last-output-end)
+     (goto-char eshell-last-output-end))
+   (set-window-start nil (eshell-end-of-output)))))
+
+(defun eshell/clear (&optional clear-scrollback)
+  "Scroll contents of the Eshell window out of sight, leaving a blank window.
+If CLEAR-SCROLLBACK is non-nil, clear the scrollback contents.
+
+Otherwise, the behavior depends on `eshell-scroll-show-maximum-output'.
+If non-nil, fill newlines before the current prompt so that the prompt
+is the last line in the window; if nil, just scroll the window so that
+the prompt is the first line in the window.
+
+This command is for use as an Eshell command (entered at the prompt);
+for clearing the Eshell buffer from elsewhere (e.g. via
+\\[execute-extended-command]), use `eshell-clear'."
   (interactive)
-  (if scrollback
-      (eshell/clear-scrollback)
+  (cond
+   ((null eshell-current-handles)
+    (eshell-clear clear-scrollback))
+   (clear-scrollback
+    (let ((inhibit-read-only t))
+      (erase-buffer)))
+   (eshell-scroll-show-maximum-output
     (let ((eshell-input-filter-functions nil))
-      (insert (make-string (window-size) ?\n))
-      (eshell-send-input))))
+      (ignore (eshell-interactive-print (make-string (window-size) ?\n)))))
+   (t
+    (recenter 0))))
 
 (defun eshell/clear-scrollback ()
-  "Clear the scrollback content of the eshell window."
-  (let ((inhibit-read-only t))
-    (erase-buffer)))
+  "Clear the scrollback content of the Eshell window."
+  (eshell/clear t))
 
 (defun eshell-get-old-input (&optional use-current-region)
   "Return the command input on the current line.
diff --git a/test/lisp/eshell/esh-mode-tests.el b/test/lisp/eshell/esh-mode-tests.el
index 306e11ce445..28839eb65cf 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,44 @@ 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/eshell-command/erase ()
+  "Test that `eshell/clear' can erase the buffer."
+  (let ((eshell-banner-message "")
+        (eshell-prompt-function (lambda () "$ ")))
+    (with-temp-eshell
+      (eshell-insert-command "echo hi")
+      (eshell-insert-command "clear t")
+      (should (string-match "\\`\\$ " (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))))))
+
+(ert-deftest esh-mode-test/clear/emacs-command/erase ()
+  "Test that `eshell-clear' can erase the buffer."
+  (let ((eshell-banner-message "")
+        (eshell-prompt-function (lambda () "$ ")))
+    (with-temp-eshell
+      (eshell-insert-command "echo hi")
+      (insert "echo b")
+      (eshell-clear t)
+      (should (string-match "\\`\\$ echo b" (buffer-string))))))
+
 ;; esh-mode-tests.el ends here
-- 
2.25.1


  reply	other threads:[~2024-10-17  4:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-09 17:33 bug#73722: 30.0.91; error calling eshell/clear with no argument Christopher Howard
2024-10-15 17:00 ` Jim Porter
2024-10-15 18:04   ` Eli Zaretskii
2024-10-15 21:31     ` Jim Porter
2024-10-16  5:17       ` Eli Zaretskii
2024-10-16 18:04         ` Jim Porter
2024-10-17  4:47           ` Jim Porter [this message]
2024-11-01  5:40             ` Jim Porter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=700c0daa-0b38-a43f-2705-493c5ca61c37@gmail.com \
    --to=jporterbugs@gmail.com \
    --cc=73722@debbugs.gnu.org \
    --cc=christopher@librehacker.com \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.