Fix last change to use semantic-varalias-obsolete.
[bpt/emacs.git] / lisp / cedet / cedet-cscope.el
1 ;;; cedet-cscope.el --- CScope support for CEDET
2
3 ;;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Support using CScope for symbol lookups.
25
26 ;;; Code:
27
28 (defvar cedet-cscope-min-version "16.0"
29 "Minimum version of GNU global required.")
30
31 (defcustom cedet-cscope-command "cscope"
32 "Command name for the CScope executable."
33 :type 'string
34 :group 'cedet)
35
36 (defun cedet-cscope-search (searchtext texttype type scope)
37 "Perform a search with CScope, return the created buffer.
38 SEARCHTEXT is text to find.
39 TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
40 'tagregexp, or 'tagcompletions.
41 TYPE is the type of search, meaning that SEARCHTEXT is compared to
42 filename, tagname (tags table), references (uses of a tag) , or
43 symbol (uses of something not in the tag table.)
44 SCOPE is the scope of the search, such as 'project or 'subdirs."
45 ;; CScope is an interactive program. It uses number flags
46 ;; in order to perform command line searches. Useful for this
47 ;; tool are:
48 ;;
49 ;; -0 = Find C symbol
50 ;; -1 = Find global definition
51 ;; -3 = Find references
52 ;; -6 = Find egrep pattern
53 ;; -7 = Find file
54 (let ((idx (cond ((eq type 'file)
55 "-7")
56 ;; Non files are symbols and such
57 ((eq texttype 'tagname)
58 "-1")
59 ((eq texttype 'tagregexp)
60 "-0")
61 ((eq texttype 'tagcompletions)
62 (setq searchtext (concat "^" searchtext ".*"))
63 "-1")
64 ((eq texttype 'regexp)
65 "-5")
66 (t
67 "-3")
68 )
69 )
70 )
71 (cedet-cscope-call (list "-d" "-L" idx searchtext))))
72
73 (defun cedet-cscope-call (flags)
74 "Call CScope with the list of FLAGS."
75 (let ((b (get-buffer-create "*CEDET CScope*"))
76 (cd default-directory)
77 )
78 (save-excursion
79 (set-buffer b)
80 (setq default-directory cd)
81 (erase-buffer))
82 (apply 'call-process cedet-cscope-command
83 nil b nil
84 flags)
85 b))
86
87 (defun cedet-cscope-expand-filename (filename)
88 "Expand the FILENAME with CScope.
89 Return a fully qualified filename."
90 (interactive "sFile: ")
91 (let* ((ans1 (save-excursion
92 (set-buffer (cedet-cscope-call (list "-d" "-L" "-7" filename)))
93 (goto-char (point-min))
94 (if (looking-at "[^ \n]*cscope: ")
95 (error "CScope not available")
96 (split-string (buffer-string) "\n" t))))
97 (ans2 (mapcar (lambda (hit)
98 (expand-file-name (car (split-string hit " "))))
99 ans1)))
100 (when (interactive-p)
101 (if ans2
102 (if (= (length ans2) 1)
103 (message "%s" (car ans2))
104 (message "%s + %d others" (car ans2)
105 (length (cdr ans2))))
106 (error "No file found")))
107 ans2))
108
109 (defun cedet-cscope-support-for-directory (&optional dir)
110 "Return non-nil if CScope has a support file for DIR.
111 If DIR is not supplied, use the current default directory.
112 This works by running cscope on a bogus symbol, and looking for
113 the error code."
114 (save-excursion
115 (let ((default-directory (or dir default-directory)))
116 (set-buffer (cedet-cscope-call (list "-d" "-L" "-7" "moose")))
117 (goto-char (point-min))
118 (if (looking-at "[^ \n]*cscope: ")
119 nil
120 t))))
121
122 (declare-function inversion-check-version "inversion")
123
124 (defun cedet-cscope-version-check (&optional noerror)
125 "Check the version of the installed CScope command.
126 If optional programatic argument NOERROR is non-nil, then
127 instead of throwing an error if Global isn't available, then
128 return nil."
129 (interactive)
130 (require 'inversion)
131 (let ((b (condition-case nil
132 (cedet-cscope-call (list "-V"))
133 (error nil)))
134 (rev nil))
135 (if (not b)
136 (progn
137 (when (interactive-p)
138 (message "CScope not found."))
139 nil)
140 (save-excursion
141 (set-buffer b)
142 (goto-char (point-min))
143 (re-search-forward "cscope: version \\([0-9.]+\\)" nil t)
144 (setq rev (match-string 1))
145 (if (inversion-check-version rev nil cedet-cscope-min-version)
146 (if noerror
147 nil
148 (error "Version of CScope is %s. Need at least %s"
149 rev cedet-cscope-min-version))
150 ;; Else, return TRUE, as in good enough.
151 (when (interactive-p)
152 (message "CScope %s - Good enough for CEDET." rev))
153 t)))))
154
155 (provide 'cedet-cscope)
156
157 ;;; cedet-cscope.el ends here