map.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. ;;; srecode/map.el --- Manage a template file map
  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. ;; Read template files, and build a map of where they can be found.
  18. ;; Save the map to disk, and refer to it when bootstrapping a new
  19. ;; Emacs session with srecode.
  20. (require 'semantic)
  21. (require 'eieio-base)
  22. (require 'srecode)
  23. ;;; Code:
  24. ;; The defcustom is given at the end of the file.
  25. (defvar srecode-map-load-path)
  26. (defun srecode-map-base-template-dir ()
  27. "Find the base template directory for SRecode."
  28. (expand-file-name "srecode" data-directory))
  29. ;;; Current MAP
  30. ;;
  31. (defvar srecode-current-map nil
  32. "The current map for global SRecode templates.")
  33. (defcustom srecode-map-save-file
  34. (locate-user-emacs-file "srecode-map.el" ".srecode/srecode-map")
  35. "The save location for SRecode's map file.
  36. If the save file is nil, then the MAP is not saved between sessions."
  37. :group 'srecode
  38. :type 'file)
  39. (defclass srecode-map (eieio-persistent)
  40. ((fileheaderline :initform ";; SRECODE TEMPLATE MAP")
  41. (files :initarg :files
  42. :initform nil
  43. :type list
  44. :documentation
  45. "An alist of files and the major-mode that they cover.")
  46. (apps :initarg :apps
  47. :initform nil
  48. :type list
  49. :documentation
  50. "An alist of applications.
  51. Each app keys to an alist of files and modes (as above.)")
  52. )
  53. "A map of srecode templates.")
  54. (defmethod srecode-map-entry-for-file ((map srecode-map) file)
  55. "Return the entry in MAP for FILE."
  56. (assoc file (oref map files)))
  57. (defmethod srecode-map-entries-for-mode ((map srecode-map) mode)
  58. "Return the entries in MAP for major MODE."
  59. (let ((ans nil))
  60. (dolist (f (oref map files))
  61. (when (mode-local-use-bindings-p mode (cdr f))
  62. (setq ans (cons f ans))))
  63. ans))
  64. (defmethod srecode-map-entry-for-app ((map srecode-map) app)
  65. "Return the entry in MAP for APP'lication."
  66. (assoc app (oref map apps))
  67. )
  68. (defmethod srecode-map-entries-for-app-and-mode ((map srecode-map) app mode)
  69. "Return the entries in MAP for major MODE."
  70. (let ((ans nil)
  71. (appentry (srecode-map-entry-for-app map app)))
  72. (dolist (f (cdr appentry))
  73. (when (eq (cdr f) mode)
  74. (setq ans (cons f ans))))
  75. ans))
  76. (defmethod srecode-map-entry-for-file-anywhere ((map srecode-map) file)
  77. "Search in all entry points in MAP for FILE.
  78. Return a list ( APP . FILE-ASSOC ) where APP is nil
  79. in the global map."
  80. (or
  81. ;; Look in the global entry
  82. (let ((globalentry (srecode-map-entry-for-file map file)))
  83. (when globalentry
  84. (cons nil globalentry)))
  85. ;; Look in each app.
  86. (let ((match nil))
  87. (dolist (app (oref map apps))
  88. (let ((appmatch (assoc file (cdr app))))
  89. (when appmatch
  90. (setq match (cons app appmatch)))))
  91. match)
  92. ;; Other?
  93. ))
  94. (defmethod srecode-map-delete-file-entry ((map srecode-map) file)
  95. "Update MAP to exclude FILE from the file list."
  96. (let ((entry (srecode-map-entry-for-file map file)))
  97. (when entry
  98. (object-remove-from-list map 'files entry))))
  99. (defmethod srecode-map-update-file-entry ((map srecode-map) file mode)
  100. "Update a MAP entry for FILE to be used with MODE.
  101. Return non-nil if the MAP was changed."
  102. (let ((entry (srecode-map-entry-for-file map file))
  103. (dirty t))
  104. (cond
  105. ;; It is already a match.. do nothing.
  106. ((and entry (eq (cdr entry) mode))
  107. (setq dirty nil))
  108. ;; We have a non-matching entry. Change the cdr.
  109. (entry
  110. (setcdr entry mode))
  111. ;; No entry, just add it to the list.
  112. (t
  113. (object-add-to-list map 'files (cons file mode))
  114. ))
  115. dirty))
  116. (defmethod srecode-map-delete-file-entry-from-app ((map srecode-map) file app)
  117. "Delete from MAP the FILE entry within the APP'lication."
  118. (let* ((appe (srecode-map-entry-for-app map app))
  119. (fentry (assoc file (cdr appe))))
  120. (setcdr appe (delete fentry (cdr appe))))
  121. )
  122. (defmethod srecode-map-update-app-file-entry ((map srecode-map) file mode app)
  123. "Update the MAP entry for FILE to be used with MODE within APP.
  124. Return non-nil if the map was changed."
  125. (let* ((appentry (srecode-map-entry-for-app map app))
  126. (appfileentry (assoc file (cdr appentry)))
  127. (dirty t)
  128. )
  129. (cond
  130. ;; Option 1 - We have this file in this application already
  131. ;; with the correct mode.
  132. ((and appfileentry (eq (cdr appfileentry) mode))
  133. (setq dirty nil)
  134. )
  135. ;; Option 2 - We have a non-matching entry. Change Cdr.
  136. (appfileentry
  137. (setcdr appfileentry mode))
  138. (t
  139. ;; For option 3 & 4 - remove the entry from any other lists
  140. ;; we can find.
  141. (let ((any (srecode-map-entry-for-file-anywhere map file)))
  142. (when any
  143. (if (null (car any))
  144. ;; Global map entry
  145. (srecode-map-delete-file-entry map file)
  146. ;; Some app
  147. (let ((appentry (srecode-map-entry-for-app map app)))
  148. (setcdr appentry (delete (cdr any) (cdr appentry))))
  149. )))
  150. ;; Now do option 3 and 4
  151. (cond
  152. ;; Option 3 - No entry for app. Add to the list.
  153. (appentry
  154. (setcdr appentry (cons (cons file mode) (cdr appentry)))
  155. )
  156. ;; Option 4 - No app entry. Add app to list with this file.
  157. (t
  158. (object-add-to-list map 'apps (list app (cons file mode)))
  159. )))
  160. )
  161. dirty))
  162. ;;; MAP Updating
  163. ;;
  164. ;;;###autoload
  165. (defun srecode-get-maps (&optional reset)
  166. "Get a list of maps relevant to the current buffer.
  167. Optional argument RESET forces a reset of the current map."
  168. (interactive "P")
  169. ;; Always update the map, but only do a full reset if
  170. ;; the user asks for one.
  171. (srecode-map-update-map (not reset))
  172. (if (called-interactively-p 'any)
  173. ;; Dump this map.
  174. (with-output-to-temp-buffer "*SRECODE MAP*"
  175. (princ " -- SRecode Global map --\n")
  176. (srecode-maps-dump-file-list (oref srecode-current-map files))
  177. (princ "\n -- Application Maps --\n")
  178. (dolist (ap (oref srecode-current-map apps))
  179. (let ((app (car ap))
  180. (files (cdr ap)))
  181. (princ app)
  182. (princ " :\n")
  183. (srecode-maps-dump-file-list files))
  184. (princ "\n"))
  185. (princ "\nUse:\n\n M-x customize-variable RET srecode-map-load-path RET")
  186. (princ "\n To change the path where SRecode loads templates from.")
  187. )
  188. ;; Eventually, I want to return many maps to search through.
  189. (list srecode-current-map)))
  190. (eval-when-compile (require 'data-debug))
  191. (defun srecode-adebug-maps ()
  192. "Run ADEBUG on the output of `srecode-get-maps'."
  193. (interactive)
  194. (require 'data-debug)
  195. (let ((start (current-time))
  196. (p (srecode-get-maps t)) ;; Time the reset.
  197. (end (current-time))
  198. )
  199. (message "Updating the map took %.2f seconds."
  200. (semantic-elapsed-time start end))
  201. (data-debug-new-buffer "*SRECODE ADEBUG*")
  202. (data-debug-insert-stuff-list p "*")))
  203. (defun srecode-maps-dump-file-list (flist)
  204. "Dump a file list FLIST to `standard-output'."
  205. (princ "Mode\t\t\tFilename\n")
  206. (princ "------\t\t\t------------------\n")
  207. (dolist (fe flist)
  208. (prin1 (cdr fe))
  209. (princ "\t")
  210. (when (> (* 2 8) (length (symbol-name (cdr fe))))
  211. (princ "\t"))
  212. (when (> 8 (length (symbol-name (cdr fe))))
  213. (princ "\t"))
  214. (princ (car fe))
  215. (princ "\n")
  216. ))
  217. (defun srecode-map-file-still-valid-p (filename map)
  218. "Return t if FILENAME should be in MAP still."
  219. (let ((valid nil))
  220. (and (file-exists-p filename)
  221. (progn
  222. (dolist (p srecode-map-load-path)
  223. (when (and (< (length p) (length filename))
  224. (string= p (substring filename 0 (length p))))
  225. (setq valid t))
  226. )
  227. valid))
  228. ))
  229. (defun srecode-map-update-map (&optional fast)
  230. "Update the current map from `srecode-map-load-path'.
  231. Scans all the files on the path, and makes sure we have entries
  232. for them.
  233. If option FAST is non-nil, then only parse a file for the mode-string
  234. if that file is NEW, otherwise assume the mode has not changed."
  235. (interactive)
  236. ;; When no map file, we are configured to not use a save file.
  237. (if (not srecode-map-save-file)
  238. ;; 0) Create a MAP when in no save file mode.
  239. (when (not srecode-current-map)
  240. (setq srecode-current-map (srecode-map "SRecode Map"))
  241. (message "SRecode map created in non-save mode.")
  242. )
  243. ;; 1) Do we even have a MAP or save file?
  244. (when (and (not srecode-current-map)
  245. (not (file-exists-p srecode-map-save-file)))
  246. (when (not (file-exists-p (file-name-directory srecode-map-save-file)))
  247. ;; Only bother with this interactively, not during a build
  248. ;; or test.
  249. (when (not noninteractive)
  250. ;; No map, make the dir?
  251. (if (y-or-n-p (format "Create dir %s? "
  252. (file-name-directory srecode-map-save-file)))
  253. (make-directory (file-name-directory srecode-map-save-file))
  254. ;; No make, change save file
  255. (customize-variable 'srecode-map-save-file)
  256. (error "Change your SRecode map file"))))
  257. ;; Have a dir. Make the object.
  258. (setq srecode-current-map
  259. (srecode-map "SRecode Map"
  260. :file srecode-map-save-file)))
  261. ;; 2) Do we not have a current map? If so load.
  262. (when (not srecode-current-map)
  263. (condition-case nil
  264. (setq srecode-current-map
  265. (eieio-persistent-read srecode-map-save-file))
  266. (error
  267. ;; There was an error loading the old map. Create a new one.
  268. (setq srecode-current-map
  269. (srecode-map "SRecode Map"
  270. :file srecode-map-save-file))))
  271. )
  272. )
  273. ;;
  274. ;; We better have a MAP object now.
  275. ;;
  276. (let ((dirty nil))
  277. ;; 3) - Purge dead files from the file list.
  278. (dolist (entry (copy-sequence (oref srecode-current-map files)))
  279. (when (not (srecode-map-file-still-valid-p
  280. (car entry) srecode-current-map))
  281. (srecode-map-delete-file-entry srecode-current-map (car entry))
  282. (setq dirty t)
  283. ))
  284. (dolist (app (copy-sequence (oref srecode-current-map apps)))
  285. (dolist (entry (copy-sequence (cdr app)))
  286. (when (not (srecode-map-file-still-valid-p
  287. (car entry) srecode-current-map))
  288. (srecode-map-delete-file-entry-from-app
  289. srecode-current-map (car entry) (car app))
  290. (setq dirty t)
  291. )))
  292. ;; 4) - Find new files and add them to the map.
  293. (dolist (dir srecode-map-load-path)
  294. (when (file-exists-p dir)
  295. (dolist (f (directory-files dir t "\\.srt$"))
  296. (when (and (not (backup-file-name-p f))
  297. (not (auto-save-file-name-p f))
  298. (file-readable-p f))
  299. (let ((fdirty (srecode-map-validate-file-for-mode f fast)))
  300. (setq dirty (or dirty fdirty))))
  301. )))
  302. ;; Only do the save if we are dirty, or if we are in an interactive
  303. ;; Emacs.
  304. (when (and dirty (not noninteractive)
  305. (slot-boundp srecode-current-map :file))
  306. (eieio-persistent-save srecode-current-map))
  307. ))
  308. (defun srecode-map-validate-file-for-mode (file fast)
  309. "Read and validate FILE via the parser. Return the mode.
  310. Argument FAST implies that the file should not be reparsed if there
  311. is already an entry for it.
  312. Return non-nil if the map changed."
  313. (when (or (not fast)
  314. (not (srecode-map-entry-for-file-anywhere srecode-current-map file)))
  315. (let ((buff-orig (get-file-buffer file))
  316. (dirty nil))
  317. (save-excursion
  318. (if buff-orig
  319. (set-buffer buff-orig)
  320. (set-buffer (get-buffer-create " *srecode-map-tmp*"))
  321. (insert-file-contents file nil nil nil t)
  322. ;; Force it to be ready to parse.
  323. (srecode-template-mode)
  324. (let ((semantic-init-hook nil))
  325. (semantic-new-buffer-fcn))
  326. )
  327. (semantic-fetch-tags)
  328. (let* ((mode-tag
  329. (semantic-find-first-tag-by-name "mode" (current-buffer)))
  330. (val nil)
  331. (app-tag
  332. (semantic-find-first-tag-by-name "application" (current-buffer)))
  333. (app nil))
  334. (if mode-tag
  335. (setq val (car (semantic-tag-variable-default mode-tag)))
  336. (error "There should be a mode declaration in %s" file))
  337. (when app-tag
  338. (setq app (car (semantic-tag-variable-default app-tag))))
  339. (setq dirty
  340. (if app
  341. (srecode-map-update-app-file-entry srecode-current-map
  342. file
  343. (read val)
  344. (read app))
  345. (srecode-map-update-file-entry srecode-current-map
  346. file
  347. (read val))))
  348. )
  349. )
  350. dirty)))
  351. ;;; THE PATH
  352. ;;
  353. ;; We need to do this last since the setter needs the above code.
  354. (defun srecode-map-load-path-set (sym val)
  355. "Set SYM to the new VAL, then update the srecode map."
  356. (set-default sym val)
  357. (srecode-map-update-map t))
  358. (defcustom srecode-map-load-path
  359. (list (srecode-map-base-template-dir)
  360. (expand-file-name "~/.srecode/")
  361. )
  362. "Global load path for SRecode template files."
  363. :group 'srecode
  364. :type '(repeat file)
  365. :set 'srecode-map-load-path-set)
  366. (provide 'srecode/map)
  367. ;; Local variables:
  368. ;; generated-autoload-file: "loaddefs.el"
  369. ;; generated-autoload-load-name: "srecode/map"
  370. ;; End:
  371. ;;; srecode/map.el ends here