Merge branch 'master' into comment-cache
[emacs.git] / lisp / cedet / semantic / db-el.el
blob89bbd1c0c299e4e86a681bf3c744b6c5530ffcdb
1 ;;; semantic/db-el.el --- Semantic database extensions for Emacs Lisp
3 ;;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: tags
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
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.
29 ;; This allows you to use programs written for Semantic using the database
30 ;; to also work in Emacs Lisp with no compromises.
33 (require 'semantic/db)
34 (require 'eieio-opt)
36 (declare-function semantic-elisp-desymbolify "semantic/bovine/el")
37 (declare-function semantic-tag-similar-p "semantic/tag-ls")
39 ;;; Code:
41 ;;; Classes:
42 (defclass semanticdb-table-emacs-lisp (semanticdb-abstract-table)
43 ((major-mode :initform emacs-lisp-mode)
45 "A table for returning search results from Emacs.")
47 (cl-defmethod semanticdb-refresh-table ((_obj semanticdb-table-emacs-lisp) &optional _force)
48 "Do not refresh Emacs Lisp table.
49 It does not need refreshing."
50 nil)
52 (cl-defmethod semanticdb-needs-refresh-p ((_obj semanticdb-table-emacs-lisp))
53 "Return nil, we never need a refresh."
54 nil)
56 (cl-defmethod object-print ((obj semanticdb-table-emacs-lisp) &rest strings)
57 "Pretty printer extension for `semanticdb-table-emacs-lisp'.
58 Adds the number of tags in this file to the object print name."
59 (apply #'cl-call-next-method obj (cons " (proxy)" strings)))
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.")
68 "Database representing Emacs core.")
70 (cl-defmethod object-print ((obj semanticdb-project-database-emacs-lisp) &rest strings)
71 "Pretty printer extension for `semanticdb-table-emacs-lisp'.
72 Adds the number of tags in this file to the object print name."
73 (let ((count 0))
74 (mapatoms (lambda (_sym) (setq count (1+ count))))
75 (apply #'cl-call-next-method obj (cons
76 (format " (%d known syms)" count)
77 strings))))
79 ;; Create the database, and add it to searchable databases for Emacs Lisp mode.
80 (defvar-mode-local emacs-lisp-mode semanticdb-project-system-databases
81 (list
82 (make-instance 'semanticdb-project-database-emacs-lisp))
83 "Search Emacs core for symbols.")
85 (defvar-mode-local emacs-lisp-mode semanticdb-find-default-throttle
86 '(project omniscience)
87 "Search project files, then search this omniscience database.
88 It is not necessary to do system or recursive searching because of
89 the omniscience database.")
91 ;;; Filename based methods
93 (cl-defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-emacs-lisp))
94 "For an Emacs Lisp database, there are no explicit tables.
95 Create one of our special tables that can act as an intermediary."
96 ;; We need to return something since there is always the "master table"
97 ;; The table can then answer file name type questions.
98 (when (not (slot-boundp obj 'tables))
99 (let ((newtable (make-instance 'semanticdb-table-emacs-lisp)))
100 (oset obj tables (list newtable))
101 (oset newtable parent-db obj)
102 (oset newtable tags nil)
104 (cl-call-next-method))
106 (cl-defmethod semanticdb-file-table ((obj semanticdb-project-database-emacs-lisp) _filename)
107 "From OBJ, return FILENAME's associated table object.
108 For Emacs Lisp, creates a specialized table."
109 (car (semanticdb-get-database-tables obj))
112 (cl-defmethod semanticdb-get-tags ((_table semanticdb-table-emacs-lisp ))
113 "Return the list of tags belonging to TABLE."
114 ;; specialty table ? Probably derive tags at request time.
115 nil)
117 (cl-defmethod semanticdb-equivalent-mode ((_table semanticdb-table-emacs-lisp) &optional buffer)
118 "Return non-nil if TABLE's mode is equivalent to BUFFER.
119 Equivalent modes are specified by the `semantic-equivalent-major-modes'
120 local variable."
121 (with-current-buffer buffer
122 (eq (or mode-local-active-mode major-mode) 'emacs-lisp-mode)))
124 (cl-defmethod semanticdb-full-filename ((_obj semanticdb-table-emacs-lisp))
125 "Fetch the full filename that OBJ refers to.
126 For Emacs Lisp system DB, there isn't one."
127 nil)
129 ;;; Conversion
131 (cl-defmethod semanticdb-normalize-tags ((obj semanticdb-table-emacs-lisp) tags)
132 "Convert tags, originating from Emacs OBJ, into standardized form."
133 (let ((newtags nil))
134 (dolist (T tags)
135 (let* ((ot (semanticdb-normalize-one-tag obj T))
136 (tag (cdr ot)))
137 (setq newtags (cons tag newtags))))
138 ;; There is no promise to have files associated.
139 (nreverse newtags)))
141 (cl-defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-emacs-lisp) tag)
142 "Convert one TAG, originating from Emacs OBJ, into standardized form.
143 If Emacs cannot resolve this symbol to a particular file, then return nil."
144 ;; Here's the idea. For each tag, get the name, then use
145 ;; Emacs's `symbol-file' to get the source. Once we have that,
146 ;; we can use more typical semantic searching techniques to
147 ;; get a regularly parsed tag.
148 (let* ((type (cond ((semantic-tag-of-class-p tag 'function)
149 'defun)
150 ((semantic-tag-of-class-p tag 'variable)
151 'defvar)
153 (sym (intern (semantic-tag-name tag)))
154 (file (condition-case nil
155 (symbol-file sym type)
156 ;; Older [X]Emacs don't have a 2nd argument.
157 (error (symbol-file sym))))
159 (if (or (not file) (not (file-exists-p file)))
160 ;; The file didn't exist. Return nil.
161 ;; We can't normalize this tag. Fake it out.
162 (cons obj tag)
163 (when (string-match "\\.elc" file)
164 (setq file (concat (file-name-sans-extension file)
165 ".el"))
166 (when (and (not (file-exists-p file))
167 (file-exists-p (concat file ".gz")))
168 ;; Is it a .gz file?
169 (setq file (concat file ".gz"))))
171 (let* ((tab (semanticdb-file-table-object file))
172 (newtags (when tab (semanticdb-find-tags-by-name-method
173 tab (semantic-tag-name tag))))
174 (match nil))
175 ;; We might not have a parsed tag in this file, because it
176 ;; might be generated through a macro like defstruct.
177 (if (null newtags)
178 (setq match tag)
179 ;; Find the best match.
180 (dolist (T newtags)
181 (when (semantic-tag-similar-p T tag)
182 (setq match T)))
183 ;; Backup system.
184 (when (not match)
185 (setq match (car newtags))))
186 ;; Return it.
187 (when tab (cons tab match))))))
189 (autoload 'help-function-arglist "help-fns")
190 (defalias 'semanticdb-elisp-sym-function-arglist 'help-function-arglist)
191 (make-obsolete 'semanticdb-elisp-sym-function-arglist
192 'help-function-arglist "CEDET 1.1")
194 (defun semanticdb-elisp-sym->tag (sym &optional toktype)
195 "Convert SYM into a semantic tag.
196 TOKTYPE is a hint to the type of tag desired."
197 (if (stringp sym)
198 (setq sym (intern-soft sym)))
199 (when sym
200 (cond ((and (eq toktype 'function) (fboundp sym))
201 (require 'semantic/bovine/el)
202 (let ((arglist (help-function-arglist sym)))
203 (when (not (listp arglist))
204 ;; Function might be autoloaded, in which case
205 ;; the arglist is not available yet.
206 (setq arglist nil))
207 (semantic-tag-new-function
208 (symbol-name sym)
209 nil ;; return type
210 (semantic-elisp-desymbolify arglist)
211 :user-visible-flag (condition-case nil
212 (interactive-form sym)
213 (error nil)))))
214 ((and (eq toktype 'variable) (boundp sym))
215 (semantic-tag-new-variable
216 (symbol-name sym)
217 nil ;; type
218 nil ;; value - ignore for now
220 ((and (eq toktype 'type) (class-p sym))
221 (semantic-tag-new-type
222 (symbol-name sym)
223 "class"
224 (semantic-elisp-desymbolify
225 (let ((class (find-class sym)))
226 (if (fboundp 'eieio--class-public-a) ; Emacs < 25.1
227 (eieio--class-public-a class)
228 (mapcar #'eieio-slot-descriptor-name
229 (eieio-class-slots class)))))
230 (semantic-elisp-desymbolify (eieio-class-parents sym)) ;; parents
232 ((not toktype)
233 ;; Figure it out on our own.
234 (cond ((class-p sym)
235 (semanticdb-elisp-sym->tag sym 'type))
236 ((fboundp sym)
237 (semanticdb-elisp-sym->tag sym 'function))
238 ((boundp sym)
239 (semanticdb-elisp-sym->tag sym 'variable))
240 (t nil))
242 (t nil))))
244 ;;; Search Overrides
246 (defvar semanticdb-elisp-mapatom-collector nil
247 "Variable used to collect `mapatoms' output.")
249 (cl-defmethod semanticdb-find-tags-by-name-method
250 ((_table semanticdb-table-emacs-lisp) name &optional tags)
251 "Find all tags named NAME in TABLE.
252 Uses `intern-soft' to match NAME to Emacs symbols.
253 Return a list of tags."
254 (if tags (cl-call-next-method)
255 ;; No need to search. Use `intern-soft' which does the same thing for us.
256 (let* ((sym (intern-soft name))
257 (fun (semanticdb-elisp-sym->tag sym 'function))
258 (var (semanticdb-elisp-sym->tag sym 'variable))
259 (typ (semanticdb-elisp-sym->tag sym 'type))
260 (taglst nil)
262 (when (or fun var typ)
263 ;; If the symbol is any of these things, build the search table.
264 (when var (setq taglst (cons var taglst)))
265 (when typ (setq taglst (cons typ taglst)))
266 (when fun (setq taglst (cons fun taglst)))
267 taglst
268 ))))
270 (cl-defmethod semanticdb-find-tags-by-name-regexp-method
271 ((_table semanticdb-table-emacs-lisp) regex &optional tags)
272 "Find all tags with name matching REGEX in TABLE.
273 Optional argument TAGS is a list of tags to search.
274 Uses `apropos-internal' to find matches.
275 Return a list of tags."
276 (if tags (cl-call-next-method)
277 (delq nil (mapcar #'semanticdb-elisp-sym->tag
278 (apropos-internal regex)))))
280 (cl-defmethod semanticdb-find-tags-for-completion-method
281 ((_table semanticdb-table-emacs-lisp) prefix &optional tags)
282 "In TABLE, find all occurrences of tags matching PREFIX.
283 Optional argument TAGS is a list of tags to search.
284 Returns a table of all matching tags."
285 (if tags (cl-call-next-method)
286 (delq nil (mapcar #'semanticdb-elisp-sym->tag
287 (all-completions prefix obarray)))))
289 (cl-defmethod semanticdb-find-tags-by-class-method
290 ((_table semanticdb-table-emacs-lisp) _class &optional tags)
291 "In TABLE, find all occurrences of tags of CLASS.
292 Optional argument TAGS is a list of tags to search.
293 Returns a table of all matching tags."
294 (if tags (cl-call-next-method)
295 ;; We could implement this, but it could be messy.
296 nil))
298 ;;; Deep Searches
300 ;; For Emacs Lisp deep searches are like top level searches.
301 (cl-defmethod semanticdb-deep-find-tags-by-name-method
302 ((table semanticdb-table-emacs-lisp) name &optional tags)
303 "Find all tags name NAME in TABLE.
304 Optional argument TAGS is a list of tags to search.
305 Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
306 (semanticdb-find-tags-by-name-method table name tags))
308 (cl-defmethod semanticdb-deep-find-tags-by-name-regexp-method
309 ((table semanticdb-table-emacs-lisp) regex &optional tags)
310 "Find all tags with name matching REGEX in TABLE.
311 Optional argument TAGS is a list of tags to search.
312 Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
313 (semanticdb-find-tags-by-name-regexp-method table regex tags))
315 (cl-defmethod semanticdb-deep-find-tags-for-completion-method
316 ((table semanticdb-table-emacs-lisp) prefix &optional tags)
317 "In TABLE, find all occurrences of tags matching PREFIX.
318 Optional argument TAGS is a list of tags to search.
319 Like `semanticdb-find-tags-for-completion-method' for Emacs Lisp."
320 (semanticdb-find-tags-for-completion-method table prefix tags))
322 ;;; Advanced Searches
324 (cl-defmethod semanticdb-find-tags-external-children-of-type-method
325 ((_table semanticdb-table-emacs-lisp) type &optional tags)
326 "Find all nonterminals which are child elements of TYPE
327 Optional argument TAGS is a list of tags to search.
328 Return a list of tags."
329 (if tags (cl-call-next-method)
330 ;; EIEIO is the only time this matters
331 (when (featurep 'eieio)
332 (let* ((class (intern-soft type))
333 (taglst (when class
334 (delq nil
335 (mapcar #'semanticdb-elisp-sym->tag
336 ;; Fancy eieio function that knows all about
337 ;; built in methods belonging to CLASS.
338 (cl-generic-all-functions class)))))
340 taglst))))
342 (provide 'semantic/db-el)
344 ;;; semantic/db-el.el ends here