util.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. ;;; semantic/util.el --- Utilities for use with semantic tag tables
  2. ;;; Copyright (C) 1999-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: syntax
  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. ;; Semantic utility API for use with semantic tag tables.
  19. ;;
  20. (require 'semantic)
  21. (eval-when-compile
  22. (require 'semantic/db-find)
  23. ;; For semantic-find-tags-by-class, semantic--find-tags-by-function,
  24. ;; and semantic-brute-find-tag-standard:
  25. (require 'semantic/find))
  26. (declare-function data-debug-insert-stuff-list "data-debug")
  27. (declare-function data-debug-insert-thing "data-debug")
  28. (declare-function semantic-ctxt-current-symbol-and-bounds "semantic/ctxt")
  29. ;;; Code:
  30. (defvar semantic-type-relation-separator-character '(".")
  31. "Character strings used to separate a parent/child relationship.
  32. This list of strings are used for displaying or finding separators
  33. in variable field dereferencing. The first character will be used for
  34. display. In C, a type field is separated like this: \"type.field\"
  35. thus, the character is a \".\". In C, and additional value of \"->\"
  36. would be in the list, so that \"type->field\" could be found.")
  37. (make-variable-buffer-local 'semantic-type-relation-separator-character)
  38. (defvar semantic-equivalent-major-modes nil
  39. "List of major modes which are considered equivalent.
  40. Equivalent modes share a parser, and a set of override methods.
  41. A value of nil means that the current major mode is the only one.")
  42. (make-variable-buffer-local 'semantic-equivalent-major-modes)
  43. ;; These semanticdb calls will throw warnings in the byte compiler.
  44. ;; Doing the right thing to make them available at compile time
  45. ;; really messes up the compilation sequence.
  46. (defun semantic-file-tag-table (file)
  47. "Return a tag table for FILE.
  48. If it is loaded, return the stream after making sure it's ok.
  49. If FILE is not loaded, check to see if `semanticdb' feature exists,
  50. and use it to get tags from files not in memory.
  51. If FILE is not loaded, and semanticdb is not available, find the file
  52. and parse it."
  53. (save-match-data
  54. (if (find-buffer-visiting file)
  55. (with-current-buffer (find-buffer-visiting file)
  56. (semantic-fetch-tags))
  57. ;; File not loaded
  58. (if (and (require 'semantic/db-mode)
  59. (semanticdb-minor-mode-p))
  60. ;; semanticdb is around, use it.
  61. (semanticdb-file-stream file)
  62. ;; Get the stream ourselves.
  63. (with-current-buffer (find-file-noselect file)
  64. (semantic-fetch-tags))))))
  65. (semantic-alias-obsolete 'semantic-file-token-stream
  66. 'semantic-file-tag-table "23.2")
  67. (defun semantic-something-to-tag-table (something)
  68. "Convert SOMETHING into a semantic tag table.
  69. Something can be a tag with a valid BUFFER property, a tag table, a
  70. buffer, or a filename. If SOMETHING is nil return nil."
  71. (cond
  72. ;; A list of tags
  73. ((and (listp something)
  74. (semantic-tag-p (car something)))
  75. something)
  76. ;; A buffer
  77. ((bufferp something)
  78. (with-current-buffer something
  79. (semantic-fetch-tags)))
  80. ;; A Tag: Get that tag's buffer
  81. ((and (semantic-tag-with-position-p something)
  82. (semantic-tag-in-buffer-p something))
  83. (with-current-buffer (semantic-tag-buffer something)
  84. (semantic-fetch-tags)))
  85. ;; Tag with a file name in it
  86. ((and (semantic-tag-p something)
  87. (semantic-tag-file-name something)
  88. (file-exists-p (semantic-tag-file-name something)))
  89. (semantic-file-tag-table
  90. (semantic-tag-file-name something)))
  91. ;; A file name
  92. ((and (stringp something)
  93. (file-exists-p something))
  94. (semantic-file-tag-table something))
  95. ;; A Semanticdb table
  96. ((and (featurep 'semantic/db)
  97. (semanticdb-minor-mode-p)
  98. (semanticdb-abstract-table-child-p something))
  99. (semanticdb-refresh-table something)
  100. (semanticdb-get-tags something))
  101. ;; Semanticdb find-results
  102. ((and (featurep 'semantic/db)
  103. (semanticdb-minor-mode-p)
  104. (require 'semantic/db-find)
  105. (semanticdb-find-results-p something))
  106. (semanticdb-strip-find-results something))
  107. ;; NOTE: This commented out since if a search result returns
  108. ;; empty, that empty would turn into everything on the next search.
  109. ;; Use the current buffer for nil
  110. ;; ((null something)
  111. ;; (semantic-fetch-tags))
  112. ;; don't know what it is
  113. (t nil)))
  114. (semantic-alias-obsolete 'semantic-something-to-stream
  115. 'semantic-something-to-tag-table "23.2")
  116. ;;; Completion APIs
  117. ;;
  118. ;; These functions provide minibuffer reading/completion for lists of
  119. ;; nonterminals.
  120. (defvar semantic-read-symbol-history nil
  121. "History for a symbol read.")
  122. (defun semantic-read-symbol (prompt &optional default stream filter)
  123. "Read a symbol name from the user for the current buffer.
  124. PROMPT is the prompt to use.
  125. Optional arguments:
  126. DEFAULT is the default choice. If no default is given, one is read
  127. from under point.
  128. STREAM is the list of tokens to complete from.
  129. FILTER is provides a filter on the types of things to complete.
  130. FILTER must be a function to call on each element."
  131. (if (not default) (setq default (thing-at-point 'symbol)))
  132. (if (not stream) (setq stream (semantic-fetch-tags)))
  133. (setq stream
  134. (if filter
  135. (semantic--find-tags-by-function filter stream)
  136. (semantic-brute-find-tag-standard stream)))
  137. (if (and default (string-match ":" prompt))
  138. (setq prompt
  139. (concat (substring prompt 0 (match-end 0))
  140. " (default: " default ") ")))
  141. (completing-read prompt stream nil t ""
  142. 'semantic-read-symbol-history
  143. default))
  144. (defun semantic-read-variable (prompt &optional default stream)
  145. "Read a variable name from the user for the current buffer.
  146. PROMPT is the prompt to use.
  147. Optional arguments:
  148. DEFAULT is the default choice. If no default is given, one is read
  149. from under point.
  150. STREAM is the list of tokens to complete from."
  151. (semantic-read-symbol
  152. prompt default
  153. (or (semantic-find-tags-by-class
  154. 'variable (or stream (current-buffer)))
  155. (error "No local variables"))))
  156. (defun semantic-read-function (prompt &optional default stream)
  157. "Read a function name from the user for the current buffer.
  158. PROMPT is the prompt to use.
  159. Optional arguments:
  160. DEFAULT is the default choice. If no default is given, one is read
  161. from under point.
  162. STREAM is the list of tags to complete from."
  163. (semantic-read-symbol
  164. prompt default
  165. (or (semantic-find-tags-by-class
  166. 'function (or stream (current-buffer)))
  167. (error "No local functions"))))
  168. (defun semantic-read-type (prompt &optional default stream)
  169. "Read a type name from the user for the current buffer.
  170. PROMPT is the prompt to use.
  171. Optional arguments:
  172. DEFAULT is the default choice. If no default is given, one is read
  173. from under point.
  174. STREAM is the list of tags to complete from."
  175. (semantic-read-symbol
  176. prompt default
  177. (or (semantic-find-tags-by-class
  178. 'type (or stream (current-buffer)))
  179. (error "No local types"))))
  180. ;;; Interactive Functions for
  181. ;;
  182. (defun semantic-describe-tag (&optional tag)
  183. "Describe TAG in the minibuffer.
  184. If TAG is nil, describe the tag under the cursor."
  185. (interactive)
  186. (if (not tag) (setq tag (semantic-current-tag)))
  187. (semantic-fetch-tags)
  188. (if tag (message (semantic-format-tag-summarize tag))))
  189. ;;; Putting keys on tags.
  190. ;;
  191. (defun semantic-add-label (label value &optional tag)
  192. "Add a LABEL with VALUE on TAG.
  193. If TAG is not specified, use the tag at point."
  194. (interactive "sLabel: \nXValue (eval): ")
  195. (if (not tag)
  196. (progn
  197. (semantic-fetch-tags)
  198. (setq tag (semantic-current-tag))))
  199. (semantic--tag-put-property tag (intern label) value)
  200. (message "Added label %s with value %S" label value))
  201. (defun semantic-show-label (label &optional tag)
  202. "Show the value of LABEL on TAG.
  203. If TAG is not specified, use the tag at point."
  204. (interactive "sLabel: ")
  205. (if (not tag)
  206. (progn
  207. (semantic-fetch-tags)
  208. (setq tag (semantic-current-tag))))
  209. (message "%s: %S" label (semantic--tag-get-property tag (intern label))))
  210. ;;; Hacks
  211. ;;
  212. ;; Some hacks to help me test these functions
  213. (defun semantic-describe-buffer-var-helper (varsym buffer)
  214. "Display to standard out the value of VARSYM in BUFFER."
  215. (require 'data-debug)
  216. (let ((value (with-current-buffer buffer
  217. (symbol-value varsym))))
  218. (cond
  219. ((and (consp value)
  220. (< (length value) 10))
  221. ;; Draw the list of things in the list.
  222. (princ (format " %s: #<list of %d items>\n"
  223. varsym (length value)))
  224. (data-debug-insert-stuff-list
  225. value " " )
  226. )
  227. (t
  228. ;; Else do a one-liner.
  229. (data-debug-insert-thing
  230. value " " (concat " " (symbol-name varsym) ": "))
  231. ))))
  232. (defun semantic-describe-buffer ()
  233. "Describe the semantic environment for the current buffer."
  234. (interactive)
  235. (let ((buff (current-buffer))
  236. )
  237. (with-output-to-temp-buffer (help-buffer)
  238. (help-setup-xref (list #'semantic-describe-buffer)
  239. (called-interactively-p 'interactive))
  240. (with-current-buffer standard-output
  241. (princ "Semantic Configuration in ")
  242. (princ (buffer-name buff))
  243. (princ "\n\n")
  244. (princ "Buffer specific configuration items:\n")
  245. (let ((vars '(major-mode
  246. semantic-case-fold
  247. semantic-tag-expand-function
  248. semantic-parser-name
  249. semantic-parse-tree-state
  250. semantic-lex-analyzer
  251. semantic-lex-reset-hooks
  252. semantic-lex-syntax-modifications
  253. )))
  254. (dolist (V vars)
  255. (semantic-describe-buffer-var-helper V buff)))
  256. (princ "\nGeneral configuration items:\n")
  257. (let ((vars '(semantic-inhibit-functions
  258. semantic-init-hook
  259. semantic-init-db-hook
  260. semantic-unmatched-syntax-hook
  261. semantic--before-fetch-tags-hook
  262. semantic-after-toplevel-bovinate-hook
  263. semantic-after-toplevel-cache-change-hook
  264. semantic-before-toplevel-cache-flush-hook
  265. semantic-dump-parse
  266. semantic-type-relation-separator-character
  267. semantic-command-separation-character
  268. )))
  269. (dolist (V vars)
  270. (semantic-describe-buffer-var-helper V buff)))
  271. (princ "\n\n")
  272. (mode-local-describe-bindings-2 buff)
  273. )))
  274. )
  275. (defun semantic-assert-valid-token (tok)
  276. "Assert that TOK is a valid token."
  277. (if (semantic-tag-p tok)
  278. (if (semantic-tag-with-position-p tok)
  279. (let ((o (semantic-tag-overlay tok)))
  280. (if (and (semantic-overlay-p o)
  281. (not (semantic-overlay-live-p o)))
  282. (let ((debug-on-error t))
  283. (error "Tag %s is invalid!" (semantic-tag-name tok)))
  284. ;; else, tag is OK.
  285. ))
  286. ;; Positionless tags are also ok.
  287. )
  288. (let ((debug-on-error t))
  289. (error "Not a semantic tag: %S" tok))))
  290. (defun semantic-sanity-check (&optional cache over notfirst)
  291. "Perform a sanity check on the current buffer.
  292. The buffer's set of overlays, and those overlays found via the cache
  293. are verified against each other.
  294. CACHE, and OVER are the semantic cache, and the overlay list.
  295. NOTFIRST indicates that this was not the first call in the recursive use."
  296. (interactive)
  297. (if (and (not cache) (not over) (not notfirst))
  298. (setq cache semantic--buffer-cache
  299. over (semantic-overlays-in (point-min) (point-max))))
  300. (while cache
  301. (let ((chil (semantic-tag-components-with-overlays (car cache))))
  302. (if (not (memq (semantic-tag-overlay (car cache)) over))
  303. (message "Tag %s not in buffer overlay list."
  304. (semantic-format-tag-concise-prototype (car cache))))
  305. (setq over (delq (semantic-tag-overlay (car cache)) over))
  306. (setq over (semantic-sanity-check chil over t))
  307. (setq cache (cdr cache))))
  308. (if (not notfirst)
  309. ;; Strip out all overlays which aren't semantic overlays
  310. (let ((o nil))
  311. (while over
  312. (when (and (semantic-overlay-get (car over) 'semantic)
  313. (not (eq (semantic-overlay-get (car over) 'semantic)
  314. 'unmatched)))
  315. (setq o (cons (car over) o)))
  316. (setq over (cdr over)))
  317. (when (called-interactively-p 'any)
  318. (message "Remaining overlays: %S" o))))
  319. over)
  320. ;;; Interactive commands (from Senator).
  321. ;; The Senator library from upstream CEDET is not included in the
  322. ;; built-in version of Emacs. The plan is to fold it into the
  323. ;; different parts of CEDET and Emacs, so that it works
  324. ;; "transparently". Here are some interactive commands based on
  325. ;; Senator.
  326. ;; Symbol completion
  327. (defun semantic-find-tag-for-completion (prefix)
  328. "Find all tags with name starting with PREFIX.
  329. This uses `semanticdb' when available."
  330. (let (result ctxt)
  331. ;; Try the Semantic analyzer
  332. (condition-case nil
  333. (and (featurep 'semantic/analyze)
  334. (setq ctxt (semantic-analyze-current-context))
  335. (setq result (semantic-analyze-possible-completions ctxt)))
  336. (error nil))
  337. (or result
  338. ;; If the analyzer fails, then go into boring completion.
  339. (if (and (featurep 'semantic/db)
  340. (semanticdb-minor-mode-p)
  341. (require 'semantic/db-find))
  342. (semanticdb-fast-strip-find-results
  343. (semanticdb-deep-find-tags-for-completion prefix))
  344. (semantic-deep-find-tags-for-completion prefix (current-buffer))))))
  345. (defun semantic-complete-symbol (&optional predicate)
  346. "Complete the symbol under point, using Semantic facilities.
  347. When called from a program, optional arg PREDICATE is a predicate
  348. determining which symbols are considered."
  349. (interactive)
  350. (require 'semantic/ctxt)
  351. (let* ((start (car (nth 2 (semantic-ctxt-current-symbol-and-bounds
  352. (point)))))
  353. (pattern (regexp-quote (buffer-substring start (point))))
  354. collection completion)
  355. (when start
  356. (if (and semantic--completion-cache
  357. (eq (nth 0 semantic--completion-cache) (current-buffer))
  358. (= (nth 1 semantic--completion-cache) start)
  359. (save-excursion
  360. (goto-char start)
  361. (looking-at (nth 3 semantic--completion-cache))))
  362. ;; Use cached value.
  363. (setq collection (nthcdr 4 semantic--completion-cache))
  364. ;; Perform new query.
  365. (setq collection (semantic-find-tag-for-completion pattern))
  366. (setq semantic--completion-cache
  367. (append (list (current-buffer) start 0 pattern)
  368. collection))))
  369. (if (null collection)
  370. (let ((str (if pattern (format " for \"%s\"" pattern) "")))
  371. (if (window-minibuffer-p (selected-window))
  372. (minibuffer-message (format " [No completions%s]" str))
  373. (message "Can't find completion%s" str)))
  374. (setq completion (try-completion pattern collection predicate))
  375. (if (string= pattern completion)
  376. (let ((list (all-completions pattern collection predicate)))
  377. (setq list (sort list 'string<))
  378. (if (> (length list) 1)
  379. (with-output-to-temp-buffer "*Completions*"
  380. (display-completion-list list pattern))
  381. ;; Bury any out-of-date completions buffer.
  382. (let ((win (get-buffer-window "*Completions*" 0)))
  383. (if win (with-selected-window win (bury-buffer))))))
  384. ;; Exact match
  385. (delete-region start (point))
  386. (insert completion)
  387. ;; Bury any out-of-date completions buffer.
  388. (let ((win (get-buffer-window "*Completions*" 0)))
  389. (if win (with-selected-window win (bury-buffer))))))))
  390. (provide 'semantic/util)
  391. ;;; Minor modes
  392. ;;
  393. (require 'semantic/util-modes)
  394. ;;; semantic/util.el ends here