unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: miha--- via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 49484@debbugs.gnu.org, Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#49484: 27.2; [PATCH] Undoing a 'RET' in comint and eshell
Date: Sun, 18 Jul 2021 09:42:28 +0200	[thread overview]
Message-ID: <86zgukf4pn.fsf@miha-pc> (raw)
In-Reply-To: <87fswmnmsl.fsf@gnus.org>


[-- Attachment #1.1: Type: text/plain, Size: 725 bytes --]

Lars Ingebrigtsen <larsi@gnus.org> writes:

> miha@kamnitnik.top writes:
>
>> My idea to solve this is to record process mark and related marker
>> positions as `apply' entries in the undo list. Attached patch implements
>> this for comint and eshell.
>
> Hm, interesting...  The patch looks good to me, but I'm not really that
> familiar with undo internals myself, so it'd be good to get more
> opinions on this first.  So I've added Stefan to the CCs.

So after thinking about this some more, I arrived at a simpler solution:
deleting and reinserting text to generate suitable undo list entries
instead of adding them explicitly.
As opposed to the first patch, this one should also handle
undo-in-region reasonably well.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-Improve-undoing-of-RET-in-comint-and-eshell.patch --]
[-- Type: text/x-patch, Size: 3680 bytes --]

From ad98e21545e5d248e13ef8b124b42ca4f0215f6e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= <miha@kamnitnik.top>
Date: Fri, 16 Jul 2021 17:08:12 +0200
Subject: [PATCH] Improve undoing of RET in comint and eshell

* lisp/comint.el (comint-send-input):
(comint-accumulate):
* lisp/eshell/esh-mode.el (eshell-send-input): Before sending input to
the process, delete it and reinsert it again.  Undoing this
insertion with 'C-/' will delete the region, moving the process mark
back to its original position.
---
 lisp/comint.el          | 24 +++++++++++++++++++++++-
 lisp/eshell/esh-mode.el |  8 ++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index 9e406614b9..f24ec4b6bf 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -1890,6 +1890,14 @@ comint-send-input
                           (delete-region pmark start)
                           copy))))
 
+        ;; Delete and reinsert input.  This seems like a no-op, except
+        ;; for the resulting entries in the undo list: undoing this
+        ;; insertion will delete the region, moving the process mark
+        ;; back to its original position.
+        (let ((inhibit-read-only t))
+          (delete-region pmark (point))
+          (insert input))
+
         (unless no-newline
           (insert ?\n))
 
@@ -1933,7 +1941,7 @@ comint-send-input
         ;; in case we get output amidst sending the input.
         (set-marker comint-last-input-start pmark)
         (set-marker comint-last-input-end (point))
-        (set-marker (process-mark proc) (point))
+        (set-marker pmark (point))
         ;; clear the "accumulation" marker
         (set-marker comint-accum-marker nil)
         (let ((comint-input-sender-no-newline no-newline))
@@ -3489,6 +3497,20 @@ comint-accumulate
 The entire accumulated text becomes one item in the input history
 when you send it."
   (interactive)
+  (when-let* ((proc (get-buffer-process (current-buffer)))
+              (pmark (process-mark proc))
+              ((or (marker-position comint-accum-marker)
+                   (set-marker comint-accum-marker pmark)
+                   t))
+              ((>= (point) comint-accum-marker pmark)))
+    ;; Delete and reinsert input.  This seems like a no-op, except for
+    ;; the resulting entries in the undo list: undoing this insertion
+    ;; will delete the region, moving the accumulation marker back to
+    ;; its original position.
+    (let ((text (buffer-substring comint-accum-marker (point)))
+          (inhibit-read-only t))
+      (delete-region comint-accum-marker (point))
+      (insert text)))
   (insert "\n")
   (set-marker comint-accum-marker (point))
   (if comint-input-ring-index
diff --git a/lisp/eshell/esh-mode.el b/lisp/eshell/esh-mode.el
index f9dbce9770..92e1e9eb6a 100644
--- a/lisp/eshell/esh-mode.el
+++ b/lisp/eshell/esh-mode.el
@@ -614,6 +614,14 @@ eshell-send-input
 		  (and eshell-send-direct-to-subprocesses
 		       proc-running-p))
 	(insert-before-markers-and-inherit ?\n))
+      ;; Delete and reinsert input.  This seems like a no-op, except
+      ;; for the resulting entries in the undo list: undoing this
+      ;; insertion will delete the region, moving the process mark
+      ;; back to its original position.
+      (let ((text (buffer-substring eshell-last-output-end (point)))
+            (inhibit-read-only t))
+        (delete-region eshell-last-output-end (point))
+        (insert text))
       (if proc-running-p
 	  (progn
 	    (eshell-update-markers eshell-last-output-end)
-- 
2.32.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

  reply	other threads:[~2021-07-18  7:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09  9:24 bug#49484: 27.2; [PATCH] Undoing a 'RET' in comint and eshell miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-07-10 16:42 ` Lars Ingebrigtsen
2021-07-18  7:42   ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2021-11-07 23:11     ` Lars Ingebrigtsen

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=86zgukf4pn.fsf@miha-pc \
    --to=bug-gnu-emacs@gnu.org \
    --cc=49484@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=miha@kamnitnik.top \
    --cc=monnier@iro.umontreal.ca \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).