Commit | Line | Data |
---|---|---|
691a065e | 1 | ;;; semantic/db-el.el --- Semantic database extensions for Emacs Lisp |
f273dfc6 CY |
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 | ||
691a065e CY |
34 | (require 'semantic/db) |
35 | ||
f273dfc6 CY |
36 | (eval-when-compile |
37 | ;; For generic function searching. | |
38 | (require 'eieio) | |
39 | (require 'eieio-opt) | |
a4556861 CY |
40 | (require 'eieio-base)) |
41 | ||
42 | (declare-function semantic-elisp-desymbolify "semantic/bovine/el") | |
43 | ||
f273dfc6 CY |
44 | ;;; Code: |
45 | ||
46 | ;;; Classes: | |
47 | (defclass semanticdb-table-emacs-lisp (semanticdb-abstract-table) | |
48 | ((major-mode :initform emacs-lisp-mode) | |
49 | ) | |
50 | "A table for returning search results from Emacs.") | |
51 | ||
52 | (defmethod semanticdb-refresh-table ((obj semanticdb-table-emacs-lisp) &optional force) | |
53 | "Do not refresh Emacs Lisp table. | |
54 | It does not need refreshing." | |
55 | nil) | |
56 | ||
57 | (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table-emacs-lisp)) | |
58 | "Return nil, we never need a refresh." | |
59 | nil) | |
60 | ||
61 | (defclass semanticdb-project-database-emacs-lisp | |
62 | (semanticdb-project-database eieio-singleton) | |
63 | ((new-table-class :initform semanticdb-table-emacs-lisp | |
64 | :type class | |
65 | :documentation | |
66 | "New tables created for this database are of this class.") | |
67 | ) | |
68 | "Database representing Emacs core.") | |
69 | ||
70 | ;; Create the database, and add it to searchable databases for Emacs Lisp mode. | |
71 | (defvar-mode-local emacs-lisp-mode semanticdb-project-system-databases | |
72 | (list | |
73 | (semanticdb-project-database-emacs-lisp "Emacs")) | |
74 | "Search Emacs core for symbols.") | |
75 | ||
76 | (defvar-mode-local emacs-lisp-mode semanticdb-find-default-throttle | |
77 | '(project omniscience) | |
78 | "Search project files, then search this omniscience database. | |
79 | It is not necessary to to system or recursive searching because of | |
80 | the omniscience database.") | |
81 | ||
82 | ;;; Filename based methods | |
83 | ;; | |
84 | (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-emacs-lisp)) | |
85 | "For an Emacs Lisp database, there are no explicit tables. | |
86 | Create one of our special tables that can act as an intermediary." | |
87 | ;; We need to return something since there is always the "master table" | |
88 | ;; The table can then answer file name type questions. | |
89 | (when (not (slot-boundp obj 'tables)) | |
90 | (let ((newtable (semanticdb-table-emacs-lisp "Emacs System Table"))) | |
91 | (oset obj tables (list newtable)) | |
92 | (oset newtable parent-db obj) | |
93 | (oset newtable tags nil) | |
94 | )) | |
95 | (call-next-method)) | |
96 | ||
97 | (defmethod semanticdb-file-table ((obj semanticdb-project-database-emacs-lisp) filename) | |
98 | "From OBJ, return FILENAME's associated table object. | |
99 | For Emacs Lisp, creates a specialized table." | |
100 | (car (semanticdb-get-database-tables obj)) | |
101 | ) | |
102 | ||
103 | (defmethod semanticdb-get-tags ((table semanticdb-table-emacs-lisp )) | |
104 | "Return the list of tags belonging to TABLE." | |
105 | ;; specialty table ? Probably derive tags at request time. | |
106 | nil) | |
107 | ||
108 | (defmethod semanticdb-equivalent-mode ((table semanticdb-table-emacs-lisp) &optional buffer) | |
109 | "Return non-nil if TABLE's mode is equivalent to BUFFER. | |
110 | Equivalent modes are specified by by `semantic-equivalent-major-modes' | |
111 | local variable." | |
112 | (save-excursion | |
113 | (set-buffer buffer) | |
114 | (eq (or mode-local-active-mode major-mode) 'emacs-lisp-mode))) | |
115 | ||
116 | (defmethod semanticdb-full-filename ((obj semanticdb-table-emacs-lisp)) | |
117 | "Fetch the full filename that OBJ refers to. | |
118 | For Emacs Lisp system DB, there isn't one." | |
119 | nil) | |
120 | ||
121 | ;;; Conversion | |
122 | ;; | |
123 | (defmethod semanticdb-normalize-tags ((obj semanticdb-table-emacs-lisp) tags) | |
124 | "Convert tags, originating from Emacs OBJ, into standardized form." | |
125 | (let ((newtags nil)) | |
126 | (dolist (T tags) | |
127 | (let* ((ot (semanticdb-normalize-one-tag obj T)) | |
128 | (tag (cdr ot))) | |
129 | (setq newtags (cons tag newtags)))) | |
130 | ;; There is no promise to have files associated. | |
131 | (nreverse newtags))) | |
132 | ||
133 | (defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-emacs-lisp) tag) | |
134 | "Convert one TAG, originating from Emacs OBJ, into standardized form. | |
135 | If Emacs cannot resolve this symbol to a particular file, then return nil." | |
136 | ;; Here's the idea. For each tag, get the name, then use | |
137 | ;; Emacs' `symbol-file' to get the source. Once we have that, | |
138 | ;; we can use more typical semantic searching techniques to | |
139 | ;; get a regularly parsed tag. | |
140 | (let* ((type (cond ((semantic-tag-of-class-p tag 'function) | |
141 | 'defun) | |
142 | ((semantic-tag-of-class-p tag 'variable) | |
143 | 'defvar) | |
144 | )) | |
145 | (sym (intern (semantic-tag-name tag))) | |
146 | (file (condition-case err | |
147 | (symbol-file sym type) | |
148 | ;; Older [X]Emacs don't have a 2nd argument. | |
149 | (error (symbol-file sym)))) | |
150 | ) | |
151 | (if (or (not file) (not (file-exists-p file))) | |
152 | ;; The file didn't exist. Return nil. | |
153 | ;; We can't normalize this tag. Fake it out. | |
154 | (cons obj tag) | |
155 | (when (string-match "\\.elc" file) | |
156 | (setq file (concat (file-name-sans-extension file) | |
157 | ".el")) | |
158 | (when (and (not (file-exists-p file)) | |
159 | (file-exists-p (concat file ".gz"))) | |
160 | ;; Is it a .gz file? | |
161 | (setq file (concat file ".gz")))) | |
162 | ||
163 | (let* ((tab (semanticdb-file-table-object file)) | |
164 | (alltags (semanticdb-get-tags tab)) | |
165 | (newtags (semanticdb-find-tags-by-name-method | |
166 | tab (semantic-tag-name tag))) | |
167 | (match nil)) | |
168 | ;; Find the best match. | |
169 | (dolist (T newtags) | |
170 | (when (semantic-tag-similar-p T tag) | |
171 | (setq match T))) | |
172 | ;; Backup system. | |
173 | (when (not match) | |
174 | (setq match (car newtags))) | |
175 | ;; Return it. | |
176 | (cons tab match))))) | |
177 | ||
178 | (defun semanticdb-elisp-sym-function-arglist (sym) | |
179 | "Get the argument list for SYM. | |
180 | Deal with all different forms of function. | |
181 | This was snarfed out of eldoc." | |
182 | (let* ((prelim-def | |
183 | (let ((sd (and (fboundp sym) | |
184 | (symbol-function sym)))) | |
185 | (and (symbolp sd) | |
186 | (condition-case err | |
187 | (setq sd (indirect-function sym)) | |
188 | (error (setq sd nil)))) | |
189 | sd)) | |
190 | (def (if (eq (car-safe prelim-def) 'macro) | |
191 | (cdr prelim-def) | |
192 | prelim-def)) | |
193 | (arglist (cond ((null def) nil) | |
194 | ((byte-code-function-p def) | |
195 | ;; This is an eieio compatibility function. | |
196 | ;; We depend on EIEIO, so use this. | |
197 | (eieio-compiled-function-arglist def)) | |
198 | ((eq (car-safe def) 'lambda) | |
199 | (nth 1 def)) | |
200 | (t nil)))) | |
201 | arglist)) | |
202 | ||
203 | (defun semanticdb-elisp-sym->tag (sym &optional toktype) | |
204 | "Convert SYM into a semantic tag. | |
205 | TOKTYPE is a hint to the type of tag desired." | |
206 | (if (stringp sym) | |
207 | (setq sym (intern-soft sym))) | |
208 | (when sym | |
209 | (cond ((and (eq toktype 'function) (fboundp sym)) | |
a4556861 | 210 | (require 'semantic/bovine/el) |
f273dfc6 CY |
211 | (semantic-tag-new-function |
212 | (symbol-name sym) | |
213 | nil ;; return type | |
214 | (semantic-elisp-desymbolify | |
215 | (semanticdb-elisp-sym-function-arglist sym)) ;; arg-list | |
216 | :user-visible-flag (condition-case nil | |
217 | (interactive-form sym) | |
218 | (error nil)) | |
219 | )) | |
220 | ((and (eq toktype 'variable) (boundp sym)) | |
221 | (semantic-tag-new-variable | |
222 | (symbol-name sym) | |
223 | nil ;; type | |
224 | nil ;; value - ignore for now | |
225 | )) | |
226 | ((and (eq toktype 'type) (class-p sym)) | |
227 | (semantic-tag-new-type | |
228 | (symbol-name sym) | |
229 | "class" | |
230 | (semantic-elisp-desymbolify | |
231 | (aref (class-v semanticdb-project-database) | |
232 | class-public-a)) ;; slots | |
233 | (semantic-elisp-desymbolify (class-parents sym)) ;; parents | |
234 | )) | |
235 | ((not toktype) | |
236 | ;; Figure it out on our own. | |
237 | (cond ((class-p sym) | |
238 | (semanticdb-elisp-sym->tag sym 'type)) | |
239 | ((fboundp sym) | |
240 | (semanticdb-elisp-sym->tag sym 'function)) | |
241 | ((boundp sym) | |
242 | (semanticdb-elisp-sym->tag sym 'variable)) | |
243 | (t nil)) | |
244 | ) | |
245 | (t nil)))) | |
246 | ||
247 | ;;; Search Overrides | |
248 | ;; | |
249 | (defvar semanticdb-elisp-mapatom-collector nil | |
250 | "Variable used to collect mapatoms output.") | |
251 | ||
252 | (defmethod semanticdb-find-tags-by-name-method | |
253 | ((table semanticdb-table-emacs-lisp) name &optional tags) | |
254 | "Find all tags name NAME in TABLE. | |
255 | Uses `inter-soft' to match NAME to emacs symbols. | |
256 | Return a list of tags." | |
257 | (if tags (call-next-method) | |
258 | ;; No need to search. Use `intern-soft' which does the same thing for us. | |
259 | (let* ((sym (intern-soft name)) | |
260 | (fun (semanticdb-elisp-sym->tag sym 'function)) | |
261 | (var (semanticdb-elisp-sym->tag sym 'variable)) | |
262 | (typ (semanticdb-elisp-sym->tag sym 'type)) | |
263 | (taglst nil) | |
264 | ) | |
265 | (when (or fun var typ) | |
266 | ;; If the symbol is any of these things, build the search table. | |
267 | (when var (setq taglst (cons var taglst))) | |
268 | (when typ (setq taglst (cons typ taglst))) | |
269 | (when fun (setq taglst (cons fun taglst))) | |
270 | taglst | |
271 | )))) | |
272 | ||
273 | (defmethod semanticdb-find-tags-by-name-regexp-method | |
274 | ((table semanticdb-table-emacs-lisp) regex &optional tags) | |
275 | "Find all tags with name matching REGEX in TABLE. | |
276 | Optional argument TAGS is a list of tags to search. | |
277 | Uses `apropos-internal' to find matches. | |
278 | Return a list of tags." | |
279 | (if tags (call-next-method) | |
280 | (delq nil (mapcar 'semanticdb-elisp-sym->tag | |
281 | (apropos-internal regex))))) | |
282 | ||
283 | (defmethod semanticdb-find-tags-for-completion-method | |
284 | ((table semanticdb-table-emacs-lisp) prefix &optional tags) | |
285 | "In TABLE, find all occurances of tags matching PREFIX. | |
286 | Optional argument TAGS is a list of tags to search. | |
287 | Returns a table of all matching tags." | |
288 | (if tags (call-next-method) | |
289 | (delq nil (mapcar 'semanticdb-elisp-sym->tag | |
290 | (all-completions prefix obarray))))) | |
291 | ||
292 | (defmethod semanticdb-find-tags-by-class-method | |
293 | ((table semanticdb-table-emacs-lisp) class &optional tags) | |
294 | "In TABLE, find all occurances of tags of CLASS. | |
295 | Optional argument TAGS is a list of tags to search. | |
296 | Returns a table of all matching tags." | |
297 | (if tags (call-next-method) | |
298 | ;; We could implement this, but it could be messy. | |
299 | nil)) | |
300 | ||
301 | ;;; Deep Searches | |
302 | ;; | |
303 | ;; For Emacs Lisp deep searches are like top level searches. | |
304 | (defmethod semanticdb-deep-find-tags-by-name-method | |
305 | ((table semanticdb-table-emacs-lisp) name &optional tags) | |
306 | "Find all tags name NAME in TABLE. | |
307 | Optional argument TAGS is a list of tags to search. | |
308 | Like `semanticdb-find-tags-by-name-method' for Emacs Lisp." | |
309 | (semanticdb-find-tags-by-name-method table name tags)) | |
310 | ||
311 | (defmethod semanticdb-deep-find-tags-by-name-regexp-method | |
312 | ((table semanticdb-table-emacs-lisp) regex &optional tags) | |
313 | "Find all tags with name matching REGEX in TABLE. | |
314 | Optional argument TAGS is a list of tags to search. | |
315 | Like `semanticdb-find-tags-by-name-method' for Emacs Lisp." | |
316 | (semanticdb-find-tags-by-name-regexp-method table regex tags)) | |
317 | ||
318 | (defmethod semanticdb-deep-find-tags-for-completion-method | |
319 | ((table semanticdb-table-emacs-lisp) prefix &optional tags) | |
320 | "In TABLE, find all occurances of tags matching PREFIX. | |
321 | Optional argument TAGS is a list of tags to search. | |
322 | Like `semanticdb-find-tags-for-completion-method' for Emacs Lisp." | |
323 | (semanticdb-find-tags-for-completion-method table prefix tags)) | |
324 | ||
325 | ;;; Advanced Searches | |
326 | ;; | |
327 | (defmethod semanticdb-find-tags-external-children-of-type-method | |
328 | ((table semanticdb-table-emacs-lisp) type &optional tags) | |
329 | "Find all nonterminals which are child elements of TYPE | |
330 | Optional argument TAGS is a list of tags to search. | |
331 | Return a list of tags." | |
332 | (if tags (call-next-method) | |
333 | ;; EIEIO is the only time this matters | |
334 | (when (featurep 'eieio) | |
335 | (let* ((class (intern-soft type)) | |
336 | (taglst (when class | |
337 | (delq nil | |
338 | (mapcar 'semanticdb-elisp-sym->tag | |
339 | ;; Fancy eieio function that knows all about | |
340 | ;; built in methods belonging to CLASS. | |
341 | (eieio-all-generic-functions class))))) | |
342 | ) | |
343 | taglst)))) | |
344 | ||
345 | (provide 'semantic/db-el) | |
346 | ||
691a065e | 347 | ;;; semantic/db-el.el ends here |