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