3

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?

kotchwane
  • 513
  • 2
  • 12

1 Answers1

4

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))
Stefan
  • 26,404
  • 3
  • 48
  • 85