4

My current configuration of Emacs (24.5.1 on U15.10) has auto-suggest set up for find-file, that is, when I do C-x C-f, the mini-buffer fills with possible completions that I can tab to and hit enter to select. I'd like to do that on an interactive function I'm writing where it would auto-suggest each element of this list

((thing1a . thing1b) (thing2a . thing2b) ... )

Is there boilerplate code for this someone knows about?

  • Have you tried just using icomplete-mode? That presents completion candidates in the minibuffer, and you can choose any of them by hitting a key. C-h f icomplete-mode RET. – Drew Dec 3 '15 at 17:51
  • 1
    Could you clarify what you mean by "auto-suggest"? Do you mean auto-complete, or something like helm or ido-mode? – Dan Dec 3 '15 at 18:07
  • Yes, ido-mode is enabled and that's the sort of behavior I'm looking for. Sorry I didn't mention this. – 147pm Dec 3 '15 at 19:02
  • Found emacswiki.org/emacs/InteractivelyDoThings and am looking at (ido-completing-read "What, ... is your favorite color? " mylist) section. Need it to work with my list. – 147pm Dec 3 '15 at 19:29
  • . . . seems like I need to convert the list elements to strings to fit into ido-completing-read – 147pm Dec 3 '15 at 19:47
3

With a combination of ido-completing-read and a conversion of the list items to strings, we may achieve this effect:

(setq mystrlist (mapcar (lambda (l-item) (format "%s" l-item)) mylist))

then

(ido-completing-read "What is your favorite thing? " mystrlist)

Running this should pop up each list item as a possible completion.

2

My suggestion is to use code like this:

(let ((lst '(("John" . "John Smith")
             ("James" . "James Bond"))))
  (assoc (completing-read "Test" lst) lst))

Here, lst is an alist, which is a valid collection type to pass to completing-read. If you want completing-read to use Ido, use ido-ubiquitous to set completing-read-function accordingly. But the code above automatically will adapt to whatever completing mode you have (default/helm/ido/ivy).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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