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