Subject: Re: 29.0.60; comint-mode redirection

From: Rah Guzar <[email-masked]>
Date: Sun, 09 Apr 2023 12:04:28 +0200

Dear Emacs maintainers,
  Please find attached a patch that addresses these points. If desired I
  can also separate out the documentation changes which I think are more
  important from the code changes. I think even code changes are trivial
  and safe. Please let me know if any changes are required.

  I have not yet signed the copyright assignment but I am in the process
  of doing it. I think this change falls within the 15 lines exemption
  although it saturates it.

Thanks,
Rah Guzar

Rah Guzar <[email-masked]> writes:

> Dear Emacs maintainers,
>    I have been working on a major mode derived from comint-mode. The mode
>    uses redirection to show documentation in a separate buffer. Sometimes,
>    a prompt would appear in the output of the command producing the
>    documentation and this would end the redirection prematurely. Searching
>    among variables in comint-mode I found comint-redirect-finished-regexp
>    and its docstring made me believe that I could set it to change the end
>    of redirection. However setting it had a no effect and looking at the
>    sources I found that this is because it is set by comint-redirect-setup
>    and comint-redirect-send-command-to-process calls comint-redirect-setup
>    so that it is always set to comint-prompt-regexp during redirection.
>
>    This is confusing behavior because docstring of
>    comint-redirect-finished-regexp gives no clue that setting it will have
>    no effect. Similarly the docstring of comint-prompt-regexp states that
>    its value is only used when comint-use-prompt-regexp is non-nil but
>    that is not true for redirection.
>
>    Overall I think it would be nice to have an optional argument to
>    comint-redirect-send-command-to-process which allows a user to
>    explicitly pass the regexp to use for ending redirection.
>
>    Another issue I had was that I wanted to run some commands when
>    redirection ended. There didn't seem to be any hooks that accomplished
>    this but from source of comint-redirect-preoutput-filter I found that
>    comint-redirect-hook is run when redirection finishes. However
>    comint-redirect-hook is not defined as a variable and nothing seems to
>    give any hint of its existence.
>
> Thanks,
> Rahguzar
>
>
> In GNU Emacs 29.0.60 (build 1, x86_64-suse-linux-gnu, GTK+ Version
>  3.24.35, cairo version 1.17.6) of 2023-02-17 built on build79
> System Description: openSUSE Tumbleweed
>
> Configured using:
>  'configure --host=x86_64-suse-linux-gnu --build=x86_64-suse-linux-gnu
>  --program-prefix= --disable-dependency-tracking --prefix=/usr
>  --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin
>  --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include
>  --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var
>  --sharedstatedir=/var/lib --mandir=/usr/share/man
>  --infodir=/usr/share/info --with-pgtk --with-native-compilation=aot
>  --with-cairo --with-libotf --with-jpeg --with-tiff --with-gif
>  --with-png --with-rsvg --with-xft --with-xml2 --with-dbus --with-sound
>  --with-json --with-mailutils --with-gnutls --with-tree-sitter
>  --enable-locallisppath=/usr/share/emacs/29.0.60/site-lisp:/usr/share/emacs/site-lisp
>  'CFLAGS=-O2 -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
>  -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables
>  -fstack-clash-protection -Werror=return-type -flto=auto'
>  LDFLAGS=-Wl,-O2'
>
> Configured features:
> CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
> LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY
> PDUMPER PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
> TOOLKIT_SCROLL_BARS TREE_SITTER XIM GTK3 ZLIB
>
> Important settings:
>   value of $LANG: en_US.UTF-8
>   locale-coding-system: utf-8-unix
>
> Major mode: mu4e-headers
>
> Minor modes in effect:
>   delete-selection-mode: t
>   projectile-mode: t
>   recentf-mode: t
>   save-place-mode: t
>   global-so-long-mode: t
>   which-key-mode: t
>   savehist-mode: t
>   better-jumper-mode: t
>   better-jumper-local-mode: t
>   global-company-mode: t
>   company-mode: t
>   vertico-mode: t
>   all-the-icons-completion-mode: t
>   marginalia-mode: t
>   evil-goggles-mode: t
>   evil-escape-mode: t
>   evil-snipe-override-mode: t
>   evil-snipe-mode: t
>   gcmh-mode: t
>   winner-mode: t
>   smartparens-global-mode: t
>   ws-butler-global-mode: t
>   global-undo-fu-session-mode: t
>   undo-fu-mode: t
>   workroom-mode: t
>   mu4e-search-minor-mode: t
>   global-hl-line-mode: t
>   hl-line-mode: t
>   mu4e-update-minor-mode: t
>   mu4e-context-minor-mode: t
>   dirvish-override-dired-mode: t
>   server-mode: t
>   solaire-global-mode: t
>   solaire-mode: t
>   shell-dirtrack-mode: t
>   evil-mode: t
>   evil-local-mode: t
>   windmove-mode: t
>   +popup-mode: t
>   +modeline-global-mode: t
>   +modeline-mode: t
>   override-global-mode: t
>   general-override-mode: t
>   global-eldoc-mode: t
>   eldoc-mode: t
>   show-paren-mode: t
>   electric-indent-mode: t
>   mouse-wheel-mode: t
>   file-name-shadow-mode: t
>   global-font-lock-mode: t
>   font-lock-mode: t
>   window-divider-mode: t
>   buffer-read-only: t
>   size-indication-mode: t
>   line-number-mode: t
>   transient-mark-mode: t
>   auto-composition-mode: t
>   auto-encryption-mode: t
>   auto-compression-mode: t
>   abbrev-mode: t
>
> Load-path shadows:
> /home/azeem/.emacs.d/.local/straight/build-29.0.60/citar-embark/citar-embark hides /home/azeem/.emacs.d/.local/straight/build-29.0.60/citar/citar-embark
>
> Features:
> (shadow disp-table whitespace delsel display-line-numbers adaptive-wrap
> spell-fu ispell auto-minor-mode projectile lisp-mnt evil-collection-grep
> grep ibuf-ext evil-collection-ibuffer ibuffer ibuffer-loaddefs recentf
> tree-widget saveplace evil-collection-so-long so-long emacsbug sort
> gnus-cite smiley shr-color mm-archive mail-extr textsec uni-scripts
> idna-mapping ucs-normalize uni-confusable textsec-check qp cursor-sensor
> vertico-repeat evil-collection-which-key which-key savehist
> better-jumper company-capf company evil-collection-vertico vertico
> orderless all-the-icons-completion marginalia evil-goggles pulse color
> evil-easymotion evil-escape evil-snipe autorevert filenotify gcmh winner
> smartparens-config smartparens-text smartparens dash ws-butler
> undo-fu-session undo-fu workroom compat desktop frameset
> evil-collection-mu4e mu4e mu4e-org evil-collection-org smartparens-org
> org ob ob-tangle ob-ref ob-lob ob-table ob-exp org-macro org-src
> ob-comint org-pcomplete org-list org-footnote org-faces org-entities
> noutline outline ob-emacs-lisp org-table ol org-keys oc-natbib oc-csl
> bibtex oc-biblatex oc org-loaddefs find-func mu4e-main mu4e-view
> gnus-art mm-uu mml2015 mm-view mml-smime smime gnutls dig gnus-sum
> gnus-group gnus-undo gnus-start gnus-dbus dbus gnus-cloud nnimap nnmail
> mail-source utf7 nnoo parse-time iso8601 gnus-spec gnus-int gnus-range
> gnus-win evil-collection-gnus gnus nnheader range
> evil-collection-calendar cal-menu calendar cal-loaddefs mu4e-headers
> mu4e-compose mu4e-draft mu4e-actions smtpmail mu4e-search mu4e-lists
> mu4e-bookmarks mu4e-mark mu4e-message shr pixel-fill kinsoku url-file
> svg xml dom browse-url url url-proxy url-privacy url-expand url-methods
> url-history url-cookie generate-lisp-file url-domsuf url-util url-parse
> auth-source json map url-vars flow-fill mule-util hl-line mu4e-contacts
> mu4e-update mu4e-folders mu4e-server mu4e-context mu4e-vars mu4e-helpers
> mu4e-config evil-collection-bookmark bookmark ido message sendmail
> mailcap yank-media puny dirvish transient eieio eieio-core
> evil-collection-dired dired dired-loaddefs rfc822 mml mml-sec
> password-cache evil-collection-epa epa epg rfc6068 epg-config gnus-util
> time-date mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047
> rfc2045 mm-util ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils
> mailheader server dtrt-indent doom-themes-ext-org solaire-mode
> face-remap doom-earl-grey-theme doom-themes doom-themes-base midnight
> evil-collection-flymake flymake-proc flymake project
> evil-collection-compile evil-collection-custom cus-edit cus-load
> wid-edit evil-collection-comint evil-collection annalist compile
> text-property-search html2text ob-core org-cycle org-fold org-fold-core
> org-compat ob-eval org-version org-macs format-spec ibuf-macs evil
> evil-integration evil-maps evil-commands reveal flyspell evil-jumps
> evil-command-window evil-search shell pcomplete comint ansi-osc
> ansi-color evil-types evil-macros evil-repeat evil-states evil-core comp
> comp-cstr warnings icons byte-opt advice evil-common windmove calc
> calc-loaddefs calc-macs thingatpt rect evil-digraphs evil-vars ring
> all-the-icons all-the-icons-faces data-material data-weathericons
> data-octicons data-fileicons data-faicons data-alltheicons let-alist
> derived edmacro kmacro use-package-bind-key bind-key rx doom-editor
> doom-projects doom-ui easy-mmode doom-keybinds pp cl-extra help-mode
> use-package-core bytecomp byte-compile general tex-site doom-start
> doom-modules cl-seq doom doom-lib cl-macs cl-loaddefs cl-lib pcase gv
> jansson dynamic-modules subr-x rmc iso-transl tooltip cconv eldoc paren
> electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
> term/pgtk-win pgtk-win term/common-win pgtk-dnd tool-bar dnd fontset
> image regexp-opt fringe tabulated-list replace newcomment text-mode
> lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow isearch
> easymenu timer select scroll-bar mouse jit-lock font-lock syntax
> font-core term/tty-colors frame minibuffer nadvice seq simple cl-generic
> indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
> tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
> romanian slovak czech european ethiopic indian cyrillic chinese
> composite emoji-zwj charscript charprop case-table epa-hook
> jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
> theme-loaddefs faces cus-face macroexp files window text-properties
> overlay sha1 md5 base64 format env code-pages mule custom widget keymap
> hashtable-print-readable backquote threads dbusbind inotify
> dynamic-setting system-font-setting font-render-setting cairo gtk pgtk
> multi-tty make-network-process native-compile emacs)
>
> Memory information:
> ((conses 16 944541 309286)
>  (symbols 48 41240 1)
>  (strings 32 291783 19116)
>  (string-bytes 1 6577740)
>  (vectors 16 85429)
>  (vector-slots 8 2190100 207157)
>  (floats 8 1276 1590)
>  (intervals 56 12251 9085)
>  (buffers 984 21))

