konr
konr

Reputation: 2565

Giving a value to a function that requires the minibuffer

Suppose I have a function (foo) defined as (defun foo () (read-from-minibuffer "What? ")). I cannot change the definition, but I'd like to wrap it around a macro, or another function, so to avoid having to manually give any value.

Unfortunately the following solutions don't work, as (exit-minibuffer) is only called after leaving the minibuffer, so I was wondering if you know of something.

(defmacro return-an-empty-string (&rest code) `(progn ,@code (exit-minibuffer)))
(defun return-an-empty-string (function) (funcall function) (exit-minibuffer))

Thanks!

Upvotes: 3

Views: 110

Answers (2)

sergv
sergv

Reputation: 398

If you have access to function source you can always make your own function that will do almost the same thing and substitute it for original function.

Suppose somewhere you have function foo in file foo.el

(defun foo ()
   ...)

Now you can add to your .emacs

(eval-after-load "foo"
                 '(progn
                   (defun foo+ ()
                     ...)
                   (fset 'foo 'foo+)))

and when foo.el will be loaded, if not already, your foo+ will substitute original foo.

Upvotes: 0

Sean
Sean

Reputation: 29772

You can temporarily make read-from-minibuffer into a do-nothing function:

(require 'cl)
(defmacro preempt-minibuffer (&rest body)
  `(flet ((read-from-minibuffer (&rest ignore)))
     ,@body))

Upvotes: 3

Related Questions