unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Lars Ingebrigtsen <larsi@gnus.org>,
	lars@matholka.se, 45029@debbugs.gnu.org
Subject: bug#45029: 27.1.50; Regression: Yanking into externally modified file with delete-selection-mode
Date: Sat, 05 Dec 2020 21:42:10 +0200	[thread overview]
Message-ID: <87eek4gl3p.fsf@mail.linkov.net> (raw)
In-Reply-To: <83a6uscwv2.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 05 Dec 2020 13:31:45 +0200")

[-- Attachment #1: Type: text/plain, Size: 1544 bytes --]

> I'm guessing this is some unintended consequence of replacing
> read-char-choice in userlock.el with read-char-from-minibuffer.
>
> Juri, could you please look into this regression in Emacs 27?  I'd
> like to try to fix this for 27.2.

Here is a brief trace that explains the cause of the problem:

1. C-y (yank) calls delete-selection-pre-hook from pre-command-hook
2. delete-selection-helper calls delete-active-region
3. this activates ask-user-about-supersession-threat on a modified file
4. it calls read-char-from-minibuffer ('this-command' is still 'yank')
5. read-char-from-minibuffer waits when user types 'y'
   (bound to the command 'read-char-from-minibuffer-insert-char')
6. after typing 'y', read-char-from-minibuffer returns 'y', but
   now 'this-command' is 'read-char-from-minibuffer-insert-char'
7. when delete-selection-pre-hook finishes, due to 'this-command'
   'read-char-from-minibuffer-insert-char' is called again,
   and this time operates on the buffer as if it's the minibuffer

It seems there is a fundamental problem when read-from-minibuffer
is used from pre-command-hook it modifies the value of this-command.
But I wonder why this problem doesn't occur in other places.
Maybe other places doesn't try to operate on the buffer as on
the minibuffer?

Anyway, here is a patch with two fixes:
1. Guards read-char-from-minibuffer-insert-char against inadvertent
   operating on the non-minibuffer buffer;
2. Prevents read-char-from-minibuffer from changing the value of
   'this-command' by read-from-minibuffer:


[-- Attachment #2: protect-this-command.patch --]
[-- Type: text/x-diff, Size: 3261 bytes --]

diff --git a/lisp/subr.el b/lisp/subr.el
index 4b75268c04..7f1450d4a2 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2745,20 +2746,22 @@ read-char-from-minibuffer-insert-char
   "Insert the character you type in the minibuffer and exit.
 Discard all previous input before inserting and exiting the minibuffer."
   (interactive)
-  (delete-minibuffer-contents)
-  (insert last-command-event)
-  (exit-minibuffer))
+  (when (minibufferp)
+    (delete-minibuffer-contents)
+    (insert last-command-event)
+    (exit-minibuffer)))
 
 (defun read-char-from-minibuffer-insert-other ()
   "Handle inserting of a character other than allowed.
 Display an error on trying to insert a disallowed character.
 Also discard all previous input in the minibuffer."
   (interactive)
-  (delete-minibuffer-contents)
-  (ding)
-  (discard-input)
-  (minibuffer-message "Wrong answer")
-  (sit-for 2))
+  (when (minibufferp)
+    (delete-minibuffer-contents)
+    (ding)
+    (discard-input)
+    (minibuffer-message "Wrong answer")
+    (sit-for 2)))
 
 (defvar empty-history)
 
@@ -2802,6 +2805,8 @@ read-char-from-minibuffer
                                  map read-char-from-minibuffer-map-hash)
                         map))
                 read-char-from-minibuffer-map))
+         ;; Protect this-command when called from pre-command-hook (bug#45029)
+         (this-command this-command)
          (result
           (read-from-minibuffer prompt nil map nil
                                 (or history 'empty-history)))
@@ -2856,28 +2861,31 @@ y-or-n-p-insert-y
   "Insert the answer \"y\" and exit the minibuffer of `y-or-n-p'.
 Discard all previous input before inserting and exiting the minibuffer."
   (interactive)
-  (delete-minibuffer-contents)
-  (insert "y")
-  (exit-minibuffer))
+  (when (minibufferp)
+    (delete-minibuffer-contents)
+    (insert "y")
+    (exit-minibuffer)))
 
 (defun y-or-n-p-insert-n ()
   "Insert the answer \"n\" and exit the minibuffer of `y-or-n-p'.
 Discard all previous input before inserting and exiting the minibuffer."
   (interactive)
-  (delete-minibuffer-contents)
-  (insert "n")
-  (exit-minibuffer))
+  (when (minibufferp)
+    (delete-minibuffer-contents)
+    (insert "n")
+    (exit-minibuffer)))
 
 (defun y-or-n-p-insert-other ()
   "Handle inserting of other answers in the minibuffer of `y-or-n-p'.
 Display an error on trying to insert a disallowed character.
 Also discard all previous input in the minibuffer."
   (interactive)
-  (delete-minibuffer-contents)
-  (ding)
-  (discard-input)
-  (minibuffer-message "Please answer y or n")
-  (sit-for 2))
+  (when (minibufferp)
+    (delete-minibuffer-contents)
+    (ding)
+    (discard-input)
+    (minibuffer-message "Please answer y or n")
+    (sit-for 2)))
 
 (defvar empty-history)
 
@@ -2955,6 +2963,8 @@ y-or-n-p
                              (let ((help-form msg)) ; lexically bound msg
                                (help-form-show)))))
                        map))
+             ;; Protect this-command when called from pre-command-hook (bug#45029)
+             (this-command this-command)
              (str (read-from-minibuffer
                    prompt nil keymap nil
                    (or y-or-n-p-history-variable 'empty-history))))

  reply	other threads:[~2020-12-05 19:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-03 20:10 bug#45029: 27.1.50; Regression: Yanking into externally modified file with delete-selection-mode Lars Ljung
2020-12-04 10:18 ` Lars Ingebrigtsen
2020-12-05 11:31   ` Eli Zaretskii
2020-12-05 19:42     ` Juri Linkov [this message]
2020-12-05 20:04       ` Eli Zaretskii
2020-12-06 21:12       ` Juri Linkov
2020-12-07  3:27         ` Eli Zaretskii
     [not found]       ` <jwv8r9aj7u3.fsf-monnier+emacs@gnu.org>
2023-09-13 17:59         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-14  6:39           ` Juri Linkov
2023-09-14 13:44             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-15  6:36               ` Juri Linkov
2023-09-15 13:17                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-15 16:06                   ` Juri Linkov

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=87eek4gl3p.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=45029@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=lars@matholka.se \
    --cc=larsi@gnus.org \
    /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).