Mercurial > hg > xemacs-beta
comparison lisp/hm--html-menus/tmpl-minor-mode.el @ 28:1917ad0d78d7 r19-15b97
Import from CVS: tag r19-15b97
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:51:55 +0200 |
parents | 8fc7fe29b841 |
children | 8b8b7f3559a2 |
comparison
equal
deleted
inserted
replaced
27:0a3286277d9b | 28:1917ad0d78d7 |
---|---|
1 (binary file application/octet-stream, hash: 2c37aaa09d20e95cbe60b1f40fbc1c8500bfb1bf) | 1 ;;; tmpl-minor-mode.el --- Template Minor Mode |
2 ;;; | |
3 ;;; $Id: tmpl-minor-mode.el,v 1.3 1997/03/04 08:44:56 steve Exp $ | |
4 ;;; | |
5 ;;; Copyright (C) 1993 - 1997 Heiko Muenkel | |
6 ;;; email: muenkel@tnt.uni-hannover.de | |
7 ;;; | |
8 ;;; Keywords: data tools | |
9 ;;; | |
10 ;;; This program is free software; you can redistribute it and/or modify | |
11 ;;; it under the terms of the GNU General Public License as published by | |
12 ;;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;;; any later version. | |
14 ;;; | |
15 ;;; This program is distributed in the hope that it will be useful, | |
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;;; GNU General Public License for more details. | |
19 ;;; | |
20 ;;; You should have received a copy of the GNU General Public License | |
21 ;;; along with this program; if not, write to the Free Software | |
22 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 ;;; | |
24 ;;; | |
25 ;;; Commentary: | |
26 ;;; | |
27 ;;; This file contains functions to expand templates. | |
28 ;;; Look at the file templates-syntax.doc for the syntax of the | |
29 ;;; templates. | |
30 ;;; There are the following 2 interactive functions to expand | |
31 ;;; templates: | |
32 ;;; tmpl-expand-templates-in-region | |
33 ;;; tmpl-expand-templates-in-buffer | |
34 ;;; The following two interactive functions are to escape the | |
35 ;;; unescaped special template signs: | |
36 ;;; tmpl-escape-tmpl-sign-in-region | |
37 ;;; tmpl-escape-tmpl-sign-in-buffer | |
38 ;;; The following function ask for a name of a template file, inserts | |
39 ;;; the template file and expands the templates: | |
40 ;;; tmpl-insert-template-file | |
41 ;;; If you want to use keystrokes to call the above functions, you must | |
42 ;;; switch the minor mode tmpl-mode on with `tmpl-minor-mode'. After | |
43 ;;; that, the following keys are defined: | |
44 ;;; `C-c x' = tmpl-expand-templates-in-region | |
45 ;;; `C-c C-x' = tmpl-expand-templates-in-buffer | |
46 ;;; `C-c ESC' = tmpl-escape-tmpl-sign-in-region | |
47 ;;; `C-c C-ESC' = tmpl-escape-tmpl-sign-in-buffer | |
48 ;;; Type again `M-x tmpl-minor-mode' to switch the template minor mode off. | |
49 ;;; | |
50 ;;; This file needs also the file adapt.el ! | |
51 ;;; | |
52 ;;; Installation: | |
53 ;;; | |
54 ;;; Put this file in one of your lisp directories and the following | |
55 ;;; lisp command in your .emacs: | |
56 ;;; (load-library "templates") | |
57 ;;; | |
58 | |
59 (require 'adapt) | |
60 | |
61 | |
62 (defvar tmpl-template-dir-list nil | |
63 "*A list of directories with the template files. | |
64 If it is nil, then the default-directory is used. | |
65 If more the one directories are given, then the | |
66 template filenames should differ in all directories. | |
67 | |
68 This variable is used in the commands for inserting templates. | |
69 Look at `tmpl-insert-template-file-from-fixed-dirs' and | |
70 at `tmpl-insert-template-file'. The command `tmpl-insert-template-file' | |
71 uses only the car of the list (if it is a list).") | |
72 | |
73 (defvar tmpl-automatic-expand t | |
74 "*An inserted template will be automaticly expanded, if this is t. | |
75 | |
76 This variable is used in the commands for inserting templates. | |
77 Look at `tmpl-insert-template-file-from-fixed-dirs' and | |
78 at `tmpl-insert-template-file'.") | |
79 | |
80 (defvar tmpl-filter-regexp ".*\\.tmpl$" | |
81 "*Regexp for filtering out non template files in a directory. | |
82 It is used in `tmpl-insert-template-file-from-fixed-dirs' to allow | |
83 only the selecting of files, which are matching the regexp. | |
84 If it is nil, then the Filter \".*\\.tmpl$\" is used. | |
85 Set it to \".*\" if you want to disable the filter function or | |
86 use the command `tmpl-insert-template-file'.") | |
87 | |
88 (defvar tmpl-history-variable-name 'tmpl-history-variable | |
89 "The name of the history variable. | |
90 The history variable is used by the commands `tmpl-insert-template-file' | |
91 and `tmpl-insert-template-file-from-fixed-dirs'.") | |
92 | |
93 (defvar tmpl-history-variable nil | |
94 "The history variable. See also `tmpl-history-variable-name'.") | |
95 | |
96 (defvar tmpl-sign "\000" "Sign which marks a template expression.") | |
97 | |
98 | |
99 (defvar tmpl-name-lisp "LISP" "Name of the lisp templates.") | |
100 | |
101 | |
102 (defvar tmpl-name-command "COMMAND" "Name of the emacs command templates.") | |
103 | |
104 | |
105 (defvar tmpl-name-comment "C" "Name of a comment template.") | |
106 | |
107 | |
108 (defvar tmpl-attribute-delete-line 'DELETE-LINE | |
109 "Attribute name of the attribute `delete-line`.") | |
110 | |
111 | |
112 (defvar tmpl-attribute-dont-delete 'DONT-DELETE | |
113 "Attribute name of the attribute `dont-delete`.") | |
114 | |
115 | |
116 (defvar tmpl-end-template "END" "End of a template.") | |
117 | |
118 | |
119 (defvar tmpl-white-spaces " | |
120 | |
121 " "String with white spaces.") | |
122 | |
123 | |
124 (defmacro tmpl-save-excursion (&rest body) | |
125 "Put `save-excursion' and `save-window-excursion' around the body." | |
126 (`(save-excursion | |
127 (, (cons 'save-window-excursion | |
128 body))))) | |
129 | |
130 | |
131 (defun tmpl-current-line () | |
132 "Returns the current line number." | |
133 (save-restriction | |
134 (widen) | |
135 (save-excursion | |
136 (beginning-of-line) | |
137 (1+ (count-lines 1 (point)))))) | |
138 | |
139 | |
140 ;(defun mapcar* (f &rest args) | |
141 ; "Apply FUNCTION to successive cars of all ARGS, until one ends. | |
142 ;Return the list of results." | |
143 ; (if (not (memq 'nil args)) ; If no list is exhausted, | |
144 ; (cons (apply f (mapcar 'car args)) ; Apply function to CARs. | |
145 ; (apply 'mapcar* f ; Recurse for rest of elements. | |
146 ; (mapcar 'cdr args))))) | |
147 ; | |
148 ;(defmacro tmpl-error (&rest args) | |
149 ; "Widen the buffer and signal an error. | |
150 ;Making error message by passing all args to `error', | |
151 ;which passes all args to format." | |
152 ; (widen) | |
153 ; (error args)) | |
154 | |
155 | |
156 (defun tmpl-search-next-template-sign (&optional dont-unescape) | |
157 "Search the next template sign after the current point. | |
158 It returns t, if a template is found and nil otherwise. | |
159 If DONT-UNESCAPE is t, then the escaped template signs are not unescaped." | |
160 (if (search-forward tmpl-sign nil t) | |
161 (if (or (eq (point) (point-max)) | |
162 (not (string= tmpl-sign | |
163 (buffer-substring (point) (+ (length tmpl-sign) | |
164 (point)))))) | |
165 t | |
166 (if (not dont-unescape) | |
167 (delete-char (length tmpl-sign)) | |
168 (forward-char)) | |
169 (tmpl-search-next-template-sign dont-unescape)))) | |
170 | |
171 | |
172 (defun tmpl-get-template-tag () | |
173 "Return a string with the template tag. | |
174 That is the string from the current point to the next `tmpl-sign', | |
175 without the tmpl-sign. The point is set after the `tmpl-sign'." | |
176 (let ((template-start (point))) | |
177 (if (tmpl-search-next-template-sign) | |
178 (buffer-substring template-start (- (point) (length tmpl-sign))) | |
179 nil))) | |
180 | |
181 | |
182 (defun tmpl-get-template-name (template-string) | |
183 "Returns the name of the template in the TEMPLATE-STRING." | |
184 (let* ((start (string-match (concat "[^" | |
185 tmpl-white-spaces | |
186 "]") | |
187 template-string)) | |
188 (end (string-match (concat "[" | |
189 tmpl-white-spaces | |
190 "]") | |
191 template-string start))) | |
192 (if end | |
193 (substring template-string start end) | |
194 (substring template-string start)))) | |
195 | |
196 | |
197 (defun tmpl-get-template-attribute-list (template-string) | |
198 "Returns the attribute list (as a lisp list) from the template-string." | |
199 (let* ((start (string-match (concat "[^" | |
200 tmpl-white-spaces | |
201 "]") | |
202 template-string))) | |
203 (setq start (string-match (concat "[" | |
204 tmpl-white-spaces | |
205 "]") | |
206 template-string start)) | |
207 (if start | |
208 (car (read-from-string template-string start)) | |
209 nil))) | |
210 | |
211 | |
212 (defun template-delete-template (begin-of-template template-attribute-list) | |
213 "Delete the current template from BEGIN-OF-TEMPLATE to the current point." | |
214 (tmpl-save-excursion | |
215 (if (or (not (assoc tmpl-attribute-dont-delete template-attribute-list)) | |
216 (not (car (cdr (assoc tmpl-attribute-dont-delete | |
217 template-attribute-list))))) | |
218 (if (and (assoc tmpl-attribute-delete-line template-attribute-list) | |
219 (car (cdr (assoc tmpl-attribute-delete-line | |
220 template-attribute-list)))) | |
221 (let ((end-of-template (point)) | |
222 (diff 1)) | |
223 (skip-chars-forward " \t") ; Skip blanks and tabs | |
224 (if (string= "\n" (buffer-substring (point) (1+ (point)))) | |
225 (progn | |
226 (setq diff 0) ; don't delete the linefeed at the beginnig | |
227 (setq end-of-template (1+ (point))))) | |
228 (goto-char begin-of-template) | |
229 (skip-chars-backward " \t") ; Skip blanks and tabs | |
230 (if (eq (point) (point-min)) | |
231 (delete-region (point) end-of-template) | |
232 (if (string= "\n" (buffer-substring (1- (point)) (point))) | |
233 (delete-region (- (point) diff) end-of-template) | |
234 (delete-region begin-of-template end-of-template)))) | |
235 (delete-region begin-of-template (point)))))) | |
236 | |
237 | |
238 (defun tmpl-expand-comment-template (begin-of-template template-attribute-list) | |
239 "Expand the comment template, which starts at the point BEGIN-OF-TEMPLATE. | |
240 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template." | |
241 (end-of-line) | |
242 (template-delete-template begin-of-template template-attribute-list)) | |
243 ; (tmpl-save-excursion | |
244 ; (if (or (not (assoc tmpl-attribute-dont-delete template-attribute-list)) | |
245 ; (not (car (cdr (assoc tmpl-attribute-dont-delete | |
246 ; template-attribute-list))))) | |
247 ; (if (and (assoc tmpl-attribute-delete-line template-attribute-list) | |
248 ; (car (cdr (assoc tmpl-attribute-delete-line | |
249 ; template-attribute-list)))) | |
250 ; ;; Delete the whole line | |
251 ; (let ((end-of-region (progn (end-of-line) (point))) | |
252 ; (start-of-region begin-of-template)) ; ausgetauscht | |
253 ; (delete-region start-of-region end-of-region) | |
254 ; (delete-char 1)) | |
255 ; ;; Delete only the comment | |
256 ; (let ((end-of-region (progn | |
257 ; (end-of-line) | |
258 ; (point))) | |
259 ; (start-of-region (progn (goto-char begin-of-template) | |
260 ; (point)))) | |
261 ; (delete-region start-of-region end-of-region)))))) | |
262 | |
263 | |
264 (defun tmpl-get-template-argument () | |
265 "Return the Text between a start tag and the end tag as symbol. | |
266 The point must be after the `templ-sign' of the start tag. | |
267 After this function has returned, the point is after the | |
268 first `templ-sign' of the end tag." | |
269 (let ((start-of-argument-text (progn (skip-chars-forward tmpl-white-spaces) | |
270 (point)))) | |
271 (if (tmpl-search-next-template-sign) | |
272 (car (read-from-string (buffer-substring start-of-argument-text | |
273 (- (point) | |
274 (length tmpl-sign))))) | |
275 (widen) | |
276 (error "Error Before Line %d: First Template Sign Of End Tag Missing !" | |
277 (tmpl-current-line))))) | |
278 | |
279 | |
280 (defun tmpl-make-list-of-words-from-string (string) | |
281 "Return a list of words which occur in the string." | |
282 (cond ((or (not (stringp string)) (string= "" string)) | |
283 ()) | |
284 (t (let* ((end-of-first-word (string-match | |
285 (concat "[" | |
286 tmpl-white-spaces | |
287 "]") | |
288 string)) | |
289 (rest-of-string (substring string (1+ | |
290 (or | |
291 end-of-first-word | |
292 (1- (length string))))))) | |
293 (cons (substring string 0 end-of-first-word) | |
294 (tmpl-make-list-of-words-from-string | |
295 (substring rest-of-string (or | |
296 (string-match | |
297 (concat "[^" | |
298 tmpl-white-spaces | |
299 "]") | |
300 rest-of-string) | |
301 0)))))))) | |
302 | |
303 | |
304 (defun tmpl-get-template-end-tag () | |
305 "Return a list with the elements of the following end tag. | |
306 The point must be after the first `templ-sign' of the end tag. | |
307 After this function has returned, the point is after the | |
308 last `templ-sign' of the end tag." | |
309 (let* ((start-point (progn (skip-chars-forward tmpl-white-spaces) | |
310 (point))) | |
311 (end-tag-string (if (tmpl-search-next-template-sign) | |
312 (buffer-substring start-point | |
313 (- (point) (length tmpl-sign))) | |
314 (widen) | |
315 (error "Error Before Line %d: Last Template Sign Of End Tag Missing !" | |
316 (tmpl-current-line))))) | |
317 (tmpl-make-list-of-words-from-string end-tag-string) | |
318 )) | |
319 | |
320 | |
321 (defun tmpl-expand-command-template (begin-of-template template-attribute-list) | |
322 "Expand the command template, which starts at the point BEGIN-OF-TEMPLATE. | |
323 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template." | |
324 (let ((template-argument (tmpl-get-template-argument)) | |
325 (template-end-tag (tmpl-get-template-end-tag))) | |
326 (if (equal (list tmpl-end-template tmpl-name-command) | |
327 template-end-tag) | |
328 (tmpl-save-excursion | |
329 (save-restriction | |
330 (widen) | |
331 (template-delete-template begin-of-template | |
332 template-attribute-list) | |
333 (command-execute template-argument))) | |
334 (widen) | |
335 (error "ERROR in Line %d: Wrong Template Command End Tag" | |
336 (tmpl-current-line))))) | |
337 | |
338 | |
339 (defun tmpl-expand-lisp-template (begin-of-template template-attribute-list) | |
340 "Expand the lisp template, which starts at the point BEGIN-OF-TEMPLATE. | |
341 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template." | |
342 (let ((template-argument (tmpl-get-template-argument)) | |
343 (template-end-tag (tmpl-get-template-end-tag))) | |
344 (if (equal (list tmpl-end-template tmpl-name-lisp) | |
345 template-end-tag) | |
346 (tmpl-save-excursion | |
347 (save-restriction | |
348 (widen) | |
349 (template-delete-template begin-of-template | |
350 template-attribute-list) | |
351 (eval template-argument))) | |
352 (widen) | |
353 (error "ERROR in Line %d: Wrong Template Lisp End Tag" | |
354 (tmpl-current-line))))) | |
355 | |
356 | |
357 (defun tmpl-expand-template-at-point () | |
358 "Expand the template at the current point. | |
359 The point must be after the sign ^@." | |
360 (let ((begin-of-template (- (point) (length tmpl-sign))) | |
361 (template-tag (tmpl-get-template-tag))) | |
362 (if (not template-tag) | |
363 (progn | |
364 (widen) | |
365 (error "ERROR In Line %d: End Sign Of Template Tag Missing !" | |
366 (tmpl-current-line))) | |
367 (let ((template-name (tmpl-get-template-name template-tag)) | |
368 (template-attribute-list (tmpl-get-template-attribute-list | |
369 template-tag))) | |
370 (cond ((not template-name) | |
371 (widen) | |
372 (error "ERROR In Line %d: No Template Name" | |
373 (tmpl-current-line))) | |
374 ((string= tmpl-name-comment template-name) | |
375 ;; comment template found | |
376 (tmpl-expand-comment-template begin-of-template | |
377 template-attribute-list)) | |
378 ((string= tmpl-name-command template-name) | |
379 ;; command template found | |
380 (tmpl-expand-command-template begin-of-template | |
381 template-attribute-list)) | |
382 ((string= tmpl-name-lisp template-name) | |
383 ;; lisp template found | |
384 (tmpl-expand-lisp-template begin-of-template | |
385 template-attribute-list)) | |
386 (t (widen) | |
387 (error "ERROR In Line %d: Wrong Template Name (%s) !" | |
388 (tmpl-current-line) template-name))))))) | |
389 | |
390 ;;;###autoload | |
391 (defun tmpl-expand-templates-in-region (&optional begin end) | |
392 "Expand the templates in the region from BEGIN to END. | |
393 If BEGIN and and are nil, then the current region is used." | |
394 (interactive) | |
395 (tmpl-save-excursion | |
396 (narrow-to-region (or begin (region-beginning)) | |
397 (or end (region-end))) | |
398 (goto-char (point-min)) | |
399 (while (tmpl-search-next-template-sign) | |
400 (tmpl-expand-template-at-point)) | |
401 (widen))) | |
402 | |
403 | |
404 ;;;###autoload | |
405 (defun tmpl-expand-templates-in-buffer () | |
406 "Expand all templates in the current buffer." | |
407 (interactive) | |
408 (tmpl-expand-templates-in-region (point-min) (point-max))) | |
409 | |
410 | |
411 (defun tmpl-escape-tmpl-sign-in-region (&optional begin end) | |
412 "Escape all `tmpl-sign' with a `tmpl-sign' in the region from BEGIN to END. | |
413 If BEGIN and END are nil, then the active region between mark and point is | |
414 used." | |
415 (interactive) | |
416 (save-excursion | |
417 (narrow-to-region (or begin (region-beginning)) | |
418 (or end (region-end))) | |
419 (goto-char (point-min)) | |
420 (while (tmpl-search-next-template-sign t) | |
421 (insert tmpl-sign)) | |
422 (widen))) | |
423 | |
424 | |
425 (defun tmpl-escape-tmpl-sign-in-buffer () | |
426 "Escape all `tmpl-sign' with a `tmpl-sign' in the buffer." | |
427 (interactive) | |
428 (tmpl-escape-tmpl-sign-in-region (point-min) (point-max))) | |
429 | |
430 (defun tmpl-get-table-with-template-files (template-dirs filter-regexp) | |
431 "Returns a table (alist) with all filenames matching the FILTER-REGEXP. | |
432 It searches the files in in all directories of the list TEMPLATE-DIRS. | |
433 The alist looks like: | |
434 '((file-name . file-name-with-path) ...)" | |
435 (cond ((not template-dirs) '()) | |
436 ((file-accessible-directory-p (car template-dirs)) | |
437 (append (mapcar '(lambda (file) | |
438 (cons (file-name-nondirectory file) file)) | |
439 (directory-files (car template-dirs) | |
440 t | |
441 filter-regexp | |
442 nil | |
443 t)) | |
444 (tmpl-get-table-with-template-files (cdr template-dirs) | |
445 filter-regexp))) | |
446 (t (tmpl-get-table-with-template-files (cdr template-dirs) | |
447 filter-regexp)))) | |
448 | |
449 (defun tmpl-insert-change-dir-entry (table) | |
450 "Insert the entry '(\"Change the directory\" . select-other-directory)." | |
451 (cons '("Change the directory" . select-other-directory) | |
452 table)) | |
453 | |
454 (defun tmpl-read-template-directory (prompt default-direcory) | |
455 "Reads a template directory. | |
456 If the directory isn't already in `tmpl-template-dirs', the it asks, | |
457 if it should be added to it." | |
458 (let ((directory (expand-file-name | |
459 (read-directory-name prompt | |
460 (or default-direcory | |
461 (default-directory)))))) | |
462 (when (and (not (member* directory tmpl-template-dir-list :test 'string=)) | |
463 (y-or-n-p | |
464 (format | |
465 "Add %s permanent to the template directory list? " | |
466 directory))) | |
467 (setq tmpl-template-dir-list (cons directory tmpl-template-dir-list))) | |
468 directory)) | |
469 | |
470 (defun tmpl-read-template-filename (&optional | |
471 template-dirs | |
472 filter-regexp | |
473 history-variable) | |
474 "Reads interactive the name of a template file. | |
475 The TEMPLATE-DIRS is a list of directories with template files. | |
476 Note: The template filenames should differ in all directories. | |
477 If it is nil, then the default-directory is used. | |
478 FILTER-REGEXP can be used to allow only the selecting of files, | |
479 which are matching the regexp. If FILTER-REGEXP is nil, then the | |
480 Filter \".*\\.tmpl$\" is used. | |
481 HISTROY-VARIABLE contains the last template file names." | |
482 (let ((filter (or filter-regexp ".*\\.tmpl$")) | |
483 (directories (or template-dirs (list (default-directory)))) | |
484 (table nil) | |
485 (file nil) | |
486 (answer nil) | |
487 (start nil) | |
488 (anser-not-ok t) | |
489 (internal-history (mapcar '(lambda (path) | |
490 (file-name-nondirectory path)) | |
491 (eval history-variable)))) | |
492 (while anser-not-ok | |
493 (setq table (tmpl-get-table-with-template-files directories filter)) | |
494 (while (not table) | |
495 ;; Try another filter (all files) | |
496 (setq table (tmpl-get-table-with-template-files directories ".*")) | |
497 (unless table | |
498 (setq directories (list (tmpl-read-template-directory | |
499 "No files found, try another directory: " | |
500 (car directories)))))) | |
501 (setq table (tmpl-insert-change-dir-entry table)) | |
502 (setq answer (completing-read "Templatefile: " | |
503 table | |
504 nil | |
505 t | |
506 nil | |
507 'internal-history)) | |
508 (setq file (cdr (assoc* answer table :test 'string=))) | |
509 (setq anser-not-ok (equal file 'select-other-directory)) | |
510 (when anser-not-ok | |
511 (setq directories (list (tmpl-read-template-directory | |
512 "Directory with Templatefiles: " | |
513 (car directories)))))) | |
514 (unless (or (not history-variable) | |
515 ; (string= answer (car internal-history))) | |
516 (string= file (car (eval history-variable)))) | |
517 (set history-variable (cons file (eval history-variable)))) | |
518 file)) | |
519 | |
520 | |
521 | |
522 ;;;###autoload | |
523 (defun tmpl-insert-template-file-from-fixed-dirs (file) | |
524 "Inserts a template FILE and expands it, if `tmpl-automatic-expand' is t. | |
525 This command tries to read the template file from a list of | |
526 predefined directries (look at `tmpl-template-dir-list') and it filters | |
527 the contents of this directories with the regular expression | |
528 `tmpl-filter-regexp' (look also at this variable). | |
529 The command uses a history variable, which could be changed with the | |
530 variable `tmpl-history-variable-name'. | |
531 | |
532 The user of the command is able to change interactive to another | |
533 directory by entering at first the string \"Change the directory\". | |
534 This maybe to difficult for the user. Therefore another command | |
535 called `tmpl-insert-template-file' exist, which doesn't use fixed | |
536 directories and filters." | |
537 (interactive | |
538 (list (tmpl-read-template-filename tmpl-template-dir-list | |
539 tmpl-filter-regexp | |
540 tmpl-history-variable-name))) | |
541 (insert-file (expand-file-name file)) | |
542 (if tmpl-automatic-expand | |
543 (tmpl-expand-templates-in-region (point) (mark t))) | |
544 file) | |
545 | |
546 | |
547 ;;;###autoload | |
548 (defun tmpl-insert-template-file (file) | |
549 "Insert a template FILE and expand it, if `tmpl-automatic-expand' is t. | |
550 Look also at `tmpl-template-dir-list', to specify a default template directory. | |
551 You should also take a look at `tmpl-insert-template-file-from-fixed-dirs' | |
552 which has additional advantages (and disadvantages :-). | |
553 | |
554 ATTENTION: The interface of this function has changed. The old | |
555 function had the argument list (&optional TEMPLATE-DIR AUTOMATIC-EXPAND). | |
556 The variables `tmpl-template-dir-list' and `tmpl-automatic-expand' must | |
557 now be used instead of the args TEMPLATE-DIR and AUTOMATIC-EXPAND." | |
558 (interactive | |
559 (list | |
560 (let* ((default-directory (or (car tmpl-template-dir-list) | |
561 default-directory)) | |
562 (filename (read-file-name "Templatefile: " | |
563 (if (listp tmpl-template-dir-list) | |
564 (car tmpl-template-dir-list)) | |
565 nil | |
566 t | |
567 nil | |
568 tmpl-history-variable-name)) | |
569 (directory (expand-file-name (file-name-directory filename)))) | |
570 (when (and (not (member* directory | |
571 tmpl-template-dir-list | |
572 :test 'string=)) | |
573 (y-or-n-p | |
574 (format | |
575 "Add %s permanent to the template directory list? " | |
576 directory))) | |
577 (setq tmpl-template-dir-list (cons directory tmpl-template-dir-list))) | |
578 filename))) | |
579 (insert-file (expand-file-name file)) | |
580 (if tmpl-automatic-expand | |
581 (tmpl-expand-templates-in-buffer)) | |
582 file) | |
583 | |
584 ;(defun tmpl-insert-template-file (&optional template-dir automatic-expand) | |
585 ; "Insert a template file and expand it, if AUTOMATIC-EXPAND is t. | |
586 ;The TEMPLATE-DIR is the directory with the template files." | |
587 ; (interactive) | |
588 ; (insert-file | |
589 ; (expand-file-name | |
590 ; (read-file-name "Templatefile: " | |
591 ; template-dir | |
592 ; nil | |
593 ; t))) | |
594 ; (if automatic-expand | |
595 ; (tmpl-expand-templates-in-buffer))) | |
596 | |
597 | |
598 ;;; Definition of the minor mode tmpl | |
599 | |
600 (defvar tmpl-minor-mode nil | |
601 "*t, if the minor mode tmpl-mode is on and nil otherwise.") | |
602 | |
603 | |
604 (make-variable-buffer-local 'tmpl-minor-mode) | |
605 ;(set-default 'tmpl-minor-mode nil) | |
606 | |
607 | |
608 (defvar tmpl-old-local-map nil | |
609 "Local keymap, before the minor-mode tmpl was switched on.") | |
610 | |
611 | |
612 (make-variable-buffer-local 'tmpl-old-local-map) | |
613 | |
614 | |
615 | |
616 (defvar tmpl-minor-mode-map nil | |
617 "*The keymap for the minor mode tmpl-mode.") | |
618 | |
619 | |
620 (make-variable-buffer-local 'tmpl-minor-mode-map) | |
621 | |
622 | |
623 (if (adapt-xemacsp) | |
624 (defun tmpl-define-minor-mode-keymap () | |
625 "Defines the minor mode keymap." | |
626 (define-key tmpl-minor-mode-map [(control c) x] | |
627 'tmpl-expand-templates-in-region) | |
628 (define-key tmpl-minor-mode-map [(control c) (control x)] | |
629 'tmpl-expand-templates-in-buffer) | |
630 (define-key tmpl-minor-mode-map [(control c) escape] | |
631 'tmpl-escape-tmpl-sign-in-region) | |
632 (define-key tmpl-minor-mode-map [(control c) (control escape)] | |
633 'tmpl-escape-tmpl-sign-in-buffer)) | |
634 (defun tmpl-define-minor-mode-keymap () | |
635 "Defines the minor mode keymap." | |
636 (define-key tmpl-minor-mode-map [?\C-c ?x] | |
637 'tmpl-expand-templates-in-region) | |
638 (define-key tmpl-minor-mode-map [?\C-c ?\C-x] | |
639 'tmpl-expand-templates-in-buffer) | |
640 (define-key tmpl-minor-mode-map [?\C-c escape] | |
641 'tmpl-escape-tmpl-sign-in-region) | |
642 (define-key tmpl-minor-mode-map [?\C-c C-escape] | |
643 'tmpl-escape-tmpl-sign-in-buffer)) | |
644 ) | |
645 | |
646 | |
647 (defun tmpl-minor-mode () | |
648 "Toggle the minor mode tmpl-mode." | |
649 (interactive) | |
650 (if tmpl-minor-mode | |
651 (progn | |
652 (setq tmpl-minor-mode nil) | |
653 (use-local-map tmpl-old-local-map) | |
654 (setq tmpl-old-local-map nil)) | |
655 (setq tmpl-minor-mode t) | |
656 (setq tmpl-old-local-map (current-local-map)) | |
657 (if tmpl-old-local-map | |
658 (setq tmpl-minor-mode-map (copy-keymap tmpl-old-local-map)) | |
659 (setq tmpl-minor-mode-map nil) | |
660 (setq tmpl-minor-mode-map (make-keymap)) | |
661 (set-keymap-name tmpl-minor-mode-map 'minor-mode-map)) | |
662 (tmpl-define-minor-mode-keymap) | |
663 (use-local-map tmpl-minor-mode-map))) | |
664 | |
665 | |
666 (setq minor-mode-alist (cons '(tmpl-minor-mode " TMPL") minor-mode-alist)) | |
667 | |
668 | |
669 (provide 'tmpl-minor-mode) |