unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@catern.com>
To: Juri Linkov <juri@linkov.net>
Cc: emacs-devel@gnu.org
Subject: Re: Navigating completions from minibuffer
Date: Tue, 28 Nov 2023 14:48:35 +0000 (UTC)	[thread overview]
Message-ID: <87leaiue78.fsf@catern.com> (raw)
In-Reply-To: <86il5niukm.fsf@mail.linkov.net>

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

Juri Linkov <juri@linkov.net> writes:
>>> A possible workaround would be to check the special property
>>> 'first-completion'.  But then when the logic uses text properties,
>>> why not to use more text properties that indicate that a candidate
>>> is deselected (and remove highlighting in this case) instead of
>>> moving point somewhere outside of the candidate?
>>
>> Even if we work around this with text properties, I think it's nice for
>> deselection to be usually determined by location of point.  I think
>> that's easier to understand for the user - they should be able to
>> deselect or reselect something purely by moving point around, it
>> shouldn't require text property manipulation.
>
> I agree that it's better to indicate deselection by moving point.
>
> After more testing I found a small problem:
>
>   M-x calc TAB TAB
>
> When the selected candidate is
>
>   calc-dispatch (C-x *)
>
> then point is moved before the suffix "(C-x *)",
> and after editing it is selected on RET.

Ah, good catch.  Fixed (by using 'completion--string to detect whether
point is on a completion rather 'mouse-face)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Deselect-the-selected-completion-candidate-when-typi.patch --]
[-- Type: text/x-patch, Size: 7149 bytes --]

From 9321015b4000ee1d26788a6b5b2d38d6c248c5d4 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@catern.com>
Date: Thu, 23 Nov 2023 13:37:29 +0000
Subject: [PATCH] Deselect the selected completion candidate when typing

minibuffer-choose-completion-or-exit submits the selected completion
candidate, if any, ignoring the contents of the minibuffer.  But a
user might select a completion candidate and then want to type
something else in the minibuffer and submit what they typed.

Now typing will automatically deselect the selected completion
candidate so that minibuffer-choose-completion-or-exit will not choose
it.

minibuffer-choose-completion has the same behavior as before, and is
not affected by the deselection.

* lisp/minibuffer.el (completion-auto-deselect, completions--deselect)
(completions--after-change): Add.
(minibuffer-completion-help): Add completions--after-change hook.
(minibuffer-next-completion): Bind completion-auto-deselect to nil to
avoid immediately deselecting the completion.
(minibuffer-choose-completion-or-exit): Bind
choose-completion-deselect-if-after so deselection takes effect.
(display-completion-list): Guarantee a newline at the beginning of
*Completions* to avoid ambiguity about candidate selection.
* lisp/simple.el (choose-completion-deselect-if-after): Add.
(choose-completion): Check choose-completion-deselect-if-after.
---
 lisp/minibuffer.el | 43 +++++++++++++++++++++++++++++++++++++++----
 lisp/simple.el     | 11 ++++++++++-
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 5c12d9fc914..382d4458e26 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2310,8 +2310,11 @@ display-completion-list
 
     (with-current-buffer standard-output
       (goto-char (point-max))
-      (when completions-header-format
-        (insert (format completions-header-format (length completions))))
+      (if completions-header-format
+          (insert (format completions-header-format (length completions)))
+        (unless completion-show-help
+          ;; Ensure beginning-of-buffer isn't a completion.
+          (insert (propertize "\n" 'face '(:height 0)))))
       (completion--insert-strings completions group-fun)))
 
   (run-hooks 'completion-setup-hook)
@@ -2378,6 +2381,33 @@ completions--fit-window-to-buffer
         (resize-temp-buffer-window win))
     (fit-window-to-buffer win completions-max-height)))
 
