1 ;;; semantic/symref/list.el --- Symref Output List UI.
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;; Provide a simple user facing API to finding symbol references.
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.
32 ;; NOTE: Need to add some refactoring tools.
34 (require 'semantic/symref)
35 (require 'semantic/complete)
36 (require 'semantic/senator)
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
48 Display the references in `semantic-symref-results-mode'."
51 (let ((ct (semantic-current-tag))
55 (when (not ct) (error "Place cursor inside tag to be searched for"))
57 (when (not (y-or-n-p (format "Find references for %s? " (semantic-tag-name ct))))
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))))
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
71 Display the references in `semantic-symref-results-mode'."
72 (interactive (list (semantic-tag-name (semantic-complete-read-tag-project
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)))
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
89 Display the references in`semantic-symref-results-mode'."
90 (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
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)))
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)))
110 (switch-to-buffer-other-window buff)
112 (semantic-symref-results-mode res))
117 (defgroup semantic-symref-results-mode nil
118 "Symref Results group."
121 (defvar semantic-symref-results-mode-map
122 (let ((km (make-sparse-keymap)))
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)
139 "Keymap used in `semantic-symref-results-mode'.")
141 (defvar semantic-symref-list-menu-entries
146 semantic-symref-list-toggle-showing
148 :help "Toggle the current line open or closed."
151 ["Expand All Entries"
152 semantic-symref-list-expand-all
154 :help "Expand every expandable entry."
157 ["Contract All Entries"
158 semantic-symref-list-contract-all
160 :help "Close every expandable entry."
163 ["Rename Symbol in Open hits"
164 semantic-symref-list-rename-open-hits
166 :help "Rename the searched for symbol in all hits that are currently open."
169 "Menu entries for the Semantic Symref list mode.")
171 (defvar semantic-symref-list-menu nil
172 "Menu keymap build from `semantic-symref-results-mode'.")
174 (easy-menu-define semantic-symref-list-menu
175 semantic-symref-results-mode-map
177 semantic-symref-list-menu-entries)
179 (defcustom semantic-symref-auto-expand-results nil
180 "Non-nil to expand symref results on buffer creation."
181 :group 'semantic-symref
184 (defcustom semantic-symref-results-mode-hook nil
185 "Hook run when `semantic-symref-results-mode' starts."
186 :group 'semantic-symref
189 (defvar semantic-symref-current-results nil
190 "The current results in a results mode buffer.")
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'."
197 (kill-all-local-variables)
198 (setq major-mode 'semantic-symref-results-mode
201 (use-local-map semantic-symref-results-mode-map)
202 (set (make-local-variable 'semantic-symref-current-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)
209 (run-mode-hooks 'semantic-symref-results-mode-hook)
212 (defun semantic-symref-hide-buffer ()
213 "Hide buffer with semantic-symref results."
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)
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))
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
240 'mouse-face 'highlight
242 'action 'semantic-symref-rb-toggle-expand-tag
246 (insert-button (funcall semantic-symref-results-summary-function
248 'mouse-face 'custom-button-pressed-face
250 'action 'semantic-symref-rb-goto-tag
254 (when semantic-symref-auto-expand-results
255 (semantic-symref-list-expand-all)))
257 (set-buffer-modified-p nil))
259 ;;; Commands for semantic-symref-results
261 (defun semantic-symref-list-toggle-showing ()
262 "Toggle showing the contents below the current line."
265 (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
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."
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))
280 (with-current-buffer buff
282 (goto-char (point-min))
283 (forward-line (1- H))
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))
290 (let ((inhibit-read-only t))
293 (button-put button 'state 'open)
299 (insert-button (car text)
300 'mouse-face 'highlight
302 'action 'semantic-symref-rb-goto-match
305 (setq text (cdr text)
308 (let ((inhibit-read-only t))
309 (button-put button 'state 'closed)
310 ;; Delete the various bits.
311 (goto-char (button-start button))
318 (delete-region (point)
321 (forward-line (length hits))
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))
331 (switch-to-buffer-other-window buff)
332 (pulse-momentary-highlight-one-line (point))
333 (when (eq last-command-event ?\s) (select-window win))
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."
341 (let* ((tag (button-get button 'tag))
342 (buff (semantic-tag-buffer tag))
343 (win (selected-window))
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))
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."
356 (let* ((tag (button-get button 'tag))
357 (line (button-get button 'line))
358 (buff (semantic-tag-buffer tag))
359 (win (selected-window))
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))
369 (defun semantic-symref-list-next-line ()
370 "Next line in `semantic-symref-results-mode'."
373 (back-to-indentation))
375 (defun semantic-symref-list-prev-line ()
376 "Next line in `semantic-symref-results-mode'."
379 (back-to-indentation))
381 (defun semantic-symref-list-expand-all ()
382 "Expand all the nodes in the current buffer."
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))
392 (defun semantic-symref-list-contract-all ()
393 "Expand all the nodes in the current buffer."
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))
405 ;; List mode utils for understanding the current line
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."
412 (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
413 (when ol (semantic-overlay-get ol 'line)))))
416 ;;; Keyboard Macros on a Hit
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."
426 (let* ((oldsym (oref (oref semantic-symref-current-results
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))))
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)
446 (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
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."
454 (save-window-excursion
455 (let ((count (semantic-symref-list-map-open-hits
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))))
462 ;;; REFACTORING EDITS
464 ;; Utilities and features for refactoring across a list of hits.
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."
471 (list (read-string "Rename to: "
472 (oref (oref semantic-symref-current-results
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)))
480 ;;; REFACTORING UTILITIES
482 ;; Refactoring tools want to operate on only the "good" stuff the
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."
489 ;; First Pass in this function - a straight rename.
490 ;; Second Pass - Allow context specification based on
491 ;; class members. (Not Done)
493 (let ((oldsym (oref (oref semantic-symref-current-results
498 (goto-char (point-min))
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))))
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))
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
520 (defun semantic-symref-list-update-open-hits ()
521 "Update the text for all the open hits in the symref list."
523 (goto-char (point-min))
524 (while (re-search-forward "\\[-\\]" nil t)
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.
530 (semantic-symref-list-toggle-showing)
531 (semantic-symref-list-toggle-showing))))))
533 (provide 'semantic/symref/list)
536 ;; generated-autoload-file: "../loaddefs.el"
537 ;; generated-autoload-load-name: "semantic/symref/list"
540 ;;; semantic/symref/list.el ends here