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

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.

Related

clojuredocs integration with emacs

My Current Workflow
I need to use 'doseq'
I don't remember it's args.
I go to clojuredocs , lookup 'doseq', and look at the examples.
My Ideal Setup
Inside emacs, I type some magic key combo, I type in doseq,
and it EXTRACTS EXAMPLES FROM A LOCAL MIRROR OF CLOJUREDOCS
and presents it inside my emacs browser.
What I found via Google:
https://www.reddit.com/r/emacs/comments/450x84/a_clojuredocsorg_for_emacs/
Basically other people also want this.
My Question:
Is there anyway to do this in emacs?
Thanks!
I use cider-grimoire. In Spacemacs, I just type the leader key, followed by hg. Behind the scene, cider will perform a search on conj.io and return the result in another Emacs buffer.
Late to the party, but here it is:
https://github.com/mbuczko/helm-clojuredocs
It is helm-based solution which redirects to the clojuredocs.org displaying documentation of given search phrase (if found). Most useful when used with helm-clojuredocs-at-point function which simply searches for phrase under the cursor.
If you use cider, C-c C-d C-d on a var will show you the docstring for that var. Not exactly what you're describing but for many use-cases the docstring is enough to figure out the arguments to a function.
cider will also show the arguments to the current function (under cursor) in the echo area.

Slime/Emacs : Keyboard shortcut for Go to function ( Not M-.)

