Emacs: displaying list of subroutines/functions/classes in a source file
Solution 1
Try M-x occur RET exp RET
- this lists in a new buffer all the occurrences of the exp
in the current buffer.
Solution 2
Most people of course will tell you to just use a modern IDE but they don't understand the power of emacs
. Personally, I use ECB which makes my emacs
look like this:
The middle window on the left hand side is the list of functions of the current source file. Middle clicking the name moves the cursor to that function's definition.
To install, follow the instructions on the ECB website to install ECB and then add this line to your ~/.emacs
(changing the location accordingly):
(add-to-list 'load-path
"~/.emacs-lisp/ecb/")
Another option is CEDET but I have not used it.
Solution 3
Use Imenu:
M-X imenu
-
tab
,All.Methods
-
tab
to see all methods
Solution 4
You said "display the list of the subroutines/functions/classes (or any such thing--depending on what regexp you define in its configuration file) in a source text file". Ignoring the part in parens for the moment, to which others have responded, it's not clear what you mean by a function etc. being "in" a source file.
If you are asking about seeing a list of the functions etc. that are defined in a given source file, and then navigating among their definitions, then look at Imenu
in Emacs. See the Emacs manual, node Imenu
. See also this EmacsWiki page about Imenu:
Solution 5
Emacs has a couple of ways to obtain this functionality.
The lighter-weight method is by means of tags, which with suitable customization can serve most purposes. Once you've built a tags file (which is done in a shell, via e.g. cd /path/to/project; ctags -e -R .
) and selected it via M-x visit-tags-table RET /path/to/TAGS RET
, you can, for example, use M-.
on a function call to jump to the function definition, M-x tags-search
to grep your way through the files described in the tags table, &c. See the linked page for a fairly thorough description of the tools available.
The heavier-weight method is by means of Semantic, a lexer/parser combination implemented entirely in Emacs Lisp. While I'm not as familiar with Semantic as with tags, the impression I get is that Semantic incurs a heavier initial setup burden, in exchange for a more generally useful result. In addition to the linked manual page, (the Emacs Wiki article)[http://www.emacswiki.org/emacs/SemanticBovinator] may be of additional help here.
Related videos on Youtube
Wirawan Purwanto
Updated on September 18, 2022Comments
-
Wirawan Purwanto almost 2 years
I have been using a little-known editor called "FTE" (or eFTE) for years; it has a nice utility that can display the list of the subroutines/functions/classes (or any such thing--depending on what regexp you define in its configuration file) in a source text file (C, C++, Fortran, ... what have you). This function allows me to jump quickly from one point to another in a source file. Perhaps the original shortcut in FTE is
^I
(Control+I). Does emacs have a similar functionality? Emacs is much more advanced than FTE and I would be surprised if no one ever developed such a facility.Thanks, Wirawan
-
user1686 almost 11 yearsI haven't used emacs, but I think you might be looking for some sort of
ctags
integration.
-
-
Aaron Miller almost 11 yearsI've seen ECB before, but always shied away because it seems awfully heavyweight. How have you found it?
-
terdon almost 11 years@AaronMiller Honestly, I am in love with it. I finally got all my eclipse using friends to shut up. It is kinda heavyweight but I switch it on with a shortcut when necessary and that takes around a second, maybe two, no more (on my 3-year old laptop). I am very pleased with it though I seem to recall that installation was not painless. I would recommend you try it out and if its too heavy, don't use it. Mind you ecb is old (last version ~2009) so CEDET might be a better option.
-
Aaron Miller almost 11 yearsThanks! I'll try it and CEDET both (I gather 24.3 has CEDET 2 built in), not least because I find your point about Eclipse compelling. :)
-
Aaron Miller almost 11 yearsHow have I been using Emacs for well over two years and not found out about that yet? If I could upvote your answer more than once, I would.
-
ack over 9 years@AaronMiller Welcome to the secret forest hiding very useful but little-known Emacs function fairies :-) Another (related) very useful function is
re-builder
, which lets you build up regular expressions interactively, incrementally, highlighting all matches in the buffer from which it was called. Enjoy :-)