Fix Python tests on MS-Windows
[emacs.git] / lisp / autoinsert.el
Commit [+]AuthorDateLineData
c0274f38 Eric S. Raymond1992-05-30 23:54:21 +00001;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
b578f267 Erik Naggum1996-01-14 07:34:30 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2015 Free Software
ab422c4d Paul Eggert2013-01-01 09:11:05 +00004;; Foundation, Inc.
9750e079 Eric S. Raymond1992-07-22 04:22:30 +00005
e5167999 Eric S. Raymond1992-07-16 21:47:34 +00006;; Author: Charlie Martin <crm@cs.duke.edu>
3e910376 Karl Heuer1998-12-14 03:22:44 +00007;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
f5f727f8 Dan Nicolaescu1998-05-26 09:23:23 +00008;; Keywords: convenience
34dc21db Glenn Morris2014-02-09 17:34:22 -08009;; Maintainer: emacs-devel@gnu.org
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000010
11;; This file is part of GNU Emacs.
12
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000013;; GNU Emacs is free software: you can redistribute it and/or modify
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000014;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000015;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000017
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000024;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000025
e5167999 Eric S. Raymond1992-07-16 21:47:34 +000026;;; Commentary:
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000027
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +000028;; 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;;
12394086 Dave Love1999-11-30 14:45:39 +000034;; To use:
a702ffbb Stefan Monnier2005-03-24 12:44:46 +000035;; (add-hook 'find-file-hook 'auto-insert)
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +000036;; setq auto-insert-directory to an appropriate slash-terminated value
37;;
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000038;; You can also customize the variable `auto-insert-mode' to load the
865fe16f Chong Yidong2012-09-17 13:41:04 +080039;; package. Alternatively, add the following to your init file:
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +000040;; (auto-insert-mode 1)
41;;
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +000042;; 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
12394086 Dave Love1999-11-30 14:45:39 +000048;; (crm@cs.duke.edu,mcnc!duke!crm)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000049
e5167999
ER
Eric S. Raymond1992-07-16 21:47:34 +000050;;; Code:
51
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +000052(defgroup auto-insert nil
53 "Automatic mode-dependent insertion of text into new files."
54 :prefix "auto-insert-"
f5f727f8 Dan Nicolaescu1998-05-26 09:23:23 +000055 :group 'files
b1d4664a
JB
Juanma Barranquero2008-05-29 09:40:37 +000056 :group 'convenience
57 :link '(custom-manual "(autotype) Autoinserting"))
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +000058
59
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000060(defcustom auto-insert 'not-modified
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000061 "Controls automatic insertion into newly found empty files.
12394086 Dave Love1999-11-30 14:45:39 +000062Possible values:
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +000063 nil do nothing
64 t insert if possible
65 other insert if possible, but mark as unmodified.
66Insertion is possible when something appropriate is found in
67`auto-insert-alist'. When the insertion is marked as unmodified, you can
68save it with \\[write-file] RET.
a461758e Dave Love2000-09-25 14:43:35 +000069This variable is used when the function `auto-insert' is called, e.g.
875a5d0e Paul Eggert2015-08-24 23:39:33 -070070when you do (add-hook \\='find-file-hook \\='auto-insert).
12394086 Dave Love1999-11-30 14:45:39 +000071With \\[auto-insert], this is always treated as if it were t."
4605a50d
DL
Dave Love1998-03-25 19:06:46 +000072 :type '(choice (const :tag "Insert if possible" t)
73 (const :tag "Do nothing" nil)
12394086 Dave Love1999-11-30 14:45:39 +000074 (other :tag "insert if possible, mark as unmodified."
4605a50d Dave Love1998-03-25 19:06:46 +000075 not-modified))
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000076 :group 'auto-insert)
c11af8a5 Karl Heuer1995-07-07 19:30:23 +000077
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000078(defcustom auto-insert-query 'function
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000079 "Non-nil means ask user before auto-inserting.
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000080When this is `function', only ask when called non-interactively."
5c213454
AS
Andreas Schwab1998-06-24 08:48:01 +000081 :type '(choice (const :tag "Don't ask" nil)
82 (const :tag "Ask if called non-interactively" function)
83 (other :tag "Ask" t))
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000084 :group 'auto-insert)
c11af8a5 Karl Heuer1995-07-07 19:30:23 +000085
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000086(defcustom auto-insert-prompt "Perform %s auto-insertion? "
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000087 "Prompt to use when querying whether to auto-insert.
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +000088If this contains a %s, that will be replaced by the matching rule."
89 :type 'string
90 :group 'auto-insert)
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +000091
92
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +000093(defcustom auto-insert-alist
a0363ffa
RS
Rüdiger Sonderfeld2014-12-04 07:08:18 +010094 '((("\\.\\([Hh]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'" . "C / C++ header")
95 (replace-regexp-in-string
96 "[^A-Z0-9]" "_"
97 (replace-regexp-in-string
98 "\\+" "P"
99 (upcase (file-name-nondirectory buffer-file-name))))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000100 "#ifndef " str \n
101 "#define " str "\n\n"
102 _ "\n\n#endif")
103
a0363ffa Rüdiger Sonderfeld2014-12-04 07:08:18 +0100104 (("\\.\\([Cc]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'" . "C / C++ program")
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000105 nil
106 "#include \""
a0363ffa
RS
Rüdiger Sonderfeld2014-12-04 07:08:18 +0100107 (let ((stem (file-name-sans-extension buffer-file-name))
108 ret)
109 (dolist (ext '("H" "h" "hh" "hpp" "hxx" "h++") ret)
110 (when (file-exists-p (concat stem "." ext))
111 (setq ret (file-name-nondirectory (concat stem "." ext))))))
f6ec1532
RS
Richard M. Stallman2001-12-18 19:37:37 +0000112 & ?\" | -10)
113
114 (("[Mm]akefile\\'" . "Makefile") . "makefile.inc")
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000115
d60ae75c Richard M. Stallman1996-01-12 05:54:33 +0000116 (html-mode . (lambda () (sgml-tag "html")))
71296446 Juanma Barranquero2003-02-04 11:26:42 +0000117
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000118 (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: "
47e58cf5 Dave Love2000-11-06 15:17:06 +0000123 "\\documentclass[" str & ?\] | -1
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000124 ?{ (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
a6ed0e28
JL
Juri Linkov2010-07-03 01:35:09 +0300130 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") .
131 (lambda ()
14acf2f5 Stefan Monnier2009-08-28 17:02:53 +0000132 (if (eq major-mode (default-value 'major-mode))
a6ed0e28 Juri Linkov2010-07-03 01:35:09 +0300133 (sh-mode))))
71296446 Juanma Barranquero2003-02-04 11:26:42 +0000134
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000135 (ada-mode . ada-header)
136
bf9f5c17
DL
Dave Love2003-01-03 20:01:24 +0000137 (("\\.[1-9]\\'" . "Man page skeleton")
138 "Short description: "
0bfcf5c5 Paul Eggert2012-05-03 23:13:18 -0700139 ".\\\" Copyright (C), " (format-time-string "%Y") " "
12148b0e Richard M. Stallman2004-04-21 19:09:01 +0000140 (getenv "ORGANIZATION") | (progn user-full-name)
bf9f5c17
DL
Dave Love2003-01-03 20:01:24 +0000141 "
142.\\\" You may distribute this file under the terms of the GNU Free
2201b59a Juanma Barranquero2005-06-21 13:41:18 +0000143.\\\" Documentation License.
d2c32364 Sam Steingold2012-06-26 12:23:01 -0400144.TH " (file-name-base)
bf9f5c17
DL
Dave Love2003-01-03 20:01:24 +0000145 " " (file-name-extension (buffer-file-name))
146 " " (format-time-string "%Y-%m-%d ")
147 "\n.SH NAME\n"
d2c32364 Sam Steingold2012-06-26 12:23:01 -0400148 (file-name-base)
bf9f5c17
DL
Dave Love2003-01-03 20:01:24 +0000149 " \\- " str
150 "\n.SH SYNOPSIS
d2c32364 Sam Steingold2012-06-26 12:23:01 -0400151.B " (file-name-base)
bf9f5c17
DL
Dave Love2003-01-03 20:01:24 +0000152 "\n"
153 _
154 "
155.SH DESCRIPTION
156.SH OPTIONS
157.SH FILES
158.SH \"SEE ALSO\"
159.SH BUGS
160.SH AUTHOR
161" (user-full-name)
162 '(if (search-backward "&" (line-beginning-position) t)
163 (replace-match (capitalize (user-login-name)) t t))
164 '(end-of-line 1) " <" (progn user-mail-address) ">\n")
165
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000166 (("\\.el\\'" . "Emacs Lisp header")
167 "Short description: "
96eb829a
LL
Leo Liu2013-07-05 10:37:39 +0800168 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
169 (make-string (max 2 (- 80 (current-column) 27)) ?\s)
33215353 Leo Liu2013-09-23 12:21:41 +0800170 "-*- lexical-binding: t; -*-" '(setq lexical-binding t)
96eb829a Leo Liu2013-07-05 10:37:39 +0800171 "
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000172
0bfcf5c5 Paul Eggert2012-05-03 23:13:18 -0700173;; Copyright (C) " (format-time-string "%Y") " "
fbee29e6 John Paul Wallington2004-11-15 20:50:08 +0000174 (getenv "ORGANIZATION") | (progn user-full-name) "
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000175
176;; Author: " (user-full-name)
a461758e Dave Love2000-09-25 14:43:35 +0000177'(if (search-backward "&" (line-beginning-position) t)
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000178 (replace-match (capitalize (user-login-name)) t t))
c5f7d536 Richard M. Stallman1998-11-29 21:12:28 +0000179'(end-of-line 1) " <" (progn user-mail-address) ">
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000180;; Keywords: "
181 '(require 'finder)
182 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
183 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
184 finder-known-keywords)
8ed6049f Martin Rudalics2007-12-21 10:22:37 +0000185 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000186 finder-known-keywords
187 "\n"))
188 ((let ((minibuffer-help-form v2))
189 (completing-read "Keyword, C-h: " v1 nil t))
190 str ", ") & -2 "
191
88ddede6 Glenn Morris2008-02-27 05:03:06 +0000192\;; This program is free software; you can redistribute it and/or modify
31b6b8cd Stefan Monnier2007-05-23 17:29:28 +0000193\;; it under the terms of the GNU General Public License as published by
88ddede6
GM
Glenn Morris2008-02-27 05:03:06 +0000194\;; the Free Software Foundation, either version 3 of the License, or
195\;; (at your option) any later version.
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000196
88ddede6 Glenn Morris2008-02-27 05:03:06 +0000197\;; This program is distributed in the hope that it will be useful,
31b6b8cd
SM
Stefan Monnier2007-05-23 17:29:28 +0000198\;; but WITHOUT ANY WARRANTY; without even the implied warranty of
199\;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
200\;; GNU General Public License for more details.
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000201
31b6b8cd Stefan Monnier2007-05-23 17:29:28 +0000202\;; You should have received a copy of the GNU General Public License
88ddede6 Glenn Morris2008-02-27 05:03:06 +0000203\;; along with this program. If not, see <http://www.gnu.org/licenses/>.
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000204
31b6b8cd Stefan Monnier2007-05-23 17:29:28 +0000205\;;; Commentary:
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000206
31b6b8cd Stefan Monnier2007-05-23 17:29:28 +0000207\;; " _ "
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000208
31b6b8cd Stefan Monnier2007-05-23 17:29:28 +0000209\;;; Code:
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000210
211
212
3c98b2e9 Stefan Monnier2000-11-22 22:50:22 +0000213\(provide '"
d2c32364 Sam Steingold2012-06-26 12:23:01 -0400214 (file-name-base)
3c98b2e9 Stefan Monnier2000-11-22 22:50:22 +0000215 ")
1caf5f96
GM
Glenn Morris2007-10-06 01:49:53 +0000216\;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
217 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
218 "Title: "
219 "\\input texinfo @c -*-texinfo-*-
220@c %**start of header
221@setfilename "
d2c32364 Sam Steingold2012-06-26 12:23:01 -0400222 (file-name-base) ".info\n"
1caf5f96
GM
Glenn Morris2007-10-06 01:49:53 +0000223 "@settitle " str "
224@c %**end of header
225@copying\n"
226 (setq short-description (read-string "Short description: "))
227 ".\n\n"
0bfcf5c5 Paul Eggert2012-05-03 23:13:18 -0700228 "Copyright @copyright{} " (format-time-string "%Y") " "
1caf5f96
GM
Glenn Morris2007-10-06 01:49:53 +0000229 (getenv "ORGANIZATION") | (progn user-full-name) "
230
231@quotation
232Permission is granted to copy, distribute and/or modify this document
d5259d01 Glenn Morris2008-11-19 04:32:05 +0000233under the terms of the GNU Free Documentation License, Version 1.3
af8b50d1 Glenn Morris2008-02-28 03:44:36 +0000234or any later version published by the Free Software Foundation;
d5259d01
GM
Glenn Morris2008-11-19 04:32:05 +0000235with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
236A copy of the license is included in the section entitled ``GNU
af8b50d1 Glenn Morris2008-02-28 03:44:36 +0000237Free Documentation License''.
1caf5f96
GM
Glenn Morris2007-10-06 01:49:53 +0000238
239A copy of the license is also available from the Free Software
240Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
241
242@end quotation
243
244The document was typeset with
245@uref{http://www.texinfo.org/, GNU Texinfo}.
246
247@end copying
248
249@titlepage
250@title " str "
251@subtitle " short-description "
252@author " (getenv "ORGANIZATION") | (progn user-full-name)
253 " <" (progn user-mail-address) ">
254@page
255@vskip 0pt plus 1filll
256@insertcopying
257@end titlepage
258
259@c Output the table of the contents at the beginning.
260@contents
261
262@ifnottex
263@node Top
264@top " str "
265
266@insertcopying
267@end ifnottex
268
269@c Generate the nodes for this menu with `C-c C-u C-m'.
270@menu
271@end menu
272
273@c Update all node entries with `C-c C-u C-n'.
274@c Insert new nodes with `C-c C-c n'.
275@node Chapter One
276@chapter Chapter One
277
278" _ "
279
280@node Copying This Manual
281@appendix Copying This Manual
282
283@menu
284* GNU Free Documentation License:: License for copying this manual.
285@end menu
286
287@c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
288@include fdl.texi
289
290@node Index
291@unnumbered Index
292
293@printindex cp
294
295@bye
296
297@c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000298 "A list specifying text to insert by default into a new file.
c11af8a5 Karl Heuer1995-07-07 19:30:23 +0000299Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
2a575769 Eli Zaretskii2006-10-07 11:03:00 +0000300CONDITION may be a regexp that must match the new file's name, or it may be
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000301a symbol that must match the major mode for this element to apply.
302Only the first matching element is effective.
303Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
304ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
305file-name or one relative to `auto-insert-directory' or a function to call.
306ACTION may also be a vector containing several successive single actions as
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +0000307described above, e.g. [\"header.insert\" date-and-author-update]."
308 :type 'sexp
a0363ffa Rüdiger Sonderfeld2014-12-04 07:08:18 +0100309 :version "25.1"
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000310 :group 'auto-insert)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000311
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000312
313;; Establish a default value for auto-insert-directory
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000314(defcustom auto-insert-directory "~/insert/"
9201cc28 Lute Kamstra2008-12-03 05:48:14 +0000315 "Directory from which auto-inserted files are taken.
87810ca9
RS
Richard M. Stallman2002-04-30 16:36:02 +0000316The value must be an absolute directory name;
317thus, on a GNU or Unix system, it must end in a slash."
6c5b39e4
SE
Stephen Eglen1998-02-01 19:13:59 +0000318 :type 'directory
319 :group 'auto-insert)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000320
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000321
322;;;###autoload
323(defun auto-insert ()
a461758e Dave Love2000-09-25 14:43:35 +0000324 "Insert default contents into new files if variable `auto-insert' is non-nil.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000325Matches the visited file name against the elements of `auto-insert-alist'."
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000326 (interactive)
327 (and (not buffer-read-only)
328 (or (eq this-command 'auto-insert)
329 (and auto-insert
330 (bobp) (eobp)))
331 (let ((alist auto-insert-alist)
332 case-fold-search cond desc action)
333 (goto-char 1)
334 ;; find first matching alist entry
335 (while alist
336 (if (atom (setq cond (car (car alist))))
337 (setq desc cond)
338 (setq desc (cdr cond)
339 cond (car cond)))
340 (if (if (symbolp cond)
341 (eq cond major-mode)
a461758e
DL
Dave Love2000-09-25 14:43:35 +0000342 (and buffer-file-name
343 (string-match cond buffer-file-name)))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000344 (setq action (cdr (car alist))
345 alist nil)
346 (setq alist (cdr alist))))
347
348 ;; Now, if we found something, do it
349 (and action
bdaf166c
SM
Stefan Monnier2008-12-08 16:30:43 +0000350 (or (not (stringp action))
351 (file-readable-p (expand-file-name
352 action auto-insert-directory)))
353 (or (not auto-insert-query)
354 (if (eq auto-insert-query 'function)
355 (eq this-command 'auto-insert))
356 (y-or-n-p (format auto-insert-prompt desc)))
6fd09e19 Juanma Barranquero2007-09-25 10:42:46 +0000357 (mapc
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000358 (lambda (action)
359 (if (stringp action)
360 (if (file-readable-p
bdaf166c
SM
Stefan Monnier2008-12-08 16:30:43 +0000361 (setq action (expand-file-name
362 action auto-insert-directory)))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000363 (insert-file-contents action))
364 (save-window-excursion
365 ;; make buffer visible before skeleton or function
366 ;; which might ask the user for something
c3313451 Chong Yidong2011-09-02 12:38:40 -0400367 (switch-to-buffer (current-buffer))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000368 (if (and (consp action)
369 (not (eq (car action) 'lambda)))
370 (skeleton-insert action)
371 (funcall action)))))
372 (if (vectorp action)
373 action
374 (vector action))))
375 (and (buffer-modified-p)
376 (not (eq this-command 'auto-insert))
12394086
DL
Dave Love1999-11-30 14:45:39 +0000377 (set-buffer-modified-p (eq auto-insert t)))))
378 ;; Return nil so that it could be used in
379 ;; `find-file-not-found-hooks', though that's probably inadvisable.
380 nil)
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000381
382
383;;;###autoload
4605a50d Dave Love1998-03-25 19:06:46 +0000384(defun define-auto-insert (condition action &optional after)
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000385 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
386Optional AFTER means to insert action after all existing actions for CONDITION,
387or if CONDITION had no actions, after all other CONDITIONs."
4605a50d Dave Love1998-03-25 19:06:46 +0000388 (let ((elt (assoc condition auto-insert-alist)))
c11af8a5
KH
Karl Heuer1995-07-07 19:30:23 +0000389 (if elt
390 (setcdr elt
391 (if (vectorp (cdr elt))
392 (vconcat (if after (cdr elt))
393 (if (vectorp action) action (vector action))
394 (if after () (cdr elt)))
395 (if after
396 (vector (cdr elt) action)
397 (vector action (cdr elt)))))
398 (if after
4605a50d Dave Love1998-03-25 19:06:46 +0000399 (nconc auto-insert-alist (list (cons condition action)))
bdaf166c Stefan Monnier2008-12-08 16:30:43 +0000400 (push (cons condition action) auto-insert-alist)))))
c0274f38 Eric S. Raymond1992-05-30 23:54:21 +0000401
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000402;;;###autoload
bff6da3d Stefan Monnier2000-09-29 22:14:58 +0000403(define-minor-mode auto-insert-mode
06e21633
CY
Chong Yidong2011-10-19 08:54:24 -0400404 "Toggle Auto-insert mode, a global minor mode.
405With a prefix argument ARG, enable Auto-insert mode if ARG is
406positive, and disable it otherwise. If called from Lisp, enable
407the mode if ARG is omitted or nil.
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000408
12394086 Dave Love1999-11-30 14:45:39 +0000409When Auto-insert mode is enabled, when new files are created you can
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000410insert a template for the file depending on the mode of the buffer."
44e70da2 Stefan Monnier2000-11-03 22:12:48 +0000411 :global t :group 'auto-insert
bff6da3d Stefan Monnier2000-09-29 22:14:58 +0000412 (if auto-insert-mode
a702ffbb
SM
Stefan Monnier2005-03-24 12:44:46 +0000413 (add-hook 'find-file-hook 'auto-insert)
414 (remove-hook 'find-file-hook 'auto-insert)))
6c5b39e4 Stephen Eglen1998-02-01 19:13:59 +0000415
896546cd
RS
Richard M. Stallman1997-06-22 18:57:55 +0000416(provide 'autoinsert)
417
c0274f38 Eric S. Raymond1992-05-30 23:54:21 +0000418;;; autoinsert.el ends here