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