Correct some viewcvs links in docs and comments.
[bpt/emacs.git] / lisp / cedet / semantic / symref / list.el
CommitLineData
a4bdf715
CY
1;;; semantic/symref/list.el --- Symref Output List UI.
2
114f9c96 3;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
a4bdf715
CY
4
5;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7;; This file is part of GNU Emacs.
8
d037e45a
GM
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.
a4bdf715 13
d037e45a
GM
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.
a4bdf715
CY
18
19;; You should have received a copy of the GNU General Public License
d037e45a 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a4bdf715
CY
21
22;;; Commentary:
23;;
24;; Provide a simple user facing API to finding symbol references.
25;;
9bf6c65c
GM
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.
a4bdf715
CY
31;;
32;; NOTE: Need to add some refactoring tools.
33
34(require 'semantic/symref)
78f9c78d 35(require 'semantic/complete)
dd9af436 36(require 'semantic/senator)
a4bdf715
CY
37(require 'pulse)
38
39;;; Code:
40
1fe1547a 41;;;###autoload
a4bdf715
CY
42(defun semantic-symref ()
43 "Find references to the current tag.
44This command uses the currently configured references tool within the
9bf6c65c 45current project to find references to the current tag. The
dd9af436 46references are organized by file and the name of the function
a4bdf715 47they are used in.
dd9af436 48Display the references in `semantic-symref-results-mode'."
a4bdf715
CY
49 (interactive)
50 (semantic-fetch-tags)
51 (let ((ct (semantic-current-tag))
52 (res nil)
53 )
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))))
63
1fe1547a 64;;;###autoload
a4bdf715
CY
65(defun semantic-symref-symbol (sym)
66 "Find references to the symbol SYM.
67This command uses the currently configured references tool within the
68current project to find references to the input SYM. The
dd9af436
CY
69references are organized by file and the name of the function
70they are used in.
71Display 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)
76 )
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)))
81
82;;;###autoload
83(defun semantic-symref-regexp (sym)
84 "Find references to the a symbol regexp SYM.
85This command uses the currently configured references tool within the
86current project to find references to the input SYM. The
a4bdf715
CY
87references are the organized by file and the name of the function
88they are used in.
9bf6c65c 89Display the references in`semantic-symref-results-mode'."
78f9c78d
CY
90 (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
91 "Symrefs for: "))))
a4bdf715
CY
92 (semantic-fetch-tags)
93 (let ((res nil)
94 )
95 ;; Gather results and tags
96 (message "Gathering References...")
dd9af436 97 (setq res (semantic-symref-find-text sym))
a4bdf715
CY
98 (semantic-symref-produce-list-on-results res sym)))
99
100
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)))
109 )
110 (switch-to-buffer-other-window buff)
111 (set-buffer buff)
112 (semantic-symref-results-mode res))
113 )
114
115;;; RESULTS MODE
116;;
117(defgroup semantic-symref-results-mode nil
118 "Symref Results group."
119 :group 'semantic)
120
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)
dd9af436
CY
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)
a4bdf715
CY
137 km)
138 "Keymap used in `semantic-symref-results-mode'.")
139
dd9af436
CY
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."
148 ])
149 (semantic-menu-item
150 ["Expand All Entries"
151 semantic-symref-list-expand-all
152 :active t
153 :help "Expand every expandable entry."
154 ])
155 (semantic-menu-item
156 ["Contract All Entries"
157 semantic-symref-list-contract-all
158 :active t
159 :help "Close every expandable entry."
160 ])
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."
166 ])
167 )
168 "Menu entries for the Semantic Symref list mode.")
169
170(defvar semantic-symref-list-menu nil
171 "Menu keymap build from `semantic-symref-results-mode'.")
172
173(easy-menu-define semantic-symref-list-menu
174 semantic-symref-results-mode-map
175 "Symref Mode Menu"
176 semantic-symref-list-menu-entries)
177
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)
182
a4bdf715 183(defcustom semantic-symref-results-mode-hook nil
dd9af436 184 "Hook run when `semantic-symref-results-mode' starts."
a4bdf715
CY
185 :group 'semantic-symref
186 :type 'hook)
187
188(defvar semantic-symref-current-results nil
189 "The current results in a results mode buffer.")
190
191(defun semantic-symref-results-mode (results)
192 "Major-mode for displaying Semantic Symbol Reference RESULTS.
193RESULTS is an object of class `semantic-symref-results'."
194 (interactive)
195 (kill-all-local-variables)
196 (setq major-mode 'semantic-symref-results-mode
197 mode-name "Symref"
198 )
199 (use-local-map semantic-symref-results-mode-map)
200 (set (make-local-variable 'semantic-symref-current-results)
201 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)
206 (font-lock-mode -1)
207 (run-hooks 'semantic-symref-results-mode-hook)
208 )
209
210(defun semantic-symref-hide-buffer ()
9bf6c65c 211 "Hide buffer with semantic-symref results."
a4bdf715
CY
212 (interactive)
213 (bury-buffer))
214
215(defcustom semantic-symref-results-summary-function 'semantic-format-tag-prototype
216 "*Function to use when creating items in Imenu.
217Some useful functions are found in `semantic-format-tag-functions'."
218 :group 'semantic-symref
219 :type semantic-format-tag-custom-list)
220
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)
225 (erase-buffer)
226
227 ;; Insert the contents.
228 (let ((lastfile nil)
229 )
230 (dolist (T (oref results :hit-tags))
231
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
237 'tag T
238 )
239 (insert "\n"))
240
241 (insert " ")
242 (insert-button "[+]"
243 'mouse-face 'highlight
244 'face nil
245 'action 'semantic-symref-rb-toggle-expand-tag
246 'tag T
247 'state 'closed)
248 (insert " ")
249 (insert-button (funcall semantic-symref-results-summary-function
250 T nil t)
251 'mouse-face 'custom-button-pressed-face
252 'face nil
253 'action 'semantic-symref-rb-goto-tag
254 'tag T)
255 (insert "\n")
256
257 ))
258
dd9af436
CY
259 ;; Auto expand
260 (when semantic-symref-auto-expand-results
261 (semantic-symref-list-expand-all))
262
a4bdf715
CY
263 ;; Clean up the mess
264 (toggle-read-only 1)
265 (set-buffer-modified-p nil)
266 )
267
268;;; Commands for semantic-symref-results
269;;
270(defun semantic-symref-list-toggle-showing ()
271 "Toggle showing the contents below the current line."
272 (interactive)
273 (beginning-of-line)
274 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
275 (forward-char -1)
276 (push-button)))
277
278(defun semantic-symref-rb-toggle-expand-tag (&optional button)
279 "Go to the file specified in the symref results buffer.
280BUTTON is the button that was clicked."
281 (interactive)
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))
286 (text nil)
287 )
288 (cond
289 ((eq state 'closed)
290 (toggle-read-only -1)
0816d744 291 (with-current-buffer buff
a4bdf715
CY
292 (dolist (H hits)
293 (goto-char (point-min))
294 (forward-line (1- H))
295 (beginning-of-line)
296 (back-to-indentation)
297 (setq text (cons (buffer-substring (point) (point-at-eol)) text)))
298 (setq text (nreverse text))
299 )
300 (goto-char (button-start button))
301 (forward-char 1)
302 (delete-char 1)
303 (insert "-")
304 (button-put button 'state 'open)
305 (save-excursion
306 (end-of-line)
307 (while text
308 (insert "\n")
309 (insert " ")
310 (insert-button (car text)
311 'mouse-face 'highlight
312 'face nil
313 'action 'semantic-symref-rb-goto-match
314 'tag tag
315 'line (car hits))
316 (setq text (cdr text)
317 hits (cdr hits))))
318 (toggle-read-only 1)
319 )
320 ((eq state 'open)
321 (toggle-read-only -1)
322 (button-put button 'state 'closed)
323 ;; Delete the various bits.
324 (goto-char (button-start button))
325 (forward-char 1)
326 (delete-char 1)
327 (insert "+")
328 (save-excursion
329 (end-of-line)
330 (forward-char 1)
331 (delete-region (point)
332 (save-excursion
333 (forward-char 1)
334 (forward-line (length hits))
335 (point))))
336 (toggle-read-only 1)
337 )
338 ))
339 )
340
341(defun semantic-symref-rb-goto-file (&optional button)
342 "Go to the file specified in the symref results buffer.
343BUTTON is the button that was clicked."
344 (let* ((tag (button-get button 'tag))
345 (buff (semantic-tag-buffer tag))
346 (win (selected-window))
347 )
348 (switch-to-buffer-other-window buff)
349 (pulse-momentary-highlight-one-line (point))
1fe1547a 350 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
351 ))
352
353
354(defun semantic-symref-rb-goto-tag (&optional button)
355 "Go to the file specified in the symref results buffer.
356BUTTON is the button that was clicked."
357 (interactive)
358 (let* ((tag (button-get button 'tag))
359 (buff (semantic-tag-buffer tag))
360 (win (selected-window))
361 )
362 (switch-to-buffer-other-window buff)
363 (semantic-go-to-tag tag)
364 (pulse-momentary-highlight-one-line (point))
1fe1547a 365 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
366 )
367 )
368
369(defun semantic-symref-rb-goto-match (&optional button)
370 "Go to the file specified in the symref results buffer.
371BUTTON is the button that was clicked."
372 (interactive)
373 (let* ((tag (button-get button 'tag))
374 (line (button-get button 'line))
375 (buff (semantic-tag-buffer tag))
376 (win (selected-window))
377 )
378 (switch-to-buffer-other-window buff)
dd9af436
CY
379 (goto-char (point-min))
380 (forward-line (1- line))
a4bdf715 381 (pulse-momentary-highlight-one-line (point))
1fe1547a 382 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
383 )
384 )
385
386(defun semantic-symref-list-next-line ()
387 "Next line in `semantic-symref-results-mode'."
388 (interactive)
389 (forward-line 1)
390 (back-to-indentation))
391
392(defun semantic-symref-list-prev-line ()
393 "Next line in `semantic-symref-results-mode'."
394 (interactive)
395 (forward-line -1)
396 (back-to-indentation))
397
dd9af436
CY
398(defun semantic-symref-list-expand-all ()
399 "Expand all the nodes in the current buffer."
400 (interactive)
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))
406 ;; Restore position
407 (goto-char start)))
408
409(defun semantic-symref-list-contract-all ()
410 "Expand all the nodes in the current buffer."
411 (interactive)
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))
417 ;; Restore position
418 (goto-char start)))
419
420;;; UTILS
421;;
422;; List mode utils for understadning the current line
423
424(defun semantic-symref-list-on-hit-p ()
425 "Return the line number if the cursor is on a buffer line with a hit.
426Hits are the line of code from the buffer, not the tag summar or file lines."
427 (save-excursion
428 (end-of-line)
429 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
430 (when ol (semantic-overlay-get ol 'line)))))
431
432
433;;; Keyboard Macros on a Hit
434;;
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.
438Under point should be one hit for the active keyword. Move
439cursor 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."
442 (interactive)
443 (let* ((oldsym (oref (oref semantic-symref-current-results
444 :created-by)
445 :searchfor))
446 (ol (save-excursion
447 (end-of-line)
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))))
451 (when (not 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)
462 ;; Notify the user
463 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
464
465(defun semantic-symref-list-call-macro-on-open-hits ()
466 "Call the most recently created keyboard macro on each hit.
467Cursor is placed at the beginning of the symbol found, even if
468there is more than one symbol on the current line. The
469previously recorded macro is then executed."
470 (interactive)
471 (save-window-excursion
472 (let ((count (semantic-symref-list-map-open-hits
473 (lambda ()
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))))
478
479;;; REFACTORING EDITS
480;;
481;; Utilities and features for refactoring across a list of hits.
482;;
483(defun semantic-symref-list-rename-open-hits (newname)
484 "Rename the discovered symbol references to NEWNAME.
485Only renames the locations that are open in the symref list.
486Closed items will be skipped."
487 (interactive
488 (list (read-string "Rename to: "
489 (oref (oref semantic-symref-current-results
490 :created-by)
491 :searchfor))))
492 (let ((count (semantic-symref-list-map-open-hits
493 (lambda () (replace-match newname nil t)))))
494 (semantic-symref-list-update-open-hits)
c0943d3d 495 (message "Renamed %d occurrences." count)))
dd9af436
CY
496
497;;; REFACTORING UTILITIES
498;;
499;; Refactoring tools want to operate on only the "good" stuff the
500;; user selected.
501(defun semantic-symref-list-map-open-hits (function)
502 "For every open hit in the symref buffer, perform FUNCTION.
503The `match-data' will be set to a successful hit of the searched for symbol.
c0943d3d 504Return the number of occurrences FUNCTION was operated upon."
dd9af436
CY
505
506 ;; First Pass in this function - a straight rename.
507 ;; Second Pass - Allow context specification based on
508 ;; class members. (Not Done)
509
510 (let ((oldsym (oref (oref semantic-symref-current-results
511 :created-by)
512 :searchfor))
513 (count 0))
514 (save-excursion
515 (goto-char (point-min))
516 (while (not (eobp))
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))))
521 (when 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))
526 (beginning-of-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
533 (forward-line 1)
534 (end-of-line)))
535 count))
536
537(defun semantic-symref-list-update-open-hits ()
538 "Update the text for all the open hits in the symref list."
539 (save-excursion
540 (goto-char (point-min))
541 (while (re-search-forward "\\[-\\]" nil t)
542 (end-of-line)
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.
546 (when tag
547 (semantic-symref-list-toggle-showing)
548 (semantic-symref-list-toggle-showing))))))
549
a4bdf715
CY
550(provide 'semantic/symref/list)
551
1fe1547a
CY
552;; Local variables:
553;; generated-autoload-file: "../loaddefs.el"
1fe1547a
CY
554;; generated-autoload-load-name: "semantic/symref/list"
555;; End:
556
3999968a 557;; arch-tag: e355d9c6-26e0-42d1-9bf1-f4801a54fffa
a4bdf715 558;;; semantic/symref/list.el ends here