Skip to content

Instantly share code, notes, and snippets.

@quasi
Last active January 2, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quasi/8280332 to your computer and use it in GitHub Desktop.
Save quasi/8280332 to your computer and use it in GitHub Desktop.
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 :)"
(interactive)
(let ((text (thing-at-point 'word)))
(push (point) *quasi-js-current-pos*)
(goto-char (point-min))
(if (search-forward (concat "function " text) nil t)
(recenter)
(progn
(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"
(interactive)
(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