db-el.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ;;; semantic/db-el.el --- Semantic database extensions for Emacs Lisp
  2. ;;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: tags
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; There are a lot of Emacs Lisp functions and variables available for
  19. ;; the asking. This adds on to the semanticdb programming interface to
  20. ;; allow all loaded Emacs Lisp functions to be queried via semanticdb.
  21. ;;
  22. ;; This allows you to use programs written for Semantic using the database
  23. ;; to also work in Emacs Lisp with no compromises.
  24. ;;
  25. (require 'semantic/db)
  26. (require 'eieio-opt)
  27. (declare-function semantic-elisp-desymbolify "semantic/bovine/el")
  28. (declare-function semantic-tag-similar-p "semantic/tag-ls")
  29. ;;; Code:
  30. ;;; Classes:
  31. (defclass semanticdb-table-emacs-lisp (semanticdb-abstract-table)
  32. ((major-mode :initform emacs-lisp-mode)
  33. )
  34. "A table for returning search results from Emacs.")
  35. (cl-defmethod semanticdb-refresh-table ((_obj semanticdb-table-emacs-lisp) &optional _force)
  36. "Do not refresh Emacs Lisp table.
  37. It does not need refreshing."
  38. nil)
  39. (cl-defmethod semanticdb-needs-refresh-p ((_obj semanticdb-table-emacs-lisp))
  40. "Return nil, we never need a refresh."
  41. nil)
  42. (cl-defmethod object-print ((obj semanticdb-table-emacs-lisp) &rest strings)
  43. "Pretty printer extension for `semanticdb-table-emacs-lisp'.
  44. Adds the number of tags in this file to the object print name."
  45. (apply #'cl-call-next-method obj (cons " (proxy)" strings)))
  46. (defclass semanticdb-project-database-emacs-lisp
  47. (semanticdb-project-database eieio-singleton)
  48. ((new-table-class :initform semanticdb-table-emacs-lisp
  49. :type class
  50. :documentation
  51. "New tables created for this database are of this class.")
  52. )
  53. "Database representing Emacs core.")
  54. (cl-defmethod object-print ((obj semanticdb-project-database-emacs-lisp) &rest strings)
  55. "Pretty printer extension for `semanticdb-table-emacs-lisp'.
  56. Adds the number of tags in this file to the object print name."
  57. (let ((count 0))
  58. (mapatoms (lambda (_sym) (setq count (1+ count))))
  59. (apply #'cl-call-next-method obj (cons
  60. (format " (%d known syms)" count)
  61. strings))))
  62. ;; Create the database, and add it to searchable databases for Emacs Lisp mode.
  63. (defvar-mode-local emacs-lisp-mode semanticdb-project-system-databases
  64. (list
  65. (make-instance 'semanticdb-project-database-emacs-lisp))
  66. "Search Emacs core for symbols.")
  67. (defvar-mode-local emacs-lisp-mode semanticdb-find-default-throttle
  68. '(project omniscience)
  69. "Search project files, then search this omniscience database.
  70. It is not necessary to do system or recursive searching because of
  71. the omniscience database.")
  72. ;;; Filename based methods
  73. ;;
  74. (cl-defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-emacs-lisp))
  75. "For an Emacs Lisp database, there are no explicit tables.
  76. Create one of our special tables that can act as an intermediary."
  77. ;; We need to return something since there is always the "master table"
  78. ;; The table can then answer file name type questions.
  79. (when (not (slot-boundp obj 'tables))
  80. (let ((newtable (make-instance 'semanticdb-table-emacs-lisp)))
  81. (oset obj tables (list newtable))
  82. (oset newtable parent-db obj)
  83. (oset newtable tags nil)
  84. ))
  85. (cl-call-next-method))
  86. (cl-defmethod semanticdb-file-table ((obj semanticdb-project-database-emacs-lisp) _filename)
  87. "From OBJ, return FILENAME's associated table object.
  88. For Emacs Lisp, creates a specialized table."
  89. (car (semanticdb-get-database-tables obj))
  90. )
  91. (cl-defmethod semanticdb-get-tags ((_table semanticdb-table-emacs-lisp ))
  92. "Return the list of tags belonging to TABLE."
  93. ;; specialty table ? Probably derive tags at request time.
  94. nil)
  95. (cl-defmethod semanticdb-equivalent-mode ((_table semanticdb-table-emacs-lisp) &optional buffer)
  96. "Return non-nil if TABLE's mode is equivalent to BUFFER.
  97. Equivalent modes are specified by the `semantic-equivalent-major-modes'
  98. local variable."
  99. (with-current-buffer buffer
  100. (eq (or mode-local-active-mode major-mode) 'emacs-lisp-mode)))
  101. (cl-defmethod semanticdb-full-filename ((_obj semanticdb-table-emacs-lisp))
  102. "Fetch the full filename that OBJ refers to.
  103. For Emacs Lisp system DB, there isn't one."
  104. nil)
  105. ;;; Conversion
  106. ;;
  107. (cl-defmethod semanticdb-normalize-tags ((obj semanticdb-table-emacs-lisp) tags)
  108. "Convert tags, originating from Emacs OBJ, into standardized form."
  109. (let ((newtags nil))
  110. (dolist (T tags)
  111. (let* ((ot (semanticdb-normalize-one-tag obj T))
  112. (tag (cdr ot)))
  113. (setq newtags (cons tag newtags))))
  114. ;; There is no promise to have files associated.
  115. (nreverse newtags)))
  116. (cl-defmethod semanticdb-normalize-one-tag ((obj semanticdb-table-emacs-lisp) tag)
  117. "Convert one TAG, originating from Emacs OBJ, into standardized form.
  118. If Emacs cannot resolve this symbol to a particular file, then return nil."
  119. ;; Here's the idea. For each tag, get the name, then use
  120. ;; Emacs's `symbol-file' to get the source. Once we have that,
  121. ;; we can use more typical semantic searching techniques to
  122. ;; get a regularly parsed tag.
  123. (let* ((type (cond ((semantic-tag-of-class-p tag 'function)
  124. 'defun)
  125. ((semantic-tag-of-class-p tag 'variable)
  126. 'defvar)
  127. ))
  128. (sym (intern (semantic-tag-name tag)))
  129. (file (condition-case nil
  130. (symbol-file sym type)
  131. ;; Older [X]Emacs don't have a 2nd argument.
  132. (error (symbol-file sym))))
  133. )
  134. (if (or (not file) (not (file-exists-p file)))
  135. ;; The file didn't exist. Return nil.
  136. ;; We can't normalize this tag. Fake it out.
  137. (cons obj tag)
  138. (when (string-match "\\.elc" file)
  139. (setq file (concat (file-name-sans-extension file)
  140. ".el"))
  141. (when (and (not (file-exists-p file))
  142. (file-exists-p (concat file ".gz")))
  143. ;; Is it a .gz file?
  144. (setq file (concat file ".gz"))))
  145. (let* ((tab (semanticdb-file-table-object file))
  146. (newtags (when tab (semanticdb-find-tags-by-name-method
  147. tab (semantic-tag-name tag))))
  148. (match nil))
  149. ;; We might not have a parsed tag in this file, because it
  150. ;; might be generated through a macro like defstruct.
  151. (if (null newtags)
  152. (setq match tag)
  153. ;; Find the best match.
  154. (dolist (T newtags)
  155. (when (semantic-tag-similar-p T tag)
  156. (setq match T)))
  157. ;; Backup system.
  158. (when (not match)
  159. (setq match (car newtags))))
  160. ;; Return it.
  161. (when tab (cons tab match))))))
  162. (autoload 'help-function-arglist "help-fns")
  163. (defalias 'semanticdb-elisp-sym-function-arglist 'help-function-arglist)
  164. (make-obsolete 'semanticdb-elisp-sym-function-arglist
  165. 'help-function-arglist "CEDET 1.1")
  166. (defun semanticdb-elisp-sym->tag (sym &optional toktype)
  167. "Convert SYM into a semantic tag.
  168. TOKTYPE is a hint to the type of tag desired."
  169. (if (stringp sym)
  170. (setq sym (intern-soft sym)))
  171. (when sym
  172. (cond ((and (eq toktype 'function) (fboundp sym))
  173. (require 'semantic/bovine/el)
  174. (let ((arglist (help-function-arglist sym)))
  175. (when (not (listp arglist))
  176. ;; Function might be autoloaded, in which case
  177. ;; the arglist is not available yet.
  178. (setq arglist nil))
  179. (semantic-tag-new-function
  180. (symbol-name sym)
  181. nil ;; return type
  182. (semantic-elisp-desymbolify arglist)
  183. :user-visible-flag (condition-case nil
  184. (interactive-form sym)
  185. (error nil)))))
  186. ((and (eq toktype 'variable) (boundp sym))
  187. (semantic-tag-new-variable
  188. (symbol-name sym)
  189. nil ;; type
  190. nil ;; value - ignore for now
  191. ))
  192. ((and (eq toktype 'type) (class-p sym))
  193. (semantic-tag-new-type
  194. (symbol-name sym)
  195. "class"
  196. (semantic-elisp-desymbolify
  197. (let ((class (find-class sym)))
  198. (if (fboundp 'eieio--class-public-a) ; Emacs < 25.1
  199. (eieio--class-public-a class)
  200. (mapcar #'eieio-slot-descriptor-name
  201. (eieio-class-slots class)))))
  202. (semantic-elisp-desymbolify (eieio-class-parents sym)) ;; parents
  203. ))
  204. ((not toktype)
  205. ;; Figure it out on our own.
  206. (cond ((class-p sym)
  207. (semanticdb-elisp-sym->tag sym 'type))
  208. ((fboundp sym)
  209. (semanticdb-elisp-sym->tag sym 'function))
  210. ((boundp sym)
  211. (semanticdb-elisp-sym->tag sym 'variable))
  212. (t nil))
  213. )
  214. (t nil))))
  215. ;;; Search Overrides
  216. ;;
  217. (defvar semanticdb-elisp-mapatom-collector nil
  218. "Variable used to collect `mapatoms' output.")
  219. (cl-defmethod semanticdb-find-tags-by-name-method
  220. ((_table semanticdb-table-emacs-lisp) name &optional tags)
  221. "Find all tags named NAME in TABLE.
  222. Uses `intern-soft' to match NAME to Emacs symbols.
  223. Return a list of tags."
  224. (if tags (cl-call-next-method)
  225. ;; No need to search. Use `intern-soft' which does the same thing for us.
  226. (let* ((sym (intern-soft name))
  227. (fun (semanticdb-elisp-sym->tag sym 'function))
  228. (var (semanticdb-elisp-sym->tag sym 'variable))
  229. (typ (semanticdb-elisp-sym->tag sym 'type))
  230. (taglst nil)
  231. )
  232. (when (or fun var typ)
  233. ;; If the symbol is any of these things, build the search table.
  234. (when var (setq taglst (cons var taglst)))
  235. (when typ (setq taglst (cons typ taglst)))
  236. (when fun (setq taglst (cons fun taglst)))
  237. taglst
  238. ))))
  239. (cl-defmethod semanticdb-find-tags-by-name-regexp-method
  240. ((_table semanticdb-table-emacs-lisp) regex &optional tags)
  241. "Find all tags with name matching REGEX in TABLE.
  242. Optional argument TAGS is a list of tags to search.
  243. Uses `apropos-internal' to find matches.
  244. Return a list of tags."
  245. (if tags (cl-call-next-method)
  246. (delq nil (mapcar #'semanticdb-elisp-sym->tag
  247. (apropos-internal regex)))))
  248. (cl-defmethod semanticdb-find-tags-for-completion-method
  249. ((_table semanticdb-table-emacs-lisp) prefix &optional tags)
  250. "In TABLE, find all occurrences of tags matching PREFIX.
  251. Optional argument TAGS is a list of tags to search.
  252. Returns a table of all matching tags."
  253. (if tags (cl-call-next-method)
  254. (delq nil (mapcar #'semanticdb-elisp-sym->tag
  255. (all-completions prefix obarray)))))
  256. (cl-defmethod semanticdb-find-tags-by-class-method
  257. ((_table semanticdb-table-emacs-lisp) _class &optional tags)
  258. "In TABLE, find all occurrences of tags of CLASS.
  259. Optional argument TAGS is a list of tags to search.
  260. Returns a table of all matching tags."
  261. (if tags (cl-call-next-method)
  262. ;; We could implement this, but it could be messy.
  263. nil))
  264. ;;; Deep Searches
  265. ;;
  266. ;; For Emacs Lisp deep searches are like top level searches.
  267. (cl-defmethod semanticdb-deep-find-tags-by-name-method
  268. ((table semanticdb-table-emacs-lisp) name &optional tags)
  269. "Find all tags name NAME in TABLE.
  270. Optional argument TAGS is a list of tags to search.
  271. Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
  272. (semanticdb-find-tags-by-name-method table name tags))
  273. (cl-defmethod semanticdb-deep-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. Like `semanticdb-find-tags-by-name-method' for Emacs Lisp."
  278. (semanticdb-find-tags-by-name-regexp-method table regex tags))
  279. (cl-defmethod semanticdb-deep-find-tags-for-completion-method
  280. ((table semanticdb-table-emacs-lisp) prefix &optional tags)
  281. "In TABLE, find all occurrences of tags matching PREFIX.
  282. Optional argument TAGS is a list of tags to search.
  283. Like `semanticdb-find-tags-for-completion-method' for Emacs Lisp."
  284. (semanticdb-find-tags-for-completion-method table prefix tags))
  285. ;;; Advanced Searches
  286. ;;
  287. (cl-defmethod semanticdb-find-tags-external-children-of-type-method
  288. ((_table semanticdb-table-emacs-lisp) type &optional tags)
  289. "Find all nonterminals which are child elements of TYPE
  290. Optional argument TAGS is a list of tags to search.
  291. Return a list of tags."
  292. (if tags (cl-call-next-method)
  293. ;; EIEIO is the only time this matters
  294. (when (featurep 'eieio)
  295. (let* ((class (intern-soft type))
  296. (taglst (when class
  297. (delq nil
  298. (mapcar #'semanticdb-elisp-sym->tag
  299. ;; Fancy eieio function that knows all about
  300. ;; built in methods belonging to CLASS.
  301. (cl-generic-all-functions class)))))
  302. )
  303. taglst))))
  304. (provide 'semantic/db-el)
  305. ;;; semantic/db-el.el ends here