Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
For js2-mode, to go to function definition and back. I sorely missed the M-. and M-, from SLIME.
;;; Find JS function definition in your current buffer (and go back).
(defvar *quasi-js-current-pos* nil)
(defun quasi-js-function-search ()
"Search for JS function definations. In a rather dumb way, but works, albeit only for current buffer.
Works recurcively too :)"
(let ((text (thing-at-point 'word)))
(push (point) *quasi-js-current-pos*)
(goto-char (point-min))
(if (search-forward (concat "function " text) nil t)
(goto-char (pop *quasi-js-current-pos*))
(message "Could not find definition for %s" text)))))
(defun quasi-js-function-go-back ()
"Go back to where you initiated search from"
(if *quasi-js-current-pos*
(goto-char (pop *quasi-js-current-pos*))
(message "Nowhere to jump!")))
;; Add hooks to js2-mode. It will cobbler the default tag-search bindings. Beware.
(add-hook 'js2-mode-hook
(lambda ()
(local-set-key (kbd "M-.") #'quasi-js-function-search)
(local-set-key (kbd "M-,") #'quasi-js-function-go-back)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment