1 ;;; semantic/symref/list.el --- Symref Output List UI.
3 ;; Copyright (C) 2008-2014 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 (semantic-tag-name (semantic-complete-read-tag-buffer-deep
91 (message "Gathering References...")
92 ;; Gather results and tags
93 (let ((res (semantic-symref-find-text sym
)))
94 (semantic-symref-produce-list-on-results res sym
)))
98 (defgroup semantic-symref-results-mode nil
99 "Symref Results group."
102 (defvar semantic-symref-results-mode-map
103 (let ((km (make-sparse-keymap)))
105 (define-key km
"\C-i" 'forward-button
)
106 (define-key km
"\M-C-i" 'backward-button
)
107 (define-key km
" " 'push-button
)
108 (define-key km
"-" 'semantic-symref-list-toggle-showing
)
109 (define-key km
"=" 'semantic-symref-list-toggle-showing
)
110 (define-key km
"+" 'semantic-symref-list-toggle-showing
)
111 (define-key km
"n" 'semantic-symref-list-next-line
)
112 (define-key km
"p" 'semantic-symref-list-prev-line
)
113 (define-key km
"q" 'semantic-symref-hide-buffer
)
114 (define-key km
"\C-c\C-e" 'semantic-symref-list-expand-all
)
115 (define-key km
"\C-c\C-r" 'semantic-symref-list-contract-all
)
116 (define-key km
"R" 'semantic-symref-list-rename-open-hits
)
117 (define-key km
"(" 'semantic-symref-list-create-macro-on-open-hit
)
118 (define-key km
"E" 'semantic-symref-list-call-macro-on-open-hits
)
120 "Keymap used in `semantic-symref-results-mode'.")
122 (defvar semantic-symref-list-menu-entries
127 semantic-symref-list-toggle-showing
129 :help
"Toggle the current line open or closed."
132 ["Expand All Entries"
133 semantic-symref-list-expand-all
135 :help
"Expand every expandable entry."
138 ["Contract All Entries"
139 semantic-symref-list-contract-all
141 :help
"Close every expandable entry."
144 ["Rename Symbol in Open hits"
145 semantic-symref-list-rename-open-hits
147 :help
"Rename the searched for symbol in all hits that are currently open."
150 "Menu entries for the Semantic Symref list mode.")
152 (defvar semantic-symref-list-menu nil
153 "Menu keymap build from `semantic-symref-results-mode'.")
155 (easy-menu-define semantic-symref-list-menu
156 semantic-symref-results-mode-map
158 semantic-symref-list-menu-entries
)
160 (defcustom semantic-symref-auto-expand-results nil
161 "Non-nil to expand symref results on buffer creation."
164 (defcustom semantic-symref-results-mode-hook nil
165 "Hook run when `semantic-symref-results-mode' starts."
168 (defvar semantic-symref-current-results nil
169 "The current results in a results mode buffer.")
171 (defun semantic-symref-produce-list-on-results (res str
)
172 "Produce a symref list mode buffer on the results RES."
173 (when (not res
) (error "No references found"))
174 (semantic-symref-result-get-tags res t
)
175 (message "Gathering References...done")
176 ;; Build a references buffer.
177 (let ((buff (get-buffer-create (format "*Symref %s" str
))))
178 (switch-to-buffer-other-window buff
)
180 (semantic-symref-results-mode)
181 (set (make-local-variable 'semantic-symref-current-results
) res
)
182 (semantic-symref-results-dump res
)
183 (goto-char (point-min))))
185 (define-derived-mode semantic-symref-results-mode nil
"Symref"
186 "Major-mode for displaying Semantic Symbol Reference results."
187 (buffer-disable-undo)
188 ;; FIXME: Why bother turning off font-lock?
189 (set (make-local-variable 'font-lock-global-modes
) nil
)
192 (defun semantic-symref-hide-buffer ()
193 "Hide buffer with semantic-symref results."
197 (defcustom semantic-symref-results-summary-function
'semantic-format-tag-prototype
198 "Function to use when creating items in Imenu.
199 Some useful functions are found in `semantic-format-tag-functions'."
200 :type semantic-format-tag-custom-list
)
202 (defun semantic-symref-results-dump (results)
203 "Dump the RESULTS into the current buffer."
204 ;; Get ready for the insert.
205 (let ((inhibit-read-only t
))
207 ;; Insert the contents.
208 (let ((lastfile nil
))
209 (dolist (T (oref results
:hit-tags
))
210 (unless (equal lastfile
(semantic-tag-file-name T
))
211 (setq lastfile
(semantic-tag-file-name T
))
212 (insert-button lastfile
213 'mouse-face
'custom-button-pressed-face
214 'action
'semantic-symref-rb-goto-file
219 'mouse-face
'highlight
221 'action
'semantic-symref-rb-toggle-expand-tag
225 (insert-button (funcall semantic-symref-results-summary-function
227 'mouse-face
'custom-button-pressed-face
229 'action
'semantic-symref-rb-goto-tag
233 (when semantic-symref-auto-expand-results
234 (semantic-symref-list-expand-all)))
236 (set-buffer-modified-p nil
))
238 ;;; Commands for semantic-symref-results
240 (defun semantic-symref-list-toggle-showing ()
241 "Toggle showing the contents below the current line."
244 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t
)
248 (defun semantic-symref-rb-toggle-expand-tag (&optional button
)
249 "Go to the file specified in the symref results buffer.
250 BUTTON is the button that was clicked."
252 (let* ((tag (button-get button
'tag
))
253 (buff (semantic-tag-buffer tag
))
254 (hits (semantic--tag-get-property tag
:hit
))
255 (state (button-get button
'state
))
259 (with-current-buffer buff
261 (goto-char (point-min))
262 (forward-line (1- H
))
264 (back-to-indentation)
265 (setq text
(cons (buffer-substring (point) (point-at-eol)) text
)))
266 (setq text
(nreverse text
)))
267 (goto-char (button-start button
))
269 (let ((inhibit-read-only t
))
272 (button-put button
'state
'open
)
278 (insert-button (car text
)
279 'mouse-face
'highlight
281 'action
'semantic-symref-rb-goto-match
284 (setq text
(cdr text
)
287 (let ((inhibit-read-only t
))
288 (button-put button
'state
'closed
)
289 ;; Delete the various bits.
290 (goto-char (button-start button
))
297 (delete-region (point)
300 (forward-line (length hits
))
303 (defun semantic-symref-rb-goto-file (&optional button
)
304 "Go to the file specified in the symref results buffer.
305 BUTTON is the button that was clicked."
306 (let* ((tag (button-get button
'tag
))
307 (buff (semantic-tag-buffer tag
))
308 (win (selected-window))
310 (switch-to-buffer-other-window buff
)
311 (pulse-momentary-highlight-one-line (point))
312 (when (eq last-command-event ?\s
) (select-window win
))
316 (defun semantic-symref-rb-goto-tag (&optional button
)
317 "Go to the file specified in the symref results buffer.
318 BUTTON is the button that was clicked."
320 (let* ((tag (button-get button
'tag
))
321 (buff (semantic-tag-buffer tag
))
322 (win (selected-window))
324 (switch-to-buffer-other-window buff
)
325 (semantic-go-to-tag tag
)
326 (pulse-momentary-highlight-one-line (point))
327 (when (eq last-command-event ?\s
) (select-window win
))
331 (defun semantic-symref-rb-goto-match (&optional button
)
332 "Go to the file specified in the symref results buffer.
333 BUTTON is the button that was clicked."
335 (let* ((tag (button-get button
'tag
))
336 (line (button-get button
'line
))
337 (buff (semantic-tag-buffer tag
))
338 (win (selected-window))
340 (switch-to-buffer-other-window buff
)
341 (goto-char (point-min))
342 (forward-line (1- line
))
343 (pulse-momentary-highlight-one-line (point))
344 (when (eq last-command-event ?\s
) (select-window win
))
348 (defun semantic-symref-list-next-line ()
349 "Next line in `semantic-symref-results-mode'."
352 (back-to-indentation))
354 (defun semantic-symref-list-prev-line ()
355 "Next line in `semantic-symref-results-mode'."
358 (back-to-indentation))
360 (defun semantic-symref-list-expand-all ()
361 "Expand all the nodes in the current buffer."
363 (let ((start (make-marker)))
364 (move-marker start
(point))
365 (goto-char (point-min))
366 (while (re-search-forward "\\[[+]\\]" nil t
)
367 (semantic-symref-list-toggle-showing))
371 (defun semantic-symref-list-contract-all ()
372 "Expand all the nodes in the current buffer."
374 (let ((start (make-marker)))
375 (move-marker start
(point))
376 (goto-char (point-min))
377 (while (re-search-forward "\\[[-]\\]" nil t
)
378 (semantic-symref-list-toggle-showing))
384 ;; List mode utils for understanding the current line
386 (defun semantic-symref-list-on-hit-p ()
387 "Return the line number if the cursor is on a buffer line with a hit.
388 Hits are the line of code from the buffer, not the tag summar or file lines."
391 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
392 (when ol
(semantic-overlay-get ol
'line
)))))
395 ;;; Keyboard Macros on a Hit
397 ;; Record a macro on a hit, and store in a special way for execution later.
398 (defun semantic-symref-list-create-macro-on-open-hit ()
399 "Record a keyboard macro at the location of the hit in the current list.
400 Under point should be one hit for the active keyword. Move
401 cursor to the beginning of that symbol, then record a macro as if
402 `kmacro-start-macro' was pressed. Use `kmacro-end-macro',
403 {kmacro-end-macro} to end the macro, and return to the symbol found list."
405 (let* ((oldsym (oref (oref semantic-symref-current-results
410 (car (semantic-overlays-at (1- (point))))))
411 (tag (when ol
(semantic-overlay-get ol
'tag
)))
412 (line (when ol
(semantic-overlay-get ol
'line
))))
414 (error "Cannot create macro on a non-hit line"))
415 ;; Go there, and do something useful.
416 (switch-to-buffer-other-window (semantic-tag-buffer tag
))
417 (goto-char (point-min))
418 (forward-line (1- line
))
419 (when (not (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
))
420 (error "Cannot find hit. Cannot record macro"))
421 (goto-char (match-beginning 0))
422 ;; Cursor is now in the right location. Start recording a macro.
423 (kmacro-start-macro nil
)
425 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
427 (defun semantic-symref-list-call-macro-on-open-hits ()
428 "Call the most recently created keyboard macro on each hit.
429 Cursor is placed at the beginning of the symbol found, even if
430 there is more than one symbol on the current line. The
431 previously recorded macro is then executed."
433 (save-window-excursion
434 (let ((count (semantic-symref-list-map-open-hits
436 (switch-to-buffer (current-buffer))
437 (kmacro-call-macro nil
)))))
438 (semantic-symref-list-update-open-hits)
439 (message "Executed Macro %d times." count
))))
441 ;;; REFACTORING EDITS
443 ;; Utilities and features for refactoring across a list of hits.
445 (defun semantic-symref-list-rename-open-hits (newname)
446 "Rename the discovered symbol references to NEWNAME.
447 Only renames the locations that are open in the symref list.
448 Closed items will be skipped."
450 (list (read-string "Rename to: "
451 (oref (oref semantic-symref-current-results
454 (let ((count (semantic-symref-list-map-open-hits
455 (lambda () (replace-match newname nil t
)))))
456 (semantic-symref-list-update-open-hits)
457 (message "Renamed %d occurrences." count
)))
459 ;;; REFACTORING UTILITIES
461 ;; Refactoring tools want to operate on only the "good" stuff the
463 (defun semantic-symref-list-map-open-hits (function)
464 "For every open hit in the symref buffer, perform FUNCTION.
465 The `match-data' will be set to a successful hit of the searched for symbol.
466 Return the number of occurrences FUNCTION was operated upon."
468 ;; First Pass in this function - a straight rename.
469 ;; Second Pass - Allow context specification based on
470 ;; class members. (Not Done)
472 (let ((oldsym (oref (oref semantic-symref-current-results
477 (goto-char (point-min))
479 ;; Is this line a "hit" line?
480 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
481 (tag (when ol
(semantic-overlay-get ol
'tag
)))
482 (line (when ol
(semantic-overlay-get ol
'line
))))
484 ;; The "line" means we have an open hit.
485 (with-current-buffer (semantic-tag-buffer tag
)
486 (goto-char (point-min))
487 (forward-line (1- line
))
489 (while (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
)
490 (setq count
(1+ count
))
491 (save-excursion ;; Leave cursor after the matched name.
492 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
493 (funcall function
))))))
494 ;; Go to the next line
499 (defun semantic-symref-list-update-open-hits ()
500 "Update the text for all the open hits in the symref list."
502 (goto-char (point-min))
503 (while (re-search-forward "\\[-\\]" nil t
)
505 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
506 (tag (when ol
(semantic-overlay-get ol
'tag
))))
507 ;; If there is a tag, then close/open it.
509 (semantic-symref-list-toggle-showing)
510 (semantic-symref-list-toggle-showing))))))
512 (provide 'semantic
/symref
/list
)
515 ;; generated-autoload-file: "../loaddefs.el"
516 ;; generated-autoload-load-name: "semantic/symref/list"
519 ;;; semantic/symref/list.el ends here