I want to center my screen after calling flyspell-goto-next-error. If you have a "direct" solution please let me know, but I encounter the following issue more often:

I'm looking to hook to the invocation of a function, so that I can call my own function afterwards (or before). In this case, I would hook to flyspell-goto-next-error and execute the command to center my screen. Is this possible?

One possible solution is to write my own function that first calls flyspell-goto-next-error, and then performs the screen centering. However, that would require rebinding all instances where this function is called, which is not ideal.

  • 3
    It's called advice
    – Rusi
    Feb 23, 2020 at 14:50
  • You can also create a new function named flyspell-goto-next-error and ensure that it loads subsequent to the flyspell.el library -- the newly named function will supersede the original function.
    – lawlist
    Feb 23, 2020 at 16:24
  • @Rusi Both the manual and the type of advice you linked to are obsolete; here is the modern documentation: gnu.org/software/emacs/manual/html_node/elisp/…
    – Basil
    Feb 26, 2020 at 4:02
  • Vow emacs20! Didn't notice that @basil.
    – Rusi
    Feb 26, 2020 at 4:10

1 Answer 1


Since not all emacs commands and features bring there own hook, you need to extend the given function.

One way is to use advice-add which can do all sorts of manipulation. (i.e filter function arguments, modify return values, call functions befor, after, etc). It is easy to apply and universal usable throughout emacs. For detailed information please look at the doc string of add-function

It can be used that way:

(defun my-flyspell-goto-next-error-do-stuff (&rest r)
  (do some funky stuff))

(add-function 'flyspell-goto-next-error :after #'my-flyspell-goto-next-error-do-stuff)

Another way would be to copy the original function code, manipulate it as needed and define it as the new function.

There is one problem with that approach, if the original function changes for some reason (bugfix, improved features), you keep working with the old version and nothing gives you a hint about the change. It might happen that this introduces some weird behaviour on your config. There exist packages like el-patch, which address this issue.

I thought: both methods need to be installed after the original functionality has been loaded. with-eval-after-load can help you there.
But user Phils said otherwise in its comment below (forward advice).
I did not (yet) test this. In any case: you should be on the safe side if you install the advice after loading.

  • Advice can be defined before the advised function has been defined. This is known as forward advice, and offhand I don't think you need to do anything different for it to work.
    – phils
    Feb 23, 2020 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.