From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: miha--- via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#49484: 27.2; [PATCH] Undoing a 'RET' in comint and eshell Date: Fri, 09 Jul 2021 11:24:11 +0200 Message-ID: <87eec7c01w.fsf@miha-pc> Reply-To: miha@kamnitnik.top Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23000"; mail-complaints-to="usenet@ciao.gmane.io" To: 49484@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 09 11:23:22 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1mjC-0005nA-1l for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 09 Jul 2021 11:23:22 +0200 Original-Received: from localhost ([::1]:58422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m1mjB-0000G1-4D for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 09 Jul 2021 05:23:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48138) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m1mit-0000Fs-Q8 for bug-gnu-emacs@gnu.org; Fri, 09 Jul 2021 05:23:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:46811) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m1mit-0002VG-9Y for bug-gnu-emacs@gnu.org; Fri, 09 Jul 2021 05:23:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m1mis-00042o-Lj for bug-gnu-emacs@gnu.org; Fri, 09 Jul 2021 05:23:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: miha@kamnitnik.top Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 09 Jul 2021 09:23:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 49484 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.162582255415488 (code B ref -1); Fri, 09 Jul 2021 09:23:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 9 Jul 2021 09:22:34 +0000 Original-Received: from localhost ([127.0.0.1]:58356 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m1miP-00041k-Lj for submit@debbugs.gnu.org; Fri, 09 Jul 2021 05:22:34 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:45922) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m1miK-00041X-RU for submit@debbugs.gnu.org; Fri, 09 Jul 2021 05:22:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48076) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m1miK-0000Ez-Kl for bug-gnu-emacs@gnu.org; Fri, 09 Jul 2021 05:22:28 -0400 Original-Received: from kamnitnik.top ([209.250.245.214]:44166 helo=mail.kamnitnik.top) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m1miH-0002DE-RQ for bug-gnu-emacs@gnu.org; Fri, 09 Jul 2021 05:22:28 -0400 Original-Received: from localhost (unknown [IPv6:2a00:ee2:e04:9300:e609:6c46:d026:8c47]) by mail.kamnitnik.top (Postfix) with ESMTPSA id 28526BBB71 for ; Fri, 9 Jul 2021 09:22:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kamnitnik.top; s=mail; t=1625822535; bh=Age9peYTinAecLUOoMJOo/9L0tvjsni+ljDgCxdh0sU=; h=From:To:Subject:Date:From; b=gnp6/9Y64D1YeNZ58JdIMPWLAa7ap+lq5OndbGB3jIeTJIli522uYG++LkuW155vm F3swRVWIdTaJn7wJDHgHs4VzsbNOnSCbmD4JnFpr52wUAdwP+0eWtWPX48Y/kIxUPl 7BO5ZUngjC1LOg1ygp3aYhPe3AymcJWJ/BSPUPA/QxBzXJnngqU1T2nZyLm8kC6KFb tfzBPNlgGX0XnAw6VYQJsh22toJ9zUYm/mq5X/4ypu7UMWCoZm+C/T0FLN80ToiRCW lMH7tin3B3c+QdY6vK53CPJpnGhwLmRUDWWYbOQYFEPLw1DAs8K9WdHjWAZcLANQbK H9vxJ0L18Omtg== Received-SPF: pass client-ip=209.250.245.214; envelope-from=miha@kamnitnik.top; helo=mail.kamnitnik.top X-Spam_score_int: 4 X-Spam_score: 0.4 X-Spam_bar: / X-Spam_report: (0.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_SUSPICIOUS_NTLD=0.499, FROM_SUSPICIOUS_NTLD_FP=2, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_PDS_OTHER_BAD_TLD=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:209719 Archived-At: --=-=-= Content-Type: text/plain 1) M-x shell 2) echo foo RET 3) C-/ to undo this 'RET' The buffer now contain the shell's prompt and "echo foo". However, the process mark is located at eob after "echo foo" 4) Type bar The buffer now contain the shell's prompt and "echo foobar" 5) RET Shell will output "bar: command not found", because the process mark is located before "bar" after "foo". Similar behaviour can be observed with C-c SPC (comint-accumulate) and with eshell. 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Improve-undo-in-comint-and-eshell.patch >From fde3b5ce8964e001a9019feff83e267b2cf367dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= Date: Fri, 9 Jul 2021 10:57:11 +0200 Subject: [PATCH] Improve undo in comint and eshell * lisp/simple.el (marker-record-undo): New function. * etc/NEWS: * doc/lispref/markers.texi (Moving Markers): Document it. * lisp/comint.el (comint-send-input): (comint-accumulate): (comint-set-process-mark): * lisp/eshell/esh-mode.el (eshell-reset): (eshell-update-markers): Use it to record adjustments to various marker positions in undo list. --- doc/lispref/markers.texi | 17 +++++++++++++++++ etc/NEWS | 5 +++++ lisp/comint.el | 14 ++++++++++---- lisp/eshell/esh-mode.el | 15 ++++++++++----- lisp/simple.el | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/doc/lispref/markers.texi b/doc/lispref/markers.texi index 80f79b67e5..b0c454be8d 100644 --- a/doc/lispref/markers.texi +++ b/doc/lispref/markers.texi @@ -395,6 +395,23 @@ Moving Markers @defun move-marker marker position &optional buffer This is another name for @code{set-marker}. +@end defun + + Function @code{set-marker} does not record marker movement in the +undo list. Before moving a marker, you can explicitly record its +original position as an undo list entry with +@code{marker-record-undo}. + +@defun marker-record-undo &rest markers +This function records the current position and buffer of each marker +in MARKERS as an entry in the undo list. Undoing it will relocate +these markers to point back to their recorded positions. Passing +markers that currently point nowhere is allowed and undoing will +simply make them point nowhere again. + +Undo in region will always ignore entries made with this function. +Also, this function doesn't do anything if undo is disabled in the +current buffer. @end defun @node The Mark diff --git a/etc/NEWS b/etc/NEWS index da5524a555..2e0e7abc47 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2945,6 +2945,11 @@ The former is now declared obsolete. * Lisp Changes in Emacs 28.1 ++++ +** New function 'marker-record-undo'. +To make marker movement undoable, use this function to store a +marker's current position in the undo list before moving the marker. + --- *** ':safe' settings in 'defcustom' are now propagated to the loaddefs files. diff --git a/lisp/comint.el b/lisp/comint.el index 9e406614b9..f464ecbbe4 100644 --- a/lisp/comint.el +++ b/lisp/comint.el @@ -1931,9 +1931,12 @@ comint-send-input (setq comint-input-ring-index nil) ;; Update the markers before we send the input ;; in case we get output amidst sending the input. + (marker-record-undo + pmark comint-last-input-start comint-last-input-end + comint-accum-marker) (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)) @@ -3490,6 +3493,7 @@ comint-accumulate when you send it." (interactive) (insert "\n") + (marker-record-undo comint-accum-marker) (set-marker comint-accum-marker (point)) (if comint-input-ring-index (setq comint-save-input-ring-index @@ -3525,9 +3529,11 @@ comint-bol-or-process-mark (defun comint-set-process-mark () "Set the process mark at point." (interactive) - (let ((proc (or (get-buffer-process (current-buffer)) - (user-error "Current buffer has no process")))) - (set-marker (process-mark proc) (point)) + (let* ((proc (or (get-buffer-process (current-buffer)) + (user-error "Current buffer has no process"))) + (pmark (process-mark proc))) + (marker-record-undo pmark) + (set-marker pmark (point)) (message "Process mark set"))) diff --git a/lisp/eshell/esh-mode.el b/lisp/eshell/esh-mode.el index f9dbce9770..9aa00016c0 100644 --- a/lisp/eshell/esh-mode.el +++ b/lisp/eshell/esh-mode.el @@ -534,11 +534,14 @@ eshell-reset "Output a prompt on a new line, aborting any current input. If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run." (goto-char (point-max)) - (setq eshell-last-input-start (point-marker) - eshell-last-input-end (point-marker) - eshell-last-output-start (point-marker) - eshell-last-output-block-begin (point) - eshell-last-output-end (point-marker)) + (marker-record-undo + eshell-last-input-start eshell-last-input-end + eshell-last-output-start eshell-last-output-end) + (set-marker eshell-last-input-start (point)) + (set-marker eshell-last-input-end (point)) + (set-marker eshell-last-output-start (point)) + (set-marker eshell-last-output-end (point)) + (setq eshell-last-output-block-begin (point)) (eshell-begin-on-new-line) (unless no-hooks (run-hooks 'eshell-post-command-hook) @@ -568,6 +571,8 @@ eshell-parse-command-input (defun eshell-update-markers (pmark) "Update the input and output markers relative to point and PMARK." + (marker-record-undo eshell-last-input-start eshell-last-input-end + eshell-last-output-end) (set-marker eshell-last-input-start pmark) (set-marker eshell-last-input-end (point)) (set-marker eshell-last-output-end (point))) diff --git a/lisp/simple.el b/lisp/simple.el index f746d738a6..337cfe6234 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -3466,6 +3466,24 @@ undo-adjust-pos ;; comments. (max (car d) (- pos (cdr d))))))) +(defun marker-record-undo (&rest markers) + "Record positions of MARKERS in the undo list. +Undoing this entry will make each marker in MARKERS point to its +recorded position and buffer, or nowhere if it currently points +nowhere. Undo in region will always ignore these entries. + +If undo is disabled in the current buffer, this function does +nothing." + (let ((undo-list buffer-undo-list)) + (unless (eq undo-list t) + (dolist (marker markers) + (push (list 'apply #'set-marker marker + (marker-position marker) (marker-buffer marker)) + undo-list)) + (setq buffer-undo-list + `((apply ,#'marker-record-undo ,@markers) + ,@undo-list))))) + ;; Return the first affected buffer position and the delta for an undo element ;; delta is defined as the change in subsequent buffer positions if we *did* ;; the undo. -- 2.32.0 --=-=-=--