+(defcustom completion-auto-deselect t
+  "If non-nil, deselect the selected completion candidate when you type.
+
+A non-nil value means that after typing, point in *Completions*
+will be moved off any completion candidates.  This means
+`minibuffer-choose-completion-or-exit' will exit with the
+minibuffer's current contents, instead of a completion candidate."
+  :type '(choice (const :tag "Candidates in *Completions* stay selected as you type" nil)
+                 (const :tag "Typing deselects any completion candidate in *Completions*" t))
+  :version "30.1")
+
+(defun completions--deselect ()
+  "If point is in a completion candidate, move to just after the end of it.
+
+The candidate will still be chosen by `choose-completion' unless
+`choose-completion-deselect-if-after' is non-nil."
+  (when (get-text-property (point) 'completion--string)
+    (goto-char (or (next-single-property-change (point) 'completion--string)
+                   (point-max)))))
+
+(defun completions--after-change (_start _end _old-len)
+  "Update displayed *Completions* buffer after change in buffer contents."
+  (when completion-auto-deselect
+    (when-let (window (get-buffer-window "*Completions*" 0))
+      (with-selected-window window
+        (completions--deselect)))))
+
 (defun minibuffer-completion-help (&optional start end)
   "Display a list of possible completions of the current minibuffer contents."
   (interactive)
@@ -2400,6 +2430,7 @@ minibuffer-completion-help
           ;; If there are no completions, or if the current input is already
           ;; the sole completion, then hide (previous&stale) completions.
           (minibuffer-hide-completions)
+          (remove-hook 'after-change-functions #'completions--after-change t)
           (if completions
               (completion--message "Sole completion")
             (unless completion-fail-discreetly
@@ -2460,6 +2491,8 @@ minibuffer-completion-help
             (body-function
              . ,#'(lambda (_window)
                     (with-current-buffer mainbuf
+                      (when completion-auto-deselect
+                        (add-hook 'after-change-functions #'completions--after-change t))
                       ;; Remove the base-size tail because `sort' requires a properly
                       ;; nil-terminated list.
                       (when last (setcdr last nil))
@@ -4673,7 +4706,8 @@ minibuffer-next-completion
           (next-line-completion (or n 1))
         (next-completion (or n 1)))
       (when auto-choose
-        (let ((completion-use-base-affixes t))
+        (let ((completion-use-base-affixes t)
+              (completion-auto-deselect nil))
           (choose-completion nil t t))))))
 
 (defun minibuffer-previous-completion (&optional n)
@@ -4721,7 +4755,8 @@ minibuffer-choose-completion-or-exit
 contents."
   (interactive "P")
   (condition-case nil
-      (minibuffer-choose-completion no-exit no-quit)
+      (let ((choose-completion-deselect-if-after t))
+        (minibuffer-choose-completion no-exit no-quit))
     (error (minibuffer-complete-and-exit))))
 
 (defun minibuffer-complete-history ()
diff --git a/lisp/simple.el b/lisp/simple.el
index 02c68912dba..bf57b285fca 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -10094,6 +10094,11 @@ next-line-completion
           (if pos (goto-char pos))))
       (setq n (1+ n)))))
 
