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