*** empty log message ***
[emacs.git] / lisp / autoinsert.el
blobca583bb0780ed9cd49501630c4bb9b3da448f673
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Author: Charlie Martin <crm@cs.duke.edu>
4 ;; Created: 01 Jul 1988
5 ;; Last-Modified: 30 Jun 1992
7 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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.
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.
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;;; Commentary:
27 ;;; Abstract:
28 ;;;
29 ;;; The following defines an association list for files 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 files much as the mode is automatically set using
33 ;;; auto-mode-alist.
34 ;;;
35 ;;; The auto-insert-alist consists of dotted pairs of
36 ;;; ( REGEXP . FILENAME ) where REGEXP is a regular expression, and
37 ;;; FILENAME is the file name of a file which is to be inserted into
38 ;;; all new files matching the regular expression with which it is
39 ;;; paired.
40 ;;;
41 ;;; To use:
42 ;;; load autoinsert.el
43 ;;; setq auto-insert-directory to an appropriate value, which
44 ;;; must end in "/"
45 ;;;
46 ;;; Author: Charlie Martin
47 ;;; Department of Computer Science and
48 ;;; National Biomedical Simulation Resource
49 ;;; Box 3709
50 ;;; Duke University Medical Center
51 ;;; Durham, NC 27710
52 ;;; (crm@cs.duke.edu,mcnc!duke!crm)
53 ;;;
54 ;;; Date: Fri Jul 1 16:15:31 EDT 1988
56 ;;; Code:
58 (defvar auto-insert-alist '(("\\.tex$" . "tex-insert.tex")
59 ("\\.c$" . "c-insert.c")
60 ("\\.h$" . "h-insert.c")
61 ("[Mm]akefile" . "makefile.inc")
62 ("\\.bib$" . "tex-insert.tex"))
63 "A list specifying text to insert by default into a new file.
64 Elements look like (REGEXP . FILENAME); if the new file's name
65 matches REGEXP, then the file FILENAME is inserted into the buffer.
66 Only the first matching element is effective.")
68 ;;; Establish a default value for auto-insert-directory
69 (defvar auto-insert-directory "~/insert/"
70 "*Directory from which auto-inserted files are taken.")
72 (defun insert-auto-insert-files ()
73 "Insert default contents into a new file.
74 Matches the visited file name against the elements of `auto-insert-alist'."
75 (let ((alist auto-insert-alist)
76 ;; remove backup suffixes from file name
77 (name (file-name-sans-versions buffer-file-name))
78 (insert-file nil))
80 ;; find first matching alist entry
81 (while (and (not insert-file) alist)
82 (if (string-match (car (car alist)) name)
83 (setq insert-file (cdr (car alist)))
84 (setq alist (cdr alist))))
86 ;; Now, if we found an appropriate insert file, insert it
87 (if insert-file
88 (let ((file (concat auto-insert-directory insert-file)))
89 (if (file-readable-p file)
90 (progn
91 (insert-file-contents file)
92 (set-buffer-modified-p nil))
93 (message "Auto-insert: file %s not found" file)
94 (sleep-for 1))))))
96 ;; Make this feature take effect when a nonexistent file is visited.
97 (setq find-file-not-found-hooks
98 (cons 'insert-auto-insert-files
99 find-file-not-found-hooks))
101 ;;; autoinsert.el ends here