From: sbaugh@catern.com
To: Juri Linkov <juri@linkov.net>
Cc: emacs-devel@gnu.org
Subject: Re: Updating *Completions* as you type
Date: Sun, 19 Nov 2023 19:22:49 +0000 (UTC) [thread overview]
Message-ID: <87il5xlf9b.fsf@catern.com> (raw)
In-Reply-To: <86r0lrw17x.fsf@mail.linkov.net> (Juri Linkov's message of "Thu, 19 Oct 2023 09:55:14 +0300")
[-- Attachment #1: Type: text/plain, Size: 1322 bytes --]
Juri Linkov <juri@linkov.net> writes:
>> How about this?
>
> Looks good.
>
>> + (read-buffer-sort minibuffer
>> + (choice (const :tag "completions-sort controls sorting" nil)
>> + (const :tag "sort matching buffer-list" buffer-list))
>> + "30.1")
>
> Shouldn't 'read-buffer-sort' support other choices from 'completions-sort'?
> Your new choice 'historical' would sort buffers by the order of 'M-p'
> the same way as 'buffer-list' will sort buffers by the order of 'M-n'.
> Also 'alphabetical' would be useful for 'C-x b' when 'completions-sort'
> was customized to something else.
Finally got to it, I've added that in this patch (which combines both
the read-buffer-sort and completions-sort changes).
The nice reusable part is that the new minibuffer-sort-by-history and
minibuffer-sort-alphabetically should work for any completion table. So
it will be easy to just add options for different completion tables,
e.g. read-file-name-sort, and they can just use those pre-existing
functions.
This does require adding the new dynamic variable
minibuffer-completion-base which maybe should go in some other place, or
could be derived from some other variable - I'm not sure. Possibly it's
fine as I did it.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-read-buffer-sort-to-customize-how-read-buffer-so.patch --]
[-- Type: text/x-patch, Size: 8826 bytes --]
From 5e898c3ef323971af89b80fe37d94be0bf29443f 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 read-buffer-sort to customize how read-buffer sorts
completions
Add the ability to customize how read-buffer (actually
internal-complete-buffer) sorts the buffer completion candidates.
Also add minibuffer-sort-alphabetically and minibuffer-sort-by-history
to provide some reasonable options for read-buffer-sort.
And add support for them to completions-sort, too, as a simple change.
* 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.
* lisp/cus-start.el (standard): Add customization information for
read-buffer-sort.
* src/minibuf.c (Finternal_complete_buffer): Use read-buffer-sort.
(syms_of_minibuf): Add read-buffer-sort.
---
lisp/cus-start.el | 7 ++++++
lisp/minibuffer.el | 63 +++++++++++++++++++++++++++++++++++++++++-----
src/minibuf.c | 32 ++++++++++++++++++++---
3 files changed, 93 insertions(+), 9 deletions(-)
diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index 6d83aaf4d14..40bc4078d77 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -441,6 +441,13 @@ minibuffer-prompt-properties--setter
(read-buffer-function minibuffer
(choice (const nil)
function))
+ (read-buffer-sort minibuffer
+ (choice (const :tag "completions-sort controls sorting" nil)
+ (const :tag "sort matching buffer-list" buffer-list)
+ (const :tag "sort alphabetically" minibuffer-sort-alphabetically)
+ (const :tag "sort historically" minibuffer-sort-by-history)
+ (function :tag "User-defined function"))
+ "30.1")
;; msdos.c
(dos-unsupported-char-glyph display integer)
;; nsterm.m
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 026613c9eb2..49086ff6eff 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1307,14 +1307,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 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.
@@ -1640,6 +1652,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))
@@ -2470,6 +2519,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
@@ -2540,7 +2590,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
diff --git a/src/minibuf.c b/src/minibuf.c
index 58adde1bf66..04551afeb79 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -2186,9 +2186,15 @@ DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete
else if (EQ (flag, Qlambda))
return Ftest_completion (string, Vbuffer_alist, predicate);
else if (EQ (flag, Qmetadata))
- return list3 (Qmetadata,
- Fcons (Qcategory, Qbuffer),
- Fcons (Qcycle_sort_function, Qidentity));
+ {
+ Lisp_Object res = list2 (Fcons (Qcategory, Qbuffer),
+ Fcons (Qcycle_sort_function, Qidentity));
+ if (EQ (Vread_buffer_sort, Qbuffer_list))
+ res = Fcons (Fcons (Qdisplay_sort_function, Qidentity), res);
+ else if (FUNCTIONP (Vread_buffer_sort))
+ res = Fcons (Fcons (Qdisplay_sort_function, Vread_buffer_sort), res);
+ return Fcons (Qmetadata, res);
+ }
else
return Qnil;
}
@@ -2323,6 +2329,7 @@ syms_of_minibuf (void)
DEFSYM (Qcase_fold_search, "case-fold-search");
DEFSYM (Qmetadata, "metadata");
DEFSYM (Qcycle_sort_function, "cycle-sort-function");
+ DEFSYM (Qdisplay_sort_function, "display-sort-function");
/* A frame parameter. */
DEFSYM (Qminibuffer_exit, "minibuffer-exit");
@@ -2522,6 +2529,25 @@ syms_of_minibuf (void)
the minibuffer. However, if `minibuffer-restore-windows' is present
in `minibuffer-exit-hook', exiting the minibuffer will remove the window
showing the *Completions* buffer, if any. */);
+
+ DEFVAR_LISP ("read-buffer-sort", Vread_buffer_sort,
+ doc: /* Non-nil means sort completions in `read-buffer'.
+
+If this is nil (the default), completions in `read-buffer' are sorted
+according to `completions-sort'.
+
+If this is `buffer-list', completions are sorted to match the order of
+`buffer-list'.
+
+If a function, the function is called to sort the candidates. Some
+possible values are `minibuffer-sort-alphabetically' and
+`minibuffer-sort-by-history'.
+
+This variable only affects the default `read-buffer', so if
+`read-buffer-function' is set to a function which does not use
+`internal-complete-buffer', this variable will have no effect.*/);
+ Vread_buffer_sort = Qnil;
+
read_minibuffer_restore_windows = true;
defsubr (&Sactive_minibuffer_window);
--
2.42.1
next prev parent reply other threads:[~2023-11-19 19:22 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
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 [this message]
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=87il5xlf9b.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 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.