]> code.delx.au - gnu-emacs/blob - lisp/autoinsert.el
* net/tramp.el (tramp-ssh-controlmaster-template): Make it a
[gnu-emacs] / lisp / autoinsert.el
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
2
3 ;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2013 Free Software
4 ;; Foundation, Inc.
5
6 ;; Author: Charlie Martin <crm@cs.duke.edu>
7 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: convenience
9 ;; Maintainer: FSF
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; The following defines an association list for text to be
29 ;; automatically inserted when a new file is created, and a function
30 ;; which automatically inserts these files; the idea is to insert
31 ;; default text much as the mode is automatically set using
32 ;; auto-mode-alist.
33 ;;
34 ;; To use:
35 ;; (add-hook 'find-file-hook 'auto-insert)
36 ;; setq auto-insert-directory to an appropriate slash-terminated value
37 ;;
38 ;; You can also customize the variable `auto-insert-mode' to load the
39 ;; package. Alternatively, add the following to your init file:
40 ;; (auto-insert-mode 1)
41 ;;
42 ;; Author: Charlie Martin
43 ;; Department of Computer Science and
44 ;; National Biomedical Simulation Resource
45 ;; Box 3709
46 ;; Duke University Medical Center
47 ;; Durham, NC 27710
48 ;; (crm@cs.duke.edu,mcnc!duke!crm)
49
50 ;;; Code:
51
52 (defgroup auto-insert nil
53 "Automatic mode-dependent insertion of text into new files."
54 :prefix "auto-insert-"
55 :group 'files
56 :group 'convenience
57 :link '(custom-manual "(autotype) Autoinserting"))
58
59
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-hook '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)
77
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)
85
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)
91
92
93 (defcustom auto-insert-alist
94 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
95 (upcase (concat (file-name-nondirectory
96 (file-name-sans-extension buffer-file-name))
97 "_"
98 (file-name-extension buffer-file-name)))
99 "#ifndef " str \n
100 "#define " str "\n\n"
101 _ "\n\n#endif")
102
103 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
104 nil
105 "#include \""
106 (let ((stem (file-name-sans-extension buffer-file-name)))
107 (cond ((file-exists-p (concat stem ".h"))
108 (file-name-nondirectory (concat stem ".h")))
109 ((file-exists-p (concat stem ".hh"))
110 (file-name-nondirectory (concat stem ".hh")))))
111 & ?\" | -10)
112
113 (("[Mm]akefile\\'" . "Makefile") . "makefile.inc")
114
115 (html-mode . (lambda () (sgml-tag "html")))
116
117 (plain-tex-mode . "tex-insert.tex")
118 (bibtex-mode . "tex-insert.tex")
119 (latex-mode
120 ;; should try to offer completing read for these
121 "options, RET: "
122 "\\documentclass[" str & ?\] | -1
123 ?{ (read-string "class: ") "}\n"
124 ("package, %s: "
125 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
126 _ "\n\\begin{document}\n" _
127 "\n\\end{document}")
128
129 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") .
130 (lambda ()
131 (if (eq major-mode (default-value 'major-mode))
132 (sh-mode))))
133
134 (ada-mode . ada-header)
135
136 (("\\.[1-9]\\'" . "Man page skeleton")
137 "Short description: "
138 ".\\\" Copyright (C), " (format-time-string "%Y") " "
139 (getenv "ORGANIZATION") | (progn user-full-name)
140 "
141 .\\\" You may distribute this file under the terms of the GNU Free
142 .\\\" Documentation License.
143 .TH " (file-name-base)
144 " " (file-name-extension (buffer-file-name))
145 " " (format-time-string "%Y-%m-%d ")
146 "\n.SH NAME\n"
147 (file-name-base)
148 " \\- " str
149 "\n.SH SYNOPSIS
150 .B " (file-name-base)
151 "\n"
152 _
153 "
154 .SH DESCRIPTION
155 .SH OPTIONS
156 .SH FILES
157 .SH \"SEE ALSO\"
158 .SH BUGS
159 .SH AUTHOR
160 " (user-full-name)
161 '(if (search-backward "&" (line-beginning-position) t)
162 (replace-match (capitalize (user-login-name)) t t))
163 '(end-of-line 1) " <" (progn user-mail-address) ">\n")
164
165 (("\\.el\\'" . "Emacs Lisp header")
166 "Short description: "
167 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
168
169 ;; Copyright (C) " (format-time-string "%Y") " "
170 (getenv "ORGANIZATION") | (progn user-full-name) "
171
172 ;; Author: " (user-full-name)
173 '(if (search-backward "&" (line-beginning-position) t)
174 (replace-match (capitalize (user-login-name)) t t))
175 '(end-of-line 1) " <" (progn user-mail-address) ">
176 ;; Keywords: "
177 '(require 'finder)
178 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
179 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
180 finder-known-keywords)
181 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
182 finder-known-keywords
183 "\n"))
184 ((let ((minibuffer-help-form v2))
185 (completing-read "Keyword, C-h: " v1 nil t))
186 str ", ") & -2 "
187
188 \;; This program is free software; you can redistribute it and/or modify
189 \;; it under the terms of the GNU General Public License as published by
190 \;; the Free Software Foundation, either version 3 of the License, or
191 \;; (at your option) any later version.
192
193 \;; This program is distributed in the hope that it will be useful,
194 \;; but WITHOUT ANY WARRANTY; without even the implied warranty of
195 \;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
196 \;; GNU General Public License for more details.
197
198 \;; You should have received a copy of the GNU General Public License
199 \;; along with this program. If not, see <http://www.gnu.org/licenses/>.
200
201 \;;; Commentary:
202
203 \;; " _ "
204
205 \;;; Code:
206
207
208
209 \(provide '"
210 (file-name-base)
211 ")
212 \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
213 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
214 "Title: "
215 "\\input texinfo @c -*-texinfo-*-
216 @c %**start of header
217 @setfilename "
218 (file-name-base) ".info\n"
219 "@settitle " str "
220 @c %**end of header
221 @copying\n"
222 (setq short-description (read-string "Short description: "))
223 ".\n\n"
224 "Copyright @copyright{} " (format-time-string "%Y") " "
225 (getenv "ORGANIZATION") | (progn user-full-name) "
226
227 @quotation
228 Permission is granted to copy, distribute and/or modify this document
229 under the terms of the GNU Free Documentation License, Version 1.3
230 or any later version published by the Free Software Foundation;
231 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
232 A copy of the license is included in the section entitled ``GNU
233 Free Documentation License''.
234
235 A copy of the license is also available from the Free Software
236 Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
237
238 @end quotation
239
240 The document was typeset with
241 @uref{http://www.texinfo.org/, GNU Texinfo}.
242
243 @end copying
244
245 @titlepage
246 @title " str "
247 @subtitle " short-description "
248 @author " (getenv "ORGANIZATION") | (progn user-full-name)
249 " <" (progn user-mail-address) ">
250 @page
251 @vskip 0pt plus 1filll
252 @insertcopying
253 @end titlepage
254
255 @c Output the table of the contents at the beginning.
256 @contents
257
258 @ifnottex
259 @node Top
260 @top " str "
261
262 @insertcopying
263 @end ifnottex
264
265 @c Generate the nodes for this menu with `C-c C-u C-m'.
266 @menu
267 @end menu
268
269 @c Update all node entries with `C-c C-u C-n'.
270 @c Insert new nodes with `C-c C-c n'.
271 @node Chapter One
272 @chapter Chapter One
273
274 " _ "
275
276 @node Copying This Manual
277 @appendix Copying This Manual
278
279 @menu
280 * GNU Free Documentation License:: License for copying this manual.
281 @end menu
282
283 @c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
284 @include fdl.texi
285
286 @node Index
287 @unnumbered Index
288
289 @printindex cp
290
291 @bye
292
293 @c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
294 "A list specifying text to insert by default into a new file.
295 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
296 CONDITION may be a regexp that must match the new file's name, or it may be
297 a symbol that must match the major mode for this element to apply.
298 Only the first matching element is effective.
299 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
300 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
301 file-name or one relative to `auto-insert-directory' or a function to call.
302 ACTION may also be a vector containing several successive single actions as
303 described above, e.g. [\"header.insert\" date-and-author-update]."
304 :type 'sexp
305 :group 'auto-insert)
306
307
308 ;; Establish a default value for auto-insert-directory
309 (defcustom auto-insert-directory "~/insert/"
310 "Directory from which auto-inserted files are taken.
311 The value must be an absolute directory name;
312 thus, on a GNU or Unix system, it must end in a slash."
313 :type 'directory
314 :group 'auto-insert)
315
316
317 ;;;###autoload
318 (defun auto-insert ()
319 "Insert default contents into new files if variable `auto-insert' is non-nil.
320 Matches the visited file name against the elements of `auto-insert-alist'."
321 (interactive)
322 (and (not buffer-read-only)
323 (or (eq this-command 'auto-insert)
324 (and auto-insert
325 (bobp) (eobp)))
326 (let ((alist auto-insert-alist)
327 case-fold-search cond desc action)
328 (goto-char 1)
329 ;; find first matching alist entry
330 (while alist
331 (if (atom (setq cond (car (car alist))))
332 (setq desc cond)
333 (setq desc (cdr cond)
334 cond (car cond)))
335 (if (if (symbolp cond)
336 (eq cond major-mode)
337 (and buffer-file-name
338 (string-match cond buffer-file-name)))
339 (setq action (cdr (car alist))
340 alist nil)
341 (setq alist (cdr alist))))
342
343 ;; Now, if we found something, do it
344 (and action
345 (or (not (stringp action))
346 (file-readable-p (expand-file-name
347 action auto-insert-directory)))
348 (or (not auto-insert-query)
349 (if (eq auto-insert-query 'function)
350 (eq this-command 'auto-insert))
351 (y-or-n-p (format auto-insert-prompt desc)))
352 (mapc
353 (lambda (action)
354 (if (stringp action)
355 (if (file-readable-p
356 (setq action (expand-file-name
357 action auto-insert-directory)))
358 (insert-file-contents action))
359 (save-window-excursion
360 ;; make buffer visible before skeleton or function
361 ;; which might ask the user for something
362 (switch-to-buffer (current-buffer))
363 (if (and (consp action)
364 (not (eq (car action) 'lambda)))
365 (skeleton-insert action)
366 (funcall action)))))
367 (if (vectorp action)
368 action
369 (vector action))))
370 (and (buffer-modified-p)
371 (not (eq this-command 'auto-insert))
372 (set-buffer-modified-p (eq auto-insert t)))))
373 ;; Return nil so that it could be used in
374 ;; `find-file-not-found-hooks', though that's probably inadvisable.
375 nil)
376
377
378 ;;;###autoload
379 (defun define-auto-insert (condition action &optional after)
380 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
381 Optional AFTER means to insert action after all existing actions for CONDITION,
382 or if CONDITION had no actions, after all other CONDITIONs."
383 (let ((elt (assoc condition auto-insert-alist)))
384 (if elt
385 (setcdr elt
386 (if (vectorp (cdr elt))
387 (vconcat (if after (cdr elt))
388 (if (vectorp action) action (vector action))
389 (if after () (cdr elt)))
390 (if after
391 (vector (cdr elt) action)
392 (vector action (cdr elt)))))
393 (if after
394 (nconc auto-insert-alist (list (cons condition action)))
395 (push (cons condition action) auto-insert-alist)))))
396
397 ;;;###autoload
398 (define-minor-mode auto-insert-mode
399 "Toggle Auto-insert mode, a global minor mode.
400 With a prefix argument ARG, enable Auto-insert mode if ARG is
401 positive, and disable it otherwise. If called from Lisp, enable
402 the mode if ARG is omitted or nil.
403
404 When Auto-insert mode is enabled, when new files are created you can
405 insert a template for the file depending on the mode of the buffer."
406 :global t :group 'auto-insert
407 (if auto-insert-mode
408 (add-hook 'find-file-hook 'auto-insert)
409 (remove-hook 'find-file-hook 'auto-insert)))
410
411 (provide 'autoinsert)
412
413 ;;; autoinsert.el ends here