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