Skip to main content

Questions tagged [advice]

is for the Elisp advice feature that changes an existing definition of a function in a way that lets you undo the change easily. This tag applies to both old and new advice features. The old advice feature uses `defadvice`. The new advice feature uses functions and macros such as `advice-add` and `add-function`.

Filter by
Sorted by
Tagged with
55 votes
4 answers
4k views

Practical benefits of new advice system in Emacs 24.4

Emacs 24.4 comes with a new advice system. The NEWS file says: ** New package nadvice.el offers lighter-weight advice facilities. It is layered as: *** add-function/remove-function, which ...
itsjeyd's user avatar
  • 14.8k
31 votes
1 answer
4k views

How do I use nadvice?

My config is full of advice, and I keep hearing about the new shiny minimalist nadvice.el package. I've searched the manuals, and I've read the source, but I'll openly admit: I still have no idea ...
PythonNut's user avatar
  • 10.4k
20 votes
3 answers
9k views

before-save-hook for cc-mode

I want to customize cc-mode to execute a function before saving. One solution would be to modify cc-mode keymap to bind C-x C-s to a function which performs the work I need and then saves it. My ...
Pradhan's user avatar
  • 2,370
14 votes
1 answer
977 views

Emacs let-bound advice

I'd like to temporarily override a function in a piece of code. Take, for example, the following: (defun nadvice/load-quiet (args) (cl-destructuring-bind (file &optional noerror ...
PythonNut's user avatar
  • 10.4k
12 votes
3 answers
2k views

How to write a transparent "pass-through" function wrapper?

What I mean by a "transparent 'pass-through' function wrapper" is a function, let's call it wrapper, that returns the result from passing all its argument to some other function, let's call it wrappee....
kjo's user avatar
  • 3,267
12 votes
3 answers
1k views

How to manipulate argument list in nadvice.el?

Following on from an answer to another question about the new advice system: In old-style advice.el, it was possible to manipulate individual members of an advised function's argument list, without ...
Aaron Miller's user avatar
11 votes
3 answers
971 views

How can I remove an unnamed advice?

