]> git.eshelyaron.com Git - emacs.git/commitdiff
Support more metadata properties in completion-extra-properties (bug#68214)
authorJuri Linkov <juri@linkov.net>
Tue, 9 Jan 2024 17:57:50 +0000 (19:57 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 10 Jan 2024 10:33:23 +0000 (11:33 +0100)
* doc/lispref/minibuf.texi (Completion Variables): Add
to the table of completion-extra-properties new items:
`group-function', `display-sort-function', `cycle-sort-function'.

* lisp/icomplete.el (icomplete--augment): Remove unnecessary
plist-get from completion-extra-properties since now
completion-metadata-get does this.

* lisp/minibuffer.el (completion-metadata-get): Use plist-get to
get prop from completion-extra-properties and cache the keyword.
Thanks to Daniel Mendler <mail@daniel-mendler.de>.
(completion-extra-properties): Mention new properties in docstring.
(minibuffer-completion-help): Remove unnecessary
plist-get from completion-extra-properties since now
completion-metadata-get does this.

* lisp/net/eww.el (eww-switch-to-buffer):
* test/lisp/minibuffer-tests.el (completions-affixation-navigation-test):
Unquote lambda in completion-extra-properties.

(cherry picked from commit aff1d53cd466b64ded08d5cf12f83e5746704c07)

doc/lispref/minibuf.texi
etc/NEWS
lisp/icomplete.el
lisp/minibuffer.el
lisp/net/eww.el
test/lisp/minibuffer-tests.el

index 0d497e3b87dfe0810f8f6c1231b014ffb100a922..2f2212067cc55bab8f7bd6a699dcb24da9e228c5 100644 (file)
@@ -2054,6 +2054,15 @@ element of the returned list must be a three-element list, the
 completion, a prefix string, and a suffix string.  This function takes
 priority over @code{:annotation-function}.
 
+@item :group-function
+The function to group completions.
+
+@item :display-sort-function
+The function to sort entries in the @file{*Completions*} buffer.
+
+@item :cycle-sort-function
+The function to sort entries when cycling.
+
 @item :exit-function
 The value should be a function to run after performing completion.
 The function should accept two arguments, @var{string} and
index 86d35f258a942f11fae5116b4937bab3aa42bfba..5a7cfefb1002c2385806011edd54d8c219f425bc 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -783,6 +783,11 @@ completion styles for the current minibuffer.  See Info node
 "(emacs)Completion Styles" for more information about this new
 command, and completion styles in general.
 
++++
+*** 'completion-extra-properties' supports more metadata.
+The new supported completion properties are 'group-function',
+'display-sort-function', 'cycle-sort-function'.
+
 ** Pcomplete
 
 ---
index d49714f320445f86d8738fe8c3b6ea4508b5633f..aa3c5680a7e1c69f102c3495e307df96d84f5a53 100644 (file)
@@ -789,10 +789,8 @@ and SUFFIX, if non-nil, are obtained from `affixation-function' or
 `group-function'.  Consecutive `equal' sections are avoided.
 COMP is the element in PROSPECTS or a transformation also given
 by `group-function''s second \"transformation\" protocol."
-  (let* ((aff-fun (or (completion-metadata-get md 'affixation-function)
-                      (plist-get completion-extra-properties :affixation-function)))
-         (ann-fun (or (completion-metadata-get md 'annotation-function)
-                      (plist-get completion-extra-properties :annotation-function)))
+  (let* ((aff-fun (completion-metadata-get md 'affixation-function))
+         (ann-fun (completion-metadata-get md 'annotation-function))
          (grp-fun (and completions-group
                        (completion-metadata-get md 'group-function)))
          (annotated
index 59c1fdb0b2227c77d7aca71825c5bb18251ad4e2..a90ba93f9ae370d04c66d4913caf8eb7cf89fb0d 100644 (file)
@@ -154,15 +154,25 @@ The metadata of a completion table should be constant between two boundaries."
                        minibuffer-completion-predicate))
 
 (defun completion-metadata-get (metadata prop)
-  "Get PROP from completion METADATA.
+  "Get property PROP from completion METADATA.
 If the metadata specifies a completion category, the variables
 `completion-category-overrides' and
-`completion-category-defaults' take precedence."
+`completion-category-defaults' take precedence for
+category-specific overrides.  If the completion metadata does not
+specify the property, the `completion-extra-properties' plist is
+consulted.  Note that the keys of the
+`completion-extra-properties' plist are keyword symbols, not
+plain symbols."
   (if-let (((not (eq prop 'category)))
            (cat (alist-get 'category metadata))
            (over (completion--category-override cat prop)))
       (cdr over)
-    (alist-get prop metadata)))
+    (or (alist-get prop metadata)
+        (plist-get completion-extra-properties
+                   ;; Cache the keyword
+                   (or (get prop 'completion-extra-properties--keyword)
+                       (put prop 'completion-extra-properties--keyword
+                            (intern (concat ":" (symbol-name prop)))))))))
 
 (defun complete-with-action (action collection string predicate)
   "Perform completion according to ACTION.
@@ -2554,6 +2564,15 @@ These include:
    `:annotation-function' when both are provided, so only this
    function is used.
 
+`:group-function': Function for grouping the completion candidates.
+
+`:display-sort-function': Function to sort entries in *Completions*.
+
+`:cycle-sort-function': Function to sort entries when cycling.
+
+See more information about these functions above
+in `completion-metadata'.
+
 `:exit-function': Function to run after completion is performed.
 
    The function must accept two arguments, STRING and STATUS.
@@ -2797,12 +2816,8 @@ completions list."
                                            base-size md
                                            minibuffer-completion-table
                                            minibuffer-completion-predicate))
-             (ann-fun (or (completion-metadata-get all-md 'annotation-function)
-                          (plist-get completion-extra-properties
-                                     :annotation-function)))
-             (aff-fun (or (completion-metadata-get all-md 'affixation-function)
-                          (plist-get completion-extra-properties
-                                     :affixation-function)))
+             (ann-fun (completion-metadata-get all-md 'annotation-function))
+             (aff-fun (completion-metadata-get all-md 'affixation-function))
              (sort-fun (completion-metadata-get all-md 'display-sort-function))
              (group-fun (completion-metadata-get all-md 'group-function))
              (mainbuf (current-buffer))
index 22f07cbc5b4b71092025fb2c5ed30d281c8bcf67..6c46ef0fedb0dfb68a54b90f47174371c64f13cc 100644 (file)
@@ -2064,9 +2064,10 @@ If CHARSET is nil then use UTF-8."
   "Prompt for an EWW buffer to display in the selected window."
   (interactive nil eww-mode)
   (let ((completion-extra-properties
-         '(:annotation-function (lambda (buf)
-                                  (with-current-buffer buf
-                                    (format " %s" (eww-current-url))))))
+         `(:annotation-function
+           ,(lambda (buf)
+              (with-current-buffer buf
+                (format " %s" (eww-current-url))))))
         (curbuf (current-buffer)))
     (pop-to-buffer-same-window
      (read-buffer "Switch to EWW buffer: "
index 6dc15d0801f61900dc2daf3f39b8fa6e9f9631a9..c1fe3032cb5e1380c135eaa8019616b43b7ce95a 100644 (file)
 
 (ert-deftest completions-affixation-navigation-test ()
   (let ((completion-extra-properties
-         '(:affixation-function
-           (lambda (completions)
-             (mapcar (lambda (c)
-                       (list c "prefix " " suffix"))
-                     completions)))))
+         `(:affixation-function
+           ,(lambda (completions)
+              (mapcar (lambda (c)
+                        (list c "prefix " " suffix"))
+                      completions)))))
     (completing-read-with-minibuffer-setup
         '("aa" "ab" "ac")
       (insert "a")