2

I need to prompt user for input, but I also would like to prepend the user's minibuffer input with a string. e.g.:

  • user runs my function
  • after the prompt, the text awesome (hardcoded string) is automatically inserted in the minibuffer
  • user can start typing and it simply appends what she types to awesome, which is already there
  • user types bananas, presses RET, the value returned is awesome bananas

User should also be able to modify awesome part of the string. For example one may chose to send bad bananas instead.

Can someone help me to achieve that? I've tried with completing-read, but can't get it right.

0

2 Answers 2

7

I think what you need is (read-string "input: " "awesome").

3
  • I don't have a read-input function, I think you meant read-string.
    – Omar
    Commented Feb 6, 2019 at 2:13
  • 1
    It looks like read-input is obsolete, and read-string is preferred. I updated the answer. Thanks for noting it! Commented Feb 6, 2019 at 2:52
  • Exactly what I needed. Thanks John!
    – iLemming
    Commented Feb 6, 2019 at 18:48
1

I think you're asking how you can tell completing-read to insert some text in the minibuffer by default, so you can append some more text that you type, to have the returned value be the result of the append.

That is, I don't think you're asking about prepending awesome to the prompt, but rather appending it to the prompt. I think you're saying that you want it to be part of the returned value (by default, i.e., if the user doesn't erase it), not part of the prompt. (I've edited your question along those lines. If I guessed wrong then please reject the edit.)

(completing-read "My prompt: " '("awesome blue" "awesome red" "some" "other")
                 nil nil "awesome " nil "a default, if you want it")

The initial input is the 5th arg. The 3rd arg is nil, so you can enter anything you like - it need not match any of the completion candidates. C-h f completing-read tells you:

completing-read is a built-in function in C source code.

(completing-read PROMPT COLLECTION &optional PREDICATE REQUIRE-MATCH INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)

Read a string in the minibuffer, with completion.

PROMPT is a string to prompt with; normally it ends in a colon and a space.

COLLECTION can be a list of strings, an alist, an obarray or a hash table. COLLECTION can also be a function to do the completion itself.

PREDICATE limits completion to a subset of COLLECTION.

See try-completion, all-completions, test-completion, and completion-boundaries, for more details on completion, COLLECTION, and PREDICATE. See also Info node (elisp)Basic Completion for the details about completion, and Info node (elisp)Programmed Completion for expectations from COLLECTION when it’s a function.

REQUIRE-MATCH can take the following values:

  • t means that the user is not allowed to exit unless the input is (or completes to) an element of COLLECTION or is null.

  • nil means that the user can exit with any input.

  • confirm means that the user can exit with any input, but she needs to confirm her choice if the input is not an element of COLLECTION.

  • confirm-after-completion means that the user can exit with any input, but she needs to confirm her choice if she called minibuffer-complete right before minibuffer-complete-and-exit and the input is not an element of COLLECTION.

  • anything else behaves like t except that typing RET does not exit if it does non-null completion.

If the input is null, completing-read returns DEF, or the first element of the list of default values, or an empty string if DEF is nil, regardless of the value of REQUIRE-MATCH.

If INITIAL-INPUT is non-nil, insert it in the minibuffer initially, with point positioned at the end. If it is (STRING . POSITION), the initial input is STRING, but point is placed at zero-indexed position POSITION in STRING. (Note that this is different from read-from-minibuffer and related functions, which use one-indexing for POSITION.) This feature is deprecated--it is best to pass nil for INITIAL-INPUT and supply the default value DEF instead. The user can yank the default value into the minibuffer easily using M-n.

HIST, if non-nil, specifies a history list and optionally the initial position in the list. It can be a symbol, which is the history list variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable to use, and HISTPOS is the initial position (the position in the list used by the minibuffer history commands). For consistency, you should also specify that element of the history as the value of INITIAL-INPUT. (This is the only case in which you should use INITIAL-INPUT instead of DEF.) Positions are counted starting from 1 at the beginning of the list. The variable history-length controls the maximum length of a history list.

DEF, if non-nil, is the default value or the list of default values.

If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits the current input method and the setting of enable-multibyte-characters.

Completion ignores case if the ambient value of completion-ignore-case is non-nil.

See also completing-read-function.

You'll notice that it tells you that parameter INITIAL-INPUT is deprecated. That's nonsense, IMHO. That just reflects someone's preference for not using it, as a user-interface style. If you want input inserted initially, that's what it's for. Whoever deprecated it is really just telling you that you shouldn't want input inserted initially. If you want it then do it.

1
  • I noticed that INITIAL-INPUT is labelled as "deprecated" in many other functions... i really like to use it so your comment eliminated my doubts.
    – Gabriele
    Commented Dec 19, 2023 at 23:07

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.