find.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. ;;; semantic/find.el --- Search routines for Semantic
  2. ;; Copyright (C) 1999-2005, 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: syntax
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Routines for searching through lists of tags.
  19. ;; There are several groups of tag search routines:
  20. ;;
  21. ;; 1) semantic-brute-find-tag-by-*
  22. ;; These routines use brute force hierarchical search to scan
  23. ;; through lists of tags. They include some parameters
  24. ;; used for compatibility with the semantic 1.x search routines.
  25. ;;
  26. ;; 1.5) semantic-brute-find-first-tag-by-*
  27. ;; Like 1, except searching stops on the first match for the given
  28. ;; information.
  29. ;;
  30. ;; 2) semantic-find-tag-by-*
  31. ;; These preferred search routines attempt to scan through lists
  32. ;; in an intelligent way based on questions asked.
  33. ;;
  34. ;; 3) semantic-find-*-overlay
  35. ;; These routines use overlays to return tags based on a buffer position.
  36. ;;
  37. ;; 4) ...
  38. ;;; Code:
  39. (require 'semantic)
  40. (require 'semantic/tag)
  41. (declare-function semantic-tag-protected-p "semantic/tag-ls")
  42. ;;; Overlay Search Routines
  43. ;;
  44. ;; These routines provide fast access to tokens based on a buffer that
  45. ;; has parsed tokens in it. Uses overlays to perform the hard work.
  46. ;;
  47. ;;;###autoload
  48. (defun semantic-find-tag-by-overlay (&optional positionormarker buffer)
  49. "Find all tags covering POSITIONORMARKER by using overlays.
  50. If POSITIONORMARKER is nil, use the current point.
  51. Optional BUFFER is used if POSITIONORMARKER is a number, otherwise the current
  52. buffer is used. This finds all tags covering the specified position
  53. by checking for all overlays covering the current spot. They are then sorted
  54. from largest to smallest via the start location."
  55. (save-excursion
  56. (when positionormarker
  57. (if (markerp positionormarker)
  58. (set-buffer (marker-buffer positionormarker))
  59. (if (bufferp buffer)
  60. (set-buffer buffer))))
  61. (let ((ol (semantic-overlays-at (or positionormarker (point))))
  62. (ret nil))
  63. (while ol
  64. (let ((tmp (semantic-overlay-get (car ol) 'semantic)))
  65. (when (and tmp
  66. ;; We don't need with-position because no tag w/out
  67. ;; a position could exist in an overlay.
  68. (semantic-tag-p tmp))
  69. (setq ret (cons tmp ret))))
  70. (setq ol (cdr ol)))
  71. (sort ret (lambda (a b) (< (semantic-tag-start a)
  72. (semantic-tag-start b)))))))
  73. ;;;###autoload
  74. (defun semantic-find-tag-by-overlay-in-region (start end &optional buffer)
  75. "Find all tags which exist in whole or in part between START and END.
  76. Uses overlays to determine position.
  77. Optional BUFFER argument specifies the buffer to use."
  78. (save-excursion
  79. (if buffer (set-buffer buffer))
  80. (let ((ol (semantic-overlays-in start end))
  81. (ret nil))
  82. (while ol
  83. (let ((tmp (semantic-overlay-get (car ol) 'semantic)))
  84. (when (and tmp
  85. ;; See above about position
  86. (semantic-tag-p tmp))
  87. (setq ret (cons tmp ret))))
  88. (setq ol (cdr ol)))
  89. (sort ret (lambda (a b) (< (semantic-tag-start a)
  90. (semantic-tag-start b)))))))
  91. ;;;###autoload
  92. (defun semantic-find-tag-by-overlay-next (&optional start buffer)
  93. "Find the next tag after START in BUFFER.
  94. If START is in an overlay, find the tag which starts next,
  95. not the current tag."
  96. (save-excursion
  97. (if buffer (set-buffer buffer))
  98. (if (not start) (setq start (point)))
  99. (let ((os start) (ol nil))
  100. (while (and os (< os (point-max)) (not ol))
  101. (setq os (semantic-overlay-next-change os))
  102. (when os
  103. ;; Get overlays at position
  104. (setq ol (semantic-overlays-at os))
  105. ;; find the overlay that belongs to semantic
  106. ;; and starts at the found position.
  107. (while (and ol (listp ol))
  108. (if (and (semantic-overlay-get (car ol) 'semantic)
  109. (semantic-tag-p
  110. (semantic-overlay-get (car ol) 'semantic))
  111. (= (semantic-overlay-start (car ol)) os))
  112. (setq ol (car ol)))
  113. (when (listp ol) (setq ol (cdr ol))))))
  114. ;; convert ol to a tag
  115. (when (and ol (semantic-tag-p (semantic-overlay-get ol 'semantic)))
  116. (semantic-overlay-get ol 'semantic)))))
  117. ;;;###autoload
  118. (defun semantic-find-tag-by-overlay-prev (&optional start buffer)
  119. "Find the next tag before START in BUFFER.
  120. If START is in an overlay, find the tag which starts next,
  121. not the current tag."
  122. (save-excursion
  123. (if buffer (set-buffer buffer))
  124. (if (not start) (setq start (point)))
  125. (let ((os start) (ol nil))
  126. (while (and os (> os (point-min)) (not ol))
  127. (setq os (semantic-overlay-previous-change os))
  128. (when os
  129. ;; Get overlays at position
  130. (setq ol (semantic-overlays-at (1- os)))
  131. ;; find the overlay that belongs to semantic
  132. ;; and ENDS at the found position.
  133. ;;
  134. ;; Use end because we are going backward.
  135. (while (and ol (listp ol))
  136. (if (and (semantic-overlay-get (car ol) 'semantic)
  137. (semantic-tag-p
  138. (semantic-overlay-get (car ol) 'semantic))
  139. (= (semantic-overlay-end (car ol)) os))
  140. (setq ol (car ol)))
  141. (when (listp ol) (setq ol (cdr ol))))))
  142. ;; convert ol to a tag
  143. (when (and ol
  144. (semantic-tag-p (semantic-overlay-get ol 'semantic)))
  145. (semantic-overlay-get ol 'semantic)))))
  146. ;;;###autoload
  147. (defun semantic-find-tag-parent-by-overlay (tag)
  148. "Find the parent of TAG by overlays.
  149. Overlays are a fast way of finding this information for active buffers."
  150. (let ((tag (nreverse (semantic-find-tag-by-overlay
  151. (semantic-tag-start tag)))))
  152. ;; This is a lot like `semantic-current-tag-parent', but
  153. ;; it uses a position to do it's work. Assumes two tags don't share
  154. ;; the same start unless they are siblings.
  155. (car (cdr tag))))
  156. ;;;###autoload
  157. (defun semantic-current-tag ()
  158. "Return the current tag in the current buffer.
  159. If there are more than one in the same location, return the
  160. smallest tag. Return nil if there is no tag here."
  161. (car (nreverse (semantic-find-tag-by-overlay))))
  162. ;;;###autoload
  163. (defun semantic-current-tag-parent ()
  164. "Return the current tags parent in the current buffer.
  165. A tag's parent would be a containing structure, such as a type
  166. containing a field. Return nil if there is no parent."
  167. (car (cdr (nreverse (semantic-find-tag-by-overlay)))))
  168. (defun semantic-current-tag-of-class (class)
  169. "Return the current (smallest) tags of CLASS in the current buffer.
  170. If the smallest tag is not of type CLASS, keep going upwards until one
  171. is found.
  172. Uses `semantic-tag-class' for classification."
  173. (let ((tags (nreverse (semantic-find-tag-by-overlay))))
  174. (while (and tags
  175. (not (eq (semantic-tag-class (car tags)) class)))
  176. (setq tags (cdr tags)))
  177. (car tags)))
  178. ;;; Search Routines
  179. ;;
  180. ;; These are routines that search a single tags table.
  181. ;;
  182. ;; The original API (see COMPATIBILITY section below) in semantic 1.4
  183. ;; had these usage statistics:
  184. ;;
  185. ;; semantic-find-nonterminal-by-name 17
  186. ;; semantic-find-nonterminal-by-name-regexp 8 - Most doing completion
  187. ;; semantic-find-nonterminal-by-position 13
  188. ;; semantic-find-nonterminal-by-token 21
  189. ;; semantic-find-nonterminal-by-type 2
  190. ;; semantic-find-nonterminal-standard 1
  191. ;;
  192. ;; semantic-find-nonterminal-by-function (not in other searches) 1
  193. ;;
  194. ;; New API: As above w/out `search-parts' or `search-includes' arguments.
  195. ;; Extra fcn: Specific to completion which is what -name-regexp is
  196. ;; mostly used for
  197. ;;
  198. ;; As for the sarguments "search-parts" and "search-includes" here
  199. ;; are stats:
  200. ;;
  201. ;; search-parts: 4 - charting x2, find-doc, senator (sans db)
  202. ;;
  203. ;; Implement command to flatten a tag table. Call new API Fcn w/
  204. ;; flattened table for same results.
  205. ;;
  206. ;; search-include: 2 - analyze x2 (sans db)
  207. ;;
  208. ;; Not used effectively. Not to be re-implemented here.
  209. (defsubst semantic--find-tags-by-function (predicate &optional table)
  210. "Find tags for which PREDICATE is non-nil in TABLE.
  211. PREDICATE is a lambda expression which accepts on TAG.
  212. TABLE is a semantic tags table. See `semantic-something-to-tag-table'."
  213. (let ((tags (semantic-something-to-tag-table table))
  214. (result nil))
  215. ; (mapc (lambda (tag) (and (funcall predicate tag)
  216. ; (setq result (cons tag result))))
  217. ; tags)
  218. ;; A while loop is actually faster. Who knew
  219. (while tags
  220. (and (funcall predicate (car tags))
  221. (setq result (cons (car tags) result)))
  222. (setq tags (cdr tags)))
  223. (nreverse result)))
  224. ;; I can shave off some time by removing the funcall (see above)
  225. ;; and having the question be inlined in the while loop.
  226. ;; Strangely turning the upper level fcns into macros had a larger
  227. ;; impact.
  228. (defmacro semantic--find-tags-by-macro (form &optional table)
  229. "Find tags for which FORM is non-nil in TABLE.
  230. TABLE is a semantic tags table. See `semantic-something-to-tag-table'."
  231. `(let ((tags (semantic-something-to-tag-table ,table))
  232. (result nil))
  233. (while tags
  234. (and ,form
  235. (setq result (cons (car tags) result)))
  236. (setq tags (cdr tags)))
  237. (nreverse result)))
  238. ;;; Top level Searches
  239. ;;
  240. ;;;###autoload
  241. (defun semantic-find-first-tag-by-name (name &optional table)
  242. "Find the first tag with NAME in TABLE.
  243. NAME is a string.
  244. TABLE is a semantic tags table. See `semantic-something-to-tag-table'.
  245. This routine uses `assoc' to quickly find the first matching entry."
  246. (funcall (if semantic-case-fold 'assoc-ignore-case 'assoc)
  247. name (semantic-something-to-tag-table table)))
  248. (defmacro semantic-find-tags-by-name (name &optional table)
  249. "Find all tags with NAME in TABLE.
  250. NAME is a string.
  251. TABLE is a tag table. See `semantic-something-to-tag-table'."
  252. `(let ((case-fold-search semantic-case-fold))
  253. (semantic--find-tags-by-macro
  254. (string= ,name (semantic-tag-name (car tags)))
  255. ,table)))
  256. (defmacro semantic-find-tags-for-completion (prefix &optional table)
  257. "Find all tags whose name begins with PREFIX in TABLE.
  258. PREFIX is a string.
  259. TABLE is a tag table. See `semantic-something-to-tag-table'.
  260. While it would be nice to use `try-completion' or `all-completions',
  261. those functions do not return the tags, only a string.
  262. Uses `compare-strings' for fast comparison."
  263. `(let ((l (length ,prefix)))
  264. (semantic--find-tags-by-macro
  265. (eq (compare-strings ,prefix 0 nil
  266. (semantic-tag-name (car tags)) 0 l
  267. semantic-case-fold)
  268. t)
  269. ,table)))
  270. (defmacro semantic-find-tags-by-name-regexp (regexp &optional table)
  271. "Find all tags with name matching REGEXP in TABLE.
  272. REGEXP is a string containing a regular expression,
  273. TABLE is a tag table. See `semantic-something-to-tag-table'.
  274. Consider using `semantic-find-tags-for-completion' if you are
  275. attempting to do completions."
  276. `(let ((case-fold-search semantic-case-fold))
  277. (semantic--find-tags-by-macro
  278. (string-match ,regexp (semantic-tag-name (car tags)))
  279. ,table)))
  280. (defmacro semantic-find-tags-by-class (class &optional table)
  281. "Find all tags of class CLASS in TABLE.
  282. CLASS is a symbol representing the class of the token, such as
  283. 'variable, of 'function..
  284. TABLE is a tag table. See `semantic-something-to-tag-table'."
  285. `(semantic--find-tags-by-macro
  286. (eq ,class (semantic-tag-class (car tags)))
  287. ,table))
  288. (defmacro semantic-find-tags-by-type (type &optional table)
  289. "Find all tags of with a type TYPE in TABLE.
  290. TYPE is a string or tag representing a data type as defined in the
  291. language the tags were parsed from, such as \"int\", or perhaps
  292. a tag whose name is that of a struct or class.
  293. TABLE is a tag table. See `semantic-something-to-tag-table'."
  294. `(semantic--find-tags-by-macro
  295. (semantic-tag-of-type-p (car tags) ,type)
  296. ,table))
  297. (defmacro semantic-find-tags-of-compound-type (&optional table)
  298. "Find all tags which are a compound type in TABLE.
  299. Compound types are structures, or other data type which
  300. is not of a primitive nature, such as int or double.
  301. Used in completion."
  302. `(semantic--find-tags-by-macro
  303. (semantic-tag-type-compound-p (car tags))
  304. ,table))
  305. ;;;###autoload
  306. (define-overloadable-function semantic-find-tags-by-scope-protection (scopeprotection parent &optional table)
  307. "Find all tags accessible by SCOPEPROTECTION.
  308. SCOPEPROTECTION is a symbol which can be returned by the method
  309. `semantic-tag-protection'. A hard-coded order is used to determine a match.
  310. PARENT is a tag representing the PARENT slot needed for
  311. `semantic-tag-protection'.
  312. TABLE is a list of tags (a subset of PARENT members) to scan. If TABLE is nil,
  313. the type members of PARENT are used.
  314. See `semantic-tag-protected-p' for details on which tags are returned."
  315. (if (not (eq (semantic-tag-class parent) 'type))
  316. (signal 'wrong-type-argument '(semantic-find-tags-by-scope-protection
  317. parent
  318. semantic-tag-class type))
  319. (:override)))
  320. (defun semantic-find-tags-by-scope-protection-default
  321. (scopeprotection parent &optional table)
  322. "Find all tags accessible by SCOPEPROTECTION.
  323. SCOPEPROTECTION is a symbol which can be returned by the method
  324. `semantic-tag-protection'. A hard-coded order is used to determine a match.
  325. PARENT is a tag representing the PARENT slot needed for
  326. `semantic-tag-protection'.
  327. TABLE is a list of tags (a subset of PARENT members) to scan. If TABLE is nil,
  328. the type members of PARENT are used.
  329. See `semantic-tag-protected-p' for details on which tags are returned."
  330. (if (not table) (setq table (semantic-tag-type-members parent)))
  331. (if (null scopeprotection)
  332. table
  333. (require 'semantic/tag-ls)
  334. (semantic--find-tags-by-macro
  335. (not (semantic-tag-protected-p (car tags) scopeprotection parent))
  336. table)))
  337. (defsubst semantic-find-tags-included (&optional table)
  338. "Find all tags in TABLE that are of the 'include class.
  339. TABLE is a tag table. See `semantic-something-to-tag-table'."
  340. (semantic-find-tags-by-class 'include table))
  341. ;;; Deep Searches
  342. (defmacro semantic-deep-find-tags-by-name (name &optional table)
  343. "Find all tags with NAME in TABLE.
  344. Search in top level tags, and their components, in TABLE.
  345. NAME is a string.
  346. TABLE is a tag table. See `semantic-flatten-tags-table'.
  347. See also `semantic-find-tags-by-name'."
  348. `(semantic-find-tags-by-name
  349. ,name (semantic-flatten-tags-table ,table)))
  350. (defmacro semantic-deep-find-tags-for-completion (prefix &optional table)
  351. "Find all tags whose name begins with PREFIX in TABLE.
  352. Search in top level tags, and their components, in TABLE.
  353. TABLE is a tag table. See `semantic-flatten-tags-table'.
  354. See also `semantic-find-tags-for-completion'."
  355. `(semantic-find-tags-for-completion
  356. ,prefix (semantic-flatten-tags-table ,table)))
  357. (defmacro semantic-deep-find-tags-by-name-regexp (regexp &optional table)
  358. "Find all tags with name matching REGEXP in TABLE.
  359. Search in top level tags, and their components, in TABLE.
  360. REGEXP is a string containing a regular expression,
  361. TABLE is a tag table. See `semantic-flatten-tags-table'.
  362. See also `semantic-find-tags-by-name-regexp'.
  363. Consider using `semantic-deep-find-tags-for-completion' if you are
  364. attempting to do completions."
  365. `(semantic-find-tags-by-name-regexp
  366. ,regexp (semantic-flatten-tags-table ,table)))
  367. ;;; Specialty Searches
  368. (defun semantic-find-tags-external-children-of-type (type &optional table)
  369. "Find all tags in whose parent is TYPE in TABLE.
  370. These tags are defined outside the scope of the original TYPE declaration.
  371. TABLE is a tag table. See `semantic-something-to-tag-table'."
  372. (semantic--find-tags-by-macro
  373. (equal (semantic-tag-external-member-parent (car tags))
  374. type)
  375. table))
  376. (defun semantic-find-tags-subclasses-of-type (type &optional table)
  377. "Find all tags of class type in whose parent is TYPE in TABLE.
  378. These tags are defined outside the scope of the original TYPE declaration.
  379. TABLE is a tag table. See `semantic-something-to-tag-table'."
  380. (semantic--find-tags-by-macro
  381. (and (eq (semantic-tag-class (car tags)) 'type)
  382. (or (member type (semantic-tag-type-superclasses (car tags)))
  383. (member type (semantic-tag-type-interfaces (car tags)))))
  384. table))
  385. ;;
  386. ;; ************************** Compatibility ***************************
  387. ;;
  388. ;;; Old Style Brute Force Search Routines
  389. ;;
  390. ;; These functions will search through tags lists explicitly for
  391. ;; desired information.
  392. ;; The -by-name nonterminal search can use the built in fcn
  393. ;; `assoc', which is faster than looping ourselves, so we will
  394. ;; not use `semantic-brute-find-tag-by-function' to do this,
  395. ;; instead erroring on the side of speed.
  396. (defun semantic-brute-find-first-tag-by-name
  397. (name streamorbuffer &optional search-parts search-include)
  398. "Find a tag NAME within STREAMORBUFFER. NAME is a string.
  399. If SEARCH-PARTS is non-nil, search children of tags.
  400. If SEARCH-INCLUDE was never implemented.
  401. Use `semantic-find-first-tag-by-name' instead."
  402. (let* ((stream (semantic-something-to-tag-table streamorbuffer))
  403. (assoc-fun (if semantic-case-fold
  404. #'assoc-ignore-case
  405. #'assoc))
  406. (m (funcall assoc-fun name stream)))
  407. (if m
  408. m
  409. (let ((toklst stream)
  410. (children nil))
  411. (while (and (not m) toklst)
  412. (if search-parts
  413. (progn
  414. (setq children (semantic-tag-components-with-overlays
  415. (car toklst)))
  416. (if children
  417. (setq m (semantic-brute-find-first-tag-by-name
  418. name children search-parts search-include)))))
  419. (setq toklst (cdr toklst)))
  420. (if (not m)
  421. ;; Go to dependencies, and search there.
  422. nil)
  423. m))))
  424. (defmacro semantic-brute-find-tag-by-class
  425. (class streamorbuffer &optional search-parts search-includes)
  426. "Find all tags with a class CLASS within STREAMORBUFFER.
  427. CLASS is a symbol representing the class of the tags to find.
  428. See `semantic-tag-class'.
  429. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  430. `semantic-brute-find-tag-by-function'.
  431. Use `semantic-find-tag-by-class' instead."
  432. `(semantic-brute-find-tag-by-function
  433. (lambda (tag) (eq ,class (semantic-tag-class tag)))
  434. ,streamorbuffer ,search-parts ,search-includes))
  435. (defmacro semantic-brute-find-tag-standard
  436. (streamorbuffer &optional search-parts search-includes)
  437. "Find all tags in STREAMORBUFFER which define simple class types.
  438. See `semantic-tag-class'.
  439. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  440. `semantic-brute-find-tag-by-function'."
  441. `(semantic-brute-find-tag-by-function
  442. (lambda (tag) (member (semantic-tag-class tag)
  443. '(function variable type)))
  444. ,streamorbuffer ,search-parts ,search-includes))
  445. (defun semantic-brute-find-tag-by-type
  446. (type streamorbuffer &optional search-parts search-includes)
  447. "Find all tags with type TYPE within STREAMORBUFFER.
  448. TYPE is a string which is the name of the type of the tags returned.
  449. See `semantic-tag-type'.
  450. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  451. `semantic-brute-find-tag-by-function'."
  452. (semantic-brute-find-tag-by-function
  453. (lambda (tag)
  454. (let ((ts (semantic-tag-type tag)))
  455. (if (and (listp ts)
  456. (or (= (length ts) 1)
  457. (eq (semantic-tag-class ts) 'type)))
  458. (setq ts (semantic-tag-name ts)))
  459. (equal type ts)))
  460. streamorbuffer search-parts search-includes))
  461. (defun semantic-brute-find-tag-by-type-regexp
  462. (regexp streamorbuffer &optional search-parts search-includes)
  463. "Find all tags with type matching REGEXP within STREAMORBUFFER.
  464. REGEXP is a regular expression which matches the name of the type of the
  465. tags returned. See `semantic-tag-type'.
  466. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  467. `semantic-brute-find-tag-by-function'."
  468. (semantic-brute-find-tag-by-function
  469. (lambda (tag)
  470. (let ((ts (semantic-tag-type tag)))
  471. (if (listp ts)
  472. (setq ts
  473. (if (eq (semantic-tag-class ts) 'type)
  474. (semantic-tag-name ts)
  475. (car ts))))
  476. (and ts (string-match regexp ts))))
  477. streamorbuffer search-parts search-includes))
  478. (defun semantic-brute-find-tag-by-name-regexp
  479. (regex streamorbuffer &optional search-parts search-includes)
  480. "Find all tags whose name match REGEX in STREAMORBUFFER.
  481. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  482. `semantic-brute-find-tag-by-function'."
  483. (semantic-brute-find-tag-by-function
  484. (lambda (tag) (string-match regex (semantic-tag-name tag)))
  485. streamorbuffer search-parts search-includes)
  486. )
  487. (defun semantic-brute-find-tag-by-property
  488. (property value streamorbuffer &optional search-parts search-includes)
  489. "Find all tags with PROPERTY equal to VALUE in STREAMORBUFFER.
  490. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  491. `semantic-brute-find-tag-by-function'."
  492. (semantic-brute-find-tag-by-function
  493. (lambda (tag) (equal (semantic--tag-get-property tag property) value))
  494. streamorbuffer search-parts search-includes)
  495. )
  496. (defun semantic-brute-find-tag-by-attribute
  497. (attr streamorbuffer &optional search-parts search-includes)
  498. "Find all tags with a given ATTR in STREAMORBUFFER.
  499. ATTR is a symbol key into the attributes list.
  500. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  501. `semantic-brute-find-tag-by-function'."
  502. (semantic-brute-find-tag-by-function
  503. (lambda (tag) (semantic-tag-get-attribute tag attr))
  504. streamorbuffer search-parts search-includes)
  505. )
  506. (defun semantic-brute-find-tag-by-attribute-value
  507. (attr value streamorbuffer &optional search-parts search-includes)
  508. "Find all tags with a given ATTR equal to VALUE in STREAMORBUFFER.
  509. ATTR is a symbol key into the attributes list.
  510. VALUE is the value that ATTR should match.
  511. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
  512. `semantic-brute-find-tag-by-function'."
  513. (semantic-brute-find-tag-by-function
  514. (lambda (tag) (equal (semantic-tag-get-attribute tag attr) value))
  515. streamorbuffer search-parts search-includes)
  516. )
  517. (defun semantic-brute-find-tag-by-function
  518. (function streamorbuffer &optional search-parts search-includes)
  519. "Find all tags for which FUNCTION's value is non-nil within STREAMORBUFFER.
  520. FUNCTION must return non-nil if an element of STREAM will be included
  521. in the new list.
  522. If optional argument SEARCH-PARTS is non-nil, all sub-parts of tags
  523. are searched. The overloadable function `semantic-tag-components' is
  524. used for the searching child lists. If SEARCH-PARTS is the symbol
  525. 'positiononly, then only children that have positional information are
  526. searched.
  527. If SEARCH-INCLUDES has not been implemented.
  528. This parameter hasn't be active for a while and is obsolete."
  529. (let ((stream (semantic-something-to-tag-table streamorbuffer))
  530. (sl nil) ;list of tag children
  531. (nl nil) ;new list
  532. (case-fold-search semantic-case-fold))
  533. (dolist (tag stream)
  534. (if (not (semantic-tag-p tag))
  535. ;; `semantic-tag-components-with-overlays' can return invalid
  536. ;; tags if search-parts is not equal to 'positiononly
  537. nil ;; Ignore them!
  538. (if (funcall function tag)
  539. (setq nl (cons tag nl)))
  540. (and search-parts
  541. (setq sl (if (eq search-parts 'positiononly)
  542. (semantic-tag-components-with-overlays tag)
  543. (semantic-tag-components tag))
  544. )
  545. (setq nl (nconc nl
  546. (semantic-brute-find-tag-by-function
  547. function sl
  548. search-parts))))))
  549. (setq nl (nreverse nl))
  550. nl))
  551. (defun semantic-brute-find-first-tag-by-function
  552. (function streamorbuffer &optional search-parts search-includes)
  553. "Find the first tag which FUNCTION match within STREAMORBUFFER.
  554. FUNCTION must return non-nil if an element of STREAM will be included
  555. in the new list.
  556. The following parameters were never implemented.
  557. If optional argument SEARCH-PARTS, all sub-parts of tags are searched.
  558. The overloadable function `semantic-tag-components' is used for
  559. searching.
  560. If SEARCH-INCLUDES is non-nil, then all include files are also
  561. searched for matches."
  562. (let ((stream (semantic-something-to-tag-table streamorbuffer))
  563. (found nil)
  564. (case-fold-search semantic-case-fold))
  565. (while (and (not found) stream)
  566. (if (funcall function (car stream))
  567. (setq found (car stream)))
  568. (setq stream (cdr stream)))
  569. found))
  570. ;;; Old Positional Searches
  571. ;;
  572. ;; Are these useful anymore?
  573. ;;
  574. (defun semantic-brute-find-tag-by-position (position streamorbuffer
  575. &optional nomedian)
  576. "Find a tag covering POSITION within STREAMORBUFFER.
  577. POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do
  578. the median calculation, and return nil."
  579. (save-excursion
  580. (if (markerp position) (set-buffer (marker-buffer position)))
  581. (let* ((stream (if (bufferp streamorbuffer)
  582. (with-current-buffer streamorbuffer
  583. (semantic-fetch-tags))
  584. streamorbuffer))
  585. (prev nil)
  586. (found nil))
  587. (while (and stream (not found))
  588. ;; perfect fit
  589. (if (and (>= position (semantic-tag-start (car stream)))
  590. (<= position (semantic-tag-end (car stream))))
  591. (setq found (car stream))
  592. ;; Median between to objects.
  593. (if (and prev (not nomedian)
  594. (>= position (semantic-tag-end prev))
  595. (<= position (semantic-tag-start (car stream))))
  596. (let ((median (/ (+ (semantic-tag-end prev)
  597. (semantic-tag-start (car stream)))
  598. 2)))
  599. (setq found
  600. (if (> position median)
  601. (car stream)
  602. prev)))))
  603. ;; Next!!!
  604. (setq prev (car stream)
  605. stream (cdr stream)))
  606. found)))
  607. (defun semantic-brute-find-innermost-tag-by-position
  608. (position streamorbuffer &optional nomedian)
  609. "Find a list of tags covering POSITION within STREAMORBUFFER.
  610. POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do
  611. the median calculation, and return nil.
  612. This function will find the topmost item, and recurse until no more
  613. details are available of findable."
  614. (let* ((returnme nil)
  615. (current (semantic-brute-find-tag-by-position
  616. position streamorbuffer nomedian))
  617. (nextstream (and current
  618. (if (eq (semantic-tag-class current) 'type)
  619. (semantic-tag-type-members current)
  620. nil))))
  621. (while nextstream
  622. (setq returnme (cons current returnme))
  623. (setq current (semantic-brute-find-tag-by-position
  624. position nextstream nomedian))
  625. (setq nextstream (and current
  626. ;; NOTE TO SELF:
  627. ;; Looking at this after several years away,
  628. ;; what does this do???
  629. (if (eq (semantic-tag-class current) 'token)
  630. (semantic-tag-type-members current)
  631. nil))))
  632. (nreverse (cons current returnme))))
  633. (provide 'semantic/find)
  634. ;; Local variables:
  635. ;; generated-autoload-file: "loaddefs.el"
  636. ;; generated-autoload-load-name: "semantic/find"
  637. ;; End:
  638. ;;; semantic/find.el ends here