1 ;;; semantic/symref/list.el --- Symref Output List UI.
3 ;; Copyright (C) 2008, 2009, 2010 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 execture [FIXME what?] `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))
55 (when (not ct
) (error "Place cursor inside tag to be searched for"))
57 (when (not (y-or-n-p (format "Find references for %s? " (semantic-tag-name ct
))))
59 ;; Gather results and tags
60 (message "Gathering References...")
61 (setq res
(semantic-symref-find-references-by-name (semantic-tag-name ct
)))
62 (semantic-symref-produce-list-on-results res
(semantic-tag-name ct
))))
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-buffer-deep
77 ;; Gather results and tags
78 (message "Gathering References...")
79 (setq res
(semantic-symref-find-references-by-name sym
))
80 (semantic-symref-produce-list-on-results res sym
)))
83 (defun semantic-symref-regexp (sym)
84 "Find references to the a symbol regexp SYM.
85 This command uses the currently configured references tool within the
86 current project to find references to the input SYM. The
87 references are the organized by file and the name of the function
89 Display the references in`semantic-symref-results-mode'."
90 (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
95 ;; Gather results and tags
96 (message "Gathering References...")
97 (setq res
(semantic-symref-find-text sym
))
98 (semantic-symref-produce-list-on-results res sym
)))
101 (defun semantic-symref-produce-list-on-results (res str
)
102 "Produce a symref list mode buffer on the results RES."
103 (when (not res
) (error "No references found"))
104 (semantic-symref-result-get-tags res t
)
105 (message "Gathering References...done")
106 ;; Build a refrences buffer.
107 (let ((buff (get-buffer-create
108 (format "*Symref %s" str
)))
110 (switch-to-buffer-other-window buff
)
112 (semantic-symref-results-mode res
))
117 (defgroup semantic-symref-results-mode nil
118 "Symref Results group."
121 (defvar semantic-symref-results-mode-map
122 (let ((km (make-sparse-keymap)))
123 (define-key km
"\C-i" 'forward-button
)
124 (define-key km
"\M-C-i" 'backward-button
)
125 (define-key km
" " 'push-button
)
126 (define-key km
"-" 'semantic-symref-list-toggle-showing
)
127 (define-key km
"=" 'semantic-symref-list-toggle-showing
)
128 (define-key km
"+" 'semantic-symref-list-toggle-showing
)
129 (define-key km
"n" 'semantic-symref-list-next-line
)
130 (define-key km
"p" 'semantic-symref-list-prev-line
)
131 (define-key km
"q" 'semantic-symref-hide-buffer
)
132 (define-key km
"\C-c\C-e" 'semantic-symref-list-expand-all
)
133 (define-key km
"\C-c\C-r" 'semantic-symref-list-contract-all
)
134 (define-key km
"R" 'semantic-symref-list-rename-open-hits
)
135 (define-key km
"(" 'semantic-symref-list-create-macro-on-open-hit
)
136 (define-key km
"E" 'semantic-symref-list-call-macro-on-open-hits
)
138 "Keymap used in `semantic-symref-results-mode'.")
140 (defvar semantic-symref-list-menu-entries
145 semantic-symref-list-toggle-showing
147 :help
"Toggle the current line open or closed."
150 ["Expand All Entries"
151 semantic-symref-list-expand-all
153 :help
"Expand every expandable entry."
156 ["Contract All Entries"
157 semantic-symref-list-contract-all
159 :help
"Close every expandable entry."
162 ["Rename Symbol in Open hits"
163 semantic-symref-list-rename-open-hits
165 :help
"Rename the searched for symbol in all hits that are currently open."
168 "Menu entries for the Semantic Symref list mode.")
170 (defvar semantic-symref-list-menu nil
171 "Menu keymap build from `semantic-symref-results-mode'.")
173 (easy-menu-define semantic-symref-list-menu
174 semantic-symref-results-mode-map
176 semantic-symref-list-menu-entries
)
178 (defcustom semantic-symref-auto-expand-results nil
179 "Non-nil to expand symref results on buffer creation."
180 :group
'semantic-symref
183 (defcustom semantic-symref-results-mode-hook nil
184 "Hook run when `semantic-symref-results-mode' starts."
185 :group
'semantic-symref
188 (defvar semantic-symref-current-results nil
189 "The current results in a results mode buffer.")
191 (defun semantic-symref-results-mode (results)
192 "Major-mode for displaying Semantic Symbol Reference RESULTS.
193 RESULTS is an object of class `semantic-symref-results'."
195 (kill-all-local-variables)
196 (setq major-mode
'semantic-symref-results-mode
199 (use-local-map semantic-symref-results-mode-map
)
200 (set (make-local-variable 'semantic-symref-current-results
)
202 (semantic-symref-results-dump results
)
203 (goto-char (point-min))
204 (buffer-disable-undo)
205 (set (make-local-variable 'font-lock-global-modes
) nil
)
207 (run-hooks 'semantic-symref-results-mode-hook
)
210 (defun semantic-symref-hide-buffer ()
211 "Hide buffer with semantic-symref results."
215 (defcustom semantic-symref-results-summary-function
'semantic-format-tag-prototype
216 "*Function to use when creating items in Imenu.
217 Some useful functions are found in `semantic-format-tag-functions'."
218 :group
'semantic-symref
219 :type semantic-format-tag-custom-list
)
221 (defun semantic-symref-results-dump (results)
222 "Dump the RESULTS into the current buffer."
223 ;; Get ready for the insert.
224 (toggle-read-only -
1)
227 ;; Insert the contents.
230 (dolist (T (oref results
:hit-tags
))
232 (when (not (equal lastfile
(semantic-tag-file-name T
)))
233 (setq lastfile
(semantic-tag-file-name T
))
234 (insert-button lastfile
235 'mouse-face
'custom-button-pressed-face
236 'action
'semantic-symref-rb-goto-file
243 'mouse-face
'highlight
245 'action
'semantic-symref-rb-toggle-expand-tag
249 (insert-button (funcall semantic-symref-results-summary-function
251 'mouse-face
'custom-button-pressed-face
253 'action
'semantic-symref-rb-goto-tag
260 (when semantic-symref-auto-expand-results
261 (semantic-symref-list-expand-all))
265 (set-buffer-modified-p nil
)
268 ;;; Commands for semantic-symref-results
270 (defun semantic-symref-list-toggle-showing ()
271 "Toggle showing the contents below the current line."
274 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t
)
278 (defun semantic-symref-rb-toggle-expand-tag (&optional button
)
279 "Go to the file specified in the symref results buffer.
280 BUTTON is the button that was clicked."
282 (let* ((tag (button-get button
'tag
))
283 (buff (semantic-tag-buffer tag
))
284 (hits (semantic--tag-get-property tag
:hit
))
285 (state (button-get button
'state
))
290 (toggle-read-only -
1)
291 (with-current-buffer buff
293 (goto-char (point-min))
294 (forward-line (1- H
))
296 (back-to-indentation)
297 (setq text
(cons (buffer-substring (point) (point-at-eol)) text
)))
298 (setq text
(nreverse text
))
300 (goto-char (button-start button
))
304 (button-put button
'state
'open
)
310 (insert-button (car text
)
311 'mouse-face
'highlight
313 'action
'semantic-symref-rb-goto-match
316 (setq text
(cdr text
)
321 (toggle-read-only -
1)
322 (button-put button
'state
'closed
)
323 ;; Delete the various bits.
324 (goto-char (button-start button
))
331 (delete-region (point)
334 (forward-line (length hits
))
341 (defun semantic-symref-rb-goto-file (&optional button
)
342 "Go to the file specified in the symref results buffer.
343 BUTTON is the button that was clicked."
344 (let* ((tag (button-get button
'tag
))
345 (buff (semantic-tag-buffer tag
))
346 (win (selected-window))
348 (switch-to-buffer-other-window buff
)
349 (pulse-momentary-highlight-one-line (point))
350 (when (eq last-command-event ?\s
) (select-window win
))
354 (defun semantic-symref-rb-goto-tag (&optional button
)
355 "Go to the file specified in the symref results buffer.
356 BUTTON is the button that was clicked."
358 (let* ((tag (button-get button
'tag
))
359 (buff (semantic-tag-buffer tag
))
360 (win (selected-window))
362 (switch-to-buffer-other-window buff
)
363 (semantic-go-to-tag tag
)
364 (pulse-momentary-highlight-one-line (point))
365 (when (eq last-command-event ?\s
) (select-window win
))
369 (defun semantic-symref-rb-goto-match (&optional button
)
370 "Go to the file specified in the symref results buffer.
371 BUTTON is the button that was clicked."
373 (let* ((tag (button-get button
'tag
))
374 (line (button-get button
'line
))
375 (buff (semantic-tag-buffer tag
))
376 (win (selected-window))
378 (switch-to-buffer-other-window buff
)
379 (goto-char (point-min))
380 (forward-line (1- line
))
381 (pulse-momentary-highlight-one-line (point))
382 (when (eq last-command-event ?\s
) (select-window win
))
386 (defun semantic-symref-list-next-line ()
387 "Next line in `semantic-symref-results-mode'."
390 (back-to-indentation))
392 (defun semantic-symref-list-prev-line ()
393 "Next line in `semantic-symref-results-mode'."
396 (back-to-indentation))
398 (defun semantic-symref-list-expand-all ()
399 "Expand all the nodes in the current buffer."
401 (let ((start (make-marker)))
402 (move-marker start
(point))
403 (goto-char (point-min))
404 (while (re-search-forward "\\[[+]\\]" nil t
)
405 (semantic-symref-list-toggle-showing))
409 (defun semantic-symref-list-contract-all ()
410 "Expand all the nodes in the current buffer."
412 (let ((start (make-marker)))
413 (move-marker start
(point))
414 (goto-char (point-min))
415 (while (re-search-forward "\\[[-]\\]" nil t
)
416 (semantic-symref-list-toggle-showing))
422 ;; List mode utils for understadning the current line
424 (defun semantic-symref-list-on-hit-p ()
425 "Return the line number if the cursor is on a buffer line with a hit.
426 Hits are the line of code from the buffer, not the tag summar or file lines."
429 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
430 (when ol
(semantic-overlay-get ol
'line
)))))
433 ;;; Keyboard Macros on a Hit
435 ;; Record a macro on a hit, and store in a special way for execution later.
436 (defun semantic-symref-list-create-macro-on-open-hit ()
437 "Record a keyboard macro at the location of the hit in the current list.
438 Under point should be one hit for the active keyword. Move
439 cursor to the beginning of that symbol, then record a macro as if
440 `kmacro-start-macro' was pressed. Use `kmacro-end-macro',
441 {kmacro-end-macro} to end the macro, and return to the symbol found list."
443 (let* ((oldsym (oref (oref semantic-symref-current-results
448 (car (semantic-overlays-at (1- (point))))))
449 (tag (when ol
(semantic-overlay-get ol
'tag
)))
450 (line (when ol
(semantic-overlay-get ol
'line
))))
452 (error "Cannot create macro on a non-hit line"))
453 ;; Go there, and do something useful.
454 (switch-to-buffer-other-window (semantic-tag-buffer tag
))
455 (goto-char (point-min))
456 (forward-line (1- line
))
457 (when (not (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
))
458 (error "Cannot find hit. Cannot record macro"))
459 (goto-char (match-beginning 0))
460 ;; Cursor is now in the right location. Start recording a macro.
461 (kmacro-start-macro nil
)
463 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
465 (defun semantic-symref-list-call-macro-on-open-hits ()
466 "Call the most recently created keyboard macro on each hit.
467 Cursor is placed at the beginning of the symbol found, even if
468 there is more than one symbol on the current line. The
469 previously recorded macro is then executed."
471 (save-window-excursion
472 (let ((count (semantic-symref-list-map-open-hits
474 (switch-to-buffer (current-buffer))
475 (kmacro-call-macro nil
)))))
476 (semantic-symref-list-update-open-hits)
477 (message "Executed Macro %d times." count
))))
479 ;;; REFACTORING EDITS
481 ;; Utilities and features for refactoring across a list of hits.
483 (defun semantic-symref-list-rename-open-hits (newname)
484 "Rename the discovered symbol references to NEWNAME.
485 Only renames the locations that are open in the symref list.
486 Closed items will be skipped."
488 (list (read-string "Rename to: "
489 (oref (oref semantic-symref-current-results
492 (let ((count (semantic-symref-list-map-open-hits
493 (lambda () (replace-match newname nil t
)))))
494 (semantic-symref-list-update-open-hits)
495 (message "Renamed %d occurrences." count
)))
497 ;;; REFACTORING UTILITIES
499 ;; Refactoring tools want to operate on only the "good" stuff the
501 (defun semantic-symref-list-map-open-hits (function)
502 "For every open hit in the symref buffer, perform FUNCTION.
503 The `match-data' will be set to a successful hit of the searched for symbol.
504 Return the number of occurrences FUNCTION was operated upon."
506 ;; First Pass in this function - a straight rename.
507 ;; Second Pass - Allow context specification based on
508 ;; class members. (Not Done)
510 (let ((oldsym (oref (oref semantic-symref-current-results
515 (goto-char (point-min))
517 ;; Is this line a "hit" line?
518 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
519 (tag (when ol
(semantic-overlay-get ol
'tag
)))
520 (line (when ol
(semantic-overlay-get ol
'line
))))
522 ;; The "line" means we have an open hit.
523 (with-current-buffer (semantic-tag-buffer tag
)
524 (goto-char (point-min))
525 (forward-line (1- line
))
527 (while (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
)
528 (setq count
(1+ count
))
529 (save-excursion ;; Leave cursor after the matched name.
530 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
531 (funcall function
))))))
532 ;; Go to the next line
537 (defun semantic-symref-list-update-open-hits ()
538 "Update the text for all the open hits in the symref list."
540 (goto-char (point-min))
541 (while (re-search-forward "\\[-\\]" nil t
)
543 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
544 (tag (when ol
(semantic-overlay-get ol
'tag
))))
545 ;; If there is a tag, then close/open it.
547 (semantic-symref-list-toggle-showing)
548 (semantic-symref-list-toggle-showing))))))
550 (provide 'semantic
/symref
/list
)
553 ;; generated-autoload-file: "../loaddefs.el"
554 ;; generated-autoload-load-name: "semantic/symref/list"
557 ;; arch-tag: e355d9c6-26e0-42d1-9bf1-f4801a54fffa
558 ;;; semantic/symref/list.el ends here