Using Emacs builtin functions to jump source code function definitions of variables.
Emacs has several ways to navigate through method and function definitions for each language without installing any additional package.
In Emacs a function, a method or a class, is known as an identifier, i.e.: a name of a syntactical subunit of the program.
Emacs provides a unified interface to quickly find where each identifier was defined and referenced or to rename identifiers across the entire project, called xref.
Each programming major mode will process its own rules and make xref able to:
- what files to search for identifiers
- how to find references to identifiers
- how to complete on identifiers
A tipical workflow for a backend to implement xref is:
- using an external program to extract references to identifiers by scanning source code files, and
- putting them into a database
We can use programs like
etags to regularly scan the code
for tagging identifier definitions and extract references into tags
Find function definitions and usage
To find the definition of a specific identifier (i.e.: function, method, class, etc) we can use M-. which is bound to xref-find-definitions and it will look for the identifier at point.
To get back where you previously were, use M-, which invokes xref-pop-marker-stack and pops back to where M-. was last invoked.
The above behaviour is possible due to the Marker stack which pushes each time with xref-find-definitions and pops from the stack with xref-pop-marker-stack.
Find all class or functions
To look for a regex pattern we use C-M-. which invokes xref-find-apropos.
This is useful also to look for all the classes defined in the project, or functions, for example in Python: C-M-. class would retrieve all the classes known by the backend.
- Xref in Emacs manual https://www.gnu.org/software/emacs/manual/html_node/emacs/Xref.html#Xref
- Tags tables at https://www.gnu.org/software/emacs/manual/html_node/emacs/Identifier-Search.html#Identifier-Search
- Tags tables https://www.gnu.org/software/emacs/manual/html_node/emacs/List-Identifiers.html#List-Identifiers
- Using Emacs As A Python Ide with basic Emacs featuresJuly 10, 2017
- Jump To Function Definitions In Emacs
- Writing Alternating Between English And Spanish In EmacsJune 17, 2017
- Writing In Emacs Checking Spelling Style And GrammarJune 10, 2017
- Adding Custom Snippets To YasnippetNovember 1, 2016