Initial revision
[emacs.git] / lisp / autoinsert.el
blob61736c9f68b414ec9ae867a330883f28cdee1726
1 ;; Automatic mode-dependent insertion of text into new files.
2 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 ;;; autoinsert.el
22 ;;; Abstract:
23 ;;;
24 ;;; The following defines an association list for files to be
25 ;;; automatically inserted when a new file is created, and a function
26 ;;; which automatically inserts these files; the idea is to insert
27 ;;; default files much as the mode is automatically set using
28 ;;; auto-mode-alist.
29 ;;;
30 ;;; The auto-insert-alist consists of dotted pairs of
31 ;;; ( REGEXP . FILENAME ) where REGEXP is a regular expression, and
32 ;;; FILENAME is the file name of a file which is to be inserted into
33 ;;; all new files matching the regular expression with which it is
34 ;;; paired.
35 ;;;
36 ;;; To use:
37 ;;; load autoinsert.el
38 ;;; setq auto-insert-directory to an appropriate value, which
39 ;;; must end in "/"
40 ;;;
41 ;;; Author: Charlie Martin
42 ;;; Department of Computer Science and
43 ;;; National Biomedical Simulation Resource
44 ;;; Box 3709
45 ;;; Duke University Medical Center
46 ;;; Durham, NC 27710
47 ;;; (crm@cs.duke.edu,mcnc!duke!crm)
48 ;;;
49 ;;; Date: Fri Jul 1 16:15:31 EDT 1988
51 (defvar auto-insert-alist '(("\\.tex$" . "tex-insert.tex")
52 ("\\.c$" . "c-insert.c")
53 ("\\.h$" . "h-insert.c")
54 ("[Mm]akefile" . "makefile.inc")
55 ("\\.bib$" . "tex-insert.tex"))
56 "A list specifying text to insert by default into a new file.
57 Elements look like (REGEXP . FILENAME); if the new file's name
58 matches REGEXP, then the file FILENAME is inserted into the buffer.
59 Only the first matching element is effective.")
61 ;;; Establish a default value for auto-insert-directory
62 (defvar auto-insert-directory "~/insert/"
63 "*Directory from which auto-inserted files are taken.")
65 (defun insert-auto-insert-files ()
66 "Insert default contents into a new file.
67 Matches the visited file name against the elements of `auto-insert-alist'."
68 (let ((alist auto-insert-alist)
69 ;; remove backup suffixes from file name
70 (name (file-name-sans-versions buffer-file-name))
71 (insert-file nil))
73 ;; find first matching alist entry
74 (while (and (not insert-file) alist)
75 (if (string-match (car (car alist)) name)
76 (setq insert-file (cdr (car alist)))
77 (setq alist (cdr alist))))
79 ;; Now, if we found an appropriate insert file, insert it
80 (if insert-file
81 (let ((file (concat auto-insert-directory insert-file)))
82 (if (file-readable-p file)
83 (progn
84 (insert-file-contents file)
85 (set-buffer-modified-p nil))
86 (message "Auto-insert: file %s not found" file)
87 (sleep-for 1))))))
89 ;; Make this feature take effect when a nonexistent file is visited.
90 (setq find-file-not-found-hooks
91 (cons 'insert-auto-insert-files
92 find-file-not-found-hooks))