Support display-sort-function in completion-category-overrides (bug#68214)

* doc/lispref/minibuf.texi (Completion Variables):
Add 'display-sort-function' to the table of
'completion-category-overrides'.

* lisp/calendar/calendar.el (calendar-read-date): Add metadata
category 'calendar-month' for completing-read reading a month name.

* lisp/minibuffer.el (completion-category-defaults):
Add 'display-sort-function' with identity for the category 'calendar-month'.
(completion-category-overrides): Add customization for completion sorting
with 'display-sort-function' and a choice like in 'completions-sort'.
(completion-metadata-override-get): New function.
(minibuffer-completion-help): Use 'completion-metadata-override-get'
instead of 'completion-metadata-get' to get sort-fun from
'display-sort-function'.
This commit is contained in:
Juri Linkov 2024-01-04 19:20:30 +02:00
parent d69fb6dab2
commit dc99be8e63
4 changed files with 44 additions and 4 deletions

View file

@ -1878,6 +1878,12 @@ The value should be a list of completion styles (symbols).
The value should be a value for @code{completion-cycle-threshold}
(@pxref{Completion Options,,, emacs, The GNU Emacs Manual}) for this
category.
@item display-sort-function
The possible values are: @code{nil} that means to use either the sorting
function from metadata or if it's nil then fall back to @code{completions-sort};
@code{identity} that means to not use any sorting to keep the original order;
and other values are the same as in @code{completions-sort}.
@end table
@noindent

View file

@ -740,6 +740,12 @@ When 'completions-sort' is set to 'historical', completion candidates
will be first sorted alphabetically, and then re-sorted by their order
in the minibuffer history, with more recent candidates appearing first.
+++
*** 'completion-category-overrides' supports 'display-sort-function'.
You can now customize the sorting order for any category in
'completion-category-overrides' that will override the sorting order
defined in the metadata or in 'completions-sort'.
** Pcomplete
---

View file

@ -2339,7 +2339,11 @@ returned is (month year)."
(month (cdr (assoc-string
(completing-read
(format-prompt "Month name" defmon)
(append month-array nil)
(lambda (string pred action)
(if (eq action 'metadata)
'(metadata (category . calendar-month))
(complete-with-action
action (append month-array nil) string pred)))
nil t nil nil defmon)
(calendar-make-alist month-array 1) t)))
(defday (calendar-extract-day default-date))

View file

@ -1135,12 +1135,14 @@ styles for specific categories, such as files, buffers, etc."
(project-file (styles . (substring)))
(xref-location (styles . (substring)))
(info-menu (styles . (basic substring)))
(symbol-help (styles . (basic shorthand substring))))
(symbol-help (styles . (basic shorthand substring)))
(calendar-month (display-sort-function . identity)))
"Default settings for specific completion categories.
Each entry has the shape (CATEGORY . ALIST) where ALIST is
an association list that can specify properties such as:
- `styles': the list of `completion-styles' to use for that category.
- `cycle': the `completion-cycle-threshold' to use for that category.
- `display-sort-function': the sorting function.
Categories are symbols such as `buffer' and `file', used when
completing buffer and file names, respectively.
@ -1148,10 +1150,16 @@ Also see `completion-category-overrides'.")
(defcustom completion-category-overrides nil
"List of category-specific user overrides for completion styles.
Each override has the shape (CATEGORY . ALIST) where ALIST is
an association list that can specify properties such as:
- `styles': the list of `completion-styles' to use for that category.
- `cycle': the `completion-cycle-threshold' to use for that category.
- `display-sort-function': where `nil' means to use either the sorting
function from metadata or if it's nil then fall back to `completions-sort';
`identity' means to not use any sorting to keep the original order;
and other values are the same as in `completions-sort'.
Categories are symbols such as `buffer' and `file', used when
completing buffer and file names, respectively.
@ -1171,12 +1179,28 @@ overrides the default specified in `completion-category-defaults'."
,completion--styles-type)
(cons :tag "Completion Cycling"
(const :tag "Select one value from the menu." cycle)
,completion--cycling-threshold-type))))
,completion--cycling-threshold-type)
(cons :tag "Completion Sorting"
(const :tag "Select one value from the menu."
display-sort-function)
(choice (const :tag "Use default" nil)
(const :tag "No sorting" identity)
(const :tag "Alphabetical sorting"
minibuffer-sort-alphabetically)
(const :tag "Historical sorting"
minibuffer-sort-by-history)
(function :tag "Custom function"))))))
(defun completion--category-override (category tag)
(or (assq tag (cdr (assq category completion-category-overrides)))
(assq tag (cdr (assq category completion-category-defaults)))))
(defun completion-metadata-override-get (metadata prop)
(if-let ((cat (completion-metadata-get metadata 'category))
(over (completion--category-override cat prop)))
(cdr over)
(completion-metadata-get metadata prop)))
(defun completion--styles (metadata)
(let* ((cat (completion-metadata-get metadata 'category))
(over (completion--category-override cat 'styles)))
@ -2522,7 +2546,7 @@ The candidate will still be chosen by `choose-completion' unless
(aff-fun (or (completion-metadata-get all-md 'affixation-function)
(plist-get completion-extra-properties
:affixation-function)))
(sort-fun (completion-metadata-get all-md 'display-sort-function))
(sort-fun (completion-metadata-override-get all-md 'display-sort-function))
(group-fun (completion-metadata-get all-md 'group-function))
(mainbuf (current-buffer))
;; If the *Completions* buffer is shown in a new