1 ;;; db-el.el --- Semantic database extensions for Emacs Lisp
3 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 ;;; Free Software Foundation, Inc.
6 ;; Author: 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 ;; There are a lot of Emacs Lisp functions and variables available for
27 ;; the asking. This adds on to the semanticdb programming interface to
28 ;; allow all loaded Emacs Lisp functions to be queried via semanticdb.
30 ;; This allows you to use programs written for Semantic using the database
31 ;; to also work in Emacs Lisp with no compromises.
34 (require 'semantic
/db-search
)
36 ;; For generic function searching.
44 (defclass semanticdb-table-emacs-lisp
(semanticdb-abstract-table)
45 ((major-mode :initform emacs-lisp-mode
)
47 "A table for returning search results from Emacs.")
49 (defmethod semanticdb-refresh-table ((obj semanticdb-table-emacs-lisp
) &optional force
)
50 "Do not refresh Emacs Lisp table.
51 It does not need refreshing."
54 (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table-emacs-lisp
))
55 "Return nil, we never need a refresh."
58 (defclass semanticdb-project-database-emacs-lisp
59 (semanticdb-project-database eieio-singleton
)
60 ((new-table-class :initform semanticdb-table-emacs-lisp
63 "New tables created for this database are of this class.")
65 "Database representing Emacs core.")
67 ;; Create the database, and add it to searchable databases for Emacs Lisp mode.
68 (defvar-mode-local emacs-lisp-mode semanticdb-project-system-databases
70 (semanticdb-project-database-emacs-lisp "Emacs"))
71 "Search Emacs core for symbols.")
73 (defvar-mode-local emacs-lisp-mode semanticdb-find-default-throttle
74 '(project omniscience
)
75 "Search project files, then search this omniscience database.
76 It is not necessary to to system or recursive searching because of
77 the omniscience database.")
79 ;;; Filename based methods
81 (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-emacs-lisp
))
82 "For an Emacs Lisp database, there are no explicit tables.
83 Create one of our special tables that can act as an intermediary."
84 ;; We need to return something since there is always the "master table"
85 ;; The table can then answer file name type questions.
86 (when (not (slot-boundp obj
'tables
))
87 (let ((newtable (semanticdb-table-emacs-lisp "Emacs System Table")))
88 (oset obj tables
(list newtable
))
89 (oset newtable parent-db obj
)
90 (oset newtable tags nil
)
94 (defmethod semanticdb-file-table ((obj semanticdb-project-database-emacs-lisp
) filename
)
95 "From OBJ, return FILENAME's associated table object.
96 For Emacs Lisp, creates a specialized table."
97 (car (semanticdb-get-database-tables obj
))
100 (defmethod semanticdb-get-tags ((table semanticdb-table-emacs-lisp
))
101 "Return the list of tags belonging to TABLE."
102 ;; specialty table ? Probably derive tags at request time.
105 (defmethod semanticdb-equivalent-mode ((table semanticdb-table-emacs-lisp
) &optional buffer
)
106 "Return non-nil if TABLE's mode is equivalent to BUFFER.
107 Equivalent modes are specified by by `semantic-equivalent-major-modes'
111 (eq (or mode-local-active-mode major-mode
) 'emacs-lisp-mode
)))
113 (defmethod semanticdb-full-filename ((obj semanticdb-table-emacs-lisp
))
114 "Fetch the full filename that OBJ refers to.
115 For Emacs Lisp system DB, there isn't one."
120 (defmethod semanticdb-normalize-tags ((obj semanticdb-table-emacs-lisp
) tags
)
121 "Convert tags, originating from Emacs OBJ, into standardized form."
124 (let* ((ot (semanticdb-normalize-one-tag obj T
))
126 (setq newtags
(cons tag newtags
))))
127 ;; There is no promise to have files associated.
130 (defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-emacs-lisp
) tag
)
131 "Convert one TAG, originating from Emacs OBJ, into standardized form.
132 If Emacs cannot resolve this symbol to a particular file, then return nil."
133 ;; Here's the idea. For each tag, get the name, then use
134 ;; Emacs' `symbol-file' to get the source. Once we have that,
135 ;; we can use more typical semantic searching techniques to
136 ;; get a regularly parsed tag.
137 (let* ((type (cond ((semantic-tag-of-class-p tag
'function
)
139 ((semantic-tag-of-class-p tag
'variable
)
142 (sym (intern (semantic-tag-name tag
)))
143 (file (condition-case err
144 (symbol-file sym type
)
145 ;; Older [X]Emacs don't have a 2nd argument.
146 (error (symbol-file sym
))))
148 (if (or (not file
) (not (file-exists-p file
)))
149 ;; The file didn't exist. Return nil.
150 ;; We can't normalize this tag. Fake it out.
152 (when (string-match "\\.elc" file
)
153 (setq file
(concat (file-name-sans-extension file
)
155 (when (and (not (file-exists-p file
))
156 (file-exists-p (concat file
".gz")))
158 (setq file
(concat file
".gz"))))
160 (let* ((tab (semanticdb-file-table-object file
))
161 (alltags (semanticdb-get-tags tab
))
162 (newtags (semanticdb-find-tags-by-name-method
163 tab
(semantic-tag-name tag
)))
165 ;; Find the best match.
167 (when (semantic-tag-similar-p T tag
)
171 (setq match
(car newtags
)))
175 (defun semanticdb-elisp-sym-function-arglist (sym)
176 "Get the argument list for SYM.
177 Deal with all different forms of function.
178 This was snarfed out of eldoc."
180 (let ((sd (and (fboundp sym
)
181 (symbol-function sym
))))
184 (setq sd
(indirect-function sym
))
185 (error (setq sd nil
))))
187 (def (if (eq (car-safe prelim-def
) 'macro
)
190 (arglist (cond ((null def
) nil
)
191 ((byte-code-function-p def
)
192 ;; This is an eieio compatibility function.
193 ;; We depend on EIEIO, so use this.
194 (eieio-compiled-function-arglist def
))
195 ((eq (car-safe def
) 'lambda
)
200 (defun semanticdb-elisp-sym->tag
(sym &optional toktype
)
201 "Convert SYM into a semantic tag.
202 TOKTYPE is a hint to the type of tag desired."
204 (setq sym
(intern-soft sym
)))
206 (cond ((and (eq toktype
'function
) (fboundp sym
))
207 (semantic-tag-new-function
210 (semantic-elisp-desymbolify
211 (semanticdb-elisp-sym-function-arglist sym
)) ;; arg-list
212 :user-visible-flag
(condition-case nil
213 (interactive-form sym
)
216 ((and (eq toktype
'variable
) (boundp sym
))
217 (semantic-tag-new-variable
220 nil
;; value - ignore for now
222 ((and (eq toktype
'type
) (class-p sym
))
223 (semantic-tag-new-type
226 (semantic-elisp-desymbolify
227 (aref (class-v semanticdb-project-database
)
228 class-public-a
)) ;; slots
229 (semantic-elisp-desymbolify (class-parents sym
)) ;; parents
232 ;; Figure it out on our own.
234 (semanticdb-elisp-sym->tag sym
'type
))
236 (semanticdb-elisp-sym->tag sym
'function
))
238 (semanticdb-elisp-sym->tag sym
'variable
))
245 (defvar semanticdb-elisp-mapatom-collector nil
246 "Variable used to collect mapatoms output.")
248 (defmethod semanticdb-find-tags-by-name-method
249 ((table semanticdb-table-emacs-lisp
) name
&optional tags
)
250 "Find all tags name NAME in TABLE.
251 Uses `inter-soft' to match NAME to emacs symbols.
252 Return a list of tags."
253 (if tags
(call-next-method)
254 ;; No need to search. Use `intern-soft' which does the same thing for us.
255 (let* ((sym (intern-soft name
))
256 (fun (semanticdb-elisp-sym->tag sym
'function
))
257 (var (semanticdb-elisp-sym->tag sym
'variable
))
258 (typ (semanticdb-elisp-sym->tag sym
'type
))
261 (when (or fun var typ
)
262 ;; If the symbol is any of these things, build the search table.
263 (when var
(setq taglst
(cons var taglst
)))
264 (when typ
(setq taglst
(cons typ taglst
)))
265 (when fun
(setq taglst
(cons fun taglst
)))
269 (defmethod semanticdb-find-tags-by-name-regexp-method
270 ((table semanticdb-table-emacs-lisp
) regex
&optional tags
)
271 "Find all tags with name matching REGEX in TABLE.
272 Optional argument TAGS is a list of tags to search.
273 Uses `apropos-internal' to find matches.
274 Return a list of tags."
275 (if tags
(call-next-method)
276 (delq nil
(mapcar 'semanticdb-elisp-sym-
>tag
277 (apropos-internal regex
)))))
279 (defmethod semanticdb-find-tags-for-completion-method
280 ((table semanticdb-table-emacs-lisp
) prefix
&optional tags
)
281 "In TABLE, find all occurances of tags matching PREFIX.
282 Optional argument TAGS is a list of tags to search.
283 Returns a table of all matching tags."
284 (if tags
(call-next-method)
285 (delq nil
(mapcar 'semanticdb-elisp-sym-
>tag
286 (all-completions prefix obarray
)))))
288 (defmethod semanticdb-find-tags-by-class-method
289 ((table semanticdb-table-emacs-lisp
) class
&optional tags
)
290 "In TABLE, find all occurances of tags of CLASS.
291 Optional argument TAGS is a list of tags to search.
292 Returns a table of all matching tags."
293 (if tags
(call-next-method)
294 ;; We could implement this, but it could be messy.
299 ;; For Emacs Lisp deep searches are like top level searches.
300 (defmethod semanticdb-deep-find-tags-by-name-method
301 ((table semanticdb-table-emacs-lisp
) name
&optional tags
)
302 "Find all tags name NAME in TABLE.
303 Optional argument TAGS is a list of tags to search.
304 Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
305 (semanticdb-find-tags-by-name-method table name tags
))
307 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
308 ((table semanticdb-table-emacs-lisp
) regex
&optional tags
)
309 "Find all tags with name matching REGEX in TABLE.
310 Optional argument TAGS is a list of tags to search.
311 Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
312 (semanticdb-find-tags-by-name-regexp-method table regex tags
))
314 (defmethod semanticdb-deep-find-tags-for-completion-method
315 ((table semanticdb-table-emacs-lisp
) prefix
&optional tags
)
316 "In TABLE, find all occurances of tags matching PREFIX.
317 Optional argument TAGS is a list of tags to search.
318 Like `semanticdb-find-tags-for-completion-method' for Emacs Lisp."
319 (semanticdb-find-tags-for-completion-method table prefix tags
))
321 ;;; Advanced Searches
323 (defmethod semanticdb-find-tags-external-children-of-type-method
324 ((table semanticdb-table-emacs-lisp
) type
&optional tags
)
325 "Find all nonterminals which are child elements of TYPE
326 Optional argument TAGS is a list of tags to search.
327 Return a list of tags."
328 (if tags
(call-next-method)
329 ;; EIEIO is the only time this matters
330 (when (featurep 'eieio
)
331 (let* ((class (intern-soft type
))
334 (mapcar 'semanticdb-elisp-sym-
>tag
335 ;; Fancy eieio function that knows all about
336 ;; built in methods belonging to CLASS.
337 (eieio-all-generic-functions class
)))))
341 (provide 'semantic
/db-el
)
343 ;;; semanticdb-el.el ends here