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 (point-max))
;; ... do many things ...
(goto-char (point-max)))
The function uses goto-char
in various places in its body.
I want to change the behavior of the function such that every time it calls (goto-char (point-max))
, it will also call (recenter -1)
. To that end, I added this advice to redefine the calls to goto-char
:
(advice-add 'example-function
:around
(lambda (orig-fun &rest args)
(cl-labels ((goto-char (pos)
(goto-char pos)
(recenter -1)))
(apply orig-fun args))))
However, the cl-labels
appears to have no effect; goto-char
still retains its old behavior inside example-function
. What is wrong with the advice?
(Emacs version: GNU Emacs 25.2)
goto-char
just insideexample-function
using an advice. You have two options: rewriteexample-function
to callrecenter
after everygoto-char
; or add an:after
advice togoto-char
which callsrecenter
. In the latter case, the advisedgoto-char
will be called every time, not just from insideexample-function
. – NickD Jan 17 '21 at 19:27