Commit | Line | Data |
---|---|---|
acc33231 CY |
1 | ;;; ede/files.el --- Associate projects with files and directories. |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2008-2013 Free Software Foundation, Inc. |
acc33231 CY |
4 | |
5 | ;; Author: Eric M. Ludlam <eric@siege-engine.com> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
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. | |
13 | ||
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. | |
18 | ||
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/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ;; | |
24 | ;; Directory and File scanning and matching functions. | |
25 | ;; | |
26 | ;; Basic Model: | |
27 | ;; | |
28 | ;; A directory belongs to a project if a ede-project-autoload structure | |
29 | ;; matches your directory. | |
30 | ;; | |
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. | |
34 | ;; | |
35 | ||
36 | (require 'ede) | |
37 | ||
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") | |
cb85c0d8 | 41 | (declare-function ede-locate-flush-hash "ede/locate") |
acc33231 CY |
42 | |
43 | (defvar ede--disable-inode nil | |
44 | "Set to 't' to simulate systems w/out inode support.") | |
45 | ||
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: ") | |
e8cc7880 DE |
53 | (let* ((proj (ede-current-project)) |
54 | (fname (ede-expand-filename proj file)) | |
acc33231 CY |
55 | ) |
56 | (unless fname | |
57 | (error "Could not find %s in %s" | |
58 | file | |
e8cc7880 | 59 | (ede-project-root-directory proj))) |
acc33231 CY |
60 | (find-file fname))) |
61 | ||
cb85c0d8 EL |
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)))) | |
62a81506 CY |
67 | (when loc |
68 | (ede-locate-flush-hash loc)))) | |
cb85c0d8 | 69 | |
acc33231 CY |
70 | ;;; Placeholders for ROOT directory scanning on base objects |
71 | ;; | |
72 | (defmethod ede-project-root ((this ede-project-placeholder)) | |
cb85c0d8 | 73 | "If a project knows its root, return it here. |
acc33231 CY |
74 | Allows for one-project-object-for-a-tree type systems." |
75 | (oref this rootproject)) | |
76 | ||
77 | (defmethod ede-project-root-directory ((this ede-project-placeholder) | |
78 | &optional file) | |
cb85c0d8 | 79 | "If a project knows its root, return it here. |
acc33231 CY |
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)))) | |
84 | ||
85 | ||
acc33231 CY |
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))))) | |
91 | ||
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) | |
c7015153 | 106 | ;; We can use inodes, so let's try it. |
acc33231 CY |
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) | |
62a81506 | 115 | (setq ans (ede-find-subproject-for-directory SP dir)))))) |
acc33231 CY |
116 | ans))) |
117 | ||
118 | ;;; DIRECTORY IN OPEN PROJECT | |
119 | ;; | |
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.") | |
127 | ||
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)) | |
133 | ||
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) | |
138 | )) | |
139 | ||
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 | ))))) | |
149 | ||
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)) | |
170 | ||
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) | |
178 | ) | |
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))) | |
184 | ||
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)) | |
193 | ) | |
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))) | |
211 | )) | |
212 | (setq all (cdr all))) | |
213 | ans)) | |
214 | ||
215 | ;;; DIRECTORY-PROJECT-P | |
216 | ;; | |
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.") | |
223 | ||
62a81506 CY |
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))) | |
234 | ) | |
235 | ||
acc33231 CY |
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! | |
240 | ||
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)) | |
249 | )) | |
250 | ||
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))) | |
255 | ||
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)) | |
261 | ||
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. | |
cb85c0d8 | 265 | This depends on an up to date `ede-project-class-files' variable. |
e4920bc9 | 266 | Any directory that contains the file .ede-ignore will always |
cb85c0d8 EL |
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) | |
276 | (t | |
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)))))) | |
acc33231 CY |
289 | |
290 | ;;; TOPLEVEL | |
291 | ;; | |
292 | ;; These utilities will identify the "toplevel" of a project. | |
293 | ;; | |
294 | (defun ede-toplevel-project-or-nil (dir) | |
295 | "Starting with DIR, find the toplevel project directory, or return nil. | |
a785b776 | 296 | nil is returned if the current directory is not a part of a project." |
acc33231 CY |
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)))) | |
303 | ||
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))) | |
317 | )) | |
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))) | |
326 | ||
327 | ;; If PROJ didn't know, or there is no PROJ, then | |
328 | ||
329 | ;; Loop up to the topmost project, and then load that single | |
cb85c0d8 | 330 | ;; project, and its sub projects. When we are done, identify the |
acc33231 CY |
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))) | |
337 | ||
338 | (when proj | |
339 | ;; We can home someone in the middle knows too. | |
340 | (setq ans (ede-project-root-directory proj))) | |
341 | ) | |
342 | (or ans toppath)))))) | |
343 | ||
acc33231 CY |
344 | ;;; DIRECTORY CONVERSION STUFF |
345 | ;; | |
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)))))) | |
359 | ||
cb85c0d8 | 360 | (defmethod ede-convert-path ((this ede-target) path &optional project) |
acc33231 CY |
361 | "Convert path in a standard way for a given project. |
362 | Default to making it project relative. | |
cb85c0d8 EL |
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)))) | |
acc33231 CY |
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)) | |
374 | p)) | |
375 | (error "Parentless target %s" this)))) | |
376 | ||
377 | ;;; FILENAME EXPANSION | |
378 | ;; | |
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))) | |
62a81506 CY |
385 | (when top |
386 | (when (not (slot-boundp top 'locate-obj)) | |
387 | (ede-enable-locate-on-project top)) | |
388 | (oref top locate-obj) | |
389 | ))) | |
acc33231 CY |
390 | |
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. | |
cb85c0d8 EL |
397 | If FORCE equals 'newfile, then the cache is ignored and a new file in THIS |
398 | is returned." | |
acc33231 CY |
399 | (require 'ede/locate) |
400 | (let* ((loc (ede-get-locator-object this)) | |
401 | (ha (ede-locate-file-in-hash loc filename)) | |
402 | (ans nil) | |
403 | ) | |
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. | |
410 | ||
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. | |
426 | (t | |
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 | |
5a89f0a7 | 434 | ;; going to FORCE the file into existence. |
acc33231 CY |
435 | (when (not force) |
436 | (ede-locate-add-file-to-hash loc filename 'nomatch)))) | |
437 | )) | |
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. | |
440 | ||
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)))) | |
445 | ||
446 | ans)) | |
447 | ||
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. | |
cb85c0d8 EL |
459 | (setq found (or (ede-expand-filename-local this filename) |
460 | (ede-expand-filename-impl-via-subproj this filename))) | |
acc33231 CY |
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)) | |
467 | ||
cb85c0d8 EL |
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))))) | |
475 | ||
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))) | |
488 | )) | |
489 | ;; Return it | |
490 | found)) | |
491 | ||
acc33231 CY |
492 | (defmethod ede-expand-filename ((this ede-target) filename &optional force) |
493 | "Return a fully qualified file name based on target THIS. | |
045b9da7 | 494 | FILENAME should be a filename which occurs in a directory in which THIS works. |
acc33231 CY |
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)) | |
498 | ||
499 | ;;; UTILITIES | |
500 | ;; | |
501 | ||
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. | |
509 | nil | |
510 | fnd))) | |
511 | ||
e8cc7880 DE |
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)))) | |
518 | ||
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)) | |
530 | ||
531 | ||
acc33231 CY |
532 | (provide 'ede/files) |
533 | ||
534 | ;; Local variables: | |
535 | ;; generated-autoload-file: "loaddefs.el" | |
acc33231 CY |
536 | ;; generated-autoload-load-name: "ede/files" |
537 | ;; End: | |
538 | ||
539 | ;;; ede/files.el ends here |