all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: sbaugh@catern.com
To: emacs-devel@gnu.org
Subject: Re: Updating *Completions* as you type
Date: Tue, 17 Oct 2023 09:48:30 -0400	[thread overview]
Message-ID: <87ttqpwea9.fsf@catern.com> (raw)
In-Reply-To: 87sf6dx954.fsf@catern.com

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

sbaugh@catern.com writes:
>>> That being said, yes this may be nice.  But minibuffer-completion-help
>>> already does sort the completions using display-sort-function, just like
>>> completion-all-sorted-completions, so what's causing the difference in
>>> behavior?
>>
>> It seems this is implementable in minibuffer-completion-help
>> by copying this code from completion-all-sorted-completions:
>>
>>               (setq all (minibuffer--sort-by-position
>>                          (minibuffer--sort-preprocess-history
>>                           (substring string 0 base-size))
>>                          all))
>
>Oh, very interesting!  Maybe completions-sort should accept a new symbol
>'history to behave this way?
>
>I already think this would be very nice for project-switch-project, for
>example, since I'm often switching between a few related projects.
>
>Perhaps 'history should break ties by alphabetizing, otherwise
>e.g. filenames you haven't visited before would be unsorted randomly.
>
>Hmm, actually the case of filenames is a bit complex.  Because for
>filenames, the strings we're completing over don't appear verbatim in
>the history.  So perhaps read-file-name would need its own specialized
>sorting function, which whenever you're completing in some directory,
>sorts to the front any files in that directory whose full path appears
>in file-name-history.
>
>>> Honestly the main place I find myself wanting different sorting of
>>> completions is for buffer completion - I'd prefer buffers to be sorted
>>> by most-recently-used.  Maybe we can just add such an option?
>>
>> Such an option would be nice.  Maybe the right way to support it
>> is to add a new sort function with the code above.  Then the caller
>> could provide such a function in the completion metadata.
>
>Oh, that's also interesting.  So there would be a function that the
>completion metadata could specify as display-sort-function, which would
>have the behavior of sorting based on history?
>
>That also makes a lot of sense, and would allow commands like
>project-switch-project and read-buffer to opt in on a command-by-command
>basis, which might be more sensible.
>
>So maybe adding 'history as a new option for completions-sort isn't a
>good idea.  Instead we should just add user options for enabling history
>sorting for files and buffers.  (And perhaps we could just enable
>history sorting by default for project-switch-project.)
>
>Also: it might be nice to switch between history-sorting and
>alphabetized-sorting during the course of completion, both for files and
>buffers.  Maybe we could do that by making a command which puts
>completion into a mode where it just ignores the display-sort-function
>specified by the completion metadata, and just always uses
>completions-sort.

On reflection, I think adding a new option for completions-sort is
indeed what we should do.  Otherwise, we won't get historical sorting
for non-programmed completing-read, which is a pretty common kind of
completing-read.  Here's a patch to do that.

We can always add display-sort-functions later for buffers and files, if
that ends up being desirable.

Also, tangentially, I think probably we should rework
minibuffer-complete-history and minibuffer-complete-defaults to be
persistent - as in, regular TAB afterwards continues to complete history
or defaults.  And there should be some way to reset back to normal.
That would be a good complement to this completions-sort change, by
maybe giving a way to switch on-demand to alphabetical sorting.  (I've
long thought this would be good and useful, but in particular it's
relevant for completions-auto-update since that will otherwise nearly
immediately reset the displayed completions back to normal.)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-a-historical-option-to-completions-sort.patch --]
[-- Type: text/x-patch, Size: 3851 bytes --]

From b97e311ee42f7f9021b3c0e017636e258dd6d5d9 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@catern.com>
Date: Tue, 17 Oct 2023 09:09:55 -0400
Subject: [PATCH] Add a historical option to completions-sort

This causes completion to sort based on history.  This is useful for
getting, e.g., most-recently-used order from C-x b.

We only do historical sorting when there's a completion-specific
history variable.  Otherwise, candidate would be spuriously brought to
the front if they just happened to match a string that the user has
entered before.

Also, the presence of a completion-specific history variable is a
decent proxy for "does history matter for this command?"; if there
isn't a specific history variable, sorting based on history would
probably be undesirable.  (Note also that the user can always choose
to do historical sorting mid-completion by running
minibuffer-complete-history.)

