db-find.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. ;;; semantic/db-find.el --- Searching through semantic databases.
  2. ;; Copyright (C) 2000-2017 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: tags
  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. ;; Databases of various forms can all be searched.
  19. ;; There are a few types of searches that can be done:
  20. ;;
  21. ;; Basic Name Search:
  22. ;; These searches scan a database table collection for tags based
  23. ;; on name.
  24. ;;
  25. ;; Basic Attribute Search:
  26. ;; These searches allow searching on specific attributes of tags,
  27. ;; such as name, type, or other attribute.
  28. ;;
  29. ;; Advanced Search:
  30. ;; These are searches that were needed to accomplish some
  31. ;; specialized tasks as discovered in utilities. Advanced searches
  32. ;; include matching methods defined outside some parent class.
  33. ;;
  34. ;; The reason for advanced searches are so that external
  35. ;; repositories such as the Emacs obarray, or java .class files can
  36. ;; quickly answer these needed questions without dumping the entire
  37. ;; symbol list into Emacs for additional refinement searches via
  38. ;; regular semanticdb search.
  39. ;;
  40. ;; How databases are decided upon is another important aspect of a
  41. ;; database search. When it comes to searching for a name, there are
  42. ;; these types of searches:
  43. ;;
  44. ;; Basic Search:
  45. ;; Basic search means that tags looking for a given name start
  46. ;; with a specific search path. Names are sought on that path
  47. ;; until it is empty or items on the path can no longer be found.
  48. ;; Use `semanticdb-dump-all-table-summary' to test this list.
  49. ;; Use `semanticdb-find-throttle-custom-list' to refine this list.
  50. ;;
  51. ;; Deep Search:
  52. ;; A deep search will search more than just the global namespace.
  53. ;; It will recurse into tags that contain more tags, and search
  54. ;; those too.
  55. ;;
  56. ;; Brute Search:
  57. ;; Brute search means that all tables in all databases in a given
  58. ;; project are searched. Brute searches are the search style as
  59. ;; written for semantic version 1.x.
  60. ;;
  61. ;; How does the search path work?
  62. ;;
  63. ;; A basic search starts with three parameters:
  64. ;;
  65. ;; (FINDME &optional PATH FIND-FILE-MATCH)
  66. ;;
  67. ;; FINDME is key to be searched for dependent on the type of search.
  68. ;; PATH is an indicator of which tables are to be searched.
  69. ;; FIND-FILE-MATCH indicates that any time a match is found, the
  70. ;; file associated with the tag should be read into a file.
  71. ;;
  72. ;; The PATH argument is then the most interesting argument. It can
  73. ;; have these values:
  74. ;;
  75. ;; nil - Take the current buffer, and use its include list
  76. ;; buffer - Use that buffer's include list.
  77. ;; filename - Use that file's include list. If the file is not
  78. ;; in a buffer, see of there is a semanticdb table for it. If
  79. ;; not, read that file into a buffer.
  80. ;; tag - Get that tag's buffer of file file. See above.
  81. ;; table - Search that table, and its include list.
  82. ;;
  83. ;; Search Results:
  84. ;;
  85. ;; Semanticdb returns the results in a specific format. There are a
  86. ;; series of routines for using those results, and results can be
  87. ;; passed in as a search-path for refinement searches with
  88. ;; semanticdb. Apropos for semanticdb.*find-result for more.
  89. ;;
  90. ;; Application:
  91. ;;
  92. ;; Here are applications where different searches are needed which
  93. ;; exist as of semantic 1.4.x
  94. ;;
  95. ;; eldoc - popup help
  96. ;; => Requires basic search using default path. (Header files ok)
  97. ;; tag jump - jump to a named tag
  98. ;; => Requires a brute search using whole project. (Source files only)
  99. ;; completion - Completing symbol names in a smart way
  100. ;; => Basic search (headers ok)
  101. ;; type analysis - finding type definitions for variables & fcns
  102. ;; => Basic search (headers ok)
  103. ;; Class browser - organize types into some structure
  104. ;; => Brute search, or custom navigation.
  105. ;; TODO:
  106. ;; During a search, load any unloaded DB files based on paths in the
  107. ;; current project.
  108. (require 'semantic/db)
  109. (require 'semantic/db-ref)
  110. (eval-when-compile
  111. (require 'semantic/find))
  112. ;;; Code:
  113. (defvar data-debug-thing-alist)
  114. (declare-function data-debug-insert-stuff-list "data-debug")
  115. (declare-function data-debug-new-buffer "data-debug")
  116. ;;;(declare-function data-debug-insert-tag-list "adebug")
  117. (declare-function semantic-scope-reset-cache "semantic/scope")
  118. (declare-function semanticdb-typecache-notify-reset "semantic/db-typecache")
  119. (declare-function ede-current-project "ede")
  120. (defvar semanticdb-find-throttle-custom-list
  121. '(set (const local)
  122. (const project)
  123. (const unloaded)
  124. (const system)
  125. (const recursive)
  126. (const omniscience))
  127. "Customization values for semanticdb find throttle.
  128. See `semanticdb-find-throttle' for details.")
  129. ;;;###autoload
  130. (defcustom semanticdb-find-default-throttle
  131. '(local project unloaded system recursive)
  132. "The default throttle for `semanticdb-find' routines.
  133. The throttle controls how detailed the list of database
  134. tables is for a symbol lookup. The value is a list with
  135. the following keys:
  136. `file' - The file the search is being performed from.
  137. This option is here for completeness only, and
  138. is assumed to always be on.
  139. `local' - Tables from the same local directory are included.
  140. This includes files directly referenced by a file name
  141. which might be in a different directory.
  142. `project' - Tables from the same local project are included
  143. If `project' is specified, then `local' is assumed.
  144. `unloaded' - If a table is not in memory, load it. If it is not cached
  145. on disk either, get the source, parse it, and create
  146. the table.
  147. `system' - Tables from system databases. These are specifically
  148. tables from system header files, or language equivalent.
  149. `recursive' - For include based searches, includes tables referenced
  150. by included files.
  151. `omniscience' - Included system databases which are omniscience, or
  152. somehow know everything. Omniscience databases are found
  153. in `semanticdb-project-system-databases'.
  154. The Emacs Lisp system DB is an omniscience database."
  155. :group 'semanticdb
  156. :type semanticdb-find-throttle-custom-list)
  157. (make-variable-buffer-local 'semanticdb-find-default-throttle)
  158. (defun semanticdb-find-throttle-active-p (access-type)
  159. "Non-nil if ACCESS-TYPE is an active throttle type."
  160. (or (memq access-type semanticdb-find-default-throttle)
  161. (eq access-type 'file)
  162. (and (eq access-type 'local)
  163. (memq 'project semanticdb-find-default-throttle))
  164. ))
  165. ;;; Index Class
  166. ;;
  167. ;; The find routines spend a lot of time looking stuff up.
  168. ;; Use this handy search index to cache data between searches.
  169. ;; This should allow searches to start running faster.
  170. (defclass semanticdb-find-search-index (semanticdb-abstract-search-index)
  171. ((include-path :initform nil
  172. :documentation
  173. "List of semanticdb tables from the include path.")
  174. (type-cache :initform nil
  175. :documentation
  176. "Cache of all the data types accessible from this file.
  177. Includes all types from all included files, merged namespaces, and
  178. expunge duplicates.")
  179. )
  180. "Concrete search index for `semanticdb-find'.
  181. This class will cache data derived during various searches.")
  182. (cl-defmethod semantic-reset ((idx semanticdb-find-search-index))
  183. "Reset the object IDX."
  184. (require 'semantic/scope)
  185. ;; Clear the include path.
  186. (oset idx include-path nil)
  187. (when (oref idx type-cache)
  188. (semantic-reset (oref idx type-cache)))
  189. ;; Clear the scope. Scope doesn't have the data it needs to track
  190. ;; its own reset.
  191. (semantic-scope-reset-cache)
  192. )
  193. (cl-defmethod semanticdb-synchronize ((idx semanticdb-find-search-index)
  194. new-tags)
  195. "Synchronize the search index IDX with some NEW-TAGS."
  196. ;; Reset our parts.
  197. (semantic-reset idx)
  198. ;; Notify dependants by clearing their indices.
  199. (semanticdb-notify-references
  200. (oref idx table)
  201. (lambda (tab me)
  202. (semantic-reset (semanticdb-get-table-index tab))))
  203. )
  204. (cl-defmethod semanticdb-partial-synchronize ((idx semanticdb-find-search-index)
  205. new-tags)
  206. "Synchronize the search index IDX with some changed NEW-TAGS."
  207. ;; Only reset if include statements changed.
  208. (if (semantic-find-tags-by-class 'include new-tags)
  209. (progn
  210. (semantic-reset idx)
  211. ;; Notify dependants by clearing their indices.
  212. (semanticdb-notify-references
  213. (oref idx table)
  214. (lambda (tab me)
  215. (semantic-reset (semanticdb-get-table-index tab))))
  216. )
  217. ;; Else, not an include, by just a type.
  218. (when (oref idx type-cache)
  219. (when (semanticdb-partial-synchronize (oref idx type-cache) new-tags)
  220. ;; If the synchronize returns true, we need to notify.
  221. ;; Notify dependants by clearing their indices.
  222. (semanticdb-notify-references
  223. (oref idx table)
  224. (lambda (tab me)
  225. (let ((tab-idx (semanticdb-get-table-index tab)))
  226. ;; Not a full reset?
  227. (when (oref tab-idx type-cache)
  228. (require 'semantic/db-typecache)
  229. (semanticdb-typecache-notify-reset
  230. (oref tab-idx type-cache)))
  231. )))
  232. ))
  233. ))
  234. ;;; Path Translations
  235. ;;
  236. ;;; OVERLOAD Functions
  237. ;;
  238. ;; These routines needed to be overloaded by specific language modes.
  239. ;; They are needed for translating an INCLUDE tag into a semanticdb
  240. ;; TABLE object.
  241. ;;;###autoload
  242. (define-overloadable-function semanticdb-find-translate-path (path brutish)
  243. "Translate PATH into a list of semantic tables.
  244. Path translation involves identifying the PATH input argument
  245. in one of the following ways:
  246. nil - Take the current buffer, and use its include list
  247. buffer - Use that buffer's include list.
  248. filename - Use that file's include list. If the file is not
  249. in a buffer, see of there is a semanticdb table for it. If
  250. not, read that file into a buffer.
  251. tag - Get that tag's buffer of file file. See above.
  252. table - Search that table, and its include list.
  253. find result - Search the results of a previous find.
  254. In addition, once the base path is found, there is the possibility of
  255. each added table adding yet more tables to the path, so this routine
  256. can return a lengthy list.
  257. If argument BRUTISH is non-nil, then instead of using the include
  258. list, use all tables found in the parent project of the table
  259. identified by translating PATH. Such searches use brute force to
  260. scan every available table.
  261. The return value is a list of objects of type `semanticdb-table' or
  262. their children. In the case of passing in a find result, the result
  263. is returned unchanged.
  264. This routine uses `semanticdb-find-table-for-include' to translate
  265. specific include tags into a semanticdb table.
  266. Note: When searching using a non-brutish method, the list of
  267. included files will be cached between runs. Database-references
  268. are used to track which files need to have their include lists
  269. refreshed when things change. See `semanticdb-ref-test'.
  270. Note for overloading: If you opt to overload this function for your
  271. major mode, and your routine takes a long time, be sure to call
  272. (semantic-throw-on-input \\='your-symbol-here)
  273. so that it can be called from the idle work handler."
  274. )
  275. (defun semanticdb-find-translate-path-default (path brutish)
  276. "Translate PATH into a list of semantic tables.
  277. If BRUTISH is non nil, return all tables associated with PATH.
  278. Default action as described in `semanticdb-find-translate-path'."
  279. (if (semanticdb-find-results-p path)
  280. ;; nil means perform the search over these results.
  281. nil
  282. (if brutish
  283. (semanticdb-find-translate-path-brutish-default path)
  284. (semanticdb-find-translate-path-includes-default path))))
  285. ;;;###autoload
  286. (define-overloadable-function semanticdb-find-table-for-include (includetag &optional table)
  287. "For a single INCLUDETAG found in TABLE, find a `semanticdb-table' object
  288. INCLUDETAG is a semantic TAG of class `include'.
  289. TABLE is a semanticdb table that identifies where INCLUDETAG came from.
  290. TABLE is optional if INCLUDETAG has an overlay of :filename attribute."
  291. )
  292. (defun semanticdb-find-translate-path-brutish-default (path)
  293. "Translate PATH into a list of semantic tables.
  294. Default action as described in `semanticdb-find-translate-path'."
  295. (let ((basedb
  296. (cond ((null path) semanticdb-current-database)
  297. ((semanticdb-table-p path) (oref path parent-db))
  298. (t (let ((tt (semantic-something-to-tag-table path)))
  299. (if tt
  300. ;; @todo - What does this DO ??!?!
  301. (with-current-buffer (semantic-tag-buffer (car tt))
  302. semanticdb-current-database)
  303. semanticdb-current-database))))))
  304. (apply
  305. #'nconc
  306. (mapcar
  307. (lambda (db)
  308. (let ((tabs (semanticdb-get-database-tables db))
  309. (ret nil))
  310. ;; Only return tables of the same language (major-mode)
  311. ;; as the current search environment.
  312. (while tabs
  313. (semantic-throw-on-input 'translate-path-brutish)
  314. (if (semanticdb-equivalent-mode-for-search (car tabs)
  315. (current-buffer))
  316. (setq ret (cons (car tabs) ret)))
  317. (setq tabs (cdr tabs)))
  318. ret))
  319. ;; FIXME:
  320. ;; This should scan the current project directory list for all
  321. ;; semanticdb files, perhaps handling proxies for them.
  322. (semanticdb-current-database-list
  323. (if basedb (oref basedb reference-directory)
  324. default-directory))))
  325. ))
  326. (defun semanticdb-find-incomplete-cache-entries-p (cache)
  327. "Are there any incomplete entries in CACHE?"
  328. (let ((ans nil))
  329. (dolist (tab cache)
  330. (when (and (semanticdb-table-child-p tab)
  331. (not (number-or-marker-p (oref tab pointmax))))
  332. (setq ans t))
  333. )
  334. ans))
  335. (defun semanticdb-find-need-cache-update-p (table)
  336. "Non-nil if the semanticdb TABLE cache needs to be updated."
  337. ;; If we were passed in something related to a TABLE,
  338. ;; do a caching lookup.
  339. (let* ((index (semanticdb-get-table-index table))
  340. (cache (when index (oref index include-path)))
  341. (incom (semanticdb-find-incomplete-cache-entries-p cache))
  342. (unl (semanticdb-find-throttle-active-p 'unloaded))
  343. )
  344. (if (and
  345. cache ;; Must have a cache
  346. (or
  347. ;; If all entries are "full", or if 'unloaded
  348. ;; OR
  349. ;; is not in the throttle, it is ok to use the cache.
  350. (not incom) (not unl)
  351. ))
  352. nil
  353. ;;cache
  354. ;; ELSE
  355. ;;
  356. ;; We need an update.
  357. t))
  358. )
  359. (defun semanticdb-find-translate-path-includes-default (path)
  360. "Translate PATH into a list of semantic tables.
  361. Default action as described in `semanticdb-find-translate-path'."
  362. (let ((table (cond ((null path)
  363. semanticdb-current-table)
  364. ((bufferp path)
  365. (semantic-buffer-local-value 'semanticdb-current-table path))
  366. ((and (stringp path) (file-exists-p path))
  367. (semanticdb-file-table-object path t))
  368. ((semanticdb-abstract-table-child-p path)
  369. path)
  370. (t nil))))
  371. (if table
  372. ;; If we were passed in something related to a TABLE,
  373. ;; do a caching lookup.
  374. (let ((index (semanticdb-get-table-index table)))
  375. (if (semanticdb-find-need-cache-update-p table)
  376. ;; Let's go look up our indices.
  377. (let ((ans (semanticdb-find-translate-path-includes--internal path)))
  378. (oset index include-path ans)
  379. ;; Once we have our new indices set up, notify those
  380. ;; who depend on us if we found something for them to
  381. ;; depend on.
  382. (when ans (semanticdb-refresh-references table))
  383. ans)
  384. ;; ELSE
  385. ;;
  386. ;; Just return the cache.
  387. (oref index include-path)))
  388. ;; If we were passed in something like a tag list, or other boring
  389. ;; searchable item, then instead do the regular thing without caching.
  390. (semanticdb-find-translate-path-includes--internal path))))
  391. (defvar semanticdb-find-lost-includes nil
  392. "Include files that we cannot find associated with this buffer.")
  393. (make-variable-buffer-local 'semanticdb-find-lost-includes)
  394. (defvar semanticdb-find-scanned-include-tags nil
  395. "All include tags scanned, plus action taken on the tag.
  396. Each entry is an alist:
  397. (ACTION . TAG)
  398. where ACTION is one of 'scanned, 'duplicate, 'lost
  399. and TAG is a clone of the include tag that was found.")
  400. (make-variable-buffer-local 'semanticdb-find-scanned-include-tags)
  401. (defvar semanticdb-implied-include-tags nil
  402. "Include tags implied for all files of a given mode.
  403. Set this variable with `defvar-mode-local' for a particular mode so
  404. that any symbols that exist for all files for that mode are included.
  405. Note: This could be used as a way to write a file in a language
  406. to declare all the built-ins for that language.")
  407. (defun semanticdb-find-translate-path-includes--internal (path)
  408. "Internal implementation of `semanticdb-find-translate-path-includes-default'.
  409. This routine does not depend on the cache, but will always derive
  410. a new path from the provided PATH."
  411. (let ((includetags nil)
  412. (curtable nil)
  413. (matchedtables (list semanticdb-current-table))
  414. (matchedincludes nil)
  415. (lostincludes nil)
  416. (scannedincludes nil)
  417. (incfname nil)
  418. nexttable)
  419. (cond ((null path)
  420. (semantic-refresh-tags-safe)
  421. (setq includetags (append
  422. (semantic-find-tags-included (current-buffer))
  423. semanticdb-implied-include-tags)
  424. curtable semanticdb-current-table
  425. incfname (buffer-file-name))
  426. )
  427. ((semanticdb-table-p path)
  428. (setq includetags (semantic-find-tags-included path)
  429. curtable path
  430. incfname (semanticdb-full-filename path))
  431. )
  432. ((bufferp path)
  433. (with-current-buffer path
  434. (semantic-refresh-tags-safe))
  435. (setq includetags (semantic-find-tags-included path)
  436. curtable (with-current-buffer path
  437. semanticdb-current-table)
  438. incfname (buffer-file-name path)))
  439. (t
  440. (setq includetags (semantic-find-tags-included path))
  441. (when includetags
  442. ;; If we have some tags, derive a table from them.
  443. ;; else we will do nothing, so the table is useless.
  444. ;; @todo - derive some tables
  445. (message "Need to derive tables for %S in translate-path-includes--default."
  446. path)
  447. )))
  448. ;; Make sure each found include tag has an originating file name associated
  449. ;; with it.
  450. (when incfname
  451. (dolist (it includetags)
  452. (semantic--tag-put-property it :filename incfname)))
  453. ;; Loop over all include tags adding to matchedtables
  454. (while includetags
  455. (semantic-throw-on-input 'semantic-find-translate-path-includes-default)
  456. ;; If we've seen this include string before, lets skip it.
  457. (if (member (semantic-tag-name (car includetags)) matchedincludes)
  458. (progn
  459. (setq nexttable nil)
  460. (push (cons 'duplicate (semantic-tag-clone (car includetags)))
  461. scannedincludes)
  462. )
  463. (setq nexttable (semanticdb-find-table-for-include (car includetags) curtable))
  464. (when (not nexttable)
  465. ;; Save the lost include.
  466. (push (car includetags) lostincludes)
  467. (push (cons 'lost (semantic-tag-clone (car includetags)))
  468. scannedincludes)
  469. )
  470. )
  471. ;; Push the include file, so if we can't find it, we only
  472. ;; can't find it once.
  473. (push (semantic-tag-name (car includetags)) matchedincludes)
  474. ;; (message "Scanning %s" (semantic-tag-name (car includetags)))
  475. (when (and nexttable
  476. (not (memq nexttable matchedtables))
  477. (semanticdb-equivalent-mode-for-search nexttable
  478. (current-buffer))
  479. )
  480. ;; Add to list of tables
  481. (push nexttable matchedtables)
  482. ;; Queue new includes to list
  483. (if (semanticdb-find-throttle-active-p 'recursive)
  484. ;; @todo - recursive includes need to have the originating
  485. ;; buffer's location added to the path.
  486. (let ((newtags
  487. (cond
  488. ((semanticdb-table-p nexttable)
  489. (semanticdb-refresh-table nexttable)
  490. ;; Use the method directly, or we will recurse
  491. ;; into ourselves here.
  492. (semanticdb-find-tags-by-class-method
  493. nexttable 'include))
  494. (t ;; @todo - is this ever possible???
  495. (message "semanticdb-ftp - how did you do that?")
  496. (semantic-find-tags-included
  497. (semanticdb-get-tags nexttable)))
  498. ))
  499. (newincfname (semanticdb-full-filename nexttable))
  500. )
  501. (push (cons 'scanned (semantic-tag-clone (car includetags)))
  502. scannedincludes)
  503. ;; Setup new tags so we know where they are.
  504. (dolist (it newtags)
  505. (semantic--tag-put-property it :filename
  506. newincfname))
  507. (setq includetags (nconc includetags newtags)))
  508. ;; ELSE - not recursive throttle
  509. (push (cons 'scanned-no-recurse
  510. (semantic-tag-clone (car includetags)))
  511. scannedincludes)
  512. )
  513. )
  514. (setq includetags (cdr includetags)))
  515. (setq semanticdb-find-lost-includes lostincludes)
  516. (setq semanticdb-find-scanned-include-tags (reverse scannedincludes))
  517. ;; Find all the omniscient databases for this major mode, and
  518. ;; add them if needed
  519. (when (and (semanticdb-find-throttle-active-p 'omniscience)
  520. semanticdb-search-system-databases)
  521. ;; We can append any mode-specific omniscience databases into
  522. ;; our search list here.
  523. (let ((systemdb semanticdb-project-system-databases)
  524. (ans nil))
  525. (while systemdb
  526. (setq ans (semanticdb-file-table
  527. (car systemdb)
  528. ;; I would expect most omniscient to return the same
  529. ;; thing regardless of filename, but we may have
  530. ;; one that can return a table of all things the
  531. ;; current file needs.
  532. (buffer-file-name (current-buffer))))
  533. (when (not (memq ans matchedtables))
  534. (setq matchedtables (cons ans matchedtables)))
  535. (setq systemdb (cdr systemdb))))
  536. )
  537. (nreverse matchedtables)))
  538. (define-overloadable-function semanticdb-find-load-unloaded (filename)
  539. "Create a database table for FILENAME if it hasn't been parsed yet.
  540. Assumes that FILENAME exists as a source file.
  541. Assumes that a preexisting table does not exist, even if it
  542. isn't in memory yet."
  543. (if (semanticdb-find-throttle-active-p 'unloaded)
  544. (:override)
  545. (semanticdb-file-table-object filename t)))
  546. (defun semanticdb-find-load-unloaded-default (filename)
  547. "Load an unloaded file in FILENAME using the default semanticdb loader."
  548. (semanticdb-file-table-object filename))
  549. ;; The creation of the overload occurs above.
  550. (defun semanticdb-find-table-for-include-default (includetag &optional table)
  551. "Default implementation of `semanticdb-find-table-for-include'.
  552. Uses `semanticdb-current-database-list' as the search path.
  553. INCLUDETAG and TABLE are documented in `semanticdb-find-table-for-include'.
  554. Included databases are filtered based on `semanticdb-find-default-throttle'."
  555. (if (not (eq (semantic-tag-class includetag) 'include))
  556. (signal 'wrong-type-argument (list includetag 'include)))
  557. (let ((name
  558. ;; Note, some languages (like Emacs or Java) use include tag names
  559. ;; that don't represent files! We want to have file names.
  560. (semantic-tag-include-filename includetag))
  561. (originfiledir nil)
  562. (roots nil)
  563. (tmp nil)
  564. (ans nil))
  565. ;; INCLUDETAG should have some way to reference where it came
  566. ;; from! If not, TABLE should provide the way. Each time we
  567. ;; look up a tag, we may need to find it in some relative way
  568. ;; and must set our current buffer eto the origin of includetag
  569. ;; or nothing may work.
  570. (setq originfiledir
  571. (cond ((semantic-tag-file-name includetag)
  572. ;; A tag may have a buffer, or a :filename property.
  573. (file-name-directory (semantic-tag-file-name includetag)))
  574. (table
  575. (file-name-directory (semanticdb-full-filename table)))
  576. (t
  577. ;; @todo - what to do here? Throw an error maybe
  578. ;; and fix usage bugs?
  579. default-directory)))
  580. (cond
  581. ;; Step 1: Relative path name
  582. ;;
  583. ;; If the name is relative, then it should be findable as relative
  584. ;; to the source file that this tag originated in, and be fast.
  585. ;;
  586. ((and (semanticdb-find-throttle-active-p 'local)
  587. (file-exists-p (expand-file-name name originfiledir)))
  588. (setq ans (semanticdb-find-load-unloaded
  589. (expand-file-name name originfiledir)))
  590. )
  591. ;; Step 2: System or Project level includes
  592. ;;
  593. ((or
  594. ;; First, if it a system include, we can investigate that tags
  595. ;; dependency file
  596. (and (semanticdb-find-throttle-active-p 'system)
  597. ;; Sadly, not all languages make this distinction.
  598. ;;(semantic-tag-include-system-p includetag)
  599. ;; Here, we get local and system files.
  600. (setq tmp (semantic-dependency-tag-file includetag))
  601. )
  602. ;; Second, project files are active, we and we have EDE,
  603. ;; we can find it using the same tool.
  604. (and (semanticdb-find-throttle-active-p 'project)
  605. ;; Make sure EDE is available, and we have a project
  606. (featurep 'ede) (ede-current-project originfiledir)
  607. ;; The EDE query is hidden in this call.
  608. (setq tmp (semantic-dependency-tag-file includetag))
  609. )
  610. )
  611. (setq ans (semanticdb-find-load-unloaded tmp))
  612. )
  613. ;; Somewhere in our project hierarchy
  614. ;;
  615. ;; Remember: Roots includes system databases which can create
  616. ;; specialized tables we can search.
  617. ;;
  618. ;; NOTE: Not used if EDE is active!
  619. ((and (semanticdb-find-throttle-active-p 'project)
  620. ;; And don't do this if it is a system include. Not supported by all languages,
  621. ;; but when it is, this is a nice fast way to skip this step.
  622. (not (semantic-tag-include-system-p includetag))
  623. ;; Don't do this if we have an EDE project.
  624. (not (and (featurep 'ede)
  625. ;; Note: We don't use originfiledir here because
  626. ;; we want to know about the source file we are
  627. ;; starting from.
  628. (ede-current-project)))
  629. )
  630. (setq roots (semanticdb-current-database-list))
  631. (while (and (not ans) roots)
  632. (let* ((ref (if (slot-boundp (car roots) 'reference-directory)
  633. (oref (car roots) reference-directory)))
  634. (fname (cond ((null ref) nil)
  635. ((file-exists-p (expand-file-name name ref))
  636. (expand-file-name name ref))
  637. ((file-exists-p (expand-file-name (file-name-nondirectory name) ref))
  638. (expand-file-name (file-name-nondirectory name) ref)))))
  639. (when (and ref fname)
  640. ;; There is an actual file. Grab it.
  641. (setq ans (semanticdb-find-load-unloaded fname)))
  642. ;; ELSE
  643. ;;
  644. ;; NOTE: We used to look up omniscient databases here, but that
  645. ;; is now handled one layer up.
  646. ;;
  647. ;; Missing: a database that knows where missing files are. Hmm.
  648. ;; perhaps I need an override function for that?
  649. )
  650. (setq roots (cdr roots))))
  651. )
  652. ans))
  653. ;;; Perform interactive tests on the path/search mechanisms.
  654. ;;
  655. ;;;###autoload
  656. (defun semanticdb-find-test-translate-path (&optional arg)
  657. "Call and output results of `semanticdb-find-translate-path'.
  658. With ARG non-nil, specify a BRUTISH translation.
  659. See `semanticdb-find-default-throttle' and `semanticdb-project-roots'
  660. for details on how this list is derived."
  661. (interactive "P")
  662. (semantic-fetch-tags)
  663. (require 'data-debug)
  664. (let ((start (current-time))
  665. (p (semanticdb-find-translate-path nil arg))
  666. (end (current-time))
  667. )
  668. (data-debug-new-buffer "*SEMANTICDB FTP ADEBUG*")
  669. (message "Search of tags took %.2f seconds."
  670. (semantic-elapsed-time start end))
  671. (data-debug-insert-stuff-list p "*")))
  672. (defun semanticdb-find-test-translate-path-no-loading (&optional arg)
  673. "Call and output results of `semanticdb-find-translate-path'.
  674. With ARG non-nil, specify a BRUTISH translation.
  675. See `semanticdb-find-default-throttle' and `semanticdb-project-roots'
  676. for details on how this list is derived."
  677. (interactive "P")
  678. (semantic-fetch-tags)
  679. (require 'data-debug)
  680. (let* ((semanticdb-find-default-throttle
  681. (if (featurep 'semantic/db-find)
  682. (remq 'unloaded semanticdb-find-default-throttle)
  683. nil))
  684. (start (current-time))
  685. (p (semanticdb-find-translate-path nil arg))
  686. (end (current-time))
  687. )
  688. (data-debug-new-buffer "*SEMANTICDB FTP ADEBUG*")
  689. (message "Search of tags took %.2f seconds."
  690. (semantic-elapsed-time start end))
  691. (data-debug-insert-stuff-list p "*")))
  692. ;;;###autoload
  693. (defun semanticdb-find-adebug-lost-includes ()
  694. "Translate the current path, then display the lost includes.
  695. Examines the variable `semanticdb-find-lost-includes'."
  696. (interactive)
  697. (require 'data-debug)
  698. (semanticdb-find-translate-path nil nil)
  699. (let ((lost semanticdb-find-lost-includes)
  700. )
  701. (if (not lost)
  702. (message "There are no unknown includes for %s"
  703. (buffer-name))
  704. (data-debug-new-buffer "*SEMANTICDB lost-includes ADEBUG*")
  705. ;; (data-debug-insert-tag-list lost "*")
  706. )))
  707. (defun semanticdb-find-adebug-insert-scanned-tag-cons (consdata prefix prebuttontext)
  708. "Insert a button representing scanned include CONSDATA.
  709. PREFIX is the text that precedes the button.
  710. PREBUTTONTEXT is some text between prefix and the overlay button."
  711. (let* ((start (point))
  712. (end nil)
  713. (mode (car consdata))
  714. (tag (cdr consdata))
  715. (name (semantic-tag-name tag))
  716. (file (semantic-tag-file-name tag))
  717. (str1 (format "%S %s" mode name))
  718. (str2 (format " : %s" file))
  719. (tip nil))
  720. (insert prefix prebuttontext str1)
  721. (setq end (point))
  722. (insert str2)
  723. (put-text-property start end 'face
  724. (cond ((eq mode 'scanned)
  725. 'font-lock-function-name-face)
  726. ((eq mode 'duplicate)
  727. 'font-lock-comment-face)
  728. ((eq mode 'lost)
  729. 'font-lock-variable-name-face)
  730. ((eq mode 'scanned-no-recurse)
  731. 'font-lock-type-face)))
  732. (put-text-property start end 'ddebug (cdr consdata))
  733. (put-text-property start end 'ddebug-indent(length prefix))
  734. (put-text-property start end 'ddebug-prefix prefix)
  735. (put-text-property start end 'help-echo tip)
  736. (put-text-property start end 'ddebug-function
  737. 'data-debug-insert-tag-parts-from-point)
  738. (insert "\n")
  739. )
  740. )
  741. (defun semanticdb-find-adebug-scanned-includes ()
  742. "Translate the current path, then display the lost includes.
  743. Examines the variable `semanticdb-find-lost-includes'."
  744. (interactive)
  745. (require 'data-debug)
  746. (semanticdb-find-translate-path nil nil)
  747. (let ((scanned semanticdb-find-scanned-include-tags)
  748. (data-debug-thing-alist
  749. (cons
  750. '((lambda (thing) (and (consp thing)
  751. (symbolp (car thing))
  752. (memq (car thing)
  753. '(scanned scanned-no-recurse
  754. lost duplicate))))
  755. . semanticdb-find-adebug-insert-scanned-tag-cons)
  756. data-debug-thing-alist))
  757. )
  758. (if (not scanned)
  759. (message "There are no includes scanned %s"
  760. (buffer-name))
  761. (data-debug-new-buffer "*SEMANTICDB scanned-includes ADEBUG*")
  762. (data-debug-insert-stuff-list scanned "*")
  763. )))
  764. ;;; API Functions
  765. ;;
  766. ;; Once you have a search result, use these routines to operate
  767. ;; on the search results at a higher level
  768. ;;;###autoload
  769. (defun semanticdb-strip-find-results (results &optional find-file-match)
  770. "Strip a semanticdb search RESULTS to exclude objects.
  771. This makes it appear more like the results of a `semantic-find-' call.
  772. Optional FIND-FILE-MATCH loads all files associated with RESULTS
  773. into buffers. This has the side effect of enabling `semantic-tag-buffer' to
  774. return a value.
  775. If FIND-FILE-MATCH is `name', then only the filename is stored
  776. in each tag instead of loading each file into a buffer.
  777. If the input RESULTS are not going to be used again, and if
  778. FIND-FILE-MATCH is nil, you can use `semanticdb-fast-strip-find-results'
  779. instead."
  780. (if find-file-match
  781. ;; Load all files associated with RESULTS.
  782. (let ((tmp results)
  783. (output nil))
  784. (while tmp
  785. (let ((tab (car (car tmp)))
  786. (tags (cdr (car tmp))))
  787. (dolist (T tags)
  788. ;; Normalization gives specialty database tables a chance
  789. ;; to convert into a more stable tag format.
  790. (let* ((norm (semanticdb-normalize-one-tag tab T))
  791. (ntab (car norm))
  792. (ntag (cdr norm))
  793. (nametable ntab))
  794. ;; If it didn't normalize, use what we had.
  795. (if (not norm)
  796. (setq nametable tab)
  797. (setq output (append output (list ntag))))
  798. ;; Find-file-match allows a tool to make sure the tag is
  799. ;; 'live', somewhere in a buffer.
  800. (cond ((eq find-file-match 'name)
  801. (or (semantic--tag-get-property ntag :filename)
  802. (let ((f (semanticdb-full-filename nametable)))
  803. (semantic--tag-put-property ntag :filename f))))
  804. ((and find-file-match ntab)
  805. (semanticdb-get-buffer ntab))
  806. )
  807. ))
  808. )
  809. (setq tmp (cdr tmp)))
  810. output)
  811. ;; @todo - I could use nconc, but I don't know what the caller may do with
  812. ;; RESULTS after this is called. Right now semantic-complete will
  813. ;; recycling the input after calling this routine.
  814. (apply #'append (mapcar #'cdr results))))
  815. (defun semanticdb-fast-strip-find-results (results)
  816. "Destructively strip a semanticdb search RESULTS to exclude objects.
  817. This makes it appear more like the results of a `semantic-find-' call.
  818. This is like `semanticdb-strip-find-results', except the input list RESULTS
  819. will be changed."
  820. (mapcan #'cdr results))
  821. (defun semanticdb-find-results-p (resultp)
  822. "Non-nil if RESULTP is in the form of a semanticdb search result.
  823. This query only really tests the first entry in the list that is RESULTP,
  824. but should be good enough for debugging assertions."
  825. (and (listp resultp)
  826. (listp (car resultp))
  827. (semanticdb-abstract-table-child-p (car (car resultp)))
  828. (or (semantic-tag-p (car (cdr (car resultp))))
  829. (null (car (cdr (car resultp)))))))
  830. (defun semanticdb-find-result-prin1-to-string (result)
  831. "Presuming RESULT satisfies `semanticdb-find-results-p', provide a short PRIN1 output."
  832. (if (< (length result) 2)
  833. (concat "#<FIND RESULT "
  834. (mapconcat (lambda (a)
  835. (concat "(" (eieio-object-name (car a) ) " . "
  836. "#<TAG LIST " (number-to-string (length (cdr a))) ">)"))
  837. result
  838. " ")
  839. ">")
  840. ;; Longer results should have an abbreviated form.
  841. (format "#<FIND RESULT %d TAGS in %d FILES>"
  842. (semanticdb-find-result-length result)
  843. (length result))))
  844. (defun semanticdb-find-result-with-nil-p (resultp)
  845. "Non-nil of RESULTP is in the form of a semanticdb search result.
  846. The value nil is valid where a TABLE usually is, but only if the TAG
  847. results include overlays.
  848. This query only really tests the first entry in the list that is RESULTP,
  849. but should be good enough for debugging assertions."
  850. (and (listp resultp)
  851. (listp (car resultp))
  852. (let ((tag-to-test (car-safe (cdr (car resultp)))))
  853. (or (and (semanticdb-abstract-table-child-p (car (car resultp)))
  854. (or (semantic-tag-p tag-to-test)
  855. (null tag-to-test)))
  856. (and (null (car (car resultp)))
  857. (or (semantic-tag-with-position-p tag-to-test)
  858. (null tag-to-test))))
  859. )))
  860. ;;;###autoload
  861. (defun semanticdb-find-result-length (result)
  862. "Number of tags found in RESULT."
  863. (let ((count 0))
  864. (mapc (lambda (onetable)
  865. (setq count (+ count (1- (length onetable)))))
  866. result)
  867. count))
  868. ;;;###autoload
  869. (defun semanticdb-find-result-nth (result n)
  870. "In RESULT, return the Nth search result.
  871. This is a 0 based search result, with the first match being element 0.
  872. The returned value is a cons cell: (TAG . TABLE) where TAG
  873. is the tag at the Nth position. TABLE is the semanticdb table where
  874. the TAG was found. Sometimes TABLE can be nil."
  875. (let ((ans nil)
  876. (anstable nil))
  877. ;; Loop over each single table hit.
  878. (while (and (not ans) result)
  879. ;; For each table result, get local length, and modify
  880. ;; N to be that much less.
  881. (let ((ll (length (cdr (car result))))) ;; local length
  882. (if (> ll n)
  883. ;; We have a local match.
  884. (setq ans (nth n (cdr (car result)))
  885. anstable (car (car result)))
  886. ;; More to go. Decrement N.
  887. (setq n (- n ll))))
  888. ;; Keep moving.
  889. (setq result (cdr result)))
  890. (cons ans anstable)))
  891. (defun semanticdb-find-result-test (result)
  892. "Test RESULT by accessing all the tags in the list."
  893. (if (not (semanticdb-find-results-p result))
  894. (error "Does not pass `semanticdb-find-results-p.\n"))
  895. (let ((len (semanticdb-find-result-length result))
  896. (i 0))
  897. (while (< i len)
  898. (let ((tag (semanticdb-find-result-nth result i)))
  899. (if (not (semantic-tag-p (car tag)))
  900. (error "%d entry is not a tag" i)))
  901. (setq i (1+ i)))))
  902. ;;;###autoload
  903. (defun semanticdb-find-result-nth-in-buffer (result n)
  904. "In RESULT, return the Nth search result.
  905. Like `semanticdb-find-result-nth', except that only the TAG
  906. is returned, and the buffer it is found it will be made current.
  907. If the result tag has no position information, the originating buffer
  908. is still made current."
  909. (let* ((ret (semanticdb-find-result-nth result n))
  910. (ans (car ret))
  911. (anstable (cdr ret)))
  912. ;; If we have a hit, double-check the find-file
  913. ;; entry. If the file must be loaded, then gat that table's
  914. ;; source file into a buffer.
  915. (if anstable
  916. (let ((norm (semanticdb-normalize-one-tag anstable ans)))
  917. (when norm
  918. ;; The normalized tags can now be found based on that
  919. ;; tags table.
  920. (condition-case foo
  921. (progn
  922. (semanticdb-set-buffer (car norm))
  923. ;; Now reset ans
  924. (setq ans (cdr norm)))
  925. ;; Don't error for this case, but don't store
  926. ;; the thing either.
  927. (no-method-definition nil))
  928. ))
  929. )
  930. ;; Return the tag.
  931. ans))
  932. (defun semanticdb-find-result-mapc (fcn result)
  933. "Apply FCN to each element of find RESULT for side-effects only.
  934. FCN takes two arguments. The first is a TAG, and the
  935. second is a DB from whence TAG originated.
  936. Returns result."
  937. (mapc (lambda (sublst-icky)
  938. (mapc (lambda (tag-icky)
  939. (funcall fcn tag-icky (car sublst-icky)))
  940. (cdr sublst-icky)))
  941. result)
  942. result)
  943. ;;; Search Logging
  944. ;;
  945. ;; Basic logging to see what the search routines are doing.
  946. (defvar semanticdb-find-log-flag nil
  947. "Non-nil means log the process of searches.")
  948. (defvar semanticdb-find-log-buffer-name "*SemanticDB Find Log*"
  949. "The name of the logging buffer.")
  950. (defun semanticdb-find-toggle-logging ()
  951. "Toggle semanticdb logging."
  952. (interactive)
  953. (setq semanticdb-find-log-flag (null semanticdb-find-log-flag))
  954. (message "Semanticdb find logging is %sabled"
  955. (if semanticdb-find-log-flag "en" "dis")))
  956. (defun semanticdb-reset-log ()
  957. "Reset the log buffer."
  958. (interactive)
  959. (when semanticdb-find-log-flag
  960. (with-current-buffer (get-buffer-create semanticdb-find-log-buffer-name)
  961. (erase-buffer)
  962. )))
  963. (defun semanticdb-find-log-move-to-end ()
  964. "Move to the end of the semantic log."
  965. (let ((cb (current-buffer))
  966. (cw (selected-window)))
  967. (unwind-protect
  968. (progn
  969. (set-buffer semanticdb-find-log-buffer-name)
  970. (if (get-buffer-window (current-buffer) 'visible)
  971. (select-window (get-buffer-window (current-buffer) 'visible)))
  972. (goto-char (point-max)))
  973. (if cw (select-window cw))
  974. (set-buffer cb))))
  975. (defun semanticdb-find-log-new-search (forwhat)
  976. "Start a new search FORWHAT."
  977. (when semanticdb-find-log-flag
  978. (with-current-buffer (get-buffer-create semanticdb-find-log-buffer-name)
  979. (insert (format "New Search: %S\n" forwhat))
  980. )
  981. (semanticdb-find-log-move-to-end)))
  982. (defun semanticdb-find-log-activity (table result)
  983. "Log that TABLE has been searched and RESULT was found."
  984. (when semanticdb-find-log-flag
  985. (with-current-buffer semanticdb-find-log-buffer-name
  986. (insert "Table: " (object-print table)
  987. " Result: " (int-to-string (length result)) " tags"
  988. "\n")
  989. )
  990. (semanticdb-find-log-move-to-end)))
  991. ;;; Semanticdb find API functions
  992. ;; These are the routines actually used to perform searches.
  993. ;;
  994. (defun semanticdb-find-tags-collector (function &optional path find-file-match
  995. brutish)
  996. "Collect all tags returned by FUNCTION over PATH.
  997. The FUNCTION must take two arguments. The first is TABLE,
  998. which is a semanticdb table containing tags. The second argument
  999. to FUNCTION is TAGS. TAGS may be a list of tags. If TAGS is non-nil,
  1000. then FUNCTION should search the TAG list, not through TABLE.
  1001. See `semanticdb-find-translate-path' for details on PATH.
  1002. FIND-FILE-MATCH indicates that any time a match is found, the file
  1003. associated with that tag should be loaded into a buffer.
  1004. Note: You should leave FIND-FILE-MATCH as nil. It is far more
  1005. efficient to take the results from any search and use
  1006. `semanticdb-strip-find-results' instead. This argument is here
  1007. for backward compatibility.
  1008. If optional argument BRUTISH is non-nil, then ignore include statements,
  1009. and search all tables in this project tree."
  1010. (let (found match)
  1011. (save-current-buffer
  1012. ;; If path is a buffer, set ourselves up in that buffer
  1013. ;; so that the override methods work correctly.
  1014. (when (bufferp path) (set-buffer path))
  1015. (if (semanticdb-find-results-p path)
  1016. ;; When we get find results, loop over that.
  1017. (dolist (tableandtags path)
  1018. (semantic-throw-on-input 'semantic-find-translate-path)
  1019. ;; If FIND-FILE-MATCH is non-nil, skip tables of class
  1020. ;; `semanticdb-search-results-table', since those are system
  1021. ;; databases and not associated with a file.
  1022. (unless (and find-file-match
  1023. (obj-of-class-p
  1024. (car tableandtags) 'semanticdb-search-results-table))
  1025. (when (setq match (funcall function
  1026. (car tableandtags) (cdr tableandtags)))
  1027. (when find-file-match
  1028. (save-excursion (semanticdb-set-buffer (car tableandtags))))
  1029. (push (cons (car tableandtags) match) found)))
  1030. )
  1031. ;; Only log searches across data bases.
  1032. (semanticdb-find-log-new-search nil)
  1033. ;; If we get something else, scan the list of tables resulting
  1034. ;; from translating it into a list of objects.
  1035. (dolist (table (semanticdb-find-translate-path path brutish))
  1036. (semantic-throw-on-input 'semantic-find-translate-path)
  1037. ;; If FIND-FILE-MATCH is non-nil, skip tables of class
  1038. ;; `semanticdb-search-results-table', since those are system
  1039. ;; databases and not associated with a file.
  1040. (unless (and find-file-match
  1041. (obj-of-class-p table 'semanticdb-search-results-table))
  1042. (when (and table (setq match (funcall function table nil)))
  1043. (semanticdb-find-log-activity table match)
  1044. (when find-file-match
  1045. (save-excursion (semanticdb-set-buffer table)))
  1046. (push (cons table match) found))))))
  1047. ;; At this point, FOUND has had items pushed onto it.
  1048. ;; This means items are being returned in REVERSE order
  1049. ;; of the tables searched, so if you just get th CAR, then
  1050. ;; too-bad, you may have some system-tag that has no
  1051. ;; buffer associated with it.
  1052. ;; It must be reversed.
  1053. (nreverse found)))
  1054. ;;;###autoload
  1055. (defun semanticdb-find-tags-by-name (name &optional path find-file-match)
  1056. "Search for all tags matching NAME on PATH.
  1057. See `semanticdb-find-translate-path' for details on PATH.
  1058. FIND-FILE-MATCH indicates that any time a match is found, the file
  1059. associated with that tag should be loaded into a buffer."
  1060. (semanticdb-find-tags-collector
  1061. (lambda (table tags)
  1062. (semanticdb-find-tags-by-name-method table name tags))
  1063. path find-file-match))
  1064. ;;;###autoload
  1065. (defun semanticdb-find-tags-by-name-regexp (regexp &optional path find-file-match)
  1066. "Search for all tags matching REGEXP on PATH.
  1067. See `semanticdb-find-translate-path' for details on PATH.
  1068. FIND-FILE-MATCH indicates that any time a match is found, the file
  1069. associated with that tag should be loaded into a buffer."
  1070. (semanticdb-find-tags-collector
  1071. (lambda (table tags)
  1072. (semanticdb-find-tags-by-name-regexp-method table regexp tags))
  1073. path find-file-match))
  1074. ;;;###autoload
  1075. (defun semanticdb-find-tags-for-completion (prefix &optional path find-file-match)
  1076. "Search for all tags matching PREFIX on PATH.
  1077. See `semanticdb-find-translate-path' for details on PATH.
  1078. FIND-FILE-MATCH indicates that any time a match is found, the file
  1079. associated with that tag should be loaded into a buffer."
  1080. (semanticdb-find-tags-collector
  1081. (lambda (table tags)
  1082. (semanticdb-find-tags-for-completion-method table prefix tags))
  1083. path find-file-match))
  1084. ;;;###autoload
  1085. (defun semanticdb-find-tags-by-class (class &optional path find-file-match)
  1086. "Search for all tags of CLASS on PATH.
  1087. See `semanticdb-find-translate-path' for details on PATH.
  1088. FIND-FILE-MATCH indicates that any time a match is found, the file
  1089. associated with that tag should be loaded into a buffer."
  1090. (semanticdb-find-tags-collector
  1091. (lambda (table tags)
  1092. (semanticdb-find-tags-by-class-method table class tags))
  1093. path find-file-match))
  1094. ;;; Deep Searches
  1095. (defun semanticdb-deep-find-tags-by-name (name &optional path find-file-match)
  1096. "Search for all tags matching NAME on PATH.
  1097. Search also in all components of top level tags founds.
  1098. See `semanticdb-find-translate-path' for details on PATH.
  1099. FIND-FILE-MATCH indicates that any time a match is found, the file
  1100. associated with that tag should be loaded into a buffer."
  1101. (semanticdb-find-tags-collector
  1102. (lambda (table tags)
  1103. (semanticdb-deep-find-tags-by-name-method table name tags))
  1104. path find-file-match))
  1105. (defun semanticdb-deep-find-tags-by-name-regexp (regexp &optional path find-file-match)
  1106. "Search for all tags matching REGEXP on PATH.
  1107. Search also in all components of top level tags founds.
  1108. See `semanticdb-find-translate-path' for details on PATH.
  1109. FIND-FILE-MATCH indicates that any time a match is found, the file
  1110. associated with that tag should be loaded into a buffer."
  1111. (semanticdb-find-tags-collector
  1112. (lambda (table tags)
  1113. (semanticdb-deep-find-tags-by-name-regexp-method table regexp tags))
  1114. path find-file-match))
  1115. (defun semanticdb-deep-find-tags-for-completion (prefix &optional path find-file-match)
  1116. "Search for all tags matching PREFIX on PATH.
  1117. Search also in all components of top level tags founds.
  1118. See `semanticdb-find-translate-path' for details on PATH.
  1119. FIND-FILE-MATCH indicates that any time a match is found, the file
  1120. associated with that tag should be loaded into a buffer."
  1121. (semanticdb-find-tags-collector
  1122. (lambda (table tags)
  1123. (semanticdb-deep-find-tags-for-completion-method table prefix tags))
  1124. path find-file-match))
  1125. ;;; Brutish Search Routines
  1126. ;;
  1127. (defun semanticdb-brute-deep-find-tags-by-name (name &optional path find-file-match)
  1128. "Search for all tags matching NAME on PATH.
  1129. See `semanticdb-find-translate-path' for details on PATH.
  1130. The argument BRUTISH will be set so that searching includes all tables
  1131. in the current project.
  1132. FIND-FILE-MATCH indicates that any time a match is found, the file
  1133. associated wit that tag should be loaded into a buffer."
  1134. (semanticdb-find-tags-collector
  1135. (lambda (table tags)
  1136. (semanticdb-deep-find-tags-by-name-method table name tags))
  1137. path find-file-match t))
  1138. (defun semanticdb-brute-deep-find-tags-for-completion (prefix &optional path find-file-match)
  1139. "Search for all tags matching PREFIX on PATH.
  1140. See `semanticdb-find-translate-path' for details on PATH.
  1141. The argument BRUTISH will be set so that searching includes all tables
  1142. in the current project.
  1143. FIND-FILE-MATCH indicates that any time a match is found, the file
  1144. associated wit that tag should be loaded into a buffer."
  1145. (semanticdb-find-tags-collector
  1146. (lambda (table tags)
  1147. (semanticdb-deep-find-tags-for-completion-method table prefix tags))
  1148. path find-file-match t))
  1149. (defun semanticdb-brute-find-tags-by-class (class &optional path find-file-match)
  1150. "Search for all tags of CLASS on PATH.
  1151. See `semanticdb-find-translate-path' for details on PATH.
  1152. The argument BRUTISH will be set so that searching includes all tables
  1153. in the current project.
  1154. FIND-FILE-MATCH indicates that any time a match is found, the file
  1155. associated with that tag should be loaded into a buffer."
  1156. (semanticdb-find-tags-collector
  1157. (lambda (table tags)
  1158. (semanticdb-find-tags-by-class-method table class tags))
  1159. path find-file-match t))
  1160. ;;; Specialty Search Routines
  1161. (defun semanticdb-find-tags-external-children-of-type
  1162. (type &optional path find-file-match)
  1163. "Search for all tags defined outside of TYPE w/ TYPE as a parent.
  1164. See `semanticdb-find-translate-path' for details on PATH.
  1165. FIND-FILE-MATCH indicates that any time a match is found, the file
  1166. associated with that tag should be loaded into a buffer."
  1167. (semanticdb-find-tags-collector
  1168. (lambda (table tags)
  1169. (semanticdb-find-tags-external-children-of-type-method table type tags))
  1170. path find-file-match t))
  1171. (defun semanticdb-find-tags-subclasses-of-type
  1172. (type &optional path find-file-match)
  1173. "Search for all tags of class type defined that subclass TYPE.
  1174. See `semanticdb-find-translate-path' for details on PATH.
  1175. FIND-FILE-MATCH indicates that any time a match is found, the file
  1176. associated with that tag should be loaded into a buffer."
  1177. (semanticdb-find-tags-collector
  1178. (lambda (table tags)
  1179. (semanticdb-find-tags-subclasses-of-type-method table type tags))
  1180. path find-file-match t))
  1181. ;;; METHODS
  1182. ;;
  1183. ;; Default methods for semanticdb database and table objects.
  1184. ;; Override these with system databases to as new types of back ends.
  1185. ;;; Top level Searches
  1186. (cl-defmethod semanticdb-find-tags-by-name-method ((table semanticdb-abstract-table) name &optional tags)
  1187. "In TABLE, find all occurrences of tags with NAME.
  1188. Optional argument TAGS is a list of tags to search.
  1189. Returns a table of all matching tags."
  1190. (semantic-find-tags-by-name name (or tags (semanticdb-get-tags table))))
  1191. (cl-defmethod semanticdb-find-tags-by-name-regexp-method ((table semanticdb-abstract-table) regexp &optional tags)
  1192. "In TABLE, find all occurrences of tags matching REGEXP.
  1193. Optional argument TAGS is a list of tags to search.
  1194. Returns a table of all matching tags."
  1195. (semantic-find-tags-by-name-regexp regexp (or tags (semanticdb-get-tags table))))
  1196. (cl-defmethod semanticdb-find-tags-for-completion-method ((table semanticdb-abstract-table) prefix &optional tags)
  1197. "In TABLE, find all occurrences of tags matching PREFIX.
  1198. Optional argument TAGS is a list of tags to search.
  1199. Returns a table of all matching tags."
  1200. (semantic-find-tags-for-completion prefix (or tags (semanticdb-get-tags table))))
  1201. (cl-defmethod semanticdb-find-tags-by-class-method ((table semanticdb-abstract-table) class &optional tags)
  1202. "In TABLE, find all occurrences of tags of CLASS.
  1203. Optional argument TAGS is a list of tags to search.
  1204. Returns a table of all matching tags."
  1205. ;; Delegate 'include' to the overridable
  1206. ;; `semantic-find-tags-included', which by default will just call
  1207. ;; `semantic-find-tags-by-class'.
  1208. (if (eq class 'include)
  1209. (semantic-find-tags-included (or tags (semanticdb-get-tags table)))
  1210. (semantic-find-tags-by-class class (or tags (semanticdb-get-tags table)))))
  1211. (cl-defmethod semanticdb-find-tags-external-children-of-type-method ((table semanticdb-abstract-table) parent &optional tags)
  1212. "In TABLE, find all occurrences of tags whose parent is the PARENT type.
  1213. Optional argument TAGS is a list of tags to search.
  1214. Returns a table of all matching tags."
  1215. (require 'semantic/find)
  1216. (semantic-find-tags-external-children-of-type parent (or tags (semanticdb-get-tags table))))
  1217. (cl-defmethod semanticdb-find-tags-subclasses-of-type-method ((table semanticdb-abstract-table) parent &optional tags)
  1218. "In TABLE, find all occurrences of tags whose parent is the PARENT type.
  1219. Optional argument TAGS is a list of tags to search.
  1220. Returns a table of all matching tags."
  1221. (require 'semantic/find)
  1222. (semantic-find-tags-subclasses-of-type parent (or tags (semanticdb-get-tags table))))
  1223. ;;; Deep Searches
  1224. (cl-defmethod semanticdb-deep-find-tags-by-name-method ((table semanticdb-abstract-table) name &optional tags)
  1225. "In TABLE, find all occurrences of tags with NAME.
  1226. Search in all tags in TABLE, and all components of top level tags in
  1227. TABLE.
  1228. Optional argument TAGS is a list of tags to search.
  1229. Return a table of all matching tags."
  1230. (semantic-find-tags-by-name name (semantic-flatten-tags-table (or tags (semanticdb-get-tags table)))))
  1231. (cl-defmethod semanticdb-deep-find-tags-by-name-regexp-method ((table semanticdb-abstract-table) regexp &optional tags)
  1232. "In TABLE, find all occurrences of tags matching REGEXP.
  1233. Search in all tags in TABLE, and all components of top level tags in
  1234. TABLE.
  1235. Optional argument TAGS is a list of tags to search.
  1236. Return a table of all matching tags."
  1237. (semantic-find-tags-by-name-regexp regexp (semantic-flatten-tags-table (or tags (semanticdb-get-tags table)))))
  1238. (cl-defmethod semanticdb-deep-find-tags-for-completion-method ((table semanticdb-abstract-table) prefix &optional tags)
  1239. "In TABLE, find all occurrences of tags matching PREFIX.
  1240. Search in all tags in TABLE, and all components of top level tags in
  1241. TABLE.
  1242. Optional argument TAGS is a list of tags to search.
  1243. Return a table of all matching tags."
  1244. (semantic-find-tags-for-completion prefix (semantic-flatten-tags-table (or tags (semanticdb-get-tags table)))))
  1245. (provide 'semantic/db-find)
  1246. ;; Local variables:
  1247. ;; generated-autoload-file: "loaddefs.el"
  1248. ;; generated-autoload-load-name: "semantic/db-find"
  1249. ;; End:
  1250. ;;; semantic/db-find.el ends here