Is there a feature in Emacs for function call hierarchy - emacs

I am maintaining someone else's code. The code is written in C using GCC 4.4.3 on Linux platform. However, the code jumps around a lot and its difficult to find out where all the functions are called from.
In Visual Studio, there is a feature called 'Call Hierarchy' which will display where functions are called from and called to. Does Emacs (23.1.1) have any such feature?

The classic Emacs way to jump to calls is to use TAGS file and use the M-. command. I recommend using Exuberant C Tags with the following command in the root directory of your project :
ctags -e --c-kinds=+pxd -R .
Then using visit-tags-table you can open the TAGS file. With M-. you can jump to each definition or call to your keyword. Use C-u M-. to jump to another occurrence. Use C-x z z z... to repeat the search.
If you have many projects, you can create a TAGS file for each one of them and then call visit-tags-file to add the TAGS file to your list of TAGS files to search from.
Another classic way (the un*x way), is to use the command M-x find-grep to search for occurrences of your keyword.

http://cedet.sourceforge.net/symref.shtml

Related

Emacs command to Find and Open File similar to Eclipse

I've recently switched from using Eclipse to emacs. I'm trying to find a way to emulate eclipse's Ctrl-Shft-r functionality which lets you type in a file name and it begins showing all files in the current workspace that begin with the string you are typing.
C-x C-f seems to handle just tab-completion in the current directory, whereas Eclipse's functionality looked through all sub-directories to find matching files.
I'm looking for something (maybe there's a plugin that does this) that allows you to type the name of folder to look in, and then a partial file and returns back the results in a buffer. Possibly that uses auto-complete to list off matching files with their full paths.
First of all, steer clear of vanilla find-file function (that's the interactive function that is run when you hit C-x C-f). It is very limited, it forces you to hit TAB all the time, and the first thing most people do when switching to emacs is replace find-file with something more powefull.
There're a number of alternatives. ido-mode is one, helm is another. The former is light-weight, fast and comes built-in with emacs. The latter is immensely powerful and strives to be fast, too.
Second of all, there're two ways a recursive file search can usually be done:
directory search - that's when you just search a directory, no surprises here;
project search - that's when you setup a project your're working on, thus making emacs aware of which files are of interest to you right now.
For directory search, ido-find-file and helm-find-file are both viable options. Ido does its search automatically when you pause typing; helm uses (C-u) M-g s to activate grep. See this SO question for more info.
For project search, you need a library to manage your projects. Projectile is great for that. Set it up and use C-c p f or C-c p F to list files in current or all of your projects, respectively. Oh, and projectile uses ido by default, but there is helm support, too.
You're looking for projectile which indexes your project's files. I used it for a while but have recently switched to using helm-recentf
(global-set-key "\C-x\ \C-r" 'helm-recentf)
I have recent files set to a large number. Pretty much anything I've ever opened is a few keystrokes away. This even doubles up as a handy way to switch buffers.
(require 'recentf)
(setq recentf-auto-cleanup 'never)
(recentf-mode 1)
(setq recentf-max-saved-items 200)

Emacs filesets: how to run other (elisp, not shell) commands?

There are 5 Elisp commands that can be run on an Emacs fileset, plus the ability to run any shell command. What about all the other Emacs commands? Just to give one example, it would be nice to be able to run M-x occur on a fileset.
I know its possible to mark several files in dired and then run any Emacs command on them (is that true, or am I confused with shell commands?), but it would be very convenient to define a fileset once and then be able to use it like one single file for all kinds of text editing.
Thanks for any advice
The commands that can operate on file sets are specified in the global custom variable "filesets-commands". You can add your own commands to that list. The default value for this variable is:
("Isearch" multi-isearch-files
(filesets-cmd-isearch-getargs))
("Isearch (regexp)" multi-isearch-files-regexp
(filesets-cmd-isearch-getargs))
("Query Replace" perform-replace
(filesets-cmd-query-replace-getargs))
("Query Replace (regexp)" perform-replace
(filesets-cmd-query-replace-regexp-getargs))
("Grep <<selection>>" "grep"
("-n " filesets-get-quoted-selection " " "<<file-name>>"))
("Run Shell Command" filesets-cmd-shell-command
(filesets-cmd-shell-command-getargs)))
The values consist of an association list of names, functions, and an argument list (or a function that returns one) to be run on a filesets' files. So, if you wanted to add a command that does an "occur" command on the file set, you could use the "Isearch" entry as an example to create your own new entry (that you would add to the "filesets-commands" global variable) that would look something like:
("Occur (regexp)" multi-occur-files-regexp
(filesets-cmd-occur-getargs))
You would need to write the "multi-occur-files-regexp" and "filesets-cmd-occur-getargs" functions (you could use the existing "multi-isearch-files-regexp" and "filesets-cmd-isearch-getargs" functions as a basis since they would be similar). The same would apply for any additional Emacs command that you wanted to add to work on file sets.
Dired has several operations on filesets. An example is dired-do-search (bound to A), where you can navigate through search results on several files with M-, just like with tags-search. Similarly, you can query-replace in tagged files (with Q).
The recent posts on irreal.org describe some nice dired features.
With Icicles you can use filesets for anything you might want to do with a set of files and directories. And you can create a fileset from any set of file and directory names in buffer Completions during completion. And you can use substring and regexp matching during completion to get such a set of file names in Completions.
These links might help:
http://www.emacswiki.org/emacs/Icicles_-_Persistent_Completions#Filesets
http://www.emacswiki.org/emacs/Icicles_-_Dired_Enhancements#OpenDiredOnSavedFiles
http://www.emacswiki.org/emacs/Icicles_-_Dired_Enhancements#MarkedFilesAsProject
http://www.emacswiki.org/emacs/Icicles_-_Customization_and_General_Tips#icicle-filesets-as-saved-completion-sets-flag
Dired+ has command diredp-fileset, which opens Dired on an Emacs fileset. You are prompted for the fileset to use.

Is there any ECB (or other)-enhanced find file mojo?

Can I use ECB (or some other project aware thing, like eproject, ibuffer, etc.) to enhance emacs's find-file goodness?
Imagine this scenario: I have several projects/directories specified in my ecb-source-path, let's call them Project1 and Project2, and they look like so:
Project1
foo.c
bar.c
Project2
foo.c
baz.c
I'd like to have find-file (or another command) work to find and jump to the files there without having to navigate to the ecb directories buffer, or have the buffers open already.
I guess my ideal usage scenario would be like so:
I'd like to C-x C-f baz (or another special command) to open Project2/baz.c. Going even further, I'd love if I could use something like ido's flex matching to get p2foo open Project2/foo.c.
¿C'est posible?
If in your example you are in project1 foo.c, and you want to get to project1 bar.c, I think most of the project tools like ede, eproject, gnu global, idutils integration etc have "jump to file" type commands. I, of course, recommend the CEDET/EDE and ECB combination. The keybinding is C-c . f which is ede-find-file.
Setting up CEDET and EDE is pretty easy, but EDE knows only a small number of different project types, and you may need to configure it for your projects by hand which is less easy.
If you want to be in project1/foo.c and jump to project2/baz.c, you just need to use locate:
M-x locate RET baz.c RET
Now select from the list.
You could use the filecache package to index the directories you want, then use ido to choose a filename from the cache. Here is one example; there are other implementations you can Google for.
Take a look at find-file-in-project. It seems to me you're looking for something similar.
I've written a package find-file-in-tags which enables you to open files in your TAGS file without specifying the path to the file. If there are multiple files that match what you've entered, you're prompted to refine your choice (or open all).
Additionally, if you have multiple work areas for the same project, find-file-in-tags uses a single TAGS file for all those work areas but opens up the file in the same work area as the buffer in which you are invoking find-file-in-tags.

In Emacs, how can I jump between functions in the current file?

I'd like to quickly move point to a function in my Emacs buffer. I'd like to run some function and get a prompt asking me for the function name, with completion provided for every function defined in the current buffer.
I generally use etags to navigate around, but sometimes I'm looking for a framework method that's been overridden in several files. In these cases, I can find the file I need but then I'd like to quickly jump to the function there. There is a similar feature in TextMate where you can select a definition from a list in the bottom right of the editor.
Just to jump around functions in the current file? Use imenu. It's the simplest and lightest of all the alternatives listed so far and might be enough for what you want. It's also built into Emacs and has minimum setup hassle. It features graphical and textual interfaces. Anything extra and you'll be better off using one of the other excellent suggestions made here.
speedbar comes standard, and gives you a collapsible menu for each file in the current directory, by default middle clicking on an entry for a function definition jumps to that def. With emacs23 this was changed to the more normal leftclick.
You can use etags-select to select from multiple matching tags. But the answer to what you asked is imenu.
Icicles is probably closer to what you are looking for:
http://www.emacswiki.org/emacs/Icicles_-_Tags_Enhancements
It's an enhancement to etags and includes (among other things) the file name with the tag so you can tell if it's the one you are looking for.
try CEDET. It is a bit difficult to set up the first, but here is an excellent tutorial: by Alex ott
And when he gets installed, you can use semantic-complete-jump. pressed tab couple times, and it is also brings up symbol definitions.
If M-. brings up the wrong method, you can type C-u M-. to find the next one with the same name.
global gtags is very good
To navigate within the current file or a set of files that you select, you do not need a TAGS file. You can use Imenu. But it is better to use Icicles imenu commands.
Why? Because they let you use completion. Substring, regexp, prefix, or fuzzy completion. Combine simple patterns to match, or subtract them.
Command icicle-imenu is bound in Icicle mode to C-c =. Butyou can also look up just a command or just a non-command function (non-interactive), using command icicle-imenu-command or icicle-imenu-non-interactive-function.
These commands are multi-commands, meaning that they are actually browsers: you can trip among function definitions using keys C-RET or C-mouse-2 (direct jumps) and C-down (cycle). Hit RET or click mouse-2 to settle down at a final destination.
I use C-M-a and C-M-e to jump between the beginning and end of functions.
Otherwise, open up Speedbar and click the + icon next to a file name to view a list of functions contained in the file. Then click on the function names to jump to them directly.

Emacs command for searching in files

I want to search in all files from the current folder for macro CODE_INIT_PARAMETERS.
I can do Alt + X occur, Return CODE_INIT_PARAMETERS Return, but this shows only entries from open buffers.
Is there a way to search all files from current folder, from Emacs, without switching to M-x shell and then grep? I want to avoid grep, because for some commands (M-x occur) Emacs do jumps to offending code, and I want that too.
You can try M-x rgrep.
It will ask for:
the directory where you want to search recursively
a file pattern for the files you want to include in the search
the pattern you want to search
As an extra, it will exclude source control private directories from your search (like CVS, .svn or .git).
Emacs provides a built-in command:
M-x grep RET CODE_INIT_PARAMETERS *.c
(and 'grep-find to search sub directories)
Though I prefer the interface provided by an external package igrep (which provides the commands igrep and igrep-find).
If you open a folder in dired, and mark all of the files (with 'm') you can run 'dired-do-search ('A' in my bindings). This will search all marked files. To get to the next one, run tags-loop-continue (M-,)
I have set up several ELisp functions to mark various subsets of the files (.h files, .cpp files, etc.) and to create a recursive dired to search a whole tree...
This is an improvement on Trey Jackson's suggestion.
M-x grep
You will see the grep command, e.g. grep -nH -e
Add R to the first set of flags (for recursive), and put your search term after -e
grep -nHR -e CODE_INIT_PARAMETERS
Hit RET. The results will be understandable by Emacs -- you will be able to click or otherwise navigate to them, like M-x occur. You may need to put the search directory at the end of the command:
grep -nHR -e CODE_INIT_PARAMETERS /path/to/root/of/search
M-x find-grep-dired also works similarly as rgrep
In cases where
you may be searching repeatedly; and
etags will work
you might consider using etags and invoking either find-tag (bound to M-. by default) or tags-search (no default binding but can be continued with M-,).
There is as well ack-grep mode for Emacs which uses the ack-grep tool which is specifically designed for ''grepping'' programming languages and IMHO looks nicer than the output of M-x grep.
But as mentioned earlier etags should be the proper way!