1 ;;; semantic/dep.el --- Methods for tracking dependencies (include files)
3 ;; Copyright (C) 2006-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
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
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 databases (from
36 ;; Provide ways to track these different files here.
38 (require 'semantic
/tag
)
42 (defvar semantic-dependency-include-path nil
43 "Defines the include path used when searching for files.
44 This should be a list of directories to search which is specific
45 to the file being included.
47 If `semantic-dependency-tag-file' is overridden for a given
48 language, this path is most likely ignored.
50 The above function, regardless of being overridden, caches the
51 located dependency file location in the tag property
52 `dependency-file'. If you override this function, you do not
53 need to implement your own cache. Each time the buffer is fully
54 reparsed, the cache will be reset.
56 TODO: use ffap.el to locate such items?
58 NOTE: Obsolete this, or use as special user")
59 (make-variable-buffer-local `semantic-dependency-include-path
)
61 (defvar semantic-dependency-system-include-path nil
62 "Defines the system include path.
63 This should be set with either `defvar-mode-local', or with
64 `semantic-add-system-include'.
67 `defcustom-mode-local-semantic-dependency-system-include-path'
68 to create a mode-specific variable to control this.
70 When searching for a file associated with a name found in a tag of
71 class include, this path will be inspected for includes of type
72 `system'. Some include tags are agnostic to this setting and will
73 check both the project and system directories.")
74 (make-variable-buffer-local `semantic-dependency-system-include-path
)
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.
79 MODE is the `major-mode' this name/value pairs is for.
80 NAME is the name of the customizable value users will use.
81 VALUE is the path (a list of strings) to add.
82 DOCSTRING is a documentation string applied to the variable NAME
85 Creates a customizable variable users can customize that will
86 keep semantic data structures up to date."
88 ;; Create a variable users can customize.
89 (defcustom ,name
,value
91 :group
(quote ,(intern (car (split-string (symbol-name mode
) "-"))))
93 :type
'(repeat (directory :tag
"Directory"))
94 :set
(lambda (sym val
)
96 (setq-mode-local ,mode
97 semantic-dependency-system-include-path
100 'semantic-decoration-unparsed-include-do-reset
)
101 (mode-local-map-mode-buffers
102 'semantic-decoration-unparsed-include-do-reset
105 ;; Set the variable to the default value.
106 (defvar-mode-local ,mode semantic-dependency-system-include-path
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
))
117 ;; Some fcns to manage paths for a give mode.
119 (defun semantic-add-system-include (dir &optional mode
)
120 "Add a system include DIR to path for MODE.
121 Modifies a mode-local version of `semantic-dependency-system-include-path'.
123 Changes 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
))
128 (mode-local-value mode
'semantic-dependency-system-include-path
))
130 (add-to-list 'value dirtmp t
)
131 (eval `(setq-mode-local ,mode
132 semantic-dependency-system-include-path value
))
136 (defun semantic-remove-system-include (dir &optional mode
)
137 "Add a system include DIR to path for MODE.
138 Modifies a mode-local version of`semantic-dependency-system-include-path'.
140 Changes made by this function are not persistent."
143 "Include Directory to Remove: "
144 semantic-dependency-system-include-path
))
146 (if (not mode
) (setq mode major-mode
))
147 (let ((dirtmp (file-name-as-directory dir
))
149 (mode-local-value mode
'semantic-dependency-system-include-path
))
151 (setq value
(delete dirtmp value
))
152 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
157 (defun semantic-reset-system-include (&optional mode
)
158 "Reset the system include list to empty for MODE.
159 Modifies a mode-local version of
160 `semantic-dependency-system-include-path'."
162 (if (not mode
) (setq mode major-mode
))
163 (eval `(setq-mode-local ,mode semantic-dependency-system-include-path
168 (defun semantic-customize-system-include-path (&optional mode
)
169 "Customize the include path for this `major-mode'.
170 To create a customizable include path for a major MODE, use the
171 macro `defcustom-mode-local-semantic-dependency-system-include-path'."
173 (let ((ips (get 'semantic-dependency-system-include-path
174 (or mode major-mode
))))
177 (error "There is no customizable includepath variable for %s"
178 (or mode major-mode
)))
180 (customize-variable ips
)))
184 ;; methods for finding files on a provided path.
185 (defmacro semantic--dependency-find-file-on-path
(file path
)
186 (if (fboundp 'locate-file
)
187 `(locate-file ,file
,path
)
190 (while (and p
(not found
))
191 (let ((f (expand-file-name ,file
(car p
))))
192 (if (file-exists-p f
)
197 (defvar ede-minor-mode
)
199 (declare-function ede-system-include-path
"ede")
201 (defun semantic-dependency-find-file-on-path (file systemp
&optional mode
)
202 "Return an expanded file name for FILE on available paths.
203 If SYSTEMP is true, then only search system paths.
204 If optional argument MODE is non-nil, then derive paths from the
205 provided 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
211 (ede-system-include-path
212 (if (listp ede-object
) (car ede-object
) ede-object
))))
213 (locp (mode-local-value
214 mode
'semantic-dependency-include-path
))
216 (when (file-exists-p 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
)))
223 (setq found
(semantic--dependency-find-file-on-path file sysp
)))
224 (if found
(expand-file-name found
))))
227 (provide 'semantic
/dep
)
230 ;; generated-autoload-file: "loaddefs.el"
231 ;; generated-autoload-load-name: "semantic/dep"
234 ;;; semantic/dep.el ends here