1 ;;; semantic/symref/list.el --- Symref Output List UI.
3 ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Provide a simple user facing API to finding symbol references.
26 ;; This UI is the base of some refactoring tools. For any refactor,
27 ;; the user will execute `semantic-symref' in a tag.
28 ;; Once that data is collected, the output will be listed in a buffer.
29 ;; In the output buffer, the user can then initiate different
30 ;; refactoring operations.
32 ;; NOTE: Need to add some refactoring tools.
34 (require 'semantic
/symref
)
35 (require 'semantic
/complete
)
36 (require 'semantic
/senator
)
42 (defun semantic-symref ()
43 "Find references to the current tag.
44 This command uses the currently configured references tool within the
45 current project to find references to the current tag. The
46 references are organized by file and the name of the function
48 Display the references in `semantic-symref-results-mode'."
51 (let ((ct (semantic-current-tag)))
53 (when (not ct
) (error "Place cursor inside tag to be searched for"))
55 (when (not (y-or-n-p (format "Find references for %s? "
56 (semantic-tag-name ct
))))
58 ;; Gather results and tags
59 (message "Gathering References...")
60 (let* ((name (semantic-tag-name ct
))
61 (res (semantic-symref-find-references-by-name name
)))
62 (semantic-symref-produce-list-on-results res name
))))
65 (defun semantic-symref-symbol (sym)
66 "Find references to the symbol SYM.
67 This command uses the currently configured references tool within the
68 current project to find references to the input SYM. The
69 references are organized by file and the name of the function
71 Display the references in `semantic-symref-results-mode'."
72 (interactive (list (semantic-tag-name (semantic-complete-read-tag-project
75 ;; Gather results and tags
76 (message "Gathering References...")
77 (let ((res (semantic-symref-find-references-by-name sym
)))
78 (semantic-symref-produce-list-on-results res sym
)))
81 (defun semantic-symref-regexp (sym)
82 "Find references to the a symbol regexp SYM.
83 This command uses the currently configured references tool within the
84 current project to find references to the input SYM. The
85 references are the organized by file and the name of the function
87 Display the references in `semantic-symref-results-mode'."
88 (interactive (list (let ((tag (semantic-current-tag)))
89 (read-string " Symrefs for: " nil nil
91 (regexp-quote (semantic-tag-name tag
)))))))
92 ;; FIXME: Shouldn't the input be in Emacs regexp format, for
93 ;; consistency? Converting it to extended is not hard.
95 (message "Gathering References...")
96 ;; Gather results and tags
97 (let ((res (semantic-symref-find-text sym
)))
98 (semantic-symref-produce-list-on-results res sym
)))
102 (defgroup semantic-symref-results-mode nil
103 "Symref Results group."
106 (defvar semantic-symref-results-mode-map
107 (let ((km (make-sparse-keymap)))
109 (define-key km
"\C-i" 'forward-button
)
110 (define-key km
"\M-C-i" 'backward-button
)
111 (define-key km
" " 'push-button
)
112 (define-key km
"-" 'semantic-symref-list-toggle-showing
)
113 (define-key km
"=" 'semantic-symref-list-toggle-showing
)
114 (define-key km
"+" 'semantic-symref-list-toggle-showing
)
115 (define-key km
"n" 'semantic-symref-list-next-line
)
116 (define-key km
"p" 'semantic-symref-list-prev-line
)
117 (define-key km
"q" 'semantic-symref-hide-buffer
)
118 (define-key km
"\C-c\C-e" 'semantic-symref-list-expand-all
)
119 (define-key km
"\C-c\C-r" 'semantic-symref-list-contract-all
)
120 (define-key km
"R" 'semantic-symref-list-rename-open-hits
)
121 (define-key km
"(" 'semantic-symref-list-create-macro-on-open-hit
)
122 (define-key km
"E" 'semantic-symref-list-call-macro-on-open-hits
)
124 "Keymap used in `semantic-symref-results-mode'.")
126 (defvar semantic-symref-list-menu-entries
131 semantic-symref-list-toggle-showing
133 :help
"Toggle the current line open or closed."
136 ["Expand All Entries"
137 semantic-symref-list-expand-all
139 :help
"Expand every expandable entry."
142 ["Contract All Entries"
143 semantic-symref-list-contract-all
145 :help
"Close every expandable entry."
148 ["Rename Symbol in Open hits"
149 semantic-symref-list-rename-open-hits
151 :help
"Rename the searched for symbol in all hits that are currently open."
154 "Menu entries for the Semantic Symref list mode.")
156 (defvar semantic-symref-list-menu nil
157 "Menu keymap build from `semantic-symref-results-mode'.")
159 (easy-menu-define semantic-symref-list-menu
160 semantic-symref-results-mode-map
162 semantic-symref-list-menu-entries
)
164 (defcustom semantic-symref-auto-expand-results nil
165 "Non-nil to expand symref results on buffer creation."
168 (defcustom semantic-symref-results-mode-hook nil
169 "Hook run when `semantic-symref-results-mode' starts."
172 (defvar semantic-symref-current-results nil
173 "The current results in a results mode buffer.")
175 (defun semantic-symref-produce-list-on-results (res str
)
176 "Produce a symref list mode buffer on the results RES."
177 (when (not res
) (error "No references found"))
178 (semantic-symref-result-get-tags res t
)
179 (message "Gathering References...done")
180 ;; Build a references buffer.
181 (let ((buff (get-buffer-create (format "*Symref %s" str
))))
182 (switch-to-buffer-other-window buff
)
184 (semantic-symref-results-mode)
185 (set (make-local-variable 'semantic-symref-current-results
) res
)
186 (semantic-symref-results-dump res
)
187 (goto-char (point-min))))
189 (define-derived-mode semantic-symref-results-mode nil
"Symref"
190 "Major-mode for displaying Semantic Symbol Reference results."
191 (buffer-disable-undo)
192 ;; FIXME: Why bother turning off font-lock?
193 (set (make-local-variable 'font-lock-global-modes
) nil
)
196 (defun semantic-symref-hide-buffer ()
197 "Hide buffer with semantic-symref results."
201 (defcustom semantic-symref-results-summary-function
'semantic-format-tag-prototype
202 "Function to use when creating items in Imenu.
203 Some useful functions are found in `semantic-format-tag-functions'."
204 :type semantic-format-tag-custom-list
)
206 (defun semantic-symref-results-dump (results)
207 "Dump the RESULTS into the current buffer."
208 ;; Get ready for the insert.
209 (let ((inhibit-read-only t
))
211 ;; Insert the contents.
212 (let ((lastfile nil
))
213 (dolist (T (oref results
:hit-tags
))
214 (unless (equal lastfile
(semantic-tag-file-name T
))
215 (setq lastfile
(semantic-tag-file-name T
))
216 (insert-button lastfile
217 'mouse-face
'custom-button-pressed-face
218 'action
'semantic-symref-rb-goto-file
223 'mouse-face
'highlight
225 'action
'semantic-symref-rb-toggle-expand-tag
229 (insert-button (funcall semantic-symref-results-summary-function
231 'mouse-face
'custom-button-pressed-face
233 'action
'semantic-symref-rb-goto-tag
237 (when semantic-symref-auto-expand-results
238 (semantic-symref-list-expand-all)))
240 (set-buffer-modified-p nil
))
242 ;;; Commands for semantic-symref-results
244 (defun semantic-symref-list-toggle-showing ()
245 "Toggle showing the contents below the current line."
248 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t
)
252 (defun semantic-symref-rb-toggle-expand-tag (&optional button
)
253 "Go to the file specified in the symref results buffer.
254 BUTTON is the button that was clicked."
256 (let* ((tag (button-get button
'tag
))
257 (buff (semantic-tag-buffer tag
))
258 (hits (semantic--tag-get-property tag
:hit
))
259 (state (button-get button
'state
))
263 (with-current-buffer buff
265 (goto-char (point-min))
266 (forward-line (1- H
))
268 (back-to-indentation)
269 (setq text
(cons (buffer-substring (point) (point-at-eol)) text
)))
270 (setq text
(nreverse text
)))
271 (goto-char (button-start button
))
273 (let ((inhibit-read-only t
))
276 (button-put button
'state
'open
)
282 (insert-button (car text
)
283 'mouse-face
'highlight
285 'action
'semantic-symref-rb-goto-match
288 (setq text
(cdr text
)
291 (let ((inhibit-read-only t
))
292 (button-put button
'state
'closed
)
293 ;; Delete the various bits.
294 (goto-char (button-start button
))
301 (delete-region (point)
304 (forward-line (length hits
))
307 (defun semantic-symref-rb-goto-file (&optional button
)
308 "Go to the file specified in the symref results buffer.
309 BUTTON is the button that was clicked."
310 (let* ((tag (button-get button
'tag
))
311 (buff (semantic-tag-buffer tag
))
312 (win (selected-window))
314 (switch-to-buffer-other-window buff
)
315 (pulse-momentary-highlight-one-line (point))
316 (when (eq last-command-event ?\s
) (select-window win
))
320 (defun semantic-symref-rb-goto-tag (&optional button
)
321 "Go to the file specified in the symref results buffer.
322 BUTTON is the button that was clicked."
324 (let* ((tag (button-get button
'tag
))
325 (buff (semantic-tag-buffer tag
))
326 (win (selected-window))
328 (switch-to-buffer-other-window buff
)
329 (semantic-go-to-tag tag
)
330 (pulse-momentary-highlight-one-line (point))
331 (when (eq last-command-event ?\s
) (select-window win
))
335 (defun semantic-symref-rb-goto-match (&optional button
)
336 "Go to the file specified in the symref results buffer.
337 BUTTON is the button that was clicked."
339 (let* ((tag (button-get button
'tag
))
340 (line (button-get button
'line
))
341 (buff (semantic-tag-buffer tag
))
342 (win (selected-window))
344 (switch-to-buffer-other-window buff
)
345 (goto-char (point-min))
346 (forward-line (1- line
))
347 (pulse-momentary-highlight-one-line (point))
348 (when (eq last-command-event ?\s
) (select-window win
))
352 (defun semantic-symref-list-next-line ()
353 "Next line in `semantic-symref-results-mode'."
356 (back-to-indentation))
358 (defun semantic-symref-list-prev-line ()
359 "Next line in `semantic-symref-results-mode'."
362 (back-to-indentation))
364 (defun semantic-symref-list-expand-all ()
365 "Expand all the nodes in the current buffer."
367 (let ((start (make-marker)))
368 (move-marker start
(point))
369 (goto-char (point-min))
370 (while (re-search-forward "\\[[+]\\]" nil t
)
371 (semantic-symref-list-toggle-showing))
375 (defun semantic-symref-list-contract-all ()
376 "Expand all the nodes in the current buffer."
378 (let ((start (make-marker)))
379 (move-marker start
(point))
380 (goto-char (point-min))
381 (while (re-search-forward "\\[[-]\\]" nil t
)
382 (semantic-symref-list-toggle-showing))
388 ;; List mode utils for understanding the current line
390 (defun semantic-symref-list-on-hit-p ()
391 "Return the line number if the cursor is on a buffer line with a hit.
392 Hits are the line of code from the buffer, not the tag summar or file lines."
395 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
396 (when ol
(semantic-overlay-get ol
'line
)))))
399 ;;; Keyboard Macros on a Hit
401 ;; Record a macro on a hit, and store in a special way for execution later.
402 (defun semantic-symref-list-create-macro-on-open-hit ()
403 "Record a keyboard macro at the location of the hit in the current list.
404 Under point should be one hit for the active keyword. Move
405 cursor to the beginning of that symbol, then record a macro as if
406 `kmacro-start-macro' was pressed. Use `kmacro-end-macro',
407 {kmacro-end-macro} to end the macro, and return to the symbol found list."
409 (let* ((oldsym (oref (oref semantic-symref-current-results
414 (car (semantic-overlays-at (1- (point))))))
415 (tag (when ol
(semantic-overlay-get ol
'tag
)))
416 (line (when ol
(semantic-overlay-get ol
'line
))))
418 (error "Cannot create macro on a non-hit line"))
419 ;; Go there, and do something useful.
420 (switch-to-buffer-other-window (semantic-tag-buffer tag
))
421 (goto-char (point-min))
422 (forward-line (1- line
))
423 (when (not (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
))
424 (error "Cannot find hit. Cannot record macro"))
425 (goto-char (match-beginning 0))
426 ;; Cursor is now in the right location. Start recording a macro.
427 (kmacro-start-macro nil
)
429 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
431 (defun semantic-symref-list-call-macro-on-open-hits ()
432 "Call the most recently created keyboard macro on each hit.
433 Cursor is placed at the beginning of the symbol found, even if
434 there is more than one symbol on the current line. The
435 previously recorded macro is then executed."
437 (save-window-excursion
438 (let ((count (semantic-symref-list-map-open-hits
440 (switch-to-buffer (current-buffer))
441 (kmacro-call-macro nil
)))))
442 (semantic-symref-list-update-open-hits)
443 (message "Executed Macro %d times." count
))))
445 ;;; REFACTORING EDITS
447 ;; Utilities and features for refactoring across a list of hits.
449 (defun semantic-symref-list-rename-open-hits (newname)
450 "Rename the discovered symbol references to NEWNAME.
451 Only renames the locations that are open in the symref list.
452 Closed items will be skipped."
454 (list (read-string "Rename to: "
455 (oref (oref semantic-symref-current-results
458 (let ((count (semantic-symref-list-map-open-hits
459 (lambda () (replace-match newname nil t
)))))
460 (semantic-symref-list-update-open-hits)
461 (message "Renamed %d occurrences." count
)))
463 ;;; REFACTORING UTILITIES
465 ;; Refactoring tools want to operate on only the "good" stuff the
467 (defun semantic-symref-list-map-open-hits (function)
468 "For every open hit in the symref buffer, perform FUNCTION.
469 The `match-data' will be set to a successful hit of the searched for symbol.
470 Return the number of occurrences FUNCTION was operated upon."
472 ;; First Pass in this function - a straight rename.
473 ;; Second Pass - Allow context specification based on
474 ;; class members. (Not Done)
476 (let ((oldsym (oref (oref semantic-symref-current-results
481 (goto-char (point-min))
483 ;; Is this line a "hit" line?
484 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
485 (tag (when ol
(semantic-overlay-get ol
'tag
)))
486 (line (when ol
(semantic-overlay-get ol
'line
))))
488 ;; The "line" means we have an open hit.
489 (with-current-buffer (semantic-tag-buffer tag
)
490 (goto-char (point-min))
491 (forward-line (1- line
))
493 (while (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
)
494 (setq count
(1+ count
))
495 (save-excursion ;; Leave cursor after the matched name.
496 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
497 (funcall function
))))))
498 ;; Go to the next line
503 (defun semantic-symref-list-update-open-hits ()
504 "Update the text for all the open hits in the symref list."
506 (goto-char (point-min))
507 (while (re-search-forward "\\[-\\]" nil t
)
509 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
510 (tag (when ol
(semantic-overlay-get ol
'tag
))))
511 ;; If there is a tag, then close/open it.
513 (semantic-symref-list-toggle-showing)
514 (semantic-symref-list-toggle-showing))))))
516 (provide 'semantic
/symref
/list
)
519 ;; generated-autoload-file: "../loaddefs.el"
520 ;; generated-autoload-load-name: "semantic/symref/list"
523 ;;; semantic/symref/list.el ends here