1

How can one pass a list of (arbitrary) functions to completing-read and run the selected candidate? Something like this:

(defun find/buffer ()
  (interactive)
   (completing-read "Chose one: "
            '(("find-file" . 'find-file)("switch-to-buffer" . 'switch-to-buffer))))

1 Answer 1

1
  1. Remove the quote in front of each function symbol. You've already quoted the alist.

  2. Just look up the choice in the alist to get the function, then call it.

(defun find/buffer ()
  (interactive)
  (let* ((choices  '(("find-file" . find-file)
                     ("switch-to-buffer" . switch-to-buffer)))
         (choice   (completing-read "Choose one: " choices)))
    (call-interactively (cdr (assoc choice choices)))))

But if you make no difference between the name to choose and the function-symbol name then you might as well just do this:

(defun find/buffer ()
  (interactive)
  (call-interactively
    (intern (completing-read "Choose one: " '(find-file switch-to-buffer)))))
1

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.