Hot answers tagged

58 votes

Practical benefits of new advice system in Emacs 24.4

The new advice system has the following advantages: add-function: This is brand new functionality that simply was not part of defadvice. It is a kind of hybrid between add-hook and defadvice. In ...
  • 25.5k
33 votes
Accepted

How do I use nadvice?

All information you need is included in C-h f add-function which describes the underlying mechanism of advice-add. The new advice system basically acts like replacing the current definition of a ...
  • 1,501
17 votes
Accepted

Emacs let-bound advice

Couldn't you use (cl-)letf while referencing the original function yourself? Something like this: ;; Original function (defun my-fun (arg) (message "my-fun (%s)" arg)) ;; Standard call (my-fun "...
17 votes

How to enter view-only mode when browsing Emacs source code from help?

You can use directory-local variables to make Emacs' source files read-only by default. (See also C-hig (emacs) Directory Variables RET). Create a file called .dir-locals.el at the root of the ...
  • 44.3k
14 votes
Accepted

before-save-hook for cc-mode

One fairly common way of restricting the action of a hook to a particular major mode is to check the value of major-mode. For example, this hook prints a message before saving buffers using c++-mode: ...
  • 9,002
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 ...
  • 30.8k
13 votes

Change focus to *Help* buffer after running describe-mode

Customize the variable help-window-select: "Non-nil means select help window for viewing. Choices are: never (nil) Select help window only if there is no other window on its frame. ...
  • 18k
13 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 ...
  • 25.5k
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) (...
10 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))
  • 13.8k
9 votes

Practical benefits of new advice system in Emacs 24.4

One of the clear benefits of advice being normal functions is that you can visit a definition with find-function. At present, when viewing the help for a (new-style) advised function, it provides a ...
  • 44.3k
8 votes

Practical benefits of new advice system in Emacs 24.4

From the file header: ;;; Commentary: ;; This package lets you add behavior (which we call "piece of advice") to ;; existing functions, like the old `advice.el' package, but with much fewer ;; bells ...
  • 4,998
8 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 ...
  • 11.5k
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 ...
  • 20.7k
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-...
  • 24.5k
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) (...
  • 71k
6 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-...
6 votes
Accepted

How to get the arguments of an advised function?

You can use ad-get-orig-definition to get the original definition and then use help-function-arglist as usual to get the argument list, for example (help-function-arglist (ad-get-orig-definition 'ido-...
  • 7,328
6 votes
Accepted

Sort of autoreply for specific messages in minibuffer

The tags-revert-without-query variable is the generic mechanism for skipping this prompt. Check whether setting that directly circumvents the problem. If so, you could let-bind it in a wrapper around ...
  • 44.3k
6 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)
  • 21.3k
6 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.
  • 13.8k
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 ...
  • 24.5k
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))) <...
  • 25.5k
5 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 ...
  • 3,416
4 votes
Accepted

How can I get VolatileHighlights to work with evil-yank, evil-delete, etc?

Read the source code of volatile-highlights and you'll find you don't need advice at all, supporting any command is built into volatile-highlights. All you need is two lines of code: (vhl/define-...
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 ...
  • 25.5k
4 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 ...
  • 71k
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")) (...
  • 9,002
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-...

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