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