bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#68214: Completion sorting customization by category


From: Juri Linkov
Subject: bug#68214: Completion sorting customization by category
Date: Wed, 03 Jan 2024 18:07:50 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu)

> Would it make sense to define a general
> `completion-metadata-override-get` function instead of
> `completion--display-sort-function`?

Thanks for the suggestion, Daniel.  This is used now in a new patch below.

> This function could be used to look up the other meta data
> functions too, `display-sort-function`, `annotation-function`,
> `affixation-function`, `group-function`, etc.

All these meta data functions could be added later to
completion-category-overrides after pushing the current patch.

> (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)))
>
> I suggest to use `if-let` instead of `let*`, such that an override is
> not retrieved if the category is nil.

Maybe customization of completion-category-overrides could support
a catch-all category `nil` for completions without metadata, like
e.g. `nil` can be used in .dir-locals.el as a catch-all for all modes.
But I'm not sure how useful this would be.

> Besides that, in the `completion-category-overrides`, you use
> `completion--sorting-type` for the `display-sort-function`, while it
> should just be `function`.

A new patch doesn't use anymore `completion--sorting-type`
since there is a small difference between customization choices
in `completions-sort` and `completion-category-overrides`:
`completion-category-overrides` needs the value `identity`
that prevents a fallback to `completions-sort` as `nil` does.

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index fa2dcb4f698..ac07b92acb8 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1148,10 +1153,16 @@ completion-category-defaults
 
 (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 +1182,26 @@ completion-category-overrides
                 ,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" alphabetical)
+                         (const :tag "Historical sorting" historical)
+                         (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 +2547,7 @@ minibuffer-completion-help
              (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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]