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