+(defvar choose-completion-deselect-if-after nil
+  "If non-nil, don't choose a completion candidate if point is right after it.
+
+This makes `completions--deselect' effective.")
+
 (defun choose-completion (&optional event no-exit no-quit)
   "Choose the completion at point.
 If EVENT, use EVENT's position to determine the starting position.
@@ -10114,6 +10119,10 @@ choose-completion
           (insert-function completion-list-insert-choice-function)
           (completion-no-auto-exit (if no-exit t completion-no-auto-exit))
           (choice
+           (if choose-completion-deselect-if-after
+               (if-let ((str (get-text-property (posn-point (event-start event)) 'completion--string)))
+                   (substring-no-properties str)
+                 (error "No completion here"))
            (save-excursion
              (goto-char (posn-point (event-start event)))
              (let (beg)
@@ -10129,7 +10138,7 @@ choose-completion
                               beg 'completion--string)
                              beg))
                (substring-no-properties
-                (get-text-property beg 'completion--string))))))
+                (get-text-property beg 'completion--string)))))))
 
       (unless (buffer-live-p buffer)
         (error "Destination buffer is dead"))
-- 
2.42.1


  reply	other threads:[~2023-11-28 14:48 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07  3:57 Navigating completions from minibuffer T.V Raman
2023-11-07  4:55 ` T.V Raman
2023-11-07  7:20   ` Juri Linkov
2023-11-07 17:53     ` T.V Raman
2023-11-07 19:36       ` T.V Raman
2023-11-08  7:39         ` Juri Linkov
2023-11-08 16:21           ` T.V Raman
2023-11-08 17:18             ` T.V Raman
2023-11-08 22:11               ` Slow completion-at-point was " T.V Raman
2023-11-09  7:22                 ` Slow completion-at-point Juri Linkov
2023-11-09 12:10                   ` Dmitry Gutov
2023-11-09 16:20                     ` Juri Linkov
2023-11-09 18:32                       ` João Távora
2023-11-11  2:48                     ` T.V Raman
2023-11-11 10:36                       ` Dmitry Gutov
2023-11-11 16:40                         ` T.V Raman
2023-11-11 19:00                           ` Juri Linkov
2023-11-11 19:43                             ` T.V Raman
2023-11-11 21:50                               ` Dmitry Gutov
2023-11-09 15:39                   ` T.V Raman
2023-11-09 12:20                 ` Slow completion-at-point was Re: Navigating completions from minibuffer Dmitry Gutov
2023-11-09 15:41                   ` T.V Raman
2023-11-09 17:46                 ` T.V Raman
2023-11-10 13:12           ` Spencer Baugh
2023-11-11 18:58             ` Juri Linkov
2023-11-14  7:36               ` Juri Linkov
2023-11-15 21:40                 ` Spencer Baugh
2023-11-16 17:15                   ` T.V Raman
2023-11-15 22:03                 ` Spencer Baugh
2023-11-16  7:16                   ` Juri Linkov
2023-11-16 14:41                     ` Spencer Baugh
2023-11-16 17:28                       ` Juri Linkov
2023-11-16 18:25                         ` Spencer Baugh
2023-11-17  7:09                           ` Juri Linkov
2023-11-17 17:22                             ` Spencer Baugh
2023-11-18 20:58                               ` sbaugh
2023-11-19  7:08                                 ` Juri Linkov
2023-11-19  8:19                                   ` Eli Zaretskii
2023-11-19 14:41                                   ` Spencer Baugh
2023-11-19 18:01                                     ` Juri Linkov
2023-11-19 19:41                                       ` Spencer Baugh
2023-11-20  2:58                                       ` Spencer Baugh
2023-11-23 13:39                                     ` sbaugh
2023-11-24  7:54                                       ` Juri Linkov
2023-11-25 15:19                                         ` Spencer Baugh
2023-11-25 16:08                                           ` Eli Zaretskii
2023-11-25 18:23                                             ` sbaugh
2023-11-25 18:48                                               ` Juri Linkov
2023-11-26 13:10                                                 ` sbaugh
2023-11-25 19:00                                               ` Eli Zaretskii
2023-11-25 17:46                                           ` Juri Linkov
2023-11-26 14:33                                             ` Spencer Baugh
2023-11-27  7:22                                               ` Juri Linkov
2023-11-28 14:48                                                 ` Spencer Baugh [this message]
2023-11-28 17:23                                                   ` Juri Linkov
2023-11-29  0:20                                                     ` Spencer Baugh
2023-12-03 17:21                                                       ` Juri Linkov
2023-12-03 18:13                                                         ` Eli Zaretskii
2023-12-06 17:20                                                           ` Juri Linkov
2023-12-06 17:50                                                             ` Eli Zaretskii
2023-12-07  7:44                                                               ` Juri Linkov
2023-12-08  8:46                                                                 ` Eli Zaretskii

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=87leaiue78.fsf@catern.com \
    --to=sbaugh@catern.com \
    --cc=emacs-devel@gnu.org \
    --cc=juri@linkov.net \
    /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).