cedet-cscope.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; cedet-cscope.el --- CScope support for CEDET
  2. ;;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Package: cedet
  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. ;; Support using CScope for symbol lookups.
  19. ;;; Code:
  20. (declare-function inversion-check-version "inversion")
  21. (defvar cedet-cscope-min-version "16.0"
  22. "Minimum version of CScope required.")
  23. (defcustom cedet-cscope-command "cscope"
  24. "Command name for the CScope executable."
  25. :type 'string
  26. :group 'cedet)
  27. (defun cedet-cscope-search (searchtext texttype type scope)
  28. "Perform a search with CScope, return the created buffer.
  29. SEARCHTEXT is text to find.
  30. TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
  31. 'tagregexp, or 'tagcompletions.
  32. TYPE is the type of search, meaning that SEARCHTEXT is compared to
  33. filename, tagname (tags table), references (uses of a tag) , or
  34. symbol (uses of something not in the tag table.)
  35. SCOPE is the scope of the search, such as 'project or 'subdirs."
  36. ;; CScope is an interactive program. It uses number flags
  37. ;; in order to perform command line searches. Useful for this
  38. ;; tool are:
  39. ;;
  40. ;; -0 = Find C symbol
  41. ;; -1 = Find global definition
  42. ;; -3 = Find references
  43. ;; -6 = Find egrep pattern
  44. ;; -7 = Find file
  45. (let ((idx (cond ((eq type 'file)
  46. "-7")
  47. ;; Non files are symbols and such
  48. ((eq texttype 'tagname)
  49. "-1")
  50. ((eq texttype 'tagregexp)
  51. "-0")
  52. ((eq texttype 'tagcompletions)
  53. (setq searchtext (concat "^" searchtext ".*"))
  54. "-1")
  55. ((eq texttype 'regexp)
  56. "-5")
  57. (t
  58. "-3")
  59. )
  60. )
  61. )
  62. (cedet-cscope-call (list "-d" "-L" idx searchtext))))
  63. (defun cedet-cscope-create (flags)
  64. "Create a CScope database at the current directory.
  65. FLAGS are additional flags to pass to cscope beyond the
  66. options -cR."
  67. (cedet-cscope-call (append (list "-cR") flags)))
  68. (defun cedet-cscope-call (flags)
  69. "Call CScope with the list of FLAGS."
  70. (let ((b (get-buffer-create "*CEDET CScope*"))
  71. (cd default-directory)
  72. )
  73. (with-current-buffer b
  74. (setq default-directory cd)
  75. (erase-buffer))
  76. (apply 'call-process cedet-cscope-command
  77. nil b nil
  78. flags)
  79. b))
  80. (defun cedet-cscope-expand-filename (filename)
  81. "Expand the FILENAME with CScope.
  82. Return a fully qualified filename."
  83. (interactive "sFile: ")
  84. (let* ((ans1 (with-current-buffer
  85. (cedet-cscope-call (list "-d" "-L" "-7" filename))
  86. (goto-char (point-min))
  87. (if (looking-at "[^ \n]*cscope: ")
  88. (error "CScope not available")
  89. (split-string (buffer-string) "\n" t))))
  90. (ans2 (mapcar (lambda (hit)
  91. (expand-file-name (car (split-string hit " "))))
  92. ans1)))
  93. (when (called-interactively-p 'interactive)
  94. (if ans2
  95. (if (= (length ans2) 1)
  96. (message "%s" (car ans2))
  97. (message "%s + %d others" (car ans2)
  98. (length (cdr ans2))))
  99. (error "No file found")))
  100. ans2))
  101. (defun cedet-cscope-support-for-directory (&optional dir)
  102. "Return non-nil if CScope has a support file for DIR.
  103. If DIR is not supplied, use the current default directory.
  104. This works by running cscope on a bogus symbol, and looking for
  105. the error code."
  106. (interactive "DDirectory: ")
  107. (save-excursion
  108. (let ((default-directory (or dir default-directory)))
  109. (set-buffer (cedet-cscope-call (list "-d" "-L" "-7" "moose")))
  110. (goto-char (point-min))
  111. (let ((ans (looking-at "[^ \n]*cscope: ")))
  112. (if (called-interactively-p 'interactive)
  113. (if ans
  114. (message "No support for CScope in %s" default-directory)
  115. (message "CScope is supported in %s" default-directory))
  116. (if ans
  117. nil
  118. t))))))
  119. (defun cedet-cscope-version-check (&optional noerror)
  120. "Check the version of the installed CScope command.
  121. If optional programmatic argument NOERROR is non-nil,
  122. then instead of throwing an error if CScope isn't available,
  123. return nil."
  124. (interactive)
  125. (require 'inversion)
  126. (let ((b (condition-case nil
  127. (cedet-cscope-call (list "-V"))
  128. (error nil)))
  129. (rev nil))
  130. (if (not b)
  131. (progn
  132. (when (called-interactively-p 'interactive)
  133. (message "CScope not found."))
  134. nil)
  135. (with-current-buffer b
  136. (goto-char (point-min))
  137. (re-search-forward "cscope: version \\([0-9.]+\\)" nil t)
  138. (setq rev (match-string 1))
  139. (if (inversion-check-version rev nil cedet-cscope-min-version)
  140. (if noerror
  141. nil
  142. (error "Version of CScope is %s. Need at least %s"
  143. rev cedet-cscope-min-version))
  144. ;; Else, return TRUE, as in good enough.
  145. (when (called-interactively-p 'interactive)
  146. (message "CScope %s - Good enough for CEDET." rev))
  147. t)))))
  148. (defun cedet-cscope-create/update-database (&optional dir)
  149. "Create a CScope database in DIR.
  150. CScope will automatically choose incremental rebuild if
  151. there is already a database in DIR."
  152. (interactive "DDirectory: ")
  153. (let ((default-directory dir))
  154. (cedet-cscope-create nil)))
  155. (provide 'cedet-cscope)
  156. ;;; cedet-cscope.el ends here