Is it possible to use advice to not only run your function first, but also prevent the advised function from running at all?

For example, say a command ordinarily calls read-from-minibuffer to get a return value:

(defun get-name ()
  (read-from-minibuffer "Enter your name: "))

Let's say, in this example, my name never canges. Entering it manually is time-consuming. Can I use the advice system to intercept the call to read-from-minibuffer and return my own value?

For example, something like:

(defun return-my-name (&rest args)

(defun quick-get-name ()
  ;; Add the intercepting advice, call the function, then remove the advice.
  (advice-add 'read-from-minibuffer :intercept 'return-my-name)
  (advice-remove 'read-from-minibuffer 'return-my-name))

The key behaviour I need is to be able to fall back on the original function depending on the input. For example, I might want to intercept the command if the prompt is asking for a name, but not if it is asking for some other information, like age:

(defun return-my-name (prompt &rest args)
  (if (eq prompt "Enter your name: ")
    (read-from-minibuffer prompt))

Anyone know if the advice system is capable of this? Can it be done some other way, for example using aliases?

1 Answer 1


Of course you can. You can do it with:

(advice-add 'read-from-minibuffer :around #'return-my-name)
(defun return-my-name (orig-fun &rest args)
  (let ((orig-val (apply orig-fun args)))

Or you can do it with

(advice-add 'read-from-minibuffer :filter-return #'return-my-name)
(defun return-my-name (orig-val)

depending on how much control you want to have.

  • In the filter-return version, the original function will still prompt iiuc
    – YoungFrog
    Aug 25, 2016 at 7:37
  • I am stating the obvious but in the :around version you are free to not use orig-fun.
    – YoungFrog
    Aug 25, 2016 at 7:40
  • Yes, in the :filter-return case you only get to modify the return value (and only based on the default return value, i.e. without knowing what were the arguments), whereas in the :around you get to do whatever you want, including calling the orig-fun as many times as you want (including 0 times), with whatever other parameters you like and combine the result however you please.
    – Stefan
    Aug 25, 2016 at 12:07
  • This is perfect! Thank you Stefan. YoungFrog, I think that's a helpful caveat. Makes it easier to understand the meaning of the snippets. Cheers guys!
    – JCC
    Aug 25, 2016 at 13:17

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.