1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985, 86, 87, 94, 95, 98, 2000 Free Software Foundation, Inc.
5 ;; Author: Charlie Martin <crm@cs.duke.edu>
6 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; The following defines an association list for text 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 text much as the mode is automatically set using
36 ;; (add-hook 'find-file-hooks 'auto-insert)
37 ;; setq auto-insert-directory to an appropriate slash-terminated value
39 ;; You can also customize the variable `auto-insert-mode' to load the
40 ;; package. Alternatively, add the following to your .emacs file:
41 ;; (auto-insert-mode 1)
43 ;; Author: Charlie Martin
44 ;; Department of Computer Science and
45 ;; National Biomedical Simulation Resource
47 ;; Duke University Medical Center
49 ;; (crm@cs.duke.edu,mcnc!duke!crm)
53 (defgroup auto-insert nil
54 "Automatic mode-dependent insertion of text into new files."
55 :prefix
"auto-insert-"
60 (defcustom auto-insert
'not-modified
61 "*Controls automatic insertion into newly found empty files.
65 other insert if possible, but mark as unmodified.
66 Insertion is possible when something appropriate is found in
67 `auto-insert-alist'. When the insertion is marked as unmodified, you can
68 save it with \\[write-file] RET.
69 This variable is used when the function `auto-insert' is called, e.g.
70 when you do (add-hook 'find-file-hooks 'auto-insert).
71 With \\[auto-insert], this is always treated as if it were t."
72 :type
'(choice (const :tag
"Insert if possible" t
)
73 (const :tag
"Do nothing" nil
)
74 (other :tag
"insert if possible, mark as unmodified."
78 (defcustom auto-insert-query
'function
79 "*Non-nil means ask user before auto-inserting.
80 When this is `function', only ask when called non-interactively."
81 :type
'(choice (const :tag
"Don't ask" nil
)
82 (const :tag
"Ask if called non-interactively" function
)
86 (defcustom auto-insert-prompt
"Perform %s auto-insertion? "
87 "*Prompt to use when querying whether to auto-insert.
88 If this contains a %s, that will be replaced by the matching rule."
93 (defcustom auto-insert-alist
94 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" .
"C / C++ header")
95 (upcase (concat (file-name-nondirectory
96 (substring buffer-file-name
0 (match-beginning 0)))
98 (substring buffer-file-name
(1+ (match-beginning 0)))))
100 "#define " str
"\n\n"
103 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" .
"C / C++ program")
106 (let ((stem (file-name-sans-extension buffer-file-name
)))
107 (cond ((file-exists-p (concat stem
".h"))
108 (file-name-nondirectory (concat stem
".h")))
109 ((file-exists-p (concat stem
".hh"))
110 (file-name-nondirectory (concat stem
".hh")))))
113 ("[Mm]akefile\\'" .
"makefile.inc")
115 (html-mode .
(lambda () (sgml-tag "html")))
117 (plain-tex-mode .
"tex-insert.tex")
118 (bibtex-mode .
"tex-insert.tex")
120 ;; should try to offer completing read for these
122 "\\documentclass[" str
& ?\
] | -
1
123 ?
{ (read-string "class: ") "}\n"
125 "\\usepackage[" (read-string "options, RET: ") & ?\
] | -
1 ?
{ str
"}\n")
126 _
"\n\\begin{document}\n" _
129 (("/bin/.*[^/]\\'" .
"Shell-Script mode magic number")
131 (if (eq major-mode default-major-mode
)
134 (ada-mode . ada-header
)
136 (("\\.el\\'" .
"Emacs Lisp header")
137 "Short description: "
138 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
"
140 ;; Copyright (C) " (substring (current-time-string) -
4) " "
141 (getenv "ORGANIZATION") |
"Free Software Foundation, Inc." "
143 ;; Author: " (user-full-name)
144 '(if (search-backward "&" (line-beginning-position) t
)
145 (replace-match (capitalize (user-login-name)) t t
))
146 '(end-of-line 1) " <" (progn user-mail-address
) ">
149 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
150 '(setq v1
(mapcar (lambda (x) (list (symbol-name (car x
))))
151 finder-known-keywords
)
152 v2
(mapconcat (lambda (x) (format "%10.0s: %s" (car x
) (cdr x
)))
153 finder-known-keywords
155 ((let ((minibuffer-help-form v2
))
156 (completing-read "Keyword, C-h: " v1 nil t
))
159 ;; This file is free software; you can redistribute it and/or modify
160 ;; it under the terms of the GNU General Public License as published by
161 ;; the Free Software Foundation; either version 2, or (at your option)
162 ;; any later version.
164 ;; This file is distributed in the hope that it will be useful,
165 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
166 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
167 ;; GNU General Public License for more details.
169 ;; You should have received a copy of the GNU General Public License
170 ;; along with GNU Emacs; see the file COPYING. If not, write to
171 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
172 ;; Boston, MA 02111-1307, USA.
183 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
185 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
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 new files if variable `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 (and buffer-file-name
228 (string-match cond buffer-file-name
)))
229 (setq action
(cdr (car alist
))
231 (setq alist
(cdr alist
))))
233 ;; Now, if we found something, do it
236 (file-readable-p (concat auto-insert-directory action
))
238 (if auto-insert-query
239 (or (if (eq auto-insert-query
'function
)
240 (eq this-command
'auto-insert
))
241 (y-or-n-p (format auto-insert-prompt desc
)))
247 (setq action
(concat auto-insert-directory action
)))
248 (insert-file-contents action
))
249 (save-window-excursion
250 ;; make buffer visible before skeleton or function
251 ;; which might ask the user for something
252 (switch-to-buffer (current-buffer))
253 (if (and (consp action
)
254 (not (eq (car action
) 'lambda
)))
255 (skeleton-insert action
)
260 (and (buffer-modified-p)
261 (not (eq this-command
'auto-insert
))
262 (set-buffer-modified-p (eq auto-insert t
)))))
263 ;; Return nil so that it could be used in
264 ;; `find-file-not-found-hooks', though that's probably inadvisable.
269 (defun define-auto-insert (condition action
&optional after
)
270 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
271 Optional AFTER means to insert action after all existing actions for CONDITION,
272 or if CONDITION had no actions, after all other CONDITIONs."
273 (let ((elt (assoc condition auto-insert-alist
)))
276 (if (vectorp (cdr elt
))
277 (vconcat (if after
(cdr elt
))
278 (if (vectorp action
) action
(vector action
))
279 (if after
() (cdr elt
)))
281 (vector (cdr elt
) action
)
282 (vector action
(cdr elt
)))))
284 (nconc auto-insert-alist
(list (cons condition action
)))
285 (setq auto-insert-alist
(cons (cons condition action
)
286 auto-insert-alist
))))))
289 (define-minor-mode auto-insert-mode
290 "Toggle Auto-insert mode.
291 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
292 Returns the new status of Auto-insert mode (non-nil means on).
294 When Auto-insert mode is enabled, when new files are created you can
295 insert a template for the file depending on the mode of the buffer."
296 :global t
:group
'auto-insert
298 (add-hook 'find-file-hooks
'auto-insert
)
299 (remove-hook 'find-file-hooks
'auto-insert
)))
301 (provide 'autoinsert
)
303 ;;; autoinsert.el ends here