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?

Drew's user avatar

Drew

30.9k7 gold badges81 silver badges111 bronze badges

asked Mar 20, 2016 at 20:09

curious's user avatar

Use minibuffer-setup-hook:

(defun foo () (insert "ABCDE"))

(add-hook 'minibuffer-setup-hook 'foo)

answered Mar 21, 2016 at 2:24

Drew's user avatar

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)

Nope, that's the idiomatic way: add/remove for the hook.

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")))

user333958's user avatar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.