How to return a value instead of a key by reading

(completing-read
 "Complete a foo: "
 '(("foobar1" "~/foobar1/") ("barfoo" "/usr/barfoo/") ("foobaz" "/hello/")))

As shown above, I would like to query "foobar1", "barfoo"and "foobaz", but returning the pair directory in reverse order.

Moverover if I have a hash table like this

(cl-defstruct person ID name)
(setq person-object (make-person :ID 123 :name "foo"))
(setq person-table (make-hash-table))
(pushash (person-ID person-object) person-object person-table)

How can I request the username but the person who returned the identifier?

+4
source share
2 answers

@legoscia provided a good answer: it completing-readdoes not give you access to the value associated with the key that it uses to complete. For example, for the alist argument, COLLECTIONit does not give you access to the cdr of the selected alist key.

alist assoc, alist, - maphash get.

, , , .

, 13-. , vanilla Emacs completing-read , (). Emacs cdr alist . alist , , , ( ), conses.

Icicles, . cdr. , completing-read.

Icicles , completing-read, , , . alist .

, ? (, *Completions*), ?

:

  • , - , .

  • , . Icicles search, ( ). ( , ) ().

  • , , . ( , *Completions*.)

  • Imenu, , , (, ), .

  • (, ), .

  • , Lisp, , .

Icicles, , ?

  • () , . *Completions* . . , . ( , .)

  • *Completions* , , /. ( ) (, ).

  • (, ) .

  • ( *Help*) , .

, Icicles ?

. , () , , (. Tripping Icicles.)

, :

  • icicle-whole-candidate-as-text-prop-p nil.

  • icicle-candidates-alist , completing-read. alist.

  • icicle-get-alist-candidate completing-read, , , alist, cdr.

(. : , Icicles.)

+1

completing-read, , :

(let ((completions '(("foobar1" "~/foobar1/") ("barfoo" "/usr/barfoo/") ("foobaz" "/hello/"))))
  (cadr (assoc (completing-read "Complete a foo: " completions) completions)))

-, , -, , maphash. , , , catch throw, :

(catch 'found-it
  (maphash
   (lambda (key value)
     (when (equal (person-name value) desired-name)
       (throw 'found-it key)))
   person-table))

nil, , desired-name.

+2

Source: https://habr.com/ru/post/1628699/


All Articles