Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / ede / files.el
blobac245cf61bd6c529c3c0cd75aa92138b46f34fa4
1 ;;; ede/files.el --- Associate projects with files and directories.
3 ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Directory and File scanning and matching functions.
26 ;; Basic Model:
28 ;; A directory belongs to a project if a ede-project-autoload structure
29 ;; matches your directory.
31 ;; A toplevel project is one where there is no active project above
32 ;; it. Finding the toplevel project involves going up a directory
33 ;; till no ede-project-autoload structure matches.
36 (require 'ede)
38 (declare-function ede-locate-file-in-hash "ede/locate")
39 (declare-function ede-locate-add-file-to-hash "ede/locate")
40 (declare-function ede-locate-file-in-project "ede/locate")
41 (declare-function ede-locate-flush-hash "ede/locate")
43 (defvar ede--disable-inode nil
44 "Set to 't' to simulate systems w/out inode support.")
46 ;;; Code:
47 ;;;###autoload
48 (defun ede-find-file (file)
49 "Find FILE in project. FILE can be specified without a directory.
50 There is no completion at the prompt. FILE is searched for within
51 the current EDE project."
52 (interactive "sFile: ")
53 (let* ((proj (ede-current-project))
54 (fname (ede-expand-filename proj file))
56 (unless fname
57 (error "Could not find %s in %s"
58 file
59 (ede-project-root-directory proj)))
60 (find-file fname)))
62 (defun ede-flush-project-hash ()
63 "Flush the file locate hash for the current project."
64 (interactive)
65 (require 'ede/locate)
66 (let* ((loc (ede-get-locator-object (ede-current-project))))
67 (when loc
68 (ede-locate-flush-hash loc))))
70 ;;; Placeholders for ROOT directory scanning on base objects
72 (defmethod ede-project-root ((this ede-project-placeholder))
73 "If a project knows its root, return it here.
74 Allows for one-project-object-for-a-tree type systems."
75 (oref this rootproject))
77 (defmethod ede-project-root-directory ((this ede-project-placeholder)
78 &optional file)
79 "If a project knows its root, return it here.
80 Allows for one-project-object-for-a-tree type systems.
81 Optional FILE is the file to test. It is ignored in preference
82 of the anchor file for the project."
83 (file-name-directory (expand-file-name (oref this file))))
86 (defmethod ede--project-inode ((proj ede-project-placeholder))
87 "Get the inode of the directory project PROJ is in."
88 (if (slot-boundp proj 'dirinode)
89 (oref proj dirinode)
90 (oset proj dirinode (ede--inode-for-dir (oref proj :directory)))))
92 (defmethod ede-find-subproject-for-directory ((proj ede-project-placeholder)
93 dir)
94 "Find a subproject of PROJ that corresponds to DIR."
95 (if ede--disable-inode
96 (let ((ans nil))
97 ;; Try to find the right project w/out inodes.
98 (ede-map-subprojects
99 proj
100 (lambda (SP)
101 (when (not ans)
102 (if (string= (file-truename dir) (oref SP :directory))
103 (setq ans SP)
104 (ede-find-subproject-for-directory SP dir)))))
105 ans)
106 ;; We can use inodes, so let's try it.
107 (let ((ans nil)
108 (inode (ede--inode-for-dir dir)))
109 (ede-map-subprojects
110 proj
111 (lambda (SP)
112 (when (not ans)
113 (if (equal (ede--project-inode SP) inode)
114 (setq ans SP)
115 (setq ans (ede-find-subproject-for-directory SP dir))))))
116 ans)))
118 ;;; DIRECTORY IN OPEN PROJECT
120 ;; These routines match some directory name to one of the many pre-existing
121 ;; open projects. This should avoid hitting the disk, or asking lots of questions
122 ;; if used throughout the other routines.
123 (defvar ede-inode-directory-hash (make-hash-table
124 ;; Note on test. Can we compare inodes or something?
125 :test 'equal)
126 "A hash of directory names and inodes.")
128 (defun ede--put-inode-dir-hash (dir inode)
129 "Add to the EDE project hash DIR associated with INODE."
130 (when (fboundp 'puthash)
131 (puthash dir inode ede-inode-directory-hash)
132 inode))
134 (defun ede--get-inode-dir-hash (dir)
135 "Get the EDE project hash DIR associated with INODE."
136 (when (fboundp 'gethash)
137 (gethash dir ede-inode-directory-hash)
140 (defun ede--inode-for-dir (dir)
141 "Return the inode for the directory DIR."
142 (let ((hashnode (ede--get-inode-dir-hash (expand-file-name dir))))
143 (or hashnode
144 (if ede--disable-inode
145 (ede--put-inode-dir-hash dir 0)
146 (let ((fattr (file-attributes dir)))
147 (ede--put-inode-dir-hash dir (nth 10 fattr))
148 )))))
150 (defun ede-directory-get-open-project (dir &optional rootreturn)
151 "Return an already open project that is managing DIR.
152 Optional ROOTRETURN specifies a symbol to set to the root project.
153 If DIR is the root project, then it is the same."
154 (let* ((inode (ede--inode-for-dir dir))
155 (ft (file-name-as-directory (expand-file-name dir)))
156 (proj (ede--inode-get-toplevel-open-project inode))
157 (ans nil))
158 ;; Try file based search.
159 (when (not proj)
160 (setq proj (ede-directory-get-toplevel-open-project ft)))
161 ;; Default answer is this project
162 (setq ans proj)
163 ;; Save.
164 (when rootreturn (set rootreturn proj))
165 ;; Find subprojects.
166 (when (and proj (or ede--disable-inode
167 (not (equal inode (ede--project-inode proj)))))
168 (setq ans (ede-find-subproject-for-directory proj ft)))
169 ans))
171 (defun ede--inode-get-toplevel-open-project (inode)
172 "Return an already open toplevel project that is managing INODE.
173 Does not check subprojects."
174 (when (or (and (numberp inode) (/= inode 0))
175 (consp inode))
176 (let ((all ede-projects)
177 (found nil)
179 (while (and all (not found))
180 (when (equal inode (ede--project-inode (car all)))
181 (setq found (car all)))
182 (setq all (cdr all)))
183 found)))
185 (defun ede-directory-get-toplevel-open-project (dir)
186 "Return an already open toplevel project that is managing DIR."
187 (let ((ft (file-name-as-directory (expand-file-name dir)))
188 (all ede-projects)
189 (ans nil))
190 (while (and all (not ans))
191 ;; Do the check.
192 (let ((pd (oref (car all) :directory))
194 (cond
195 ;; Exact text match.
196 ((string= pd ft)
197 (setq ans (car all)))
198 ;; Some sub-directory
199 ((string-match (concat "^" (regexp-quote pd)) ft)
200 (setq ans (car all)))
201 ;; Exact inode match. Useful with symlinks or complex automounters.
202 ((let ((pin (ede--project-inode (car all)))
203 (inode (ede--inode-for-dir dir)))
204 (and (not (eql pin 0)) (equal pin inode)))
205 (setq ans (car all)))
206 ;; Subdir via truename - slower by far, but faster than a traditional lookup.
207 ((let ((ftn (file-truename ft))
208 (ptd (file-truename (oref (car all) :directory))))
209 (string-match (concat "^" (regexp-quote ptd)) ftn))
210 (setq ans (car all)))
212 (setq all (cdr all)))
213 ans))
215 ;;; DIRECTORY-PROJECT-P
217 ;; For a fresh buffer, or for a path w/ no open buffer, use this
218 ;; routine to determine if there is a known project type here.
219 (defvar ede-project-directory-hash (make-hash-table
220 ;; Note on test. Can we compare inodes or something?
221 :test 'equal)
222 "A hash of directory names and associated EDE objects.")
224 (defun ede-flush-directory-hash ()
225 "Flush the project directory hash.
226 Do this only when developing new projects that are incorrectly putting
227 'nomatch tokens into the hash."
228 (interactive)
229 (setq ede-project-directory-hash (make-hash-table :test 'equal))
230 ;; Also slush the current project's locator hash.
231 (let ((loc (ede-get-locator-object ede-object)))
232 (when loc
233 (ede-locate-flush-hash loc)))
236 (defun ede-project-directory-remove-hash (dir)
237 "Reset the directory hash for DIR.
238 Do this whenever a new project is created, as opposed to loaded."
239 ;; TODO - Use maphash, and delete by regexp, not by dir searching!
241 (when (fboundp 'remhash)
242 (remhash (file-name-as-directory dir) ede-project-directory-hash)
243 ;; Look for all subdirs of D, and remove them.
244 (let ((match (concat "^" (regexp-quote dir))))
245 (maphash (lambda (K O)
246 (when (string-match match K)
247 (remhash K ede-project-directory-hash)))
248 ede-project-directory-hash))
251 (defun ede-directory-project-from-hash (dir)
252 "If there is an already loaded project for DIR, return it from the hash."
253 (when (fboundp 'gethash)
254 (gethash dir ede-project-directory-hash nil)))
256 (defun ede-directory-project-add-description-to-hash (dir desc)
257 "Add to the EDE project hash DIR associated with DESC."
258 (when (fboundp 'puthash)
259 (puthash dir desc ede-project-directory-hash)
260 desc))
262 (defun ede-directory-project-p (dir &optional force)
263 "Return a project description object if DIR has a project.
264 Optional argument FORCE means to ignore a hash-hit of 'nomatch.
265 This depends on an up to date `ede-project-class-files' variable.
266 Any directory that contains the file .ede-ignore will always
267 return nil."
268 (when (not (file-exists-p (expand-file-name ".ede-ignore" dir)))
269 (let* ((dirtest (expand-file-name dir))
270 (match (ede-directory-project-from-hash dirtest)))
271 (cond
272 ((and (eq match 'nomatch) (not force))
273 nil)
274 ((and match (not (eq match 'nomatch)))
275 match)
277 (let ((types ede-project-class-files)
278 (ret nil))
279 ;; Loop over all types, loading in the first type that we find.
280 (while (and types (not ret))
281 (if (ede-dir-to-projectfile (car types) dirtest)
282 (progn
283 ;; We found one! Require it now since we will need it.
284 (require (oref (car types) file))
285 (setq ret (car types))))
286 (setq types (cdr types)))
287 (ede-directory-project-add-description-to-hash dirtest (or ret 'nomatch))
288 ret))))))
290 ;;; TOPLEVEL
292 ;; These utilities will identify the "toplevel" of a project.
294 (defun ede-toplevel-project-or-nil (dir)
295 "Starting with DIR, find the toplevel project directory, or return nil.
296 nil is returned if the current directory is not a part of a project."
297 (let* ((ans (ede-directory-get-toplevel-open-project dir)))
298 (if ans
299 (oref ans :directory)
300 (if (ede-directory-project-p dir)
301 (ede-toplevel-project dir)
302 nil))))
304 (defun ede-toplevel-project (dir)
305 "Starting with DIR, find the toplevel project directory."
306 (if (and (string= dir default-directory)
307 ede-object-root-project)
308 ;; Try the local buffer cache first.
309 (oref ede-object-root-project :directory)
310 ;; Otherwise do it the hard way.
311 (let* ((thisdir (ede-directory-project-p dir))
312 (ans (ede-directory-get-toplevel-open-project dir)))
313 (if (and ans ;; We have an answer
314 (or (not thisdir) ;; this dir isn't setup
315 (and (object-of-class-p ;; Same as class for this dir?
316 ans (oref thisdir :class-sym)))
318 (oref ans :directory)
319 (let* ((toppath (expand-file-name dir))
320 (newpath toppath)
321 (proj (ede-directory-project-p dir))
322 (ans nil))
323 (if proj
324 ;; If we already have a project, ask it what the root is.
325 (setq ans (ede-project-root-directory proj)))
327 ;; If PROJ didn't know, or there is no PROJ, then
329 ;; Loop up to the topmost project, and then load that single
330 ;; project, and its sub projects. When we are done, identify the
331 ;; sub-project object belonging to file.
332 (while (and (not ans) newpath proj)
333 (setq toppath newpath
334 newpath (ede-up-directory toppath))
335 (when newpath
336 (setq proj (ede-directory-project-p newpath)))
338 (when proj
339 ;; We can home someone in the middle knows too.
340 (setq ans (ede-project-root-directory proj)))
342 (or ans toppath))))))
344 ;;; DIRECTORY CONVERSION STUFF
346 (defmethod ede-convert-path ((this ede-project) path)
347 "Convert path in a standard way for a given project.
348 Default to making it project relative.
349 Argument THIS is the project to convert PATH to."
350 (let ((pp (ede-project-root-directory this))
351 (fp (expand-file-name path)))
352 (if (string-match (regexp-quote pp) fp)
353 (substring fp (match-end 0))
354 (let ((pptf (file-truename pp))
355 (fptf (file-truename fp)))
356 (if (string-match (regexp-quote pptf) fptf)
357 (substring fptf (match-end 0))
358 (error "Cannot convert relativize path %s" fp))))))
360 (defmethod ede-convert-path ((this ede-target) path &optional project)
361 "Convert path in a standard way for a given project.
362 Default to making it project relative.
363 Argument THIS is the project to convert PATH to.
364 Optional PROJECT is the project that THIS belongs to. Associating
365 a target to a project is expensive, so using this can speed things up."
366 (let ((proj (or project (ede-target-parent this))))
367 (if proj
368 (let ((p (ede-convert-path proj path))
369 (lp (or (oref this path) "")))
370 ;; Our target THIS may have path information.
371 ;; strip this out of the conversion.
372 (if (string-match (concat "^" (regexp-quote lp)) p)
373 (substring p (length lp))
375 (error "Parentless target %s" this))))
377 ;;; FILENAME EXPANSION
379 (defun ede-get-locator-object (proj)
380 "Get the locator object for project PROJ.
381 Get it from the toplevel project. If it doesn't have one, make one."
382 ;; Make sure we have a location object available for
383 ;; caching values, and for locating things more robustly.
384 (let ((top (ede-toplevel proj)))
385 (when top
386 (when (not (slot-boundp top 'locate-obj))
387 (ede-enable-locate-on-project top))
388 (oref top locate-obj)
391 (defmethod ede-expand-filename ((this ede-project) filename &optional force)
392 "Return a fully qualified file name based on project THIS.
393 FILENAME should be just a filename which occurs in a directory controlled
394 by this project.
395 Optional argument FORCE forces the default filename to be provided even if it
396 doesn't exist.
397 If FORCE equals 'newfile, then the cache is ignored and a new file in THIS
398 is returned."
399 (require 'ede/locate)
400 (let* ((loc (ede-get-locator-object this))
401 (ha (ede-locate-file-in-hash loc filename))
402 (ans nil)
404 ;; NOTE: This function uses a locator object, which keeps a hash
405 ;; table of files it has found in the past. The hash table is
406 ;; used to make commonly found file very fast to location. Some
407 ;; complex routines, such as smart completion asks this question
408 ;; many times, so doing this speeds things up, especially on NFS
409 ;; or other remote file systems.
411 ;; As such, special care is needed to use the hash, and also obey
412 ;; the FORCE option, which is needed when trying to identify some
413 ;; new file that needs to be created, such as a Makefile.
414 (cond
415 ;; We have a hash-table match, AND that match wasn't the 'nomatch
416 ;; flag, we can return it.
417 ((and ha (not (eq ha 'nomatch)))
418 (setq ans ha))
419 ;; If we had a match, and it WAS no match, then we need to look
420 ;; at the force-option to see what to do. Since ans is already
421 ;; nil, then we do nothing.
422 ((and (eq ha 'nomatch) (not (eq force 'newfile)))
423 nil)
424 ;; We had no hash table match, so we have to look up this file
425 ;; using the usual EDE file expansion rules.
427 (let ((calc (ede-expand-filename-impl this filename)))
428 (if calc
429 (progn
430 (ede-locate-add-file-to-hash loc filename calc)
431 (setq ans calc))
432 ;; If we failed to calculate something, we
433 ;; should add it to the hash, but ONLY if we are not
434 ;; going to FORCE the file into existence.
435 (when (not force)
436 (ede-locate-add-file-to-hash loc filename 'nomatch))))
438 ;; Now that all options have been queried, if the FORCE option is
439 ;; true, but ANS is still nil, then we can make up a file name.
441 ;; Is it forced?
442 (when (and force (not ans))
443 (let ((dir (ede-project-root-directory this)))
444 (setq ans (expand-file-name filename dir))))
446 ans))
448 (defmethod ede-expand-filename-impl ((this ede-project) filename &optional force)
449 "Return a fully qualified file name based on project THIS.
450 FILENAME should be just a filename which occurs in a directory controlled
451 by this project.
452 Optional argument FORCE forces the default filename to be provided even if it
453 doesn't exist."
454 (let ((loc (ede-get-locator-object this))
455 (path (ede-project-root-directory this))
456 (proj (oref this subproj))
457 (found nil))
458 ;; find it Locally.
459 (setq found (or (ede-expand-filename-local this filename)
460 (ede-expand-filename-impl-via-subproj this filename)))
461 ;; Use an external locate tool.
462 (when (not found)
463 (require 'ede/locate)
464 (setq found (car (ede-locate-file-in-project loc filename))))
465 ;; Return it
466 found))
468 (defmethod ede-expand-filename-local ((this ede-project) filename)
469 "Expand filename locally to project THIS with filesystem tests."
470 (let ((path (ede-project-root-directory this)))
471 (cond ((file-exists-p (expand-file-name filename path))
472 (expand-file-name filename path))
473 ((file-exists-p (expand-file-name (concat "include/" filename) path))
474 (expand-file-name (concat "include/" filename) path)))))
476 (defmethod ede-expand-filename-impl-via-subproj ((this ede-project) filename)
477 "Return a fully qualified file name based on project THIS.
478 FILENAME should be just a filename which occurs in a directory controlled
479 by this project."
480 (let ((proj (list (ede-toplevel this)))
481 (found nil))
482 ;; find it Locally.
483 (while (and (not found) proj)
484 (let ((thisproj (car proj)))
485 (setq proj (append (cdr proj) (oref thisproj subproj)))
486 (setq found (when thisproj
487 (ede-expand-filename-local thisproj filename)))
489 ;; Return it
490 found))
492 (defmethod ede-expand-filename ((this ede-target) filename &optional force)
493 "Return a fully qualified file name based on target THIS.
494 FILENAME should be a filename which occurs in a directory in which THIS works.
495 Optional argument FORCE forces the default filename to be provided even if it
496 doesn't exist."
497 (ede-expand-filename (ede-target-parent this) filename force))
499 ;;; UTILITIES
502 (defun ede-up-directory (dir)
503 "Return a dir that is up one directory.
504 Argument DIR is the directory to trim upwards."
505 (let* ((fad (directory-file-name dir))
506 (fnd (file-name-directory fad)))
507 (if (string= dir fnd) ; This will catch the old string-match against
508 ; c:/ for DOS like systems.
510 fnd)))
512 (defun ede-find-project-root (prj-file-name &optional dir)
513 "Tries to find directory with given project file"
514 (let ((prj-dir (locate-dominating-file (or dir default-directory)
515 prj-file-name)))
516 (when prj-dir
517 (expand-file-name prj-dir))))
519 (defun ede-files-find-existing (dir prj-list)
520 "Find a project in the list of projects stored in given variable.
521 DIR is the directory to search from."
522 (let ((projs prj-list)
523 (ans nil))
524 (while (and projs (not ans))
525 (let ((root (ede-project-root-directory (car projs))))
526 (when (string-match (concat "^" (regexp-quote root)) dir)
527 (setq ans (car projs))))
528 (setq projs (cdr projs)))
529 ans))
532 (provide 'ede/files)
534 ;; Local variables:
535 ;; generated-autoload-file: "loaddefs.el"
536 ;; generated-autoload-load-name: "ede/files"
537 ;; End:
539 ;;; ede/files.el ends here