Skip to content

Instantly share code, notes, and snippets.

@magnars
Created April 11, 2012 17:05
Show Gist options
  • Save magnars/2360578 to your computer and use it in GitHub Desktop.
Save magnars/2360578 to your computer and use it in GitHub Desktop.
ido-imenu as used in Emacs Rocks #10
(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))))
(goto-char position))))
@developernotes
Copy link

I updated this to allow for you to conditionally pass in a symbol for it to navigate to, specifically the symbol at point. Think of it like a go to definition within the file. It uses thingatpt. It looks like this:

(require 'thingatpt)
(require 'imenu)

(defun mine-goto-symbol-at-point ()
  "Will navigate to the symbol at the current point of the cursor"
  (interactive)
  (ido-goto-symbol (thing-at-point 'symbol)))

(defun ido-goto-symbol (&optional a-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
            (if (null a-symbol)
                (ido-completing-read "Symbol? " symbol-names)
              a-symbol))
           (position (cdr (assoc selected-symbol name-and-pos))))
      (cond
       ((overlayp position)
        (goto-char (overlay-start position)))
       (t
        (goto-char position))))))

;; optionally bind these to key chords
(global-set-key "\C-cs"     'ido-goto-symbol)
(global-set-key "\C-cp"     'mine-goto-symbol-at-point)

@terranpro
Copy link

I had problems with jumping to symbols with the original post (wrong type argument: integer-or-marker-p).

Found an alternative, http://emacswiki.org/emacs/idomenu.el - which works well enough for me.

@Wilfred
Copy link

Wilfred commented May 20, 2013

Yep, I'm getting the same issue as terranpro. Traceback:

Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
  goto-char(nil)
  (cond ((overlayp position) (goto-char (overlay-start position))) (t (goto-char position)))
  (let* ((selected-symbol (if (null a-symbol) (ido-completing-read "Symbol? " symbol-names) a-symbol)) (position (cdr (assoc selected-symbol name-and-pos)))) (cond ((overlayp position) (goto-char (overlay-start position))) (t (goto-char position))))
  (let ((name-and-pos (quote nil)) (symbol-names (quote nil))) (let* ((vnew (function (lambda (symbol-list) (progn (if ... ...))))) (old (if (fboundp (quote addsymbols)) (symbol-function (quote addsymbols)) (quote cl--unbound)))) (unwind-protect (progn (if (eq vnew (quote cl--unbound)) (fmakunbound (quote addsymbols)) (fset (quote addsymbols) vnew)) (addsymbols imenu--index-alist)) (if (eq old (quote cl--unbound)) (fmakunbound (quote addsymbols)) (fset (quote addsymbols) old)))) (let* ((selected-symbol (if (null a-symbol) (ido-completing-read "Symbol? " symbol-names) a-symbol)) (position (cdr (assoc selected-symbol name-and-pos)))) (cond ((overlayp position) (goto-char (overlay-start position))) (t (goto-char position)))))
  ido-goto-symbol(#("Article" 0 7 (fontified t face (highlight-symbol-face))))
  mine-goto-symbol-at-point()
  call-interactively(mine-goto-symbol-at-point record nil)
  command-execute(mine-goto-symbol-at-point record)
  smex-read-and-run(("toggle-debug-on-error" "mine-goto-symbol-at-point" "flycheck-mode" "eval-buffer" "set-variable" "emacs-lisp-mode" "push-button" "vc-annotate" "tags-generate-for-this-repo" "rename-file-and-buffer" "flymake-mode" "whitespace-mode" "copy-for-code-snippet" "smerge-mode" "run-python" "query-replace-regexp" "list-packages" "occur" "git-flow-release-start" "start-scratch-file" "indent-region" "toggle-truncate-lines" "virtualenv-workon" "git-flow-release-push" "yas-reload-all" "which-function-mode" "apropos" "virtualenv-search" "python-insert-super-function" "re-builder" "viper-mode" "paredit-mode" "make-directory" "eshell" "smerge-next" "run-haskell" "query-replace-repeat" "package-list-packages" "next-error" "python-mode" "pylookup-lookup" "fundamental-mode" "package-install" "highlight-symbol-mode" "calc" "js2-mode" "jedi-mode" "electric-pair-mode" "jedi:goto-definition" "html-mode" ...))
  smex()
  call-interactively(smex nil nil)

This occurs when I jump to symbols that have been imported rather than defined in the current file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment