I'm trying to insert text into the mini buffer after running an external command. E.G
(call-interactively 'eval-expression)
(insert "blah")
The problem of course is that eval-expression doesn't return before the user has entered input. My end goal is to add some default editable text into the mini buffer prompt of an arbitrary command (i.e a 'default string' or template). How can I go about accomplishing this?
30.9k7 gold badges81 silver badges111 bronze badges
Use minibuffer-setup-hook:
(defun foo () (insert "ABCDE"))
(add-hook 'minibuffer-setup-hook 'foo)
4 Comments
Is there a way to accomplish this temporarily without having to add/remove the hook each time the function call is made? I only want this behavior when a custom function of mine is used.
Just add/remove each time you need to do it "temporarily". It is not costly. Put the add/remove code in whatever function you use that needs it.
Thanks, I just wanted to make sure there isn't a more idiomatic way to do it (adding/removing a hook seems wasteful somehow)
Here is a function definition to write anything to the current minibuffer
(defun write-to-minibuffer (text)
"writes to minibuffer"
(let ((output (or (when (stringp text)
text)
(format "%S" text))))
(ignore-errors
(with-current-buffer (window-buffer (minibuffer-window))
(read-only-mode -1)
(widen)
(erase-buffer)
(end-of-buffer)
(insert output)
(read-only-mode 1)))))
Now one can "print" a plain string to the minibuffer:
(write-to-minibuffer "Hello World")
And one can also write coloful(propertized) strings to the minibuffer
(write-to-minibuffer (propertize "Hello World" 'face
(list :background "#E76F0F"
:foreground "#FFFFFF")))
6151 gold badge5 silver badges4 bronze badges
Comments
Explore related questions
See similar questions with these tags.
