Jump to function definition in Emacs by mouse-click

1.9k views Asked by At

Using Emacs + Common Lisp (SBCL) + Slime, is there a quick way to jump to the definition of a function (defun ...) without typing (find-function) the name of the function? I am looking for an similar solution as often provided in Eclipse: Strg + (left-mouse-click on function call).

3

There are 3 answers

0
Rainer Joswig On

You can use the keyboard. Usually m-. or esc-. in Emacs. The cursor must be on the symbol.

5
Drew On

In Emacs:

With point on the function whose definition you want to visit, hit C-x 5 F (find-function-other-frame).

You can also bind find-function, find-function-other-window, or find-function-other-frame to a mouse button action.

You mention Strg + (left-mouse-click on function call), for instance. Dunno what Strg is here, but you can easily bind [mouse-1] or [down-mouse-1] to a command that checks the symbol at the click position, and if it is a defined function calls find-function for it, and if it is not has the usual [mouse-1] or [down-mouse-1] behavior.

Personally, I wouldn't want such behavior, but it is easy to achieve in Emacs. For example:

(defun my-find-func-mouse (event)
  (interactive "e")
  (let ((fn  (save-excursion
               (goto-char (posn-point (event-end event)))
               (function-called-at-point))))
    (unless (fboundp fn) (error "No function here"))
    (find-function-do-it fn nil 'switch-to-buffer-other-window)))

(global-set-key [C-down-mouse-1] nil)
(global-set-key [C-mouse-1] #'my-find-func-mouse)
1
Prasoon Shukla On

The usual way is to create a TAGS file using etags:

$ find . -type f -iname "*.lisp*" | etags -

Then, do M-x visit-tags-table and navigate to the TAGS file. After the tags are loaded, you can simply do M-. with point on function and it will open in the current buffer.

Also, you can use M-* to go back to where you were before.

If you're using projectile, you can simply keep your TAGS file in the project root and you won't need to run M-x visit-tags-table. Projectile will load the tags table automatically when you first press M-.