Remove some function declarations, no longer needed or correct
[bpt/emacs.git] / lisp / autoinsert.el
CommitLineData
c0274f38 1;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
b578f267 2
ba318903 3;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2014 Free Software
ab422c4d 4;; Foundation, Inc.
9750e079 5
e5167999 6;; Author: Charlie Martin <crm@cs.duke.edu>
3e910376 7;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
f5f727f8 8;; Keywords: convenience
34dc21db 9;; Maintainer: emacs-devel@gnu.org
b1d6ae0b
JB
10
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
b1d6ae0b 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
b1d6ae0b
JB
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
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b1d6ae0b 25
e5167999 26;;; Commentary:
b1d6ae0b 27
c11af8a5
KH
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;;
12394086 34;; To use:
a702ffbb 35;; (add-hook 'find-file-hook 'auto-insert)
c11af8a5
KH
36;; setq auto-insert-directory to an appropriate slash-terminated value
37;;
6c5b39e4 38;; You can also customize the variable `auto-insert-mode' to load the
865fe16f 39;; package. Alternatively, add the following to your init file:
6c5b39e4
SE
40;; (auto-insert-mode 1)
41;;
c11af8a5
KH
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
12394086 48;; (crm@cs.duke.edu,mcnc!duke!crm)
b1d6ae0b 49
e5167999
ER
50;;; Code:
51
6c5b39e4
SE
52(defgroup auto-insert nil
53 "Automatic mode-dependent insertion of text into new files."
54 :prefix "auto-insert-"
f5f727f8 55 :group 'files
b1d4664a
JB
56 :group 'convenience
57 :link '(custom-manual "(autotype) Autoinserting"))
6c5b39e4
SE
58
59
6c5b39e4 60(defcustom auto-insert 'not-modified
9201cc28 61 "Controls automatic insertion into newly found empty files.
12394086 62Possible values:
c11af8a5
KH
63 nil do nothing
64 t insert if possible
65 other insert if possible, but mark as unmodified.
66Insertion is possible when something appropriate is found in
67`auto-insert-alist'. When the insertion is marked as unmodified, you can
68save it with \\[write-file] RET.
a461758e 69This variable is used when the function `auto-insert' is called, e.g.
a702ffbb 70when you do (add-hook 'find-file-hook 'auto-insert).
12394086 71With \\[auto-insert], this is always treated as if it were t."
4605a50d
DL
72 :type '(choice (const :tag "Insert if possible" t)
73 (const :tag "Do nothing" nil)
12394086 74 (other :tag "insert if possible, mark as unmodified."
4605a50d 75 not-modified))
6c5b39e4 76 :group 'auto-insert)
c11af8a5 77
6c5b39e4 78(defcustom auto-insert-query 'function
9201cc28 79 "Non-nil means ask user before auto-inserting.
6c5b39e4 80When this is `function', only ask when called non-interactively."
5c213454
AS
81 :type '(choice (const :tag "Don't ask" nil)
82 (const :tag "Ask if called non-interactively" function)
83 (other :tag "Ask" t))
6c5b39e4 84 :group 'auto-insert)
c11af8a5 85
6c5b39e4 86(defcustom auto-insert-prompt "Perform %s auto-insertion? "
9201cc28 87 "Prompt to use when querying whether to auto-insert.
6c5b39e4
SE
88If this contains a %s, that will be replaced by the matching rule."
89 :type 'string
90 :group 'auto-insert)
c11af8a5
KH
91
92
6c5b39e4 93(defcustom auto-insert-alist
c11af8a5
KH
94 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
95 (upcase (concat (file-name-nondirectory
a3610f0c 96 (file-name-sans-extension buffer-file-name))
c11af8a5 97 "_"
a3610f0c 98 (file-name-extension buffer-file-name)))
c11af8a5
KH
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 \""
f6ec1532
RS
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")
c11af8a5 114
d60ae75c 115 (html-mode . (lambda () (sgml-tag "html")))
71296446 116
c11af8a5
KH
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: "
47e58cf5 122 "\\documentclass[" str & ?\] | -1
c11af8a5
KH
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
a6ed0e28
JL
129 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") .
130 (lambda ()
14acf2f5 131 (if (eq major-mode (default-value 'major-mode))
a6ed0e28 132 (sh-mode))))
71296446 133
c11af8a5
KH
134 (ada-mode . ada-header)
135
bf9f5c17
DL
136 (("\\.[1-9]\\'" . "Man page skeleton")
137 "Short description: "
0bfcf5c5 138 ".\\\" Copyright (C), " (format-time-string "%Y") " "
12148b0e 139 (getenv "ORGANIZATION") | (progn user-full-name)
bf9f5c17
DL
140 "
141.\\\" You may distribute this file under the terms of the GNU Free
2201b59a 142.\\\" Documentation License.
d2c32364 143.TH " (file-name-base)
bf9f5c17
DL
144 " " (file-name-extension (buffer-file-name))
145 " " (format-time-string "%Y-%m-%d ")
146 "\n.SH NAME\n"
d2c32364 147 (file-name-base)
bf9f5c17
DL
148 " \\- " str
149 "\n.SH SYNOPSIS
d2c32364 150.B " (file-name-base)
bf9f5c17
DL
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
c11af8a5
KH
165 (("\\.el\\'" . "Emacs Lisp header")
166 "Short description: "
96eb829a
LL
167 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
168 (make-string (max 2 (- 80 (current-column) 27)) ?\s)
33215353 169 "-*- lexical-binding: t; -*-" '(setq lexical-binding t)
96eb829a 170 "
c11af8a5 171
0bfcf5c5 172;; Copyright (C) " (format-time-string "%Y") " "
fbee29e6 173 (getenv "ORGANIZATION") | (progn user-full-name) "
c11af8a5
KH
174
175;; Author: " (user-full-name)
a461758e 176'(if (search-backward "&" (line-beginning-position) t)
c11af8a5 177 (replace-match (capitalize (user-login-name)) t t))
c5f7d536 178'(end-of-line 1) " <" (progn user-mail-address) ">
c11af8a5
KH
179;; Keywords: "
180 '(require 'finder)
181 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
182 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
183 finder-known-keywords)
8ed6049f 184 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
c11af8a5
KH
185 finder-known-keywords
186 "\n"))
187 ((let ((minibuffer-help-form v2))
188 (completing-read "Keyword, C-h: " v1 nil t))
189 str ", ") & -2 "
190
88ddede6 191\;; This program is free software; you can redistribute it and/or modify
31b6b8cd 192\;; it under the terms of the GNU General Public License as published by
88ddede6
GM
193\;; the Free Software Foundation, either version 3 of the License, or
194\;; (at your option) any later version.
c11af8a5 195
88ddede6 196\;; This program is distributed in the hope that it will be useful,
31b6b8cd
SM
197\;; but WITHOUT ANY WARRANTY; without even the implied warranty of
198\;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199\;; GNU General Public License for more details.
c11af8a5 200
31b6b8cd 201\;; You should have received a copy of the GNU General Public License
88ddede6 202\;; along with this program. If not, see <http://www.gnu.org/licenses/>.
c11af8a5 203
31b6b8cd 204\;;; Commentary:
c11af8a5 205
31b6b8cd 206\;; " _ "
c11af8a5 207
31b6b8cd 208\;;; Code:
c11af8a5
KH
209
210
211
3c98b2e9 212\(provide '"
d2c32364 213 (file-name-base)
3c98b2e9 214 ")
1caf5f96
GM
215\;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
216 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
217 "Title: "
218 "\\input texinfo @c -*-texinfo-*-
219@c %**start of header
220@setfilename "
d2c32364 221 (file-name-base) ".info\n"
1caf5f96
GM
222 "@settitle " str "
223@c %**end of header
224@copying\n"
225 (setq short-description (read-string "Short description: "))
226 ".\n\n"
0bfcf5c5 227 "Copyright @copyright{} " (format-time-string "%Y") " "
1caf5f96
GM
228 (getenv "ORGANIZATION") | (progn user-full-name) "
229
230@quotation
231Permission is granted to copy, distribute and/or modify this document
d5259d01 232under the terms of the GNU Free Documentation License, Version 1.3
af8b50d1 233or any later version published by the Free Software Foundation;
d5259d01
GM
234with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
235A copy of the license is included in the section entitled ``GNU
af8b50d1 236Free Documentation License''.
1caf5f96
GM
237
238A copy of the license is also available from the Free Software
239Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
240
241@end quotation
242
243The document was typeset with
244@uref{http://www.texinfo.org/, GNU Texinfo}.
245
246@end copying
247
248@titlepage
249@title " str "
250@subtitle " short-description "
251@author " (getenv "ORGANIZATION") | (progn user-full-name)
252 " <" (progn user-mail-address) ">
253@page
254@vskip 0pt plus 1filll
255@insertcopying
256@end titlepage
257
258@c Output the table of the contents at the beginning.
259@contents
260
261@ifnottex
262@node Top
263@top " str "
264
265@insertcopying
266@end ifnottex
267
268@c Generate the nodes for this menu with `C-c C-u C-m'.
269@menu
270@end menu
271
272@c Update all node entries with `C-c C-u C-n'.
273@c Insert new nodes with `C-c C-c n'.
274@node Chapter One
275@chapter Chapter One
276
277" _ "
278
279@node Copying This Manual
280@appendix Copying This Manual
281
282@menu
283* GNU Free Documentation License:: License for copying this manual.
284@end menu
285
286@c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
287@include fdl.texi
288
289@node Index
290@unnumbered Index
291
292@printindex cp
293
294@bye
295
296@c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
b1d6ae0b 297 "A list specifying text to insert by default into a new file.
c11af8a5 298Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
2a575769 299CONDITION may be a regexp that must match the new file's name, or it may be
c11af8a5
KH
300a symbol that must match the major mode for this element to apply.
301Only the first matching element is effective.
302Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
303ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
304file-name or one relative to `auto-insert-directory' or a function to call.
305ACTION may also be a vector containing several successive single actions as
6c5b39e4
SE
306described above, e.g. [\"header.insert\" date-and-author-update]."
307 :type 'sexp
308 :group 'auto-insert)
b1d6ae0b 309
c11af8a5
KH
310
311;; Establish a default value for auto-insert-directory
6c5b39e4 312(defcustom auto-insert-directory "~/insert/"
9201cc28 313 "Directory from which auto-inserted files are taken.
87810ca9
RS
314The value must be an absolute directory name;
315thus, on a GNU or Unix system, it must end in a slash."
6c5b39e4
SE
316 :type 'directory
317 :group 'auto-insert)
b1d6ae0b 318
c11af8a5
KH
319
320;;;###autoload
321(defun auto-insert ()
a461758e 322 "Insert default contents into new files if variable `auto-insert' is non-nil.
b1d6ae0b 323Matches the visited file name against the elements of `auto-insert-alist'."
c11af8a5
KH
324 (interactive)
325 (and (not buffer-read-only)
326 (or (eq this-command 'auto-insert)
327 (and auto-insert
328 (bobp) (eobp)))
329 (let ((alist auto-insert-alist)
330 case-fold-search cond desc action)
331 (goto-char 1)
332 ;; find first matching alist entry
333 (while alist
334 (if (atom (setq cond (car (car alist))))
335 (setq desc cond)
336 (setq desc (cdr cond)
337 cond (car cond)))
338 (if (if (symbolp cond)
339 (eq cond major-mode)
a461758e
DL
340 (and buffer-file-name
341 (string-match cond buffer-file-name)))
c11af8a5
KH
342 (setq action (cdr (car alist))
343 alist nil)
344 (setq alist (cdr alist))))
345
346 ;; Now, if we found something, do it
347 (and action
bdaf166c
SM
348 (or (not (stringp action))
349 (file-readable-p (expand-file-name
350 action auto-insert-directory)))
351 (or (not auto-insert-query)
352 (if (eq auto-insert-query 'function)
353 (eq this-command 'auto-insert))
354 (y-or-n-p (format auto-insert-prompt desc)))
6fd09e19 355 (mapc
c11af8a5
KH
356 (lambda (action)
357 (if (stringp action)
358 (if (file-readable-p
bdaf166c
SM
359 (setq action (expand-file-name
360 action auto-insert-directory)))
c11af8a5
KH
361 (insert-file-contents action))
362 (save-window-excursion
363 ;; make buffer visible before skeleton or function
364 ;; which might ask the user for something
c3313451 365 (switch-to-buffer (current-buffer))
c11af8a5
KH
366 (if (and (consp action)
367 (not (eq (car action) 'lambda)))
368 (skeleton-insert action)
369 (funcall action)))))
370 (if (vectorp action)
371 action
372 (vector action))))
373 (and (buffer-modified-p)
374 (not (eq this-command 'auto-insert))
12394086
DL
375 (set-buffer-modified-p (eq auto-insert t)))))
376 ;; Return nil so that it could be used in
377 ;; `find-file-not-found-hooks', though that's probably inadvisable.
378 nil)
c11af8a5
KH
379
380
381;;;###autoload
4605a50d 382(defun define-auto-insert (condition action &optional after)
c11af8a5
KH
383 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
384Optional AFTER means to insert action after all existing actions for CONDITION,
385or if CONDITION had no actions, after all other CONDITIONs."
4605a50d 386 (let ((elt (assoc condition auto-insert-alist)))
c11af8a5
KH
387 (if elt
388 (setcdr elt
389 (if (vectorp (cdr elt))
390 (vconcat (if after (cdr elt))
391 (if (vectorp action) action (vector action))
392 (if after () (cdr elt)))
393 (if after
394 (vector (cdr elt) action)
395 (vector action (cdr elt)))))
396 (if after
4605a50d 397 (nconc auto-insert-alist (list (cons condition action)))
bdaf166c 398 (push (cons condition action) auto-insert-alist)))))
c0274f38 399
6c5b39e4 400;;;###autoload
bff6da3d 401(define-minor-mode auto-insert-mode
06e21633
CY
402 "Toggle Auto-insert mode, a global minor mode.
403With a prefix argument ARG, enable Auto-insert mode if ARG is
404positive, and disable it otherwise. If called from Lisp, enable
405the mode if ARG is omitted or nil.
6c5b39e4 406
12394086 407When Auto-insert mode is enabled, when new files are created you can
6c5b39e4 408insert a template for the file depending on the mode of the buffer."
44e70da2 409 :global t :group 'auto-insert
bff6da3d 410 (if auto-insert-mode
a702ffbb
SM
411 (add-hook 'find-file-hook 'auto-insert)
412 (remove-hook 'find-file-hook 'auto-insert)))
6c5b39e4 413
896546cd
RS
414(provide 'autoinsert)
415
c0274f38 416;;; autoinsert.el ends here