1 ;;; ede/files.el --- Associate projects with files and directories.
3 ;; Copyright (C) 2008-2016 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/>.
24 ;; Directory and File scanning and matching functions.
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.
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.")
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
))
57 (error "Could not find %s in %s"
59 (ede-project-root-directory proj
)))
62 (defun ede-flush-project-hash ()
63 "Flush the file locate hash for the current project."
66 (let* ((loc (ede-get-locator-object (ede-current-project))))
68 (ede-locate-flush-hash loc
))))
70 ;;; Placeholders for ROOT directory scanning on base objects
72 (cl-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 (cl-defmethod ede-project-root-directory ((this ede-project-placeholder
)
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 (let ((root (or (ede-project-root this
) this
)))
84 (file-name-directory (expand-file-name (oref this file
)))))
88 ;; An inode represents a unique ID that transcends symlinks, hardlinks, etc.
89 ;; so when we cache an inode in a project, and hash directories to inodes, we
90 ;; can avoid costly filesystem queries and regex matches.
92 (defvar ede-inode-directory-hash
(make-hash-table
93 ;; Note on test. Can we compare inodes or something?
95 "A hash of directory names and inodes.")
97 (defun ede--put-inode-dir-hash (dir inode
)
98 "Add to the EDE project hash DIR associated with INODE."
99 (when (fboundp 'puthash
)
100 (puthash dir inode ede-inode-directory-hash
)
103 (defun ede--get-inode-dir-hash (dir)
104 "Get the EDE project hash DIR associated with INODE."
105 (when (fboundp 'gethash
)
106 (gethash dir ede-inode-directory-hash
)
109 (defun ede--inode-for-dir (dir)
110 "Return the inode for the directory DIR."
111 (let ((hashnode (ede--get-inode-dir-hash (expand-file-name dir
))))
113 (if ede--disable-inode
114 (ede--put-inode-dir-hash dir
0)
115 (let ((fattr (file-attributes dir
)))
116 (ede--put-inode-dir-hash dir
(nth 10 fattr
))
119 (cl-defmethod ede--project-inode ((proj ede-project-placeholder
))
120 "Get the inode of the directory project PROJ is in."
121 (if (slot-boundp proj
'dirinode
)
123 (oset proj dirinode
(ede--inode-for-dir (oref proj
:directory
)))))
125 (defun ede--inode-get-toplevel-open-project (inode)
126 "Return an already open toplevel project that is managing INODE.
127 Does not check subprojects."
128 (when (or (and (numberp inode
) (/= inode
0))
130 (let ((all ede-projects
)
133 (while (and all
(not found
))
134 (when (equal inode
(ede--project-inode (car all
)))
135 (setq found
(car all
)))
136 (setq all
(cdr all
)))
139 ;;; DIRECTORY IN OPEN PROJECT
141 ;; These routines match some directory name to one of the many pre-existing
142 ;; open projects. This should avoid hitting the disk, or asking lots of questions
143 ;; if used throughout the other routines.
145 (defun ede-directory-get-open-project (dir &optional rootreturn
)
146 "Return an already open project that is managing DIR.
147 Optional ROOTRETURN specifies a symbol to set to the root project.
148 If DIR is the root project, then it is the same."
149 (let* ((inode (ede--inode-for-dir dir
))
150 (ft (file-name-as-directory (expand-file-name dir
)))
151 (proj (ede--inode-get-toplevel-open-project inode
))
153 ;; Try file based search.
154 (when (or ede--disable-inode
(not proj
))
155 (setq proj
(ede-directory-get-toplevel-open-project ft
)))
156 ;; Default answer is this project
159 (when rootreturn
(set rootreturn proj
))
161 (when (and proj
(if ede--disable-inode
162 (not (string= ft
(expand-file-name (oref proj
:directory
))))
163 (not (equal inode
(ede--project-inode proj
)))))
164 (setq ans
(ede-find-subproject-for-directory proj ft
)))
167 ;; Force all users to switch to `ede-directory-get-open-project'
168 ;; for performance reasons.
169 (defun ede-directory-get-toplevel-open-project (dir &optional exact
)
170 "Return an already open toplevel project that is managing DIR.
171 If optional EXACT is non-nil, only return exact matches for DIR."
172 (let ((ft (file-name-as-directory (expand-file-name dir
)))
176 (while (and all
(not ans
))
178 (let ((pd (expand-file-name (oref (car all
) :directory
)))
183 (setq ans
(car all
)))
184 ;; Some sub-directory
185 ((and (not exact
) (string-match (concat "^" (regexp-quote pd
)) ft
))
187 (setq shortans
(car all
))
188 ;; We already have a short answer, so see if pd (the match we found)
189 ;; is longer. If it is longer, then it is more precise.
190 (when (< (length (oref shortans
:directory
))
192 (setq shortans
(car all
))))
194 ;; Exact inode match. Useful with symlinks or complex automounters.
195 ((and (not ede--disable-inode
)
196 (let ((pin (ede--project-inode (car all
)))
197 (inode (ede--inode-for-dir dir
)))
198 (and (not (eql pin
0)) (equal pin inode
))))
199 (setq ans
(car all
)))
200 ;; Subdir via truename - slower by far, but faster than a traditional lookup.
201 ;; Note that we must resort to truename in order to resolve issues such as
202 ;; cross-symlink projects.
204 (let ((ftn (file-truename ft
))
205 (ptd (file-truename pd
)))
206 (string-match (concat "^" (regexp-quote ptd
)) ftn
)))
208 (setq shortans
(car all
))
209 ;; We already have a short answer, so see if pd (the match we found)
210 ;; is longer. If it is longer, then it is more precise.
211 (when (< (length (expand-file-name (oref shortans
:directory
)))
213 (setq shortans
(car all
))))
215 (setq all
(cdr all
)))
216 ;; If we have an exact answer, use that, otherwise use
217 ;; the short answer we found -> ie - we are in a subproject.
220 (cl-defmethod ede-find-subproject-for-directory ((proj ede-project-placeholder
)
222 "Find a subproject of PROJ that corresponds to DIR."
223 (if ede--disable-inode
225 (fulldir (file-truename dir
)))
226 ;; Try to find the right project w/out inodes.
231 (if (string= fulldir
(file-truename (oref SP
:directory
)))
233 (ede-find-subproject-for-directory SP dir
)))))
235 ;; We can use inodes, so let's try it.
237 (inode (ede--inode-for-dir dir
)))
242 (if (equal (ede--project-inode SP
) inode
)
244 (setq ans
(ede-find-subproject-for-directory SP dir
))))))
249 ;; The directory hash matches expanded directory names to already detected
250 ;; projects. By hashing projects to directories, we can detect projects in
251 ;; places we have been before much more quickly.
253 (defvar ede-project-directory-hash
(make-hash-table
254 ;; Note on test. Can we compare inodes or something?
256 "A hash of directory names and associated EDE objects.")
258 (defun ede-flush-directory-hash ()
259 "Flush the project directory hash.
260 Do this only when developing new projects that are incorrectly putting
261 'nomatch tokens into the hash."
263 (setq ede-project-directory-hash
(make-hash-table :test
'equal
))
264 ;; Also slush the current project's locator hash.
265 (let ((loc (ede-get-locator-object ede-object
)))
267 (ede-locate-flush-hash loc
)))
270 (defun ede-project-directory-remove-hash (dir)
271 "Reset the directory hash for DIR.
272 Do this whenever a new project is created, as opposed to loaded."
273 ;; TODO - Use maphash, and delete by regexp, not by dir searching!
274 (setq dir
(expand-file-name dir
))
275 (when (fboundp 'remhash
)
276 (remhash (file-name-as-directory dir
) ede-project-directory-hash
)
277 ;; Look for all subdirs of D, and remove them.
278 (let ((match (concat "^" (regexp-quote dir
))))
279 (maphash (lambda (K O
)
280 (when (string-match match K
)
281 (remhash K ede-project-directory-hash
)))
282 ede-project-directory-hash
))
285 (defun ede--directory-project-from-hash (dir)
286 "If there is an already loaded project for DIR, return it from the hash."
287 (when (fboundp 'gethash
)
288 (setq dir
(expand-file-name dir
))
289 (gethash dir ede-project-directory-hash nil
)))
291 (defun ede--directory-project-add-description-to-hash (dir desc
)
292 "Add to the EDE project hash DIR associated with DESC."
293 (when (fboundp 'puthash
)
294 (setq dir
(expand-file-name dir
))
295 (puthash dir desc ede-project-directory-hash
)
298 ;;; DIRECTORY-PROJECT-P, -CONS
300 ;; These routines are useful for detecting if a project exists
301 ;; in a provided directory.
303 ;; Note that -P provides less information than -CONS, so use -CONS
304 ;; instead so that -P can be obsoleted.
305 (defun ede-directory-project-p (dir &optional force
)
306 "Return a project description object if DIR is in a project.
307 Optional argument FORCE means to ignore a hash-hit of `nomatch'.
308 This depends on an up to date `ede-project-class-files' variable.
309 Any directory that contains the file .ede-ignore will always
312 Consider using `ede-directory-project-cons' instead if the next
313 question you want to ask is where the root of found project is."
314 ;; @TODO - We used to have a full impl here, but moved it all
315 ;; to ede-directory-project-cons, and now hash contains only
316 ;; the results of detection which includes the root dir.
317 ;; Perhaps we can eventually remove this fcn?
318 (let ((detect (ede-directory-project-cons dir force
)))
321 (defun ede-directory-project-cons (dir &optional force
)
322 "Return a project CONS (ROOTDIR . AUTOLOAD) for DIR.
323 If there is no project in DIR, return nil.
324 Optional FORCE means to ignore the hash of known directories."
325 (when (not (file-exists-p (expand-file-name ".ede-ignore" dir
)))
326 (let* ((dirtest (expand-file-name dir
))
327 (match (ede--directory-project-from-hash dirtest
)))
329 ((and (eq match
'nomatch
) (not force
))
331 ((and match
(not (eq match
'nomatch
)))
334 ;; First time here? Use the detection code to identify if we have
336 (let* ((detect (ede-detect-directory-for-project dirtest
))
337 (autoloader (cdr detect
))) ;; autoloader
338 (when autoloader
(require (oref autoloader file
)))
339 (ede--directory-project-add-description-to-hash dirtest
(or detect
'nomatch
))
346 ;; These utilities will identify the "toplevel" of a project.
348 ;; NOTE: These two -toplevel- functions return a directory even though
349 ;; the function name implies a project.
351 (defun ede-toplevel-project (dir)
352 "Starting with DIR, find the toplevel project directory.
353 If DIR is not part of a project, return nil."
357 ;; Check if it is cached in the current buffer.
358 ((and (string= dir default-directory
)
359 ede-object-root-project
)
360 ;; Try the local buffer cache first.
361 (oref ede-object-root-project
:directory
))
363 ;; See if there is an existing project in DIR.
364 ((setq ans
(ede-directory-get-toplevel-open-project dir
))
365 (oref ans
:directory
))
367 ;; Detect using our file system detector.
368 ((setq ans
(ede-detect-directory-for-project dir
))
373 (defalias 'ede-toplevel-project-or-nil
'ede-toplevel-project
)
375 ;;; DIRECTORY CONVERSION STUFF
377 (cl-defmethod ede-convert-path ((this ede-project
) path
)
378 "Convert path in a standard way for a given project.
379 Default to making it project relative.
380 Argument THIS is the project to convert PATH to."
381 (let ((pp (ede-project-root-directory this
))
382 (fp (expand-file-name path
)))
383 (if (string-match (regexp-quote pp
) fp
)
384 (substring fp
(match-end 0))
385 (let ((pptf (file-truename pp
))
386 (fptf (file-truename fp
)))
387 (if (string-match (regexp-quote pptf
) fptf
)
388 (substring fptf
(match-end 0))
389 (error "Cannot convert relativize path %s" fp
))))))
391 (cl-defmethod ede-convert-path ((this ede-target
) path
&optional project
)
392 "Convert path in a standard way for a given project.
393 Default to making it project relative.
394 Argument THIS is the project to convert PATH to.
395 Optional PROJECT is the project that THIS belongs to. Associating
396 a target to a project is expensive, so using this can speed things up."
397 (let ((proj (or project
(ede-target-parent this
))))
399 (let ((p (ede-convert-path proj path
))
400 (lp (or (oref this path
) "")))
401 ;; Our target THIS may have path information.
402 ;; strip this out of the conversion.
403 (if (string-match (concat "^" (regexp-quote lp
)) p
)
404 (substring p
(length lp
))
406 (error "Parentless target %s" this
))))
408 ;;; FILENAME EXPANSION
410 (defun ede-get-locator-object (proj)
411 "Get the locator object for project PROJ.
412 Get it from the toplevel project. If it doesn't have one, make one."
413 ;; Make sure we have a location object available for
414 ;; caching values, and for locating things more robustly.
415 (let ((top (ede-toplevel proj
)))
417 (when (not (slot-boundp top
'locate-obj
))
418 (ede-enable-locate-on-project top
))
419 (oref top locate-obj
)
422 (cl-defmethod ede-expand-filename ((this ede-project
) filename
&optional force
)
423 "Return a fully qualified file name based on project THIS.
424 FILENAME should be just a filename which occurs in a directory controlled
426 Optional argument FORCE forces the default filename to be provided even if it
428 If FORCE equals 'newfile, then the cache is ignored and a new file in THIS
430 (require 'ede
/locate
)
431 (let* ((loc (ede-get-locator-object this
))
432 (ha (ede-locate-file-in-hash loc filename
))
435 ;; NOTE: This function uses a locator object, which keeps a hash
436 ;; table of files it has found in the past. The hash table is
437 ;; used to make commonly found file very fast to location. Some
438 ;; complex routines, such as smart completion asks this question
439 ;; many times, so doing this speeds things up, especially on NFS
440 ;; or other remote file systems.
442 ;; As such, special care is needed to use the hash, and also obey
443 ;; the FORCE option, which is needed when trying to identify some
444 ;; new file that needs to be created, such as a Makefile.
446 ;; We have a hash-table match, AND that match wasn't the 'nomatch
447 ;; flag, we can return it.
448 ((and ha
(not (eq ha
'nomatch
)))
450 ;; If we had a match, and it WAS no match, then we need to look
451 ;; at the force-option to see what to do. Since ans is already
452 ;; nil, then we do nothing.
453 ((and (eq ha
'nomatch
) (not (eq force
'newfile
)))
455 ;; We had no hash table match, so we have to look up this file
456 ;; using the usual EDE file expansion rules.
458 (let ((calc (ede-expand-filename-impl this filename
)))
461 (ede-locate-add-file-to-hash loc filename calc
)
463 ;; If we failed to calculate something, we
464 ;; should add it to the hash, but ONLY if we are not
465 ;; going to FORCE the file into existence.
467 (ede-locate-add-file-to-hash loc filename
'nomatch
))))
469 ;; Now that all options have been queried, if the FORCE option is
470 ;; true, but ANS is still nil, then we can make up a file name.
473 (when (and force
(not ans
))
474 (let ((dir (ede-project-root-directory this
)))
475 (setq ans
(expand-file-name filename dir
))))
479 (cl-defmethod ede-expand-filename-impl ((this ede-project
) filename
&optional force
)
480 "Return a fully qualified file name based on project THIS.
481 FILENAME should be just a filename which occurs in a directory controlled
483 Optional argument FORCE forces the default filename to be provided even if it
485 (let ((loc (ede-get-locator-object this
))
486 (path (ede-project-root-directory this
))
487 (proj (oref this subproj
))
490 (setq found
(or (ede-expand-filename-local this filename
)
491 (ede-expand-filename-impl-via-subproj this filename
)))
492 ;; Use an external locate tool.
494 (require 'ede
/locate
)
495 (setq found
(car (ede-locate-file-in-project loc filename
))))
499 (cl-defmethod ede-expand-filename-local ((this ede-project
) filename
)
500 "Expand filename locally to project THIS with filesystem tests."
501 (let ((path (ede-project-root-directory this
)))
502 (cond ((file-exists-p (expand-file-name filename path
))
503 (expand-file-name filename path
))
504 ((file-exists-p (expand-file-name (concat "include/" filename
) path
))
505 (expand-file-name (concat "include/" filename
) path
)))))
507 (cl-defmethod ede-expand-filename-impl-via-subproj ((this ede-project
) filename
)
508 "Return a fully qualified file name based on project THIS.
509 FILENAME should be just a filename which occurs in a directory controlled
511 (let ((proj (list (ede-toplevel this
)))
514 (while (and (not found
) proj
)
515 (let ((thisproj (car proj
)))
516 (setq proj
(append (cdr proj
) (oref thisproj subproj
)))
517 (setq found
(when thisproj
518 (ede-expand-filename-local thisproj filename
)))
523 (cl-defmethod ede-expand-filename ((this ede-target
) filename
&optional force
)
524 "Return a fully qualified file name based on target THIS.
525 FILENAME should be a filename which occurs in a directory in which THIS works.
526 Optional argument FORCE forces the default filename to be provided even if it
528 (ede-expand-filename (ede-target-parent this
) filename force
))
533 (defun ede-up-directory (dir)
534 "Return a dir that is up one directory.
535 Argument DIR is the directory to trim upwards."
536 (let* ((fad (directory-file-name dir
))
537 (fnd (file-name-directory fad
)))
538 (if (string= dir fnd
) ; This will catch the old string-match against
539 ; c:/ for DOS like systems.
547 ;; generated-autoload-file: "loaddefs.el"
548 ;; generated-autoload-load-name: "ede/files"
551 ;;; ede/files.el ends here