In a function X that calls completing-read
, I'd like to be able to call X and pass in a constant input without invoking the minibuffer. From the emacs manual, it seems I'm supposed to flet
completing-read-function
, but I'm not sure the correct way to do this.
Asked
Active
Viewed 165 times
1

extremeaxe5
- 659
- 3
- 11
1 Answers
0
Assuming that you want the constant input to be optional, you can declare the function as taking an optional argument and then check if the argument is actually passed: if it is, go ahead and use it; if it isn't, then call completing-read
.
Something like this:
(defun X (&optional foo)
(message (or foo (completing-read "Enter foo value: " nil)))
where I assume that the value of foo
is a string.
If you call it with M-: (X)
, then foo
will be nil
and you will get the prompt. If you call it with M-: (X "bar")
then no prompt will be issued and the value of foo
will be the string bar
.

Phil Hudson
- 1,741
- 10
- 13

NickD
- 29,717
- 3
- 27
- 44
(when (null foo))
=(unless foo)
. – Drew Jan 01 '22 at 20:34counsel-org-goto
, which isn't my function. I'd like to refrain from changing it, and instead, when it callscompleting-read
, have a user-supplied constant value automatically fed in. – extremeaxe5 Jan 05 '22 at 18:50