Commit | Line | Data |
---|---|---|
f273dfc6 CY |
1 | ;;; db-el.el --- Semantic database extensions for Emacs Lisp |
2 | ||
3 | ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 | |
4 | ;;; Free Software Foundation, Inc. | |
5 | ||
6 | ;; Author: Eric M. Ludlam <zappo@gnu.org> | |
7 | ;; Keywords: tags | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ;; | |
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. | |
29 | ;; | |
30 | ;; This allows you to use programs written for Semantic using the database | |
31 | ;; to also work in Emacs Lisp with no compromises. | |
32 | ;; | |
33 | ||
34 | (require 'semantic/db-search) | |
35 | (eval-when-compile | |
36 | ;; For generic function searching. | |
37 | (require 'eieio) | |
38 | (require 'eieio-opt) | |
39 | (require 'eieio-base) | |
40 | ) | |
41 | ;;; Code: | |
42 | ||
43 | ;;; Classes: | |
44 | (defclass semanticdb-table-emacs-lisp (semanticdb-abstract-table) | |
45 | ((major-mode :initform emacs-lisp-mode) | |
46 | ) | |
47 | "A table for returning search results from Emacs.") | |
48 | ||
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." | |
52 | nil) | |
53 | ||
54 | (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table-emacs-lisp)) | |
55 | "Return nil, we never need a refresh." | |
56 | nil) | |
57 | ||
58 | (defclass semanticdb-project-database-emacs-lisp | |
59 | (semanticdb-project-database eieio-singleton) | |
60 | ((new-table-class :initform semanticdb-table-emacs-lisp | |
61 | :type class | |
62 | :documentation | |
63 | "New tables created for this database are of this class.") | |
64 | ) | |
65 | "Database representing Emacs core.") | |
66 | ||
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 | |
69 | (list | |
70 | (semanticdb-project-database-emacs-lisp "Emacs")) | |
71 | "Search Emacs core for symbols.") | |
72 | ||
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.") | |
78 | ||
79 | ;;; Filename based methods | |
80 | ;; | |
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) | |
91 | )) | |
92 | (call-next-method)) | |
93 | ||
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)) | |
98 | ) | |
99 | ||
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. | |
103 | nil) | |
104 | ||
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' | |
108 | local variable." | |
109 | (save-excursion | |
110 | (set-buffer buffer) | |
111 | (eq (or mode-local-active-mode major-mode) 'emacs-lisp-mode))) | |
112 | ||
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." | |
116 | nil) | |
117 | ||
118 | ;;; Conversion | |
119 | ;; | |
120 | (defmethod semanticdb-normalize-tags ((obj semanticdb-table-emacs-lisp) tags) | |
121 | "Convert tags, originating from Emacs OBJ, into standardized form." | |
122 | (let ((newtags nil)) | |
123 | (dolist (T tags) | |
124 | (let* ((ot (semanticdb-normalize-one-tag obj T)) | |
125 | (tag (cdr ot))) | |
126 | (setq newtags (cons tag newtags)))) | |
127 | ;; There is no promise to have files associated. | |
128 | (nreverse newtags))) | |
129 | ||
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) | |
138 | 'defun) | |
139 | ((semantic-tag-of-class-p tag 'variable) | |
140 | 'defvar) | |
141 | )) | |
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)))) | |
147 | ) | |
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. | |
151 | (cons obj tag) | |
152 | (when (string-match "\\.elc" file) | |
153 | (setq file (concat (file-name-sans-extension file) | |
154 | ".el")) | |
155 | (when (and (not (file-exists-p file)) | |
156 | (file-exists-p (concat file ".gz"))) | |
157 | ;; Is it a .gz file? | |
158 | (setq file (concat file ".gz")))) | |
159 | ||
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))) | |
164 | (match nil)) | |
165 | ;; Find the best match. | |
166 | (dolist (T newtags) | |
167 | (when (semantic-tag-similar-p T tag) | |
168 | (setq match T))) | |
169 | ;; Backup system. | |
170 | (when (not match) | |
171 | (setq match (car newtags))) | |
172 | ;; Return it. | |
173 | (cons tab match))))) | |
174 | ||
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." | |
179 | (let* ((prelim-def | |
180 | (let ((sd (and (fboundp sym) | |
181 | (symbol-function sym)))) | |
182 | (and (symbolp sd) | |
183 | (condition-case err | |
184 | (setq sd (indirect-function sym)) | |
185 | (error (setq sd nil)))) | |
186 | sd)) | |
187 | (def (if (eq (car-safe prelim-def) 'macro) | |
188 | (cdr prelim-def) | |
189 | prelim-def)) | |
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) | |
196 | (nth 1 def)) | |
197 | (t nil)))) | |
198 | arglist)) | |
199 | ||
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." | |
203 | (if (stringp sym) | |
204 | (setq sym (intern-soft sym))) | |
205 | (when sym | |
206 | (cond ((and (eq toktype 'function) (fboundp sym)) | |
207 | (semantic-tag-new-function | |
208 | (symbol-name sym) | |
209 | nil ;; return type | |
210 | (semantic-elisp-desymbolify | |
211 | (semanticdb-elisp-sym-function-arglist sym)) ;; arg-list | |
212 | :user-visible-flag (condition-case nil | |
213 | (interactive-form sym) | |
214 | (error nil)) | |
215 | )) | |
216 | ((and (eq toktype 'variable) (boundp sym)) | |
217 | (semantic-tag-new-variable | |
218 | (symbol-name sym) | |
219 | nil ;; type | |
220 | nil ;; value - ignore for now | |
221 | )) | |
222 | ((and (eq toktype 'type) (class-p sym)) | |
223 | (semantic-tag-new-type | |
224 | (symbol-name sym) | |
225 | "class" | |
226 | (semantic-elisp-desymbolify | |
227 | (aref (class-v semanticdb-project-database) | |
228 | class-public-a)) ;; slots | |
229 | (semantic-elisp-desymbolify (class-parents sym)) ;; parents | |
230 | )) | |
231 | ((not toktype) | |
232 | ;; Figure it out on our own. | |
233 | (cond ((class-p sym) | |
234 | (semanticdb-elisp-sym->tag sym 'type)) | |
235 | ((fboundp sym) | |
236 | (semanticdb-elisp-sym->tag sym 'function)) | |
237 | ((boundp sym) | |
238 | (semanticdb-elisp-sym->tag sym 'variable)) | |
239 | (t nil)) | |
240 | ) | |
241 | (t nil)))) | |
242 | ||
243 | ;;; Search Overrides | |
244 | ;; | |
245 | (defvar semanticdb-elisp-mapatom-collector nil | |
246 | "Variable used to collect mapatoms output.") | |
247 | ||
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)) | |
259 | (taglst nil) | |
260 | ) | |
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))) | |
266 | taglst | |
267 | )))) | |
268 | ||
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))))) | |
278 | ||
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))))) | |
287 | ||
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. | |
295 | nil)) | |
296 | ||
297 | ;;; Deep Searches | |
298 | ;; | |
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)) | |
306 | ||
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)) | |
313 | ||
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)) | |
320 | ||
321 | ;;; Advanced Searches | |
322 | ;; | |
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)) | |
332 | (taglst (when class | |
333 | (delq nil | |
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))))) | |
338 | ) | |
339 | taglst)))) | |
340 | ||
341 | (provide 'semantic/db-el) | |
342 | ||
343 | ;;; semanticdb-el.el ends here |