Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / cedet / semantic / dep.el
CommitLineData
978c25c6 1;;; semantic/dep.el --- Methods for tracking dependencies (include files)
f273dfc6 2
acaf905b 3;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
f273dfc6
CY
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
9bf6c65c 33;; manifest themselves in the form of system databases (from
f273dfc6
CY
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
9bf6c65c 50The above function, regardless of being overridden, caches the
f273dfc6
CY
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.
e969cc5d 118;;;###autoload
f273dfc6
CY
119(defun semantic-add-system-include (dir &optional mode)
120 "Add a system include DIR to path for MODE.
121Modifies a mode-local version of `semantic-dependency-system-include-path'.
122
123Changes made by this function are not persistent."
124 (interactive "DNew Include Directory: ")
125 (if (not mode) (setq mode major-mode))
126 (let ((dirtmp (file-name-as-directory dir))
127 (value
128 (mode-local-value mode 'semantic-dependency-system-include-path))
129 )
130 (add-to-list 'value dirtmp t)
131 (eval `(setq-mode-local ,mode
132 semantic-dependency-system-include-path value))
133 ))
134
e969cc5d 135;;;###autoload
f273dfc6
CY
136(defun semantic-remove-system-include (dir &optional mode)
137 "Add a system include DIR to path for MODE.
138Modifies a mode-local version of`semantic-dependency-system-include-path'.
139
140Changes made by this function are not persistent."
141 (interactive (list
142 (completing-read
143 "Include Directory to Remove: "
144 semantic-dependency-system-include-path))
145 )
146 (if (not mode) (setq mode major-mode))
147 (let ((dirtmp (file-name-as-directory dir))
148 (value
149 (mode-local-value mode 'semantic-dependency-system-include-path))
150 )
151 (setq value (delete dirtmp value))
152 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
153 value))
154 ))
155
e969cc5d 156;;;###autoload
f273dfc6
CY
157(defun semantic-reset-system-include (&optional mode)
158 "Reset the system include list to empty for MODE.
159Modifies a mode-local version of
160`semantic-dependency-system-include-path'."
161 (interactive)
162 (if (not mode) (setq mode major-mode))
163 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
164 nil))
165 )
166
e969cc5d 167;;;###autoload
f273dfc6
CY
168(defun semantic-customize-system-include-path (&optional mode)
169 "Customize the include path for this `major-mode'.
170To create a customizable include path for a major MODE, use the
171macro `defcustom-mode-local-semantic-dependency-system-include-path'."
172 (interactive)
173 (let ((ips (get 'semantic-dependency-system-include-path
174 (or mode major-mode))))
175 ;; Do we have one?
176 (when (not ips)
177 (error "There is no customizable includepath variable for %s"
178 (or mode major-mode)))
179 ;; Customize it.
180 (customize-variable ips)))
181
182;;; PATH SEARCH
183;;
184;; methods for finding files on a provided path.
978c25c6
CY
185(defmacro semantic--dependency-find-file-on-path (file path)
186 (if (fboundp 'locate-file)
187 `(locate-file ,file ,path)
188 `(let ((p ,path)
189 (found nil))
190 (while (and p (not found))
191 (let ((f (expand-file-name ,file (car p))))
192 (if (file-exists-p f)
193 (setq found f)))
194 (setq p (cdr p)))
195 found)))
196
197(defvar ede-minor-mode)
198(defvar ede-object)
199(declare-function ede-system-include-path "ede")
f273dfc6
CY
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)
dd9af436
CY
211 (ede-system-include-path
212 (if (listp ede-object) (car ede-object) ede-object))))
f273dfc6
CY
213 (locp (mode-local-value
214 mode 'semantic-dependency-include-path))
215 (found nil))
216 (when (file-exists-p file)
217 (setq found file))
218 (when (and (not found) (not systemp))
219 (setq found (semantic--dependency-find-file-on-path file locp)))
220 (when (and (not found) edesys)
221 (setq found (semantic--dependency-find-file-on-path file edesys)))
222 (when (not found)
223 (setq found (semantic--dependency-find-file-on-path file sysp)))
224 (if found (expand-file-name found))))
225
226
227(provide 'semantic/dep)
228
e969cc5d
CY
229;; Local variables:
230;; generated-autoload-file: "loaddefs.el"
e969cc5d
CY
231;; generated-autoload-load-name: "semantic/dep"
232;; End:
233
978c25c6 234;;; semantic/dep.el ends here