0
|
1 ;;; tmpl-minor-mode.el --- Template Minor Mode
|
|
2 ;;;
|
42
|
3 ;;; $Id: tmpl-minor-mode.el,v 1.4 1997/03/24 01:26:54 steve Exp $
|
2
|
4 ;;;
|
22
|
5 ;;; Copyright (C) 1993 - 1997 Heiko Muenkel
|
0
|
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
|
22
|
12 ;;; the Free Software Foundation; either version 2, or (at your option)
|
0
|
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
|
2
|
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
|
0
|
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
|
22
|
62 (defvar tmpl-template-dir-list nil
|
|
63 "*A list of directories with the template files.
|
42
|
64 If it is nil, then the default-directory will be used.
|
|
65 If more than one directory is given, then the
|
22
|
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
|
28
|
96 (defvar tmpl-sign "\000" "Sign which marks a template expression.")
|
0
|
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 "
|
2
|
120
" "String with white spaces.")
|
0
|
121
|
|
122
|
|
123 (defmacro tmpl-save-excursion (&rest body)
|
|
124 "Put `save-excursion' and `save-window-excursion' around the body."
|
|
125 (`(save-excursion
|
|
126 (, (cons 'save-window-excursion
|
|
127 body)))))
|
|
128
|
|
129
|
|
130 (defun tmpl-current-line ()
|
|
131 "Returns the current line number."
|
|
132 (save-restriction
|
|
133 (widen)
|
|
134 (save-excursion
|
|
135 (beginning-of-line)
|
|
136 (1+ (count-lines 1 (point))))))
|
|
137
|
|
138
|
|
139 ;(defun mapcar* (f &rest args)
|
|
140 ; "Apply FUNCTION to successive cars of all ARGS, until one ends.
|
|
141 ;Return the list of results."
|
|
142 ; (if (not (memq 'nil args)) ; If no list is exhausted,
|
|
143 ; (cons (apply f (mapcar 'car args)) ; Apply function to CARs.
|
|
144 ; (apply 'mapcar* f ; Recurse for rest of elements.
|
|
145 ; (mapcar 'cdr args)))))
|
|
146 ;
|
|
147 ;(defmacro tmpl-error (&rest args)
|
|
148 ; "Widen the buffer and signal an error.
|
|
149 ;Making error message by passing all args to `error',
|
|
150 ;which passes all args to format."
|
|
151 ; (widen)
|
|
152 ; (error args))
|
|
153
|
|
154
|
|
155 (defun tmpl-search-next-template-sign (&optional dont-unescape)
|
|
156 "Search the next template sign after the current point.
|
|
157 It returns t, if a template is found and nil otherwise.
|
|
158 If DONT-UNESCAPE is t, then the escaped template signs are not unescaped."
|
|
159 (if (search-forward tmpl-sign nil t)
|
|
160 (if (or (eq (point) (point-max))
|
|
161 (not (string= tmpl-sign
|
|
162 (buffer-substring (point) (+ (length tmpl-sign)
|
|
163 (point))))))
|
|
164 t
|
|
165 (if (not dont-unescape)
|
|
166 (delete-char (length tmpl-sign))
|
|
167 (forward-char))
|
|
168 (tmpl-search-next-template-sign dont-unescape))))
|
|
169
|
|
170
|
|
171 (defun tmpl-get-template-tag ()
|
|
172 "Return a string with the template tag.
|
|
173 That is the string from the current point to the next `tmpl-sign',
|
|
174 without the tmpl-sign. The point is set after the `tmpl-sign'."
|
|
175 (let ((template-start (point)))
|
|
176 (if (tmpl-search-next-template-sign)
|
|
177 (buffer-substring template-start (- (point) (length tmpl-sign)))
|
|
178 nil)))
|
|
179
|
|
180
|
|
181 (defun tmpl-get-template-name (template-string)
|
|
182 "Returns the name of the template in the TEMPLATE-STRING."
|
|
183 (let* ((start (string-match (concat "[^"
|
|
184 tmpl-white-spaces
|
|
185 "]")
|
|
186 template-string))
|
|
187 (end (string-match (concat "["
|
|
188 tmpl-white-spaces
|
|
189 "]")
|
|
190 template-string start)))
|
|
191 (if end
|
|
192 (substring template-string start end)
|
|
193 (substring template-string start))))
|
|
194
|
|
195
|
|
196 (defun tmpl-get-template-attribute-list (template-string)
|
|
197 "Returns the attribute list (as a lisp list) from the template-string."
|
|
198 (let* ((start (string-match (concat "[^"
|
|
199 tmpl-white-spaces
|
|
200 "]")
|
|
201 template-string)))
|
|
202 (setq start (string-match (concat "["
|
|
203 tmpl-white-spaces
|
|
204 "]")
|
|
205 template-string start))
|
|
206 (if start
|
|
207 (car (read-from-string template-string start))
|
|
208 nil)))
|
|
209
|
|
210
|
|
211 (defun template-delete-template (begin-of-template template-attribute-list)
|
|
212 "Delete the current template from BEGIN-OF-TEMPLATE to the current point."
|
|
213 (tmpl-save-excursion
|
|
214 (if (or (not (assoc tmpl-attribute-dont-delete template-attribute-list))
|
|
215 (not (car (cdr (assoc tmpl-attribute-dont-delete
|
|
216 template-attribute-list)))))
|
|
217 (if (and (assoc tmpl-attribute-delete-line template-attribute-list)
|
|
218 (car (cdr (assoc tmpl-attribute-delete-line
|
|
219 template-attribute-list))))
|
|
220 (let ((end-of-template (point))
|
|
221 (diff 1))
|
|
222 (skip-chars-forward " \t") ; Skip blanks and tabs
|
|
223 (if (string= "\n" (buffer-substring (point) (1+ (point))))
|
|
224 (progn
|
|
225 (setq diff 0) ; don't delete the linefeed at the beginnig
|
|
226 (setq end-of-template (1+ (point)))))
|
|
227 (goto-char begin-of-template)
|
|
228 (skip-chars-backward " \t") ; Skip blanks and tabs
|
|
229 (if (eq (point) (point-min))
|
|
230 (delete-region (point) end-of-template)
|
|
231 (if (string= "\n" (buffer-substring (1- (point)) (point)))
|
|
232 (delete-region (- (point) diff) end-of-template)
|
|
233 (delete-region begin-of-template end-of-template))))
|
|
234 (delete-region begin-of-template (point))))))
|
|
235
|
|
236
|
|
237 (defun tmpl-expand-comment-template (begin-of-template template-attribute-list)
|
|
238 "Expand the comment template, which starts at the point BEGIN-OF-TEMPLATE.
|
|
239 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template."
|
|
240 (end-of-line)
|
|
241 (template-delete-template begin-of-template template-attribute-list))
|
|
242 ; (tmpl-save-excursion
|
|
243 ; (if (or (not (assoc tmpl-attribute-dont-delete template-attribute-list))
|
|
244 ; (not (car (cdr (assoc tmpl-attribute-dont-delete
|
|
245 ; template-attribute-list)))))
|
|
246 ; (if (and (assoc tmpl-attribute-delete-line template-attribute-list)
|
|
247 ; (car (cdr (assoc tmpl-attribute-delete-line
|
|
248 ; template-attribute-list))))
|
|
249 ; ;; Delete the whole line
|
|
250 ; (let ((end-of-region (progn (end-of-line) (point)))
|
|
251 ; (start-of-region begin-of-template)) ; ausgetauscht
|
|
252 ; (delete-region start-of-region end-of-region)
|
|
253 ; (delete-char 1))
|
|
254 ; ;; Delete only the comment
|
|
255 ; (let ((end-of-region (progn
|
|
256 ; (end-of-line)
|
|
257 ; (point)))
|
|
258 ; (start-of-region (progn (goto-char begin-of-template)
|
|
259 ; (point))))
|
|
260 ; (delete-region start-of-region end-of-region))))))
|
|
261
|
|
262
|
|
263 (defun tmpl-get-template-argument ()
|
|
264 "Return the Text between a start tag and the end tag as symbol.
|
|
265 The point must be after the `templ-sign' of the start tag.
|
|
266 After this function has returned, the point is after the
|
|
267 first `templ-sign' of the end tag."
|
|
268 (let ((start-of-argument-text (progn (skip-chars-forward tmpl-white-spaces)
|
|
269 (point))))
|
|
270 (if (tmpl-search-next-template-sign)
|
|
271 (car (read-from-string (buffer-substring start-of-argument-text
|
|
272 (- (point)
|
|
273 (length tmpl-sign)))))
|
|
274 (widen)
|
|
275 (error "Error Before Line %d: First Template Sign Of End Tag Missing !"
|
|
276 (tmpl-current-line)))))
|
|
277
|
|
278
|
|
279 (defun tmpl-make-list-of-words-from-string (string)
|
|
280 "Return a list of words which occur in the string."
|
|
281 (cond ((or (not (stringp string)) (string= "" string))
|
|
282 ())
|
|
283 (t (let* ((end-of-first-word (string-match
|
|
284 (concat "["
|
|
285 tmpl-white-spaces
|
|
286 "]")
|
|
287 string))
|
|
288 (rest-of-string (substring string (1+
|
|
289 (or
|
|
290 end-of-first-word
|
|
291 (1- (length string)))))))
|
|
292 (cons (substring string 0 end-of-first-word)
|
|
293 (tmpl-make-list-of-words-from-string
|
|
294 (substring rest-of-string (or
|
|
295 (string-match
|
|
296 (concat "[^"
|
|
297 tmpl-white-spaces
|
|
298 "]")
|
|
299 rest-of-string)
|
|
300 0))))))))
|
|
301
|
|
302
|
|
303 (defun tmpl-get-template-end-tag ()
|
|
304 "Return a list with the elements of the following end tag.
|
|
305 The point must be after the first `templ-sign' of the end tag.
|
|
306 After this function has returned, the point is after the
|
|
307 last `templ-sign' of the end tag."
|
|
308 (let* ((start-point (progn (skip-chars-forward tmpl-white-spaces)
|
|
309 (point)))
|
|
310 (end-tag-string (if (tmpl-search-next-template-sign)
|
|
311 (buffer-substring start-point
|
|
312 (- (point) (length tmpl-sign)))
|
|
313 (widen)
|
|
314 (error "Error Before Line %d: Last Template Sign Of End Tag Missing !"
|
|
315 (tmpl-current-line)))))
|
|
316 (tmpl-make-list-of-words-from-string end-tag-string)
|
|
317 ))
|
|
318
|
|
319
|
|
320 (defun tmpl-expand-command-template (begin-of-template template-attribute-list)
|
|
321 "Expand the command template, which starts at the point BEGIN-OF-TEMPLATE.
|
|
322 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template."
|
|
323 (let ((template-argument (tmpl-get-template-argument))
|
|
324 (template-end-tag (tmpl-get-template-end-tag)))
|
|
325 (if (equal (list tmpl-end-template tmpl-name-command)
|
|
326 template-end-tag)
|
|
327 (tmpl-save-excursion
|
|
328 (save-restriction
|
|
329 (widen)
|
|
330 (template-delete-template begin-of-template
|
|
331 template-attribute-list)
|
|
332 (command-execute template-argument)))
|
|
333 (widen)
|
|
334 (error "ERROR in Line %d: Wrong Template Command End Tag"
|
|
335 (tmpl-current-line)))))
|
|
336
|
|
337
|
|
338 (defun tmpl-expand-lisp-template (begin-of-template template-attribute-list)
|
|
339 "Expand the lisp template, which starts at the point BEGIN-OF-TEMPLATE.
|
|
340 TEMPLATE-ATTRIBUTE-LIST is the attribute list of the template."
|
|
341 (let ((template-argument (tmpl-get-template-argument))
|
|
342 (template-end-tag (tmpl-get-template-end-tag)))
|
|
343 (if (equal (list tmpl-end-template tmpl-name-lisp)
|
|
344 template-end-tag)
|
|
345 (tmpl-save-excursion
|
|
346 (save-restriction
|
|
347 (widen)
|
|
348 (template-delete-template begin-of-template
|
|
349 template-attribute-list)
|
|
350 (eval template-argument)))
|
|
351 (widen)
|
|
352 (error "ERROR in Line %d: Wrong Template Lisp End Tag"
|
|
353 (tmpl-current-line)))))
|
|
354
|
|
355
|
|
356 (defun tmpl-expand-template-at-point ()
|
|
357 "Expand the template at the current point.
|
|
358 The point must be after the sign ^@."
|
|
359 (let ((begin-of-template (- (point) (length tmpl-sign)))
|
|
360 (template-tag (tmpl-get-template-tag)))
|
|
361 (if (not template-tag)
|
|
362 (progn
|
|
363 (widen)
|
|
364 (error "ERROR In Line %d: End Sign Of Template Tag Missing !"
|
|
365 (tmpl-current-line)))
|
|
366 (let ((template-name (tmpl-get-template-name template-tag))
|
|
367 (template-attribute-list (tmpl-get-template-attribute-list
|
|
368 template-tag)))
|
|
369 (cond ((not template-name)
|
|
370 (widen)
|
|
371 (error "ERROR In Line %d: No Template Name"
|
|
372 (tmpl-current-line)))
|
|
373 ((string= tmpl-name-comment template-name)
|
|
374 ;; comment template found
|
|
375 (tmpl-expand-comment-template begin-of-template
|
|
376 template-attribute-list))
|
|
377 ((string= tmpl-name-command template-name)
|
|
378 ;; command template found
|
|
379 (tmpl-expand-command-template begin-of-template
|
|
380 template-attribute-list))
|
|
381 ((string= tmpl-name-lisp template-name)
|
|
382 ;; lisp template found
|
|
383 (tmpl-expand-lisp-template begin-of-template
|
|
384 template-attribute-list))
|
|
385 (t (widen)
|
|
386 (error "ERROR In Line %d: Wrong Template Name (%s) !"
|
|
387 (tmpl-current-line) template-name)))))))
|
|
388
|
22
|
389 ;;;###autoload
|
0
|
390 (defun tmpl-expand-templates-in-region (&optional begin end)
|
42
|
391 "Expands the templates in the region from BEGIN to END.
|
|
392 If BEGIN and END are nil, then the current region is used."
|
0
|
393 (interactive)
|
|
394 (tmpl-save-excursion
|
|
395 (narrow-to-region (or begin (region-beginning))
|
|
396 (or end (region-end)))
|
|
397 (goto-char (point-min))
|
|
398 (while (tmpl-search-next-template-sign)
|
|
399 (tmpl-expand-template-at-point))
|
|
400 (widen)))
|
|
401
|
|
402
|
22
|
403 ;;;###autoload
|
0
|
404 (defun tmpl-expand-templates-in-buffer ()
|
42
|
405 "Expands all templates in the current buffer."
|
0
|
406 (interactive)
|
|
407 (tmpl-expand-templates-in-region (point-min) (point-max)))
|
|
408
|
|
409
|
|
410 (defun tmpl-escape-tmpl-sign-in-region (&optional begin end)
|
42
|
411 "Escapes all `tmpl-sign' with a `tmpl-sign' in the region from BEGIN to END.
|
0
|
412 If BEGIN and END are nil, then the active region between mark and point is
|
|
413 used."
|
|
414 (interactive)
|
|
415 (save-excursion
|
|
416 (narrow-to-region (or begin (region-beginning))
|
|
417 (or end (region-end)))
|
|
418 (goto-char (point-min))
|
|
419 (while (tmpl-search-next-template-sign t)
|
|
420 (insert tmpl-sign))
|
|
421 (widen)))
|
|
422
|
|
423
|
|
424 (defun tmpl-escape-tmpl-sign-in-buffer ()
|
|
425 "Escape all `tmpl-sign' with a `tmpl-sign' in the buffer."
|
|
426 (interactive)
|
|
427 (tmpl-escape-tmpl-sign-in-region (point-min) (point-max)))
|
|
428
|
22
|
429 (defun tmpl-get-table-with-template-files (template-dirs filter-regexp)
|
|
430 "Returns a table (alist) with all filenames matching the FILTER-REGEXP.
|
|
431 It searches the files in in all directories of the list TEMPLATE-DIRS.
|
|
432 The alist looks like:
|
|
433 '((file-name . file-name-with-path) ...)"
|
|
434 (cond ((not template-dirs) '())
|
|
435 ((file-accessible-directory-p (car template-dirs))
|
|
436 (append (mapcar '(lambda (file)
|
|
437 (cons (file-name-nondirectory file) file))
|
|
438 (directory-files (car template-dirs)
|
|
439 t
|
|
440 filter-regexp
|
|
441 nil
|
|
442 t))
|
|
443 (tmpl-get-table-with-template-files (cdr template-dirs)
|
|
444 filter-regexp)))
|
|
445 (t (tmpl-get-table-with-template-files (cdr template-dirs)
|
|
446 filter-regexp))))
|
0
|
447
|
22
|
448 (defun tmpl-insert-change-dir-entry (table)
|
|
449 "Insert the entry '(\"Change the directory\" . select-other-directory)."
|
|
450 (cons '("Change the directory" . select-other-directory)
|
|
451 table))
|
|
452
|
|
453 (defun tmpl-read-template-directory (prompt default-direcory)
|
|
454 "Reads a template directory.
|
|
455 If the directory isn't already in `tmpl-template-dirs', the it asks,
|
|
456 if it should be added to it."
|
|
457 (let ((directory (expand-file-name
|
|
458 (read-directory-name prompt
|
|
459 (or default-direcory
|
|
460 (default-directory))))))
|
|
461 (when (and (not (member* directory tmpl-template-dir-list :test 'string=))
|
|
462 (y-or-n-p
|
|
463 (format
|
|
464 "Add %s permanent to the template directory list? "
|
|
465 directory)))
|
|
466 (setq tmpl-template-dir-list (cons directory tmpl-template-dir-list)))
|
|
467 directory))
|
|
468
|
|
469 (defun tmpl-read-template-filename (&optional
|
|
470 template-dirs
|
|
471 filter-regexp
|
|
472 history-variable)
|
|
473 "Reads interactive the name of a template file.
|
|
474 The TEMPLATE-DIRS is a list of directories with template files.
|
|
475 Note: The template filenames should differ in all directories.
|
|
476 If it is nil, then the default-directory is used.
|
|
477 FILTER-REGEXP can be used to allow only the selecting of files,
|
|
478 which are matching the regexp. If FILTER-REGEXP is nil, then the
|
|
479 Filter \".*\\.tmpl$\" is used.
|
|
480 HISTROY-VARIABLE contains the last template file names."
|
|
481 (let ((filter (or filter-regexp ".*\\.tmpl$"))
|
|
482 (directories (or template-dirs (list (default-directory))))
|
|
483 (table nil)
|
|
484 (file nil)
|
|
485 (answer nil)
|
|
486 (start nil)
|
|
487 (anser-not-ok t)
|
|
488 (internal-history (mapcar '(lambda (path)
|
|
489 (file-name-nondirectory path))
|
|
490 (eval history-variable))))
|
|
491 (while anser-not-ok
|
|
492 (setq table (tmpl-get-table-with-template-files directories filter))
|
|
493 (while (not table)
|
|
494 ;; Try another filter (all files)
|
|
495 (setq table (tmpl-get-table-with-template-files directories ".*"))
|
|
496 (unless table
|
|
497 (setq directories (list (tmpl-read-template-directory
|
|
498 "No files found, try another directory: "
|
|
499 (car directories))))))
|
|
500 (setq table (tmpl-insert-change-dir-entry table))
|
|
501 (setq answer (completing-read "Templatefile: "
|
|
502 table
|
|
503 nil
|
|
504 t
|
|
505 nil
|
|
506 'internal-history))
|
|
507 (setq file (cdr (assoc* answer table :test 'string=)))
|
|
508 (setq anser-not-ok (equal file 'select-other-directory))
|
|
509 (when anser-not-ok
|
|
510 (setq directories (list (tmpl-read-template-directory
|
|
511 "Directory with Templatefiles: "
|
|
512 (car directories))))))
|
|
513 (unless (or (not history-variable)
|
|
514 ; (string= answer (car internal-history)))
|
|
515 (string= file (car (eval history-variable))))
|
|
516 (set history-variable (cons file (eval history-variable))))
|
|
517 file))
|
|
518
|
|
519
|
|
520
|
|
521 ;;;###autoload
|
|
522 (defun tmpl-insert-template-file-from-fixed-dirs (file)
|
|
523 "Inserts a template FILE and expands it, if `tmpl-automatic-expand' is t.
|
|
524 This command tries to read the template file from a list of
|
42
|
525 predefined directories (look at `tmpl-template-dir-list') and it filters
|
|
526 the contents of these directories with the regular expression
|
22
|
527 `tmpl-filter-regexp' (look also at this variable).
|
|
528 The command uses a history variable, which could be changed with the
|
|
529 variable `tmpl-history-variable-name'.
|
|
530
|
42
|
531 The user of the command is able to change interactively to another
|
22
|
532 directory by entering at first the string \"Change the directory\".
|
42
|
533 This may be too difficult for the user. Therefore another command
|
22
|
534 called `tmpl-insert-template-file' exist, which doesn't use fixed
|
|
535 directories and filters."
|
|
536 (interactive
|
|
537 (list (tmpl-read-template-filename tmpl-template-dir-list
|
|
538 tmpl-filter-regexp
|
|
539 tmpl-history-variable-name)))
|
|
540 (insert-file (expand-file-name file))
|
|
541 (if tmpl-automatic-expand
|
|
542 (tmpl-expand-templates-in-region (point) (mark t)))
|
|
543 file)
|
|
544
|
|
545
|
|
546 ;;;###autoload
|
|
547 (defun tmpl-insert-template-file (file)
|
42
|
548 "Inserts a template FILE and expand it, if `tmpl-automatic-expand' is t.
|
22
|
549 Look also at `tmpl-template-dir-list', to specify a default template directory.
|
|
550 You should also take a look at `tmpl-insert-template-file-from-fixed-dirs'
|
|
551 which has additional advantages (and disadvantages :-).
|
|
552
|
|
553 ATTENTION: The interface of this function has changed. The old
|
|
554 function had the argument list (&optional TEMPLATE-DIR AUTOMATIC-EXPAND).
|
|
555 The variables `tmpl-template-dir-list' and `tmpl-automatic-expand' must
|
|
556 now be used instead of the args TEMPLATE-DIR and AUTOMATIC-EXPAND."
|
|
557 (interactive
|
|
558 (list
|
|
559 (let* ((default-directory (or (car tmpl-template-dir-list)
|
|
560 default-directory))
|
|
561 (filename (read-file-name "Templatefile: "
|
|
562 (if (listp tmpl-template-dir-list)
|
|
563 (car tmpl-template-dir-list))
|
|
564 nil
|
|
565 t
|
|
566 nil
|
|
567 tmpl-history-variable-name))
|
|
568 (directory (expand-file-name (file-name-directory filename))))
|
|
569 (when (and (not (member* directory
|
|
570 tmpl-template-dir-list
|
|
571 :test 'string=))
|
|
572 (y-or-n-p
|
|
573 (format
|
|
574 "Add %s permanent to the template directory list? "
|
|
575 directory)))
|
|
576 (setq tmpl-template-dir-list (cons directory tmpl-template-dir-list)))
|
|
577 filename)))
|
|
578 (insert-file (expand-file-name file))
|
|
579 (if tmpl-automatic-expand
|
|
580 (tmpl-expand-templates-in-buffer))
|
|
581 file)
|
|
582
|
|
583 ;(defun tmpl-insert-template-file (&optional template-dir automatic-expand)
|
|
584 ; "Insert a template file and expand it, if AUTOMATIC-EXPAND is t.
|
|
585 ;The TEMPLATE-DIR is the directory with the template files."
|
|
586 ; (interactive)
|
|
587 ; (insert-file
|
|
588 ; (expand-file-name
|
|
589 ; (read-file-name "Templatefile: "
|
|
590 ; template-dir
|
|
591 ; nil
|
|
592 ; t)))
|
|
593 ; (if automatic-expand
|
|
594 ; (tmpl-expand-templates-in-buffer)))
|
2
|
595
|
|
596
|
0
|
597 ;;; Definition of the minor mode tmpl
|
|
598
|
|
599 (defvar tmpl-minor-mode nil
|
|
600 "*t, if the minor mode tmpl-mode is on and nil otherwise.")
|
|
601
|
|
602
|
|
603 (make-variable-buffer-local 'tmpl-minor-mode)
|
|
604 ;(set-default 'tmpl-minor-mode nil)
|
|
605
|
|
606
|
|
607 (defvar tmpl-old-local-map nil
|
|
608 "Local keymap, before the minor-mode tmpl was switched on.")
|
|
609
|
|
610
|
|
611 (make-variable-buffer-local 'tmpl-old-local-map)
|
|
612
|
|
613
|
|
614
|
|
615 (defvar tmpl-minor-mode-map nil
|
|
616 "*The keymap for the minor mode tmpl-mode.")
|
|
617
|
|
618
|
|
619 (make-variable-buffer-local 'tmpl-minor-mode-map)
|
|
620
|
|
621
|
|
622 (if (adapt-xemacsp)
|
|
623 (defun tmpl-define-minor-mode-keymap ()
|
|
624 "Defines the minor mode keymap."
|
42
|
625 (define-key tmpl-minor-mode-map [(control c) (control c) x]
|
0
|
626 'tmpl-expand-templates-in-region)
|
42
|
627 (define-key tmpl-minor-mode-map [(control c) (control c) (control x)]
|
0
|
628 'tmpl-expand-templates-in-buffer)
|
42
|
629 (define-key tmpl-minor-mode-map [(control c) (control c) e]
|
0
|
630 'tmpl-escape-tmpl-sign-in-region)
|
42
|
631 (define-key tmpl-minor-mode-map
|
|
632 [(control c) (control c) (control e)]
|
0
|
633 'tmpl-escape-tmpl-sign-in-buffer))
|
|
634 (defun tmpl-define-minor-mode-keymap ()
|
|
635 "Defines the minor mode keymap."
|
42
|
636 (define-key tmpl-minor-mode-map [?\C-c ?\C-c ?x]
|
0
|
637 'tmpl-expand-templates-in-region)
|
42
|
638 (define-key tmpl-minor-mode-map [?\C-c ?\C-c ?\C-x]
|
0
|
639 'tmpl-expand-templates-in-buffer)
|
42
|
640 (define-key tmpl-minor-mode-map [?\C-c ?\C-c ?e]
|
0
|
641 'tmpl-escape-tmpl-sign-in-region)
|
42
|
642 (define-key tmpl-minor-mode-map [?\C-c ?\C-c ?\C-e]
|
0
|
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)
|