cedet/semantic/db-debug.el: Don't require semantic/db-mode, since
[bpt/emacs.git] / lisp / cedet / semantic / dep.el
CommitLineData
f273dfc6
CY
1;;; dep.el --- Methods for tracking dependencies (include files)
2
3;;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
6;; Keywords: syntax
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24;;
25;; Include tags (dependencies for a given source file) usually have
26;; some short name. The target file that it is dependent on is
27;; generally found on some sort of path controlled by the compiler or
28;; project.
29;;
30;; EDE or even ECB can control our project dependencies, and help us
31;; find file within the setting of a given project. For system
32;; dependencies, we need to depend on user supplied lists, which can
33;; manifest themselves in the form of system datatabases (from
34;; semanticdb.)
35;;
36;; Provide ways to track these different files here.
37
38(require 'semantic/tag)
39
40;;; Code:
41
42(defvar semantic-dependency-include-path nil
43 "Defines the include path used when searching for files.
44This should be a list of directories to search which is specific
45to the file being included.
46
47If `semantic-dependency-tag-file' is overridden for a given
48language, this path is most likely ignored.
49
50The above function, reguardless of being overriden, caches the
51located dependency file location in the tag property
52`dependency-file'. If you override this function, you do not
53need to implement your own cache. Each time the buffer is fully
54reparsed, the cache will be reset.
55
56TODO: use ffap.el to locate such items?
57
58NOTE: Obsolete this, or use as special user")
59(make-variable-buffer-local `semantic-dependency-include-path)
60
61(defvar semantic-dependency-system-include-path nil
62 "Defines the system include path.
63This should be set with either `defvar-mode-local', or with
64`semantic-add-system-include'.
65
66For mode authors, use
67`defcustom-mode-local-semantic-dependency-system-include-path'
68to create a mode-specific variable to control this.
69
70When searching for a file associated with a name found in an tag of
71class include, this path will be inspected for includes of type
72`system'. Some include tags are agnostic to this setting and will
73check both the project and system directories.")
74(make-variable-buffer-local `semantic-dependency-system-include-path)
75
76(defmacro defcustom-mode-local-semantic-dependency-system-include-path
77 (mode name value &optional docstring)
78 "Create a mode-local value of the system-dependency include path.
79MODE is the `major-mode' this name/value pairs is for.
80NAME is the name of the customizable value users will use.
81VALUE is the path (a list of strings) to add.
82DOCSTRING is a documentation string applied to the variable NAME
83users will customize.
84
85Creates a customizable variable users can customize that will
86keep semantic data structures up to date."
87 `(progn
88 ;; Create a variable users can customize.
89 (defcustom ,name ,value
90 ,docstring
91 :group (quote ,(intern (car (split-string (symbol-name mode) "-"))))
92 :group 'semantic
93 :type '(repeat (directory :tag "Directory"))
94 :set (lambda (sym val)
95 (set-default sym val)
96 (setq-mode-local ,mode
97 semantic-dependency-system-include-path
98 val)
99 (when (fboundp
100 'semantic-decoration-unparsed-include-do-reset)
101 (mode-local-map-mode-buffers
102 'semantic-decoration-unparsed-include-do-reset
103 (quote ,mode))))
104 )
105 ;; Set the variable to the default value.
106 (defvar-mode-local ,mode semantic-dependency-system-include-path
107 ,name
108 "System path to search for include files.")
109 ;; Bind NAME onto our variable so tools can customize it
110 ;; without knowing about it.
111 (put 'semantic-dependency-system-include-path
112 (quote ,mode) (quote ,name))
113 ))
114
115;;; PATH MANAGEMENT
116;;
117;; Some fcns to manage paths for a give mode.
118(defun semantic-add-system-include (dir &optional mode)
119 "Add a system include DIR to path for MODE.
120Modifies a mode-local version of `semantic-dependency-system-include-path'.
121
122Changes made by this function are not persistent."
123 (interactive "DNew Include Directory: ")
124 (if (not mode) (setq mode major-mode))
125 (let ((dirtmp (file-name-as-directory dir))
126 (value
127 (mode-local-value mode 'semantic-dependency-system-include-path))
128 )
129 (add-to-list 'value dirtmp t)
130 (eval `(setq-mode-local ,mode
131 semantic-dependency-system-include-path value))
132 ))
133
134(defun semantic-remove-system-include (dir &optional mode)
135 "Add a system include DIR to path for MODE.
136Modifies a mode-local version of`semantic-dependency-system-include-path'.
137
138Changes made by this function are not persistent."
139 (interactive (list
140 (completing-read
141 "Include Directory to Remove: "
142 semantic-dependency-system-include-path))
143 )
144 (if (not mode) (setq mode major-mode))
145 (let ((dirtmp (file-name-as-directory dir))
146 (value
147 (mode-local-value mode 'semantic-dependency-system-include-path))
148 )
149 (setq value (delete dirtmp value))
150 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
151 value))
152 ))
153
154(defun semantic-reset-system-include (&optional mode)
155 "Reset the system include list to empty for MODE.
156Modifies a mode-local version of
157`semantic-dependency-system-include-path'."
158 (interactive)
159 (if (not mode) (setq mode major-mode))
160 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
161 nil))
162 )
163
164(defun semantic-customize-system-include-path (&optional mode)
165 "Customize the include path for this `major-mode'.
166To create a customizable include path for a major MODE, use the
167macro `defcustom-mode-local-semantic-dependency-system-include-path'."
168 (interactive)
169 (let ((ips (get 'semantic-dependency-system-include-path
170 (or mode major-mode))))
171 ;; Do we have one?
172 (when (not ips)
173 (error "There is no customizable includepath variable for %s"
174 (or mode major-mode)))
175 ;; Customize it.
176 (customize-variable ips)))
177
178;;; PATH SEARCH
179;;
180;; methods for finding files on a provided path.
181(if (fboundp 'locate-file)
182 (defsubst semantic--dependency-find-file-on-path (file path)
183 "Return an expanded file name for FILE on PATH."
184 (locate-file file path))
185
186 ;; Else, older version of Emacs.
187
188 (defsubst semantic--dependency-find-file-on-path (file path)
189 "Return an expanded file name for FILE on PATH."
190 (let ((p path)
191 (found nil))
192 (while (and p (not found))
193 (let ((f (expand-file-name file (car p))))
194 (if (file-exists-p f)
195 (setq found f)))
196 (setq p (cdr p)))
197 found))
198
199 )
200
201(defun semantic-dependency-find-file-on-path (file systemp &optional mode)
202 "Return an expanded file name for FILE on available paths.
203If SYSTEMP is true, then only search system paths.
204If optional argument MODE is non-nil, then derive paths from the
205provided mode, not from the current major mode."
206 (if (not mode) (setq mode major-mode))
207 (let ((sysp (mode-local-value
208 mode 'semantic-dependency-system-include-path))
209 (edesys (when (and (featurep 'ede) ede-minor-mode
210 ede-object)
211 (ede-system-include-path ede-object)))
212 (locp (mode-local-value
213 mode 'semantic-dependency-include-path))
214 (found nil))
215 (when (file-exists-p file)
216 (setq found file))
217 (when (and (not found) (not systemp))
218 (setq found (semantic--dependency-find-file-on-path file locp)))
219 (when (and (not found) edesys)
220 (setq found (semantic--dependency-find-file-on-path file edesys)))
221 (when (not found)
222 (setq found (semantic--dependency-find-file-on-path file sysp)))
223 (if found (expand-file-name found))))
224
225
226(provide 'semantic/dep)
227
228;;; semantic-dep.el ends here