Update copyright notices for 2013.
[bpt/emacs.git] / lisp / cedet / semantic / symref / list.el
CommitLineData
a4bdf715
CY
1;;; semantic/symref/list.el --- Symref Output List UI.
2
ab422c4d 3;; Copyright (C) 2008-2013 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'."
22b762c7 72 (interactive (list (semantic-tag-name (semantic-complete-read-tag-project
dd9af436
CY
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)))
62a81506 123 (suppress-keymap km)
a4bdf715
CY
124 (define-key km "\C-i" 'forward-button)
125 (define-key km "\M-C-i" 'backward-button)
126 (define-key km " " 'push-button)
127 (define-key km "-" 'semantic-symref-list-toggle-showing)
128 (define-key km "=" 'semantic-symref-list-toggle-showing)
129 (define-key km "+" 'semantic-symref-list-toggle-showing)
130 (define-key km "n" 'semantic-symref-list-next-line)
131 (define-key km "p" 'semantic-symref-list-prev-line)
132 (define-key km "q" 'semantic-symref-hide-buffer)
dd9af436
CY
133 (define-key km "\C-c\C-e" 'semantic-symref-list-expand-all)
134 (define-key km "\C-c\C-r" 'semantic-symref-list-contract-all)
135 (define-key km "R" 'semantic-symref-list-rename-open-hits)
136 (define-key km "(" 'semantic-symref-list-create-macro-on-open-hit)
137 (define-key km "E" 'semantic-symref-list-call-macro-on-open-hits)
a4bdf715
CY
138 km)
139 "Keymap used in `semantic-symref-results-mode'.")
140
dd9af436
CY
141(defvar semantic-symref-list-menu-entries
142 (list
143 "Symref"
144 (semantic-menu-item
145 ["Toggle Line Open"
146 semantic-symref-list-toggle-showing
147 :active t
148 :help "Toggle the current line open or closed."
149 ])
150 (semantic-menu-item
151 ["Expand All Entries"
152 semantic-symref-list-expand-all
153 :active t
154 :help "Expand every expandable entry."
155 ])
156 (semantic-menu-item
157 ["Contract All Entries"
158 semantic-symref-list-contract-all
159 :active t
160 :help "Close every expandable entry."
161 ])
162 (semantic-menu-item
163 ["Rename Symbol in Open hits"
164 semantic-symref-list-rename-open-hits
165 :active t
166 :help "Rename the searched for symbol in all hits that are currently open."
167 ])
168 )
169 "Menu entries for the Semantic Symref list mode.")
170
171(defvar semantic-symref-list-menu nil
172 "Menu keymap build from `semantic-symref-results-mode'.")
173
174(easy-menu-define semantic-symref-list-menu
175 semantic-symref-results-mode-map
176 "Symref Mode Menu"
177 semantic-symref-list-menu-entries)
178
179(defcustom semantic-symref-auto-expand-results nil
180 "Non-nil to expand symref results on buffer creation."
181 :group 'semantic-symref
182 :type 'boolean)
183
a4bdf715 184(defcustom semantic-symref-results-mode-hook nil
dd9af436 185 "Hook run when `semantic-symref-results-mode' starts."
a4bdf715
CY
186 :group 'semantic-symref
187 :type 'hook)
188
189(defvar semantic-symref-current-results nil
190 "The current results in a results mode buffer.")
191
192(defun semantic-symref-results-mode (results)
4d789d84 193 ;; FIXME: Use define-derived-mode.
a4bdf715
CY
194 "Major-mode for displaying Semantic Symbol Reference RESULTS.
195RESULTS is an object of class `semantic-symref-results'."
196 (interactive)
197 (kill-all-local-variables)
198 (setq major-mode 'semantic-symref-results-mode
199 mode-name "Symref"
200 )
201 (use-local-map semantic-symref-results-mode-map)
202 (set (make-local-variable 'semantic-symref-current-results)
203 results)
204 (semantic-symref-results-dump results)
205 (goto-char (point-min))
206 (buffer-disable-undo)
207 (set (make-local-variable 'font-lock-global-modes) nil)
208 (font-lock-mode -1)
4d789d84 209 (run-mode-hooks 'semantic-symref-results-mode-hook)
a4bdf715
CY
210 )
211
212(defun semantic-symref-hide-buffer ()
9bf6c65c 213 "Hide buffer with semantic-symref results."
a4bdf715
CY
214 (interactive)
215 (bury-buffer))
216
217(defcustom semantic-symref-results-summary-function 'semantic-format-tag-prototype
218 "*Function to use when creating items in Imenu.
219Some useful functions are found in `semantic-format-tag-functions'."
220 :group 'semantic-symref
221 :type semantic-format-tag-custom-list)
222
223(defun semantic-symref-results-dump (results)
224 "Dump the RESULTS into the current buffer."
225 ;; Get ready for the insert.
d2ce10d2
GM
226 (let ((inhibit-read-only t))
227 (erase-buffer)
228 ;; Insert the contents.
229 (let ((lastfile nil))
230 (dolist (T (oref results :hit-tags))
231 (unless (equal lastfile (semantic-tag-file-name T))
232 (setq lastfile (semantic-tag-file-name T))
233 (insert-button lastfile
234 'mouse-face 'custom-button-pressed-face
235 'action 'semantic-symref-rb-goto-file
236 'tag T)
237 (insert "\n"))
238 (insert " ")
239 (insert-button "[+]"
240 'mouse-face 'highlight
241 'face nil
242 'action 'semantic-symref-rb-toggle-expand-tag
a4bdf715 243 'tag T
d2ce10d2
GM
244 'state 'closed)
245 (insert " ")
246 (insert-button (funcall semantic-symref-results-summary-function
247 T nil t)
248 'mouse-face 'custom-button-pressed-face
249 'face nil
250 'action 'semantic-symref-rb-goto-tag
251 'tag T)
252 (insert "\n")))
253 ;; Auto expand
254 (when semantic-symref-auto-expand-results
255 (semantic-symref-list-expand-all)))
256 ;; Clean up the mess
257 (set-buffer-modified-p nil))
a4bdf715
CY
258
259;;; Commands for semantic-symref-results
260;;
261(defun semantic-symref-list-toggle-showing ()
262 "Toggle showing the contents below the current line."
263 (interactive)
264 (beginning-of-line)
265 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
266 (forward-char -1)
267 (push-button)))
268
269(defun semantic-symref-rb-toggle-expand-tag (&optional button)
270 "Go to the file specified in the symref results buffer.
271BUTTON is the button that was clicked."
272 (interactive)
273 (let* ((tag (button-get button 'tag))
274 (buff (semantic-tag-buffer tag))
275 (hits (semantic--tag-get-property tag :hit))
276 (state (button-get button 'state))
d2ce10d2 277 (text nil))
a4bdf715
CY
278 (cond
279 ((eq state 'closed)
0816d744 280 (with-current-buffer buff
a4bdf715
CY
281 (dolist (H hits)
282 (goto-char (point-min))
283 (forward-line (1- H))
284 (beginning-of-line)
285 (back-to-indentation)
286 (setq text (cons (buffer-substring (point) (point-at-eol)) text)))
d2ce10d2 287 (setq text (nreverse text)))
a4bdf715
CY
288 (goto-char (button-start button))
289 (forward-char 1)
d2ce10d2
GM
290 (let ((inhibit-read-only t))
291 (delete-char 1)
292 (insert "-")
293 (button-put button 'state 'open)
294 (save-excursion
295 (end-of-line)
296 (while text
297 (insert "\n")
298 (insert " ")
299 (insert-button (car text)
300 'mouse-face 'highlight
301 'face nil
302 'action 'semantic-symref-rb-goto-match
303 'tag tag
304 'line (car hits))
305 (setq text (cdr text)
306 hits (cdr hits))))))
a4bdf715 307 ((eq state 'open)
d2ce10d2
GM
308 (let ((inhibit-read-only t))
309 (button-put button 'state 'closed)
310 ;; Delete the various bits.
311 (goto-char (button-start button))
a4bdf715 312 (forward-char 1)
d2ce10d2
GM
313 (delete-char 1)
314 (insert "+")
315 (save-excursion
316 (end-of-line)
317 (forward-char 1)
318 (delete-region (point)
319 (save-excursion
320 (forward-char 1)
321 (forward-line (length hits))
322 (point)))))))))
a4bdf715
CY
323
324(defun semantic-symref-rb-goto-file (&optional button)
325 "Go to the file specified in the symref results buffer.
326BUTTON is the button that was clicked."
327 (let* ((tag (button-get button 'tag))
328 (buff (semantic-tag-buffer tag))
329 (win (selected-window))
330 )
331 (switch-to-buffer-other-window buff)
332 (pulse-momentary-highlight-one-line (point))
1fe1547a 333 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
334 ))
335
336
337(defun semantic-symref-rb-goto-tag (&optional button)
338 "Go to the file specified in the symref results buffer.
339BUTTON is the button that was clicked."
340 (interactive)
341 (let* ((tag (button-get button 'tag))
342 (buff (semantic-tag-buffer tag))
343 (win (selected-window))
344 )
345 (switch-to-buffer-other-window buff)
346 (semantic-go-to-tag tag)
347 (pulse-momentary-highlight-one-line (point))
1fe1547a 348 (when (eq last-command-event ?\s) (select-window win))
a4bdf715
CY
349 )
350 )
351
352(defun semantic-symref-rb-goto-match (&optional button)
353 "Go to the file specified in the symref results buffer.
354BUTTON is the button that was clicked."
355 (interactive)
356 (let* ((tag (button-get button 'tag))
357 (line (button-get button 'line))
358 (buff (semantic-tag-buffer tag))
359 (win (selected-window))
360 )
361 (switch-to-buffer-other-window buff)
dd9af436
CY
362 (goto-char (point-min))
363 (forward-line (1- line))
a4bdf715 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-list-next-line ()
370 "Next line in `semantic-symref-results-mode'."
371 (interactive)
372 (forward-line 1)
373 (back-to-indentation))
374
375(defun semantic-symref-list-prev-line ()
376 "Next line in `semantic-symref-results-mode'."
377 (interactive)
378 (forward-line -1)
379 (back-to-indentation))
380
dd9af436
CY
381(defun semantic-symref-list-expand-all ()
382 "Expand all the nodes in the current buffer."
383 (interactive)
384 (let ((start (make-marker)))
385 (move-marker start (point))
386 (goto-char (point-min))
387 (while (re-search-forward "\\[[+]\\]" nil t)
388 (semantic-symref-list-toggle-showing))
389 ;; Restore position
390 (goto-char start)))
391
392(defun semantic-symref-list-contract-all ()
393 "Expand all the nodes in the current buffer."
394 (interactive)
395 (let ((start (make-marker)))
396 (move-marker start (point))
397 (goto-char (point-min))
398 (while (re-search-forward "\\[[-]\\]" nil t)
399 (semantic-symref-list-toggle-showing))
400 ;; Restore position
401 (goto-char start)))
402
403;;; UTILS
404;;
ee7683eb 405;; List mode utils for understanding the current line
dd9af436
CY
406
407(defun semantic-symref-list-on-hit-p ()
408 "Return the line number if the cursor is on a buffer line with a hit.
409Hits are the line of code from the buffer, not the tag summar or file lines."
410 (save-excursion
411 (end-of-line)
412 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
413 (when ol (semantic-overlay-get ol 'line)))))
414
415
416;;; Keyboard Macros on a Hit
417;;
418;; Record a macro on a hit, and store in a special way for execution later.
419(defun semantic-symref-list-create-macro-on-open-hit ()
420 "Record a keyboard macro at the location of the hit in the current list.
421Under point should be one hit for the active keyword. Move
422cursor to the beginning of that symbol, then record a macro as if
423`kmacro-start-macro' was pressed. Use `kmacro-end-macro',
424{kmacro-end-macro} to end the macro, and return to the symbol found list."
425 (interactive)
426 (let* ((oldsym (oref (oref semantic-symref-current-results
427 :created-by)
428 :searchfor))
429 (ol (save-excursion
430 (end-of-line)
431 (car (semantic-overlays-at (1- (point))))))
432 (tag (when ol (semantic-overlay-get ol 'tag)))
433 (line (when ol (semantic-overlay-get ol 'line))))
434 (when (not line)
435 (error "Cannot create macro on a non-hit line"))
436 ;; Go there, and do something useful.
437 (switch-to-buffer-other-window (semantic-tag-buffer tag))
438 (goto-char (point-min))
439 (forward-line (1- line))
440 (when (not (re-search-forward (regexp-quote oldsym) (point-at-eol) t))
441 (error "Cannot find hit. Cannot record macro"))
442 (goto-char (match-beginning 0))
443 ;; Cursor is now in the right location. Start recording a macro.
444 (kmacro-start-macro nil)
445 ;; Notify the user
446 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
447
448(defun semantic-symref-list-call-macro-on-open-hits ()
449 "Call the most recently created keyboard macro on each hit.
450Cursor is placed at the beginning of the symbol found, even if
451there is more than one symbol on the current line. The
452previously recorded macro is then executed."
453 (interactive)
454 (save-window-excursion
455 (let ((count (semantic-symref-list-map-open-hits
456 (lambda ()
457 (switch-to-buffer (current-buffer))
458 (kmacro-call-macro nil)))))
459 (semantic-symref-list-update-open-hits)
460 (message "Executed Macro %d times." count))))
461
462;;; REFACTORING EDITS
463;;
464;; Utilities and features for refactoring across a list of hits.
465;;
466(defun semantic-symref-list-rename-open-hits (newname)
467 "Rename the discovered symbol references to NEWNAME.
468Only renames the locations that are open in the symref list.
469Closed items will be skipped."
470 (interactive
471 (list (read-string "Rename to: "
472 (oref (oref semantic-symref-current-results
473 :created-by)
474 :searchfor))))
475 (let ((count (semantic-symref-list-map-open-hits
476 (lambda () (replace-match newname nil t)))))
477 (semantic-symref-list-update-open-hits)
c0943d3d 478 (message "Renamed %d occurrences." count)))
dd9af436
CY
479
480;;; REFACTORING UTILITIES
481;;
482;; Refactoring tools want to operate on only the "good" stuff the
483;; user selected.
484(defun semantic-symref-list-map-open-hits (function)
485 "For every open hit in the symref buffer, perform FUNCTION.
486The `match-data' will be set to a successful hit of the searched for symbol.
c0943d3d 487Return the number of occurrences FUNCTION was operated upon."
dd9af436
CY
488
489 ;; First Pass in this function - a straight rename.
490 ;; Second Pass - Allow context specification based on
491 ;; class members. (Not Done)
492
493 (let ((oldsym (oref (oref semantic-symref-current-results
494 :created-by)
495 :searchfor))
496 (count 0))
497 (save-excursion
498 (goto-char (point-min))
499 (while (not (eobp))
500 ;; Is this line a "hit" line?
501 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
502 (tag (when ol (semantic-overlay-get ol 'tag)))
503 (line (when ol (semantic-overlay-get ol 'line))))
504 (when line
505 ;; The "line" means we have an open hit.
506 (with-current-buffer (semantic-tag-buffer tag)
507 (goto-char (point-min))
508 (forward-line (1- line))
509 (beginning-of-line)
510 (while (re-search-forward (regexp-quote oldsym) (point-at-eol) t)
511 (setq count (1+ count))
512 (save-excursion ;; Leave cursor after the matched name.
513 (goto-char (match-beginning 0)) ;; Go to beginning of that sym
514 (funcall function))))))
515 ;; Go to the next line
516 (forward-line 1)
517 (end-of-line)))
518 count))
519
520(defun semantic-symref-list-update-open-hits ()
521 "Update the text for all the open hits in the symref list."
522 (save-excursion
523 (goto-char (point-min))
524 (while (re-search-forward "\\[-\\]" nil t)
525 (end-of-line)
526 (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
527 (tag (when ol (semantic-overlay-get ol 'tag))))
528 ;; If there is a tag, then close/open it.
529 (when tag
530 (semantic-symref-list-toggle-showing)
531 (semantic-symref-list-toggle-showing))))))
532
a4bdf715
CY
533(provide 'semantic/symref/list)
534
1fe1547a
CY
535;; Local variables:
536;; generated-autoload-file: "../loaddefs.el"
1fe1547a
CY
537;; generated-autoload-load-name: "semantic/symref/list"
538;; End:
539
a4bdf715 540;;; semantic/symref/list.el ends here