How to quickly navigate/jump between functions on emacs?

8

2

How to quickly navigate/jump between functions on emacs? I'm looking for a way to jump quickly to functions on emacs. I'm using the emacs search to do it,but it's too slow and fail. For example,I need to make sure to type a string that will not match to function prototype or function call. I need to include the type of function and begging of parameters type to it.. and depending to coding syntax style used in the program,it's hard/inviable to do. What exactly I'm looking for: something that I type just the function name and jump to begging of it. My current language is C on linux. But if there's such feature to another programming languages and platforms,please show me too. Will be very appreciated.

Please: don't suggest "use an IDE" I'm fine with emacs.

Jack

Posted 2013-05-30T16:09:53.307

Reputation: 963

Answers

11

M-x imenu lets you jump to functions in the same file. I have bound it to Super-i for easy access.

legoscia

Posted 2013-05-30T16:09:53.307

Reputation: 2 197

Hmm. Seems imenu only searches the current open buffers. Any info on configuring it to search the whole project? – cevaris – 2015-03-28T16:33:24.620

I'll advice to combine imenu with ido-goto-symbol or the helm mode. This way you have just to type few letters from the name of the function to jump to it in the current buffer. – rkachach – 2015-12-08T14:56:01.717

3

I have been using cscope integrated into Emacs, and it works really well for searching for functions, variables etc and jumping around between them.

Edit: in .emacs (or .xemacs/init.el) I have:

(require 'xcscope)
(setq cscope-do-not-update-database t)

Then I just manually run cscope on the source files as needed (for the Linux kernel, make cscope, which also works on many other large projects).

Mats Petersson

Posted 2013-05-30T16:09:53.307

Reputation:

Could you post your cscope config? I'm still trying to figure out how to configure it for efficient and streamlined workflow... – elemakil – 2013-05-30T19:18:58.727

Yes, edit with config posted... – None – 2013-05-30T19:26:40.010

Wait... that's all? Now I feel kinda stupid... :-) – elemakil – 2013-05-30T20:11:57.783

3

As already suggested by others I also use Imenu exclusively to navigate in the buffer. I find it very useful to do that by having ido interface for Imenu. here is my config for ido-imenu. (this is slightly modified version from the function you find it emacswiki page)

(defun ido-imenu ()
  "Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
  (interactive)
  (imenu--make-index-alist)
  (let ((name-and-pos '())
        (symbol-names '()))
    (flet ((addsymbols
            (symbol-list)
            (when (listp symbol-list)
              (dolist (symbol symbol-list)
                (let ((name nil) (position nil))
                  (cond
                   ((and (listp symbol) (imenu--subalist-p symbol))
                    (addsymbols symbol))

                   ((listp symbol)
                    (setq name (car symbol))
                    (setq position (cdr symbol)))

                   ((stringp symbol)
                    (setq name symbol)
                    (setq position
                          (get-text-property 1 'org-imenu-marker symbol))))

                  (unless (or (null position) (null name))
                    (add-to-list 'symbol-names name)
                    (add-to-list 'name-and-pos (cons name position))))))))
      (addsymbols imenu--index-alist))
    ;; If there are matching symbols at point, put them at the beginning
    ;; of `symbol-names'.
    (let ((symbol-at-point (thing-at-point 'symbol)))
      (when symbol-at-point
        (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
               (matching-symbols
                (delq nil (mapcar
                           (lambda (symbol)
                             (if (string-match regexp symbol) symbol))
                           symbol-names))))
          (when matching-symbols
            (sort matching-symbols (lambda (a b) (> (length a) (length b))))
            (mapc
             (lambda (symbol)
               (setq symbol-names (cons symbol (delete symbol symbol-names))))
             matching-symbols)))))
    (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
           (position (cdr (assoc selected-symbol name-and-pos))))
      (push-mark)
      (if (overlayp position)
          (goto-char (overlay-start position))
        (goto-char position)))))

(global-set-key (kbd "C-x C-i") 'ido-imenu)

And I can use C-xC-i in many number of languages modes that support imenu

kindahero

Posted 2013-05-30T16:09:53.307

Reputation: 1 088

This is a very good solution. Another alternative is to enable the helm mode this way you get the same functionality by default without the need of extra lisp code. – rkachach – 2015-12-08T14:57:42.107

1This is a cool solution! – None – 2013-05-31T02:08:23.877