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