*** empty log message ***
[emacs.git] / lisp / autoinsert.el
blobae6e986309782de4db03c78372a128ba5a3b71de
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
8 ;; Maintainer: FSF
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)
15 ;; any later version.
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.
27 ;;; Commentary:
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
33 ;; auto-mode-alist.
35 ;; To use:
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
46 ;; Box 3709
47 ;; Duke University Medical Center
48 ;; Durham, NC 27710
49 ;; (crm@cs.duke.edu,mcnc!duke!crm)
51 ;;; Code:
53 (defgroup auto-insert nil
54 "Automatic mode-dependent insertion of text into new files."
55 :prefix "auto-insert-"
56 :group 'files
57 :group 'convenience)
60 (defcustom auto-insert 'not-modified
61 "*Controls automatic insertion into newly found empty files.
62 Possible values:
63 nil do nothing
64 t insert if possible
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."
75 not-modified))
76 :group 'auto-insert)
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)
83 (other :tag "Ask" t))
84 :group 'auto-insert)
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."
89 :type 'string
90 :group 'auto-insert)
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)))
97 "_"
98 (substring buffer-file-name (1+ (match-beginning 0)))))
99 "#ifndef " str \n
100 "#define " str "\n\n"
101 _ "\n\n#endif")
103 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
105 "#include \""
106 ;; nop without latest cc-mode
107 (and (fboundp 'c-companion-file)
108 ;(file-readable-p (c-companion-file 'name))
109 (file-name-nondirectory (c-companion-file 'name))) & ?\"
110 | -10)
112 ("[Mm]akefile\\'" . "makefile.inc")
114 (html-mode . (lambda () (sgml-tag "html")))
116 (plain-tex-mode . "tex-insert.tex")
117 (bibtex-mode . "tex-insert.tex")
118 (latex-mode
119 ;; should try to offer completing read for these
120 "options, RET: "
121 "\\documentclass[" str & ?\] | -1
122 ?{ (read-string "class: ") "}\n"
123 ("package, %s: "
124 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
125 _ "\n\\begin{document}\n" _
126 "\n\\end{document}")
128 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
129 lambda ()
130 (if (eq major-mode default-major-mode)
131 (sh-mode)))
133 (ada-mode . ada-header)
135 (("\\.el\\'" . "Emacs Lisp header")
136 "Short description: "
137 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
139 ;; Copyright (C) " (substring (current-time-string) -4) " "
140 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
142 ;; Author: " (user-full-name)
143 '(if (search-backward "&" (line-beginning-position) t)
144 (replace-match (capitalize (user-login-name)) t t))
145 '(end-of-line 1) " <" (progn user-mail-address) ">
146 ;; Keywords: "
147 '(require 'finder)
148 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
149 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
150 finder-known-keywords)
151 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
152 finder-known-keywords
153 "\n"))
154 ((let ((minibuffer-help-form v2))
155 (completing-read "Keyword, C-h: " v1 nil t))
156 str ", ") & -2 "
158 ;; This file is free software; you can redistribute it and/or modify
159 ;; it under the terms of the GNU General Public License as published by
160 ;; the Free Software Foundation; either version 2, or (at your option)
161 ;; any later version.
163 ;; This file is distributed in the hope that it will be useful,
164 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
165 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
166 ;; GNU General Public License for more details.
168 ;; You should have received a copy of the GNU General Public License
169 ;; along with GNU Emacs; see the file COPYING. If not, write to
170 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
171 ;; Boston, MA 02111-1307, USA.
173 ;;; Commentary:
175 ;; " _ "
177 ;;; Code:
181 \(provide '"
182 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
184 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
185 "A list specifying text to insert by default into a new file.
186 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
187 CONDITION maybe a regexp that must match the new file's name, or it may be
188 a symbol that must match the major mode for this element to apply.
189 Only the first matching element is effective.
190 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
191 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
192 file-name or one relative to `auto-insert-directory' or a function to call.
193 ACTION may also be a vector containing several successive single actions as
194 described above, e.g. [\"header.insert\" date-and-author-update]."
195 :type 'sexp
196 :group 'auto-insert)
199 ;; Establish a default value for auto-insert-directory
200 (defcustom auto-insert-directory "~/insert/"
201 "*Directory from which auto-inserted files are taken."
202 :type 'directory
203 :group 'auto-insert)
206 ;;;###autoload
207 (defun auto-insert ()
208 "Insert default contents into new files if variable `auto-insert' is non-nil.
209 Matches the visited file name against the elements of `auto-insert-alist'."
210 (interactive)
211 (and (not buffer-read-only)
212 (or (eq this-command 'auto-insert)
213 (and auto-insert
214 (bobp) (eobp)))
215 (let ((alist auto-insert-alist)
216 case-fold-search cond desc action)
217 (goto-char 1)
218 ;; find first matching alist entry
219 (while alist
220 (if (atom (setq cond (car (car alist))))
221 (setq desc cond)
222 (setq desc (cdr cond)
223 cond (car cond)))
224 (if (if (symbolp cond)
225 (eq cond major-mode)
226 (and buffer-file-name
227 (string-match cond buffer-file-name)))
228 (setq action (cdr (car alist))
229 alist nil)
230 (setq alist (cdr alist))))
232 ;; Now, if we found something, do it
233 (and action
234 (if (stringp action)
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)))
242 (mapcar
243 (lambda (action)
244 (if (stringp action)
245 (if (file-readable-p
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)
255 (funcall action)))))
256 (if (vectorp action)
257 action
258 (vector action))))
259 (and (buffer-modified-p)
260 (not (eq this-command 'auto-insert))
261 (set-buffer-modified-p (eq auto-insert t)))))
262 ;; Return nil so that it could be used in
263 ;; `find-file-not-found-hooks', though that's probably inadvisable.
264 nil)
267 ;;;###autoload
268 (defun define-auto-insert (condition action &optional after)
269 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
270 Optional AFTER means to insert action after all existing actions for CONDITION,
271 or if CONDITION had no actions, after all other CONDITIONs."
272 (let ((elt (assoc condition auto-insert-alist)))
273 (if elt
274 (setcdr elt
275 (if (vectorp (cdr elt))
276 (vconcat (if after (cdr elt))
277 (if (vectorp action) action (vector action))
278 (if after () (cdr elt)))
279 (if after
280 (vector (cdr elt) action)
281 (vector action (cdr elt)))))
282 (if after
283 (nconc auto-insert-alist (list (cons condition action)))
284 (setq auto-insert-alist (cons (cons condition action)
285 auto-insert-alist))))))
287 ;;;###autoload
288 (define-minor-mode auto-insert-mode
289 "Toggle Auto-insert mode.
290 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
291 Returns the new status of Auto-insert mode (non-nil means on).
293 When Auto-insert mode is enabled, when new files are created you can
294 insert a template for the file depending on the mode of the buffer."
295 :global t :group 'auto-insert
296 (if auto-insert-mode
297 (add-hook 'find-file-hooks 'auto-insert)
298 (remove-hook 'find-file-hooks 'auto-insert)))
300 (provide 'autoinsert)
302 ;;; autoinsert.el ends here