Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred method of overriding an emacs lisp function?

Tags:

I have rewritten and stand-alone tested the behaviour of an inner function invoked by one of the Emacs functions bundled with Emacs 24. What is the preferred way of incorporating - e.g. in my init.el - my function's behaviour overriding the bundled function?

I have followed various threads of advice vs fset etc. and am confused.

like image 710
iainH Avatar asked Mar 30 '13 09:03

iainH


People also ask

Is Emacs functional Lisp?

Emacs Lisp supports multiple programming styles or paradigms, including functional and object-oriented. Emacs Lisp is not a purely functional programming language since side effects are common. Instead, Emacs Lisp is considered an early functional flavored language.

What is Emacs Lisp used for?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

What is hook in Emacs?

Hooks are an important mechanism for customizing Emacs. A hook is a Lisp variable which holds a list of functions, to be called on some well-defined occasion. (This is called running the hook.) The individual functions in the list are called the hook functions of the hook.

Is Emacs Lisp the same as Lisp?

By default, Common Lisp is lexically scoped, that is, every variable is lexically scoped except for special variables. By default, Emacs Lisp files are dynamically scoped, that is, every variable is dynamically scoped.


2 Answers

@iainH You tend to get to a useful answer faster by describing what goal you're trying to accomplish, then what you have so far. I asked for code to try to help you do what you want to do without having to overwrite anything.

I still don't understand why you don't just use defun though? I suspect what may happen is you use defun in your init file, but the original function isn't loaded yet (see autoload). Some time later, you do something to cause the file with the original definition to be loaded and your custom function is overwritten by the original.

If this is the problem, you have three options (let's say you want to overwrite telnet-initial-filter from "telnet.el"):

  1. Load the file yourself before you define your own version.

    (require 'telnet) (defun telnet-initial-filter (proc string)   ...) 
  2. Direct Emacs to load your function only after the file loads with the eval-after-load mechanism.

    (eval-after-load "telnet"   '(defun telnet-initial-filter (proc string)      ...)) 
  3. Use advice.

Of these, 2 is best. 1 is also okay, but you have to load a file you may never use in a session. 3 is the worst. Anything involving defadvice should be left as a last resort option.

like image 143
event_jr Avatar answered Sep 25 '22 07:09

event_jr


Just to keep this question self-contained, here is how to do it with advice.

(defadvice telnet-initial-filter (around my-telnet-initial-filter-stuff act)   "Things to do when running `telnet-initial-filter'."   (message "Before")   ad-do-it   (message "After") ) 

This is obviously useful primarily if you want to wrap, not replace, an existing function (just redefine it in that case).

For much more on using advice, see the manual.

like image 26
tripleee Avatar answered Sep 24 '22 07:09

tripleee