Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to previously defined function in Emacs Lisp?

Tags:

emacs

elisp

I have a function:

(defun function-name (&optional args) ... <unknown content>)

I redefine it with

(defun function-name (&optional args) ... my own content)

Can I somehow after some time remove my own version of function-name and stay with the first one?

like image 315
Sergey Avatar asked Jan 10 '23 02:01

Sergey


2 Answers

No, you cannot.

Save/Restore

You can save the definition yourself before redefining the function:

Common Lisp:

(defparameter *old-def* (fdefinition 'function-name))
(defun function-name ...)
...
(setf (fdefinition 'function-name) *old-def*)

Emacs Lisp:

(defconst *old-def* (symbol-function 'function-name))
(defun function-name ...)
...
(fset 'function-name *old-def*)

Reload

Or, if you know where the function was defined, you can reload the definition:

Common Lisp:

(load "file-name")

Emacs Lisp: same as above or M-x load-library RET.

Reeval

Or, if you know the original definition, you can reevaluate it, by pasting it at the Common Lisp prompt or by visiting the file with the definition in Emacs and evaluating the defun using C-M-x, as suggested by @Drew in a comment.

like image 191
sds Avatar answered Jan 31 '23 02:01

sds


Note that it's risky to redefine other libraries' or Emacs' own functions, since you don't know what else depends on them working exactly as expected. If at all possible, use a different name. If not, provide copious documentation warning prominently about the redefinitions. Also, did you check first whether the existing function can be tweaked to your satisfaction using predefined hooks that run before or after it or by customizing any particular user options?

like image 43
Phil Hudson Avatar answered Jan 31 '23 02:01

Phil Hudson