4

I have a set of directories. For each directory I have a set of relative file names. I would like to select any of these relative filenames using completion, and then later reconstruct the absolute path of the filename. (The relative filenames can be assumed to be unique). The following code works:

(let ((lst '(("a/b" . "dir1") ("a/c" . "dir2")))
      (select nil))
  (setq select (completing-read "Enter file name: " lst  nil t ""))
  (cdr (assoc select lst)))

Here, the cdr of the cons cells represents (absolute) directory names (simplified to "dir1" and "dir2" for this question) for the given relative path. But it feels like I have missed something. It should be possible to avoid using assoc if completing-read just could return the whole cons cell instead of just the car of the cons cell, but I could not find information about how to achieve that in the manual.

Is it possible to tell completing-read to return more information (i.e. the cons cell)?

1
  • 1
    I know that you can substitute the list of possible matches with a function to do completion, but that doesn't sound like an easier way to do it.
    – wvxvw
    Feb 2, 2015 at 14:21

1 Answer 1

5

Nope. (cdr (assoc select list)) is the way to do it. completing-read returns a string, regardless of whether the COLLECTION arg was an alist, obarray, hash-list, or function.

If you use Icicles then you can get the whole cons cell. (In particular, this is used to be able to have multiple completion candidates with the same name but different associated information.)

Your Answer

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

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