files.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. ;;; ede/files.el --- Associate projects with files and directories.
  2. ;; Copyright (C) 2008-2015 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* ((proj (ede-current-project))
  43. (fname (ede-expand-filename proj file))
  44. )
  45. (unless fname
  46. (error "Could not find %s in %s"
  47. file
  48. (ede-project-root-directory proj)))
  49. (find-file fname)))
  50. (defun ede-flush-project-hash ()
  51. "Flush the file locate hash for the current project."
  52. (interactive)
  53. (require 'ede/locate)
  54. (let* ((loc (ede-get-locator-object (ede-current-project))))
  55. (when loc
  56. (ede-locate-flush-hash loc))))
  57. ;;; Placeholders for ROOT directory scanning on base objects
  58. ;;
  59. (cl-defmethod ede-project-root ((this ede-project-placeholder))
  60. "If a project knows its root, return it here.
  61. Allows for one-project-object-for-a-tree type systems."
  62. (oref this rootproject))
  63. (cl-defmethod ede-project-root-directory ((this ede-project-placeholder)
  64. &optional file)
  65. "If a project knows its root, return it here.
  66. Allows for one-project-object-for-a-tree type systems.
  67. Optional FILE is the file to test. It is ignored in preference
  68. of the anchor file for the project."
  69. (let ((root (or (ede-project-root this) this)))
  70. (file-name-directory (expand-file-name (oref this file)))))
  71. ;; Why INODEs?
  72. ;; An inode represents a unique ID that transcends symlinks, hardlinks, etc.
  73. ;; so when we cache an inode in a project, and hash directories to inodes, we
  74. ;; can avoid costly filesystem queries and regex matches.
  75. (defvar ede-inode-directory-hash (make-hash-table
  76. ;; Note on test. Can we compare inodes or something?
  77. :test 'equal)
  78. "A hash of directory names and inodes.")
  79. (defun ede--put-inode-dir-hash (dir inode)
  80. "Add to the EDE project hash DIR associated with INODE."
  81. (when (fboundp 'puthash)
  82. (puthash dir inode ede-inode-directory-hash)
  83. inode))
  84. (defun ede--get-inode-dir-hash (dir)
  85. "Get the EDE project hash DIR associated with INODE."
  86. (when (fboundp 'gethash)
  87. (gethash dir ede-inode-directory-hash)
  88. ))
  89. (defun ede--inode-for-dir (dir)
  90. "Return the inode for the directory DIR."
  91. (let ((hashnode (ede--get-inode-dir-hash (expand-file-name dir))))
  92. (or hashnode
  93. (if ede--disable-inode
  94. (ede--put-inode-dir-hash dir 0)
  95. (let ((fattr (file-attributes dir)))
  96. (ede--put-inode-dir-hash dir (nth 10 fattr))
  97. )))))
  98. (cl-defmethod ede--project-inode ((proj ede-project-placeholder))
  99. "Get the inode of the directory project PROJ is in."
  100. (if (slot-boundp proj 'dirinode)
  101. (oref proj dirinode)
  102. (oset proj dirinode (ede--inode-for-dir (oref proj :directory)))))
  103. (defun ede--inode-get-toplevel-open-project (inode)
  104. "Return an already open toplevel project that is managing INODE.
  105. Does not check subprojects."
  106. (when (or (and (numberp inode) (/= inode 0))
  107. (consp inode))
  108. (let ((all ede-projects)
  109. (found nil)
  110. )
  111. (while (and all (not found))
  112. (when (equal inode (ede--project-inode (car all)))
  113. (setq found (car all)))
  114. (setq all (cdr all)))
  115. found)))
  116. ;;; DIRECTORY IN OPEN PROJECT
  117. ;;
  118. ;; These routines match some directory name to one of the many pre-existing
  119. ;; open projects. This should avoid hitting the disk, or asking lots of questions
  120. ;; if used throughout the other routines.
  121. (defun ede-directory-get-open-project (dir &optional rootreturn)
  122. "Return an already open project that is managing DIR.
  123. Optional ROOTRETURN specifies a symbol to set to the root project.
  124. If DIR is the root project, then it is the same."
  125. (let* ((inode (ede--inode-for-dir dir))
  126. (ft (file-name-as-directory (expand-file-name dir)))
  127. (proj (ede--inode-get-toplevel-open-project inode))
  128. (ans nil))
  129. ;; Try file based search.
  130. (when (or ede--disable-inode (not proj))
  131. (setq proj (ede-directory-get-toplevel-open-project ft)))
  132. ;; Default answer is this project
  133. (setq ans proj)
  134. ;; Save.
  135. (when rootreturn (set rootreturn proj))
  136. ;; Find subprojects.
  137. (when (and proj (if ede--disable-inode
  138. (not (string= ft (expand-file-name (oref proj :directory))))
  139. (not (equal inode (ede--project-inode proj)))))
  140. (setq ans (ede-find-subproject-for-directory proj ft)))
  141. ans))
  142. ;; Force all users to switch to `ede-directory-get-open-project'
  143. ;; for performance reasons.
  144. (defun ede-directory-get-toplevel-open-project (dir &optional exact)
  145. "Return an already open toplevel project that is managing DIR.
  146. If optional EXACT is non-nil, only return exact matches for DIR."
  147. (let ((ft (file-name-as-directory (expand-file-name dir)))
  148. (all ede-projects)
  149. (ans nil)
  150. (shortans nil))
  151. (while (and all (not ans))
  152. ;; Do the check.
  153. (let ((pd (expand-file-name (oref (car all) :directory)))
  154. )
  155. (cond
  156. ;; Exact text match.
  157. ((string= pd ft)
  158. (setq ans (car all)))
  159. ;; Some sub-directory
  160. ((and (not exact) (string-match (concat "^" (regexp-quote pd)) ft))
  161. (if (not shortans)
  162. (setq shortans (car all))
  163. ;; We already have a short answer, so see if pd (the match we found)
  164. ;; is longer. If it is longer, then it is more precise.
  165. (when (< (length (oref shortans :directory))
  166. (length pd))
  167. (setq shortans (car all))))
  168. )
  169. ;; Exact inode match. Useful with symlinks or complex automounters.
  170. ((and (not ede--disable-inode)
  171. (let ((pin (ede--project-inode (car all)))
  172. (inode (ede--inode-for-dir dir)))
  173. (and (not (eql pin 0)) (equal pin inode))))
  174. (setq ans (car all)))
  175. ;; Subdir via truename - slower by far, but faster than a traditional lookup.
  176. ;; Note that we must resort to truename in order to resolve issues such as
  177. ;; cross-symlink projects.
  178. ((and (not exact)
  179. (let ((ftn (file-truename ft))
  180. (ptd (file-truename pd)))
  181. (string-match (concat "^" (regexp-quote ptd)) ftn)))
  182. (if (not shortans)
  183. (setq shortans (car all))
  184. ;; We already have a short answer, so see if pd (the match we found)
  185. ;; is longer. If it is longer, then it is more precise.
  186. (when (< (length (expand-file-name (oref shortans :directory)))
  187. (length pd))
  188. (setq shortans (car all))))
  189. )))
  190. (setq all (cdr all)))
  191. ;; If we have an exact answer, use that, otherwise use
  192. ;; the short answer we found -> ie - we are in a subproject.
  193. (or ans shortans)))
  194. (cl-defmethod ede-find-subproject-for-directory ((proj ede-project-placeholder)
  195. dir)
  196. "Find a subproject of PROJ that corresponds to DIR."
  197. (if ede--disable-inode
  198. (let ((ans nil)
  199. (fulldir (file-truename dir)))
  200. ;; Try to find the right project w/out inodes.
  201. (ede-map-subprojects
  202. proj
  203. (lambda (SP)
  204. (when (not ans)
  205. (if (string= fulldir (file-truename (oref SP :directory)))
  206. (setq ans SP)
  207. (ede-find-subproject-for-directory SP dir)))))
  208. ans)
  209. ;; We can use inodes, so let's try it.
  210. (let ((ans nil)
  211. (inode (ede--inode-for-dir dir)))
  212. (ede-map-subprojects
  213. proj
  214. (lambda (SP)
  215. (when (not ans)
  216. (if (equal (ede--project-inode SP) inode)
  217. (setq ans SP)
  218. (setq ans (ede-find-subproject-for-directory SP dir))))))
  219. ans)))
  220. ;;; DIRECTORY HASH
  221. ;;
  222. ;; The directory hash matches expanded directory names to already detected
  223. ;; projects. By hashing projects to directories, we can detect projects in
  224. ;; places we have been before much more quickly.
  225. (defvar ede-project-directory-hash (make-hash-table
  226. ;; Note on test. Can we compare inodes or something?
  227. :test 'equal)
  228. "A hash of directory names and associated EDE objects.")
  229. (defun ede-flush-directory-hash ()
  230. "Flush the project directory hash.
  231. Do this only when developing new projects that are incorrectly putting
  232. 'nomatch tokens into the hash."
  233. (interactive)
  234. (setq ede-project-directory-hash (make-hash-table :test 'equal))
  235. ;; Also slush the current project's locator hash.
  236. (let ((loc (ede-get-locator-object ede-object)))
  237. (when loc
  238. (ede-locate-flush-hash loc)))
  239. )
  240. (defun ede-project-directory-remove-hash (dir)
  241. "Reset the directory hash for DIR.
  242. Do this whenever a new project is created, as opposed to loaded."
  243. ;; TODO - Use maphash, and delete by regexp, not by dir searching!
  244. (setq dir (expand-file-name dir))
  245. (when (fboundp 'remhash)
  246. (remhash (file-name-as-directory dir) ede-project-directory-hash)
  247. ;; Look for all subdirs of D, and remove them.
  248. (let ((match (concat "^" (regexp-quote dir))))
  249. (maphash (lambda (K O)
  250. (when (string-match match K)
  251. (remhash K ede-project-directory-hash)))
  252. ede-project-directory-hash))
  253. ))
  254. (defun ede--directory-project-from-hash (dir)
  255. "If there is an already loaded project for DIR, return it from the hash."
  256. (when (fboundp 'gethash)
  257. (setq dir (expand-file-name dir))
  258. (gethash dir ede-project-directory-hash nil)))
  259. (defun ede--directory-project-add-description-to-hash (dir desc)
  260. "Add to the EDE project hash DIR associated with DESC."
  261. (when (fboundp 'puthash)
  262. (setq dir (expand-file-name dir))
  263. (puthash dir desc ede-project-directory-hash)
  264. desc))
  265. ;;; DIRECTORY-PROJECT-P, -CONS
  266. ;;
  267. ;; These routines are useful for detecting if a project exists
  268. ;; in a provided directory.
  269. ;;
  270. ;; Note that -P provides less information than -CONS, so use -CONS
  271. ;; instead so that -P can be obsoleted.
  272. (defun ede-directory-project-p (dir &optional force)
  273. "Return a project description object if DIR is in a project.
  274. Optional argument FORCE means to ignore a hash-hit of 'nomatch.
  275. This depends on an up to date `ede-project-class-files' variable.
  276. Any directory that contains the file .ede-ignore will always
  277. return nil.
  278. Consider using `ede-directory-project-cons' instead if the next
  279. question you want to ask is where the root of found project is."
  280. ;; @TODO - We used to have a full impl here, but moved it all
  281. ;; to ede-directory-project-cons, and now hash contains only
  282. ;; the results of detection which includes the root dir.
  283. ;; Perhaps we can eventually remove this fcn?
  284. (let ((detect (ede-directory-project-cons dir force)))
  285. (cdr detect)))
  286. (defun ede-directory-project-cons (dir &optional force)
  287. "Return a project CONS (ROOTDIR . AUTOLOAD) for DIR.
  288. If there is no project in DIR, return nil.
  289. Optional FORCE means to ignore the hash of known directories."
  290. (when (not (file-exists-p (expand-file-name ".ede-ignore" dir)))
  291. (let* ((dirtest (expand-file-name dir))
  292. (match (ede--directory-project-from-hash dirtest)))
  293. (cond
  294. ((and (eq match 'nomatch) (not force))
  295. nil)
  296. ((and match (not (eq match 'nomatch)))
  297. match)
  298. (t
  299. ;; First time here? Use the detection code to identify if we have
  300. ;; a project here.
  301. (let* ((detect (ede-detect-directory-for-project dirtest))
  302. (autoloader (cdr detect))) ;; autoloader
  303. (when autoloader (require (oref autoloader file)))
  304. (ede--directory-project-add-description-to-hash dirtest (or detect 'nomatch))
  305. detect)
  306. )))))
  307. ;;; TOPLEVEL
  308. ;;
  309. ;; These utilities will identify the "toplevel" of a project.
  310. ;;
  311. ;; NOTE: These two -toplevel- functions return a directory even though
  312. ;; the function name implies a project.
  313. (defun ede-toplevel-project (dir)
  314. "Starting with DIR, find the toplevel project directory.
  315. If DIR is not part of a project, return nil."
  316. (let ((ans nil))
  317. (cond
  318. ;; Check if it is cached in the current buffer.
  319. ((and (string= dir default-directory)
  320. ede-object-root-project)
  321. ;; Try the local buffer cache first.
  322. (oref ede-object-root-project :directory))
  323. ;; See if there is an existing project in DIR.
  324. ((setq ans (ede-directory-get-toplevel-open-project dir))
  325. (oref ans :directory))
  326. ;; Detect using our file system detector.
  327. ((setq ans (ede-detect-directory-for-project dir))
  328. (car ans))
  329. (t nil))))
  330. (defalias 'ede-toplevel-project-or-nil 'ede-toplevel-project)
  331. ;;; DIRECTORY CONVERSION STUFF
  332. ;;
  333. (cl-defmethod ede-convert-path ((this ede-project) path)
  334. "Convert path in a standard way for a given project.
  335. Default to making it project relative.
  336. Argument THIS is the project to convert PATH to."
  337. (let ((pp (ede-project-root-directory this))
  338. (fp (expand-file-name path)))
  339. (if (string-match (regexp-quote pp) fp)
  340. (substring fp (match-end 0))
  341. (let ((pptf (file-truename pp))
  342. (fptf (file-truename fp)))
  343. (if (string-match (regexp-quote pptf) fptf)
  344. (substring fptf (match-end 0))
  345. (error "Cannot convert relativize path %s" fp))))))
  346. (cl-defmethod ede-convert-path ((this ede-target) path &optional project)
  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. Optional PROJECT is the project that THIS belongs to. Associating
  351. a target to a project is expensive, so using this can speed things up."
  352. (let ((proj (or project (ede-target-parent this))))
  353. (if proj
  354. (let ((p (ede-convert-path proj path))
  355. (lp (or (oref this path) "")))
  356. ;; Our target THIS may have path information.
  357. ;; strip this out of the conversion.
  358. (if (string-match (concat "^" (regexp-quote lp)) p)
  359. (substring p (length lp))
  360. p))
  361. (error "Parentless target %s" this))))
  362. ;;; FILENAME EXPANSION
  363. ;;
  364. (defun ede-get-locator-object (proj)
  365. "Get the locator object for project PROJ.
  366. Get it from the toplevel project. If it doesn't have one, make one."
  367. ;; Make sure we have a location object available for
  368. ;; caching values, and for locating things more robustly.
  369. (let ((top (ede-toplevel proj)))
  370. (when top
  371. (when (not (slot-boundp top 'locate-obj))
  372. (ede-enable-locate-on-project top))
  373. (oref top locate-obj)
  374. )))
  375. (cl-defmethod ede-expand-filename ((this ede-project) filename &optional force)
  376. "Return a fully qualified file name based on project THIS.
  377. FILENAME should be just a filename which occurs in a directory controlled
  378. by this project.
  379. Optional argument FORCE forces the default filename to be provided even if it
  380. doesn't exist.
  381. If FORCE equals 'newfile, then the cache is ignored and a new file in THIS
  382. is returned."
  383. (require 'ede/locate)
  384. (let* ((loc (ede-get-locator-object this))
  385. (ha (ede-locate-file-in-hash loc filename))
  386. (ans nil)
  387. )
  388. ;; NOTE: This function uses a locator object, which keeps a hash
  389. ;; table of files it has found in the past. The hash table is
  390. ;; used to make commonly found file very fast to location. Some
  391. ;; complex routines, such as smart completion asks this question
  392. ;; many times, so doing this speeds things up, especially on NFS
  393. ;; or other remote file systems.
  394. ;; As such, special care is needed to use the hash, and also obey
  395. ;; the FORCE option, which is needed when trying to identify some
  396. ;; new file that needs to be created, such as a Makefile.
  397. (cond
  398. ;; We have a hash-table match, AND that match wasn't the 'nomatch
  399. ;; flag, we can return it.
  400. ((and ha (not (eq ha 'nomatch)))
  401. (setq ans ha))
  402. ;; If we had a match, and it WAS no match, then we need to look
  403. ;; at the force-option to see what to do. Since ans is already
  404. ;; nil, then we do nothing.
  405. ((and (eq ha 'nomatch) (not (eq force 'newfile)))
  406. nil)
  407. ;; We had no hash table match, so we have to look up this file
  408. ;; using the usual EDE file expansion rules.
  409. (t
  410. (let ((calc (ede-expand-filename-impl this filename)))
  411. (if calc
  412. (progn
  413. (ede-locate-add-file-to-hash loc filename calc)
  414. (setq ans calc))
  415. ;; If we failed to calculate something, we
  416. ;; should add it to the hash, but ONLY if we are not
  417. ;; going to FORCE the file into existence.
  418. (when (not force)
  419. (ede-locate-add-file-to-hash loc filename 'nomatch))))
  420. ))
  421. ;; Now that all options have been queried, if the FORCE option is
  422. ;; true, but ANS is still nil, then we can make up a file name.
  423. ;; Is it forced?
  424. (when (and force (not ans))
  425. (let ((dir (ede-project-root-directory this)))
  426. (setq ans (expand-file-name filename dir))))
  427. ans))
  428. (cl-defmethod ede-expand-filename-impl ((this ede-project) filename &optional force)
  429. "Return a fully qualified file name based on project THIS.
  430. FILENAME should be just a filename which occurs in a directory controlled
  431. by this project.
  432. Optional argument FORCE forces the default filename to be provided even if it
  433. doesn't exist."
  434. (let ((loc (ede-get-locator-object this))
  435. (path (ede-project-root-directory this))
  436. (proj (oref this subproj))
  437. (found nil))
  438. ;; find it Locally.
  439. (setq found (or (ede-expand-filename-local this filename)
  440. (ede-expand-filename-impl-via-subproj this filename)))
  441. ;; Use an external locate tool.
  442. (when (not found)
  443. (require 'ede/locate)
  444. (setq found (car (ede-locate-file-in-project loc filename))))
  445. ;; Return it
  446. found))
  447. (cl-defmethod ede-expand-filename-local ((this ede-project) filename)
  448. "Expand filename locally to project THIS with filesystem tests."
  449. (let ((path (ede-project-root-directory this)))
  450. (cond ((file-exists-p (expand-file-name filename path))
  451. (expand-file-name filename path))
  452. ((file-exists-p (expand-file-name (concat "include/" filename) path))
  453. (expand-file-name (concat "include/" filename) path)))))
  454. (cl-defmethod ede-expand-filename-impl-via-subproj ((this ede-project) filename)
  455. "Return a fully qualified file name based on project THIS.
  456. FILENAME should be just a filename which occurs in a directory controlled
  457. by this project."
  458. (let ((proj (list (ede-toplevel this)))
  459. (found nil))
  460. ;; find it Locally.
  461. (while (and (not found) proj)
  462. (let ((thisproj (car proj)))
  463. (setq proj (append (cdr proj) (oref thisproj subproj)))
  464. (setq found (when thisproj
  465. (ede-expand-filename-local thisproj filename)))
  466. ))
  467. ;; Return it
  468. found))
  469. (cl-defmethod ede-expand-filename ((this ede-target) filename &optional force)
  470. "Return a fully qualified file name based on target THIS.
  471. FILENAME should be a filename which occurs in a directory in which THIS works.
  472. Optional argument FORCE forces the default filename to be provided even if it
  473. doesn't exist."
  474. (ede-expand-filename (ede-target-parent this) filename force))
  475. ;;; UTILITIES
  476. ;;
  477. (defun ede-up-directory (dir)
  478. "Return a dir that is up one directory.
  479. Argument DIR is the directory to trim upwards."
  480. (let* ((fad (directory-file-name dir))
  481. (fnd (file-name-directory fad)))
  482. (if (string= dir fnd) ; This will catch the old string-match against
  483. ; c:/ for DOS like systems.
  484. nil
  485. fnd)))
  486. (provide 'ede/files)
  487. ;; Local variables:
  488. ;; generated-autoload-file: "loaddefs.el"
  489. ;; generated-autoload-load-name: "ede/files"
  490. ;; End:
  491. ;;; ede/files.el ends here