1

I can read a string in the minibuffer, with completion, using completing-read:

(completing-read "Chose one: " '("Han Solo" "Chewbacca" "Lando Calrissian"))

I would like to use my own completion function, which searches some of my files, instead of a list. The Programmed Completion page of the manual details just how to do that, but does not give any example.

The best I could achieve is this :

(defun completion-function (input-string predicate flag)
  (if flag
      '("Chewbacca" "C3-PO" "Calrissian")
  "C3-PO"))

(completing-read "Chose one: " 'completion-function)

However, this does not behave at all like I would :

  • it suggests only one choice on the first TAB press (I would like all of them to be shown, as what shows a second TAB push)
  • it does not filter the results on the input string (do I need to implement that as well?)

Can someone give a better, more complete example?

2

Your completion-function is a function, but not a proper completion table (it does not obey all the expected behavior).

I recommend you use completion-table-dynamic to build a proper completion table function from a function that simply lists all the possible completions.

E.g.

(defun my-completion-function (prefix)
  ;; You can just ignore the prefix
  '("Chewbacca" "C3-PO" "Calrissian"))

(completing-read "Chose one: "
                 (completion-table-dynamic #'my-completion-function))
|improve this answer|||||

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.