xref-find-definitions
is normally the function you'd use, and by default, it is bound to M-.. xref
package provides generic means of locating and navigating source files. But how it handles this depends on the backend associated with the major mode of the buffer you are editing. Emacs Lisp mode does provide a backend: elisp--xref-backend
function. But I'm not sure when exactly was it added. I think it wasn't there, at least not by default in Emacs 24 and older.
If you don't have Emacs Lisp backend for xref
, then you can use Emacs' interactive help to locate the functions' source: move the point to the function's name (otherwise you will be able to answer the prompt with the name of the function you want to locate the source of), and press C-h f. This will display a help buffer with the function's description. You can then move the point to the file name; pressing RET on the file name will bring up the buffer with the contents of the file and move the point to the function's definition.
Elisp functions can be defined in several ways, sometimes source location may not be available, or it will require extra work. One such example is a function defined in C sources. As point out by bertfred, in order to locate a function in C source code, Emacs needs to know where it is located, i.e. the variable find-function-C-source-directory
must be set to the location of the Emacs source. If you don't have the C source, you can download it from https://github.com/emacs-mirror/emacs or the official GNU site: http://ftpmirror.gnu.org/emacs/
M-.
? This should work in recent Emacs out of the box. If this doesn't, then you couldM-h f
and then from the help buffer pressRET
on the file name which defined the function.buffer-substring
is a c function and you have to specify the source directory with(setq find-function-C-source-directory "/path/to/c-source")