1 ;;; srecode/map.el --- Manage a template file map
3 ;; Copyright (C) 2008-2013 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 ;; Read template files, and build a map of where they can be found.
25 ;; Save the map to disk, and refer to it when bootstrapping a new
26 ;; Emacs session with srecode.
34 ;; The defcustom is given at the end of the file.
35 (defvar srecode-map-load-path
)
37 (defun srecode-map-base-template-dir ()
38 "Find the base template directory for SRecode."
39 (expand-file-name "srecode" data-directory
))
44 (defvar srecode-current-map nil
45 "The current map for global SRecode templates.")
47 (defcustom srecode-map-save-file
48 (locate-user-emacs-file "srecode-map.el" ".srecode/srecode-map")
49 "The save location for SRecode's map file.
50 If the save file is nil, then the MAP is not saved between sessions."
54 (defclass srecode-map
(eieio-persistent)
55 ((fileheaderline :initform
";; SRECODE TEMPLATE MAP")
56 (files :initarg
:files
60 "An alist of files and the major-mode that they cover.")
65 "An alist of applications.
66 Each app keys to an alist of files and modes (as above.)")
68 "A map of srecode templates.")
70 (defmethod srecode-map-entry-for-file ((map srecode-map
) file
)
71 "Return the entry in MAP for FILE."
72 (assoc file
(oref map files
)))
74 (defmethod srecode-map-entries-for-mode ((map srecode-map
) mode
)
75 "Return the entries in MAP for major MODE."
77 (dolist (f (oref map files
))
78 (when (mode-local-use-bindings-p mode
(cdr f
))
79 (setq ans
(cons f ans
))))
82 (defmethod srecode-map-entry-for-app ((map srecode-map
) app
)
83 "Return the entry in MAP for APP'lication."
84 (assoc app
(oref map apps
))
87 (defmethod srecode-map-entries-for-app-and-mode ((map srecode-map
) app mode
)
88 "Return the entries in MAP for major MODE."
90 (appentry (srecode-map-entry-for-app map app
)))
91 (dolist (f (cdr appentry
))
92 (when (eq (cdr f
) mode
)
93 (setq ans
(cons f ans
))))
96 (defmethod srecode-map-entry-for-file-anywhere ((map srecode-map
) file
)
97 "Search in all entry points in MAP for FILE.
98 Return a list ( APP . FILE-ASSOC ) where APP is nil
101 ;; Look in the global entry
102 (let ((globalentry (srecode-map-entry-for-file map file
)))
104 (cons nil globalentry
)))
107 (dolist (app (oref map apps
))
108 (let ((appmatch (assoc file
(cdr app
))))
110 (setq match
(cons app appmatch
)))))
115 (defmethod srecode-map-delete-file-entry ((map srecode-map
) file
)
116 "Update MAP to exclude FILE from the file list."
117 (let ((entry (srecode-map-entry-for-file map file
)))
119 (object-remove-from-list map
'files entry
))))
121 (defmethod srecode-map-update-file-entry ((map srecode-map
) file mode
)
122 "Update a MAP entry for FILE to be used with MODE.
123 Return non-nil if the MAP was changed."
124 (let ((entry (srecode-map-entry-for-file map file
))
127 ;; It is already a match.. do nothing.
128 ((and entry
(eq (cdr entry
) mode
))
130 ;; We have a non-matching entry. Change the cdr.
133 ;; No entry, just add it to the list.
135 (object-add-to-list map
'files
(cons file mode
))
139 (defmethod srecode-map-delete-file-entry-from-app ((map srecode-map
) file app
)
140 "Delete from MAP the FILE entry within the APP'lication."
141 (let* ((appe (srecode-map-entry-for-app map app
))
142 (fentry (assoc file
(cdr appe
))))
143 (setcdr appe
(delete fentry
(cdr appe
))))
146 (defmethod srecode-map-update-app-file-entry ((map srecode-map
) file mode app
)
147 "Update the MAP entry for FILE to be used with MODE within APP.
148 Return non-nil if the map was changed."
149 (let* ((appentry (srecode-map-entry-for-app map app
))
150 (appfileentry (assoc file
(cdr appentry
)))
154 ;; Option 1 - We have this file in this application already
155 ;; with the correct mode.
156 ((and appfileentry
(eq (cdr appfileentry
) mode
))
159 ;; Option 2 - We have a non-matching entry. Change Cdr.
161 (setcdr appfileentry mode
))
163 ;; For option 3 & 4 - remove the entry from any other lists
165 (let ((any (srecode-map-entry-for-file-anywhere map file
)))
169 (srecode-map-delete-file-entry map file
)
171 (let ((appentry (srecode-map-entry-for-app map app
)))
172 (setcdr appentry
(delete (cdr any
) (cdr appentry
))))
174 ;; Now do option 3 and 4
176 ;; Option 3 - No entry for app. Add to the list.
178 (setcdr appentry
(cons (cons file mode
) (cdr appentry
)))
180 ;; Option 4 - No app entry. Add app to list with this file.
182 (object-add-to-list map
'apps
(list app
(cons file mode
)))
191 (defun srecode-get-maps (&optional reset
)
192 "Get a list of maps relevant to the current buffer.
193 Optional argument RESET forces a reset of the current map."
195 ;; Always update the map, but only do a full reset if
196 ;; the user asks for one.
197 (srecode-map-update-map (not reset
))
199 (if (called-interactively-p 'any
)
201 (with-output-to-temp-buffer "*SRECODE MAP*"
202 (princ " -- SRecode Global map --\n")
203 (srecode-maps-dump-file-list (oref srecode-current-map files
))
204 (princ "\n -- Application Maps --\n")
205 (dolist (ap (oref srecode-current-map apps
))
210 (srecode-maps-dump-file-list files
))
212 (princ "\nUse:\n\n M-x customize-variable RET srecode-map-load-path RET")
213 (princ "\n To change the path where SRecode loads templates from.")
215 ;; Eventually, I want to return many maps to search through.
216 (list srecode-current-map
)))
218 (declare-function data-debug-new-buffer
"data-debug")
219 (declare-function data-debug-insert-stuff-list
"data-debug")
221 (defun srecode-adebug-maps ()
222 "Run ADEBUG on the output of `srecode-get-maps'."
224 (require 'data-debug
)
225 (let ((start (current-time))
226 (p (srecode-get-maps t
)) ;; Time the reset.
229 (message "Updating the map took %.2f seconds."
230 (semantic-elapsed-time start end
))
231 (data-debug-new-buffer "*SRECODE ADEBUG*")
232 (data-debug-insert-stuff-list p
"*")))
234 (defun srecode-maps-dump-file-list (flist)
235 "Dump a file list FLIST to `standard-output'."
236 (princ "Mode\t\t\tFilename\n")
237 (princ "------\t\t\t------------------\n")
241 (when (> (* 2 8) (length (symbol-name (cdr fe
))))
243 (when (> 8 (length (symbol-name (cdr fe
))))
249 (defun srecode-map-file-still-valid-p (filename map
)
250 "Return t if FILENAME should be in MAP still."
252 (and (file-exists-p filename
)
254 (dolist (p srecode-map-load-path
)
255 (when (and (< (length p
) (length filename
))
256 (string= p
(substring filename
0 (length p
))))
262 (defun srecode-map-update-map (&optional fast
)
263 "Update the current map from `srecode-map-load-path'.
264 Scans all the files on the path, and makes sure we have entries
266 If option FAST is non-nil, then only parse a file for the mode-string
267 if that file is NEW, otherwise assume the mode has not changed."
270 ;; When no map file, we are configured to not use a save file.
271 (if (not srecode-map-save-file
)
272 ;; 0) Create a MAP when in no save file mode.
273 (when (not srecode-current-map
)
274 (setq srecode-current-map
(srecode-map "SRecode Map"))
275 (message "SRecode map created in non-save mode.")
278 ;; 1) Do we even have a MAP or save file?
279 (when (and (not srecode-current-map
)
280 (not (file-exists-p srecode-map-save-file
)))
281 (when (not (file-exists-p (file-name-directory srecode-map-save-file
)))
282 ;; Only bother with this interactively, not during a build
284 (when (not noninteractive
)
285 ;; No map, make the dir?
286 (if (y-or-n-p (format "Create dir %s? "
287 (file-name-directory srecode-map-save-file
)))
288 (make-directory (file-name-directory srecode-map-save-file
))
289 ;; No make, change save file
290 (customize-variable 'srecode-map-save-file
)
291 (error "Change your SRecode map file"))))
292 ;; Have a dir. Make the object.
293 (setq srecode-current-map
294 (srecode-map "SRecode Map"
295 :file srecode-map-save-file
)))
297 ;; 2) Do we not have a current map? If so load.
298 (when (not srecode-current-map
)
300 (setq srecode-current-map
301 (eieio-persistent-read srecode-map-save-file srecode-map
))
303 ;; There was an error loading the old map. Create a new one.
304 (setq srecode-current-map
305 (srecode-map "SRecode Map"
306 :file srecode-map-save-file
))))
312 ;; We better have a MAP object now.
315 ;; 3) - Purge dead files from the file list.
316 (dolist (entry (copy-sequence (oref srecode-current-map files
)))
317 (when (not (srecode-map-file-still-valid-p
318 (car entry
) srecode-current-map
))
319 (srecode-map-delete-file-entry srecode-current-map
(car entry
))
322 (dolist (app (copy-sequence (oref srecode-current-map apps
)))
323 (dolist (entry (copy-sequence (cdr app
)))
324 (when (not (srecode-map-file-still-valid-p
325 (car entry
) srecode-current-map
))
326 (srecode-map-delete-file-entry-from-app
327 srecode-current-map
(car entry
) (car app
))
330 ;; 4) - Find new files and add them to the map.
331 (dolist (dir srecode-map-load-path
)
332 (when (file-exists-p dir
)
333 (dolist (f (directory-files dir t
"\\.srt$"))
334 (when (and (not (backup-file-name-p f
))
335 (not (auto-save-file-name-p f
))
337 (let ((fdirty (srecode-map-validate-file-for-mode f fast
)))
338 (setq dirty
(or dirty fdirty
))))
340 ;; Only do the save if we are dirty, or if we are in an interactive
342 (when (and dirty
(not noninteractive
)
343 (slot-boundp srecode-current-map
:file
))
344 (eieio-persistent-save srecode-current-map
))
347 (defun srecode-map-validate-file-for-mode (file fast
)
348 "Read and validate FILE via the parser. Return the mode.
349 Argument FAST implies that the file should not be reparsed if there
350 is already an entry for it.
351 Return non-nil if the map changed."
353 (not (srecode-map-entry-for-file-anywhere srecode-current-map file
)))
354 (let ((buff-orig (get-file-buffer file
))
358 (set-buffer buff-orig
)
359 (set-buffer (get-buffer-create " *srecode-map-tmp*"))
360 (insert-file-contents file nil nil nil t
)
361 ;; Force it to be ready to parse.
362 (srecode-template-mode)
363 (let ((semantic-init-hook nil
))
364 (semantic-new-buffer-fcn))
366 ;; Force semantic to be enabled in this buffer.
367 (unless (semantic-active-p)
368 (semantic-new-buffer-fcn))
370 (semantic-fetch-tags)
372 (semantic-find-first-tag-by-name "mode" (current-buffer)))
375 (semantic-find-first-tag-by-name "application" (current-buffer)))
378 (setq val
(car (semantic-tag-variable-default mode-tag
)))
379 (error "There should be a mode declaration in %s" file
))
381 (setq app
(car (semantic-tag-variable-default app-tag
))))
385 (srecode-map-update-app-file-entry srecode-current-map
389 (srecode-map-update-file-entry srecode-current-map
399 ;; We need to do this last since the setter needs the above code.
401 (defun srecode-map-load-path-set (sym val
)
402 "Set SYM to the new VAL, then update the srecode map."
403 (set-default sym val
)
404 (srecode-map-update-map t
))
406 (defcustom srecode-map-load-path
407 (list (srecode-map-base-template-dir)
408 (expand-file-name "~/.srecode/")
410 "Global load path for SRecode template files."
413 :set
'srecode-map-load-path-set
)
415 (provide 'srecode
/map
)
418 ;; generated-autoload-file: "loaddefs.el"
419 ;; generated-autoload-load-name: "srecode/map"
422 ;;; srecode/map.el ends here