Skip to content

Instantly share code, notes, and snippets.

@sabof
Last active December 13, 2015 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabof/4941970 to your computer and use it in GitHub Desktop.
Save sabof/4941970 to your computer and use it in GitHub Desktop.
javascript function lookup
(defun es-js-imenu-create-index ()
"Return an imenu index for the current buffer."
(save-excursion
(save-restriction
(let (unique-names result name pos)
(widen)
(goto-char (point-min))
(while (re-search-forward
(concat "\\(\\_<.+?\\_>\\) = function"
"\\|"
"\\(\\_<.+?\\_>\\): function"
"\\|"
"function \\(\\_<.+?\\_>\\)")
nil t)
(setq name (or (match-string-no-properties 1)
(match-string-no-properties 2)
(match-string-no-properties 3))
pos (or (match-beginning 1)
(match-beginning 2)
(match-beginning 3)))
(when (member name unique-names)
(let ((counter 2)
new-name)
(while (progn (setq new-name (concat name "[" (int-to-string counter) "]"))
(member new-name unique-names))
(incf counter))
(setq name new-name)))
(push name unique-names)
(push (cons name (js--maybe-make-marker pos)) result))
(nreverse result)))))
(defun* es-js-imenu-goto-function-at-point ()
(interactive)
(let* (( word (or (thing-at-point 'symbol)
(return-from es-js-imenu-goto-function-at-point)))
( index (es-js-imenu-create-index))
( pair (or (find-if (lambda (cons)
(and (string-prefix-p word (car cons))
(not (= (point) (marker-position (cdr cons))))))
index)
(return-from es-js-imenu-goto-function-at-point))))
(ring-insert find-tag-marker-ring (point-marker))
(goto-char (marker-position (cdr pair))))
)
(add-hook
'js-mode-hook
(lambda ()
(define-key 'js-mode-map (kbd "M-.") 'es-js-imenu-goto-function-at-point)
(setq-local imenu-create-index-function 'es-js-imenu-create-index)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment