[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Updating *Completions* as you type
From: |
Spencer Baugh |
Subject: |
Re: Updating *Completions* as you type |
Date: |
Tue, 28 Nov 2023 14:38:55 +0000 (UTC) |
Juri Linkov <juri@linkov.net> writes:
>>> This is what I believe they should do: we add a category,
>>> and they support it as well.
>>
>> OK, I'm fine with that, but when we do that, I think the per-table
>> option should override the per-category option.
>
> I agree that the per-table option should override the per-category option,
> but see no way to distinguish customized values from hard-coded ones
> without trying to turn all hard-coded values into options.
Right. I guess my position is that there's not that many hard-coded
display-sort-functions (only 5 in core Emacs), so turning them all to
options is fine.
> display-buffer has a similar problem, but the difference is that
> it's possible to identify a buffer by its name and use a regexp
> to match buffer names. Whereas for completing-read it's not easy
> to identify a completion table. A category matches a set of
> completion tables, so maybe we need another identification
> for individual tables?
True, that would help. Maybe the function symbol for the completion
table? Tables are usually lambdas today, but maybe we could make it
easy to use a defun'd function instead, which would be very good for
comprehensibility in general IMO.
>> I'm OK with a category-based approach. I just think we should reserve
>> the *ability* to use table-specific options, by making a table-specific
>> display-sort-function override the category-specific display-sort-function.
>
> Probably introducing a new field to metadata could help to resolve
> ambiguities.
>
>> Anyway, we're going around in circles a bit here. How about this patch
>> which only adds the new historical option to completions-sort? I think
>> we're in agreement on everything in this patch, and maybe installing it
>> will get some user feedback which we can use when coming back to this
>> later.
>
> Thanks, everything looks nice, only a etc/NEWS announcement is missing.
Added.
>From 4196334888fb35395deca9418203f3622dedbaff 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 historical option to completions-sort
Support sorting candidates in *Completions* by the order they show up
in the minibuffer history.
Also add minibuffer-sort-alphabetically and
minibuffer-sort-by-history, which are usable for both completions-sort
and display-sort-function.
* lisp/minibuffer.el (completions-sort): Document 'historical option.
(minibuffer-completion-help): Support 'historical option.
(minibuffer-sort-alphabetically)
(minibuffer-completion-base, minibuffer-sort-by-history): Add.
* etc/NEWS: Announce it.
---
etc/NEWS | 5 ++++
lisp/minibuffer.el | 63 +++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 62 insertions(+), 6 deletions(-)
diff --git a/etc/NEWS b/etc/NEWS
index fd633fad6fb..eefa9692e07 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -620,6 +620,11 @@ completions window. When the completions window is not
visible,
then all these keys have their usual meaning in the minibuffer.
This option is supported for in-buffer completion as well.
+*** New value for 'historical' for user option 'completions-sort'
+When 'completions-sort' is set to 'historical', completion candidates
+will be sorted by their position in the minibuffer history, more
+recent candidates appearing first.
+
** Pcomplete
---
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 5c12d9fc914..dfb30e4364c 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1314,14 +1314,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."
+Completion candidates in the *Completions* buffer are sorted
+depending on the value.
+
+If nil, sorting is disabled.
+If `alphabetical', candidates are sorted by
+`minibuffer-sort-alphabetically'.
+If `historical', candidates are sorted by
+`minibuffer-sort-by-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.
@@ -1647,6 +1659,43 @@ minibuffer--sort-preprocess-history
(substring c base-size)))
hist)))))
+(defun minibuffer-sort-alphabetically (completions)
+ "Sort COMPLETIONS alphabetically.
+
+COMPLETIONS are sorted alphabetically by `string-lessp'.
+
+This is a suitable function to use for `completions-sort' or to
+include as `display-sort-function' in completion metadata."
+ (sort completions #'string-lessp))
+
+(defvar minibuffer-completion-base nil
+ "The base for the current completion.
+
+This is the part of the current minibuffer input which is not
+being completed on. This is primarily relevant for file names,
+where this is the directory component of the file name.")
+
+(defun minibuffer-sort-by-history (completions)
+ "Sort COMPLETIONS by their position in `minibuffer-history-variable'.
+
+COMPLETIONS are sorted first by `minibuffer-sort-alphbetically',
+then any elements occuring in the minibuffer history list are
+moved to the front based on the order they occur in the history.
+If a history variable hasn't been specified for this call of
+`completing-read', COMPLETIONS are sorted only by
+`minibuffer-sort-alphbetically'.
+
+This is a suitable function to use for `completions-sort' or to
+include as `display-sort-function' in completion metadata."
+ (let ((alphabetized (sort completions #'string-lessp)))
+ ;; Only use history when it's specific to these completions.
+ (if (eq minibuffer-history-variable
+ (default-value minibuffer-history-variable))
+ alphabetized
+ (minibuffer--sort-by-position
+ (minibuffer--sort-preprocess-history minibuffer-completion-base)
+ alphabetized))))
+
(defun minibuffer--group-by (group-fun sort-fun elems)
"Group ELEMS by GROUP-FUN and sort groups by SORT-FUN."
(let ((groups))
@@ -2409,6 +2458,7 @@ minibuffer-completion-help
(let* ((last (last completions))
(base-size (or (cdr last) 0))
(prefix (unless (zerop base-size) (substring string 0 base-size)))
+ (minibuffer-completion-base (substring string 0 base-size))
(base-prefix (buffer-substring (minibuffer--completion-prompt-end)
(+ start base-size)))
(base-suffix
@@ -2473,7 +2523,8 @@ minibuffer-completion-help
(funcall sort-fun completions)
(pcase completions-sort
('nil completions)
- ('alphabetical (sort completions
#'string-lessp))
+ ('alphabetical
(minibuffer-sort-alphabetically completions))
+ ('historical
(minibuffer-sort-by-history completions))
(_ (funcall completions-sort
completions)))))
;; After sorting, group the candidates using the
--
2.42.1
- Re: Updating *Completions* as you type, (continued)
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/21
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/22
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/22
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/23
- Re: Updating *Completions* as you type, sbaugh, 2023/11/23
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/24
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/25
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/25
- Re: Updating *Completions* as you type, sbaugh, 2023/11/26
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/27
- Re: Updating *Completions* as you type,
Spencer Baugh <=
- Re: Updating *Completions* as you type, Eli Zaretskii, 2023/11/28
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/28
- Re: Updating *Completions* as you type, Eli Zaretskii, 2023/11/28
- Re: Updating *Completions* as you type, Juri Linkov, 2023/11/29
- Re: Updating *Completions* as you type, Eli Zaretskii, 2023/11/29
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/29
- Re: Updating *Completions* as you type, Eli Zaretskii, 2023/11/29
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/29
- Re: Updating *Completions* as you type, Eli Zaretskii, 2023/11/29
- Re: Updating *Completions* as you type, Spencer Baugh, 2023/11/30