From 070d8b972e5638ce90e7c2bb1608d16e53d516a4 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Tue, 11 May 2021 09:08:05 +0200 Subject: [PATCH 5/5] (minibuffer-completion-help): Add group sorting Allow sorting the groups as returned by the `group-function` of the completion table with the `completions-group-sort-function`. * lisp/minibuffer.el (completions-group-sort-function): New variable. (minibuffer--group-by): Add SORT-FUN argument. (minibuffer-completion-help): Pass `completions-group-sort-function` to `minibuffer--group-by`. --- lisp/minibuffer.el | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 73a38a8137..3399a02014 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1151,6 +1151,14 @@ completions-group :type 'boolean :version "28.1") +(defcustom completions-group-sort-function #'identity + "Sorting function for the groups. +The function takes and returns an alist of groups, where the each +element is a pair of a group title string and the candidate strings +belonging to the group." + :type 'function + :version "28.1") + (defcustom completions-group-format (concat (propertize " " 'face 'completions-group-separator) @@ -1432,16 +1440,21 @@ minibuffer--sort-preprocess-history (substring c base-size))) hist))))) -(defun minibuffer--group-by (fun elems) - "Group ELEMS by FUN." +(defun minibuffer--group-by (group-fun sort-fun elems) + "Group ELEMS by GROUP-FUN and sort using SORT-FUN." (let ((groups)) (dolist (cand elems) - (let* ((key (funcall fun cand nil)) + (let* ((key (funcall group-fun cand nil)) (group (assoc key groups))) (if group (setcdr group (cons cand (cdr group))) (push (list key cand) groups)))) - (mapcan (lambda (x) (nreverse (cdr x))) (nreverse groups)))) + (setq groups (nreverse groups) + groups (mapc (lambda (x) + (setcdr x (nreverse (cdr x)))) + groups) + groups (funcall sort-fun groups)) + (mapcan #'cdr groups))) (defun completion-all-sorted-completions (&optional start end) (or completion-all-sorted-completions @@ -2212,7 +2225,10 @@ minibuffer-completion-help ;; `group-function'. (when group-fun (setq completions - (minibuffer--group-by group-fun completions))) + (minibuffer--group-by + group-fun + completions-group-sort-function + completions))) (cond (aff-fun -- 2.20.1