0

The documentation of ivy-read states that if the collection supplied is an alist, the selected candidate is passed as a cons cell to the function supplied in the :action argument.

So why does the code snippet below return "C1" or "C2", depending on the selection, and not (1 2 3) or (3 4 5), respectively?

(ivy-read "Choose: "
          (list (cons "C1" (list 1 2 3)) (cons "C2" (list 3 4 5)))
          :action (lambda (x) (cdr x)))

I can work around this by doing something like the following, but I don't understand why I have to.

(let (tmp)
  (ivy-read "Choose: "
            (list (cons "C1" (list 1 2 3)) (cons "C2" (list 3 4 5)))
            :action (lambda (x) (setq tmp (cdr x))))
  tmp)
0

1 Answer 1

2

So why does the code snippet below return "C1" or "C2", depending on the selection, and not (1 2 3) or (3 4 5), respectively?

Because the argument passed to :action is not, and is not documented as being, the same thing as the return value of ivy-read. The value returned by ivy-read is usually, but not always, the same as what completing-read would return. This is done to allow ivy-completing-read to be used as a completing-read-function. Ivy :actions, OTOH, are intended for Ivy-specific features like non-exiting completion, multi-actions, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.