4

Let's say I want to provide partial input to an interactive function such as insert-char. For example, I might want to have a command that automatically starts typing GREEK SMALL LETTER or BOX DRAWINGS.

This does not work, because call-interactively exits before insert is called:

(defun insert-box-drawing-char ()
  (interactive)
  (call-interactively 'insert-char)
  (insert "BOX DRAWING"))

How can I provide partial input to an interactive function?

Matthew Piziak
  • 6,038
  • 3
  • 31
  • 79
  • Sounds like this might be an X-Y question. Maybe try to explain why you think you want to do this - what you are really trying to do. Just a suggestion. – Drew Apr 17 '15 at 05:17
  • In this case, my use case of inserting specific characters is presented purely as example. An interesting X question in this case would be "How do I efficiently work with a subset of characters?". Another one would be "How can I filter the choices to an interactive function?". If those sound interesting, it would be great of you to add them to the community wiki. In this case I am curious about the technique of inserting text into the minibuffer, since I believe it may be a generally useful thing to know. – Matthew Piziak Apr 17 '15 at 15:35
  • This will not help for a use case such as this one, where you do not have access to the call that initiates minibuffer reading (what's more, insert-char is coded in C), but it's maybe good to remind readers that both completing-read and read-from-minibuffer let you pass an INITIAL-INPUT argument, and you can position the cursor anywhere within that string, which is inserted in the minibuffer to start with. (Vanilla Emacs considers INITIAL-INPUT to be deprecated for completing-read, but that proscription can be ignored.) – Drew Apr 19 '15 at 15:25

1 Answers1

5

So you want to invoke a command and have a given string automatically inserted at that command's interactive prompt.

You can do this using minibuffer-setup-hook, and there is a handy macro which takes care of adding and removing the desired function to this hook, while ensuring that it only runs when you wanted it to run:

(minibuffer-with-setup-hook
    (lambda () (insert "BOX DRAWING"))
  (call-interactively 'insert-char))
phils
  • 50,977
  • 3
  • 79
  • 122