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@Informatik.START.dbp.de, fax (+49 69) 7588-2389
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 You must modify via \\[customize] for this variable to have an effect."
63 :set (lambda (symbol value)
64 (auto-insert-mode (or value 0)))
65 :initialize 'custom-initialize-default
70 (defcustom auto-insert 'not-modified
71 "*Controls automatic insertion into newly found empty files:
74 other insert if possible, but mark as unmodified.
75 Insertion is possible when something appropriate is found in
76 `auto-insert-alist'. When the insertion is marked as unmodified, you can
77 save it with \\[write-file] RET.
78 This variable is used when `auto-insert' is called as a function, e.g.
79 when you do (add-hook 'find-file-hooks 'auto-insert).
80 With \\[auto-insert], this is always treated as if it were `t'."
81 :type '(choice (const :tag "Insert if possible" t)
82 (const :tag "Do nothing" nil)
83 (other :tag "insert if possible, mark as unmodified."
87 (defcustom auto-insert-query 'function
88 "*If non-`nil', ask user before auto-inserting.
89 When this is `function', only ask when called non-interactively."
90 :type '(choice (const :tag "Don't ask" nil)
91 (const :tag "Ask if called non-interactively" function)
95 (defcustom auto-insert-prompt "Perform %s auto-insertion? "
96 "*Prompt to use when querying whether to auto-insert.
97 If this contains a %s, that will be replaced by the matching rule."
102 (defcustom auto-insert-alist
103 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
104 (upcase (concat (file-name-nondirectory
105 (substring buffer-file-name 0 (match-beginning 0)))
107 (substring buffer-file-name (1+ (match-beginning 0)))))
109 "#define " str "\n\n"
112 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
115 ;; nop without latest cc-mode
116 (and (fboundp 'c-companion-file)
117 ;(file-readable-p (c-companion-file 'name))
118 (file-name-nondirectory (c-companion-file 'name))) & ?\"
121 ("[Mm]akefile\\'" . "makefile.inc")
123 (html-mode . (lambda () (sgml-tag "html")))
125 (plain-tex-mode . "tex-insert.tex")
126 (bibtex-mode . "tex-insert.tex")
128 ;; should try to offer completing read for these
130 "\\documentstyle[" str & ?\] | -1
131 ?{ (read-string "class: ") "}\n"
133 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
134 _ "\n\\begin{document}\n" _
137 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
139 (if (eq major-mode default-major-mode)
142 (ada-mode . ada-header)
144 (("\\.el\\'" . "Emacs Lisp header")
145 "Short description: "
146 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
148 ;; Copyright (C) " (substring (current-time-string) -4) " by "
149 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
151 ;; Author: " (user-full-name)
152 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t)
153 (replace-match (capitalize (user-login-name)) t t))
154 '(end-of-line 1) " <" (user-login-name) ?@ (system-name) ">
157 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
158 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
159 finder-known-keywords)
160 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
161 finder-known-keywords
163 ((let ((minibuffer-help-form v2))
164 (completing-read "Keyword, C-h: " v1 nil t))
167 ;; This file is free software; you can redistribute it and/or modify
168 ;; it under the terms of the GNU General Public License as published by
169 ;; the Free Software Foundation; either version 2, or (at your option)
170 ;; any later version.
172 ;; This file is distributed in the hope that it will be useful,
173 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
174 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175 ;; GNU General Public License for more details.
177 ;; You should have received a copy of the GNU General Public License
178 ;; along with GNU Emacs; see the file COPYING. If not, write to
179 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
180 ;; Boston, MA 02111-1307, USA.
190 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
191 "A list specifying text to insert by default into a new file.
192 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
193 CONDITION maybe a regexp that must match the new file's name, or it may be
194 a symbol that must match the major mode for this element to apply.
195 Only the first matching element is effective.
196 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
197 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
198 file-name or one relative to `auto-insert-directory' or a function to call.
199 ACTION may also be a vector containing several successive single actions as
200 described above, e.g. [\"header.insert\" date-and-author-update]."
205 ;; Establish a default value for auto-insert-directory
206 (defcustom auto-insert-directory "~/insert/"
207 "*Directory from which auto-inserted files are taken."
213 (defun auto-insert ()
214 "Insert default contents into a new file if `auto-insert' is non-nil.
215 Matches the visited file name against the elements of `auto-insert-alist'."
217 (and (not buffer-read-only)
218 (or (eq this-command 'auto-insert)
221 (let ((alist auto-insert-alist)
222 case-fold-search cond desc action)
224 ;; find first matching alist entry
226 (if (atom (setq cond (car (car alist))))
228 (setq desc (cdr cond)
230 (if (if (symbolp cond)
232 (string-match cond buffer-file-name))
233 (setq action (cdr (car alist))
235 (setq alist (cdr alist))))
237 ;; Now, if we found something, do it
240 (file-readable-p (concat auto-insert-directory action))
242 (if auto-insert-query
243 (or (if (eq auto-insert-query 'function)
244 (eq this-command 'auto-insert))
245 (y-or-n-p (format auto-insert-prompt desc)))
251 (setq action (concat auto-insert-directory action)))
252 (insert-file-contents action))
253 (save-window-excursion
254 ;; make buffer visible before skeleton or function
255 ;; which might ask the user for something
256 (switch-to-buffer (current-buffer))
257 (if (and (consp action)
258 (not (eq (car action) 'lambda)))
259 (skeleton-insert action)
264 (and (buffer-modified-p)
265 (not (eq this-command 'auto-insert))
266 (set-buffer-modified-p (eq auto-insert t))))))
270 (defun define-auto-insert (condition action &optional after)
271 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
272 Optional AFTER means to insert action after all existing actions for CONDITION,
273 or if CONDITION had no actions, after all other CONDITIONs."
274 (let ((elt (assoc condition auto-insert-alist)))
277 (if (vectorp (cdr elt))
278 (vconcat (if after (cdr elt))
279 (if (vectorp action) action (vector action))
280 (if after () (cdr elt)))
282 (vector (cdr elt) action)
283 (vector action (cdr elt)))))
285 (nconc auto-insert-alist (list (cons condition action)))
286 (setq auto-insert-alist (cons (cons condition action)
287 auto-insert-alist))))))
290 (defun auto-insert-mode (&optional arg)
291 "Toggle auto-insert mode.
292 With prefix ARG, turn auto-insert mode on if and only if ARG is positive.
293 Returns the new status of auto-insert mode (non-nil means on).
295 When auto-insert mode is enabled, when new files are created you can
296 insert a template for the file depending on the mode of the buffer."
299 (> (prefix-numeric-value arg) 0)
300 (not auto-insert-mode))))
302 (add-hook 'find-file-hooks 'auto-insert)
303 (remove-hook 'find-file-hooks 'auto-insert))
305 (message "Auto-insert now %s." (if on-p "on" "off")))
306 (setq auto-insert-mode on-p)
310 (auto-insert-mode 1))
312 (provide 'autoinsert)
314 ;;; autoinsert.el ends here