Mercurial > emacs
annotate lisp/autoinsert.el @ 17530:9fb83ef40566
(sgml-transformation): Fix previous change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 22 Apr 1997 04:32:00 +0000 |
parents | aacf08497202 |
children | 852464ce5d6a |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files |
14169 | 2 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc. |
845 | 4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Author: Charlie Martin <crm@cs.duke.edu> |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
6 ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389 |
180 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
180 | 13 ;; any later version. |
14 | |
15 ;; GNU Emacs 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 | |
14169 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
180 | 24 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
25 ;;; Commentary: |
180 | 26 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
27 ;; The following defines an association list for text to be |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
28 ;; automatically inserted when a new file is created, and a function |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
29 ;; which automatically inserts these files; the idea is to insert |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
30 ;; default text much as the mode is automatically set using |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
31 ;; auto-mode-alist. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
32 ;; |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
33 ;; To use: |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
34 ;; (add-hook 'find-file-hooks 'auto-insert) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
35 ;; setq auto-insert-directory to an appropriate slash-terminated value |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
36 ;; |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
37 ;; Author: Charlie Martin |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
38 ;; Department of Computer Science and |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
39 ;; National Biomedical Simulation Resource |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
40 ;; Box 3709 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
41 ;; Duke University Medical Center |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
42 ;; Durham, NC 27710 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
43 ;; (crm@cs.duke.edu,mcnc!duke!crm) |
180 | 44 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
45 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
46 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
47 (defvar auto-insert 'not-modified |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
48 "*Controls automatic insertion into newly found empty files: |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
49 nil do nothing |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
50 t insert if possible |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
51 other insert if possible, but mark as unmodified. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
52 Insertion is possible when something appropriate is found in |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
53 `auto-insert-alist'. When the insertion is marked as unmodified, you can |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
54 save it with \\[write-file] RET. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
55 This variable is used when `auto-insert' is called as a function, e.g. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
56 when you do (add-hook 'find-file-hooks 'auto-insert). |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
57 With \\[auto-insert], this is always treated as if it were `t'.") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
58 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
59 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
60 (defvar auto-insert-query 'function |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
61 "*If non-`nil', ask user before auto-inserting. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
62 When this is `function', only ask when called non-interactively.") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
63 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
64 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
65 (defvar auto-insert-prompt "Perform %s auto-insertion? " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
66 "*Prompt to use when querying whether to auto-insert. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
67 If this contains a %s, that will be replaced by the matching rule.") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
68 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
69 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
70 (defvar auto-insert-alist |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
71 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
72 (upcase (concat (file-name-nondirectory |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
73 (substring buffer-file-name 0 (match-beginning 0))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
74 "_" |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
75 (substring buffer-file-name (1+ (match-beginning 0))))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
76 "#ifndef " str \n |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
77 "#define " str "\n\n" |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
78 _ "\n\n#endif") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
79 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
80 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
81 nil |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
82 "#include \"" |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
83 ;; nop without latest cc-mode |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
84 (and (fboundp 'c-companion-file) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
85 ;(file-readable-p (c-companion-file 'name)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
86 (file-name-nondirectory (c-companion-file 'name))) & ?\" |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
87 | -10) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
88 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
89 ("[Mm]akefile\\'" . "makefile.inc") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
90 |
14153
3e949d37e46f
(auto-insert-alist): For html-mode, use `sgml-tag'.
Richard M. Stallman <rms@gnu.org>
parents:
12502
diff
changeset
|
91 (html-mode . (lambda () (sgml-tag "html"))) |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
92 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
93 (plain-tex-mode . "tex-insert.tex") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
94 (bibtex-mode . "tex-insert.tex") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
95 (latex-mode |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
96 ;; should try to offer completing read for these |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
97 "options, RET: " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
98 "\\documentstyle[" str & ?\] | -1 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
99 ?{ (read-string "class: ") "}\n" |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
100 ("package, %s: " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
101 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
102 _ "\n\\begin{document}\n" _ |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
103 "\n\\end{document}") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
104 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
105 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
106 lambda () |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
107 (if (eq major-mode default-major-mode) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
108 (sh-mode))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
109 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
110 (ada-mode . ada-header) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
111 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
112 (("\\.el\\'" . "Emacs Lisp header") |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
113 "Short description: " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
114 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
115 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
116 ;; Copyright (C) " (substring (current-time-string) -4) " by " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
117 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
118 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
119 ;; Author: " (user-full-name) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
120 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
121 (replace-match (capitalize (user-login-name)) t t)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
122 '(end-of-line 1) " <" (user-login-name) ?@ (system-name) "> |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
123 ;; Keywords: " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
124 '(require 'finder) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
125 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
126 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x)))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
127 finder-known-keywords) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
128 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
129 finder-known-keywords |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
130 "\n")) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
131 ((let ((minibuffer-help-form v2)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
132 (completing-read "Keyword, C-h: " v1 nil t)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
133 str ", ") & -2 " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
134 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
135 ;; This file is part of GNU Emacs. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
136 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
137 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
138 ;; it under the terms of the GNU General Public License as published by |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
139 ;; the Free Software Foundation; either version 2, or (at your option) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
140 ;; any later version. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
141 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
142 ;; GNU Emacs is distributed in the hope that it will be useful, |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
143 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
144 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
145 ;; GNU General Public License for more details. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
146 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
147 ;; You should have received a copy of the GNU General Public License |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
148 ;; along with GNU Emacs; see the file COPYING. If not, write to |
14181
aacf08497202
(auto-insert-alist): Use FSF's new address.
Erik Naggum <erik@naggum.no>
parents:
14169
diff
changeset
|
149 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
aacf08497202
(auto-insert-alist): Use FSF's new address.
Erik Naggum <erik@naggum.no>
parents:
14169
diff
changeset
|
150 ;; Boston, MA 02111-1307, USA. |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
151 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
152 ;;; Commentary: |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
153 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
154 ;; " _ " |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
155 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
156 ;;; Code: |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
157 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
158 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
159 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
160 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here")) |
180 | 161 "A list specifying text to insert by default into a new file. |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
162 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION). |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
163 CONDITION maybe a regexp that must match the new file's name, or it may be |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
164 a symbol that must match the major mode for this element to apply. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
165 Only the first matching element is effective. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
166 Optional DESCRIPTION is a string for filling `auto-insert-prompt'. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
167 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
168 file-name or one relative to `auto-insert-directory' or a function to call. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
169 ACTION may also be a vector containing several successive single actions as |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
170 described above, e.g. [\"header.insert\" date-and-author-update].") |
180 | 171 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
172 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
173 ;; Establish a default value for auto-insert-directory |
180 | 174 (defvar auto-insert-directory "~/insert/" |
175 "*Directory from which auto-inserted files are taken.") | |
176 | |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
177 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
178 ;;;###autoload |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
179 (defun auto-insert () |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
180 "Insert default contents into a new file if `auto-insert' is non-nil. |
180 | 181 Matches the visited file name against the elements of `auto-insert-alist'." |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
182 (interactive) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
183 (and (not buffer-read-only) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
184 (or (eq this-command 'auto-insert) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
185 (and auto-insert |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
186 (bobp) (eobp))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
187 (let ((alist auto-insert-alist) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
188 case-fold-search cond desc action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
189 (goto-char 1) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
190 ;; find first matching alist entry |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
191 (while alist |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
192 (if (atom (setq cond (car (car alist)))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
193 (setq desc cond) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
194 (setq desc (cdr cond) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
195 cond (car cond))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
196 (if (if (symbolp cond) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
197 (eq cond major-mode) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
198 (string-match cond buffer-file-name)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
199 (setq action (cdr (car alist)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
200 alist nil) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
201 (setq alist (cdr alist)))) |
180 | 202 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
203 ;; Now, if we found something, do it |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
204 (and action |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
205 (if (stringp action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
206 (file-readable-p (concat auto-insert-directory action)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
207 t) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
208 (if auto-insert-query |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
209 (or (if (eq auto-insert-query 'function) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
210 (eq this-command 'auto-insert)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
211 (y-or-n-p (format auto-insert-prompt desc))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
212 t) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
213 (mapcar |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
214 (lambda (action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
215 (if (stringp action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
216 (if (file-readable-p |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
217 (setq action (concat auto-insert-directory action))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
218 (insert-file-contents action)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
219 (save-window-excursion |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
220 ;; make buffer visible before skeleton or function |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
221 ;; which might ask the user for something |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
222 (switch-to-buffer (current-buffer)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
223 (if (and (consp action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
224 (not (eq (car action) 'lambda))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
225 (skeleton-insert action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
226 (funcall action))))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
227 (if (vectorp action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
228 action |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
229 (vector action)))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
230 (and (buffer-modified-p) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
231 (not (eq this-command 'auto-insert)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
232 (set-buffer-modified-p (eq auto-insert t)))))) |
180 | 233 |
12502
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
234 |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
235 ;;;###autoload |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
236 (defun define-auto-insert (key action &optional after) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
237 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'. |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
238 Optional AFTER means to insert action after all existing actions for CONDITION, |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
239 or if CONDITION had no actions, after all other CONDITIONs." |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
240 (let ((elt (assoc key auto-insert-alist))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
241 (if elt |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
242 (setcdr elt |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
243 (if (vectorp (cdr elt)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
244 (vconcat (if after (cdr elt)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
245 (if (vectorp action) action (vector action)) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
246 (if after () (cdr elt))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
247 (if after |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
248 (vector (cdr elt) action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
249 (vector action (cdr elt))))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
250 (if after |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
251 (nconc auto-insert-alist (list (cons key action))) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
252 (setq auto-insert-alist (cons (cons key action) |
8bb3424bf745
extended to use skeletons or call functions
Karl Heuer <kwzh@gnu.org>
parents:
8764
diff
changeset
|
253 auto-insert-alist)))))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
254 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
255 ;;; autoinsert.el ends here |