Fix typos.
[emacs.git] / lisp / cedet / semantic / symref / list.el
blobce3500f83e1053980b52667f979c768d0be70b76
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/>.
22 ;;; Commentary:
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)
37 (require 'pulse)
39 ;;; Code:
41 ;;;###autoload
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
47 they are used in.
48 Display the references in `semantic-symref-results-mode'."
49 (interactive)
50 (semantic-fetch-tags)
51 (let ((ct (semantic-current-tag))
52 (res nil)
54 ;; Must have a tag...
55 (when (not ct) (error "Place cursor inside tag to be searched for"))
56 ;; Check w/ user.
57 (when (not (y-or-n-p (format "Find references for %s? " (semantic-tag-name ct))))
58 (error "Quit"))
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))))
64 ;;;###autoload
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
70 they are used in.
71 Display the references in `semantic-symref-results-mode'."
72 (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
73 "Symrefs for: "))))
74 (semantic-fetch-tags)
75 (let ((res nil)
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)))
82 ;;;###autoload
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
88 they are used in.
89 Display the references in`semantic-symref-results-mode'."
90 (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
91 "Symrefs for: "))))
92 (semantic-fetch-tags)
93 (let ((res nil)
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 references buffer.
107 (let ((buff (get-buffer-create
108 (format "*Symref %s" str)))
110 (switch-to-buffer-other-window buff)
111 (set-buffer buff)
112 (semantic-symref-results-mode res))
115 ;;; RESULTS MODE
117 (defgroup semantic-symref-results-mode nil
118 "Symref Results group."
119 :group 'semantic)
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
141 (list
142 "Symref"
143 (semantic-menu-item
144 ["Toggle Line Open"
145 semantic-symref-list-toggle-showing
146 :active t
147 :help "Toggle the current line open or closed."
149 (semantic-menu-item
150 ["Expand All Entries"
151 semantic-symref-list-expand-all
152 :active t
153 :help "Expand every expandable entry."
155 (semantic-menu-item
156 ["Contract All Entries"
157 semantic-symref-list-contract-all
158 :active t
159 :help "Close every expandable entry."
161 (semantic-menu-item
162 ["Rename Symbol in Open hits"
163 semantic-symref-list-rename-open-hits
164 :active t
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
175 "Symref Mode Menu"
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
181 :type 'boolean)
183 (defcustom semantic-symref-results-mode-hook nil
184 "Hook run when `semantic-symref-results-mode' starts."
185 :group 'semantic-symref
186 :type 'hook)
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'."
195 (interactive)
196 (kill-all-local-variables)
197 (setq major-mode 'semantic-symref-results-mode
198 mode-name "Symref"
200 (use-local-map semantic-symref-results-mode-map)
201 (set (make-local-variable 'semantic-symref-current-results)
202 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)
207 (font-lock-mode -1)
208 (run-mode-hooks 'semantic-symref-results-mode-hook)
211 (defun semantic-symref-hide-buffer ()
212 "Hide buffer with semantic-symref results."
213 (interactive)
214 (bury-buffer))
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))
226 (erase-buffer)
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
235 'tag T)
236 (insert "\n"))
237 (insert " ")
238 (insert-button "[+]"
239 'mouse-face 'highlight
240 'face nil
241 'action 'semantic-symref-rb-toggle-expand-tag
242 'tag T
243 'state 'closed)
244 (insert " ")
245 (insert-button (funcall semantic-symref-results-summary-function
246 T nil t)
247 'mouse-face 'custom-button-pressed-face
248 'face nil
249 'action 'semantic-symref-rb-goto-tag
250 'tag T)
251 (insert "\n")))
252 ;; Auto expand
253 (when semantic-symref-auto-expand-results
254 (semantic-symref-list-expand-all)))
255 ;; Clean up the mess
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."
262 (interactive)
263 (beginning-of-line)
264 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
265 (forward-char -1)
266 (push-button)))
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."
271 (interactive)
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))
276 (text nil))
277 (cond
278 ((eq state 'closed)
279 (with-current-buffer buff
280 (dolist (H hits)
281 (goto-char (point-min))
282 (forward-line (1- H))
283 (beginning-of-line)
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))
288 (forward-char 1)
289 (let ((inhibit-read-only t))
290 (delete-char 1)
291 (insert "-")
292 (button-put button 'state 'open)
293 (save-excursion
294 (end-of-line)
295 (while text
296 (insert "\n")
297 (insert " ")
298 (insert-button (car text)
299 'mouse-face 'highlight
300 'face nil
301 'action 'semantic-symref-rb-goto-match
302 'tag tag
303 'line (car hits))
304 (setq text (cdr text)
305 hits (cdr hits))))))
306 ((eq state 'open)
307 (let ((inhibit-read-only t))
308 (button-put button 'state 'closed)
309 ;; Delete the various bits.
310 (goto-char (button-start button))
311 (forward-char 1)
312 (delete-char 1)
313 (insert "+")
314 (save-excursion
315 (end-of-line)
316 (forward-char 1)
317 (delete-region (point)
318 (save-excursion
319 (forward-char 1)
320 (forward-line (length hits))
321 (point)))))))))
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."
339 (interactive)
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."
354 (interactive)
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'."
370 (interactive)
371 (forward-line 1)
372 (back-to-indentation))
374 (defun semantic-symref-list-prev-line ()
375 "Next line in `semantic-symref-results-mode'."
376 (interactive)
377 (forward-line -1)
378 (back-to-indentation))
380 (defun semantic-symref-list-expand-all ()
381 "Expand all the nodes in the current buffer."
382 (interactive)
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))
388 ;; Restore position
389 (goto-char start)))
391 (defun semantic-symref-list-contract-all ()
392 "Expand all the nodes in the current buffer."
393 (interactive)
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))
399 ;; Restore position
400 (goto-char start)))
402 ;;; UTILS
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."
409 (save-excursion
410 (end-of-line)
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."
424 (interactive)
425 (let* ((oldsym (oref (oref semantic-symref-current-results
426 :created-by)
427 :searchfor))
428 (ol (save-excursion
429 (end-of-line)
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))))
433 (when (not 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)
444 ;; Notify the user
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."
452 (interactive)
453 (save-window-excursion
454 (let ((count (semantic-symref-list-map-open-hits
455 (lambda ()
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."
469 (interactive
470 (list (read-string "Rename to: "
471 (oref (oref semantic-symref-current-results
472 :created-by)
473 :searchfor))))
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
482 ;; user selected.
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
493 :created-by)
494 :searchfor))
495 (count 0))
496 (save-excursion
497 (goto-char (point-min))
498 (while (not (eobp))
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))))
503 (when 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))
508 (beginning-of-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
515 (forward-line 1)
516 (end-of-line)))
517 count))
519 (defun semantic-symref-list-update-open-hits ()
520 "Update the text for all the open hits in the symref list."
521 (save-excursion
522 (goto-char (point-min))
523 (while (re-search-forward "\\[-\\]" nil t)
524 (end-of-line)
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.
528 (when tag
529 (semantic-symref-list-toggle-showing)
530 (semantic-symref-list-toggle-showing))))))
532 (provide 'semantic/symref/list)
534 ;; Local variables:
535 ;; generated-autoload-file: "../loaddefs.el"
536 ;; generated-autoload-load-name: "semantic/symref/list"
537 ;; End:
539 ;;; semantic/symref/list.el ends here