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