aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSpencer Baugh2023-10-17 09:09:55 -0400
committerJuri Linkov2023-12-03 19:24:16 +0200
commite33f560badac3fd6bd23a6ffc1244afee7dec5f3 (patch)
treeac1db1e51c8885d345a52dc48acd1be27785bcb2
parent3c093148958d56e0ed8e12a8e00ced1ef052259a (diff)
downloademacs-e33f560badac3fd6bd23a6ffc1244afee7dec5f3.tar.gz
emacs-e33f560badac3fd6bd23a6ffc1244afee7dec5f3.zip
Add historical option to completions-sort
Support sorting candidates in *Completions* by the order they show up in the minibuffer history. Also add minibuffer-sort-alphabetically and minibuffer-sort-by-history, which are usable for both completions-sort and display-sort-function. * lisp/minibuffer.el (completions-sort): Document 'historical option. (minibuffer-completion-help): Support 'historical option. (minibuffer-sort-alphabetically) (minibuffer-completion-base, minibuffer-sort-by-history): Add. * etc/NEWS: Announce it.
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/minibuffer.el65
2 files changed, 64 insertions, 6 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 3d26f276604..29f4e5c0b66 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -637,6 +637,11 @@ new command 'minibuffer-choose-completion-or-exit' (bound by
637contents instead. The deselection behavior can be controlled with the 637contents instead. The deselection behavior can be controlled with the
638new user option 'completion-auto-deselect'. 638new user option 'completion-auto-deselect'.
639 639
640*** New value 'historical' for user option 'completions-sort'
641When 'completions-sort' is set to 'historical', completion candidates
642will be sorted by their chronological order in the minibuffer history,
643with more recent candidates appearing first.
644
640** Pcomplete 645** Pcomplete
641 646
642--- 647---
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 382d4458e26..03b64198bcf 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1314,14 +1314,27 @@ completion candidates than this number."
1314(defcustom completions-sort 'alphabetical 1314(defcustom completions-sort 'alphabetical
1315 "Sort candidates in the *Completions* buffer. 1315 "Sort candidates in the *Completions* buffer.
1316 1316
1317The value can be nil to disable sorting, `alphabetical' for 1317Completion candidates in the *Completions* buffer are sorted
1318alphabetical sorting or a custom sorting function. The sorting 1318depending on the value.
1319function takes and returns a list of completion candidate 1319
1320strings." 1320If it's nil, sorting is disabled.
1321If it's the symbol `alphabetical', candidates are sorted by
1322`minibuffer-sort-alphabetically'.
1323If it's the symbol `historical', candidates are sorted by
1324`minibuffer-sort-by-history'.
1325If it's a function, the function is called to sort the candidates.
1326The sorting function takes a list of completion candidate
1327strings, which it may modify; it should return a sorted list,
1328which may be the same.
1329
1330If the completion-specific metadata provides a
1331`display-sort-function', that function overrides the value of
1332this variable."
1321 :type '(choice (const :tag "No sorting" nil) 1333 :type '(choice (const :tag "No sorting" nil)
1322 (const :tag "Alphabetical sorting" alphabetical) 1334 (const :tag "Alphabetical sorting" alphabetical)
1335 (const :tag "Historical sorting" historical)
1323 (function :tag "Custom function")) 1336 (function :tag "Custom function"))
1324 :version "29.1") 1337 :version "30.1")
1325 1338
1326(defcustom completions-group nil 1339(defcustom completions-group nil
1327 "Enable grouping of completion candidates in the *Completions* buffer. 1340 "Enable grouping of completion candidates in the *Completions* buffer.
@@ -1647,6 +1660,44 @@ Remove completion BASE prefix string from history elements."
1647 (substring c base-size))) 1660 (substring c base-size)))
1648 hist))))) 1661 hist)))))
1649 1662
1663(defun minibuffer-sort-alphabetically (completions)
1664 "Sort COMPLETIONS alphabetically.
1665
1666COMPLETIONS are sorted alphabetically by `string-lessp'.
1667
1668This is a suitable function to use for `completions-sort' or to
1669include as `display-sort-function' in completion metadata."
1670 (sort completions #'string-lessp))
1671
1672(defvar minibuffer-completion-base nil
1673 "The base for the current completion.
1674
1675This is the part of the current minibuffer input which comes
1676before the current completion field, as determined by
1677`completion-boundaries'. This is primarily relevant for file
1678names, where this is the directory component of the file name.")
1679
1680(defun minibuffer-sort-by-history (completions)
1681 "Sort COMPLETIONS by their position in `minibuffer-history-variable'.
1682
1683COMPLETIONS are sorted first by `minibuffer-sort-alphbetically',
1684then any elements occuring in the minibuffer history list are
1685moved to the front based on the chronological order they occur in
1686the history. If a history variable hasn't been specified for
1687this call of `completing-read', COMPLETIONS are sorted only by
1688`minibuffer-sort-alphbetically'.
1689
1690This is a suitable function to use for `completions-sort' or to
1691include as `display-sort-function' in completion metadata."
1692 (let ((alphabetized (sort completions #'string-lessp)))
1693 ;; Only use history when it's specific to these completions.
1694 (if (eq minibuffer-history-variable
1695 (default-value minibuffer-history-variable))
1696 alphabetized
1697 (minibuffer--sort-by-position
1698 (minibuffer--sort-preprocess-history minibuffer-completion-base)
1699 alphabetized))))
1700
1650(defun minibuffer--group-by (group-fun sort-fun elems) 1701(defun minibuffer--group-by (group-fun sort-fun elems)
1651 "Group ELEMS by GROUP-FUN and sort groups by SORT-FUN." 1702 "Group ELEMS by GROUP-FUN and sort groups by SORT-FUN."
1652 (let ((groups)) 1703 (let ((groups))
@@ -2440,6 +2491,7 @@ The candidate will still be chosen by `choose-completion' unless
2440 (let* ((last (last completions)) 2491 (let* ((last (last completions))
2441 (base-size (or (cdr last) 0)) 2492 (base-size (or (cdr last) 0))
2442 (prefix (unless (zerop base-size) (substring string 0 base-size))) 2493 (prefix (unless (zerop base-size) (substring string 0 base-size)))
2494 (minibuffer-completion-base (substring string 0 base-size))
2443 (base-prefix (buffer-substring (minibuffer--completion-prompt-end) 2495 (base-prefix (buffer-substring (minibuffer--completion-prompt-end)
2444 (+ start base-size))) 2496 (+ start base-size)))
2445 (base-suffix 2497 (base-suffix
@@ -2506,7 +2558,8 @@ The candidate will still be chosen by `choose-completion' unless
2506 (funcall sort-fun completions) 2558 (funcall sort-fun completions)
2507 (pcase completions-sort 2559 (pcase completions-sort
2508 ('nil completions) 2560 ('nil completions)
2509 ('alphabetical (sort completions #'string-lessp)) 2561 ('alphabetical (minibuffer-sort-alphabetically completions))
2562 ('historical (minibuffer-sort-by-history completions))
2510 (_ (funcall completions-sort completions))))) 2563 (_ (funcall completions-sort completions)))))
2511 2564
2512 ;; After sorting, group the candidates using the 2565 ;; After sorting, group the candidates using the