db-el.el 12 KB

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