* lisp/minibuffer.el (completions-sort): Document 'historical option.
(minibuffer-completion-help): Support 'historical option.
---
 lisp/minibuffer.el | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index f53cd739e2f..782efff80f9 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1313,14 +1313,26 @@ completion-cycle-threshold
 (defcustom completions-sort 'alphabetical
   "Sort candidates in the *Completions* buffer.
 
-The value can be nil to disable sorting, `alphabetical' for
-alphabetical sorting or a custom sorting function.  The sorting
-function takes and returns a list of completion candidate
-strings."
+Candidate completions in the *Completions* are sorted depending
+on the value.
+
+If nil, sorting is disabled.
+If `alphabetical', candidates are sorted alphabetically.
+If `historical', candidates are first sorted alphabetically, then
+candidates occurring in `minibuffer-history-variable' are moved
+to the front based on the order they occur in the history.
+If a function, the function is called to sort the candidates.
+The sorting function takes and returns a list of completion
+candidate strings.
+
+If the completion-specific metadata provides a
+`display-sort-function', that is used instead and this value is
+ignored."
   :type '(choice (const :tag "No sorting" nil)
                  (const :tag "Alphabetical sorting" alphabetical)
+                 (const :tag "Historical sorting" historical)
                  (function :tag "Custom function"))
-  :version "29.1")
+  :version "30.1")
 
 (defcustom completions-group nil
   "Enable grouping of completion candidates in the *Completions* buffer.
@@ -2510,6 +2522,15 @@ minibuffer-completion-help
                                           (pcase completions-sort
                                             ('nil completions)
                                             ('alphabetical (sort completions #'string-lessp))
+                                            ('historical
+                                             (let ((alphabetized (sort completions #'string-lessp)))
+                                               ;; Only use history when it's specific to these completions.
+                                               (if (eq minibuffer-history-variable 'minibuffer-history)
+                                                   alphabetized
+                                                 (minibuffer--sort-by-position
+                                                  (minibuffer--sort-preprocess-history
+                                                   (substring string 0 base-size))
+                                                  alphabetized))))
                                             (_ (funcall completions-sort completions)))))
 
                       ;; After sorting, group the candidates using the
-- 
2.41.0


  parent reply	other threads:[~2023-10-17 13:48 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 23:53 Updating *Completions* as you type sbaugh
2023-10-13  6:31 ` Eli Zaretskii
2023-10-13 18:01   ` Spencer Baugh
2023-10-14  7:09     ` Eli Zaretskii
2023-10-14 19:26       ` Björn Bidar
     [not found]       ` <874jit2ef7.fsf@>
2023-10-14 19:38         ` Eli Zaretskii
2023-10-14 16:51     ` Juri Linkov
2023-10-14 17:56       ` sbaugh
2023-10-14 19:51       ` Dmitry Gutov
2023-10-13  6:34 ` Juri Linkov
2023-10-13 19:04   ` Spencer Baugh
2023-10-14 16:58     ` Juri Linkov
2023-10-14 20:05       ` sbaugh
2023-10-15  6:06         ` Eli Zaretskii
2023-10-15 15:55           ` sbaugh
2023-10-16 11:38             ` Eli Zaretskii
2023-10-16 14:50               ` Michael Albinus
2023-10-16 15:58                 ` [External] : " Drew Adams
2023-10-16 12:16             ` sbaugh
2023-10-17 18:23               ` Juri Linkov
2023-10-18 23:27                 ` Spencer Baugh
2023-10-15  7:32         ` Juri Linkov
2023-10-16 19:28           ` Rudolf Adamkovič
2023-10-17 18:38             ` Juri Linkov
2023-10-15 20:31         ` Eshel Yaron
2023-10-16  3:18           ` [External] : " Drew Adams
2023-10-16 16:54           ` Juri Linkov
2023-10-17 13:48         ` sbaugh [this message]
2023-10-17 18:35           ` Juri Linkov
2023-10-17 22:57             ` Spencer Baugh
2023-10-18  3:04               ` [External] : " Drew Adams
2023-10-18  6:56               ` Juri Linkov
2023-10-18 12:25                 ` Spencer Baugh
2023-10-18 17:32                   ` Juri Linkov
2023-10-18 23:33                     ` Spencer Baugh
2023-10-19  2:29                       ` Spencer Baugh
2023-10-19  6:55                         ` Juri Linkov
2023-11-19 19:22                           ` sbaugh
2023-11-20  7:51                             ` Juri Linkov
2023-11-20 15:24                               ` Spencer Baugh
2023-11-20 17:47                                 ` Juri Linkov
2023-11-20 18:50                                   ` Spencer Baugh
2023-11-21  7:58                                     ` Juri Linkov
2023-11-21 12:40                                       ` sbaugh
2023-11-21 17:09                                         ` Juri Linkov
2023-11-21 20:45                                           ` Spencer Baugh
2023-11-22  7:51                                             ` Juri Linkov
2023-11-22 16:11                                               ` Spencer Baugh
2023-11-23  7:58                                                 ` Juri Linkov
2023-11-23 12:36                                                   ` sbaugh
2023-11-24  7:58                                                     ` Juri Linkov
2023-11-25 16:44                                                       ` Spencer Baugh
2023-11-25 18:31                                                         ` Juri Linkov
2023-11-26 13:33                                                           ` sbaugh
2023-11-27  7:28                                                             ` Juri Linkov
2023-11-28 14:38                                                               ` Spencer Baugh
2023-11-28 15:03                                                                 ` Eli Zaretskii
2023-11-28 17:13                                                                   ` Juri Linkov
2023-11-28 17:36                                                                     ` Eli Zaretskii
2023-11-29  7:11                                                                       ` Juri Linkov
2023-11-29 13:09                                                                         ` Eli Zaretskii
2023-11-29 14:14                                                                           ` Spencer Baugh
2023-11-29 14:54                                                                             ` Eli Zaretskii
2023-11-29 15:21                                                                               ` Spencer Baugh
2023-11-29 15:52                                                                                 ` Eli Zaretskii
2023-11-29 19:17                                                                                   ` Spencer Baugh
2023-11-30  6:12                                                                                     ` Eli Zaretskii
2023-11-30 12:33                                                                                       ` Spencer Baugh
2023-11-30 14:10                                                                                         ` Eli Zaretskii
2023-11-28 23:56                                                                   ` Spencer Baugh
2023-11-29  3:33                                                                     ` Eli Zaretskii
2023-12-03 17:25                                                                     ` Juri Linkov
2023-12-03 17:56                                                                       ` Eli Zaretskii
2023-12-06 17:17                                                                         ` Juri Linkov
2023-11-28 17:16                                                                 ` Juri Linkov
2023-11-28 23:36                                                                   ` Turning completion table lambdas into symbols Spencer Baugh
2023-11-28 23:51                                                                     ` Dmitry Gutov
2023-11-29 19:26                                                                       ` Spencer Baugh
2023-12-01  0:36                                                                         ` Dmitry Gutov
2023-11-29  7:18                                                                     ` Juri Linkov
2023-11-21 12:54                                       ` Updating *Completions* as you type John Yates
2023-11-21 17:03                                         ` Juri Linkov
2023-11-21 22:27                                           ` John Yates
2023-10-20  6:49         ` Juri Linkov
2023-10-17 15:01       ` sbaugh
2023-10-17 18:20         ` Juri Linkov
2023-10-17 23:37           ` Spencer Baugh
2023-10-17 23:44             ` Spencer Baugh
2023-10-18  6:51             ` Juri Linkov
2023-10-18 12:47               ` Spencer Baugh
2023-10-18 17:28                 ` Juri Linkov
2023-10-18 23:32                   ` Spencer Baugh
2023-10-16  3:19   ` [External] : " Drew Adams
2023-10-20  9:35   ` zcomplete Philip Kaludercic
2023-10-22 17:28     ` zcomplete Juri Linkov
2023-10-23  5:00       ` zcomplete Protesilaos Stavrou
2023-10-23  6:45         ` zcomplete Juri Linkov
2023-10-13 18:11 ` Updating *Completions* as you type Daniel Semyonov
2023-10-13 18:48   ` Spencer Baugh
2023-10-16  3:16     ` [External] : " Drew Adams
2023-10-16  9:25       ` Philip Kaludercic
2023-10-16 16:03         ` Drew Adams
2023-10-20  7:45           ` Philip Kaludercic
2023-10-20 16:10             ` Drew Adams
2023-10-16 22:55         ` Emanuel Berg
2023-10-17  6:09           ` Emanuel Berg
2023-10-17  0:44 ` Michael Heerdegen

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

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

  git send-email \
    --in-reply-to=87ttqpwea9.fsf@catern.com \
    --to=sbaugh@catern.com \
    --cc=emacs-devel@gnu.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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.