1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Charlie Martin <crm@cs.duke.edu>
6 ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
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)
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The following defines an association list for text to be
29 ;; automatically inserted when a new file is created, and a function
30 ;; which automatically inserts these files; the idea is to insert
31 ;; default text much as the mode is automatically set using
35 ;; (add-hook 'find-file-hooks 'auto-insert)
36 ;; setq auto-insert-directory to an appropriate slash-terminated value
38 ;; You can also customize the variable `auto-insert-mode' to load the
39 ;; package. Alternatively, add the following to your .emacs file:
40 ;; (auto-insert-mode 1)
42 ;; Author: Charlie Martin
43 ;; Department of Computer Science and
44 ;; National Biomedical Simulation Resource
46 ;; Duke University Medical Center
48 ;; (crm@cs.duke.edu,mcnc!duke!crm)
52 (defgroup auto-insert nil
53 "Automatic mode-dependent insertion of text into new files."
54 :prefix
"auto-insert-"
58 (defcustom auto-insert-mode nil
59 "Toggle auto-insert-mode.
60 You must modify via \\[customize] for this variable to have an effect."
61 :set
(lambda (symbol value
)
62 (auto-insert-mode (or value
0)))
63 :initialize
'custom-initialize-default
68 (defcustom auto-insert
'not-modified
69 "*Controls automatic insertion into newly found empty files:
72 other insert if possible, but mark as unmodified.
73 Insertion is possible when something appropriate is found in
74 `auto-insert-alist'. When the insertion is marked as unmodified, you can
75 save it with \\[write-file] RET.
76 This variable is used when `auto-insert' is called as a function, e.g.
77 when you do (add-hook 'find-file-hooks 'auto-insert).
78 With \\[auto-insert], this is always treated as if it were `t'."
79 :type
'(choice (const t
) (const nil
) (const not-modified
))
82 (defcustom auto-insert-query
'function
83 "*If non-`nil', ask user before auto-inserting.
84 When this is `function', only ask when called non-interactively."
85 :type
'(choice (const t
) (const nil
) (const function
))
88 (defcustom auto-insert-prompt
"Perform %s auto-insertion? "
89 "*Prompt to use when querying whether to auto-insert.
90 If this contains a %s, that will be replaced by the matching rule."
95 (defcustom auto-insert-alist
96 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" .
"C / C++ header")
97 (upcase (concat (file-name-nondirectory
98 (substring buffer-file-name
0 (match-beginning 0)))
100 (substring buffer-file-name
(1+ (match-beginning 0)))))
102 "#define " str
"\n\n"
105 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" .
"C / C++ program")
108 ;; nop without latest cc-mode
109 (and (fboundp 'c-companion-file
)
110 ;(file-readable-p (c-companion-file 'name))
111 (file-name-nondirectory (c-companion-file 'name
))) & ?
\"
114 ("[Mm]akefile\\'" .
"makefile.inc")
116 (html-mode .
(lambda () (sgml-tag "html")))
118 (plain-tex-mode .
"tex-insert.tex")
119 (bibtex-mode .
"tex-insert.tex")
121 ;; should try to offer completing read for these
123 "\\documentstyle[" str
& ?\
] | -
1
124 ?
{ (read-string "class: ") "}\n"
126 "\\usepackage[" (read-string "options, RET: ") & ?\
] | -
1 ?
{ str
"}\n")
127 _
"\n\\begin{document}\n" _
130 (("/bin/.*[^/]\\'" .
"Shell-Script mode magic number")
132 (if (eq major-mode default-major-mode
)
135 (ada-mode . ada-header
)
137 (("\\.el\\'" .
"Emacs Lisp header")
138 "Short description: "
139 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
"
141 ;; Copyright (C) " (substring (current-time-string) -
4) " by "
142 (getenv "ORGANIZATION") |
"Free Software Foundation, Inc." "
144 ;; Author: " (user-full-name)
145 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t
)
146 (replace-match (capitalize (user-login-name)) t t
))
147 '(end-of-line 1) " <" (user-login-name) ?
@ (system-name) ">
150 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
151 '(setq v1
(mapcar (lambda (x) (list (symbol-name (car x
))))
152 finder-known-keywords
)
153 v2
(mapconcat (lambda (x) (format "%10.0s: %s" (car x
) (cdr x
)))
154 finder-known-keywords
156 ((let ((minibuffer-help-form v2
))
157 (completing-read "Keyword, C-h: " v1 nil t
))
160 ;; This file is part of GNU Emacs.
162 ;; GNU Emacs is free software; you can redistribute it and/or modify
163 ;; it under the terms of the GNU General Public License as published by
164 ;; the Free Software Foundation; either version 2, or (at your option)
165 ;; any later version.
167 ;; GNU Emacs is distributed in the hope that it will be useful,
168 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
169 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
170 ;; GNU General Public License for more details.
172 ;; You should have received a copy of the GNU General Public License
173 ;; along with GNU Emacs; see the file COPYING. If not, write to
174 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
175 ;; Boston, MA 02111-1307, USA.
185 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
186 "A list specifying text to insert by default into a new file.
187 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
188 CONDITION maybe a regexp that must match the new file's name, or it may be
189 a symbol that must match the major mode for this element to apply.
190 Only the first matching element is effective.
191 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
192 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
193 file-name or one relative to `auto-insert-directory' or a function to call.
194 ACTION may also be a vector containing several successive single actions as
195 described above, e.g. [\"header.insert\" date-and-author-update]."
200 ;; Establish a default value for auto-insert-directory
201 (defcustom auto-insert-directory
"~/insert/"
202 "*Directory from which auto-inserted files are taken."
208 (defun auto-insert ()
209 "Insert default contents into a new file if `auto-insert' is non-nil.
210 Matches the visited file name against the elements of `auto-insert-alist'."
212 (and (not buffer-read-only
)
213 (or (eq this-command
'auto-insert
)
216 (let ((alist auto-insert-alist
)
217 case-fold-search cond desc action
)
219 ;; find first matching alist entry
221 (if (atom (setq cond
(car (car alist
))))
223 (setq desc
(cdr cond
)
225 (if (if (symbolp cond
)
227 (string-match cond buffer-file-name
))
228 (setq action
(cdr (car alist
))
230 (setq alist
(cdr alist
))))
232 ;; Now, if we found something, do it
235 (file-readable-p (concat auto-insert-directory action
))
237 (if auto-insert-query
238 (or (if (eq auto-insert-query
'function
)
239 (eq this-command
'auto-insert
))
240 (y-or-n-p (format auto-insert-prompt desc
)))
246 (setq action
(concat auto-insert-directory action
)))
247 (insert-file-contents action
))
248 (save-window-excursion
249 ;; make buffer visible before skeleton or function
250 ;; which might ask the user for something
251 (switch-to-buffer (current-buffer))
252 (if (and (consp action
)
253 (not (eq (car action
) 'lambda
)))
254 (skeleton-insert action
)
259 (and (buffer-modified-p)
260 (not (eq this-command
'auto-insert
))
261 (set-buffer-modified-p (eq auto-insert t
))))))
265 (defun define-auto-insert (key action
&optional after
)
266 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
267 Optional AFTER means to insert action after all existing actions for CONDITION,
268 or if CONDITION had no actions, after all other CONDITIONs."
269 (let ((elt (assoc key auto-insert-alist
)))
272 (if (vectorp (cdr elt
))
273 (vconcat (if after
(cdr elt
))
274 (if (vectorp action
) action
(vector action
))
275 (if after
() (cdr elt
)))
277 (vector (cdr elt
) action
)
278 (vector action
(cdr elt
)))))
280 (nconc auto-insert-alist
(list (cons key action
)))
281 (setq auto-insert-alist
(cons (cons key action
)
282 auto-insert-alist
))))))
285 (defun auto-insert-mode (&optional arg
)
286 "Toggle auto-insert mode.
287 With prefix ARG, turn auto-insert mode on if and only if ARG is positive.
288 Returns the new status of auto-insert mode (non-nil means on).
290 When auto-insert mode is enabled, when new files are created you can
291 insert a template for the file depending on the mode of the buffer."
294 (> (prefix-numeric-value arg
) 0)
295 (not auto-insert-mode
))))
297 (add-hook 'find-file-hooks
'auto-insert
)
298 (remove-hook 'find-file-hooks
'auto-insert
))
300 (message "Auto-insert now %s." (if on-p
"on" "off")))
301 (setq auto-insert-mode on-p
)
305 (auto-insert-mode 1))
307 (provide 'autoinsert
)
309 ;;; autoinsert.el ends here