From 08901e93797cd9e338883ab6153d98f15bc2ccfb Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Wed, 23 Aug 2023 13:29:19 -0700 Subject: [PATCH] Support 'comint-pager' in Eshell * lisp/eshell/esh-var.el (eshell-variable-aliases-list): Add "PAGER". (eshell-var-initialize): Make 'comint-pager' buffer-local and bind it in subcommands so that we can temporarily set it as necessary. * test/lisp/eshell/esh-var-tests.el (esh-var-test/pager-var/default) (esh-var-test/pager-var/set, esh-var-test/pager-var/unset) (esh-var-test/pager-var/set-locally): New tests. * doc/misc/eshell.texi (Variables): Document this (bug#63778). Co-authored-by: Morgan Smith --- doc/misc/eshell.texi | 7 +++++ lisp/eshell/esh-var.el | 11 +++++++- test/lisp/eshell/esh-var-tests.el | 46 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi index 4897b410b76..f8f60bae13a 100644 --- a/doc/misc/eshell.texi +++ b/doc/misc/eshell.texi @@ -1094,6 +1094,13 @@ necessary. By default, its value is @code{@var{emacs-version},eshell}. Other parts of Emacs, such as Tramp, may add extra information to this value. +@vindex $PAGER +@item $PAGER +This variable indicates the pager that commands should use when they +wish to paginate long output. Its value is that of +@code{comint-pager} if non-@code{nil}; otherwise, it uses the value of +@code{$PAGER} from the @code{process-environment}. + @end table @xref{Aliases}, for the built-in variables @samp{$*}, @samp{$1}, diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el index c7c0a21d2a9..711c35f8527 100644 --- a/lisp/eshell/esh-var.el +++ b/lisp/eshell/esh-var.el @@ -162,6 +162,13 @@ if they are quoted with a backslash." ("COLUMNS" ,(lambda () (window-body-width nil 'remap)) t t) ("LINES" ,(lambda () (window-body-height nil 'remap)) t t) ("INSIDE_EMACS" eshell-inside-emacs t) + ("PAGER" (,(lambda () (or comint-pager (getenv "PAGER"))) + . ,(lambda (_ value) + ;; When unsetting PAGER, be sure to clear its value + ;; from `process-environment' too. + (unless value (setenv "PAGER")) + (setq comint-pager value))) + t t) ("UID" ,(lambda () (file-user-uid)) nil t) ("GID" ,(lambda () (file-group-gid)) nil t) @@ -262,11 +269,13 @@ copied (a.k.a. \"exported\") to the environment of created subprocesses." ;; changing a variable will affect all of Emacs. (unless eshell-modify-global-environment (setq-local process-environment (eshell-copy-environment))) + (make-local-variable 'comint-pager) (setq-local eshell-subcommand-bindings (append '((process-environment (eshell-copy-environment)) (eshell-variable-aliases-list eshell-variable-aliases-list) - (eshell-path-env-list eshell-path-env-list)) + (eshell-path-env-list eshell-path-env-list) + (comint-pager comint-pager)) eshell-subcommand-bindings)) (setq-local eshell-special-chars-inside-quoting diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el index 3e58fe749dd..ff646f5f977 100644 --- a/test/lisp/eshell/esh-var-tests.el +++ b/test/lisp/eshell/esh-var-tests.el @@ -766,6 +766,52 @@ it, since the setter is nil." (eshell-match-command-output "echo $INSIDE_EMACS[, 1]" "eshell"))) +(ert-deftest esh-var-test/pager-var/default () + "Test that retrieving the default value of $PAGER works. +This should be the value of `comint-pager' if non-nil, otherwise +the value of the $PAGER env var." + (let ((comint-pager nil) + (process-environment (cons "PAGER=cat" process-environment))) + (eshell-command-result-equal "echo $PAGER" "cat") + (setq comint-pager "less") + (eshell-command-result-equal "echo $PAGER" "less"))) + +(ert-deftest esh-var-test/pager-var/set () + "Test that setting $PAGER in Eshell overrides the default value." + (let ((comint-pager nil) + (process-environment (cons "PAGER=cat" process-environment))) + (with-temp-eshell + (eshell-match-command-output "set PAGER bat" "bat") + (eshell-match-command-output "echo $PAGER" "bat")) + (setq comint-pager "less") + (with-temp-eshell + (eshell-match-command-output "set PAGER bat" "bat") + (eshell-match-command-output "echo $PAGER" "bat")))) + +(ert-deftest esh-var-test/pager-var/unset () + "Test that unsetting $PAGER in Eshell overrides the default value." + (let ((comint-pager nil) + (process-environment (cons "PAGER=cat" process-environment))) + (with-temp-eshell + (eshell-insert-command "unset PAGER") + (eshell-match-command-output "echo $PAGER" "\\`\\'")) + (setq comint-pager "less") + (with-temp-eshell + (eshell-insert-command "unset PAGER") + (eshell-match-command-output "echo $PAGER" "\\`\\'")))) + +(ert-deftest esh-var-test/pager-var/set-locally () + "Test setting $PAGER temporarily for a single command." + (let ((comint-pager nil) + (process-environment (cons "PAGER=cat" process-environment))) + (with-temp-eshell + (eshell-match-command-output "PAGER=bat env" "PAGER=bat\n") + (eshell-match-command-output "echo $PAGER" "cat")) + (setq comint-pager "less") + (with-temp-eshell + (eshell-match-command-output "PAGER=bat env" "PAGER=bat\n") + (eshell-match-command-output "echo $PAGER" "less")))) + (ert-deftest esh-var-test/path-var/local-directory () "Test using $PATH in a local directory." (let ((expected-path (string-join (eshell-get-path t) (path-separator)))) -- 2.39.2