cedet-idutils.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ;;; cedet-idutils.el --- ID Utils support for CEDET.
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; Version: 0.2
  5. ;; Keywords: OO, lisp
  6. ;; Package: cedet
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; Basic support calling ID Utils functions, and checking version
  21. ;; numbers.
  22. ;;; Code:
  23. (declare-function inversion-check-version "inversion")
  24. (defvar cedet-idutils-min-version "4.0"
  25. "Minimum version of ID Utils required.")
  26. (defcustom cedet-idutils-file-command "fnid"
  27. "Command name for the ID Utils executable for searching file names."
  28. :type 'string
  29. :group 'cedet)
  30. (defcustom cedet-idutils-token-command "lid"
  31. "Command name for the ID Utils executable for searching for tokens."
  32. :type 'string
  33. :group 'cedet)
  34. (defcustom cedet-idutils-make-command "mkid"
  35. "Command name for the ID Utils executable for creating token databases."
  36. :type 'string
  37. :group 'cedet)
  38. (defun cedet-idutils-search (searchtext texttype type scope)
  39. "Perform a search with ID Utils, return the created buffer.
  40. SEARCHTEXT is text to find.
  41. TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
  42. 'tagregexp, or 'tagcompletions.
  43. TYPE is the type of search, meaning that SEARCHTEXT is compared to
  44. filename, tagname (tags table), references (uses of a tag) , or
  45. symbol (uses of something not in the tag table.)
  46. SCOPE is the scope of the search, such as 'project or 'subdirs.
  47. Note: Scope is not yet supported."
  48. (if (eq type 'file)
  49. ;; Calls for file stuff is very simple.
  50. (cedet-idutils-fnid-call (list searchtext))
  51. ;; Calls for text searches is more complex.
  52. (let* ((resultflg (if (eq texttype 'tagcompletions)
  53. (list "--key=token")
  54. (list "--result=grep")))
  55. (scopeflgs nil) ; (cond ((eq scope 'project) "" ) ((eq scope 'target) "l")))
  56. (stflag (cond ((or (eq texttype 'tagname)
  57. (eq texttype 'tagregexp))
  58. (list "-r" "-w"))
  59. ((eq texttype 'tagcompletions)
  60. ;; Add regex to search text for beginning of char.
  61. (setq searchtext (concat "^" searchtext))
  62. (list "-r" "-s" ))
  63. ((eq texttype 'regexp)
  64. (list "-r"))
  65. ;; t means 'symbol
  66. (t (list "-l" "-w"))))
  67. )
  68. (cedet-idutils-lid-call (append resultflg scopeflgs stflag
  69. (list searchtext))))))
  70. (defun cedet-idutils-fnid-call (flags)
  71. "Call ID Utils fnid with the list of FLAGS.
  72. Return the created buffer with program output."
  73. (let ((b (get-buffer-create "*CEDET fnid*"))
  74. (cd default-directory)
  75. )
  76. (with-current-buffer b
  77. (setq default-directory cd)
  78. (erase-buffer))
  79. (apply 'call-process cedet-idutils-file-command
  80. nil b nil
  81. flags)
  82. b))
  83. (defun cedet-idutils-lid-call (flags)
  84. "Call ID Utils lid with the list of FLAGS.
  85. Return the created buffer with with program output."
  86. (let ((b (get-buffer-create "*CEDET lid*"))
  87. (cd default-directory)
  88. )
  89. (with-current-buffer b
  90. (setq default-directory cd)
  91. (erase-buffer))
  92. (apply 'call-process cedet-idutils-token-command
  93. nil b nil
  94. flags)
  95. b))
  96. (defun cedet-idutils-mkid-call (flags)
  97. "Call ID Utils mkid with the list of FLAGS.
  98. Return the created buffer with program output."
  99. (let ((b (get-buffer-create "*CEDET mkid*"))
  100. (cd default-directory)
  101. )
  102. (with-current-buffer b
  103. (setq default-directory cd)
  104. (erase-buffer))
  105. (apply 'call-process cedet-idutils-make-command
  106. nil b nil
  107. flags)
  108. b))
  109. ;;; UTIL CALLS
  110. ;;
  111. (defun cedet-idutils-expand-filename (filename)
  112. "Expand the FILENAME with ID Utils.
  113. Return a filename relative to the default directory."
  114. (interactive "sFile: ")
  115. (let ((ans (with-current-buffer (cedet-idutils-fnid-call (list filename))
  116. (goto-char (point-min))
  117. (if (looking-at "[^ \n]*fnid: ")
  118. (error "ID Utils not available")
  119. (split-string (buffer-string) "\n" t)))))
  120. (setq ans (mapcar 'expand-file-name ans))
  121. (when (called-interactively-p 'interactive)
  122. (if ans
  123. (if (= (length ans) 1)
  124. (message "%s" (car ans))
  125. (message "%s + %d others" (car ans)
  126. (length (cdr ans))))
  127. (error "No file found")))
  128. ans))
  129. (defun cedet-idutils-support-for-directory (&optional dir)
  130. "Return non-nil if ID Utils has a support file for DIR.
  131. If DIR is not supplied, use the current default directory.
  132. This works by running lid on a bogus symbol, and looking for
  133. the error code."
  134. (save-excursion
  135. (let ((default-directory (or dir default-directory)))
  136. (condition-case nil
  137. (progn
  138. (set-buffer (cedet-idutils-fnid-call '("moose")))
  139. (goto-char (point-min))
  140. (if (looking-at "[^ \n]*fnid: ")
  141. nil
  142. t))
  143. (error nil)))))
  144. (defun cedet-idutils-version-check (&optional noerror)
  145. "Check the version of the installed ID Utils command.
  146. If optional programmatic argument NOERROR is non-nil,
  147. then instead of throwing an error if Global isn't available,
  148. return nil."
  149. (interactive)
  150. (require 'inversion)
  151. (let ((b (condition-case nil
  152. (cedet-idutils-fnid-call (list "--version"))
  153. (error nil)))
  154. (rev nil))
  155. (if (not b)
  156. (progn
  157. (when (called-interactively-p 'interactive)
  158. (message "ID Utils not found."))
  159. nil)
  160. (with-current-buffer b
  161. (goto-char (point-min))
  162. (re-search-forward "fnid - \\([0-9.]+\\)" nil t)
  163. (setq rev (match-string 1))
  164. (if (inversion-check-version rev nil cedet-idutils-min-version)
  165. (if noerror
  166. nil
  167. (error "Version of ID Utils is %s. Need at least %s"
  168. rev cedet-idutils-min-version))
  169. ;; Else, return TRUE, as in good enough.
  170. (when (called-interactively-p 'interactive)
  171. (message "ID Utils %s - Good enough for CEDET." rev))
  172. t)))))
  173. (defun cedet-idutils-create/update-database (&optional dir)
  174. "Create an IDUtils database in DIR.
  175. IDUtils must start from scratch when updating a database."
  176. (interactive "DDirectory: ")
  177. (let ((default-directory dir))
  178. (cedet-idutils-mkid-call nil)))
  179. (provide 'cedet-idutils)
  180. ;;; cedet-idutils.el ends here