Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[emacs.git] / lisp / cedet / semantic / find.el
blob189ebba8d6d1314856e1d18471cc26d553948e90
1 ;;; semantic/find.el --- Search routines for Semantic
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2010, 2011, 2012
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Routines for searching through lists of tags.
27 ;; There are several groups of tag search routines:
29 ;; 1) semantic-brute-find-tag-by-*
30 ;; These routines use brute force hierarchical search to scan
31 ;; through lists of tags. They include some parameters
32 ;; used for compatibility with the semantic 1.x search routines.
34 ;; 1.5) semantic-brute-find-first-tag-by-*
35 ;; Like 1, except searching stops on the first match for the given
36 ;; information.
38 ;; 2) semantic-find-tag-by-*
39 ;; These preferred search routines attempt to scan through lists
40 ;; in an intelligent way based on questions asked.
42 ;; 3) semantic-find-*-overlay
43 ;; These routines use overlays to return tags based on a buffer position.
45 ;; 4) ...
47 ;;; Code:
49 (require 'semantic)
50 (require 'semantic/tag)
52 (declare-function semantic-tag-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 This routine uses `assoc' to quickly find the first matching entry."
269 (funcall (if semantic-case-fold 'assoc-ignore-case 'assoc)
270 name (semantic-something-to-tag-table table)))
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-find-tags-by-type (type &optional table)
317 "Find all tags of with a type TYPE in TABLE.
318 TYPE is a string or tag representing a data type as defined in the
319 language the tags were parsed from, such as \"int\", or perhaps
320 a tag whose name is that of a struct or class.
321 TABLE is a tag table. See `semantic-something-to-tag-table'."
322 `(semantic--find-tags-by-macro
323 (semantic-tag-of-type-p (car tags) ,type)
324 ,table))
326 (defmacro semantic-find-tags-of-compound-type (&optional table)
327 "Find all tags which are a compound type in TABLE.
328 Compound types are structures, or other data type which
329 is not of a primitive nature, such as int or double.
330 Used in completion."
331 `(semantic--find-tags-by-macro
332 (semantic-tag-type-compound-p (car tags))
333 ,table))
335 ;;;###autoload
336 (define-overloadable-function semantic-find-tags-by-scope-protection (scopeprotection parent &optional table)
337 "Find all tags accessable by SCOPEPROTECTION.
338 SCOPEPROTECTION is a symbol which can be returned by the method
339 `semantic-tag-protection'. A hard-coded order is used to determine a match.
340 PARENT is a tag representing the PARENT slot needed for
341 `semantic-tag-protection'.
342 TABLE is a list of tags (a subset of PARENT members) to scan. If TABLE is nil,
343 the type members of PARENT are used.
344 See `semantic-tag-protected-p' for details on which tags are returned."
345 (if (not (eq (semantic-tag-class parent) 'type))
346 (signal 'wrong-type-argument '(semantic-find-tags-by-scope-protection
347 parent
348 semantic-tag-class type))
349 (:override)))
351 (defun semantic-find-tags-by-scope-protection-default
352 (scopeprotection parent &optional table)
353 "Find all tags accessible by SCOPEPROTECTION.
354 SCOPEPROTECTION is a symbol which can be returned by the method
355 `semantic-tag-protection'. A hard-coded order is used to determine a match.
356 PARENT is a tag representing the PARENT slot needed for
357 `semantic-tag-protection'.
358 TABLE is a list of tags (a subset of PARENT members) to scan. If TABLE is nil,
359 the type members of PARENT are used.
360 See `semantic-tag-protected-p' for details on which tags are returned."
361 (if (not table) (setq table (semantic-tag-type-members parent)))
362 (if (null scopeprotection)
363 table
364 (require 'semantic/tag-ls)
365 (semantic--find-tags-by-macro
366 (not (semantic-tag-protected-p (car tags) scopeprotection parent))
367 table)))
369 (defsubst semantic-find-tags-included (&optional table)
370 "Find all tags in TABLE that are of the 'include class.
371 TABLE is a tag table. See `semantic-something-to-tag-table'."
372 (semantic-find-tags-by-class 'include table))
374 ;;; Deep Searches
376 (defmacro semantic-deep-find-tags-by-name (name &optional table)
377 "Find all tags with NAME in TABLE.
378 Search in top level tags, and their components, in TABLE.
379 NAME is a string.
380 TABLE is a tag table. See `semantic-flatten-tags-table'.
381 See also `semantic-find-tags-by-name'."
382 `(semantic-find-tags-by-name
383 ,name (semantic-flatten-tags-table ,table)))
385 (defmacro semantic-deep-find-tags-for-completion (prefix &optional table)
386 "Find all tags whose name begins with PREFIX in TABLE.
387 Search in top level tags, and their components, in TABLE.
388 TABLE is a tag table. See `semantic-flatten-tags-table'.
389 See also `semantic-find-tags-for-completion'."
390 `(semantic-find-tags-for-completion
391 ,prefix (semantic-flatten-tags-table ,table)))
393 (defmacro semantic-deep-find-tags-by-name-regexp (regexp &optional table)
394 "Find all tags with name matching REGEXP in TABLE.
395 Search in top level tags, and their components, in TABLE.
396 REGEXP is a string containing a regular expression,
397 TABLE is a tag table. See `semantic-flatten-tags-table'.
398 See also `semantic-find-tags-by-name-regexp'.
399 Consider using `semantic-deep-find-tags-for-completion' if you are
400 attempting to do completions."
401 `(semantic-find-tags-by-name-regexp
402 ,regexp (semantic-flatten-tags-table ,table)))
404 ;;; Specialty Searches
406 (defun semantic-find-tags-external-children-of-type (type &optional table)
407 "Find all tags in whose parent is TYPE in TABLE.
408 These tags are defined outside the scope of the original TYPE declaration.
409 TABLE is a tag table. See `semantic-something-to-tag-table'."
410 (semantic--find-tags-by-macro
411 (equal (semantic-tag-external-member-parent (car tags))
412 type)
413 table))
415 (defun semantic-find-tags-subclasses-of-type (type &optional table)
416 "Find all tags of class type in whose parent is TYPE in TABLE.
417 These tags are defined outside the scope of the original TYPE declaration.
418 TABLE is a tag table. See `semantic-something-to-tag-table'."
419 (semantic--find-tags-by-macro
420 (and (eq (semantic-tag-class (car tags)) 'type)
421 (or (member type (semantic-tag-type-superclasses (car tags)))
422 (member type (semantic-tag-type-interfaces (car tags)))))
423 table))
426 ;; ************************** Compatibility ***************************
429 ;;; Old Style Brute Force Search Routines
431 ;; These functions will search through tags lists explicity for
432 ;; desired information.
434 ;; The -by-name nonterminal search can use the built in fcn
435 ;; `assoc', which is faster than looping ourselves, so we will
436 ;; not use `semantic-brute-find-tag-by-function' to do this,
437 ;; instead erroring on the side of speed.
439 (defun semantic-brute-find-first-tag-by-name
440 (name streamorbuffer &optional search-parts search-include)
441 "Find a tag NAME within STREAMORBUFFER. NAME is a string.
442 If SEARCH-PARTS is non-nil, search children of tags.
443 If SEARCH-INCLUDE was never implemented.
445 Use `semantic-find-first-tag-by-name' instead."
446 (let* ((stream (semantic-something-to-tag-table streamorbuffer))
447 (assoc-fun (if semantic-case-fold
448 #'assoc-ignore-case
449 #'assoc))
450 (m (funcall assoc-fun name stream)))
451 (if m
453 (let ((toklst stream)
454 (children nil))
455 (while (and (not m) toklst)
456 (if search-parts
457 (progn
458 (setq children (semantic-tag-components-with-overlays
459 (car toklst)))
460 (if children
461 (setq m (semantic-brute-find-first-tag-by-name
462 name children search-parts search-include)))))
463 (setq toklst (cdr toklst)))
464 (if (not m)
465 ;; Go to dependencies, and search there.
466 nil)
467 m))))
469 (defmacro semantic-brute-find-tag-by-class
470 (class streamorbuffer &optional search-parts search-includes)
471 "Find all tags with a class CLASS within STREAMORBUFFER.
472 CLASS is a symbol representing the class of the tags to find.
473 See `semantic-tag-class'.
474 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
475 `semantic-brute-find-tag-by-function'.
477 Use `semantic-find-tag-by-class' instead."
478 `(semantic-brute-find-tag-by-function
479 (lambda (tag) (eq ,class (semantic-tag-class tag)))
480 ,streamorbuffer ,search-parts ,search-includes))
482 (defmacro semantic-brute-find-tag-standard
483 (streamorbuffer &optional search-parts search-includes)
484 "Find all tags in STREAMORBUFFER which define simple class types.
485 See `semantic-tag-class'.
486 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
487 `semantic-brute-find-tag-by-function'."
488 `(semantic-brute-find-tag-by-function
489 (lambda (tag) (member (semantic-tag-class tag)
490 '(function variable type)))
491 ,streamorbuffer ,search-parts ,search-includes))
493 (defun semantic-brute-find-tag-by-type
494 (type streamorbuffer &optional search-parts search-includes)
495 "Find all tags with type TYPE within STREAMORBUFFER.
496 TYPE is a string which is the name of the type of the tags returned.
497 See `semantic-tag-type'.
498 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
499 `semantic-brute-find-tag-by-function'."
500 (semantic-brute-find-tag-by-function
501 (lambda (tag)
502 (let ((ts (semantic-tag-type tag)))
503 (if (and (listp ts)
504 (or (= (length ts) 1)
505 (eq (semantic-tag-class ts) 'type)))
506 (setq ts (semantic-tag-name ts)))
507 (equal type ts)))
508 streamorbuffer search-parts search-includes))
510 (defun semantic-brute-find-tag-by-type-regexp
511 (regexp streamorbuffer &optional search-parts search-includes)
512 "Find all tags with type matching REGEXP within STREAMORBUFFER.
513 REGEXP is a regular expression which matches the name of the type of the
514 tags returned. See `semantic-tag-type'.
515 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
516 `semantic-brute-find-tag-by-function'."
517 (semantic-brute-find-tag-by-function
518 (lambda (tag)
519 (let ((ts (semantic-tag-type tag)))
520 (if (listp ts)
521 (setq ts
522 (if (eq (semantic-tag-class ts) 'type)
523 (semantic-tag-name ts)
524 (car ts))))
525 (and ts (string-match regexp ts))))
526 streamorbuffer search-parts search-includes))
528 (defun semantic-brute-find-tag-by-name-regexp
529 (regex streamorbuffer &optional search-parts search-includes)
530 "Find all tags whose name match REGEX in STREAMORBUFFER.
531 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
532 `semantic-brute-find-tag-by-function'."
533 (semantic-brute-find-tag-by-function
534 (lambda (tag) (string-match regex (semantic-tag-name tag)))
535 streamorbuffer search-parts search-includes)
538 (defun semantic-brute-find-tag-by-property
539 (property value streamorbuffer &optional search-parts search-includes)
540 "Find all tags with PROPERTY equal to VALUE in STREAMORBUFFER.
541 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
542 `semantic-brute-find-tag-by-function'."
543 (semantic-brute-find-tag-by-function
544 (lambda (tag) (equal (semantic--tag-get-property tag property) value))
545 streamorbuffer search-parts search-includes)
548 (defun semantic-brute-find-tag-by-attribute
549 (attr streamorbuffer &optional search-parts search-includes)
550 "Find all tags with a given ATTR in STREAMORBUFFER.
551 ATTR is a symbol key into the attributes list.
552 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
553 `semantic-brute-find-tag-by-function'."
554 (semantic-brute-find-tag-by-function
555 (lambda (tag) (semantic-tag-get-attribute tag attr))
556 streamorbuffer search-parts search-includes)
559 (defun semantic-brute-find-tag-by-attribute-value
560 (attr value streamorbuffer &optional search-parts search-includes)
561 "Find all tags with a given ATTR equal to VALUE in STREAMORBUFFER.
562 ATTR is a symbol key into the attributes list.
563 VALUE is the value that ATTR should match.
564 Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
565 `semantic-brute-find-tag-by-function'."
566 (semantic-brute-find-tag-by-function
567 (lambda (tag) (equal (semantic-tag-get-attribute tag attr) value))
568 streamorbuffer search-parts search-includes)
571 (defun semantic-brute-find-tag-by-function
572 (function streamorbuffer &optional search-parts search-includes)
573 "Find all tags for which FUNCTION's value is non-nil within STREAMORBUFFER.
574 FUNCTION must return non-nil if an element of STREAM will be included
575 in the new list.
577 If optional argument SEARCH-PARTS is non-nil, all sub-parts of tags
578 are searched. The overloadable function `semantic-tag-componenets' is
579 used for the searching child lists. If SEARCH-PARTS is the symbol
580 'positiononly, then only children that have positional information are
581 searched.
583 If SEARCH-INCLUDES has not been implemented.
584 This parameter hasn't be active for a while and is obsolete."
585 (let ((stream (semantic-something-to-tag-table streamorbuffer))
586 (sl nil) ;list of tag children
587 (nl nil) ;new list
588 (case-fold-search semantic-case-fold))
589 (dolist (tag stream)
590 (if (not (semantic-tag-p tag))
591 ;; `semantic-tag-components-with-overlays' can return invalid
592 ;; tags if search-parts is not equal to 'positiononly
593 nil ;; Ignore them!
594 (if (funcall function tag)
595 (setq nl (cons tag nl)))
596 (and search-parts
597 (setq sl (if (eq search-parts 'positiononly)
598 (semantic-tag-components-with-overlays tag)
599 (semantic-tag-components tag))
601 (setq nl (nconc nl
602 (semantic-brute-find-tag-by-function
603 function sl
604 search-parts))))))
605 (setq nl (nreverse nl))
606 nl))
608 (defun semantic-brute-find-first-tag-by-function
609 (function streamorbuffer &optional search-parts search-includes)
610 "Find the first tag which FUNCTION match within STREAMORBUFFER.
611 FUNCTION must return non-nil if an element of STREAM will be included
612 in the new list.
614 The following parameters were never implemented.
616 If optional argument SEARCH-PARTS, all sub-parts of tags are searched.
617 The overloadable function `semantic-tag-components' is used for
618 searching.
619 If SEARCH-INCLUDES is non-nil, then all include files are also
620 searched for matches."
621 (let ((stream (semantic-something-to-tag-table streamorbuffer))
622 (found nil)
623 (case-fold-search semantic-case-fold))
624 (while (and (not found) stream)
625 (if (funcall function (car stream))
626 (setq found (car stream)))
627 (setq stream (cdr stream)))
628 found))
631 ;;; Old Positional Searches
633 ;; Are these useful anymore?
635 (defun semantic-brute-find-tag-by-position (position streamorbuffer
636 &optional nomedian)
637 "Find a tag covering POSITION within STREAMORBUFFER.
638 POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do
639 the median calculation, and return nil."
640 (save-excursion
641 (if (markerp position) (set-buffer (marker-buffer position)))
642 (let* ((stream (if (bufferp streamorbuffer)
643 (with-current-buffer streamorbuffer
644 (semantic-fetch-tags))
645 streamorbuffer))
646 (prev nil)
647 (found nil))
648 (while (and stream (not found))
649 ;; perfect fit
650 (if (and (>= position (semantic-tag-start (car stream)))
651 (<= position (semantic-tag-end (car stream))))
652 (setq found (car stream))
653 ;; Median between to objects.
654 (if (and prev (not nomedian)
655 (>= position (semantic-tag-end prev))
656 (<= position (semantic-tag-start (car stream))))
657 (let ((median (/ (+ (semantic-tag-end prev)
658 (semantic-tag-start (car stream)))
659 2)))
660 (setq found
661 (if (> position median)
662 (car stream)
663 prev)))))
664 ;; Next!!!
665 (setq prev (car stream)
666 stream (cdr stream)))
667 found)))
669 (defun semantic-brute-find-innermost-tag-by-position
670 (position streamorbuffer &optional nomedian)
671 "Find a list of tags covering POSITION within STREAMORBUFFER.
672 POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do
673 the median calculation, and return nil.
674 This function will find the topmost item, and recurse until no more
675 details are available of findable."
676 (let* ((returnme nil)
677 (current (semantic-brute-find-tag-by-position
678 position streamorbuffer nomedian))
679 (nextstream (and current
680 (if (eq (semantic-tag-class current) 'type)
681 (semantic-tag-type-members current)
682 nil))))
683 (while nextstream
684 (setq returnme (cons current returnme))
685 (setq current (semantic-brute-find-tag-by-position
686 position nextstream nomedian))
687 (setq nextstream (and current
688 ;; NOTE TO SELF:
689 ;; Looking at this after several years away,
690 ;; what does this do???
691 (if (eq (semantic-tag-class current) 'token)
692 (semantic-tag-type-members current)
693 nil))))
694 (nreverse (cons current returnme))))
696 (provide 'semantic/find)
698 ;; Local variables:
699 ;; generated-autoload-file: "loaddefs.el"
700 ;; generated-autoload-load-name: "semantic/find"
701 ;; End:
703 ;; arch-tag: db00c93e-e561-4bd6-942b-96eca5aaa9a6
704 ;;; semantic/find.el ends here