-
Notifications
You must be signed in to change notification settings - Fork 0
/
showfunc.el
79 lines (72 loc) · 3.05 KB
/
showfunc.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(defvar show-func-map nil
"Keymap for show-func temporary buffer.")
(if show-func-map ()
(setq show-func-map (make-sparse-keymap))
(suppress-keymap show-func-map t)
(define-key show-func-map "q" 'kill-func-buffer)
(set-keymap-parent show-func-map widget-keymap)
)
(defun kill-func-buffer (&rest _ignore)
"Cancel the function dialog."
(interactive)
(kill-buffer (current-buffer))
)
(defun show-func ()
"Show the cpp functions in the current buffer"
(interactive)
(setq regExD (make-hash-table :test 'equal))
(puthash "c++-mode" "^\\([\s-]\\{0,\\}[[:alpha:]][^if|^wh]\\)\\(\\)\\(.*\\)::\\(\\w+\\)(\\(.*\\))\\(.*\\)\n.*{" regExD)
(puthash "python-mode" "^\\([\s-]\\{0,\\}\\)def\\(.*\\):" regExD)
(puthash "emacs-lisp-mode" "^\\([\s-]\\{0,\\}\\)(defun\\(.*\\)(.*)" regExD)
(puthash "go-mode" "^\\([\s-]\\{0,\\}\\)func\\(.*\\)(.*).*{" regExD)
(setq currentmode major-mode)
(setq regex (gethash (format "%s" currentmode) regExD))
(unless regex (setq regex (gethash "c++-mode" regExD)))
; (message "%s %s" currentmode regex)
;; build the functions buffer
(setq buf-name "*Functions*")
(let ((current (current-buffer)) ; buffer where to search
(buffer (get-buffer-create buf-name))
(nb 0) ; count found occurences
(fct-name nil) ; name of the found function
(fct-loc nil)) ; location of the found function
(save-excursion
(set-buffer buffer)
(let ((inhibit-read-only t))
(erase-buffer))
(widget-insert "Click or type RET on a function to explore it. Click on Cancel or type \"q\" to quit.\n\n")
(set-buffer current)
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward regex nil t)
(progn
(setq nb (1+ nb))
(setq fct-name (replace-regexp-in-string "[\t\n{]*" "" (buffer-substring (match-beginning 0) (match-end 0))))
(setq fct-loc (point))
(set-buffer buffer)
;; insert hyperlink to the source code
(widget-create 'push-button
:button-face 'default
:tag fct-name
:value (cons current fct-loc)
:help-echo (concat "Jump to " fct-name)
:notify (lambda (widget &rest ignore)
(pop-to-buffer (car (widget-value widget)))
(goto-char (cdr (widget-value widget)))
(kill-buffer buf-name)
)
)
(widget-insert "\n")
(set-buffer current))))
(set-buffer buffer)
(widget-insert (format "\n%d occurence%s found in %s\n" nb
(if (> nb 1) "s" "") (buffer-name current)))
(widget-create 'push-button
:tag "Cancel"
:notify 'kill-func-buffer)
(use-local-map show-func-map)
(setq buffer-read-only t)
)
(pop-to-buffer buffer))
)