Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / cedet / semantic / symref / list.el
CommitLineData
a4bdf715
CY
1;;; semantic/symref/list.el --- Symref Output List UI.
2
acaf905b 3;; Copyright (C) 2008-2012 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 26;; This UI is the base of some refactoring tools. For any refactor,
cd1181db 27;; the user will execute `semantic-symref' in a tag.
9bf6c65c
GM
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")
cd1181db 106 ;; Build a references buffer.
a4bdf715
CY
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)
4d789d84 192 ;; FIXME: Use define-derived-mode.
a4bdf715
CY
193 "Major-mode for displaying Semantic Symbol Reference RESULTS.
194RESULTS 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"
199 )
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)
4d789d84 208 (run-mode-hooks 'semantic-symref-results-mode-hook)
a4bdf715
CY
209 )
210
211(defun semantic-symref-hide-buffer ()
9bf6c65c 212 "Hide buffer with semantic-symref results."
a4bdf715
CY
213 (interactive)
214 (bury-buffer))
215
216(defcustom semantic-symref-results-summary-function 'semantic-format-tag-prototype
217 "*Function to use when creating items in Imenu.
218Some useful functions are found in `semantic-format-tag-functions'."
219 :group 'semantic-symref
220 :type semantic-format-tag-custom-list)
221
222(defun semantic-symref-results-dump (results)
223 "Dump the RESULTS into the current buffer."
224 ;; Get ready for the insert.
d2ce10d2
GM
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
a4bdf715 242 'tag T
d2ce10d2
GM
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))
a4bdf715
CY
257
258;;; Commands for semantic-symref-results
259;;
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)))
267
268(defun semantic-symref-rb-toggle-expand-tag (&optional button)
269 "Go to the file specified in the symref results buffer.
270BUTTON 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))
d2ce10d2 276 (text nil))
a4bdf715
CY
277 (cond
278 ((eq state 'closed)
0816d744 279 (with-current-buffer buff
a4bdf715
CY
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)))
d2ce10d2 286 (setq text (nreverse text)))
a4bdf715
CY
287 (goto-char (button-start button))
288 (forward-char 1)
d2ce10d2
GM
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))))))
a4bdf715 306 ((eq state 'open)
d2ce10d2
GM
307 (let ((inhibit-read-only t))
308 (button-put button 'state 'closed)
309 ;; Delete the various bits.
310 (goto-char (button-start button))
a4bdf715 311 (forward-char 1)
d2ce10d2
GM
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)))))))))
a4bdf715
CY
322
323(defun semantic-symref-rb-goto-file (&optional button)
324 "Go to the file specified in the symref results buffer.
325BUTTON is the button that was clicked."
326 (let* ((tag (button-get button 'tag))
327 (buff (semantic-tag-buffer tag))
328 (win (selected-window))
329 )
330 (switch-to-buffer-other-window buff)
331 (pulse-momentary-highlight-one-line (point))
1fe1547a 332 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
333 ))
334
335
336(defun semantic-symref-rb-goto-tag (&optional button)
337 "Go to the file specified in the symref results buffer.
338BUTTON 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))
343 )
344 (switch-to-buffer-other-window buff)
345 (semantic-go-to-tag tag)
346 (pulse-momentary-highlight-one-line (point))
1fe1547a 347 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
348 )
349 )
350
351(defun semantic-symref-rb-goto-match (&optional button)
352 "Go to the file specified in the symref results buffer.
353BUTTON 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))
359 )
360 (switch-to-buffer-other-window buff)
dd9af436
CY
361 (goto-char (point-min))
362 (forward-line (1- line))
a4bdf715 363 (pulse-momentary-highlight-one-line (point))
1fe1547a 364 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
365 )
366 )
367
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))
373
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))
379
dd9af436
CY
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)))
390
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)))
401
402;;; UTILS
403;;
ee7683eb 404;; List mode utils for understanding the current line
dd9af436
CY
405
406(defun semantic-symref-list-on-hit-p ()
407 "Return the line number if the cursor is on a buffer line with a hit.
408Hits 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)))))
413
414
415;;; Keyboard Macros on a Hit
416;;
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.
420Under point should be one hit for the active keyword. Move
421cursor 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.")))
446
447(defun semantic-symref-list-call-macro-on-open-hits ()
448 "Call the most recently created keyboard macro on each hit.
449Cursor is placed at the beginning of the symbol found, even if
450there is more than one symbol on the current line. The
451previously 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))))
460
461;;; REFACTORING EDITS
462;;
463;; Utilities and features for refactoring across a list of hits.
464;;
465(defun semantic-symref-list-rename-open-hits (newname)
466 "Rename the discovered symbol references to NEWNAME.
467Only renames the locations that are open in the symref list.
468Closed 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)
c0943d3d 477 (message "Renamed %d occurrences." count)))
dd9af436
CY
478
479;;; REFACTORING UTILITIES
480;;
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.
485The `match-data' will be set to a successful hit of the searched for symbol.
c0943d3d 486Return the number of occurrences FUNCTION was operated upon."
dd9af436
CY
487
488 ;; First Pass in this function - a straight rename.
489 ;; Second Pass - Allow context specification based on
490 ;; class members. (Not Done)
491
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))
518
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))))))
531
a4bdf715
CY
532(provide 'semantic/symref/list)
533
1fe1547a
CY
534;; Local variables:
535;; generated-autoload-file: "../loaddefs.el"
1fe1547a
CY
536;; generated-autoload-load-name: "semantic/symref/list"
537;; End:
538
a4bdf715 539;;; semantic/symref/list.el ends here