(whitespace): Set :version tag to 23.1.
[emacs.git] / lisp / autoinsert.el
blobe7dc061168ef647a6e79ba2ec0606caeff9c2963
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995, 1998, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Charlie Martin <crm@cs.duke.edu>
7 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: convenience
9 ;; Maintainer: FSF
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; The following defines an association list for text to be
31 ;; automatically inserted when a new file is created, and a function
32 ;; which automatically inserts these files; the idea is to insert
33 ;; default text much as the mode is automatically set using
34 ;; auto-mode-alist.
36 ;; To use:
37 ;; (add-hook 'find-file-hook 'auto-insert)
38 ;; setq auto-insert-directory to an appropriate slash-terminated value
40 ;; You can also customize the variable `auto-insert-mode' to load the
41 ;; package. Alternatively, add the following to your .emacs file:
42 ;; (auto-insert-mode 1)
44 ;; Author: Charlie Martin
45 ;; Department of Computer Science and
46 ;; National Biomedical Simulation Resource
47 ;; Box 3709
48 ;; Duke University Medical Center
49 ;; Durham, NC 27710
50 ;; (crm@cs.duke.edu,mcnc!duke!crm)
52 ;;; Code:
54 (defgroup auto-insert nil
55 "Automatic mode-dependent insertion of text into new files."
56 :prefix "auto-insert-"
57 :group 'files
58 :group 'convenience)
61 (defcustom auto-insert 'not-modified
62 "*Controls automatic insertion into newly found empty files.
63 Possible values:
64 nil do nothing
65 t insert if possible
66 other insert if possible, but mark as unmodified.
67 Insertion is possible when something appropriate is found in
68 `auto-insert-alist'. When the insertion is marked as unmodified, you can
69 save it with \\[write-file] RET.
70 This variable is used when the function `auto-insert' is called, e.g.
71 when you do (add-hook 'find-file-hook 'auto-insert).
72 With \\[auto-insert], this is always treated as if it were t."
73 :type '(choice (const :tag "Insert if possible" t)
74 (const :tag "Do nothing" nil)
75 (other :tag "insert if possible, mark as unmodified."
76 not-modified))
77 :group 'auto-insert)
79 (defcustom auto-insert-query 'function
80 "*Non-nil means ask user before auto-inserting.
81 When this is `function', only ask when called non-interactively."
82 :type '(choice (const :tag "Don't ask" nil)
83 (const :tag "Ask if called non-interactively" function)
84 (other :tag "Ask" t))
85 :group 'auto-insert)
87 (defcustom auto-insert-prompt "Perform %s auto-insertion? "
88 "*Prompt to use when querying whether to auto-insert.
89 If this contains a %s, that will be replaced by the matching rule."
90 :type 'string
91 :group 'auto-insert)
94 (defcustom auto-insert-alist
95 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
96 (upcase (concat (file-name-nondirectory
97 (substring buffer-file-name 0 (match-beginning 0)))
98 "_"
99 (substring buffer-file-name (1+ (match-beginning 0)))))
100 "#ifndef " str \n
101 "#define " str "\n\n"
102 _ "\n\n#endif")
104 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
106 "#include \""
107 (let ((stem (file-name-sans-extension buffer-file-name)))
108 (cond ((file-exists-p (concat stem ".h"))
109 (file-name-nondirectory (concat stem ".h")))
110 ((file-exists-p (concat stem ".hh"))
111 (file-name-nondirectory (concat stem ".hh")))))
112 & ?\" | -10)
114 (("[Mm]akefile\\'" . "Makefile") . "makefile.inc")
116 (html-mode . (lambda () (sgml-tag "html")))
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 "\\documentclass[" 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}")
130 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
131 lambda ()
132 (if (eq major-mode default-major-mode)
133 (sh-mode)))
135 (ada-mode . ada-header)
137 (("\\.[1-9]\\'" . "Man page skeleton")
138 "Short description: "
139 ".\\\" Copyright (C), " (substring (current-time-string) -4) " "
140 (getenv "ORGANIZATION") | (progn user-full-name)
142 .\\\" You may distribute this file under the terms of the GNU Free
143 .\\\" Documentation License.
144 .TH " (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
145 " " (file-name-extension (buffer-file-name))
146 " " (format-time-string "%Y-%m-%d ")
147 "\n.SH NAME\n"
148 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
149 " \\- " str
150 "\n.SH SYNOPSIS
151 .B " (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
152 "\n"
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")
166 (("\\.el\\'" . "Emacs Lisp header")
167 "Short description: "
168 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
170 ;; Copyright (C) " (substring (current-time-string) -4) " "
171 (getenv "ORGANIZATION") | (progn user-full-name) "
173 ;; Author: " (user-full-name)
174 '(if (search-backward "&" (line-beginning-position) t)
175 (replace-match (capitalize (user-login-name)) t t))
176 '(end-of-line 1) " <" (progn user-mail-address) ">
177 ;; Keywords: "
178 '(require 'finder)
179 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
180 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
181 finder-known-keywords)
182 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
183 finder-known-keywords
184 "\n"))
185 ((let ((minibuffer-help-form v2))
186 (completing-read "Keyword, C-h: " v1 nil t))
187 str ", ") & -2 "
189 \;; This file is free software; you can redistribute it and/or modify
190 \;; it under the terms of the GNU General Public License as published by
191 \;; the Free Software Foundation; either version 3, or (at your option)
192 \;; any later version.
194 \;; This file is distributed in the hope that it will be useful,
195 \;; but WITHOUT ANY WARRANTY; without even the implied warranty of
196 \;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
197 \;; GNU General Public License for more details.
199 \;; You should have received a copy of the GNU General Public License
200 \;; along with GNU Emacs; see the file COPYING. If not, write to
201 \;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
202 \;; Boston, MA 02110-1301, USA.
204 \;;; Commentary:
206 \;; " _ "
208 \;;; Code:
212 \(provide '"
213 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
215 \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
216 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
217 "Title: "
218 "\\input texinfo @c -*-texinfo-*-
219 @c %**start of header
220 @setfilename "
221 (file-name-sans-extension
222 (file-name-nondirectory (buffer-file-name))) ".info\n"
223 "@settitle " str "
224 @c %**end of header
225 @copying\n"
226 (setq short-description (read-string "Short description: "))
227 ".\n\n"
228 "Copyright @copyright{} " (substring (current-time-string) -4) " "
229 (getenv "ORGANIZATION") | (progn user-full-name) "
231 @quotation
232 Permission is granted to copy, distribute and/or modify this document
233 under the terms of the GNU Free Documentation License, Version 1.1 or
234 any later version published by the Free Software Foundation; with no
235 Invariant Sections, and no Cover Texts. A copy of the license is
236 included in the section entitled ``GNU Free Documentation License.''
238 A copy of the license is also available from the Free Software
239 Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
241 @end quotation
243 The document was typeset with
244 @uref{http://www.texinfo.org/, GNU Texinfo}.
246 @end copying
248 @titlepage
249 @title " str "
250 @subtitle " short-description "
251 @author " (getenv "ORGANIZATION") | (progn user-full-name)
252 " <" (progn user-mail-address) ">
253 @page
254 @vskip 0pt plus 1filll
255 @insertcopying
256 @end titlepage
258 @c Output the table of the contents at the beginning.
259 @contents
261 @ifnottex
262 @node Top
263 @top " str "
265 @insertcopying
266 @end ifnottex
268 @c Generate the nodes for this menu with `C-c C-u C-m'.
269 @menu
270 @end menu
272 @c Update all node entries with `C-c C-u C-n'.
273 @c Insert new nodes with `C-c C-c n'.
274 @node Chapter One
275 @chapter Chapter One
277 " _ "
279 @node Copying This Manual
280 @appendix Copying This Manual
282 @menu
283 * GNU Free Documentation License:: License for copying this manual.
284 @end menu
286 @c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
287 @include fdl.texi
289 @node Index
290 @unnumbered Index
292 @printindex cp
294 @bye
296 @c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
297 "A list specifying text to insert by default into a new file.
298 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
299 CONDITION may be a regexp that must match the new file's name, or it may be
300 a symbol that must match the major mode for this element to apply.
301 Only the first matching element is effective.
302 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
303 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
304 file-name or one relative to `auto-insert-directory' or a function to call.
305 ACTION may also be a vector containing several successive single actions as
306 described above, e.g. [\"header.insert\" date-and-author-update]."
307 :type 'sexp
308 :group 'auto-insert)
311 ;; Establish a default value for auto-insert-directory
312 (defcustom auto-insert-directory "~/insert/"
313 "*Directory from which auto-inserted files are taken.
314 The value must be an absolute directory name;
315 thus, on a GNU or Unix system, it must end in a slash."
316 :type 'directory
317 :group 'auto-insert)
320 ;;;###autoload
321 (defun auto-insert ()
322 "Insert default contents into new files if variable `auto-insert' is non-nil.
323 Matches the visited file name against the elements of `auto-insert-alist'."
324 (interactive)
325 (and (not buffer-read-only)
326 (or (eq this-command 'auto-insert)
327 (and auto-insert
328 (bobp) (eobp)))
329 (let ((alist auto-insert-alist)
330 case-fold-search cond desc action)
331 (goto-char 1)
332 ;; find first matching alist entry
333 (while alist
334 (if (atom (setq cond (car (car alist))))
335 (setq desc cond)
336 (setq desc (cdr cond)
337 cond (car cond)))
338 (if (if (symbolp cond)
339 (eq cond major-mode)
340 (and buffer-file-name
341 (string-match cond buffer-file-name)))
342 (setq action (cdr (car alist))
343 alist nil)
344 (setq alist (cdr alist))))
346 ;; Now, if we found something, do it
347 (and action
348 (if (stringp action)
349 (file-readable-p (concat auto-insert-directory action))
351 (if auto-insert-query
352 (or (if (eq auto-insert-query 'function)
353 (eq this-command 'auto-insert))
354 (y-or-n-p (format auto-insert-prompt desc)))
356 (mapc
357 (lambda (action)
358 (if (stringp action)
359 (if (file-readable-p
360 (setq action (concat auto-insert-directory action)))
361 (insert-file-contents action))
362 (save-window-excursion
363 ;; make buffer visible before skeleton or function
364 ;; which might ask the user for something
365 (switch-to-buffer (current-buffer))
366 (if (and (consp action)
367 (not (eq (car action) 'lambda)))
368 (skeleton-insert action)
369 (funcall action)))))
370 (if (vectorp action)
371 action
372 (vector action))))
373 (and (buffer-modified-p)
374 (not (eq this-command 'auto-insert))
375 (set-buffer-modified-p (eq auto-insert t)))))
376 ;; Return nil so that it could be used in
377 ;; `find-file-not-found-hooks', though that's probably inadvisable.
378 nil)
381 ;;;###autoload
382 (defun define-auto-insert (condition action &optional after)
383 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
384 Optional AFTER means to insert action after all existing actions for CONDITION,
385 or if CONDITION had no actions, after all other CONDITIONs."
386 (let ((elt (assoc condition auto-insert-alist)))
387 (if elt
388 (setcdr elt
389 (if (vectorp (cdr elt))
390 (vconcat (if after (cdr elt))
391 (if (vectorp action) action (vector action))
392 (if after () (cdr elt)))
393 (if after
394 (vector (cdr elt) action)
395 (vector action (cdr elt)))))
396 (if after
397 (nconc auto-insert-alist (list (cons condition action)))
398 (setq auto-insert-alist (cons (cons condition action)
399 auto-insert-alist))))))
401 ;;;###autoload
402 (define-minor-mode auto-insert-mode
403 "Toggle Auto-insert mode.
404 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
405 Returns the new status of Auto-insert mode (non-nil means on).
407 When Auto-insert mode is enabled, when new files are created you can
408 insert a template for the file depending on the mode of the buffer."
409 :global t :group 'auto-insert
410 (if auto-insert-mode
411 (add-hook 'find-file-hook 'auto-insert)
412 (remove-hook 'find-file-hook 'auto-insert)))
414 (provide 'autoinsert)
416 ;; arch-tag: 5b6630ac-c735-43cf-b097-b78c622af909
417 ;;; autoinsert.el ends here