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
. ]
icomplete
to also respect the order, you can add a (cycle-sort-function . ,#'identity)
to the metadata list.
reverse
instead of identity
to display the list in the original order! Why? (24.3.1)
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"))))
ido-completing-read
is that it has weird newline character ^
from semantic-format-tag-summarize
. As for Helm, I cannot assume everyone use it. That's why completing-read
is the only option.
semantic-format-tag-summarize
gives you.
semantic-format-tag-summarize
returns something at the end, but the character is not displayed in completing-read
or helm-comp-read
. I already wrote another function in place of semantic-format-tag-summarize
, without face yet. For displaying tags (with colors) to users, I still use semantic-format-tag-summarize
but use a custom buffer with text widgets instead.
semantic-format-tag-summarize
returns at the end. I had a solution but I still want to know a solution to completing-read
. I know Helm can make it but just wanted to know if an easy way with completing-read
exists.
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.)
ido-completing-read
is having some problem.
completing-read
with Icicles. You can even just turn on icicle-mode
temporarily (e.g. for the call to completing-read
), using, e.g., macro icicle-with-icy-mode-ON
.
elisp
tag has a specific meaning and it should be used very rarely.