Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivy-read with alist #696

Closed
jtamagnan opened this issue Sep 30, 2016 · 4 comments
Closed

Ivy-read with alist #696

jtamagnan opened this issue Sep 30, 2016 · 4 comments

Comments

@jtamagnan
Copy link
Contributor

@jtamagnan jtamagnan commented Sep 30, 2016

It seems like ivy-read no longer works properly with an alist.
For example the following code snippet will message "one" instead of "1" if the first option is chosen

(ivy-read "Choose: "
      '(("one" . 1) ("two". 2) )
      :action (lambda (x) (interactive) (message "%s" x)))

This problem also occurs with counsel-hydra-heads which renders it useless

P.S. Maybe that is how ivy-read is supposed to work and I misunderstood and counsel-hydra-heads is not working for some other reason. This was just my initial impression as to what ivy-read was supposed to do when given an alist.

@abo-abo abo-abo closed this in 9b28927 Oct 1, 2016
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Oct 1, 2016

Thanks, should be fixed now. A cdr call now needs to be added.

@boyechko
Copy link

@boyechko boyechko commented Nov 28, 2019

Could you confirm that passing an alist as the collection (as in jtamagnan's code snippet) actually is not supposed to allow for the user to get a list of "one" and "two" while the :action function would get the choice as 1 and 2, respectively?

I'm trying to present the user with a choice of file names that would then resolve to absolute file paths. Is the following workaround really what one must do, given a list of absolute file paths in files-list?

(let ((choices (mapcar #'(lambda (file)
                           (cons (file-name-base file) file))
                       files-list)))
  (cdr (assoc (ivy-read "Which file to open? " choices) choices)))

Rather than, say, the following, which is what I expected to be able to do:

(ivy-read "Which file to open? "
          (mapcar #'(lambda (file)
                      (cons file (file-name-base file)))
                  files))

What's the point of being able to pass alists or hash tables to ivy-read if only the keys are ever used, disregarding the values? Is it just a matter of it not being implemented yet?

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Nov 28, 2019

@hoybach No need for a workaround, everything works as far as I'm aware:

(ivy-read "Choose: "
          '(("one" . 1) ("two" . 2))
          :action
          (lambda (x)
            (insert (format
                     "car: %s cdr: %d"
                     (car x) (cdr x)))))

Calling this will result in car: one cdr: 1 being inserted. The trouble with the above snippet was the result of ivy-read was "one", even though ("one" . 1) was passed into the action, and the result of the eval will overwrite what the action printed.

@boyechko
Copy link

@boyechko boyechko commented Nov 28, 2019

@abo-abo Oh, I see now. Thanks a lot for the more detailed explanation and the example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants