Search code examples
functionemacsadvising-functions

Override a single function in an Emacs library


I use an Emacs mode to annotate some of my files (the actual mode is not important). It's supplied as a library and comes with compiled lisp code (of course). I want to modify its behavior by overriding a single function in it. Just for my local Emacs session. For now, I just copy-paste the function from the library's source file, modify it a bit, then hit eval-last-sexp. So far, so good. However, I get inconsistent results: I'm not sure how Emacs handles functions coming from .elc files mixed with functions coming from source. Sometimes I see my own version of the function running, sometimes the original version. Very confusing (and annoying).

Any ideas how can I consistently replace a lisp function in an Emacs library without modifying the library's source files which are read-only?


Solution

  • Something like this should do the trick:

    (advice-add 'name-of-func-to-override :override
                (lambda () (message "does this instead now")))
    

    Replace name-of-func-to-override with the function name and the lambda with your version.

    I suggest looking at the add-function (and advice-add) docs as :override may not actually be what you want.