I was having fun adding some advice to a function: (advice-add 'executable-find :around (lambda (f &rest args) (apply g args))) ;;; ^ Ouch, a typo. ...
Daniel Jour's user avatar
11 votes
5 answers
2k views

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

When I browse Emacs help for functions via C-h f, I often want to peek into the Elisp/C implementation. I want to enter view-mode automatically when I access source code this way to avoid unnecessary ...
rationalrevolt's user avatar
11 votes
1 answer
2k views

Unadvise a function (remove all advice from it)

I cannot find a predefined command in nadvice to easily unadvise a function in Emacs 24.5.1. Something like: (defun advice-unadvice (sym) "Remove all advices from symbol SYM." (interactive "...
Tobias's user avatar
  • 33.3k
10 votes
1 answer
3k views

Override a function locally, but allow calls to the original function

The advice feature allows modifying the behavior of a function globally. An advice definition can make calls to the original function. (defadvice foo (around foo-bar activate compile) "Always set ...
Gilles 'SO- stop being evil''s user avatar
9 votes
2 answers
2k views

List advice for functions

I had a look over the weekend, and I cannot see an easy way to check whether a function is advised. I must be missing something. HELP! :)
Realraptor's user avatar
  • 1,293
8 votes
2 answers
2k views

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

When I type C-h m for describe-mode the frame splits into two windows with the current buffer on top and the *Help* buffer below. But the focus stays in the current buffer, so I usually have to type C-...
Håkon Hægland's user avatar
7 votes
3 answers
2k views

How to bypass (or disable) a command's "advice"?

In my .emacs I have an expression of the form (defadvice find-file ...) ...which modifies the behavior of the standard find-file command. There are times, however, when I want to bypass this ...
kjo's user avatar
  • 3,267
6 votes
2 answers
225 views

Call function without calling advice defined on it

I sometimes want to bypass the advice I have defined on some function. Something like: (call-without-advice #'fn ...)
HappyFace's user avatar
  • 830
6 votes
3 answers
544 views

One-time advice

I've been trying to a macro to put one-time advice on a function. What I mean by one-time advice is advice which removes itself after it is called – so that when you add the advice and run the ...
digitalis_'s user avatar
6 votes
3 answers
1k views

Call a function before a specific major mode starts

I have a setup function that I want to run before I use pdb/gud to debug a Python script. I know about gud-mode-hook which runs after the debugger starts (too late) and change-major-mode-hook which, ...
elethan's user avatar
  • 4,845
6 votes
2 answers
3k views

define-advice vs advice-add vs other (?)

Can someone explain and show the right/preferred way of advising functions in Emacs these days. Official documentation says to use advice-add, but I've seen people using define-advice and I checked, ...
iLemming's user avatar
  • 1,273
6 votes
1 answer
2k views

Is there a "switch to buffer" hook?

I'm running emacs 26.3. I want to run a hook every time I switch to a buffer. However, I have not been able to find any kind of "buffer switch hook". If I want to always run a function when ...
HippoMan's user avatar
  • 616
6 votes
1 answer
825 views

How to automatically kill a shell buffer when the shell process exits

In shell-mode when you exit the shell process (e.g. by typing exit) the buffer still hangs around (with the message "Process shell<1> finished"). How can I automatically kill the shell buffer when ...
halloleo's user avatar
  • 1,345
5 votes
2 answers
949 views

Getting electric-pair mode to behave more nicely around existing strings

Electric-pair-mode always tries to insert a closing delimiter after the opening one if nothing is selected. However, if you're placing delimiters around an existing something that you don't want to ...
bright-star's user avatar
5 votes
1 answer
174 views

How to get the arguments of an advised function?

After my initial config, ido-read-internal becomes advised by flx: (ido-mode) (require 'flx-ido) (setq ido-enable-flex-matching t) So now I get this: (help-function-arglist 'ido-read-internal t) ;; ...
abo-abo's user avatar
  • 14.2k
5 votes
1 answer
138 views

How to suppress `Wrote /home/user/.emacs.d/recentf` message in minibuffer

In the minibuffer I keep seeing: Wrote /home/user/.emacs.d/recentf Would it be possible to suppress this message? (Only for file name recentf file) my minimal.el: (setq recentf-max-saved-items 50) (...
alper's user avatar
  • 1,410
5 votes
1 answer
371 views

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

With the guide on extending Emacs, I discovered the volatile highlights package. But it only works on Emacs operations like kill and yank. I would like to use it with Evil operations like evil-yank ...
ReneFroger's user avatar
  • 3,890
5 votes
2 answers
281 views

Shallowly override functions within a target function

I'm exploring options for monkey-patching and I'm wondering if the following is within reason. I would like to override a particular function within some target function so that calls within the ...
ebpa's user avatar
  • 7,519
4 votes
3 answers
2k views

Trace calls to a function with the parameters

I need to track the calls to org-tags-view to discover the params passed to it. I tried an advice: (defun his-tracing-function (orig-fun &rest args) (message "org-tags-view called with args %S" ...
squiter's user avatar
  • 229
4 votes
2 answers
185 views

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

Suppose a third-party plugin contains the following function: (defun example-function () (interactive) ;; ... do many things ... (goto-char (point-max)) ;; ... do many things ... (goto-char (...
Flux's user avatar
  • 613
4 votes
1 answer
105 views

Adding a function call after a builtin runs

I always want to run reposition-window after c-beginning-of-defun. I've tried this: (advice-add 'c-beginning-of-defun :after #'reposition-window) it results in this at execution: Variable binding ...
Gauthier's user avatar
  • 509
4 votes
1 answer
931 views

buffer-local advice

I am trying to create a buffer local advice to run some code when a minor mode is disabled. I looked through the documentation for add-function and saw this If PLACE is a symbol, its `default-value' ...
Prgrm.celeritas's user avatar
3 votes
3 answers
214 views

Sort of autoreply for specific messages in minibuffer

Every time when I call M-x projectile-regenate-tags I get bothered with the message Tags file a:/foobar/TAGS has changed, read new contents? (y or n) which is quite cumbersome. Especially when I ...
ReneFroger's user avatar
  • 3,890
3 votes
1 answer
589 views

Creating a local advice for a function

I want to slightly modify the behaviour of a function, old-function whose source I cannot modify. I can do that simply by (defun my-new-function (old-fn a b c) (if (evaluate-my-condition) (...
Tohiko's user avatar
  • 1,649
3 votes
2 answers
194 views

in org-mode, how to move point to next checkbox when it's marked completed?

I work with org-mode checkboxes, marking them as completed as I go. * TODO clean kitchen - [X] sink - [ ] stove - [ ] floors I want org to move point to the next checkbox when when it gets marked - ...
incandescentman's user avatar
3 votes
1 answer
101 views

Why advice won't work if the function is called from another compiled function?

Let's consider a minimal example, I take org-narrow-to-subtree as a compiled function, under the hood it calls narrow-to-region, I add simple advice to it: (defun bark (START END) (message "Bark!&...
Daniel Vartanov's user avatar
3 votes
1 answer
300 views

How to add a locally override the message function?

I would like to add a suffix to a message reported by a function called in some source I don't maintain. Is there a way to locally override the message function so I can manipulate it before it's ...
ideasman42's user avatar
  • 8,856
3 votes
1 answer
198 views

How does one "extend" an existing function?

What I want to do: have the function evil-delete do one more task after its usual code. (I'm presuming that evil-delete is the underlying workhorse that is called by evil-delete-line and about five ...
not-just-yeti's user avatar
3 votes
1 answer
411 views

Rewriting `defadvice' as `advice-add' makes advice ineffective

I am trying to make undo-tree auto compress the history save file. The document suggests adding the following advice (defadvice undo-tree-make-history-save-file-name (after undo-tree activate) (...
nalzok's user avatar
  • 675
3 votes
1 answer
591 views

Monkey patching a function

Some context: I'm doing some linux kernel development on a Debian VM so naturally I'm using tramp. I want to use semantic-symref, but the trouble is that: Semantic will eventually call cedet-gnu-...
c-o-d's user avatar
  • 930
3 votes
1 answer
411 views

Defining advice for cider-interactive-eval for evil compatibility

Cider offers very useful debugger for clojure code. Once you enter the debugger, you can step through it with keys like (n)ext, (o)ut, etc., like so. Using evil, the problem is that if I enter this ...
sooheon's user avatar
  • 481
3 votes
0 answers
71 views

How to advise a CL method?

I am familiar with setting advice on a function: (advice-add 'some-tag :before 'function-being-advised (lambda (arg) (message "In advice with arg: %s" arg))) How do you advise a ...
Troy Daniels's user avatar
3 votes
1 answer
100 views

Should the sharp quote should be used for the first argument of advice-add?

... when a symbol referencing a function is actually used? Documentation states: Like ‘add-function’ but for the function named SYMBOL. Contrary to ‘add-function’, this will properly handle the ...
cYrus's user avatar
  • 222
3 votes
0 answers
435 views

How to set the colors for pdf-tools annotations individually?

I would like to be able to set the annotation colors to be different (e.g. strike-out "black" while keeping highlight "yellow"). I asked about this as an issue at the github page for pdftools and was ...
brittAnderson's user avatar
3 votes
0 answers
933 views

mu4e without mu server process

Occasionally I get error from mu4e error in process sentinel: Database is locked by another process. To solve this issue I usually end up to kill the process in list-process or sometimes pkill emacs ...
Jānis Erdmanis's user avatar
3 votes
0 answers
59 views

Passing advice to mode line

I'd like to advise format-mode-line to collapse whitespace across the whole string. The following attempt does not work for me. (advice-add 'format-mode-line :filter-return 'collapse-whitespace) ...
Matthew Piziak's user avatar
2 votes
2 answers
183 views

On monkey-patching a third-party function

The following snippet, featuring the "fantasy function" clone-function, illustrates what I'd like to do: (defvar last-enabled-foo nil) ;; hold on to a "clone"/copy of third-party function enable-foo ...
kjo's user avatar
  • 3,267
2 votes
1 answer
352 views

Can you intercept a function and return your own value using advice?

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 ...
JCC's user avatar
  • 1,029
2 votes
1 answer
236 views

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

I used (advice-add 'yas-new-snippet :override 'my/yas-new-snippet ) but I was wondering if there was something like this which would allow me to override a function in a function only in that function:...
ritchie's user avatar
  • 169
2 votes
2 answers
212 views

advising two `synonyms.el` functions using `defadvice`

What's the best way to simplify this? (defadvice synonyms (after synonyms-jump-to-window) (switch-to-buffer-other-window "*Synonyms*")) (defadvice synonyms-no-read (after synonyms-jump-to-...
iLemming's user avatar
  • 1,273
2 votes
1 answer
195 views

"void-variable" error when adding advice to `kill-ring-save`

I am trying to add an advice to kill-ring-save and kill-region, so that when I call them with no active regions, instead of throwing an error, the line the point is currently on would be marked ...
nalzok's user avatar
  • 675
2 votes
2 answers
146 views

Monkey patch and restore builtin set-window-dedicated-p during advice

I would like to make set-window-dedicated-p a NOP when called from xref--show-pos-in-buf. Below is an attempt to do so using advice. It doesn't work, I think because fset is not really copying the ...
sandinmyjoints's user avatar
2 votes
1 answer
462 views

Can "advice-add ... :filter-args" detect if the advised function was called interactively?

Using advice-add from nadvice.el, I am trying to add a :filter-args advice to an interactive function. Is it somehow possible for it to detect, whether the advised function has been called ...
kdb's user avatar
  • 1,571
2 votes
1 answer
172 views

Using advice to run function before tag symbol lookup

I use xref-find-definition to look up the definition of a symbol name. When I call xref-find-definition, I want to Emacs to automatically find and load an appropriately named etags file. For this ...
std_answ's user avatar
  • 191