1

As a starting point, this:

(defvar myfoo "~/org/test-file-1.org" "myfoo") ; C-x C-e
(defun mybar()
(newline-and-indent)
(insert-file-contents myfoo)) ; C-x C-e
(mybar) ; C-x C-e
Iure repudiandae non alias.
Rerum quia dicta sunt.
Omnis veniam velit nemo saepe in.

I'm trying to modify mybar such that, instead of using myfoo, the user is presented with a predefined list of files (say those matching ~/org/test-file-*.org), and is expected to choose the one to be processed. How would I do that?

enter image description here

UPDATE:

Based on the answer,

(setq completions '("~/org/test-file-1.org" "~/org/test-file-2.org")) ; C-x C-e
(defun mybar()
(newline-and-indent)
(insert-file-contents (completing-read "Type something, use TAB for completion: " completions nil t))) ; C-x C-e
(mybar) ; C-x C-e

prompts "Type something, use TAB for completion: ". Followed by TAB, "Type something, use TAB for completion: ~/org/test-file-", and then TAB opens a buffer showing the candidate files: enter image description here

1 Answers1

2

You can use completing-read like this:

(setq completions '("foo" "bar" "baz"))

(completing-read "Type something, use TAB for completion: " completions nil t)

The fourth argument being t will allow the call to return only if a value from the list is chosen. Read the doc string of the function with C-h f completing-read - you can also search the index of the Emacs Lisp Reference manual for it with C-h i g(elisp) RET i completing TAB RET (and note that this last command to get the documentation of completing-read uses completing-read for its implementation - for the record, that's the command Info-index).

NickD
  • 27,023
  • 3
  • 23
  • 42
  • I wished I didn't have to press tab twice to get the candidate files to be displayed. Should I post anew to automate that? –  Mar 29 '23 at 07:52
  • If I press a single TAB at the prompt before typing anything else, I get all the candidates listed. If I've typed something, then a TAB will complete as far as it can (e.g. if I type `b`, it will complete to `ba`) and then a second TAB will show all the candidates that satisfy what's been completed so far. This is standard (and I should say, expected by users) behavior, so I would not do it differently for now. In any case, changing the question thereby invalidating an answer that satisfied whatever conditions you imposed to begin with is frowned upon. – NickD Mar 29 '23 at 21:58
  • Changing the question? I need two tabs to get all the candidate listed. –  Mar 30 '23 at 14:14
  • I described it inaccurately: first TAB gets you the longest common prefix. In my example, there was no common prefix, so I got the complete list; in your case, there *is* a common prefix, so you get that on first TAB and you need to press a second to get the complete list. Since completion works like that in all cases (at least with the out-of-the-box completion framework that Emacs provides), it is consistent and not a burden, but you may think differently. – NickD Mar 30 '23 at 14:42