files.el 18 KB

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