*** empty log message ***
[bpt/emacs.git] / lisp / autoinsert.el
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
2
3 ;; Copyright (C) 1985, 86, 87, 94, 95, 98 Free Software Foundation, Inc.
4
5 ;; Author: Charlie Martin <crm@cs.duke.edu>
6 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: convenience
8 ;; Maintainer: FSF
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
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.
34 ;;
35 ;; To use:
36 ;; (add-hook 'find-file-hooks 'auto-insert)
37 ;; setq auto-insert-directory to an appropriate slash-terminated value
38 ;;
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)
42 ;;
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)
50
51 ;;; Code:
52
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)
58
59
60 (defcustom auto-insert-mode nil
61 "Toggle auto-insert-mode.
62 This variable should be set only with \\[customize], which is equivalent
63 to using the function `auto-insert-mode'."
64 :set (lambda (symbol value)
65 (auto-insert-mode (or value 0)))
66 :initialize 'custom-initialize-default
67 :type 'boolean
68 :group 'auto-insert
69 :require 'autoinsert)
70
71 (defcustom auto-insert 'not-modified
72 "*Controls automatic insertion into newly found empty files:
73 nil do nothing
74 t insert if possible
75 other insert if possible, but mark as unmodified.
76 Insertion is possible when something appropriate is found in
77 `auto-insert-alist'. When the insertion is marked as unmodified, you can
78 save it with \\[write-file] RET.
79 This variable is used when `auto-insert' is called as a function, e.g.
80 when you do (add-hook 'find-file-hooks 'auto-insert).
81 With \\[auto-insert], this is always treated as if it were `t'."
82 :type '(choice (const :tag "Insert if possible" t)
83 (const :tag "Do nothing" nil)
84 (other :tag "insert if possible, mark as unmodified."
85 not-modified))
86 :group 'auto-insert)
87
88 (defcustom auto-insert-query 'function
89 "*If non-`nil', ask user before auto-inserting.
90 When this is `function', only ask when called non-interactively."
91 :type '(choice (const :tag "Don't ask" nil)
92 (const :tag "Ask if called non-interactively" function)
93 (other :tag "Ask" t))
94 :group 'auto-insert)
95
96 (defcustom auto-insert-prompt "Perform %s auto-insertion? "
97 "*Prompt to use when querying whether to auto-insert.
98 If this contains a %s, that will be replaced by the matching rule."
99 :type 'string
100 :group 'auto-insert)
101
102
103 (defcustom auto-insert-alist
104 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
105 (upcase (concat (file-name-nondirectory
106 (substring buffer-file-name 0 (match-beginning 0)))
107 "_"
108 (substring buffer-file-name (1+ (match-beginning 0)))))
109 "#ifndef " str \n
110 "#define " str "\n\n"
111 _ "\n\n#endif")
112
113 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
114 nil
115 "#include \""
116 ;; nop without latest cc-mode
117 (and (fboundp 'c-companion-file)
118 ;(file-readable-p (c-companion-file 'name))
119 (file-name-nondirectory (c-companion-file 'name))) & ?\"
120 | -10)
121
122 ("[Mm]akefile\\'" . "makefile.inc")
123
124 (html-mode . (lambda () (sgml-tag "html")))
125
126 (plain-tex-mode . "tex-insert.tex")
127 (bibtex-mode . "tex-insert.tex")
128 (latex-mode
129 ;; should try to offer completing read for these
130 "options, RET: "
131 "\\documentstyle[" str & ?\] | -1
132 ?{ (read-string "class: ") "}\n"
133 ("package, %s: "
134 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
135 _ "\n\\begin{document}\n" _
136 "\n\\end{document}")
137
138 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
139 lambda ()
140 (if (eq major-mode default-major-mode)
141 (sh-mode)))
142
143 (ada-mode . ada-header)
144
145 (("\\.el\\'" . "Emacs Lisp header")
146 "Short description: "
147 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
148
149 ;; Copyright (C) " (substring (current-time-string) -4) " by "
150 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
151
152 ;; Author: " (user-full-name)
153 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t)
154 (replace-match (capitalize (user-login-name)) t t))
155 '(end-of-line 1) " <" (progn user-mail-address) ">
156 ;; Keywords: "
157 '(require 'finder)
158 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
159 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
160 finder-known-keywords)
161 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
162 finder-known-keywords
163 "\n"))
164 ((let ((minibuffer-help-form v2))
165 (completing-read "Keyword, C-h: " v1 nil t))
166 str ", ") & -2 "
167
168 ;; This file is free software; you can redistribute it and/or modify
169 ;; it under the terms of the GNU General Public License as published by
170 ;; the Free Software Foundation; either version 2, or (at your option)
171 ;; any later version.
172
173 ;; This file is distributed in the hope that it will be useful,
174 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
175 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
176 ;; GNU General Public License for more details.
177
178 ;; You should have received a copy of the GNU General Public License
179 ;; along with GNU Emacs; see the file COPYING. If not, write to
180 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
181 ;; Boston, MA 02111-1307, USA.
182
183 ;;; Commentary:
184
185 ;; " _ "
186
187 ;;; Code:
188
189
190
191 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
192 "A list specifying text to insert by default into a new file.
193 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
194 CONDITION maybe a regexp that must match the new file's name, or it may be
195 a symbol that must match the major mode for this element to apply.
196 Only the first matching element is effective.
197 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
198 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
199 file-name or one relative to `auto-insert-directory' or a function to call.
200 ACTION may also be a vector containing several successive single actions as
201 described above, e.g. [\"header.insert\" date-and-author-update]."
202 :type 'sexp
203 :group 'auto-insert)
204
205
206 ;; Establish a default value for auto-insert-directory
207 (defcustom auto-insert-directory "~/insert/"
208 "*Directory from which auto-inserted files are taken."
209 :type 'directory
210 :group 'auto-insert)
211
212
213 ;;;###autoload
214 (defun auto-insert ()
215 "Insert default contents into a new file if `auto-insert' is non-nil.
216 Matches the visited file name against the elements of `auto-insert-alist'."
217 (interactive)
218 (and (not buffer-read-only)
219 (or (eq this-command 'auto-insert)
220 (and auto-insert
221 (bobp) (eobp)))
222 (let ((alist auto-insert-alist)
223 case-fold-search cond desc action)
224 (goto-char 1)
225 ;; find first matching alist entry
226 (while alist
227 (if (atom (setq cond (car (car alist))))
228 (setq desc cond)
229 (setq desc (cdr cond)
230 cond (car cond)))
231 (if (if (symbolp cond)
232 (eq cond major-mode)
233 (string-match cond buffer-file-name))
234 (setq action (cdr (car alist))
235 alist nil)
236 (setq alist (cdr alist))))
237
238 ;; Now, if we found something, do it
239 (and action
240 (if (stringp action)
241 (file-readable-p (concat auto-insert-directory action))
242 t)
243 (if auto-insert-query
244 (or (if (eq auto-insert-query 'function)
245 (eq this-command 'auto-insert))
246 (y-or-n-p (format auto-insert-prompt desc)))
247 t)
248 (mapcar
249 (lambda (action)
250 (if (stringp action)
251 (if (file-readable-p
252 (setq action (concat auto-insert-directory action)))
253 (insert-file-contents action))
254 (save-window-excursion
255 ;; make buffer visible before skeleton or function
256 ;; which might ask the user for something
257 (switch-to-buffer (current-buffer))
258 (if (and (consp action)
259 (not (eq (car action) 'lambda)))
260 (skeleton-insert action)
261 (funcall action)))))
262 (if (vectorp action)
263 action
264 (vector action))))
265 (and (buffer-modified-p)
266 (not (eq this-command 'auto-insert))
267 (set-buffer-modified-p (eq auto-insert t))))))
268
269
270 ;;;###autoload
271 (defun define-auto-insert (condition action &optional after)
272 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
273 Optional AFTER means to insert action after all existing actions for CONDITION,
274 or if CONDITION had no actions, after all other CONDITIONs."
275 (let ((elt (assoc condition auto-insert-alist)))
276 (if elt
277 (setcdr elt
278 (if (vectorp (cdr elt))
279 (vconcat (if after (cdr elt))
280 (if (vectorp action) action (vector action))
281 (if after () (cdr elt)))
282 (if after
283 (vector (cdr elt) action)
284 (vector action (cdr elt)))))
285 (if after
286 (nconc auto-insert-alist (list (cons condition action)))
287 (setq auto-insert-alist (cons (cons condition action)
288 auto-insert-alist))))))
289
290 ;;;###autoload
291 (defun auto-insert-mode (&optional arg)
292 "Toggle auto-insert mode.
293 With prefix ARG, turn auto-insert mode on if and only if ARG is positive.
294 Returns the new status of auto-insert mode (non-nil means on).
295
296 When auto-insert mode is enabled, when new files are created you can
297 insert a template for the file depending on the mode of the buffer."
298 (interactive "P")
299 (let ((on-p (if arg
300 (> (prefix-numeric-value arg) 0)
301 (not auto-insert-mode))))
302 (if on-p
303 (add-hook 'find-file-hooks 'auto-insert)
304 (remove-hook 'find-file-hooks 'auto-insert))
305 (if (interactive-p)
306 (message "Auto-insert now %s." (if on-p "on" "off")))
307 (setq auto-insert-mode on-p)
308 ))
309
310 (if auto-insert-mode
311 (auto-insert-mode 1))
312
313 (provide 'autoinsert)
314
315 ;;; autoinsert.el ends here