1 ;;; semantic/symref/list.el --- Symref Output List UI.
3 ;; Copyright (C) 2008-2011 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 ;; FIXME: Use define-derived-mode.
193 "Major-mode for displaying Semantic Symbol Reference RESULTS.
194 RESULTS is an object of class `semantic-symref-results'."
196 (kill-all-local-variables)
197 (setq major-mode
'semantic-symref-results-mode
200 (use-local-map semantic-symref-results-mode-map
)
201 (set (make-local-variable 'semantic-symref-current-results
)
203 (semantic-symref-results-dump results
)
204 (goto-char (point-min))
205 (buffer-disable-undo)
206 (set (make-local-variable 'font-lock-global-modes
) nil
)
208 (run-mode-hooks 'semantic-symref-results-mode-hook
)
211 (defun semantic-symref-hide-buffer ()
212 "Hide buffer with semantic-symref results."
216 (defcustom semantic-symref-results-summary-function
'semantic-format-tag-prototype
217 "*Function to use when creating items in Imenu.
218 Some useful functions are found in `semantic-format-tag-functions'."
219 :group
'semantic-symref
220 :type semantic-format-tag-custom-list
)
222 (defun semantic-symref-results-dump (results)
223 "Dump the RESULTS into the current buffer."
224 ;; Get ready for the insert.
225 (let ((inhibit-read-only t
))
227 ;; Insert the contents.
228 (let ((lastfile nil
))
229 (dolist (T (oref results
:hit-tags
))
230 (unless (equal lastfile
(semantic-tag-file-name T
))
231 (setq lastfile
(semantic-tag-file-name T
))
232 (insert-button lastfile
233 'mouse-face
'custom-button-pressed-face
234 'action
'semantic-symref-rb-goto-file
239 'mouse-face
'highlight
241 'action
'semantic-symref-rb-toggle-expand-tag
245 (insert-button (funcall semantic-symref-results-summary-function
247 'mouse-face
'custom-button-pressed-face
249 'action
'semantic-symref-rb-goto-tag
253 (when semantic-symref-auto-expand-results
254 (semantic-symref-list-expand-all)))
256 (set-buffer-modified-p nil
))
258 ;;; Commands for semantic-symref-results
260 (defun semantic-symref-list-toggle-showing ()
261 "Toggle showing the contents below the current line."
264 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t
)
268 (defun semantic-symref-rb-toggle-expand-tag (&optional button
)
269 "Go to the file specified in the symref results buffer.
270 BUTTON is the button that was clicked."
272 (let* ((tag (button-get button
'tag
))
273 (buff (semantic-tag-buffer tag
))
274 (hits (semantic--tag-get-property tag
:hit
))
275 (state (button-get button
'state
))
279 (with-current-buffer buff
281 (goto-char (point-min))
282 (forward-line (1- H
))
284 (back-to-indentation)
285 (setq text
(cons (buffer-substring (point) (point-at-eol)) text
)))
286 (setq text
(nreverse text
)))
287 (goto-char (button-start button
))
289 (let ((inhibit-read-only t
))
292 (button-put button
'state
'open
)
298 (insert-button (car text
)
299 'mouse-face
'highlight
301 'action
'semantic-symref-rb-goto-match
304 (setq text
(cdr text
)
307 (let ((inhibit-read-only t
))
308 (button-put button
'state
'closed
)
309 ;; Delete the various bits.
310 (goto-char (button-start button
))
317 (delete-region (point)
320 (forward-line (length hits
))
323 (defun semantic-symref-rb-goto-file (&optional button
)
324 "Go to the file specified in the symref results buffer.
325 BUTTON is the button that was clicked."
326 (let* ((tag (button-get button
'tag
))
327 (buff (semantic-tag-buffer tag
))
328 (win (selected-window))
330 (switch-to-buffer-other-window buff
)
331 (pulse-momentary-highlight-one-line (point))
332 (when (eq last-command-event ?\s
) (select-window win
))
336 (defun semantic-symref-rb-goto-tag (&optional button
)
337 "Go to the file specified in the symref results buffer.
338 BUTTON is the button that was clicked."
340 (let* ((tag (button-get button
'tag
))
341 (buff (semantic-tag-buffer tag
))
342 (win (selected-window))
344 (switch-to-buffer-other-window buff
)
345 (semantic-go-to-tag tag
)
346 (pulse-momentary-highlight-one-line (point))
347 (when (eq last-command-event ?\s
) (select-window win
))
351 (defun semantic-symref-rb-goto-match (&optional button
)
352 "Go to the file specified in the symref results buffer.
353 BUTTON is the button that was clicked."
355 (let* ((tag (button-get button
'tag
))
356 (line (button-get button
'line
))
357 (buff (semantic-tag-buffer tag
))
358 (win (selected-window))
360 (switch-to-buffer-other-window buff
)
361 (goto-char (point-min))
362 (forward-line (1- line
))
363 (pulse-momentary-highlight-one-line (point))
364 (when (eq last-command-event ?\s
) (select-window win
))
368 (defun semantic-symref-list-next-line ()
369 "Next line in `semantic-symref-results-mode'."
372 (back-to-indentation))
374 (defun semantic-symref-list-prev-line ()
375 "Next line in `semantic-symref-results-mode'."
378 (back-to-indentation))
380 (defun semantic-symref-list-expand-all ()
381 "Expand all the nodes in the current buffer."
383 (let ((start (make-marker)))
384 (move-marker start
(point))
385 (goto-char (point-min))
386 (while (re-search-forward "\\[[+]\\]" nil t
)
387 (semantic-symref-list-toggle-showing))
391 (defun semantic-symref-list-contract-all ()
392 "Expand all the nodes in the current buffer."
394 (let ((start (make-marker)))
395 (move-marker start
(point))
396 (goto-char (point-min))
397 (while (re-search-forward "\\[[-]\\]" nil t
)
398 (semantic-symref-list-toggle-showing))
404 ;; List mode utils for understadning the current line
406 (defun semantic-symref-list-on-hit-p ()
407 "Return the line number if the cursor is on a buffer line with a hit.
408 Hits are the line of code from the buffer, not the tag summar or file lines."
411 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
412 (when ol
(semantic-overlay-get ol
'line
)))))
415 ;;; Keyboard Macros on a Hit
417 ;; Record a macro on a hit, and store in a special way for execution later.
418 (defun semantic-symref-list-create-macro-on-open-hit ()
419 "Record a keyboard macro at the location of the hit in the current list.
420 Under point should be one hit for the active keyword. Move
421 cursor to the beginning of that symbol, then record a macro as if
422 `kmacro-start-macro' was pressed. Use `kmacro-end-macro',
423 {kmacro-end-macro} to end the macro, and return to the symbol found list."
425 (let* ((oldsym (oref (oref semantic-symref-current-results
430 (car (semantic-overlays-at (1- (point))))))
431 (tag (when ol
(semantic-overlay-get ol
'tag
)))
432 (line (when ol
(semantic-overlay-get ol
'line
))))
434 (error "Cannot create macro on a non-hit line"))
435 ;; Go there, and do something useful.
436 (switch-to-buffer-other-window (semantic-tag-buffer tag
))
437 (goto-char (point-min))
438 (forward-line (1- line
))
439 (when (not (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
))
440 (error "Cannot find hit. Cannot record macro"))
441 (goto-char (match-beginning 0))
442 ;; Cursor is now in the right location. Start recording a macro.
443 (kmacro-start-macro nil
)
445 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
447 (defun semantic-symref-list-call-macro-on-open-hits ()
448 "Call the most recently created keyboard macro on each hit.
449 Cursor is placed at the beginning of the symbol found, even if
450 there is more than one symbol on the current line. The
451 previously recorded macro is then executed."
453 (save-window-excursion
454 (let ((count (semantic-symref-list-map-open-hits
456 (switch-to-buffer (current-buffer))
457 (kmacro-call-macro nil
)))))
458 (semantic-symref-list-update-open-hits)
459 (message "Executed Macro %d times." count
))))
461 ;;; REFACTORING EDITS
463 ;; Utilities and features for refactoring across a list of hits.
465 (defun semantic-symref-list-rename-open-hits (newname)
466 "Rename the discovered symbol references to NEWNAME.
467 Only renames the locations that are open in the symref list.
468 Closed items will be skipped."
470 (list (read-string "Rename to: "
471 (oref (oref semantic-symref-current-results
474 (let ((count (semantic-symref-list-map-open-hits
475 (lambda () (replace-match newname nil t
)))))
476 (semantic-symref-list-update-open-hits)
477 (message "Renamed %d occurrences." count
)))
479 ;;; REFACTORING UTILITIES
481 ;; Refactoring tools want to operate on only the "good" stuff the
483 (defun semantic-symref-list-map-open-hits (function)
484 "For every open hit in the symref buffer, perform FUNCTION.
485 The `match-data' will be set to a successful hit of the searched for symbol.
486 Return the number of occurrences FUNCTION was operated upon."
488 ;; First Pass in this function - a straight rename.
489 ;; Second Pass - Allow context specification based on
490 ;; class members. (Not Done)
492 (let ((oldsym (oref (oref semantic-symref-current-results
497 (goto-char (point-min))
499 ;; Is this line a "hit" line?
500 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
501 (tag (when ol
(semantic-overlay-get ol
'tag
)))
502 (line (when ol
(semantic-overlay-get ol
'line
))))
504 ;; The "line" means we have an open hit.
505 (with-current-buffer (semantic-tag-buffer tag
)
506 (goto-char (point-min))
507 (forward-line (1- line
))
509 (while (re-search-forward (regexp-quote oldsym
) (point-at-eol) t
)
510 (setq count
(1+ count
))
511 (save-excursion ;; Leave cursor after the matched name.
512 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
513 (funcall function
))))))
514 ;; Go to the next line
519 (defun semantic-symref-list-update-open-hits ()
520 "Update the text for all the open hits in the symref list."
522 (goto-char (point-min))
523 (while (re-search-forward "\\[-\\]" nil t
)
525 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
526 (tag (when ol
(semantic-overlay-get ol
'tag
))))
527 ;; If there is a tag, then close/open it.
529 (semantic-symref-list-toggle-showing)
530 (semantic-symref-list-toggle-showing))))))
532 (provide 'semantic
/symref
/list
)
535 ;; generated-autoload-file: "../loaddefs.el"
536 ;; generated-autoload-load-name: "semantic/symref/list"
539 ;;; semantic/symref/list.el ends here