In Emacs Lisp, how to find where the symbol is defined - emacs

When I do C-h f or C-h v, Help tells me in which file the symbol is defined or where it will be autoloaded from. How can I find the same information programmatically?

Some digging reveals that
(find-lisp-object-file-name object type)
Should do the trick. As an example:
(find-lisp-object-file-name 'goto-line 'function)
;; => "/usr/local/Cellar/emacs/24.3/share/emacs/24.3/lisp/simple.el"
EDIT: How I discovered this information:
First I did C-h k C-h f to figure out what C-h f is bound to. The result is describe-function, so let's do C-h f describe-function to see the source for that. I noticed that it was essentially an interactive wrapper around describe-function-1, so I jumped to the source for that. There's a lot of stuff in there, but the pertinent line is:
(file-name (find-lisp-object-file-name function def))
Revealing that find-lisp-object-file-name is the function used to do this work internally.

To add to James Porter's answer
;;; run from: emacs -q
(require 'cl) ; for incf
(print (list
;; goto-line is a function defined in simple.el
(find-lisp-object-file-name 'goto-line (symbol-function 'goto-line))
;; print is a function defined in C
(find-lisp-object-file-name 'print (symbol-function 'print))
;; rx is an autoload from rx.el
(find-lisp-object-file-name 'rx (symbol-function 'rx))
;; incf is an alias for cl-incf defined in cl.el
(find-lisp-object-file-name 'incf (symbol-function 'incf))
;; cl-incf is defined in cl-lib.el
(find-lisp-object-file-name 'cl-incf (symbol-function 'cl-incf))))
;; => ("c:/run/Emacs/lisp/simple.el" C-source
;; "c:/run/Emacs/lisp/emacs-lisp/rx.el" "c:/run/Emacs/lisp/emacs-lisp/cl.el"
;; "c:/run/Emacs/lisp/emacs-lisp/cl-lib.el")
(print (list
;; print-circle is a variable defined in C
(find-lisp-object-file-name 'print-circle 'defvar)
;; indent-line-function is a variable defined in indent.el
(find-lisp-object-file-name 'indent-line-function 'defvar)))
;; => (C-source "c:/run/Emacs/lisp/indent.el")

Related

How is the defun macro implemented in lisp?

