list.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. ;;; semantic/symref/list.el --- Symref Output List UI.
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Provide a simple user facing API to finding symbol references.
  18. ;;
  19. ;; This UI is the base of some refactoring tools. For any refactor,
  20. ;; the user will execute `semantic-symref' in a tag.
  21. ;; Once that data is collected, the output will be listed in a buffer.
  22. ;; In the output buffer, the user can then initiate different
  23. ;; refactoring operations.
  24. ;;
  25. ;; NOTE: Need to add some refactoring tools.
  26. (require 'semantic/symref)
  27. (require 'semantic/complete)
  28. (require 'semantic/senator)
  29. (require 'pulse)
  30. ;;; Code:
  31. ;;;###autoload
  32. (defun semantic-symref ()
  33. "Find references to the current tag.
  34. This command uses the currently configured references tool within the
  35. current project to find references to the current tag. The
  36. references are organized by file and the name of the function
  37. they are used in.
  38. Display the references in `semantic-symref-results-mode'."
  39. (interactive)
  40. (semantic-fetch-tags)
  41. (let ((ct (semantic-current-tag))
  42. (res nil)
  43. )
  44. ;; Must have a tag...
  45. (when (not ct) (error "Place cursor inside tag to be searched for"))
  46. ;; Check w/ user.
  47. (when (not (y-or-n-p (format "Find references for %s? " (semantic-tag-name ct))))
  48. (error "Quit"))
  49. ;; Gather results and tags
  50. (message "Gathering References...")
  51. (setq res (semantic-symref-find-references-by-name (semantic-tag-name ct)))
  52. (semantic-symref-produce-list-on-results res (semantic-tag-name ct))))
  53. ;;;###autoload
  54. (defun semantic-symref-symbol (sym)
  55. "Find references to the symbol SYM.
  56. This command uses the currently configured references tool within the
  57. current project to find references to the input SYM. The
  58. references are organized by file and the name of the function
  59. they are used in.
  60. Display the references in `semantic-symref-results-mode'."
  61. (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
  62. "Symrefs for: "))))
  63. (semantic-fetch-tags)
  64. (let ((res nil)
  65. )
  66. ;; Gather results and tags
  67. (message "Gathering References...")
  68. (setq res (semantic-symref-find-references-by-name sym))
  69. (semantic-symref-produce-list-on-results res sym)))
  70. ;;;###autoload
  71. (defun semantic-symref-regexp (sym)
  72. "Find references to the a symbol regexp SYM.
  73. This command uses the currently configured references tool within the
  74. current project to find references to the input SYM. The
  75. references are the organized by file and the name of the function
  76. they are used in.
  77. Display the references in`semantic-symref-results-mode'."
  78. (interactive (list (semantic-tag-name (semantic-complete-read-tag-buffer-deep
  79. "Symrefs for: "))))
  80. (semantic-fetch-tags)
  81. (let ((res nil)
  82. )
  83. ;; Gather results and tags
  84. (message "Gathering References...")
  85. (setq res (semantic-symref-find-text sym))
  86. (semantic-symref-produce-list-on-results res sym)))
  87. (defun semantic-symref-produce-list-on-results (res str)
  88. "Produce a symref list mode buffer on the results RES."
  89. (when (not res) (error "No references found"))
  90. (semantic-symref-result-get-tags res t)
  91. (message "Gathering References...done")
  92. ;; Build a references buffer.
  93. (let ((buff (get-buffer-create
  94. (format "*Symref %s" str)))
  95. )
  96. (switch-to-buffer-other-window buff)
  97. (set-buffer buff)
  98. (semantic-symref-results-mode res))
  99. )
  100. ;;; RESULTS MODE
  101. ;;
  102. (defgroup semantic-symref-results-mode nil
  103. "Symref Results group."
  104. :group 'semantic)
  105. (defvar semantic-symref-results-mode-map
  106. (let ((km (make-sparse-keymap)))
  107. (define-key km "\C-i" 'forward-button)
  108. (define-key km "\M-C-i" 'backward-button)
  109. (define-key km " " 'push-button)
  110. (define-key km "-" 'semantic-symref-list-toggle-showing)
  111. (define-key km "=" 'semantic-symref-list-toggle-showing)
  112. (define-key km "+" 'semantic-symref-list-toggle-showing)
  113. (define-key km "n" 'semantic-symref-list-next-line)
  114. (define-key km "p" 'semantic-symref-list-prev-line)
  115. (define-key km "q" 'semantic-symref-hide-buffer)
  116. (define-key km "\C-c\C-e" 'semantic-symref-list-expand-all)
  117. (define-key km "\C-c\C-r" 'semantic-symref-list-contract-all)
  118. (define-key km "R" 'semantic-symref-list-rename-open-hits)
  119. (define-key km "(" 'semantic-symref-list-create-macro-on-open-hit)
  120. (define-key km "E" 'semantic-symref-list-call-macro-on-open-hits)
  121. km)
  122. "Keymap used in `semantic-symref-results-mode'.")
  123. (defvar semantic-symref-list-menu-entries
  124. (list
  125. "Symref"
  126. (semantic-menu-item
  127. ["Toggle Line Open"
  128. semantic-symref-list-toggle-showing
  129. :active t
  130. :help "Toggle the current line open or closed."
  131. ])
  132. (semantic-menu-item
  133. ["Expand All Entries"
  134. semantic-symref-list-expand-all
  135. :active t
  136. :help "Expand every expandable entry."
  137. ])
  138. (semantic-menu-item
  139. ["Contract All Entries"
  140. semantic-symref-list-contract-all
  141. :active t
  142. :help "Close every expandable entry."
  143. ])
  144. (semantic-menu-item
  145. ["Rename Symbol in Open hits"
  146. semantic-symref-list-rename-open-hits
  147. :active t
  148. :help "Rename the searched for symbol in all hits that are currently open."
  149. ])
  150. )
  151. "Menu entries for the Semantic Symref list mode.")
  152. (defvar semantic-symref-list-menu nil
  153. "Menu keymap build from `semantic-symref-results-mode'.")
  154. (easy-menu-define semantic-symref-list-menu
  155. semantic-symref-results-mode-map
  156. "Symref Mode Menu"
  157. semantic-symref-list-menu-entries)
  158. (defcustom semantic-symref-auto-expand-results nil
  159. "Non-nil to expand symref results on buffer creation."
  160. :group 'semantic-symref
  161. :type 'boolean)
  162. (defcustom semantic-symref-results-mode-hook nil
  163. "Hook run when `semantic-symref-results-mode' starts."
  164. :group 'semantic-symref
  165. :type 'hook)
  166. (defvar semantic-symref-current-results nil
  167. "The current results in a results mode buffer.")
  168. (defun semantic-symref-results-mode (results)
  169. ;; FIXME: Use define-derived-mode.
  170. "Major-mode for displaying Semantic Symbol Reference RESULTS.
  171. RESULTS is an object of class `semantic-symref-results'."
  172. (interactive)
  173. (kill-all-local-variables)
  174. (setq major-mode 'semantic-symref-results-mode
  175. mode-name "Symref"
  176. )
  177. (use-local-map semantic-symref-results-mode-map)
  178. (set (make-local-variable 'semantic-symref-current-results)
  179. results)
  180. (semantic-symref-results-dump results)
  181. (goto-char (point-min))
  182. (buffer-disable-undo)
  183. (set (make-local-variable 'font-lock-global-modes) nil)
  184. (font-lock-mode -1)
  185. (run-mode-hooks 'semantic-symref-results-mode-hook)
  186. )
  187. (defun semantic-symref-hide-buffer ()
  188. "Hide buffer with semantic-symref results."
  189. (interactive)
  190. (bury-buffer))
  191. (defcustom semantic-symref-results-summary-function 'semantic-format-tag-prototype
  192. "*Function to use when creating items in Imenu.
  193. Some useful functions are found in `semantic-format-tag-functions'."
  194. :group 'semantic-symref
  195. :type semantic-format-tag-custom-list)
  196. (defun semantic-symref-results-dump (results)
  197. "Dump the RESULTS into the current buffer."
  198. ;; Get ready for the insert.
  199. (let ((inhibit-read-only t))
  200. (erase-buffer)
  201. ;; Insert the contents.
  202. (let ((lastfile nil))
  203. (dolist (T (oref results :hit-tags))
  204. (unless (equal lastfile (semantic-tag-file-name T))
  205. (setq lastfile (semantic-tag-file-name T))
  206. (insert-button lastfile
  207. 'mouse-face 'custom-button-pressed-face
  208. 'action 'semantic-symref-rb-goto-file
  209. 'tag T)
  210. (insert "\n"))
  211. (insert " ")
  212. (insert-button "[+]"
  213. 'mouse-face 'highlight
  214. 'face nil
  215. 'action 'semantic-symref-rb-toggle-expand-tag
  216. 'tag T
  217. 'state 'closed)
  218. (insert " ")
  219. (insert-button (funcall semantic-symref-results-summary-function
  220. T nil t)
  221. 'mouse-face 'custom-button-pressed-face
  222. 'face nil
  223. 'action 'semantic-symref-rb-goto-tag
  224. 'tag T)
  225. (insert "\n")))
  226. ;; Auto expand
  227. (when semantic-symref-auto-expand-results
  228. (semantic-symref-list-expand-all)))
  229. ;; Clean up the mess
  230. (set-buffer-modified-p nil))
  231. ;;; Commands for semantic-symref-results
  232. ;;
  233. (defun semantic-symref-list-toggle-showing ()
  234. "Toggle showing the contents below the current line."
  235. (interactive)
  236. (beginning-of-line)
  237. (when (re-search-forward "\\[[-+]\\]" (point-at-eol) t)
  238. (forward-char -1)
  239. (push-button)))
  240. (defun semantic-symref-rb-toggle-expand-tag (&optional button)
  241. "Go to the file specified in the symref results buffer.
  242. BUTTON is the button that was clicked."
  243. (interactive)
  244. (let* ((tag (button-get button 'tag))
  245. (buff (semantic-tag-buffer tag))
  246. (hits (semantic--tag-get-property tag :hit))
  247. (state (button-get button 'state))
  248. (text nil))
  249. (cond
  250. ((eq state 'closed)
  251. (with-current-buffer buff
  252. (dolist (H hits)
  253. (goto-char (point-min))
  254. (forward-line (1- H))
  255. (beginning-of-line)
  256. (back-to-indentation)
  257. (setq text (cons (buffer-substring (point) (point-at-eol)) text)))
  258. (setq text (nreverse text)))
  259. (goto-char (button-start button))
  260. (forward-char 1)
  261. (let ((inhibit-read-only t))
  262. (delete-char 1)
  263. (insert "-")
  264. (button-put button 'state 'open)
  265. (save-excursion
  266. (end-of-line)
  267. (while text
  268. (insert "\n")
  269. (insert " ")
  270. (insert-button (car text)
  271. 'mouse-face 'highlight
  272. 'face nil
  273. 'action 'semantic-symref-rb-goto-match
  274. 'tag tag
  275. 'line (car hits))
  276. (setq text (cdr text)
  277. hits (cdr hits))))))
  278. ((eq state 'open)
  279. (let ((inhibit-read-only t))
  280. (button-put button 'state 'closed)
  281. ;; Delete the various bits.
  282. (goto-char (button-start button))
  283. (forward-char 1)
  284. (delete-char 1)
  285. (insert "+")
  286. (save-excursion
  287. (end-of-line)
  288. (forward-char 1)
  289. (delete-region (point)
  290. (save-excursion
  291. (forward-char 1)
  292. (forward-line (length hits))
  293. (point)))))))))
  294. (defun semantic-symref-rb-goto-file (&optional button)
  295. "Go to the file specified in the symref results buffer.
  296. BUTTON is the button that was clicked."
  297. (let* ((tag (button-get button 'tag))
  298. (buff (semantic-tag-buffer tag))
  299. (win (selected-window))
  300. )
  301. (switch-to-buffer-other-window buff)
  302. (pulse-momentary-highlight-one-line (point))
  303. (when (eq last-command-event ?\s) (select-window win))
  304. ))
  305. (defun semantic-symref-rb-goto-tag (&optional button)
  306. "Go to the file specified in the symref results buffer.
  307. BUTTON is the button that was clicked."
  308. (interactive)
  309. (let* ((tag (button-get button 'tag))
  310. (buff (semantic-tag-buffer tag))
  311. (win (selected-window))
  312. )
  313. (switch-to-buffer-other-window buff)
  314. (semantic-go-to-tag tag)
  315. (pulse-momentary-highlight-one-line (point))
  316. (when (eq last-command-event ?\s) (select-window win))
  317. )
  318. )
  319. (defun semantic-symref-rb-goto-match (&optional button)
  320. "Go to the file specified in the symref results buffer.
  321. BUTTON is the button that was clicked."
  322. (interactive)
  323. (let* ((tag (button-get button 'tag))
  324. (line (button-get button 'line))
  325. (buff (semantic-tag-buffer tag))
  326. (win (selected-window))
  327. )
  328. (switch-to-buffer-other-window buff)
  329. (goto-char (point-min))
  330. (forward-line (1- line))
  331. (pulse-momentary-highlight-one-line (point))
  332. (when (eq last-command-event ?\s) (select-window win))
  333. )
  334. )
  335. (defun semantic-symref-list-next-line ()
  336. "Next line in `semantic-symref-results-mode'."
  337. (interactive)
  338. (forward-line 1)
  339. (back-to-indentation))
  340. (defun semantic-symref-list-prev-line ()
  341. "Next line in `semantic-symref-results-mode'."
  342. (interactive)
  343. (forward-line -1)
  344. (back-to-indentation))
  345. (defun semantic-symref-list-expand-all ()
  346. "Expand all the nodes in the current buffer."
  347. (interactive)
  348. (let ((start (make-marker)))
  349. (move-marker start (point))
  350. (goto-char (point-min))
  351. (while (re-search-forward "\\[[+]\\]" nil t)
  352. (semantic-symref-list-toggle-showing))
  353. ;; Restore position
  354. (goto-char start)))
  355. (defun semantic-symref-list-contract-all ()
  356. "Expand all the nodes in the current buffer."
  357. (interactive)
  358. (let ((start (make-marker)))
  359. (move-marker start (point))
  360. (goto-char (point-min))
  361. (while (re-search-forward "\\[[-]\\]" nil t)
  362. (semantic-symref-list-toggle-showing))
  363. ;; Restore position
  364. (goto-char start)))
  365. ;;; UTILS
  366. ;;
  367. ;; List mode utils for understanding the current line
  368. (defun semantic-symref-list-on-hit-p ()
  369. "Return the line number if the cursor is on a buffer line with a hit.
  370. Hits are the line of code from the buffer, not the tag summar or file lines."
  371. (save-excursion
  372. (end-of-line)
  373. (let* ((ol (car (semantic-overlays-at (1- (point)))))) ;; trust this for now
  374. (when ol (semantic-overlay-get ol 'line)))))
  375. ;;; Keyboard Macros on a Hit
  376. ;;
  377. ;; Record a macro on a hit, and store in a special way for execution later.
  378. (defun semantic-symref-list-create-macro-on-open-hit ()
  379. "Record a keyboard macro at the location of the hit in the current list.
  380. Under point should be one hit for the active keyword. Move
  381. cursor to the beginning of that symbol, then record a macro as if
  382. `kmacro-start-macro' was pressed. Use `kmacro-end-macro',
  383. {kmacro-end-macro} to end the macro, and return to the symbol found list."
  384. (interactive)
  385. (let* ((oldsym (oref (oref semantic-symref-current-results
  386. :created-by)
  387. :searchfor))
  388. (ol (save-excursion
  389. (end-of-line)
  390. (car (semantic-overlays-at (1- (point))))))
  391. (tag (when ol (semantic-overlay-get ol 'tag)))
  392. (line (when ol (semantic-overlay-get ol 'line))))
  393. (when (not line)
  394. (error "Cannot create macro on a non-hit line"))
  395. ;; Go there, and do something useful.
  396. (switch-to-buffer-other-window (semantic-tag-buffer tag))
  397. (goto-char (point-min))
  398. (forward-line (1- line))
  399. (when (not (re-search-forward (regexp-quote oldsym) (point-at-eol) t))
  400. (error "Cannot find hit. Cannot record macro"))
  401. (goto-char (match-beginning 0))
  402. ;; Cursor is now in the right location. Start recording a macro.
  403. (kmacro-start-macro nil)
  404. ;; Notify the user
  405. (message "Complete with C-x ). Use E in the symref buffer to call this macro.")))
  406. (defun semantic-symref-list-call-macro-on-open-hits ()
  407. "Call the most recently created keyboard macro on each hit.
  408. Cursor is placed at the beginning of the symbol found, even if
  409. there is more than one symbol on the current line. The
  410. previously recorded macro is then executed."
  411. (interactive)
  412. (save-window-excursion
  413. (let ((count (semantic-symref-list-map-open-hits
  414. (lambda ()
  415. (switch-to-buffer (current-buffer))
  416. (kmacro-call-macro nil)))))
  417. (semantic-symref-list-update-open-hits)
  418. (message "Executed Macro %d times." count))))
  419. ;;; REFACTORING EDITS
  420. ;;
  421. ;; Utilities and features for refactoring across a list of hits.
  422. ;;
  423. (defun semantic-symref-list-rename-open-hits (newname)
  424. "Rename the discovered symbol references to NEWNAME.
  425. Only renames the locations that are open in the symref list.
  426. Closed items will be skipped."
  427. (interactive
  428. (list (read-string "Rename to: "
  429. (oref (oref semantic-symref-current-results
  430. :created-by)
  431. :searchfor))))
  432. (let ((count (semantic-symref-list-map-open-hits
  433. (lambda () (replace-match newname nil t)))))
  434. (semantic-symref-list-update-open-hits)
  435. (message "Renamed %d occurrences." count)))
  436. ;;; REFACTORING UTILITIES
  437. ;;
  438. ;; Refactoring tools want to operate on only the "good" stuff the
  439. ;; user selected.
  440. (defun semantic-symref-list-map-open-hits (function)
  441. "For every open hit in the symref buffer, perform FUNCTION.
  442. The `match-data' will be set to a successful hit of the searched for symbol.
  443. Return the number of occurrences FUNCTION was operated upon."
  444. ;; First Pass in this function - a straight rename.
  445. ;; Second Pass - Allow context specification based on
  446. ;; class members. (Not Done)
  447. (let ((oldsym (oref (oref semantic-symref-current-results
  448. :created-by)
  449. :searchfor))
  450. (count 0))
  451. (save-excursion
  452. (goto-char (point-min))
  453. (while (not (eobp))
  454. ;; Is this line a "hit" line?
  455. (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
  456. (tag (when ol (semantic-overlay-get ol 'tag)))
  457. (line (when ol (semantic-overlay-get ol 'line))))
  458. (when line
  459. ;; The "line" means we have an open hit.
  460. (with-current-buffer (semantic-tag-buffer tag)
  461. (goto-char (point-min))
  462. (forward-line (1- line))
  463. (beginning-of-line)
  464. (while (re-search-forward (regexp-quote oldsym) (point-at-eol) t)
  465. (setq count (1+ count))
  466. (save-excursion ;; Leave cursor after the matched name.
  467. (goto-char (match-beginning 0)) ;; Go to beginning of that sym
  468. (funcall function))))))
  469. ;; Go to the next line
  470. (forward-line 1)
  471. (end-of-line)))
  472. count))
  473. (defun semantic-symref-list-update-open-hits ()
  474. "Update the text for all the open hits in the symref list."
  475. (save-excursion
  476. (goto-char (point-min))
  477. (while (re-search-forward "\\[-\\]" nil t)
  478. (end-of-line)
  479. (let* ((ol (car (semantic-overlays-at (1- (point))))) ;; trust this for now
  480. (tag (when ol (semantic-overlay-get ol 'tag))))
  481. ;; If there is a tag, then close/open it.
  482. (when tag
  483. (semantic-symref-list-toggle-showing)
  484. (semantic-symref-list-toggle-showing))))))
  485. (provide 'semantic/symref/list)
  486. ;; Local variables:
  487. ;; generated-autoload-file: "../loaddefs.el"
  488. ;; generated-autoload-load-name: "semantic/symref/list"
  489. ;; End:
  490. ;;; semantic/symref/list.el ends here