simple useful functions from Tak Ota
[elbb.git] / code / Go-to-Emacs-Lisp-Definition.el
blob59fc8a8a35b97aec9de23cc1d9938d4623a74112
1 ;; From: Helmut Eller <eller.helmut@gmail.com>
2 ;; Newsgroups: gnu.emacs.help
3 ;; Date: Tue, 22 Sep 2009 11:21:31 +0200
4 ;; Message-ID: <m2tyyvcp2c.fsf@gmail.com>
5 ;; Xref: news.stanford.edu gnu.emacs.help:173250
6 ;; To: help-gnu-emacs@gnu.org
7 ;; Subject: Re: Go to Emacs-Lisp Definition
8 ;;
9 ;; * Nordlöw [2009-09-22 11:13+0200] writes:
10 ;;
11 ;; > Has anyone implemented a go to (lookup) definition of an Emacs-Lisp
12 ;; > symbol: function, variable, face, etc kind of like find-tag but using
13 ;; > Emacs own symbol database (hash-table)?
14 ;; >
15 ;; > I know that I can achieve this in Vanilla Emacs with C-h f,v, but
16 ;; > direct jump (M-.) would be more user efficient!
17 ;;
18 ;; I have this in my .emacs. It's some random elisp hacking stuff.
19 ;; Pick what you need.
20 ;;
21 ;; Helmut
23 (defun elisp-disassemble (function)
24 (interactive (list (function-called-at-point)))
25 (disassemble function))
27 (defun elisp-pp (sexp)
28 (with-output-to-temp-buffer "*Pp Eval Output*"
29 (pp sexp)
30 (with-current-buffer standard-output
31 (emacs-lisp-mode))))
33 (defun elisp-macroexpand (form)
34 (interactive (list (form-at-point 'sexp)))
35 (elisp-pp (macroexpand form)))
37 (defun elisp-macroexpand-all (form)
38 (interactive (list (form-at-point 'sexp)))
39 (elisp-pp (cl-macroexpand-all form)))
41 (defun elisp-push-point-marker ()
42 (require 'etags)
43 (cond ((featurep 'xemacs)
44 (push-tag-mark))
45 (t (ring-insert find-tag-marker-ring (point-marker)))))
47 (defun elisp-pop-found-function ()
48 (interactive)
49 (cond ((featurep 'xemacs) (pop-tag-mark nil))
50 (t (pop-tag-mark))))
52 (defun elisp-find-definition (name)
53 "Jump to the definition of the function (or variable) at point."
54 (interactive (list (thing-at-point 'symbol)))
55 (cond (name
56 (let ((symbol (intern-soft name))
57 (search (lambda (fun sym)
58 (let* ((r (save-excursion (funcall fun sym)))
59 (buffer (car r))
60 (point (cdr r)))
61 (cond ((not point)
62 (error "Found no definition for %s in %s"
63 name buffer))
65 (switch-to-buffer buffer)
66 (goto-char point)
67 (recenter 1)))))))
68 (cond ((fboundp symbol)
69 (elisp-push-point-marker)
70 (funcall search 'find-function-noselect symbol))
71 ((boundp symbol)
72 (elisp-push-point-marker)
73 (funcall search 'find-variable-noselect symbol))
75 (message "Symbol not bound: %S" symbol)))))
76 (t (message "No symbol at point"))))
78 (defun elisp-bytecompile-and-load ()
79 (interactive)
80 (or buffer-file-name
81 (error "The buffer must be saved in a file first"))
82 (require 'bytecomp)
83 ;; Recompile if file or buffer has changed since last compilation.
84 (when (and (buffer-modified-p)
85 (y-or-n-p (format "save buffer %s first? " (buffer-name))))
86 (save-buffer))
87 (let ((filename (expand-file-name buffer-file-name)))
88 (with-temp-buffer
89 (byte-compile-file filename t))))
91 ;; (defvar elisp-extra-keys
92 ;; '(((kbd "C-c d") 'elisp-disassemble)
93 ;; ((kbd "C-c m") 'elisp-macroexpand)
94 ;; ((kbd "C-c M") 'elisp-macroexpand-all)
95 ;; ((kbd "C-c C-c") 'compile-defun)
96 ;; ((kbd "C-c C-k") 'elisp-bytecompile-and-load)
97 ;; ((kbd "C-c C-l") 'load-file)
98 ;; ((kbd "C-c p") 'pp-eval-last-sexp)
99 ;; ((kbd "M-.") 'elisp-find-definition)
100 ;; ((kbd "M-,") 'elisp-pop-found-function)
101 ;; ((kbd "C-c <") 'list-callers)))
103 ;; (dolist (binding elisp-extra-keys)
104 ;; (let ((key (eval (car binding))) (val (eval (cadr binding))))
105 ;; (define-key emacs-lisp-mode-map key val)
106 ;; (define-key lisp-interaction-mode-map key val)))