]> code.delx.au - gnu-emacs/blob - lisp/autoinsert.el
(tar-header-block-tokenize): Decode codes of file
[gnu-emacs] / 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@Informatik.START.dbp.de, fax (+49 69) 7588-2389
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 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
66 :type 'boolean
67 :group 'auto-insert
68 :require 'autoinsert)
69
70 (defcustom auto-insert 'not-modified
71 "*Controls automatic insertion into newly found empty files:
72 nil do nothing
73 t insert if possible
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."
84 not-modified))
85 :group 'auto-insert)
86
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)
92 (other :tag "Ask" t))
93 :group 'auto-insert)
94
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."
98 :type 'string
99 :group 'auto-insert)
100
101
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)))
106 "_"
107 (substring buffer-file-name (1+ (match-beginning 0)))))
108 "#ifndef " str \n
109 "#define " str "\n\n"
110 _ "\n\n#endif")
111
112 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
113 nil
114 "#include \""
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))) & ?\"
119 | -10)
120
121 ("[Mm]akefile\\'" . "makefile.inc")
122
123 (html-mode . (lambda () (sgml-tag "html")))
124
125 (plain-tex-mode . "tex-insert.tex")
126 (bibtex-mode . "tex-insert.tex")
127 (latex-mode
128 ;; should try to offer completing read for these
129 "options, RET: "
130 "\\documentstyle[" str & ?\] | -1
131 ?{ (read-string "class: ") "}\n"
132 ("package, %s: "
133 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
134 _ "\n\\begin{document}\n" _
135 "\n\\end{document}")
136
137 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
138 lambda ()
139 (if (eq major-mode default-major-mode)
140 (sh-mode)))
141
142 (ada-mode . ada-header)
143
144 (("\\.el\\'" . "Emacs Lisp header")
145 "Short description: "
146 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
147
148 ;; Copyright (C) " (substring (current-time-string) -4) " by "
149 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
150
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) ">
155 ;; Keywords: "
156 '(require 'finder)
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
162 "\n"))
163 ((let ((minibuffer-help-form v2))
164 (completing-read "Keyword, C-h: " v1 nil t))
165 str ", ") & -2 "
166
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.
171
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.
176
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.
181
182 ;;; Commentary:
183
184 ;; " _ "
185
186 ;;; Code:
187
188
189
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]."
201 :type 'sexp
202 :group 'auto-insert)
203
204
205 ;; Establish a default value for auto-insert-directory
206 (defcustom auto-insert-directory "~/insert/"
207 "*Directory from which auto-inserted files are taken."
208 :type 'directory
209 :group 'auto-insert)
210
211
212 ;;;###autoload
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'."
216 (interactive)
217 (and (not buffer-read-only)
218 (or (eq this-command 'auto-insert)
219 (and auto-insert
220 (bobp) (eobp)))
221 (let ((alist auto-insert-alist)
222 case-fold-search cond desc action)
223 (goto-char 1)
224 ;; find first matching alist entry
225 (while alist
226 (if (atom (setq cond (car (car alist))))
227 (setq desc cond)
228 (setq desc (cdr cond)
229 cond (car cond)))
230 (if (if (symbolp cond)
231 (eq cond major-mode)
232 (string-match cond buffer-file-name))
233 (setq action (cdr (car alist))
234 alist nil)
235 (setq alist (cdr alist))))
236
237 ;; Now, if we found something, do it
238 (and action
239 (if (stringp action)
240 (file-readable-p (concat auto-insert-directory action))
241 t)
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)))
246 t)
247 (mapcar
248 (lambda (action)
249 (if (stringp action)
250 (if (file-readable-p
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)
260 (funcall action)))))
261 (if (vectorp action)
262 action
263 (vector action))))
264 (and (buffer-modified-p)
265 (not (eq this-command 'auto-insert))
266 (set-buffer-modified-p (eq auto-insert t))))))
267
268
269 ;;;###autoload
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)))
275 (if elt
276 (setcdr elt
277 (if (vectorp (cdr elt))
278 (vconcat (if after (cdr elt))
279 (if (vectorp action) action (vector action))
280 (if after () (cdr elt)))
281 (if after
282 (vector (cdr elt) action)
283 (vector action (cdr elt)))))
284 (if after
285 (nconc auto-insert-alist (list (cons condition action)))
286 (setq auto-insert-alist (cons (cons condition action)
287 auto-insert-alist))))))
288
289 ;;;###autoload
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).
294
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."
297 (interactive "P")
298 (let ((on-p (if arg
299 (> (prefix-numeric-value arg) 0)
300 (not auto-insert-mode))))
301 (if on-p
302 (add-hook 'find-file-hooks 'auto-insert)
303 (remove-hook 'find-file-hooks 'auto-insert))
304 (if (interactive-p)
305 (message "Auto-insert now %s." (if on-p "on" "off")))
306 (setq auto-insert-mode on-p)
307 ))
308
309 (if auto-insert-mode
310 (auto-insert-mode 1))
311
312 (provide 'autoinsert)
313
314 ;;; autoinsert.el ends here