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