1

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.

3
  • The manual does not say to flet anything. completing-read-function is a variable which you can let-bind (like any dynamic variable). You would bind that variable to a function value.
    – phils
    Commented Oct 17 at 20:49
  • To override that for a function you don't control, you would need to use advice. You would need to verify that the function you advised does not trigger any other calls to completing-read, or otherwise figure out a way to handle all of the calls sensibly.
    – phils
    Commented Oct 17 at 20:52
  • Alternatively, you could override the function in question with a custom definition that didn't use completing-read.
    – phils
    Commented Oct 17 at 20:53

1 Answer 1

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.

3
  • 1
    (Nit:) (when (null foo)) = (unless foo).
    – Drew
    Commented Jan 1, 2022 at 20:34
  • 1
    Hi @NickD, the function X in question is counsel-org-goto, which isn't my function. I'd like to refrain from changing it, and instead, when it calls completing-read, have a user-supplied constant value automatically fed in. Commented Jan 5, 2022 at 18:50
  • You should have specified that in your question. I do not have time to check (and I know nothing about counsel), so I'm afraid I can't help. Maybe somebody else will provide an answer.
    – NickD
    Commented Jan 5, 2022 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.