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