1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
2 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc.
4 ;; Author: Charlie Martin <crm@cs.duke.edu>
5 ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; The following defines an association list for text 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 text much as the mode is automatically set using
32 ;; (add-hook 'find-file-hooks 'auto-insert)
33 ;; setq auto-insert-directory to an appropriate slash-terminated value
35 ;; Author: Charlie Martin
36 ;; Department of Computer Science and
37 ;; National Biomedical Simulation Resource
39 ;; Duke University Medical Center
41 ;; (crm@cs.duke.edu,mcnc!duke!crm)
45 (defvar auto-insert
'not-modified
46 "*Controls automatic insertion into newly found empty files:
49 other insert if possible, but mark as unmodified.
50 Insertion is possible when something appropriate is found in
51 `auto-insert-alist'. When the insertion is marked as unmodified, you can
52 save it with \\[write-file] RET.
53 This variable is used when `auto-insert' is called as a function, e.g.
54 when you do (add-hook 'find-file-hooks 'auto-insert).
55 With \\[auto-insert], this is always treated as if it were `t'.")
58 (defvar auto-insert-query
'function
59 "*If non-`nil', ask user before auto-inserting.
60 When this is `function', only ask when called non-interactively.")
63 (defvar auto-insert-prompt
"Perform %s auto-insertion? "
64 "*Prompt to use when querying whether to auto-insert.
65 If this contains a %s, that will be replaced by the matching rule.")
68 (defvar auto-insert-alist
69 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" .
"C / C++ header")
70 (upcase (concat (file-name-nondirectory
71 (substring buffer-file-name
0 (match-beginning 0)))
73 (substring buffer-file-name
(1+ (match-beginning 0)))))
78 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" .
"C / C++ program")
81 ;; nop without latest cc-mode
82 (and (fboundp 'c-companion-file
)
83 ;(file-readable-p (c-companion-file 'name))
84 (file-name-nondirectory (c-companion-file 'name
))) & ?
\"
87 ("[Mm]akefile\\'" .
"makefile.inc")
93 "<title>" _
"</title>\n"
99 (plain-tex-mode .
"tex-insert.tex")
100 (bibtex-mode .
"tex-insert.tex")
102 ;; should try to offer completing read for these
104 "\\documentstyle[" str
& ?\
] | -
1
105 ?
{ (read-string "class: ") "}\n"
107 "\\usepackage[" (read-string "options, RET: ") & ?\
] | -
1 ?
{ str
"}\n")
108 _
"\n\\begin{document}\n" _
111 (("/bin/.*[^/]\\'" .
"Shell-Script mode magic number")
113 (if (eq major-mode default-major-mode
)
116 (ada-mode . ada-header
)
118 (("\\.el\\'" .
"Emacs Lisp header")
119 "Short description: "
120 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
"
122 ;; Copyright (C) " (substring (current-time-string) -
4) " by "
123 (getenv "ORGANIZATION") |
"Free Software Foundation, Inc." "
125 ;; Author: " (user-full-name)
126 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t
)
127 (replace-match (capitalize (user-login-name)) t t
))
128 '(end-of-line 1) " <" (user-login-name) ?
@ (system-name) ">
131 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
132 '(setq v1
(mapcar (lambda (x) (list (symbol-name (car x
))))
133 finder-known-keywords
)
134 v2
(mapconcat (lambda (x) (format "%10.0s: %s" (car x
) (cdr x
)))
135 finder-known-keywords
137 ((let ((minibuffer-help-form v2
))
138 (completing-read "Keyword, C-h: " v1 nil t
))
141 ;; This file is part of GNU Emacs.
143 ;; GNU Emacs is free software; you can redistribute it and/or modify
144 ;; it under the terms of the GNU General Public License as published by
145 ;; the Free Software Foundation; either version 2, or (at your option)
146 ;; any later version.
148 ;; GNU Emacs is distributed in the hope that it will be useful,
149 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
150 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
151 ;; GNU General Public License for more details.
153 ;; You should have received a copy of the GNU General Public License
154 ;; along with GNU Emacs; see the file COPYING. If not, write to
155 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
165 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
166 "A list specifying text to insert by default into a new file.
167 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
168 CONDITION maybe a regexp that must match the new file's name, or it may be
169 a symbol that must match the major mode for this element to apply.
170 Only the first matching element is effective.
171 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
172 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
173 file-name or one relative to `auto-insert-directory' or a function to call.
174 ACTION may also be a vector containing several successive single actions as
175 described above, e.g. [\"header.insert\" date-and-author-update].")
178 ;; Establish a default value for auto-insert-directory
179 (defvar auto-insert-directory
"~/insert/"
180 "*Directory from which auto-inserted files are taken.")
184 (defun auto-insert ()
185 "Insert default contents into a new file if `auto-insert' is non-nil.
186 Matches the visited file name against the elements of `auto-insert-alist'."
188 (and (not buffer-read-only
)
189 (or (eq this-command
'auto-insert
)
192 (let ((alist auto-insert-alist
)
193 case-fold-search cond desc action
)
195 ;; find first matching alist entry
197 (if (atom (setq cond
(car (car alist
))))
199 (setq desc
(cdr cond
)
201 (if (if (symbolp cond
)
203 (string-match cond buffer-file-name
))
204 (setq action
(cdr (car alist
))
206 (setq alist
(cdr alist
))))
208 ;; Now, if we found something, do it
211 (file-readable-p (concat auto-insert-directory action
))
213 (if auto-insert-query
214 (or (if (eq auto-insert-query
'function
)
215 (eq this-command
'auto-insert
))
216 (y-or-n-p (format auto-insert-prompt desc
)))
222 (setq action
(concat auto-insert-directory action
)))
223 (insert-file-contents action
))
224 (save-window-excursion
225 ;; make buffer visible before skeleton or function
226 ;; which might ask the user for something
227 (switch-to-buffer (current-buffer))
228 (if (and (consp action
)
229 (not (eq (car action
) 'lambda
)))
230 (skeleton-insert action
)
235 (and (buffer-modified-p)
236 (not (eq this-command
'auto-insert
))
237 (set-buffer-modified-p (eq auto-insert t
))))))
241 (defun define-auto-insert (key action
&optional after
)
242 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
243 Optional AFTER means to insert action after all existing actions for CONDITION,
244 or if CONDITION had no actions, after all other CONDITIONs."
245 (let ((elt (assoc key auto-insert-alist
)))
248 (if (vectorp (cdr elt
))
249 (vconcat (if after
(cdr elt
))
250 (if (vectorp action
) action
(vector action
))
251 (if after
() (cdr elt
)))
253 (vector (cdr elt
) action
)
254 (vector action
(cdr elt
)))))
256 (nconc auto-insert-alist
(list (cons key action
)))
257 (setq auto-insert-alist
(cons (cons key action
)
258 auto-insert-alist
))))))
260 ;;; autoinsert.el ends here