1 ;;; semantic/db-ebrowse.el --- Semanticdb backend using ebrowse.
3 ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
5 ;; Authors: Eric M. Ludlam <zappo@gnu.org>
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/>.
26 ;; This program was started by Eric Ludlam, and Joakim Verona finished
27 ;; the implementation by adding searches and fixing bugs.
29 ;; Read in custom-created ebrowse BROWSE files into a semanticdb back
32 ;; Add these databases to the 'system' search.
33 ;; Possibly use ebrowse for local parsing too.
35 ;; When real details are needed out of the tag system from ebrowse,
36 ;; we will need to delve into the originating source and parse those
37 ;; files the usual way.
40 ;; `semanticdb-create-ebrowse-database' - Call EBROWSE to create a
41 ;; system database for some directory. In general, use this for
42 ;; system libraries, such as /usr/include, or include directories
43 ;; large software projects.
44 ;; Customize `semanticdb-ebrowse-file-match' to make sure the correct
45 ;; file extensions are matched.
47 ;; `semanticdb-load-ebrowse-caches' - Load all the EBROWSE caches from
48 ;; your semanticdb system database directory. Once they are
49 ;; loaded, they become searchable as omnipotent databases for
50 ;; all C++ files. This is called automatically by semantic-load.
51 ;; Call it a second time to refresh the Emacs DB with the file.
56 (require 'semantic
/db-file
)
59 ;; For generic function searching.
62 (require 'semantic
/find
))
64 (declare-function semantic-add-system-include
"semantic/dep")
67 (defvar semanticdb-ebrowse-default-file-name
"BROWSE"
68 "The EBROWSE file name used for system caches.")
70 (defcustom semanticdb-ebrowse-file-match
"\\.\\(hh?\\|HH?\\|hpp\\)"
71 "Regular expression matching file names for ebrowse to parse.
72 This expression should exclude C++ headers that have no extension.
73 By default, include only headers since the semantic use of EBrowse
74 is only for searching via semanticdb, and thus only headers would
79 ;;; SEMANTIC Database related Code
81 (defclass semanticdb-table-ebrowse
(semanticdb-table)
82 ((major-mode :initform c
++-mode
)
83 (ebrowse-tree :initform nil
84 :initarg
:ebrowse-tree
86 "The raw ebrowse tree for this file."
88 (global-extract :initform nil
89 :initarg
:global-extract
91 "Table of ebrowse tags specific to this file.
92 This table is composited from the ebrowse *Globals* section.")
94 "A table for returning search results from ebrowse.")
96 (defclass semanticdb-project-database-ebrowse
97 (semanticdb-project-database)
98 ((new-table-class :initform semanticdb-table-ebrowse
101 "New tables created for this database are of this class.")
102 (system-include-p :initform nil
103 :initarg
:system-include
105 "Flag indicating this database represents a system include directory.")
106 (ebrowse-struct :initform nil
107 :initarg
:ebrowse-struct
110 "Semantic Database deriving tags using the EBROWSE tool.
111 EBROWSE is a C/C++ parser for use with `ebrowse' Emacs program.")
114 (defun semanticdb-ebrowse-C-file-p (file)
115 "Is FILE a C or C++ file?"
116 (or (string-match semanticdb-ebrowse-file-match file
)
117 (and (string-match "/\\w+$" file
)
118 (not (file-directory-p file
))
119 (let ((tmp (get-buffer-create "*semanticdb-ebrowse-tmp*")))
120 (with-current-buffer tmp
122 (insert-file-contents file nil
0 100 t
)
123 (error (insert-file-contents file nil nil nil t
)))
124 (goto-char (point-min))
125 (looking-at "\\s-*/\\(\\*\\|/\\)")
129 (defun semanticdb-create-ebrowse-database (dir)
130 "Create an EBROWSE database for directory DIR.
131 The database file is stored in ~/.semanticdb, or whichever directory
132 is specified by `semanticdb-default-save-directory'."
133 (interactive "DDirectory: ")
134 (setq dir
(file-name-as-directory dir
)) ;; for / on end
135 (let* ((savein (semanticdb-ebrowse-file-for-directory dir
))
136 (filebuff (get-buffer-create "*SEMANTICDB EBROWSE TMP*"))
137 (files (directory-files (expand-file-name dir
) t
))
138 (mma auto-mode-alist
)
141 ;; Create the input to the ebrowse command
142 (with-current-buffer filebuff
143 (buffer-disable-undo filebuff
)
144 (setq default-directory
(expand-file-name dir
))
146 ;;; @TODO - convert to use semanticdb-collect-matching-filenames
147 ;; to get the file names.
151 (when (semanticdb-ebrowse-C-file-p f
)
155 ;; Cleanup the ebrowse output buffer.
156 (with-current-buffer (get-buffer-create "*EBROWSE OUTPUT*")
158 ;; Call the EBROWSE command.
159 (message "Creating ebrowse file: %s ..." savein
)
160 (call-process-region (point-min) (point-max)
161 "ebrowse" nil
"*EBROWSE OUTPUT*" nil
162 (concat "--output-file=" savein
)
165 ;; Create a short LOADER program for loading in this database.
166 (let* ((lfn (concat savein
"-load.el"))
167 (lf (find-file-noselect lfn
)))
168 (with-current-buffer lf
170 (insert "(semanticdb-ebrowse-load-helper \""
171 (expand-file-name dir
)
174 (kill-buffer (current-buffer)))
175 (message "Creating ebrowse file: %s ... done" savein
)
176 ;; Reload that database
180 (defun semanticdb-load-ebrowse-caches ()
181 "Load all semanticdb controlled EBROWSE caches."
183 (let ((f (directory-files semanticdb-default-save-directory
184 t
(concat semanticdb-ebrowse-default-file-name
"-load.el$") t
)))
190 (defun semanticdb-ebrowse-load-helper (directory)
191 "Create the semanticdb database via ebrowse for directory.
192 If DIRECTORY is found to be defunct, it won't load the DB, and will
194 (if (file-directory-p directory
)
195 (semanticdb-create-database semanticdb-project-database-ebrowse
197 (let* ((BF (semanticdb-ebrowse-file-for-directory directory
))
198 (BFL (concat BF
"-load.el"))
199 (BFLB (concat BF
"-load.el~")))
200 (save-window-excursion
201 (with-output-to-temp-buffer "*FILES TO DELETE*"
202 (princ "The following BROWSE files are obsolete.\n\n")
207 (when (file-exists-p BFLB
)
211 (when (y-or-n-p (format
212 "Warning: Obsolete BROWSE file for: %s\nDelete? "
216 (when (file-exists-p BFLB
)
220 ;JAVE this just instantiates a default empty ebrowse struct?
221 ; how would new instances wind up here?
222 ; the ebrowse class isn't singleton, unlike the emacs lisp one
223 (defvar-mode-local c
++-mode semanticdb-project-system-databases
225 "Search Ebrowse for symbols.")
227 (defmethod semanticdb-needs-refresh-p ((table semanticdb-table-ebrowse
))
228 "EBROWSE database do not need to be refreshed.
230 JAVE: stub for needs-refresh, because, how do we know if BROWSE files
233 EML: Our database should probably remember the timestamp/checksum of
234 the most recently read EBROWSE file, and use that."
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; These routines deal with part of the ebrowse interface.
246 (defun semanticdb-ebrowse-file-for-directory (dir)
247 "Return the file name for DIR where the ebrowse BROWSE file is.
248 This file should reside in `semanticdb-default-save-directory'."
249 (let* ((semanticdb-default-save-directory
250 semanticdb-default-save-directory
)
251 (B (semanticdb-file-name-directory
252 'semanticdb-project-database-file
253 (concat (expand-file-name dir
)
254 semanticdb-ebrowse-default-file-name
)))
258 (defun semanticdb-ebrowse-get-ebrowse-structure (dir)
259 "Return the ebrowse structure for directory DIR.
260 This assumes semantic manages the BROWSE files, so they are assumed to live
261 where semantic cache files live, depending on your settings.
263 For instance: /home/<username>/.semanticdb/!usr!include!BROWSE"
264 (let* ((B (semanticdb-ebrowse-file-for-directory dir
))
265 (buf (get-buffer-create "*semanticdb ebrowse*")))
266 (message "semanticdb-ebrowse %s" B
)
267 (when (file-exists-p B
)
269 (buffer-disable-undo buf
)
271 (insert-file-contents B
)
273 (efcn (symbol-function 'ebrowse-show-progress
)))
274 (fset 'ebrowse-show-progress
#'(lambda (&rest junk
) nil
))
275 (unwind-protect ;; Protect against errors w/ ebrowse
276 (setq ans
(list B
(ebrowse-read)))
277 ;; These items must always happen
279 (fset 'ebrowse-show-fcn efcn
)
283 ;;; Methods for creating a database or tables
285 (defmethod semanticdb-create-database :STATIC
((dbeC semanticdb-project-database-ebrowse
)
287 "Create a new semantic database for DIRECTORY based on ebrowse.
288 If there is no database for DIRECTORY available, then
289 {not implemented yet} create one. Return nil if that is not possible."
290 ;; MAKE SURE THAT THE FILE LOADED DOESN'T ALREADY EXIST.
291 (require 'semantic
/dep
)
292 (let ((dbs semanticdb-database-list
)
294 (while (and (not found
) dbs
)
295 (when (semanticdb-project-database-ebrowse-p (car dbs
))
296 (when (string= (oref (car dbs
) reference-directory
) directory
)
297 (setq found
(car dbs
))))
298 (setq dbs
(cdr dbs
)))
299 ;;STATIC means DBE can't be used as object, only as a class
300 (let* ((ebrowse-data (semanticdb-ebrowse-get-ebrowse-structure directory
))
301 (dat (car (cdr ebrowse-data
)))
304 (default-directory directory
)
308 (setq db
(make-instance
313 (oset db reference-directory directory
))
315 ;; Once we recycle or make a new DB, refresh the
316 ;; contents from the BROWSE file.
318 ;; only possible after object creation, tables inited to nil.
319 (semanticdb-ebrowse-strip-trees db dat
)
321 ;; Once our database is loaded, if we are a system DB, we
322 ;; add ourselves to the include list for C++.
323 (semantic-add-system-include directory
'c
++-mode
)
324 (semantic-add-system-include directory
'c-mode
)
328 (defmethod semanticdb-ebrowse-strip-trees ((dbe semanticdb-project-database-ebrowse
)
330 "For the ebrowse database DBE, strip all tables from DATA."
331 ;JAVE what it actually seems to do is split the original tree in "tables" associated with files
332 ; im not sure it actually works:
333 ; the filename slot sometimes gets to be nil,
334 ; apparently for classes which definition can't be found, yet needs to be included in the tree
335 ; like library baseclasses
336 ; a file can define several classes
337 (let ((T (car (cdr data
))));1st comes a header, then the tree
340 (let* ((tree (car T
))
341 (class (ebrowse-ts-class tree
)); root class of tree
342 ;; Something funny going on with this file thing...
343 (filename (or (ebrowse-cs-source-file class
)
344 (ebrowse-cs-file class
)))
347 ((ebrowse-globals-tree-p tree
)
348 ;; We have the globals tree.. save this special.
349 (semanticdb-ebrowse-add-globals-to-table dbe tree
)
352 ;; ebrowse will collect all the info from multiple files
353 ;; into one tree. Semantic wants all the bits to be tied
354 ;; into different files. We need to do a full dissociation
355 ;; into semantic parsable tables.
356 (semanticdb-ebrowse-add-tree-to-table dbe tree
)
361 ;;; Filename based methods
363 (defun semanticdb-ebrowse-add-globals-to-table (dbe tree
)
364 "For database DBE, add the ebrowse TREE into the table."
365 (if (or (not (ebrowse-ts-p tree
))
366 (not (ebrowse-globals-tree-p tree
)))
367 (signal 'wrong-type-argument
(list 'ebrowse-ts-p tree
)))
369 (let* ((class (ebrowse-ts-class tree
))
370 (fname (or (ebrowse-cs-source-file class
)
371 (ebrowse-cs-file class
)
372 ;; Not def'd here, assume our current
374 (concat default-directory
"/unknown-proxy.hh")))
375 (vars (ebrowse-ts-member-functions tree
))
376 (fns (ebrowse-ts-member-variables tree
))
380 (let ((nt (semantic-tag (ebrowse-ms-name (car vars
))
382 (defpoint (ebrowse-bs-point class
)))
384 (semantic--tag-set-overlay nt
385 (vector defpoint defpoint
)))
386 (setq toks
(cons nt toks
)))
387 (setq vars
(cdr vars
)))
389 (let ((nt (semantic-tag (ebrowse-ms-name (car fns
))
391 (defpoint (ebrowse-bs-point class
)))
393 (semantic--tag-set-overlay nt
394 (vector defpoint defpoint
)))
395 (setq toks
(cons nt toks
)))
396 (setq fns
(cdr fns
)))
400 (defun semanticdb-ebrowse-add-tree-to-table (dbe tree
&optional fname baseclasses
)
401 "For database DBE, add the ebrowse TREE into the table for FNAME.
402 Optional argument BASECLASSES specifies a baseclass to the tree being provided."
403 (if (not (ebrowse-ts-p tree
))
404 (signal 'wrong-type-argument
(list 'ebrowse-ts-p tree
)))
406 ;; Strategy overview:
407 ;; 1) Calculate the filename for this tree.
408 ;; 2) Find a matching namespace in TAB, or create a new one.
409 ;; 3) Fabricate a tag proxy for CLASS
410 ;; 4) Add it to the namespace
413 ;; 1 - Find the filename
415 (setq fname
(or (ebrowse-cs-source-file (ebrowse-ts-class tree
))
416 (ebrowse-cs-file (ebrowse-ts-class tree
))
417 ;; Not def'd here, assume our current
419 (concat default-directory
"/unknown-proxy.hh"))))
421 (let* ((tab (or (semanticdb-file-table dbe fname
)
422 (semanticdb-create-table dbe fname
)))
423 (class (ebrowse-ts-class tree
))
424 (scope (ebrowse-cs-scope class
))
425 (ns (when scope
(split-string scope
":" t
)))
430 ;; 2 - Get the namespace tag
432 (let ((taglst (if (slot-boundp tab
'tags
) (oref tab tags
) nil
)))
433 (setq nst
(semantic-find-first-tag-by-name (car ns
) taglst
))
435 (setq nst
(semantic-tag (car ns
) 'type
:type
"namespace"))
436 (oset tab tags
(cons nst taglst
))
439 ;; 3 - Create a proxy tg.
440 (setq cls
(semantic-tag (ebrowse-cs-name class
)
443 :superclasses baseclasses
447 (let ((defpoint (ebrowse-bs-point class
)))
449 (semantic--tag-set-overlay cls
450 (vector defpoint defpoint
))))
452 ;; 4 - add to namespace
454 (semantic-tag-put-attribute
455 nst
:members
(cons cls
(semantic-tag-get-attribute nst
:members
)))
456 (oset tab tags
(cons cls
(when (slot-boundp tab
'tags
)
460 (let* ((subclass (ebrowse-ts-subclasses tree
))
461 (pname (ebrowse-cs-name class
)))
462 (when (ebrowse-cs-scope class
)
463 (setq pname
(concat (mapconcat (lambda (a) a
) (cdr ns
) "::") "::" pname
)))
466 (let* ((scc (ebrowse-ts-class (car subclass
)))
467 (fname (or (ebrowse-cs-source-file scc
)
468 (ebrowse-cs-file scc
)
469 ;; Not def'd here, assume our current
474 (semanticdb-ebrowse-add-tree-to-table
475 dbe
(car subclass
) fname pname
)))
476 (setq subclass
(cdr subclass
))))
480 ;; Overload for converting the simple faux tag into something better.
482 (defmethod semanticdb-normalize-tags ((obj semanticdb-table-ebrowse
) tags
)
483 "Convert in Ebrowse database OBJ a list of TAGS into a complete tag.
484 The default tag provided by searches exclude many features of a
485 semantic parsed tag. Look up the file for OBJ, and match TAGS
486 against a semantic parsed tag that has all the info needed, and
490 ;; SemanticDB will automatically create a regular database
491 ;; on top of the file just loaded by ebrowse during the set
492 ;; buffer. Fetch that table, and use it's tag list to look
493 ;; up the tag we just got, and thus turn it into a full semantic
496 (let ((tag (car tags
)))
498 (semanticdb-set-buffer obj
)
500 ;; Gee, it would be nice to do this, but ebrowse LIES. Oi.
501 (when (semantic-tag-with-position-p tag
)
502 (goto-char (semantic-tag-start tag
))
503 (let ((foundtag (semantic-current-tag)))
504 ;; Make sure the discovered tag is the same as what we started with.
505 (when (string= (semantic-tag-name tag
)
506 (semantic-tag-name foundtag
))
508 (setq ans foundtag
))))
509 ;; Sometimes ebrowse lies. Do a generic search
510 ;; to find it within this file.
512 ;; We might find multiple hits for this tag, and we have no way
513 ;; of knowing which one the user wanted. Return the first one.
514 (setq ans
(semantic-deep-find-tags-by-name
515 (semantic-tag-name tag
)
516 (semantic-fetch-tags))))
517 (if (semantic-tag-p ans
)
518 (setq tagret
(cons ans tagret
))
519 (setq tagret
(append ans tagret
)))
521 (setq tags
(cdr tags
))))
524 (defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-ebrowse
) tag
)
525 "Convert in Ebrowse database OBJ one TAG into a complete tag.
526 The default tag provided by searches exclude many features of a
527 semantic parsed tag. Look up the file for OBJ, and match TAG
528 against a semantic parsed tag that has all the info needed, and
532 ;; SemanticDB will automatically create a regular database
533 ;; on top of the file just loaded by ebrowse during the set
534 ;; buffer. Fetch that table, and use it's tag list to look
535 ;; up the tag we just got, and thus turn it into a full semantic
538 (semanticdb-set-buffer obj
)
539 (setq objret semanticdb-current-table
)
544 ;; Gee, it would be nice to do this, but ebrowse LIES. Oi.
545 (when (semantic-tag-with-position-p tag
)
546 (goto-char (semantic-tag-start tag
))
547 (let ((foundtag (semantic-current-tag)))
548 ;; Make sure the discovered tag is the same as what we started with.
549 (when (string= (semantic-tag-name tag
)
550 (semantic-tag-name foundtag
))
552 (setq ans foundtag
))))
553 ;; Sometimes ebrowse lies. Do a generic search
554 ;; to find it within this file.
556 ;; We might find multiple hits for this tag, and we have no way
557 ;; of knowing which one the user wanted. Return the first one.
558 (setq ans
(semantic-deep-find-tags-by-name
559 (semantic-tag-name tag
)
560 (semantic-fetch-tags))))
561 (if (semantic-tag-p ans
)
563 (setq tagret
(car ans
)))
565 (cons objret tagret
)))
569 ;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining
570 ;; how your new search routines are implemented.
572 (defmethod semanticdb-find-tags-by-name-method
573 ((table semanticdb-table-ebrowse
) name
&optional tags
)
574 "Find all tags named NAME in TABLE.
575 Return a list of tags."
576 ;;(message "semanticdb-find-tags-by-name-method name -- %s" name)
578 ;; If TAGS are passed in, then we don't need to do work here.
580 ;; If we ever need to do something special, add here.
581 ;; Since ebrowse tags are converted into semantic tags, we can
582 ;; get away with this sort of thing.
587 (defmethod semanticdb-find-tags-by-name-regexp-method
588 ((table semanticdb-table-ebrowse
) regex
&optional tags
)
589 "Find all tags with name matching REGEX in TABLE.
590 Optional argument TAGS is a list of tags to search.
591 Return a list of tags."
592 (if tags
(call-next-method)
593 ;; YOUR IMPLEMENTATION HERE
597 (defmethod semanticdb-find-tags-for-completion-method
598 ((table semanticdb-table-ebrowse
) prefix
&optional tags
)
599 "In TABLE, find all occurrences of tags matching PREFIX.
600 Optional argument TAGS is a list of tags to search.
601 Returns a table of all matching tags."
602 (if tags
(call-next-method)
603 ;; YOUR IMPLEMENTATION HERE
607 (defmethod semanticdb-find-tags-by-class-method
608 ((table semanticdb-table-ebrowse
) class
&optional tags
)
609 "In TABLE, find all occurrences of tags of CLASS.
610 Optional argument TAGS is a list of tags to search.
611 Returns a table of all matching tags."
612 (if tags
(call-next-method)
615 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
619 ;; If your language does not have a `deep' concept, these can be left
620 ;; alone, otherwise replace with implementations similar to those
624 (defmethod semanticdb-deep-find-tags-by-name-method
625 ((table semanticdb-table-ebrowse
) name
&optional tags
)
626 "Find all tags name NAME in TABLE.
627 Optional argument TAGS is a list of tags to search.
628 Like `semanticdb-find-tags-by-name-method' for ebrowse."
629 ;;(semanticdb-find-tags-by-name-method table name tags)
632 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
633 ((table semanticdb-table-ebrowse
) regex
&optional tags
)
634 "Find all tags with name matching REGEX in TABLE.
635 Optional argument TAGS is a list of tags to search.
636 Like `semanticdb-find-tags-by-name-method' for ebrowse."
637 ;;(semanticdb-find-tags-by-name-regexp-method table regex tags)
640 (defmethod semanticdb-deep-find-tags-for-completion-method
641 ((table semanticdb-table-ebrowse
) prefix
&optional tags
)
642 "In TABLE, find all occurrences of tags matching PREFIX.
643 Optional argument TAGS is a list of tags to search.
644 Like `semanticdb-find-tags-for-completion-method' for ebrowse."
645 ;;(semanticdb-find-tags-for-completion-method table prefix tags)
648 ;;; Advanced Searches
650 (defmethod semanticdb-find-tags-external-children-of-type-method
651 ((table semanticdb-table-ebrowse
) type
&optional tags
)
652 "Find all nonterminals which are child elements of TYPE
653 Optional argument TAGS is a list of tags to search.
654 Return a list of tags."
655 (if tags
(call-next-method)
656 ;; Ebrowse collects all this type of stuff together for us.
657 ;; but we can't use it.... yet.
661 (provide 'semantic
/db-ebrowse
)
663 ;;; semantic/db-ebrowse.el ends here