]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/symref/list.el
729bd8e153c7a9743610196569d9f9020426e4a8
[gnu-emacs] / lisp / cedet / semantic / symref / list.el
1 ;;; semantic/symref/list.el --- Symref Output List UI.
2
3 ;; Copyright (C) 2008-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 execute `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-project
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 references 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 (suppress-keymap km)
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)
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)
138 km)
139 "Keymap used in `semantic-symref-results-mode'.")
140
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
184 (defcustom semantic-symref-results-mode-hook nil
185 "Hook run when `semantic-symref-results-mode' starts."
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)
193 ;; FIXME: Use define-derived-mode.
194 "Major-mode for displaying Semantic Symbol Reference RESULTS.
195 RESULTS 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)
209 (run-mode-hooks 'semantic-symref-results-mode-hook)
210 )
211
212 (defun semantic-symref-hide-buffer ()
213 "Hide buffer with semantic-symref results."
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.
219 Some 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.
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
243 'tag T
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))
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.
271 BUTTON 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))
277 (text nil))
278 (cond
279 ((eq state 'closed)
280 (with-current-buffer buff
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)))
287 (setq text (nreverse text)))
288 (goto-char (button-start button))
289 (forward-char 1)
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))))))
307 ((eq state 'open)
308 (let ((inhibit-read-only t))
309 (button-put button 'state 'closed)
310 ;; Delete the various bits.
311 (goto-char (button-start button))
312 (forward-char 1)
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)))))))))
323
324 (defun semantic-symref-rb-goto-file (&optional button)
325 "Go to the file specified in the symref results buffer.
326 BUTTON 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))
333 (when (eq last-command-event ?\s) (select-window win))
334 ))
335
336
337 (defun semantic-symref-rb-goto-tag (&optional button)
338 "Go to the file specified in the symref results buffer.
339 BUTTON 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))
348 (when (eq last-command-event ?\s) (select-window win))
349 )
350 )
351
352 (defun semantic-symref-rb-goto-match (&optional button)
353 "Go to the file specified in the symref results buffer.
354 BUTTON 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)
362 (goto-char (point-min))
363 (forward-line (1- line))
364 (pulse-momentary-highlight-one-line (point))
365 (when (eq last-command-event ?\s) (select-window win))
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
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 ;;
405 ;; List mode utils for understanding the current line
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.
409 Hits 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.
421 Under point should be one hit for the active keyword. Move
422 cursor 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.
450 Cursor is placed at the beginning of the symbol found, even if
451 there is more than one symbol on the current line. The
452 previously 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.
468 Only renames the locations that are open in the symref list.
469 Closed 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)
478 (message "Renamed %d occurrences." count)))
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.
486 The `match-data' will be set to a successful hit of the searched for symbol.
487 Return the number of occurrences FUNCTION was operated upon."
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
533 (provide 'semantic/symref/list)
534
535 ;; Local variables:
536 ;; generated-autoload-file: "../loaddefs.el"
537 ;; generated-autoload-load-name: "semantic/symref/list"
538 ;; End:
539
540 ;;; semantic/symref/list.el ends here