senator.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. ;;; semantic/senator.el --- SEmantic NAvigaTOR
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: David Ponce <david@dponce.com>
  4. ;; Maintainer: FSF
  5. ;; Created: 10 Nov 2000
  6. ;; Keywords: syntax
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This file defines some user commands for navigating between
  21. ;; Semantic tags. This is a subset of the version of senator.el in
  22. ;; the upstream CEDET package; the rest is incorporated into other
  23. ;; parts of Semantic or Emacs.
  24. ;;; Code:
  25. (require 'ring)
  26. (require 'semantic)
  27. (require 'semantic/ctxt)
  28. (require 'semantic/decorate)
  29. (require 'semantic/format)
  30. (eval-when-compile (require 'semantic/find))
  31. ;; (eval-when-compile (require 'hippie-exp))
  32. (declare-function semantic-analyze-tag-references "semantic/analyze/refs")
  33. (declare-function semantic-analyze-refs-impl "semantic/analyze/refs")
  34. (declare-function semantic-analyze-find-tag "semantic/analyze")
  35. (declare-function semantic-analyze-tag-type "semantic/analyze/fcn")
  36. (declare-function semantic-tag-external-class "semantic/sort")
  37. (declare-function imenu--mouse-menu "imenu")
  38. ;;; Customization
  39. (defgroup senator nil
  40. "Semantic Navigator."
  41. :group 'semantic)
  42. ;;;###autoload
  43. (defcustom senator-step-at-tag-classes nil
  44. "List of tag classes recognized by Senator's navigation commands.
  45. A tag class is a symbol, such as `variable', `function', or `type'.
  46. As a special exception, if the value is nil, Senator's navigation
  47. commands recognize all tag classes."
  48. :group 'senator
  49. :type '(repeat (symbol)))
  50. ;;;###autoload
  51. (make-variable-buffer-local 'senator-step-at-tag-classes)
  52. ;;;###autoload
  53. (defcustom senator-step-at-start-end-tag-classes nil
  54. "List of tag classes at which Senator's navigation commands should stop.
  55. A tag class is a symbol, such as `variable', `function', or `type'.
  56. The navigation commands stop at the start and end of each tag
  57. class in this list, provided the tag class is recognized (see
  58. `senator-step-at-tag-classes').
  59. As a special exception, if the value is nil, the navigation
  60. commands stop at the beginning of every tag.
  61. If t, the navigation commands stop at the start and end of any
  62. tag, where possible."
  63. :group 'senator
  64. :type '(choice :tag "Identifiers"
  65. (repeat :menu-tag "Symbols" (symbol))
  66. (const :tag "All" t)))
  67. ;;;###autoload
  68. (make-variable-buffer-local 'senator-step-at-start-end-tag-classes)
  69. (defcustom senator-highlight-found nil
  70. "If non-nil, Senator commands momentarily highlight found tags."
  71. :group 'senator
  72. :type 'boolean)
  73. (make-variable-buffer-local 'senator-highlight-found)
  74. ;;; Faces
  75. (defface senator-momentary-highlight-face
  76. '((((class color) (background dark))
  77. (:background "gray30"))
  78. (((class color) (background light))
  79. (:background "gray70")))
  80. "Face used to momentarily highlight tags."
  81. :group 'semantic-faces)
  82. ;;; Common functions
  83. (defun senator-momentary-highlight-tag (tag)
  84. "Momentarily highlight TAG.
  85. Does nothing if `senator-highlight-found' is nil."
  86. (and senator-highlight-found
  87. (semantic-momentary-highlight-tag
  88. tag 'senator-momentary-highlight-face)))
  89. (defun senator-step-at-start-end-p (tag)
  90. "Return non-nil if must step at start and end of TAG."
  91. (and tag
  92. (or (eq senator-step-at-start-end-tag-classes t)
  93. (memq (semantic-tag-class tag)
  94. senator-step-at-start-end-tag-classes))))
  95. (defun senator-skip-p (tag)
  96. "Return non-nil if must skip TAG."
  97. (and tag
  98. senator-step-at-tag-classes
  99. (not (memq (semantic-tag-class tag)
  100. senator-step-at-tag-classes))))
  101. (defun senator-middle-of-tag-p (pos tag)
  102. "Return non-nil if POS is between start and end of TAG."
  103. (and (> pos (semantic-tag-start tag))
  104. (< pos (semantic-tag-end tag))))
  105. (defun senator-step-at-parent (tag)
  106. "Return TAG's outermost parent if must step at start/end of it.
  107. Return nil otherwise."
  108. (if tag
  109. (let (parent parents)
  110. (setq parents (semantic-find-tag-by-overlay
  111. (semantic-tag-start tag)))
  112. (while (and parents (not parent))
  113. (setq parent (car parents)
  114. parents (cdr parents))
  115. (if (or (eq tag parent)
  116. (senator-skip-p parent)
  117. (not (senator-step-at-start-end-p parent)))
  118. (setq parent nil)))
  119. parent)))
  120. (defun senator-previous-tag-or-parent (pos)
  121. "Return the tag before POS or one of its parent where to step."
  122. (let (ol tag)
  123. (while (and pos (> pos (point-min)) (not tag))
  124. (setq pos (semantic-overlay-previous-change pos))
  125. (when pos
  126. ;; Get overlays at position
  127. (setq ol (semantic-overlays-at pos))
  128. ;; find the overlay that belongs to semantic
  129. ;; and STARTS or ENDS at the found position.
  130. (while (and ol (not tag))
  131. (setq tag (semantic-overlay-get (car ol) 'semantic))
  132. (unless (and tag (semantic-tag-p tag)
  133. (or (= (semantic-tag-start tag) pos)
  134. (= (semantic-tag-end tag) pos)))
  135. (setq tag nil
  136. ol (cdr ol))))))
  137. (or (senator-step-at-parent tag) tag)))
  138. ;;; Search functions
  139. (defun senator-search-tag-name (tag)
  140. "Search for TAG name in current buffer.
  141. Limit the search to TAG bounds.
  142. If found, set point to the end of the name, and return point. The
  143. beginning of the name is at (match-beginning 0).
  144. Return nil if not found, that is if TAG name doesn't come from the
  145. source."
  146. (let ((name (semantic-tag-name tag)))
  147. (setq name (if (string-match "\\`\\([^[]+\\)[[]" name)
  148. (match-string 1 name)
  149. name))
  150. (goto-char (semantic-tag-start tag))
  151. (when (re-search-forward (concat
  152. ;; The tag name is expected to be
  153. ;; between word delimiters, whitespace,
  154. ;; or punctuation.
  155. "\\(\\<\\|\\s-+\\|\\s.\\)"
  156. (regexp-quote name)
  157. "\\(\\>\\|\\s-+\\|\\s.\\)")
  158. (semantic-tag-end tag)
  159. t)
  160. (goto-char (match-beginning 0))
  161. (search-forward name))))
  162. (defcustom senator-search-ignore-tag-classes
  163. '(code block)
  164. "List of ignored tag classes.
  165. Tags of those classes are excluded from search."
  166. :group 'senator
  167. :type '(repeat (symbol :tag "class")))
  168. (defun senator-search-default-tag-filter (tag)
  169. "Default function that filters searched tags.
  170. Ignore tags of classes in `senator-search-ignore-tag-classes'"
  171. (not (memq (semantic-tag-class tag)
  172. senator-search-ignore-tag-classes)))
  173. (defvar senator-search-tag-filter-functions
  174. '(senator-search-default-tag-filter)
  175. "List of functions to be called to filter searched tags.
  176. Each function is passed a tag. If one of them returns nil, the tag is
  177. excluded from the search.")
  178. (defun senator-search (searcher text &optional bound noerror count)
  179. "Use the SEARCHER function to search from point for TEXT in a tag name.
  180. SEARCHER is typically the function `search-forward', `search-backward',
  181. `word-search-forward', `word-search-backward', `re-search-forward', or
  182. `re-search-backward'. See one of the above function to see how the
  183. TEXT, BOUND, NOERROR, and COUNT arguments are interpreted."
  184. (let* ((origin (point))
  185. (count (or count 1))
  186. (step (cond ((> count 0) 1)
  187. ((< count 0) (setq count (- count)) -1)
  188. (0)))
  189. found next sstart send tag tstart tend)
  190. (or (zerop step)
  191. (while (and (not found)
  192. (setq next (funcall searcher text bound t step)))
  193. (setq sstart (match-beginning 0)
  194. send (match-end 0))
  195. (if (= sstart send)
  196. (setq found t)
  197. (and (setq tag (semantic-current-tag))
  198. (run-hook-with-args-until-failure
  199. 'senator-search-tag-filter-functions tag)
  200. (setq tend (senator-search-tag-name tag))
  201. (setq tstart (match-beginning 0)
  202. found (and (>= sstart tstart)
  203. (<= send tend)
  204. (zerop (setq count (1- count))))))
  205. (goto-char next))))
  206. (cond ((null found)
  207. (setq next origin
  208. send origin))
  209. ((= next sstart)
  210. (setq next send
  211. send sstart))
  212. (t
  213. (setq next sstart)))
  214. (goto-char next)
  215. ;; Setup the returned value and the `match-data' or maybe fail!
  216. (funcall searcher text send noerror step)))
  217. ;;; Navigation commands
  218. ;;;###autoload
  219. (defun senator-next-tag ()
  220. "Navigate to the next Semantic tag.
  221. Return the tag or nil if at end of buffer."
  222. (interactive)
  223. (let ((pos (point))
  224. (tag (semantic-current-tag))
  225. where)
  226. (if (and tag
  227. (not (senator-skip-p tag))
  228. (senator-step-at-start-end-p tag)
  229. (or (= pos (semantic-tag-start tag))
  230. (senator-middle-of-tag-p pos tag)))
  231. nil
  232. (if (setq tag (senator-step-at-parent tag))
  233. nil
  234. (setq tag (semantic-find-tag-by-overlay-next pos))
  235. (while (and tag (senator-skip-p tag))
  236. (setq tag (semantic-find-tag-by-overlay-next
  237. (semantic-tag-start tag))))))
  238. (if (not tag)
  239. (progn
  240. (goto-char (point-max))
  241. (message "End of buffer"))
  242. (cond ((and (senator-step-at-start-end-p tag)
  243. (or (= pos (semantic-tag-start tag))
  244. (senator-middle-of-tag-p pos tag)))
  245. (setq where "end")
  246. (goto-char (semantic-tag-end tag)))
  247. (t
  248. (setq where "start")
  249. (goto-char (semantic-tag-start tag))))
  250. (senator-momentary-highlight-tag tag)
  251. (message "%S: %s (%s)"
  252. (semantic-tag-class tag)
  253. (semantic-tag-name tag)
  254. where))
  255. tag))
  256. ;;;###autoload
  257. (defun senator-previous-tag ()
  258. "Navigate to the previous Semantic tag.
  259. Return the tag or nil if at beginning of buffer."
  260. (interactive)
  261. (let ((pos (point))
  262. (tag (semantic-current-tag))
  263. where)
  264. (if (and tag
  265. (not (senator-skip-p tag))
  266. (senator-step-at-start-end-p tag)
  267. (or (= pos (semantic-tag-end tag))
  268. (senator-middle-of-tag-p pos tag)))
  269. nil
  270. (if (setq tag (senator-step-at-parent tag))
  271. nil
  272. (setq tag (senator-previous-tag-or-parent pos))
  273. (while (and tag (senator-skip-p tag))
  274. (setq tag (senator-previous-tag-or-parent
  275. (semantic-tag-start tag))))))
  276. (if (not tag)
  277. (progn
  278. (goto-char (point-min))
  279. (message "Beginning of buffer"))
  280. (cond ((or (not (senator-step-at-start-end-p tag))
  281. (= pos (semantic-tag-end tag))
  282. (senator-middle-of-tag-p pos tag))
  283. (setq where "start")
  284. (goto-char (semantic-tag-start tag)))
  285. (t
  286. (setq where "end")
  287. (goto-char (semantic-tag-end tag))))
  288. (senator-momentary-highlight-tag tag)
  289. (message "%S: %s (%s)"
  290. (semantic-tag-class tag)
  291. (semantic-tag-name tag)
  292. where))
  293. tag))
  294. ;;; Search commands
  295. (defun senator-search-forward (string &optional bound noerror count)
  296. "Search in tag names forward from point for STRING.
  297. Set point to the end of the occurrence found, and return point.
  298. See also the function `search-forward' for details on the BOUND,
  299. NOERROR and COUNT arguments."
  300. (interactive "sSemantic search: ")
  301. (senator-search 'search-forward string bound noerror count))
  302. (defun senator-re-search-forward (regexp &optional bound noerror count)
  303. "Search in tag names forward from point for regular expression REGEXP.
  304. Set point to the end of the occurrence found, and return point.
  305. See also the function `re-search-forward' for details on the BOUND,
  306. NOERROR and COUNT arguments."
  307. (interactive "sSemantic regexp search: ")
  308. (senator-search 're-search-forward regexp bound noerror count))
  309. (defun senator-word-search-forward (word &optional bound noerror count)
  310. "Search in tag names forward from point for WORD.
  311. Set point to the end of the occurrence found, and return point.
  312. See also the function `word-search-forward' for details on the BOUND,
  313. NOERROR and COUNT arguments."
  314. (interactive "sSemantic word search: ")
  315. (senator-search 'word-search-forward word bound noerror count))
  316. (defun senator-search-backward (string &optional bound noerror count)
  317. "Search in tag names backward from point for STRING.
  318. Set point to the beginning of the occurrence found, and return point.
  319. See also the function `search-backward' for details on the BOUND,
  320. NOERROR and COUNT arguments."
  321. (interactive "sSemantic backward search: ")
  322. (senator-search 'search-backward string bound noerror count))
  323. (defun senator-re-search-backward (regexp &optional bound noerror count)
  324. "Search in tag names backward from point for regular expression REGEXP.
  325. Set point to the beginning of the occurrence found, and return point.
  326. See also the function `re-search-backward' for details on the BOUND,
  327. NOERROR and COUNT arguments."
  328. (interactive "sSemantic backward regexp search: ")
  329. (senator-search 're-search-backward regexp bound noerror count))
  330. (defun senator-word-search-backward (word &optional bound noerror count)
  331. "Search in tag names backward from point for WORD.
  332. Set point to the beginning of the occurrence found, and return point.
  333. See also the function `word-search-backward' for details on the BOUND,
  334. NOERROR and COUNT arguments."
  335. (interactive "sSemantic backward word search: ")
  336. (senator-search 'word-search-backward word bound noerror count))
  337. ;;; Other useful search commands (minor mode menu)
  338. (defvar senator-last-search-type nil
  339. "Type of last non-incremental search command called.")
  340. (defun senator-nonincremental-repeat-search-forward ()
  341. "Search forward for the previous search string or regexp."
  342. (interactive)
  343. (cond
  344. ((and (eq senator-last-search-type 'string)
  345. search-ring)
  346. (senator-search-forward (car search-ring)))
  347. ((and (eq senator-last-search-type 'regexp)
  348. regexp-search-ring)
  349. (senator-re-search-forward (car regexp-search-ring)))
  350. (t
  351. (error "No previous search"))))
  352. (defun senator-nonincremental-repeat-search-backward ()
  353. "Search backward for the previous search string or regexp."
  354. (interactive)
  355. (cond
  356. ((and (eq senator-last-search-type 'string)
  357. search-ring)
  358. (senator-search-backward (car search-ring)))
  359. ((and (eq senator-last-search-type 'regexp)
  360. regexp-search-ring)
  361. (senator-re-search-backward (car regexp-search-ring)))
  362. (t
  363. (error "No previous search"))))
  364. (defun senator-nonincremental-search-forward (string)
  365. "Search for STRING nonincrementally."
  366. (interactive "sSemantic search for string: ")
  367. (setq senator-last-search-type 'string)
  368. (if (equal string "")
  369. (senator-search-forward (car search-ring))
  370. (isearch-update-ring string nil)
  371. (senator-search-forward string)))
  372. (defun senator-nonincremental-search-backward (string)
  373. "Search backward for STRING nonincrementally."
  374. (interactive "sSemantic search for string: ")
  375. (setq senator-last-search-type 'string)
  376. (if (equal string "")
  377. (senator-search-backward (car search-ring))
  378. (isearch-update-ring string nil)
  379. (senator-search-backward string)))
  380. (defun senator-nonincremental-re-search-forward (string)
  381. "Search for the regular expression STRING nonincrementally."
  382. (interactive "sSemantic search for regexp: ")
  383. (setq senator-last-search-type 'regexp)
  384. (if (equal string "")
  385. (senator-re-search-forward (car regexp-search-ring))
  386. (isearch-update-ring string t)
  387. (senator-re-search-forward string)))
  388. (defun senator-nonincremental-re-search-backward (string)
  389. "Search backward for the regular expression STRING nonincrementally."
  390. (interactive "sSemantic search for regexp: ")
  391. (setq senator-last-search-type 'regexp)
  392. (if (equal string "")
  393. (senator-re-search-backward (car regexp-search-ring))
  394. (isearch-update-ring string t)
  395. (senator-re-search-backward string)))
  396. (defvar senator--search-filter nil)
  397. (defun senator-search-set-tag-class-filter (&optional classes)
  398. "In current buffer, limit search scope to tag CLASSES.
  399. CLASSES is a list of tag class symbols or nil. If nil only global
  400. filters in `senator-search-tag-filter-functions' remain active."
  401. (interactive "sClasses: ")
  402. (setq classes
  403. (cond
  404. ((null classes)
  405. nil)
  406. ((symbolp classes)
  407. (list classes))
  408. ((stringp classes)
  409. (mapcar 'read (split-string classes)))
  410. (t
  411. (signal 'wrong-type-argument (list classes)))
  412. ))
  413. ;; Clear previous filter.
  414. (remove-hook 'senator-search-tag-filter-functions
  415. senator--search-filter t)
  416. (kill-local-variable 'senator--search-filter)
  417. (if classes
  418. (let ((tag (make-symbol "tag"))
  419. (names (mapconcat 'symbol-name classes "', `")))
  420. (set (make-local-variable 'senator--search-filter)
  421. `(lambda (,tag)
  422. (memq (semantic-tag-class ,tag) ',classes)))
  423. (add-hook 'senator-search-tag-filter-functions
  424. senator--search-filter nil t)
  425. (message "Limit search to `%s' tags" names))
  426. (message "Default search filter restored")))
  427. ;;; Folding
  428. ;;
  429. ;; Use new folding state. It might be wise to extend the idea
  430. ;; of folding for hiding all but this, or show all children, etc.
  431. (defun senator-fold-tag (&optional tag)
  432. "Fold the current TAG."
  433. (interactive)
  434. (semantic-set-tag-folded (or tag (semantic-current-tag)) t))
  435. (defun senator-unfold-tag (&optional tag)
  436. "Fold the current TAG."
  437. (interactive)
  438. (semantic-set-tag-folded (or tag (semantic-current-tag)) nil))
  439. (defun senator-fold-tag-toggle (&optional tag)
  440. "Fold the current TAG."
  441. (interactive)
  442. (let ((tag (or tag (semantic-current-tag))))
  443. (if (semantic-tag-folded-p tag)
  444. (senator-unfold-tag tag)
  445. (senator-fold-tag tag))))
  446. ;; @TODO - move this to some analyzer / refs tool
  447. (define-overloadable-function semantic-up-reference (tag)
  448. "Return a tag that is referred to by TAG.
  449. A \"reference\" could be any interesting feature of TAG.
  450. In C++, a function may have a 'parent' which is non-local.
  451. If that parent which is only a reference in the function tag
  452. is found, we can jump to it.
  453. Some tags such as includes have other reference features.")
  454. ;;;###autoload
  455. (defun senator-go-to-up-reference (&optional tag)
  456. "Move up one reference from the current TAG.
  457. A \"reference\" could be any interesting feature of TAG.
  458. In C++, a function may have a 'parent' which is non-local.
  459. If that parent which is only a reference in the function tag
  460. is found, we can jump to it.
  461. Some tags such as includes have other reference features."
  462. (interactive)
  463. (let ((result (semantic-up-reference (or tag (semantic-current-tag)))))
  464. (if (not result)
  465. (error "No up reference found")
  466. (push-mark)
  467. (cond
  468. ;; A tag
  469. ((semantic-tag-p result)
  470. (semantic-go-to-tag result)
  471. (switch-to-buffer (current-buffer))
  472. (semantic-momentary-highlight-tag result))
  473. ;; Buffers
  474. ((bufferp result)
  475. (switch-to-buffer result)
  476. (pulse-momentary-highlight-one-line (point)))
  477. ;; Files
  478. ((and (stringp result) (file-exists-p result))
  479. (find-file result)
  480. (pulse-momentary-highlight-one-line (point)))
  481. (t
  482. (error "Unknown result type from `semantic-up-reference'"))))))
  483. (defun semantic-up-reference-default (tag)
  484. "Return a tag that is referred to by TAG.
  485. Makes C/C++ language like assumptions."
  486. (cond ((semantic-tag-faux-p tag)
  487. ;; Faux tags should have a real tag in some other location.
  488. (require 'semantic/sort)
  489. (let ((options (semantic-tag-external-class tag)))
  490. ;; I should do something a little better than
  491. ;; this. Oy!
  492. (car options)
  493. ))
  494. ;; Include always point to another file.
  495. ((eq (semantic-tag-class tag) 'include)
  496. (let ((file (semantic-dependency-tag-file tag)))
  497. (cond
  498. ((or (not file) (not (file-exists-p file)))
  499. (error "Could not location include %s"
  500. (semantic-tag-name tag)))
  501. ((get-file-buffer file)
  502. (get-file-buffer file))
  503. ((stringp file)
  504. file)
  505. )))
  506. ;; Is there a parent of the function to jump to?
  507. ((and (semantic-tag-of-class-p tag 'function)
  508. (semantic-tag-function-parent tag))
  509. (let* ((scope (semantic-calculate-scope (point))))
  510. ;; @todo - it would be cool to ask the user which one if
  511. ;; more than one.
  512. (car (oref scope parents))
  513. ))
  514. ;; Is there a non-prototype version of the tag to jump to?
  515. ((semantic-tag-get-attribute tag :prototype-flag)
  516. (require 'semantic/analyze/refs)
  517. (let* ((sar (semantic-analyze-tag-references tag)))
  518. (car (semantic-analyze-refs-impl sar t)))
  519. )
  520. ;; If this is a datatype, and we have superclasses
  521. ((and (semantic-tag-of-class-p tag 'type)
  522. (semantic-tag-type-superclasses tag))
  523. (require 'semantic/analyze)
  524. (let ((scope (semantic-calculate-scope (point)))
  525. (parents (semantic-tag-type-superclasses tag)))
  526. (semantic-analyze-find-tag (car parents) 'type scope)))
  527. ;; Get the data type, and try to find that.
  528. ((semantic-tag-type tag)
  529. (require 'semantic/analyze)
  530. (let ((scope (semantic-calculate-scope (point))))
  531. (semantic-analyze-tag-type tag scope))
  532. )
  533. (t nil)))
  534. (defvar senator-isearch-semantic-mode nil
  535. "Non-nil if isearch does semantic search.
  536. This is a buffer local variable.")
  537. (make-variable-buffer-local 'senator-isearch-semantic-mode)
  538. (defun senator-beginning-of-defun (&optional arg)
  539. "Move backward to the beginning of a defun.
  540. Use semantic tags to navigate.
  541. ARG is the number of tags to navigate (not yet implemented)."
  542. (semantic-fetch-tags)
  543. (let* ((senator-highlight-found nil)
  544. ;; Step at beginning of next tag with class specified in
  545. ;; `senator-step-at-tag-classes'.
  546. (senator-step-at-start-end-tag-classes t)
  547. (tag (senator-previous-tag)))
  548. (when tag
  549. (if (= (point) (semantic-tag-end tag))
  550. (goto-char (semantic-tag-start tag)))
  551. (beginning-of-line))))
  552. (defun senator-end-of-defun (&optional arg)
  553. "Move forward to next end of defun.
  554. Use semantic tags to navigate.
  555. ARG is the number of tags to navigate (not yet implemented)."
  556. (semantic-fetch-tags)
  557. (let* ((senator-highlight-found nil)
  558. ;; Step at end of next tag with class specified in
  559. ;; `senator-step-at-tag-classes'.
  560. (senator-step-at-start-end-tag-classes t)
  561. (tag (senator-next-tag)))
  562. (when tag
  563. (if (= (point) (semantic-tag-start tag))
  564. (goto-char (semantic-tag-end tag)))
  565. (skip-chars-forward " \t")
  566. (if (looking-at "\\s<\\|\n")
  567. (forward-line 1)))))
  568. (defun senator-narrow-to-defun ()
  569. "Make text outside current defun invisible.
  570. The defun visible is the one that contains point or follows point.
  571. Use semantic tags to navigate."
  572. (interactive)
  573. (semantic-fetch-tags)
  574. (save-excursion
  575. (widen)
  576. (senator-end-of-defun)
  577. (let ((end (point)))
  578. (senator-beginning-of-defun)
  579. (narrow-to-region (point) end))))
  580. (defun senator-mark-defun ()
  581. "Put mark at end of this defun, point at beginning.
  582. The defun marked is the one that contains point or follows point.
  583. Use semantic tags to navigate."
  584. (interactive)
  585. (let ((origin (point))
  586. (end (progn (senator-end-of-defun) (point)))
  587. (start (progn (senator-beginning-of-defun) (point))))
  588. (goto-char origin)
  589. (push-mark (point))
  590. (goto-char end) ;; end-of-defun
  591. (push-mark (point) nil t)
  592. (goto-char start) ;; beginning-of-defun
  593. (re-search-backward "^\n" (- (point) 1) t)))
  594. ;;; Tag Cut & Paste
  595. ;; To copy a tag, means to put a tag definition into the tag
  596. ;; ring. To kill a tag, put the tag into the tag ring AND put
  597. ;; the body of the tag into the kill-ring.
  598. ;;
  599. ;; To retrieve a killed tag's text, use C-y (yank), but to retrieve
  600. ;; the tag as a reference of some sort, use senator-yank-tag.
  601. (defvar senator-tag-ring (make-ring 20)
  602. "Ring of tags for use with cut and paste.")
  603. ;;;###autoload
  604. (defun senator-copy-tag ()
  605. "Take the current tag, and place it in the tag ring."
  606. (interactive)
  607. (semantic-fetch-tags)
  608. (let ((ft (semantic-obtain-foreign-tag)))
  609. (when ft
  610. (ring-insert senator-tag-ring ft)
  611. (kill-ring-save (semantic-tag-start ft) (semantic-tag-end ft))
  612. (when (called-interactively-p 'interactive)
  613. (message "Use C-y to yank text. \
  614. Use `senator-yank-tag' for prototype insert.")))
  615. ft))
  616. ;;;###autoload
  617. (defun senator-kill-tag ()
  618. "Take the current tag, place it in the tag ring, and kill it.
  619. Killing the tag removes the text for that tag, and places it into
  620. the kill ring. Retrieve that text with \\[yank]."
  621. (interactive)
  622. (let ((ct (senator-copy-tag))) ;; this handles the reparse for us.
  623. (kill-region (semantic-tag-start ct)
  624. (semantic-tag-end ct))
  625. (when (called-interactively-p 'interactive)
  626. (message "Use C-y to yank text. \
  627. Use `senator-yank-tag' for prototype insert."))))
  628. ;;;###autoload
  629. (defun senator-yank-tag ()
  630. "Yank a tag from the tag ring.
  631. The form the tag takes is different depending on where it is being
  632. yanked to."
  633. (interactive)
  634. (or (ring-empty-p senator-tag-ring)
  635. (let ((ft (ring-ref senator-tag-ring 0)))
  636. (semantic-foreign-tag-check ft)
  637. (semantic-insert-foreign-tag ft)
  638. (when (called-interactively-p 'interactive)
  639. (message "Use C-y to recover the yank the text of %s."
  640. (semantic-tag-name ft))))))
  641. ;;;###autoload
  642. (defun senator-copy-tag-to-register (register &optional kill-flag)
  643. "Copy the current tag into REGISTER.
  644. Optional argument KILL-FLAG will delete the text of the tag to the
  645. kill ring."
  646. (interactive "cTag to register: \nP")
  647. (semantic-fetch-tags)
  648. (let ((ft (semantic-obtain-foreign-tag)))
  649. (when ft
  650. (set-register register ft)
  651. (if kill-flag
  652. (kill-region (semantic-tag-start ft)
  653. (semantic-tag-end ft))))))
  654. ;;;###autoload
  655. (defun senator-transpose-tags-up ()
  656. "Transpose the current tag, and the preceding tag."
  657. (interactive)
  658. (semantic-fetch-tags)
  659. (let* ((current-tag (semantic-current-tag))
  660. (prev-tag (save-excursion
  661. (goto-char (semantic-tag-start current-tag))
  662. (semantic-find-tag-by-overlay-prev)))
  663. (ct-parent (semantic-find-tag-parent-by-overlay current-tag))
  664. (pt-parent (semantic-find-tag-parent-by-overlay prev-tag)))
  665. (if (not (eq ct-parent pt-parent))
  666. (error "Cannot transpose tags"))
  667. (let ((txt (buffer-substring (semantic-tag-start current-tag)
  668. (semantic-tag-end current-tag)))
  669. (line (count-lines (semantic-tag-start current-tag)
  670. (point)))
  671. (insert-point nil)
  672. )
  673. (delete-region (semantic-tag-start current-tag)
  674. (semantic-tag-end current-tag))
  675. (delete-blank-lines)
  676. (goto-char (semantic-tag-start prev-tag))
  677. (setq insert-point (point))
  678. (insert txt)
  679. (if (/= (current-column) 0)
  680. (insert "\n"))
  681. (insert "\n")
  682. (goto-char insert-point)
  683. (forward-line line)
  684. )))
  685. ;;;###autoload
  686. (defun senator-transpose-tags-down ()
  687. "Transpose the current tag, and the following tag."
  688. (interactive)
  689. (semantic-fetch-tags)
  690. (let* ((current-tag (semantic-current-tag))
  691. (next-tag (save-excursion
  692. (goto-char (semantic-tag-end current-tag))
  693. (semantic-find-tag-by-overlay-next)))
  694. (end-pt (point-marker))
  695. )
  696. (goto-char (semantic-tag-start next-tag))
  697. (forward-char 1)
  698. (senator-transpose-tags-up)
  699. ;; I know that the above fcn deletes the next tag, so our pt marker
  700. ;; will be stable.
  701. (goto-char end-pt)))
  702. ;;; Using semantic search in isearch mode
  703. (defun senator-lazy-highlight-update ()
  704. "Force lazy highlight update."
  705. (lazy-highlight-cleanup t)
  706. (set 'isearch-lazy-highlight-last-string nil)
  707. (setq isearch-adjusted t)
  708. (isearch-update))
  709. ;; Recent versions of GNU Emacs allow to override the isearch search
  710. ;; function for special needs, and avoid to advice the built-in search
  711. ;; function :-)
  712. (defun senator-isearch-search-fun ()
  713. "Return the function to use for the search.
  714. Use a senator search function when semantic isearch mode is enabled."
  715. (intern
  716. (concat (if senator-isearch-semantic-mode
  717. "senator-"
  718. "")
  719. (cond (isearch-word "word-")
  720. (isearch-regexp "re-")
  721. (t ""))
  722. "search-"
  723. (if isearch-forward
  724. "forward"
  725. "backward"))))
  726. (defun senator-isearch-toggle-semantic-mode ()
  727. "Toggle semantic searching on or off in isearch mode."
  728. (interactive)
  729. (setq senator-isearch-semantic-mode
  730. (not senator-isearch-semantic-mode))
  731. (if isearch-mode
  732. ;; force lazy highlight update
  733. (senator-lazy-highlight-update)
  734. (message "Isearch semantic mode %s"
  735. (if senator-isearch-semantic-mode
  736. "enabled"
  737. "disabled"))))
  738. (defvar senator-old-isearch-search-fun nil
  739. "Hold previous value of `isearch-search-fun-function'.")
  740. (defun senator-isearch-mode-hook ()
  741. "Isearch mode hook to setup semantic searching."
  742. (if (and isearch-mode senator-isearch-semantic-mode)
  743. (progn
  744. ;; When `senator-isearch-semantic-mode' is on save the
  745. ;; previous `isearch-search-fun-function' and install the
  746. ;; senator one.
  747. (when (and (local-variable-p 'isearch-search-fun-function)
  748. (not (local-variable-p 'senator-old-isearch-search-fun)))
  749. (set (make-local-variable 'senator-old-isearch-search-fun)
  750. isearch-search-fun-function))
  751. (set (make-local-variable 'isearch-search-fun-function)
  752. 'senator-isearch-search-fun))
  753. ;; When `senator-isearch-semantic-mode' is off restore the
  754. ;; previous `isearch-search-fun-function'.
  755. (when (eq isearch-search-fun-function 'senator-isearch-search-fun)
  756. (if (local-variable-p 'senator-old-isearch-search-fun)
  757. (progn
  758. (set (make-local-variable 'isearch-search-fun-function)
  759. senator-old-isearch-search-fun)
  760. (kill-local-variable 'senator-old-isearch-search-fun))
  761. (kill-local-variable 'isearch-search-fun-function)))))
  762. ;; (add-hook 'isearch-mode-hook 'senator-isearch-mode-hook)
  763. ;; (add-hook 'isearch-mode-end-hook 'senator-isearch-mode-hook)
  764. ;; ;; Keyboard shortcut to toggle semantic search in isearch mode.
  765. ;; (define-key isearch-mode-map
  766. ;; [(control ?,)]
  767. ;; 'senator-isearch-toggle-semantic-mode)
  768. (provide 'semantic/senator)
  769. ;; Local variables:
  770. ;; generated-autoload-file: "loaddefs.el"
  771. ;; generated-autoload-load-name: "semantic/senator"
  772. ;; End:
  773. ;;; semantic/senator.el ends here