What @JohnKitchin shows is a typical way to return something different from (but associated with) the candidate string that is chosen by the user.
In your case, it sounds like you might want the completion candidates (possible choices) to show more information than what is returned after choosing. E.g., you display a bunch of text describing something, and when one of those candidates is chosen you return only the associated player name.
You can do that using the typical approach, in which case completion is available against all of the displayed text for each candidate.
But if you don't want or need that, and you just want to display some extra text along with each candidate, but not make that extra text matchable, then you can annotate the completion candidates with extra, non-matchable text.
(You can still use the alist technique shown by John, if the value you ultimately want to return is something different from the candidate that is matched and chosen.)
To annotate completion candidates you can define an annotation function, which, given a displayed completion candidate (string), provides its annotation.
You then bind, around the call to completing-read
, variable completion-extra-properties
to a plist that contains :annotation-function
and its associated value, your annotation function.
(defun foo ()
(interactive)
(let ((completion-extra-properties '(:annotation-function my-annot-fn)))
(completing-read "Choose: " '(("aa" . AA-value)
("ab" . AB-value)
("bb" . BB-value)))))
(defun my-annot-fn (candidate)
(cdr (assoc candidate '(("aa" . " Description of AA")
("ab" . " Description of AB")
("bb" . " Description of BB")))))
The completion candidates here are the string values "aa"
, "ab"
, and "bb"
. Their associated annotations are, respectively, the strings " Description of AA"
, " Description of AB"
, and " Description of BB"
. Completion (matching etc.) is available only against the candidates, but users see the annotations right next to them.
The return value, after choosing a candidate (e.g. ab
), is the value associated with it in the COLLECTION
argument to completing-read
. For the candidate choice ab
, the return value is the Lisp symbol AB-value
. (This uses the association-list technique that @JohnKitchin mentions.)