4

I want to do a completing read with the *Completions* buffer already shown (because I'm providing initial input that I already know is an ambiguous prefix). How can I do it?

I don't see an option for this in completing-read. I want a method that works in both Emacs 23 (where completing-read is defined in C) and in Emacs 24 (where completing-read relays to the function indicated by the variable completing-read-function, so I suppose I could just change that) — of course a solution based on (if (boundp 'completing-read-function)) is fine.

Although my immediate use case is with a known ambiguous initial input, something that works regardless of that would be nice. Also, if the user has overridden the default read mechanism (e.g. by redefining completing-read to invoke ido-completing-read or anything-completing-read or by turning on Icicles or Helm), I'd like my code to achieve the same effect, i.e. start the completion mechanism with the list of completions already shown.

6

Last time I needed this, I used something like:

(minibuffer-with-setup-hook
    #'minibuffer-completion-help
  (completing-read ...))
1

My quick-and-dirty solution (tested with Emacs 23 but it should work with any version) was to pretend that the user had pressed Tab — since I already know that the input is ambiguous, this displays the *Completions* buffer. It works well enough in practice, but it's a very dirty hack since it assumes that the Tab key is bound to performing completion, and it only works if the initial input is an ambiguous prefix.

(setq unread-command-events (cons 'tab unread-command-events))
(completing-read message completions nil nil known-ambiguous-prefix history-variable)
1

If Icicles counts for you as part of "etc." in your "Solutions that work with... etc.", then you need only set or bind variable icicle-show-Completions-initially-flag to non-nil.

If it does not, try calling display-completion-list (from buffer *Completions*) when you want *Completions* to be displayed. If you want all completions for your known-ambiguous input to be displayed, then use all-completions to calculate the list of completions, and pass that list to display-completion-list.


Update:

Based on your comments, Stefan's answer works with or without Icicles (e.g., with Icicle mode on or off):

(defun foo ()
  (interactive)
  (let ((comps  (all-completions known-ambiguous-prefix completions)))
    (minibuffer-with-setup-hook
      #'minibuffer-completion-help
      (completing-read "Choose: " comps))))

But with Icicles you can get the same behavior more simply:

(defun foo ()
  (interactive)
  (let ((comps  (all-completions known-ambiguous-prefix completions))
        (icicle-show-Completions-initially-flag  t))
    (completing-read "Choose: " comps)))
  • By “solutions that work with…”, I meant solutions that work if the user has replaced the basic completion machinery by a more powerful package, not solutions that require an extra package — obviously I don't prefer to require an extra package. – Gilles 'SO- stop being evil' Dec 28 '14 at 14:15
  • 1. I don't know what you mean by "the user has replaced the basic completion machinery by a more powerful package" - especially what that might mean without such a package being required. But whatever... 2. Did you try all-completions and display-completion-list? In what way does that not do what you want? 3. So far, your question remains not very clear, at least to me. – Drew Dec 28 '14 at 17:50
  • 1. Whether to use Icicles/Helm/… is a user choice. I want my code to be compatible with any choice the user has made. For example, if the user has (defalias 'completing-read 'ido-completing-read), I want my code to work reasonably. 2. all-completions and display-completion-list are just some of the building bricks: I'd need to write (copy?) more code to create and display the completions buffer. 3. I've edited the bit that bothers you in my question. I've also posted an answer which does what I want in emacs -q but isn't robust to user customizations. – Gilles 'SO- stop being evil' Dec 28 '14 at 19:16
  • In that case, then with or without Icicles, the code that Stefan suggested does just what you want. With Icicles, it is fine as suggested, but it is not needed - all you need is to bind icicle-show-completions-initially-flag around your call to completing-read. The behavior in both cases is the same. – Drew Dec 29 '14 at 1:18
0

If you activave helm-mode: (helm-mode 1)and whenever completing-read happens, it shows all candidates in a Helm buffer for interactive selecting. With helm-mode on, no *Completions* buffer will show up because it is replaced by Helm buffer. It works with prefix too. For example, you can try this with helm-mode activated:

(completing-read "test: " '("aaa" "bbb" "abc" "abbbb") nil t "ab")

A Helm buffer appears and with only two candidates with "ab" prefix: "aabc" and "abbbb".

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.