]> code.delx.au - gnu-emacs/blob - lisp/autoinsert.el
(ange-ftp-file-name-sans-versions): Don't return
[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 part of GNU Emacs.
168
169 ;; GNU Emacs 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.
173
174 ;; GNU Emacs 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.
178
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.
183
184 ;;; Commentary:
185
186 ;; " _ "
187
188 ;;; Code:
189
190
191
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]."
203 :type 'sexp
204 :group 'auto-insert)
205
206
207 ;; Establish a default value for auto-insert-directory
208 (defcustom auto-insert-directory "~/insert/"
209 "*Directory from which auto-inserted files are taken."
210 :type 'directory
211 :group 'auto-insert)
212
213
214 ;;;###autoload
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'."
218 (interactive)
219 (and (not buffer-read-only)
220 (or (eq this-command 'auto-insert)
221 (and auto-insert
222 (bobp) (eobp)))
223 (let ((alist auto-insert-alist)
224 case-fold-search cond desc action)
225 (goto-char 1)
226 ;; find first matching alist entry
227 (while alist
228 (if (atom (setq cond (car (car alist))))
229 (setq desc cond)
230 (setq desc (cdr cond)
231 cond (car cond)))
232 (if (if (symbolp cond)
233 (eq cond major-mode)
234 (string-match cond buffer-file-name))
235 (setq action (cdr (car alist))
236 alist nil)
237 (setq alist (cdr alist))))
238
239 ;; Now, if we found something, do it
240 (and action
241 (if (stringp action)
242 (file-readable-p (concat auto-insert-directory action))
243 t)
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)))
248 t)
249 (mapcar
250 (lambda (action)
251 (if (stringp action)
252 (if (file-readable-p
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)
262 (funcall action)))))
263 (if (vectorp action)
264 action
265 (vector action))))
266 (and (buffer-modified-p)
267 (not (eq this-command 'auto-insert))
268 (set-buffer-modified-p (eq auto-insert t))))))
269
270
271 ;;;###autoload
272 (defun define-auto-insert (condition action &optional after)
273 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
274 Optional AFTER means to insert action after all existing actions for CONDITION,
275 or if CONDITION had no actions, after all other CONDITIONs."
276 (let ((elt (assoc condition auto-insert-alist)))
277 (if elt
278 (setcdr elt
279 (if (vectorp (cdr elt))
280 (vconcat (if after (cdr elt))
281 (if (vectorp action) action (vector action))
282 (if after () (cdr elt)))
283 (if after
284 (vector (cdr elt) action)
285 (vector action (cdr elt)))))
286 (if after
287 (nconc auto-insert-alist (list (cons condition action)))
288 (setq auto-insert-alist (cons (cons condition action)
289 auto-insert-alist))))))
290
291 ;;;###autoload
292 (defun auto-insert-mode (&optional arg)
293 "Toggle auto-insert mode.
294 With prefix ARG, turn auto-insert mode on if and only if ARG is positive.
295 Returns the new status of auto-insert mode (non-nil means on).
296
297 When auto-insert mode is enabled, when new files are created you can
298 insert a template for the file depending on the mode of the buffer."
299 (interactive "P")
300 (let ((on-p (if arg
301 (> (prefix-numeric-value arg) 0)
302 (not auto-insert-mode))))
303 (if on-p
304 (add-hook 'find-file-hooks 'auto-insert)
305 (remove-hook 'find-file-hooks 'auto-insert))
306 (if (interactive-p)
307 (message "Auto-insert now %s." (if on-p "on" "off")))
308 (setq auto-insert-mode on-p)
309 ))
310
311 (if auto-insert-mode
312 (auto-insert-mode 1))
313
314 (provide 'autoinsert)
315
316 ;;; autoinsert.el ends here