diff --git a/lisp/comint.el b/lisp/comint.el
index 682b555a33c..98f4d315d64 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -161,7 +161,10 @@ comint-prompt-regexp
 Defaults to \"^\", the null string at BOL.
 
 This variable is only used if the variable
-`comint-use-prompt-regexp' is non-nil.
+`comint-use-prompt-regexp' is non-nil.  The exception to
+this is redirection.  Many commands including
+`comint-redirect-send-command-to-process' use it as
+`comint-redirect-finished-regexp'.
 
 Good choices:
   Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
@@ -3637,7 +3640,12 @@ comint-redirect-output-buffer
 (defvar comint-redirect-finished-regexp nil
   "Regular expression that determines when to stop redirection in Comint.
 When the redirection filter function is given output that matches this regexp,
-the output is inserted as usual, and redirection is completed.")
+the output is inserted as usual, and redirection is completed.
+This is an internal variable set by `comint-redirect-setup' and setting it
+directly has no effect.")
+
+(defvar comint-redirect-hook nil
+  "Hook run when a redirection finishes.")
 
 (defvar comint-redirect-insert-matching-regexp nil
   "If non-nil, the text that ends a redirection is included in it.
@@ -3833,11 +3841,13 @@ comint-redirect-send-command
 
 ;;;###autoload
 (defun comint-redirect-send-command-to-process
-  (command output-buffer process echo &optional no-display)
+  (command output-buffer process echo &optional no-display finished-regexp)
   "Send COMMAND to PROCESS, with output to OUTPUT-BUFFER.
 With prefix arg, echo output in process buffer.
 
-If NO-DISPLAY is non-nil, do not show the output buffer."
+If NO-DISPLAY is non-nil, do not show the output buffer.
+If FINISHED-REGEXP is non-nil it is used as `comint-redirect-finished-regexp'
+instead of `comint-prompt-regexp'."
   (interactive "sCommand: \nBOutput Buffer: \nbProcess Buffer: \nP")
   (let* (;; The process buffer
 	 (process-buffer (if (processp process)
@@ -3858,7 +3868,7 @@ comint-redirect-send-command-to-process
       (comint-redirect-setup
        output-buffer
        (current-buffer)                 ; Comint Buffer
-       comint-prompt-regexp             ; Finished Regexp
+       (or finished-regexp comint-prompt-regexp)             ; Finished Regexp
        echo)                            ; Echo input
 
       ;; Set the filter.