Either:
(completing-read "test: " '("a" "b" "c"))
or:
(completing-read "test: " '("c" "b" "a"))
produces the same result in completion buffer when pressing TAB. How do I make it respect the sorting order?
Either:
(completing-read "test: " '("a" "b" "c"))
or:
(completing-read "test: " '("c" "b" "a"))
produces the same result in completion buffer when pressing TAB. How do I make it respect the sorting order?
The sorting order in the *Completions* list is determined by the display-sort-function
property of your completion table (as returned by completion-metadata
). In your case, your completion table has no such property, so it falls back to the default, which is to sort alphabetically.
You can use:
(defun my-presorted-completion-table (completions)
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (display-sort-function . ,#'identity))
(complete-with-action action completions string pred))))
and then
(completing-read "test: " (my-presorted-completion-table '("a" "b" "c")))
[ This assumes you're using lexical-binding
. ]
Give completing-read
a list of lists, and it will respect the order:
(completing-read "test: " '(("a") ("b") ("c")))
(completing-read "test: " '(("c") ("b") ("a")))
The docstring says:
(completing-read PROMPT COLLECTION &optional PREDICATE
REQUIRE-MATCH INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)
Read a string in the minibuffer, with completion. PROMPT is a string to prompt with; normally it ends in a colon and a space. COLLECTION can be a list of strings, an alist, an obarray or a hash table. ...
It can therefore take an alist as a collection. In effect, you're creating an alist with keys but without values.
I'd suggest not using this ancient spell.
The built-in ido-completing-read
doesn't have this deficiency:
(ido-completing-read "test: " '("a" "b" "c"))
(ido-completing-read "test: " '("c" "b" "a"))
Neither does helm
:
(helm :sources
`((name . "test: ")
(candidates . ("a" "b" "c"))))
(helm :sources
`((name . "test: ")
(candidates . ("c" "b" "a"))))
If you use Icicles then the order is respected by completing-read
.
(And you can sort using different sort orders, either interactively or via Lisp. And unlike vanilla Emacs, sorting affects both *Completions*
display and cycling order.)