1 ;;; semantic/db-ebrowse.el --- Semanticdb backend using ebrowse.
3 ;;; Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Authors: Eric M. Ludlam <zappo@gnu.org>, Joakim Verona
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This program was started by Eric Ludlam, and Joakim Verona finished
26 ;; the implementation by adding searches and fixing bugs.
28 ;; Read in custom-created ebrowse BROWSE files into a semanticdb back
31 ;; Add these databases to the 'system' search.
32 ;; Possibly use ebrowse for local parsing too.
34 ;; When real details are needed out of the tag system from ebrowse,
35 ;; we will need to delve into the originating source and parse those
36 ;; files the usual way.
39 ;; `semanticdb-create-ebrowse-database' - Call EBROWSE to create a
40 ;; system database for some directory. In general, use this for
41 ;; system libraries, such as /usr/include, or include directories
42 ;; large software projects.
43 ;; Customize `semanticdb-ebrowse-file-match' to make sure the correct
44 ;; file extensions are matched.
46 ;; `semanticdb-load-ebrowse-caches' - Load all the EBROWSE caches from
47 ;; your semanticdb system database directory. Once they are
48 ;; loaded, they become searchable as omnipotent databases for
49 ;; all C++ files. This is called automatically by semantic-load.
50 ;; Call it a second time to refresh the Emacs DB with the file.
55 (require 'semantic/db-file)
58 ;; For generic function searching.
61 (require 'semantic/find))
63 (declare-function semantic-add-system-include "semantic/dep")
66 (defvar semanticdb-ebrowse-default-file-name "BROWSE"
67 "The EBROWSE file name used for system caches.")
69 (defcustom semanticdb-ebrowse-file-match "\\.\\(hh?\\|HH?\\|hpp\\)"
70 "Regular expression matching file names for ebrowse to parse.
71 This expression should exclude C++ headers that have no extension.
72 By default, include only headers since the semantic use of EBrowse
73 is only for searching via semanticdb, and thus only headers would
78 ;;; SEMANTIC Database related Code
80 (defclass semanticdb-table-ebrowse (semanticdb-table)
81 ((major-mode :initform c++-mode)
82 (ebrowse-tree :initform nil
83 :initarg :ebrowse-tree
85 "The raw ebrowse tree for this file."
87 (global-extract :initform nil
88 :initarg :global-extract
90 "Table of ebrowse tags specific to this file.
91 This table is compisited from the ebrowse *Globals* section.")
93 "A table for returning search results from ebrowse.")
95 (defclass semanticdb-project-database-ebrowse
96 (semanticdb-project-database)
97 ((new-table-class :initform semanticdb-table-ebrowse
100 "New tables created for this database are of this class.")
101 (system-include-p :initform nil
102 :initarg :system-include
104 "Flag indicating this database represents a system include directory.")
105 (ebrowse-struct :initform nil
106 :initarg :ebrowse-struct
109 "Semantic Database deriving tags using the EBROWSE tool.
110 EBROWSE is a C/C++ parser for use with `ebrowse' Emacs program.")
113 (defun semanticdb-ebrowse-C-file-p (file)
114 "Is FILE a C or C++ file?"
115 (or (string-match semanticdb-ebrowse-file-match file)
116 (and (string-match "/\\w+$" file)
117 (not (file-directory-p file))
118 (let ((tmp (get-buffer-create "*semanticdb-ebrowse-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 EBROSE 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
143 (set-buffer filebuff)
144 (buffer-disable-undo filebuff)
145 (setq default-directory (expand-file-name dir))
147 ;;; @TODO - convert to use semanticdb-collect-matching-filenames
148 ;; to get the file names.
152 (when (semanticdb-ebrowse-C-file-p f)
156 ;; Cleanup the ebrowse output buffer.
158 (set-buffer (get-buffer-create "*EBROWSE OUTPUT*"))
160 ;; Call the EBROWSE command.
161 (message "Creating ebrowse file: %s ..." savein)
162 (call-process-region (point-min) (point-max)
163 "ebrowse" nil "*EBROWSE OUTPUT*" nil
164 (concat "--output-file=" savein)
167 ;; Create a short LOADER program for loading in this database.
168 (let* ((lfn (concat savein "-load.el"))
169 (lf (find-file-noselect lfn)))
173 (insert "(semanticdb-ebrowse-load-helper \""
174 (expand-file-name dir)
177 (kill-buffer (current-buffer)))
178 (message "Creating ebrowse file: %s ... done" savein)
179 ;; Reload that database
183 (defun semanticdb-load-ebrowse-caches ()
184 "Load all semanticdb controlled EBROWSE caches."
186 (let ((f (directory-files semanticdb-default-save-directory
187 t (concat semanticdb-ebrowse-default-file-name "-load.el$") t)))
193 (defun semanticdb-ebrowse-load-helper (directory)
194 "Create the semanticdb database via ebrowse for directory.
195 If DIRECTORY is found to be defunct, it won't load the DB, and will
197 (if (file-directory-p directory)
198 (semanticdb-create-database semanticdb-project-database-ebrowse
200 (let* ((BF (semanticdb-ebrowse-file-for-directory directory))
201 (BFL (concat BF "-load.el"))
202 (BFLB (concat BF "-load.el~")))
203 (save-window-excursion
204 (with-output-to-temp-buffer "*FILES TO DELETE*"
205 (princ "The following BROWSE files are obsolete.\n\n")
210 (when (file-exists-p BFLB)
214 (when (y-or-n-p (format
215 "Warning: Obsolete BROWSE file for: %s\nDelete? "
219 (when (file-exists-p BFLB)
223 ;JAVE this just instantiates a default empty ebrowse struct?
224 ; how would new instances wind up here?
225 ; the ebrowse class isnt singleton, unlike the emacs lisp one
226 (defvar-mode-local c++-mode semanticdb-project-system-databases
228 "Search Ebrowse for symbols.")
230 (defmethod semanticdb-needs-refresh-p ((table semanticdb-table-ebrowse))
231 "EBROWSE database do not need to be refreshed.
233 JAVE: stub for needs-refresh, because, how do we know if BROWSE files
236 EML: Our database should probably remember the timestamp/checksum of
237 the most recently read EBROWSE file, and use that."
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; These routines deal with part of the ebrowse interface.
249 (defun semanticdb-ebrowse-file-for-directory (dir)
250 "Return the file name for DIR where the ebrowse BROWSE file is.
251 This file should reside in `semanticdb-default-save-directory'."
252 (let* ((semanticdb-default-save-directory
253 semanticdb-default-save-directory)
254 (B (semanticdb-file-name-directory
255 'semanticdb-project-database-file
256 (concat (expand-file-name dir)
257 semanticdb-ebrowse-default-file-name)))
261 (defun semanticdb-ebrowse-get-ebrowse-structure (dir)
262 "Return the ebrowse structure for directory DIR.
263 This assumes semantic manages the BROWSE files, so they are assumed to live
264 where semantic cache files live, depending on your settings.
266 For instance: /home/<username>/.semanticdb/!usr!include!BROWSE"
267 (let* ((B (semanticdb-ebrowse-file-for-directory dir))
268 (buf (get-buffer-create "*semanticdb ebrowse*")))
269 (message "semanticdb-ebrowse %s" B)
270 (when (file-exists-p B)
272 (buffer-disable-undo buf)
274 (insert-file-contents B)
276 (efcn (symbol-function 'ebrowse-show-progress)))
277 (fset 'ebrowse-show-progress #'(lambda (&rest junk) nil))
278 (unwind-protect ;; Protect against errors w/ ebrowse
279 (setq ans (list B (ebrowse-read)))
280 ;; These items must always happen
282 (fset 'ebrowse-show-fcn efcn)
286 ;;; Methods for creating a database or tables
288 (defmethod semanticdb-create-database :STATIC ((dbeC semanticdb-project-database-ebrowse)
290 "Create a new semantic database for DIRECTORY based on ebrowse.
291 If there is no database for DIRECTORY available, then
292 {not implemented yet} create one. Return nil if that is not possible."
293 ;; MAKE SURE THAT THE FILE LOADED DOESN'T ALREADY EXIST.
294 (require 'semantic/dep)
295 (let ((dbs semanticdb-database-list)
297 (while (and (not found) dbs)
298 (when (semanticdb-project-database-ebrowse-p (car dbs))
299 (when (string= (oref (car dbs) reference-directory) directory)
300 (setq found (car dbs))))
301 (setq dbs (cdr dbs)))
302 ;;STATIC means DBE cant be used as object, only as a class
303 (let* ((ebrowse-data (semanticdb-ebrowse-get-ebrowse-structure directory))
304 (dat (car (cdr ebrowse-data)))
307 (default-directory directory)
311 (setq db (make-instance
316 (oset db reference-directory directory))
318 ;; Once we recycle or make a new DB, refresh the
319 ;; contents from the BROWSE file.
321 ;; only possible after object creation, tables inited to nil.
322 (semanticdb-ebrowse-strip-trees db dat)
324 ;; Once our database is loaded, if we are a system DB, we
325 ;; add ourselves to the include list for C++.
326 (semantic-add-system-include directory 'c++-mode)
327 (semantic-add-system-include directory 'c-mode)
331 (defmethod semanticdb-ebrowse-strip-trees ((dbe semanticdb-project-database-ebrowse)
333 "For the ebrowse database DBE, strip all tables from DATA."
334 ;JAVE what it actually seems to do is split the original tree in "tables" associated with files
335 ; im not sure it actually works:
336 ; the filename slot sometimes gets to be nil,
337 ; apparently for classes which definition cant be found, yet needs to be included in the tree
338 ; like library baseclasses
339 ; a file can define several classes
340 (let ((T (car (cdr data))));1st comes a header, then the tree
343 (let* ((tree (car T))
344 (class (ebrowse-ts-class tree)); root class of tree
345 ;; Something funny going on with this file thing...
346 (filename (or (ebrowse-cs-source-file class)
347 (ebrowse-cs-file class)))
350 ((ebrowse-globals-tree-p tree)
351 ;; We have the globals tree.. save this special.
352 (semanticdb-ebrowse-add-globals-to-table dbe tree)
355 ;; ebrowse will collect all the info from multiple files
356 ;; into one tree. Semantic wants all the bits to be tied
357 ;; into different files. We need to do a full dissociation
358 ;; into semantic parsable tables.
359 (semanticdb-ebrowse-add-tree-to-table dbe tree)
364 ;;; Filename based methods
366 (defun semanticdb-ebrowse-add-globals-to-table (dbe tree)
367 "For database DBE, add the ebrowse TREE into the table."
368 (if (or (not (ebrowse-ts-p tree))
369 (not (ebrowse-globals-tree-p tree)))
370 (signal 'wrong-type-argument (list 'ebrowse-ts-p tree)))
372 (let* ((class (ebrowse-ts-class tree))
373 (fname (or (ebrowse-cs-source-file class)
374 (ebrowse-cs-file class)
375 ;; Not def'd here, assume our current
377 (concat default-directory "/unknown-proxy.hh")))
378 (vars (ebrowse-ts-member-functions tree))
379 (fns (ebrowse-ts-member-variables tree))
383 (let ((nt (semantic-tag (ebrowse-ms-name (car vars))
385 (defpoint (ebrowse-bs-point class)))
387 (semantic--tag-set-overlay nt
388 (vector defpoint defpoint)))
389 (setq toks (cons nt toks)))
390 (setq vars (cdr vars)))
392 (let ((nt (semantic-tag (ebrowse-ms-name (car fns))
394 (defpoint (ebrowse-bs-point class)))
396 (semantic--tag-set-overlay nt
397 (vector defpoint defpoint)))
398 (setq toks (cons nt toks)))
399 (setq fns (cdr fns)))
403 (defun semanticdb-ebrowse-add-tree-to-table (dbe tree &optional fname baseclasses)
404 "For database DBE, add the ebrowse TREE into the table for FNAME.
405 Optional argument BASECLASSES specifyies a baseclass to the tree being provided."
406 (if (not (ebrowse-ts-p tree))
407 (signal 'wrong-type-argument (list 'ebrowse-ts-p tree)))
409 ;; Strategy overview:
410 ;; 1) Calculate the filename for this tree.
411 ;; 2) Find a matching namespace in TAB, or create a new one.
412 ;; 3) Fabricate a tag proxy for CLASS
413 ;; 4) Add it to the namespace
416 ;; 1 - Find the filename
418 (setq fname (or (ebrowse-cs-source-file (ebrowse-ts-class tree))
419 (ebrowse-cs-file (ebrowse-ts-class tree))
420 ;; Not def'd here, assume our current
422 (concat default-directory "/unknown-proxy.hh"))))
424 (let* ((tab (or (semanticdb-file-table dbe fname)
425 (semanticdb-create-table dbe fname)))
426 (class (ebrowse-ts-class tree))
427 (scope (ebrowse-cs-scope class))
428 (ns (when scope (split-string scope ":" t)))
433 ;; 2 - Get the namespace tag
435 (let ((taglst (if (slot-boundp tab 'tags) (oref tab tags) nil)))
436 (setq nst (semantic-find-first-tag-by-name (car ns) taglst))
438 (setq nst (semantic-tag (car ns) 'type :type "namespace"))
439 (oset tab tags (cons nst taglst))
442 ;; 3 - Create a proxy tg.
443 (setq cls (semantic-tag (ebrowse-cs-name class)
446 :superclasses baseclasses
450 (let ((defpoint (ebrowse-bs-point class)))
452 (semantic--tag-set-overlay cls
453 (vector defpoint defpoint))))
455 ;; 4 - add to namespace
457 (semantic-tag-put-attribute
458 nst :members (cons cls (semantic-tag-get-attribute nst :members)))
459 (oset tab tags (cons cls (when (slot-boundp tab 'tags)
463 (let* ((subclass (ebrowse-ts-subclasses tree))
464 (pname (ebrowse-cs-name class)))
465 (when (ebrowse-cs-scope class)
466 (setq pname (concat (mapconcat (lambda (a) a) (cdr ns) "::") "::" pname)))
469 (let* ((scc (ebrowse-ts-class (car subclass)))
470 (fname (or (ebrowse-cs-source-file scc)
471 (ebrowse-cs-file scc)
472 ;; Not def'd here, assume our current
477 (semanticdb-ebrowse-add-tree-to-table
478 dbe (car subclass) fname pname)))
479 (setq subclass (cdr subclass))))
483 ;; Overload for converting the simple faux tag into something better.
485 (defmethod semanticdb-normalize-tags ((obj semanticdb-table-ebrowse) tags)
486 "Convert in Ebrowse database OBJ a list of TAGS into a complete tag.
487 The default tag provided by searches exclude many features of a
488 semantic parsed tag. Look up the file for OBJ, and match TAGS
489 against a semantic parsed tag that has all the info needed, and
493 ;; SemanticDB will automatically create a regular database
494 ;; on top of the file just loaded by ebrowse during the set
495 ;; buffer. Fetch that table, and use it's tag list to look
496 ;; up the tag we just got, and thus turn it into a full semantic
499 (let ((tag (car tags)))
501 (semanticdb-set-buffer obj)
503 ;; Gee, it would be nice to do this, but ebrowse LIES. Oi.
504 (when (semantic-tag-with-position-p tag)
505 (goto-char (semantic-tag-start tag))
506 (let ((foundtag (semantic-current-tag)))
507 ;; Make sure the discovered tag is the same as what we started with.
508 (when (string= (semantic-tag-name tag)
509 (semantic-tag-name foundtag))
511 (setq ans foundtag))))
512 ;; Sometimes ebrowse lies. Do a generic search
513 ;; to find it within this file.
515 ;; We might find multiple hits for this tag, and we have no way
516 ;; of knowing which one the user wanted. Return the first one.
517 (setq ans (semantic-deep-find-tags-by-name
518 (semantic-tag-name tag)
519 (semantic-fetch-tags))))
520 (if (semantic-tag-p ans)
521 (setq tagret (cons ans tagret))
522 (setq tagret (append ans tagret)))
524 (setq tags (cdr tags))))
527 (defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-ebrowse) tag)
528 "Convert in Ebrowse database OBJ one TAG into a complete tag.
529 The default tag provided by searches exclude many features of a
530 semantic parsed tag. Look up the file for OBJ, and match TAG
531 against a semantic parsed tag that has all the info needed, and
535 ;; SemanticDB will automatically create a regular database
536 ;; on top of the file just loaded by ebrowse during the set
537 ;; buffer. Fetch that table, and use it's tag list to look
538 ;; up the tag we just got, and thus turn it into a full semantic
541 (semanticdb-set-buffer obj)
542 (setq objret semanticdb-current-table)
547 ;; Gee, it would be nice to do this, but ebrowse LIES. Oi.
548 (when (semantic-tag-with-position-p tag)
549 (goto-char (semantic-tag-start tag))
550 (let ((foundtag (semantic-current-tag)))
551 ;; Make sure the discovered tag is the same as what we started with.
552 (when (string= (semantic-tag-name tag)
553 (semantic-tag-name foundtag))
555 (setq ans foundtag))))
556 ;; Sometimes ebrowse lies. Do a generic search
557 ;; to find it within this file.
559 ;; We might find multiple hits for this tag, and we have no way
560 ;; of knowing which one the user wanted. Return the first one.
561 (setq ans (semantic-deep-find-tags-by-name
562 (semantic-tag-name tag)
563 (semantic-fetch-tags))))
564 (if (semantic-tag-p ans)
566 (setq tagret (car ans)))
568 (cons objret tagret)))
572 ;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining
573 ;; how your new search routines are implemented.
575 (defmethod semanticdb-find-tags-by-name-method
576 ((table semanticdb-table-ebrowse) name &optional tags)
577 "Find all tags named NAME in TABLE.
578 Return a list of tags."
579 ;;(message "semanticdb-find-tags-by-name-method name -- %s" name)
581 ;; If TAGS are passed in, then we don't need to do work here.
583 ;; If we ever need to do something special, add here.
584 ;; Since ebrowse tags are converted into semantic tags, we can
585 ;; get away with this sort of thing.
590 (defmethod semanticdb-find-tags-by-name-regexp-method
591 ((table semanticdb-table-ebrowse) regex &optional tags)
592 "Find all tags with name matching REGEX in TABLE.
593 Optional argument TAGS is a list of tags to search.
594 Return a list of tags."
595 (if tags (call-next-method)
596 ;; YOUR IMPLEMENTATION HERE
600 (defmethod semanticdb-find-tags-for-completion-method
601 ((table semanticdb-table-ebrowse) prefix &optional tags)
602 "In TABLE, find all occurances of tags matching PREFIX.
603 Optional argument TAGS is a list of tags to search.
604 Returns a table of all matching tags."
605 (if tags (call-next-method)
606 ;; YOUR IMPLEMENTATION HERE
610 (defmethod semanticdb-find-tags-by-class-method
611 ((table semanticdb-table-ebrowse) class &optional tags)
612 "In TABLE, find all occurances of tags of CLASS.
613 Optional argument TAGS is a list of tags to search.
614 Returns a table of all matching tags."
615 (if tags (call-next-method)
618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
622 ;; If your language does not have a `deep' concept, these can be left
623 ;; alone, otherwise replace with implementations similar to those
627 (defmethod semanticdb-deep-find-tags-by-name-method
628 ((table semanticdb-table-ebrowse) name &optional tags)
629 "Find all tags name NAME in TABLE.
630 Optional argument TAGS is a list of tags t
631 Like `semanticdb-find-tags-by-name-method' for ebrowse."
632 ;;(semanticdb-find-tags-by-name-method table name tags)
635 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
636 ((table semanticdb-table-ebrowse) regex &optional tags)
637 "Find all tags with name matching REGEX in TABLE.
638 Optional argument TAGS is a list of tags to search.
639 Like `semanticdb-find-tags-by-name-method' for ebrowse."
640 ;;(semanticdb-find-tags-by-name-regexp-method table regex tags)
643 (defmethod semanticdb-deep-find-tags-for-completion-method
644 ((table semanticdb-table-ebrowse) prefix &optional tags)
645 "In TABLE, find all occurances of tags matching PREFIX.
646 Optional argument TAGS is a list of tags to search.
647 Like `semanticdb-find-tags-for-completion-method' for ebrowse."
648 ;;(semanticdb-find-tags-for-completion-method table prefix tags)
651 ;;; Advanced Searches
653 (defmethod semanticdb-find-tags-external-children-of-type-method
654 ((table semanticdb-table-ebrowse) type &optional tags)
655 "Find all nonterminals which are child elements of TYPE
656 Optional argument TAGS is a list of tags to search.
657 Return a list of tags."
658 (if tags (call-next-method)
659 ;; Ebrowse collects all this type of stuff together for us.
660 ;; but we can't use it.... yet.
664 (provide 'semantic/db-ebrowse)
666 ;;; semantic/db-ebrowse.el ends here