Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / cedet / cedet-idutils.el
blob44c325b78cd14b3f6d97fdea1232a5cf2b7f42cf
1 ;;; cedet-idutils.el --- ID Utils support for CEDET.
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6 ;; Version: 0.2
7 ;; Keywords: OO, lisp
8 ;; Package: cedet
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Basic support calling ID Utils functions, and checking version
28 ;; numbers.
30 ;;; Code:
32 (declare-function inversion-check-version "inversion")
34 (defvar cedet-idutils-min-version "4.0"
35 "Minimum version of ID Utils required.")
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)
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)
47 (defun cedet-idutils-search (searchtext texttype type scope)
48 "Perform a search with ID Utils, return the created buffer.
49 SEARCHTEXT is text to find.
50 TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
51 'tagregexp, or 'tagcompletions.
52 TYPE is the type of search, meaning that SEARCHTEXT is compared to
53 filename, tagname (tags table), references (uses of a tag) , or
54 symbol (uses of something not in the tag table.)
55 SCOPE is the scope of the search, such as 'project or 'subdirs.
56 Note: Scope is not yet supported."
57 (if (eq type 'file)
58 ;; Calls for file stuff is very simple.
59 (cedet-idutils-fnid-call (list searchtext))
60 ;; Calls for text searches is more complex.
61 (let* ((resultflg (if (eq texttype 'tagcompletions)
62 (list "--key=token")
63 (list "--result=grep")))
64 (scopeflgs nil) ; (cond ((eq scope 'project) "" ) ((eq scope 'target) "l")))
65 (stflag (cond ((or (eq texttype 'tagname)
66 (eq texttype 'tagregexp))
67 (list "-r" "-w"))
68 ((eq texttype 'tagcompletions)
69 ;; Add regex to search text for beginning of char.
70 (setq searchtext (concat "^" searchtext))
71 (list "-r" "-s" ))
72 ((eq texttype 'regexp)
73 (list "-r"))
74 ;; t means 'symbol
75 (t (list "-l" "-w"))))
77 (cedet-idutils-lid-call (append resultflg scopeflgs stflag
78 (list searchtext))))))
80 (defun cedet-idutils-fnid-call (flags)
81 "Call ID Utils fnid with the list of FLAGS.
82 Return the created buffer with with program output."
83 (let ((b (get-buffer-create "*CEDET fnid*"))
84 (cd default-directory)
86 (with-current-buffer b
87 (setq default-directory cd)
88 (erase-buffer))
89 (apply 'call-process cedet-idutils-file-command
90 nil b nil
91 flags)
92 b))
94 (defun cedet-idutils-lid-call (flags)
95 "Call ID Utils lid with the list of FLAGS.
96 Return the created buffer with with program output."
97 (let ((b (get-buffer-create "*CEDET lid*"))
98 (cd default-directory)
100 (with-current-buffer b
101 (setq default-directory cd)
102 (erase-buffer))
103 (apply 'call-process cedet-idutils-token-command
104 nil b nil
105 flags)
108 ;;; UTIL CALLS
110 (defun cedet-idutils-expand-filename (filename)
111 "Expand the FILENAME with ID Utils.
112 Return a filename relative to the default directory."
113 (interactive "sFile: ")
114 (let ((ans (with-current-buffer (cedet-idutils-fnid-call (list filename))
115 (goto-char (point-min))
116 (if (looking-at "[^ \n]*fnid: ")
117 (error "ID Utils not available")
118 (split-string (buffer-string) "\n" t)))))
119 (setq ans (mapcar 'expand-file-name ans))
120 (when (called-interactively-p 'interactive)
121 (if ans
122 (if (= (length ans) 1)
123 (message "%s" (car ans))
124 (message "%s + %d others" (car ans)
125 (length (cdr ans))))
126 (error "No file found")))
127 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: ")
143 (error nil)))))
145 (defun cedet-idutils-version-check (&optional noerror)
146 "Check the version of the installed ID Utils command.
147 If optional programatic argument NOERROR is non-nil, then
148 instead of throwing an error if Global isn't available, then
149 return nil."
150 (interactive)
151 (require 'inversion)
152 (let ((b (condition-case nil
153 (cedet-idutils-fnid-call (list "--version"))
154 (error nil)))
155 (rev nil))
156 (if (not b)
157 (progn
158 (when (called-interactively-p 'interactive)
159 (message "ID Utils not found."))
160 nil)
161 (with-current-buffer b
162 (goto-char (point-min))
163 (re-search-forward "fnid - \\([0-9.]+\\)" nil t)
164 (setq rev (match-string 1))
165 (if (inversion-check-version rev nil cedet-idutils-min-version)
166 (if noerror
168 (error "Version of ID Utils is %s. Need at least %s"
169 rev cedet-idutils-min-version))
170 ;; Else, return TRUE, as in good enough.
171 (when (called-interactively-p 'interactive)
172 (message "ID Utils %s - Good enough for CEDET." rev))
173 t)))))
176 (provide 'cedet-idutils)
178 ;; arch-tag: 663ca082-5b3d-4384-8710-cc74f990b501
179 ;;; cedet-idutils.el ends here