Merge changes from emacs-23 branch
[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.
d2ce10d2
GM
224 (let ((inhibit-read-only t))
225 (erase-buffer)
226 ;; Insert the contents.
227 (let ((lastfile nil))
228 (dolist (T (oref results :hit-tags))
229 (unless (equal lastfile (semantic-tag-file-name T))
230 (setq lastfile (semantic-tag-file-name T))
231 (insert-button lastfile
232 'mouse-face 'custom-button-pressed-face
233 'action 'semantic-symref-rb-goto-file
234 'tag T)
235 (insert "\n"))
236 (insert " ")
237 (insert-button "[+]"
238 'mouse-face 'highlight
239 'face nil
240 'action 'semantic-symref-rb-toggle-expand-tag
a4bdf715 241 'tag T
d2ce10d2
GM
242 'state 'closed)
243 (insert " ")
244 (insert-button (funcall semantic-symref-results-summary-function
245 T nil t)
246 'mouse-face 'custom-button-pressed-face
247 'face nil
248 'action 'semantic-symref-rb-goto-tag
249 'tag T)
250 (insert "\n")))
251 ;; Auto expand
252 (when semantic-symref-auto-expand-results
253 (semantic-symref-list-expand-all)))
254 ;; Clean up the mess
255 (set-buffer-modified-p nil))
a4bdf715
CY
256
257;;; Commands for semantic-symref-results
258;;
259(defun semantic-symref-list-toggle-showing ()
260 "Toggle showing the contents below the current line."
261 (interactive)
262 (beginning-of-line)
263 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
264 (forward-char -1)
265 (push-button)))
266
267(defun semantic-symref-rb-toggle-expand-tag (&optional button)
268 "Go to the file specified in the symref results buffer.
269BUTTON is the button that was clicked."
270 (interactive)
271 (let* ((tag (button-get button 'tag))
272 (buff (semantic-tag-buffer tag))
273 (hits (semantic--tag-get-property tag :hit))
274 (state (button-get button 'state))
d2ce10d2 275 (text nil))
a4bdf715
CY
276 (cond
277 ((eq state 'closed)
0816d744 278 (with-current-buffer buff
a4bdf715
CY
279 (dolist (H hits)
280 (goto-char (point-min))
281 (forward-line (1- H))
282 (beginning-of-line)
283 (back-to-indentation)
284 (setq text (cons (buffer-substring (point) (point-at-eol)) text)))
d2ce10d2 285 (setq text (nreverse text)))
a4bdf715
CY
286 (goto-char (button-start button))
287 (forward-char 1)
d2ce10d2
GM
288 (let ((inhibit-read-only t))
289 (delete-char 1)
290 (insert "-")
291 (button-put button 'state 'open)
292 (save-excursion
293 (end-of-line)
294 (while text
295 (insert "\n")
296 (insert " ")
297 (insert-button (car text)
298 'mouse-face 'highlight
299 'face nil
300 'action 'semantic-symref-rb-goto-match
301 'tag tag
302 'line (car hits))
303 (setq text (cdr text)
304 hits (cdr hits))))))
a4bdf715 305 ((eq state 'open)
d2ce10d2
GM
306 (let ((inhibit-read-only t))
307 (button-put button 'state 'closed)
308 ;; Delete the various bits.
309 (goto-char (button-start button))
a4bdf715 310 (forward-char 1)
d2ce10d2
GM
311 (delete-char 1)
312 (insert "+")
313 (save-excursion
314 (end-of-line)
315 (forward-char 1)
316 (delete-region (point)
317 (save-excursion
318 (forward-char 1)
319 (forward-line (length hits))
320 (point)))))))))
a4bdf715
CY
321
322(defun semantic-symref-rb-goto-file (&optional button)
323 "Go to the file specified in the symref results buffer.
324BUTTON is the button that was clicked."
325 (let* ((tag (button-get button 'tag))
326 (buff (semantic-tag-buffer tag))
327 (win (selected-window))
328 )
329 (switch-to-buffer-other-window buff)
330 (pulse-momentary-highlight-one-line (point))
1fe1547a 331 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
332 ))
333
334
335(defun semantic-symref-rb-goto-tag (&optional button)
336 "Go to the file specified in the symref results buffer.
337BUTTON is the button that was clicked."
338 (interactive)
339 (let* ((tag (button-get button 'tag))
340 (buff (semantic-tag-buffer tag))
341 (win (selected-window))
342 )
343 (switch-to-buffer-other-window buff)
344 (semantic-go-to-tag tag)
345 (pulse-momentary-highlight-one-line (point))
1fe1547a 346 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
347 )
348 )
349
350(defun semantic-symref-rb-goto-match (&optional button)
351 "Go to the file specified in the symref results buffer.
352BUTTON is the button that was clicked."
353 (interactive)
354 (let* ((tag (button-get button 'tag))
355 (line (button-get button 'line))
356 (buff (semantic-tag-buffer tag))
357 (win (selected-window))
358 )
359 (switch-to-buffer-other-window buff)
dd9af436
CY
360 (goto-char (point-min))
361 (forward-line (1- line))
a4bdf715 362 (pulse-momentary-highlight-one-line (point))
1fe1547a 363 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
364 )
365 )
366
367(defun semantic-symref-list-next-line ()
368 "Next line in `semantic-symref-results-mode'."
369 (interactive)
370 (forward-line 1)
371 (back-to-indentation))
372
373(defun semantic-symref-list-prev-line ()
374 "Next line in `semantic-symref-results-mode'."
375 (interactive)
376 (forward-line -1)
377 (back-to-indentation))
378
dd9af436
CY
379(defun semantic-symref-list-expand-all ()
380 "Expand all the nodes in the current buffer."
381 (interactive)
382 (let ((start (make-marker)))
383 (move-marker start (point))
384 (goto-char (point-min))
385 (while (re-search-forward "\\[[+]\\]" nil t)
386 (semantic-symref-list-toggle-showing))
387 ;; Restore position
388 (goto-char start)))
389
390(defun semantic-symref-list-contract-all ()
391 "Expand all the nodes in the current buffer."
392 (interactive)
393 (let ((start (make-marker)))
394 (move-marker start (point))
395 (goto-char (point-min))
396 (while (re-search-forward "\\[[-]\\]" nil t)
397 (semantic-symref-list-toggle-showing))
398 ;; Restore position
399 (goto-char start)))
400
401;;; UTILS
402;;
403;; List mode utils for understadning the current line
404
405(defun semantic-symref-list-on-hit-p ()
406 "Return the line number if the cursor is on a buffer line with a hit.
407Hits are the line of code from the buffer, not the tag summar or file lines."
408 (save-excursion
409 (end-of-line)
410 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
411 (when ol (semantic-overlay-get ol 'line)))))
412
413
414;;; Keyboard Macros on a Hit
415;;
416;; Record a macro on a hit, and store in a special way for execution later.
417(defun semantic-symref-list-create-macro-on-open-hit ()
418 "Record a keyboard macro at the location of the hit in the current list.
419Under point should be one hit for the active keyword. Move
420cursor to the beginning of that symbol, then record a macro as if
421`kmacro-start-macro' was pressed. Use `kmacro-end-macro',
422{kmacro-end-macro} to end the macro, and return to the symbol found list."
423 (interactive)
424 (let* ((oldsym (oref (oref semantic-symref-current-results
425 :created-by)
426 :searchfor))
427 (ol (save-excursion
428 (end-of-line)
429 (car (semantic-overlays-at (1- (point))))))
430 (tag (when ol (semantic-overlay-get ol 'tag)))
431 (line (when ol (semantic-overlay-get ol 'line))))
432 (when (not line)
433 (error "Cannot create macro on a non-hit line"))
434 ;; Go there, and do something useful.
435 (switch-to-buffer-other-window (semantic-tag-buffer tag))
436 (goto-char (point-min))
437 (forward-line (1- line))
438 (when (not (re-search-forward (regexp-quote oldsym) (point-at-eol) t))
439 (error "Cannot find hit. Cannot record macro"))
440 (goto-char (match-beginning 0))
441 ;; Cursor is now in the right location. Start recording a macro.
442 (kmacro-start-macro nil)
443 ;; Notify the user
444 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
445
446(defun semantic-symref-list-call-macro-on-open-hits ()
447 "Call the most recently created keyboard macro on each hit.
448Cursor is placed at the beginning of the symbol found, even if
449there is more than one symbol on the current line. The
450previously recorded macro is then executed."
451 (interactive)
452 (save-window-excursion
453 (let ((count (semantic-symref-list-map-open-hits
454 (lambda ()
455 (switch-to-buffer (current-buffer))
456 (kmacro-call-macro nil)))))
457 (semantic-symref-list-update-open-hits)
458 (message "Executed Macro %d times." count))))
459
460;;; REFACTORING EDITS
461;;
462;; Utilities and features for refactoring across a list of hits.
463;;
464(defun semantic-symref-list-rename-open-hits (newname)
465 "Rename the discovered symbol references to NEWNAME.
466Only renames the locations that are open in the symref list.
467Closed items will be skipped."
468 (interactive
469 (list (read-string "Rename to: "
470 (oref (oref semantic-symref-current-results
471 :created-by)
472 :searchfor))))
473 (let ((count (semantic-symref-list-map-open-hits
474 (lambda () (replace-match newname nil t)))))
475 (semantic-symref-list-update-open-hits)
c0943d3d 476 (message "Renamed %d occurrences." count)))
dd9af436
CY
477
478;;; REFACTORING UTILITIES
479;;
480;; Refactoring tools want to operate on only the "good" stuff the
481;; user selected.
482(defun semantic-symref-list-map-open-hits (function)
483 "For every open hit in the symref buffer, perform FUNCTION.
484The `match-data' will be set to a successful hit of the searched for symbol.
c0943d3d 485Return the number of occurrences FUNCTION was operated upon."
dd9af436
CY
486
487 ;; First Pass in this function - a straight rename.
488 ;; Second Pass - Allow context specification based on
489 ;; class members. (Not Done)
490
491 (let ((oldsym (oref (oref semantic-symref-current-results
492 :created-by)
493 :searchfor))
494 (count 0))
495 (save-excursion
496 (goto-char (point-min))
497 (while (not (eobp))
498 ;; Is this line a "hit" line?
499 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
500 (tag (when ol (semantic-overlay-get ol 'tag)))
501 (line (when ol (semantic-overlay-get ol 'line))))
502 (when line
503 ;; The "line" means we have an open hit.
504 (with-current-buffer (semantic-tag-buffer tag)
505 (goto-char (point-min))
506 (forward-line (1- line))
507 (beginning-of-line)
508 (while (re-search-forward (regexp-quote oldsym) (point-at-eol) t)
509 (setq count (1+ count))
510 (save-excursion ;; Leave cursor after the matched name.
511 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
512 (funcall function))))))
513 ;; Go to the next line
514 (forward-line 1)
515 (end-of-line)))
516 count))
517
518(defun semantic-symref-list-update-open-hits ()
519 "Update the text for all the open hits in the symref list."
520 (save-excursion
521 (goto-char (point-min))
522 (while (re-search-forward "\\[-\\]" nil t)
523 (end-of-line)
524 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
525 (tag (when ol (semantic-overlay-get ol 'tag))))
526 ;; If there is a tag, then close/open it.
527 (when tag
528 (semantic-symref-list-toggle-showing)
529 (semantic-symref-list-toggle-showing))))))
530
a4bdf715
CY
531(provide 'semantic/symref/list)
532
1fe1547a
CY
533;; Local variables:
534;; generated-autoload-file: "../loaddefs.el"
1fe1547a
CY
535;; generated-autoload-load-name: "semantic/symref/list"
536;; End:
537
a4bdf715 538;;; semantic/symref/list.el ends here