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