1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985, 86, 87, 94, 95, 98 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-mode nil
61 "Toggle Auto-insert mode.
62 Setting this variable directly does not take effect;
63 use either \\[customize] or the function `auto-insert-mode'."
64 :set
(lambda (symbol value
)
65 (auto-insert-mode (or value
0)))
66 :initialize
'custom-initialize-default
71 (defcustom auto-insert
'not-modified
72 "*Controls automatic insertion into newly found empty files.
76 other insert if possible, but mark as unmodified.
77 Insertion is possible when something appropriate is found in
78 `auto-insert-alist'. When the insertion is marked as unmodified, you can
79 save it with \\[write-file] RET.
80 This variable is used when `auto-insert' is called as a function, e.g.
81 when you do (add-hook 'find-file-hooks 'auto-insert).
82 With \\[auto-insert], this is always treated as if it were t."
83 :type
'(choice (const :tag
"Insert if possible" t
)
84 (const :tag
"Do nothing" nil
)
85 (other :tag
"insert if possible, mark as unmodified."
89 (defcustom auto-insert-query
'function
90 "*Non-nil means ask user before auto-inserting.
91 When this is `function', only ask when called non-interactively."
92 :type
'(choice (const :tag
"Don't ask" nil
)
93 (const :tag
"Ask if called non-interactively" function
)
97 (defcustom auto-insert-prompt
"Perform %s auto-insertion? "
98 "*Prompt to use when querying whether to auto-insert.
99 If this contains a %s, that will be replaced by the matching rule."
104 (defcustom auto-insert-alist
105 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" .
"C / C++ header")
106 (upcase (concat (file-name-nondirectory
107 (substring buffer-file-name
0 (match-beginning 0)))
109 (substring buffer-file-name
(1+ (match-beginning 0)))))
111 "#define " str
"\n\n"
114 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" .
"C / C++ program")
117 ;; nop without latest cc-mode
118 (and (fboundp 'c-companion-file
)
119 ;(file-readable-p (c-companion-file 'name))
120 (file-name-nondirectory (c-companion-file 'name
))) & ?
\"
123 ("[Mm]akefile\\'" .
"makefile.inc")
125 (html-mode .
(lambda () (sgml-tag "html")))
127 (plain-tex-mode .
"tex-insert.tex")
128 (bibtex-mode .
"tex-insert.tex")
130 ;; should try to offer completing read for these
132 "\\documentstyle[" str
& ?\
] | -
1
133 ?
{ (read-string "class: ") "}\n"
135 "\\usepackage[" (read-string "options, RET: ") & ?\
] | -
1 ?
{ str
"}\n")
136 _
"\n\\begin{document}\n" _
139 (("/bin/.*[^/]\\'" .
"Shell-Script mode magic number")
141 (if (eq major-mode default-major-mode
)
144 (ada-mode . ada-header
)
146 (("\\.el\\'" .
"Emacs Lisp header")
147 "Short description: "
148 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
"
150 ;; Copyright (C) " (substring (current-time-string) -
4) " by "
151 (getenv "ORGANIZATION") |
"Free Software Foundation, Inc." "
153 ;; Author: " (user-full-name)
154 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t
)
155 (replace-match (capitalize (user-login-name)) t t
))
156 '(end-of-line 1) " <" (progn user-mail-address
) ">
159 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
160 '(setq v1
(mapcar (lambda (x) (list (symbol-name (car x
))))
161 finder-known-keywords
)
162 v2
(mapconcat (lambda (x) (format "%10.0s: %s" (car x
) (cdr x
)))
163 finder-known-keywords
165 ((let ((minibuffer-help-form v2
))
166 (completing-read "Keyword, C-h: " v1 nil t
))
169 ;; This file is free software; you can redistribute it and/or modify
170 ;; it under the terms of the GNU General Public License as published by
171 ;; the Free Software Foundation; either version 2, or (at your option)
172 ;; any later version.
174 ;; This file is distributed in the hope that it will be useful,
175 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
176 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177 ;; GNU General Public License for more details.
179 ;; You should have received a copy of the GNU General Public License
180 ;; along with GNU Emacs; see the file COPYING. If not, write to
181 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
182 ;; Boston, MA 02111-1307, USA.
192 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
193 "A list specifying text to insert by default into a new file.
194 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
195 CONDITION maybe a regexp that must match the new file's name, or it may be
196 a symbol that must match the major mode for this element to apply.
197 Only the first matching element is effective.
198 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
199 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
200 file-name or one relative to `auto-insert-directory' or a function to call.
201 ACTION may also be a vector containing several successive single actions as
202 described above, e.g. [\"header.insert\" date-and-author-update]."
207 ;; Establish a default value for auto-insert-directory
208 (defcustom auto-insert-directory
"~/insert/"
209 "*Directory from which auto-inserted files are taken."
215 (defun auto-insert ()
216 "Insert default contents into a new file if `auto-insert' is non-nil.
217 Matches the visited file name against the elements of `auto-insert-alist'."
219 (and (not buffer-read-only
)
220 (or (eq this-command
'auto-insert
)
223 (let ((alist auto-insert-alist
)
224 case-fold-search cond desc action
)
226 ;; find first matching alist entry
228 (if (atom (setq cond
(car (car alist
))))
230 (setq desc
(cdr cond
)
232 (if (if (symbolp cond
)
234 (string-match cond buffer-file-name
))
235 (setq action
(cdr (car alist
))
237 (setq alist
(cdr alist
))))
239 ;; Now, if we found something, do it
242 (file-readable-p (concat auto-insert-directory action
))
244 (if auto-insert-query
245 (or (if (eq auto-insert-query
'function
)
246 (eq this-command
'auto-insert
))
247 (y-or-n-p (format auto-insert-prompt desc
)))
253 (setq action
(concat auto-insert-directory action
)))
254 (insert-file-contents action
))
255 (save-window-excursion
256 ;; make buffer visible before skeleton or function
257 ;; which might ask the user for something
258 (switch-to-buffer (current-buffer))
259 (if (and (consp action
)
260 (not (eq (car action
) 'lambda
)))
261 (skeleton-insert action
)
266 (and (buffer-modified-p)
267 (not (eq this-command
'auto-insert
))
268 (set-buffer-modified-p (eq auto-insert t
)))))
269 ;; Return nil so that it could be used in
270 ;; `find-file-not-found-hooks', though that's probably inadvisable.
275 (defun define-auto-insert (condition action
&optional after
)
276 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
277 Optional AFTER means to insert action after all existing actions for CONDITION,
278 or if CONDITION had no actions, after all other CONDITIONs."
279 (let ((elt (assoc condition auto-insert-alist
)))
282 (if (vectorp (cdr elt
))
283 (vconcat (if after
(cdr elt
))
284 (if (vectorp action
) action
(vector action
))
285 (if after
() (cdr elt
)))
287 (vector (cdr elt
) action
)
288 (vector action
(cdr elt
)))))
290 (nconc auto-insert-alist
(list (cons condition action
)))
291 (setq auto-insert-alist
(cons (cons condition action
)
292 auto-insert-alist
))))))
295 (defun auto-insert-mode (&optional arg
)
296 "Toggle Auto-insert mode.
297 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
298 Returns the new status of Auto-insert mode (non-nil means on).
300 When Auto-insert mode is enabled, when new files are created you can
301 insert a template for the file depending on the mode of the buffer."
304 (> (prefix-numeric-value arg
) 0)
305 (not auto-insert-mode
))))
307 (add-hook 'find-file-hooks
'auto-insert
)
308 (remove-hook 'find-file-hooks
'auto-insert
))
310 (message "Auto-insert now %s." (if on-p
"on" "off")))
311 (setq auto-insert-mode on-p
)))
314 (auto-insert-mode 1))
316 (provide 'autoinsert
)
318 ;;; autoinsert.el ends here