Quickly jumping to symbols

September 3, 2008

This is brilliant.

For a while, I have been using ido to quickly jump between buffers and files by just typing any part of the file/buffer name I want. In fact, typing any sequence of letters narrows the interactive search to candidates with names that contain those letters in that order:

 fb -> Foobar, FrancisGailBrown

But for finding method definitions in a big source file, I have always just used isearch. It turns out that there is a better way.

By combining imenu with ido, I can quickly find methods as well:

All it takes is this method in my init.el:

(defun ido-goto-symbol ()
  "Will update the imenu index
   and then use ido to select a
   symbol to navigate to"
  (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))
    (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
           (position (cdr (assoc selected-symbol name-and-pos))))
      (goto-char position))))

I have bound it to C-t, because I realized that I don’t use transpose-chars (or whichever transpose method was there..I don’t use any of those, really).

I found this trick on the imenu wiki page.

About these ads

4 Responses to “Quickly jumping to symbols”


  1. This is pretty cool. I’m taking a second look at ido mode. Unfortunately when I try to use your function I get this error:

    let*: Wrong type argument: integer-or-marker-p, #
    
  2. chopmo Says:

    Chad posted the following question on my old blog:

    “You mentioned you can type fb -> foobar, how do you do that ? I can only use foob -> foobar”

    Answer:
    You can enable this by setting the variable ido-enable-flex-matching. Add this to your .emacs:

    (setq ido-enable-flex-matching t)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: