15
votes
Unadvise a function (remove all advice from it)
(defun advice-unadvice (sym)
"Remove all advices from symbol SYM."
(interactive "aFunction symbol: ")
(advice-mapc (lambda (advice _props) (advice-remove sym advice)) sym))
14
votes
How to write a transparent "pass-through" function wrapper?
I had to solve a very similar problem in nadvice.el, so here is a solution (which uses some of the code from nadvice.el):
(defun wrapper (&rest args)
(interactive (advice-eval-interactive-spec
...
13
votes
Accepted
How to write a transparent "pass-through" function wrapper?
Of course it is possible inclusive the interactive specification. We are dealing here with elisp! (Lisp is the language where the most important constructs are lists. Callable forms are just lists. So ...
12
votes
How can I remove an unnamed advice?
There's advice-mapc, which let's one iterate over all advices of some function, applying a given function to each. With it, it's easy to remove all advices:
(advice-mapc
(lambda (adv prop)
(...
11
votes
List advice for functions
As lawlist correctly points out in a comment, the Emacs Help system, invoked via C-hfNAMERET (M-xdescribe-functionRETNAMERET) in this case, is a quick way to interactively check whether there is any ...
7
votes
Override a function locally, but allow calls to the original function
Store the original function (obtained with symbol-function) in a local variable, and use funcall to call the function object stored in that variable. Cumbersome, but it mostly works.
(defadvice TeX-...
7
votes
How to bypass (or disable) a command's "advice"?
The defadvice macro is almost obsolete, having been replaced by an improved system that uses advice-add, advice-remove, etc, which allows you to turn advice on and off, among other things. See the ...
7
votes
Accepted
On monkey-patching a third-party function
For variety, here's a solution using the :around advice.
Copy the below test snippet to the *scratch* buffer and evaluate the progn form.
(progn
(defvar last-enabled-foo nil)
(setq last-enabled-...
7
votes
After-advice for disabling evil-mode in ansi-term has no effect
Disabling Evil is the wrong approach to this problem, customize the initial state for term instead:
(evil-set-initial-state 'term-mode 'emacs)
7
votes
Accepted
One-time advice
You don't need a macro for this. And you don't need (but you can certainly use) lexical binding.
;; Without lexical binding:
;;
(defun advise-once (symbol where function &optional props)
(...
7
votes
Accepted
How can I remove an unnamed advice?
You can also call advice-remove with the same lambda expression, i.e., replace advice-add with advice-remove and delete :around, then C-x C-e.
6
votes
Accepted
Trace calls to a function with the parameters
The easiest way to find out the arguments passed to a function is to use M-x trace-function RET name-of-your-function RET. Once you're done, use M-x untrace-function or M-x untrace-all.
Note that ...
6
votes
Call function without calling advice defined on it
I've thought about this before, and there's a less-than-perfect solution:
you need to store the original definition before any advice-adding.
(defun f ()
(insert "233\n"))
(setq f.backup ...
5
votes
Accepted
Can you intercept a function and return your own value using advice?
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)))
<...
5
votes
How to manipulate argument list in nadvice.el?
This seems unfortunate in that, in order to produce a similar effect, it's necessary to copy-paste code: [...] I can copy ansi-term's interactive form
On the contrary, I think it would be a good idea ...
5
votes
How to bypass (or disable) a command's "advice"?
If you have an Emacs version prior to release 24.4 then you have the doc for `defadvice' available from within Emacs. In that case, Ask Emacs!
C-h i to open Info, the doc browser, in Emacs. Then ...
5
votes
Accepted
Creating a local advice for a function
Let us collect the facts:
The function cell of a symbol is not buffer-local. Therefore, if you want to change the behavior of the function buffer-locally you need a buffer-local variable or a hook-...
5
votes
Accepted
How to suppress `Wrote /home/user/.emacs.d/recentf` message in minibuffer
This should do it.
(defun no-msg (function)
"Prevent FUNCTION from showing `Wrote <FILE>' messages.
\(The messages are still logged to `*Messages*'.)"
(let ((inhibit-message t))
...
4
votes
How to bypass (or disable) a command's "advice"?
I agree with PythonNut that the best solution is probably not to use advice at all, but to define a new command my-find-file instead.
This said, if you find that advising find-file is a better option ...
4
votes
On monkey-patching a third-party function
I agree that using advice is probably the way to go, but if you really, truly have to, you can use something like this. (Comments are welcome.)
(defun third-party-func ()
(message "original"))
(...
4
votes
How to manipulate argument list in nadvice.el?
Here's how I'd do it:
(defun my-ansi-term-prompt-for-name (orig-fun program
&optional buffer-name &rest args)
(apply orig-fun program
(or ...
4
votes
Call a function before a specific major mode starts
This seems to work. Apparently, defadvice is old-fashioned, and we're supposed to use advice-add instead.
(defun jk-advice ()
(message "Kilroy was here"))
(advice-add 'python-mode :before #'jk-...
4
votes
Argument Access in Advice
You're reading the doc for Emacs-21's advice (i.e. defadvice) and use it with Emacs-25's advice (i.e. advice-add).
All th ad-* thingies are specific to defadvice. So your example code should be ...
4
votes
Accepted
Rewriting `defadvice' as `advice-add' makes advice ineffective
(advice-add #'undo-tree :filter-return #'undo-tree-advice-history-save-file-name)
This advises the function undo-tree, whereas the defadvice form advises undo-tree-make-history-save-file-name. The ...
4
votes
Accepted
How to automatically kill a shell buffer when the shell process exits
Use a process sentinel when you want to react to a process exiting. e.g.:
Refer to C-hig (elisp)Sentinels
In this case shell only calls shell-mode when (and after) starting the inferior process, so ...
4
votes
Accepted
How does one "extend" an existing function?
TL;DR: As mentioned by the comments on your initial question, you are looking for Advising Functions.
Even though this question has been idle for a while at the time of my writing this, I wanted to ...
4
votes
How to add a locally override the message function?
This macro adds support for adding a suffix to messages.
Using advice allows this to be nested, so multiple functions can add their own suffixes which accumulate onto the end.
(defmacro with-temp-...
4
votes
Accepted
Why advice won't work if the function is called from another compiled function?
This (narrow-to-region) is the exact example covered by Chris Wellon's article The Limits of Emacs Advice. As @xuchunyang has already pointed out, the issue is that this function has its own byte ...
4
votes
Accepted
Is there a "switch to buffer" hook?
If you're using Emacs 27.1 or later, you can use:
C-hv window-selection-change-functions
Use add-hook with the LOCAL argument set a function for a specific buffer.
For more details see C-hig (elisp)...
3
votes
Accepted
Call a function before a specific major mode starts
Do exactly what you want!
That means define a function as follows and run this function instead of pdb.
(defun mypdb ()
"Start `pdb' with my own setup."
(interactive)
(my-setup-function)
(pdb)...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
advice × 101nadvice × 14
hooks × 10
functions × 8
evil × 7
elisp × 6
org-mode × 5
arguments × 4
interactive × 3
init-file × 2
org-babel × 2
indentation × 2
dired × 2
customize × 2
byte-compilation × 2
error-handling × 2
message × 2
let-binding × 2
narrowing × 2
dynamic-scoping × 2
cl × 2
local-functions × 2
key-bindings × 1
org-export × 1
latex × 1