Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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))
xuchunyang's user avatar
  • 14.6k
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) (...
Daniel Jour's user avatar
12 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 ...
Basil's user avatar
  • 12.4k
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.
xuchunyang's user avatar
  • 14.6k
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)
wasamasa's user avatar
  • 22.2k
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) (...
Drew's user avatar
  • 77.9k
7 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-...
7 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 ...
shynur's user avatar
  • 5,318
6 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))) <...
Stefan's user avatar
  • 26.5k
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 ...
YoungFrog's user avatar
  • 3,526
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)) ...
Drew's user avatar
  • 77.9k
5 votes
Accepted

Call function without calling advice defined on it

I can't find a public interface for that. In Emacs 25.5 and Emacs 27.1 and presumably every version in between (I haven't checked older or more recent versions), for new-style advice (define-advice, ...
Gilles 'SO- stop being evil''s user avatar
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-...
Jon Kåre Hellan's user avatar
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 ...
Stefan's user avatar
  • 26.5k
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 ...
Stefan's user avatar
  • 26.5k
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 ...
npostavs's user avatar
  • 9,203
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 ...
phils's user avatar
  • 51.5k
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 ...
cyberbisson's user avatar
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-...
ideasman42's user avatar
  • 8,826
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 ...
phils's user avatar
  • 51.5k
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)...
phils's user avatar
  • 51.5k
3 votes

Adding advice to an autoloaded function

Don't make the advice conditional, but only its effect: (defadvice something (before fix-something enable) (when enable-something-advice-p ;;; do stuff )) Tho of course I'd use advice-add ...
Stefan's user avatar
  • 26.5k
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)...
Tobias's user avatar
  • 33.2k
3 votes
Accepted

Help with function advice: Unknown add-function location ‘asm-calculate-indentation’

Drew's answer correctly identifies the source of the error message, but there are some additional problems with the code. (defun yasm-calculate-indentation (oldfun) "Add the formatting for a few ...
Basil's user avatar
  • 12.4k
3 votes

How do I use an advice to change the definition of goto-char within a function?

(Caveat: All from memory; I might have some of this wrong.) WRT your code: (lambda (orig-fun &rest args) (cl-labels ((goto-char (pos) (goto-char pos) ...
phils's user avatar
  • 51.5k
3 votes

Adding a function call after a builtin runs

reposition-window calls beginning-of-defun to compute the window boundaries. So you get an infinite recursion when calling reposition-window from c-beginning-of-defun. You can add the following guard ...
Tobias's user avatar
  • 33.2k
3 votes
Accepted

How to override a function in a function only in that function?

Expanding @lawlist's comment above, here's how to do it with cl-letf. Let's start with a simple example: you have a function foo which calls a function bar with an argument (foo is the analog of the ...
NickD's user avatar
  • 30.5k
3 votes
Accepted

Translating `defadvice` to `advice-add` for modification of modes

I agree with Drew’s advice (pun intended); there is no urgent need to convert to advice-add. It’ll be years before defadvice goes away. Still, might as well point out the obvious. Use C-h f to look up ...
db48x's user avatar
  • 18.2k
2 votes

Shallowly override functions within a target function

Maybe you did not indicate the full problem you want to solve. But based only on your description, there are simpler ways to handle it. Two obvious ones come to mind (see below). You might want to ...
Drew's user avatar
  • 77.9k

Only top scored, non community-wiki answers of a minimum length are eligible