]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/symref/list.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / cedet / semantic / symref / list.el
1 ;;; semantic/symref/list.el --- Symref Output List UI.
2
3 ;; Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Provide a simple user facing API to finding symbol references.
25 ;;
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.
31 ;;
32 ;; NOTE: Need to add some refactoring tools.
33
34 (require 'semantic/symref)
35 (require 'semantic/complete)
36 (require 'semantic/senator)
37 (require 'pulse)
38
39 ;;; Code:
40
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)
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
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)
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.
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)
94 )
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)))
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)
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)
137 km)
138 "Keymap used in `semantic-symref-results-mode'.")
139
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
183 (defcustom semantic-symref-results-mode-hook nil
184 "Hook run when `semantic-symref-results-mode' starts."
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.
193 RESULTS 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 ()
211 "Hide buffer with semantic-symref results."
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.
217 Some 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 (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
241 'tag T
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))
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.
269 BUTTON 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))
275 (text nil))
276 (cond
277 ((eq state 'closed)
278 (with-current-buffer buff
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)))
285 (setq text (nreverse text)))
286 (goto-char (button-start button))
287 (forward-char 1)
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))))))
305 ((eq state 'open)
306 (let ((inhibit-read-only t))
307 (button-put button 'state 'closed)
308 ;; Delete the various bits.
309 (goto-char (button-start button))
310 (forward-char 1)
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)))))))))
321
322 (defun semantic-symref-rb-goto-file (&optional button)
323 "Go to the file specified in the symref results buffer.
324 BUTTON 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))
331 (when (eq last-command-event ?\s) (select-window win))
332 ))
333
334
335 (defun semantic-symref-rb-goto-tag (&optional button)
336 "Go to the file specified in the symref results buffer.
337 BUTTON 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))
346 (when (eq last-command-event ?\s) (select-window win))
347 )
348 )
349
350 (defun semantic-symref-rb-goto-match (&optional button)
351 "Go to the file specified in the symref results buffer.
352 BUTTON 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)
360 (goto-char (point-min))
361 (forward-line (1- line))
362 (pulse-momentary-highlight-one-line (point))
363 (when (eq last-command-event ?\s) (select-window win))
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
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.
407 Hits 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.
419 Under point should be one hit for the active keyword. Move
420 cursor 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.
448 Cursor is placed at the beginning of the symbol found, even if
449 there is more than one symbol on the current line. The
450 previously 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.
466 Only renames the locations that are open in the symref list.
467 Closed 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)
476 (message "Renamed %d occurrences." count)))
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.
484 The `match-data' will be set to a successful hit of the searched for symbol.
485 Return the number of occurrences FUNCTION was operated upon."
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
531 (provide 'semantic/symref/list)
532
533 ;; Local variables:
534 ;; generated-autoload-file: "../loaddefs.el"
535 ;; generated-autoload-load-name: "semantic/symref/list"
536 ;; End:
537
538 ;;; semantic/symref/list.el ends here