I'd like to learn more about lisp macros and I want to create a simple implementation of the defun macro.
I'm also interested in lisp's source code in all the implementations.
This is a tricky question, because of bootstrapping: defun does a lot of things (iow, calls a lot of functions), but to define those functions one needs a working defun. Thus there are three(3!) definitions of defun in clisp/src/init.lisp: at lines
228
1789
1946
The very basic definition of defun could be this:
(defmacro defun (fname lambda-list &rest body)
`(setf (fdefinition ',fname)
(lambda ,lambda-list
(block ,fname ,#body))))
In fact, this is the first definition of defun in CLISP (line 228), except that there is no defmacro and no backquote at that moment yet, so the actual code looks a lot uglier.
See also Is defun or setf preferred for creating function definitions in common lisp and why? where I discuss macroexpansions of defuns.
You can easily check how your particular CL implementation, implemented defun by running
(macroexpand '(defun add2 (x) (+ x 2)))
On SBCL it expands to:
(PROGN
(EVAL-WHEN (:COMPILE-TOPLEVEL) (SB-C:%COMPILER-DEFUN 'ADD2 NIL T))
(SB-IMPL::%DEFUN 'ADD2
(SB-INT:NAMED-LAMBDA ADD2
(X)
(BLOCK ADD2 (+ X 2)))
(SB-C:SOURCE-LOCATION)))
T
To see the particular source code that implemented the I would use (on Emacs) the M-. key binding and then I will write defun and hit enter. Then Emacs will get to the source code:
(sb!xc:defmacro defun (&environment env name lambda-list &body body)
#!+sb-doc
"Define a function at top level."
[...]
I am not going to paste the whole macro as it is rather long. If you are not on Emacs, you can try searching in the repos as most implementations are open source.
BTW defun is not so special. You can implement much of it with setf-inf a symbol-function to a lambda. E.g.:
(setf (symbol-function 'ADD3) #'(lambda (x) (+ x 3)))
; => #<FUNCTION (LAMBDA (X)) {1006E94EBB}>
(add3 4)
; => 7

emacs macro doesn't define command

I have written a macro that makes an alias and generates global keybinding based on the name.
I expect that if I add to my emacs config the following code for example:
(defkey-alias cool-function make-directory)
I will have command my-cool-function which creates a directory and a keybinding C-c c f to it. But after evaluation I have keybindings but have no command my-cool-function.
And if I do C-h k C-c c f I see:
C-c c f runs the command my-cool-function, which is an alias for `make-directory'.
I can not evaluate (my-cool-function) in scratch either.
But if I try to (macroexpand '(defkey-alias cool-function make-directory)) and then evaluate expanded s-expr it works.
What is the difference between calling macro and calling macroexpanding and then evaluation? And why alias is not callable?
Thank you.
Emacs version is GNU Emacs 24.2.1, Windows 7
The code:
;;; defining keys
(defun name-to-key(funname)
" works like:
'my-cool-function -> \"\C-cmcl\" "
(apply 'concat
"\C-c"
(mapcar (lambda(str)(substring str 0 1))
(split-string (symbol-name funname)
"-"))))
(defmacro defkey-alias(alias function)
"works like defalias but you should not quote symbols and sets global key mapping
Usage: (defkey-alias mkdir make-directory)"
(let ((myalias (make-symbol (concat "my-" (symbol-name alias)))))
`(progn
(defalias ',myalias ',function)
(global-set-key ,(name-to-key alias) ',myalias))))
UPDATED: Using (defun ...(interactive)(call-interactively 'function)) also does not work
The reason is simple: (make-symbol (concat "my-" (symbol-name alias))) returns a non-interned symbol. I.e. it returns a symbol whose name is my-cool-function and yet it is a different symbol from the one you get when you write (my-cool-function). So instead of make-symbol you want to use intern.

Emacs: the code in the body of a defun or defmacro cannot refer to surrounding lexical variables?

Update 2013 May: As of GNU Emacs 24.3.1, (let .. (defun..)) bytecompiles just fine without warning and the bytecompiled code works the same as not-compiled code. Just don't forget to add the file variable lexical-binding: t to the file to be bytecompiled. Workarounds at the end of this question is now not necessary.
Lexical Binding - Emacs Lisp Manual has this paragraph:
Note that functions like symbol-value, boundp, and set only retrieve or modify a variable's dynamic binding (i.e. the contents of its symbol's value cell). Also, the code in the body of a defun or defmacro cannot refer to surrounding lexical variables.
I am not sure if I am getting the meaning of the second sentence right. In the following code which should be run in lexical binding mode, the code in the body of a defun is successfully referring to the lexical binding value of the name n.
(let ((n 0))
(defun my-counter ()
(incf n)))
(my-counter) ;; 1
(my-counter) ;; 2
Is the sentence simply saying that (let .. (defun ..)) is a bad practice?
Workarounds:
;; -*- lexical-binding: t -*-
;; a way to define the counter function without byte-compile error or warning
(defvar my--counter-func
(let ((n 0))
(lambda ()
(setq n (1+ n)))))
(defun my-counter ()
(funcall my--counter-func))
;; another way to define the counter function, again without byte-compile error or warning
(fset 'my-another-counter
(let ((n 0))
(lambda ()
(setq n (1+ n)))))
And here's the code for testing the above code:
;; run:
;; emacs -q --load path-to-the-el-file-of-this-code.el
(load "path-to-file-defining-my-counter.elc") ;; loading the ELC file to test if byte-compiled code runs as expected.
(print (my-counter)) ;; 1
(print (my-counter)) ;; 2
(print (my-another-counter)) ;; 1
(print (my-another-counter)) ;; 2
The code does not byte-compile well at least in Emacs 24.1.1. I saved the following code in the foo.el file, which uses setq in place of incf in order to avoid any possible effects by the cl library:
;; -*- lexical-binding: t -*-
(let ((n 0))
(defun my-counter ()
(setq n (1+ n))))
When I tried to byte-compile it (M-x byte-compile-filefoo.el), I got the following warning messages:
foo.el:3:1:Warning: Function my-counter will ignore its context (n)
foo.el:3:1:Warning: Unused lexical variable `n'
foo.el:5:11:Warning: reference to free variable `n'
foo.el:5:17:Warning: assignment to free variable `n'
All of the messages are indicating that the code in the body of the defun construct cannot refer to the surrounding lexical variable n as the manual claims.
Actually, when I loaded the byte-compiled code (M-x load-filefoo.elc) and evaluted the (my-counter) form, I got the following erorr:
Debugger entered--Lisp error: (void-variable n)
...
Unfortunately, I'm not sure why the code appears to work when evaluated in the form of source code.
As I replied on gnu.emacs.help, you can use (defalias 'foo (lambda ...)) to work around that limitation.
And that limitation is lifted in Emacs's development code.
It's perfectly fine to refer to variables in lexical scope(*) from within a defun, just as you are doing above, and just as the "my-ticker" example on that manual page does as well.
Either I am missing something the line in the manual that says:
the code in the body of a defun or defmacro cannot refer to
surrounding lexical variables.
should say something more like:
code in the body of a defun can only access lexical variables if they are defined within the same lexical scope.
NOTE: There are comments in the other answers about problems byte-compiling this kind of code. Those should be fixed in the latest emacs. I've verified in v24.2.50.1 that this byte compiles and loads correctly.

Lexical eval in emacs24

Can anybody explain me how eval works with emacs24? From eval description:
eval is a built-in function in `C source code'.
(eval FORM &optional LEXICAL)
Evaluate FORM and return its value.
If LEXICAL is t, evaluate using lexical scoping.
Does that mean, that something like this should work?
(setq lexical-binding t)
(let ((myvarr 42)) (eval 'myvarr t)) ; (void-variable myvarr)
Update:
(setq lexical-binding nil)
;; => nil
(let ((myvarr 42)) (eval 'myvarr))
;; => 42 (#o52, #x2a, ?*)
(setq lexical-binding t)
;; => t
(let ((myvarr 42)) (eval 'myvarr))
;; Debugger entered--Lisp error: (void-variable myvarr)
;; eval(myvarr)
;; (let ((myvarr 42)) (eval (quote myvarr)))
;; (progn (let ((myvarr 42)) (eval (quote myvarr))))
;; eval((progn (let ((myvarr 42)) (eval (quote myvarr)))) t)
;; eval-last-sexp-1((4))
;; eval-last-sexp((4))
;; call-interactively(eval-last-sexp nil nil)
;; call-last-kbd-macro(nil kmacro-loop-setup-function)
;; kmacro-call-macro(nil nil)
;; kmacro-end-or-call-macro(nil)
;; call-interactively(kmacro-end-or-call-macro nil nil)
(ignore-errors (let ((myvarr 42)) (eval 'myvarr)))
;; => nil
(setq lexical-binding nil)
;; => nil
(eval (let ((myvarr 42)) (eval 'myvarr)) t)
;; => 42
(eval '(let ((myvarr 42)) (eval 'myvarr)) t)
;; Debugger entered--Lisp error: (void-variable myvarr)
;; eval(myvarr)
;; (let ((myvarr 42)) (eval (quote myvarr)))
;; eval((let ((myvarr 42)) (eval (quote myvarr))) t)
;; eval((eval (quote (let ((myvarr 42)) (eval (quote myvarr)))) t) nil)
;; eval-last-sexp-1((4))
;; eval-last-sexp((4))
;; call-interactively(eval-last-sexp nil nil)
Emacs version: GNU Emacs 24.1.1 (i386-mingw-nt6.1.7600) of 2012-06-10 on MARVIN
Since lexical binding breaks much existing elisp code, it is an opt-in feature.
lexical scoping can best be understood with a simple example:
(defun some-func (callback)
(let ((a 5))
(funcall callback)))
(let ((a 3))
(some-func (lambda () a)))
Under most languages, this would return 3 since the a in some-func doesn't seem visible from the bottom form. However, in emacs before 24 or without lexical scope this program returns 5.
This has led to many unexpected surprises and subtle and often hidden bugs between interacting functions; to fix this emacs 24 introduced lexical scoping, but as mentioned previously, is backwards compatible.
The mechanism to opt-in to dynamic scoping is either the file variable lexical-binding (which turns it on per source file) or as the option you see to eval
So if we rewrite the example to use eval:
(eval '(let ((a 3))
(some-func (lambda () a))) nil) ; => 5
(eval '(let ((a 3))
(some-func (lambda () a))) t) ; => 3
In your example, what is making the difference is not whether the code inside eval is dynamically scoped, but whether the code surrounding it is. Variable binding is what is affected by lexical scoping, not variable lookup. I'm not completely certain of eval's semantics here, but what seems to be happening (and what makes the most sense) is eval evaluates the expression in an entirely new lexical context. So the outer lexical scope is hidden from the inside of eval, but the dynamic scope is still visible (so the lookup succeeds when the file is dynamically scoped, but not otherwise).
I could not find the formal semantics of the eval function in Emacs 24, but all of your examples make sense when assuming that it works in the same way as that in Common Lisp (as indicated by cobbal). The Common Lisp HyperSpec says:
Syntax:
eval form
Description:
Evaluates form in the current dynamic environment and the null lexical environment.
Let's look at your examples one by one with the description in mind.
(setq lexical-binding t)
(let ((myvarr 42)) (eval 'myvarr t)) ; Lisp error: (void-variable myvarr)
Lexical binding is enabled by setting t to lexical-binding, so myvarr turns to be a lexically bound variable, which is not available inside the eval function as stated above. The eval function's option, t, is irrelevant here.
(setq lexical-binding nil)
(let ((myvarr 42)) (eval 'myvarr)) ; 42
Lexical binding is disabled by setting nil to lexical-binding, so myvarr turns to be a dynamically bound variable, which is available inside the eval function. The eval function's option, implicilty nil, is irrelevant here.
(setq lexical-binding t)
(let ((myvarr 42)) (eval 'myvarr)) ; Lisp error: (void-variable myvarr)
Lexical binding is enabled by setting t to lexical-binding, so myvarr turns to be a lexically bound variable, which is not available inside the eval function. The eval function's option, implicilty nil, is irrelevant here.
(ignore-errors (let ((myvarr 42)) (eval 'myvarr))) ; nil
Ditto.
(setq lexical-binding nil)
(eval (let ((myvarr 42)) (eval 'myvarr)) t) ; 42
Lexical-binding is disabled by setting nil to lexical-binding, so myvar turns to be a dynamically bound variable, which is available inside the inner eval function. Note that the let form, including the inner eval function, is evaluated as argument preparation before the outer eval is called. Neither the outer nor inner eval function's option is relevenat here.
(eval '(let ((myvarr 42)) (eval 'myvarr)) t) ; Lisp error: (void-variable myvarr)
myvarr turns to be a lexically bound variable, which is not available inside the inner eval. Note that, because of ', the let form is evaluated by the outer eval function with lexical binding enabled. The outer eval function's option is relevant here while the inner eval function's is not.
Why the null lexical environment?
That is, I think, because that alpha-equivalence would not hold anymore if the current lexical environment were used.
Alpha-equivalence is a formal way to say that the names of function parameters are not important. For example, (lambda (x) x) and (lambda (y) y) are alpha-equivalent, and we have regarded them as the same. Alpha-equivalence allows us to change a function parameter's name as we wish at any time. We take it for granted, and we will be very surprised if it does not hold. See Lambda calculus and Alpha-equivalence for more formal explanation.
But alpha-equivalence turns out to have some problem when a code value involving a free variable (called open code) can be passed around as in Lisp. Let's see the following example:
;;; -*- lexical-binding: t -*-
(lambda (x) (lambda (y) (eval x))) '(1+ y)
If eval evaluated under the current lexical environment, the form would be equivalent to (lambda (y) (1+ y)). Now see the following program:
(lambda (x) (lambda (z) (eval x))) '(1+ y)
This program is different from the previous one only in its parameter's name, z in place of y, so we naturally expect them to behave in the same way. But the latter program evaluates to (lambda (z) (1+ y)), which is definitely different from (lambda (y) (1+ y). Think about what will happen when the resulting function values are applied to the same argument. The point is that the name of a function parameter DOES matter when a code value containing a free variable is allowed.
Here we have two choices in order to preserve alpha-equivalence: we cannot give up alpha-equivalence because it feels so natural and we have been so much accustomed to it. The first option is to have eval evaluated under the null lexical environment as (Common) Lisp does. With this option, the free variable y in (1+ y) does not bind to the formal parameter y in (lambda (y) ...). So those two program behave consistently and alpha-equivalence is preserved well. The other choice is to preclude the problem by not allowing open code value from the beginning. I have heard that it is a method chosen by MetaOCaml
Someone may ask "if so, why the current dynamic environment?". For dynamically bound (a.k.a. special) variables, we already acknowledge well that their names are so important and that, if you change them carelessly, your program will be broken.
The odd behavior of your inner eval forms seems similar to that of symbol-value or add-to-list under lexical binding.
emacs lexical scoping and quoted variables
Under lexical binding, a symbol's value cell only holds global values, (which can be different from the variables' lexical binding value).
Use of quoted variables (such as in (eval 'var), (eval '(print var)), (add-to-list 'var 1)) only get or set on the symbol's value cell.
Practices to avoid encountering this kind of gotcha are:
Try to define and use macros rather than eval whenever possible
If you want to create/declare a global variable, refrain from using setq to create it, use defvar, defcustom or defconst to create it as a special variable instead. Special variables are always dynamically scoped, even in lexical binding mode.

Define an emacs command that calls another emacs command (preserving interactive stuff)

How can I define an emacs command X that does something and then calls another emacs command Y and also copying the interactive interface of the command Y too?
I want to define an altenative version of query-replace with temporarilly toggled value of case-fold-search:
(defun alt-query-replace (a b c d e)
(interactive)
(let ((case-fold-search (not case-fold-search))
(query-replace a b c d e)))
This doesn't work. When I call alt-query-replace, it says "wrong number of arguments". I want the interactive interface of alt-query-replace to be the same as query-replace. Do I need to inspect the source code of query-replace or is there a general approach?
You may advise the original function, if you want to modify its behavior instead of calling a separate function.
From chapter 17.3 Around-Advice of the GNU Emacs Lisp Reference Manual:
Around-advice lets you “wrap” a Lisp
expression “around” the original
function definition.
(defadvice foo (around foo-around)
"Ignore case in `foo'."
(let ((case-fold-search t))
ad-do-it))
In your case, you can write:
(defadvice query-replace (around alt-query-replace (from-string to-string &optional delimited start end))
(let ((case-fold-search (not case-fold-search)))
ad-do-it))
(ad-activate 'query-replace)
Use call-interactively:
(defun alt-query-replace ()
(interactive)
(let ((case-fold-search (not case-fold-search)))
(call-interactively 'query-replace)))