How do I directly navigate to a function definition in a given file in Slime/Emacs using keyboard shortcuts? I know about M-. but that is not I want. I am already in the file and know the function name. Search by text won't directly take me to the function definition as it will take me to call sites of that function as well.
For those of you who also know Eclipse, I am looking for the equivalent of using Ctrl-O to open the outline view and then as I type the method name, it will filter(elide) to the function I want, I then just press enter and it takes me there.
If there is an alternative that you use, I would be happy to try that too.
It sounds like you're looking for M-x imenu. It doesn't have a keyboard shortcut by default; I like to bind it to s-i:
(global-set-key [(super ?i)] 'imenu)
As #legoscia said, Imenu is the answer. As additional info I'll mention how Icicles can enhance your use of Imenu.
The obvious enhancement is better completion (substring, regexp,...), including narrowing choices with multiple patterns.
Unobvious is the Icicles multi-commands that are specialized for Imenu navigation, giving you, in effect, an Imenu browser. This is described here.
There are different commands to navigate to/among different kinds of Emacs Lisp definitions: commands, non-command functions, faces, keymaps of different kinds, user options, and other variables.
While navigating, you can sort the candidates that match your input, and cycle among any subset of them in the sort order.
There are "full" versions of the commands, which provide as candidates not just what matches the Imenu regexps (e.g. (defun foobar () and your current input, but the complete definitions (e.g., full function definition).
These navigation commands are also for searching. In particular, the "full" versions provide the full definitions that match your current minibuffer input as candidates. As you change your input incrementally, the full definitions are searched, narrowing the choices. You can then navigate among any of those.
You can also do it with lispy.
It's a mixture of Paredit, vi and IDE features for Elisp, Clojure,
Common Lisp and Scheme.
The feature that you want is provided by lispy-goto, bound to
g. It uses CEDET to parse the whole source directory,
allowing you to jump to a tag in all files in current directory.
There's also lispy-goto-local bound to G, that
looks for tags in just the current file.
helm completion is used for both commands so it's really fast.
Have a look at
Navigating a directory of Common Lisp code with lispy.el
for a screencast.
You can see that it's much more advanced than imenu:
it recognizes tag types such as in-package, defparameter,
defconstant, defclass etc. This can also be extended to arbitrary
tags, such as SLIME's define-pattern-substitution.
Also, lispy uses SLIME to provide inline arguments (alternative to eldoc) and
eval bindings.

How can I make emacs' speedbar auto-expand file/class info based on the cursor?

I've just found speedbar and it's a wonderfully useful tool. I really like how it can drill into (python) files to show me a list of classes, methods and functions.
Is there any way I can get speedbar to auto-expand the tree of class/function references for the file in the currently active buffer?
It would be a bonus too if I could just expand to where the current cursor is; for instance, if the cursor was located in the foo function of the bar class, speedbar would expand to myfile.py > class bar > foo.
There is no canned solution for what you are asking about. Speedbar does, of course, know how to do it, but you will need to write a new command to do it.
I'll guess you might be interested in ECB, which can serve a similar function. It breaks the side window into multiple parts, and one of the windows does follow the cursor around in the list of tags. I'll guess this is what you want.

Emacs how to auto-complete words of include files on C?

How can I make Emacs to complete words that are in C include files ?
#include <stdio.h>
int main(){
print//<-- this is where I want it to complete printf
What's the simplest way? (something simpler than Cedet)
First generate tags for the source and include files you'd like to be able to autocomplete for. See my blogpost for tips on using tags if you didn't use tag tables before.
Now if you have a TAGS table that includes the stdio.h, then you can autocomplete 'printf' using the command `complete-tag'.
Perhaps bind `complete-tag' to a key:
(global-set-key [f3] 'complete-tag)
Unlike complete-tag, dabbrev-expand, or hippie-expand (which does dabbrev-expand like things), the CEDET suite does exactly what the question describes. When asked to perform a completion, it looks and sees that you have included stdio.h, and then looks there for possible completions.
CEDET does a lot of other things related to completion as well which will provide very focused and correct suggestions, not just vaguely similar suggestions. A side affect is that CEDET takes more effort to setup. You need to teach it where you include files are, for example, and sometimes how to deal with macros, and what the project you are working on is like.
There is more detail on this here:
link text
You might want to try out M-/ (dabbrev-expand). This command attempts to complete the identifier immediately preceding the point (ie, where your cursor is) using the contents of the current buffer and then the contents of other buffers of the same mode. If the first completion offered isn't the one you want, just keep typing M-/. If you have the habit of keeping a single emacs session open continuously (which, if you don't have, you should really acquire), and have a handful of files from the current project open, you're quite likely to be able to find an the expansion you want for any particular prefix.
So, to answer you're original question, M-/ will find the printf completion you're looking for if (a) you've used printf anywhere else in the buffer you're editing, or (b) it appears in any other .c or .h file you have open in emacs.
You might also try hippie-expand, which has additional options regarding where it looks for completion information. I bind M-/ to hippie-expand, and then modified the order of the elements in hippie-expand-try-functions-list as follows:
(global-set-key (kbd "M-/") 'hippie-expand)
(setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol))
This makes hippie-expand act like the normal M-/ at first, but repeated presses will yield more possible expansions.

Is there any way to enable code completion for Perl in vim?

Surprisingly as you get good at vim, you can code even faster than standard IDEs such as Eclipse. But one thing I really miss is code completion, especially for long variable names and functions.
Is there any way to enable code completion for Perl in vim?
Ctrl-P (Get Previous Match) and Ctrl-N (Get Next Match) are kind of pseudo code completion. They basically search the file (Backwards for Ctrl-P, Forwards for Ctrl-N) you are editing (and any open buffers, and if you are using TAGS anything in your TAG file) for words that start with what you are typing and add a drop down list. It works surprisingly well for variables and function names, even if it isn't intellisense. Generally I use Ctrl-P as the variable or function I am looking for is usually behind in the code. Also if you keep the same copy of Vim open, it will search the files you have previously opened.
Vim 7 supports omni completion.
For example, I have this in my vimrc
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
and then, when I press Ctrl-X Ctrl-O in Insert mode, I get a dropdown list of autocomplete possibilities.
Here's an omnicfunc for perl. No idea how well it works though.
Well, Vim's generic completion mechanism is surprisingly good, just using Ctrl-N in insert mode. Also, line completion is very handy, using C-x C-l.
Also check out this vim script for perl.
The standard Ctrl+N and Ctrl+P works even better if you add the following to your ~/.vim/ftplugin/perl.vim file:
set iskeyword+=:
Then it will autocomplete module names, etc.
The .vimrc clip in one of the other answers is slightly wrong. To turn your tab key into an auto-complete key, use this code:
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
You can find this, and tons of other vim tricks in this thread at Perlmonks--which links to even more threads with lots more customizations.
You should look at the SuperTab plugin:
http://www.vim.org/scripts/script.php?script_id=1643
It let's you do completion (either the OmniCompletion or the regular completion) using tab and shift-tab instead of ^N and ^P.
https://github.com/c9s/perlomni.vim
Ctrl+N
This is explained in the Perl Hacks book, along with how to do Package completion. Highly recommended.