Subject: bug#56646: User option to disable deleting current input when using mouse-2 in comint

From: Visuwesh <[email-masked]>
Date: Tue, 19 Jul 2022 22:04:09 +0530

Tags: patch

mouse-2 in comint can be used to reinsert old inputs however, it
unconditionally deletes the current (unsent) input.  This makes it
impossible to use the mouse-2 command to further act on the previous
input.  For example, consider you're doing something like this in a
Python REPL:

    >>> foo()
    ... some output... you verify that it is okay
    >>> bar(|

With the point being |, you say mouse-2 when the pointer is over foo().
Currently comint-mode makes the last prompt become

    >>> foo()

which is not so helpful.  Attached patch adds a new user option to turn
off this deletion so you may get...

    >>> bar(foo()|

instead, which, IMHO, is more useful.

In GNU Emacs 29.0.50 (build 22, x86_64-pc-linux-gnu, X toolkit, Xaw scroll bars)
 of 2022-07-17 built on astatine
Repository revision: 5db4ec20fe4b428e15aa53208c9a9bfb037c0f30
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101003
System Description: Debian GNU/Linux bookworm/sid

Configured using:
 'configure --with-sound=alsa --with-x-toolkit=lucid --with-json
 --without-xaw3d --without-gconf --without-libsystemd --without-cairo'

>From 6d2631be4582b38b1bf4eb78d3b8bbd6c687b0d6 Mon Sep 17 00:00:00 2001
From: Visuwesh <[email-masked]>
Date: Tue, 19 Jul 2022 21:56:02 +0530
Subject: [PATCH] New user option to disable deleting current input in comint
 mouse-2

* lisp/comint.el (comint-delete-old-input): New user option to disable
deleting current input when insert an old input using mouse-2.
(comint-insert-input): Use it.

* etc/NEWS: Announce the user option.
---
 etc/NEWS       |  5 +++++
 lisp/comint.el | 15 +++++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 604e30ce25..213b358cc2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -990,6 +990,11 @@ The user option 'comint-terminfo-terminal' and the variable
 'system-uses-terminfo' can now be set as connection-local variables to
 change the terminal used on a remote host.
 
+---
+*** New user option 'comint-delete-old-input'
+When set to nil, this prevents comint from deleting the current input
+when inserting previous input using '<mouse-2>'.
+
 ** Mwheel
 
 ---
diff --git a/lisp/comint.el b/lisp/comint.el
index d52623c00a..3ed04f098c 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -905,6 +905,12 @@ comint-nonblank-p
   "Return non-nil if STR contains non-whitespace syntax."
   (not (string-match "\\`\\s *\\'" str)))
 
+(defcustom comint-delete-old-input t
+  "When non-nil, delete old input on inserting previous input with \\<comint-mode-map>\\[comint-insert-input]."
+  :type 'boolean
+  :group 'comint
+  :version "29.1")
+
 (defun comint-insert-input (event)
   "In a Comint buffer, set the current input to the previous input at point.
 If there is no previous input at point, run the command specified
@@ -936,10 +942,11 @@ comint-insert-input
         ;; Otherwise, insert the previous input.
         (goto-char (point-max))
         ;; First delete any old unsent input at the end
-        (delete-region
-         (or (marker-position comint-accum-marker)
-             (process-mark (get-buffer-process (current-buffer))))
-         (point))
+        (when comint-delete-old-input
+          (delete-region
+           (or (marker-position comint-accum-marker)
+               (process-mark (get-buffer-process (current-buffer))))
+           (point)))
         ;; Insert the input at point
         (insert input)))))
 
-- 
2.35.1