From 3e9e0c2310afdcc921cda2a6d431d7fcbe1eb01a Mon Sep 17 00:00:00 2001 Message-Id: <3e9e0c2310afdcc921cda2a6d431d7fcbe1eb01a.1642601249.git.info@protesilaos.com> From: Protesilaos Stavrou Date: Wed, 19 Jan 2022 14:20:19 +0200 Subject: [PATCH] Make Completions sorting a user option * etc/NEWS: Document the new user option. * lisp/minibuffer.el (completions-sort): Add new user option. (minibuffer-completion-help): Implement it for the Completions buffer. --- etc/NEWS | 6 ++++++ lisp/minibuffer.el | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 47dbfba9c0..8f68336d30 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -418,6 +418,12 @@ When non-nil, the commands 'next-completion' and 'previous-completion' automatically wrap around on reaching the beginning or the end of the *Completions* buffer. +*** New user option 'completions-sort'. +Controls the sorting of the completion candidates in the *Completions* +buffer. Available styles are no sorting, alphabetical (the default), +or a custom sort function (e.g. 'string-version-lessp'). The function +takes and returns a list of completion candidate strings. + ** Isearch and Replace +++ diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index ab760a42d1..7b7e734b72 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1169,6 +1169,18 @@ (defcustom completion-cycle-threshold nil :version "24.1" :type completion--cycling-threshold-type) +(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." + :type '(choice (const :tag "No sorting" nil) + (const :tag "Alphabetical sorting" alphabetical) + function :tag "Custom function") + :version "29.1") + (defcustom completions-group nil "Enable grouping of completion candidates in the *Completions* buffer. See also `completions-group-format' and `completions-group-sort'." @@ -2264,7 +2276,9 @@ (defun minibuffer-completion-help (&optional start end) ;; same, but not always. (setq completions (if sort-fun (funcall sort-fun completions) - (sort completions 'string-lessp))) + (pcase completions-sort + ('alphabetical (sort completions #'string-lessp)) + (_ completions)))) ;; After sorting, group the candidates using the ;; `group-function'. -- 2.34.1