(news-inews-program): Look for /usr/contrib/lib/news/inews.
[emacs.git] / lisp / autoinsert.el
blobe67ab211aba2517a221ad9880159af4ca3dfcd33
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Charlie Martin <crm@cs.duke.edu>
6 ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; The following defines an association list for text to be
28 ;; automatically inserted when a new file is created, and a function
29 ;; which automatically inserts these files; the idea is to insert
30 ;; default text much as the mode is automatically set using
31 ;; auto-mode-alist.
33 ;; To use:
34 ;; (add-hook 'find-file-hooks 'auto-insert)
35 ;; setq auto-insert-directory to an appropriate slash-terminated value
37 ;; Author: Charlie Martin
38 ;; Department of Computer Science and
39 ;; National Biomedical Simulation Resource
40 ;; Box 3709
41 ;; Duke University Medical Center
42 ;; Durham, NC 27710
43 ;; (crm@cs.duke.edu,mcnc!duke!crm)
45 ;;; Code:
47 (defvar auto-insert 'not-modified
48 "*Controls automatic insertion into newly found empty files:
49 nil do nothing
50 t insert if possible
51 other insert if possible, but mark as unmodified.
52 Insertion is possible when something appropriate is found in
53 `auto-insert-alist'. When the insertion is marked as unmodified, you can
54 save it with \\[write-file] RET.
55 This variable is used when `auto-insert' is called as a function, e.g.
56 when you do (add-hook 'find-file-hooks 'auto-insert).
57 With \\[auto-insert], this is always treated as if it were `t'.")
60 (defvar auto-insert-query 'function
61 "*If non-`nil', ask user before auto-inserting.
62 When this is `function', only ask when called non-interactively.")
65 (defvar auto-insert-prompt "Perform %s auto-insertion? "
66 "*Prompt to use when querying whether to auto-insert.
67 If this contains a %s, that will be replaced by the matching rule.")
70 (defvar auto-insert-alist
71 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
72 (upcase (concat (file-name-nondirectory
73 (substring buffer-file-name 0 (match-beginning 0)))
74 "_"
75 (substring buffer-file-name (1+ (match-beginning 0)))))
76 "#ifndef " str \n
77 "#define " str "\n\n"
78 _ "\n\n#endif")
80 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
81 nil
82 "#include \""
83 ;; nop without latest cc-mode
84 (and (fboundp 'c-companion-file)
85 ;(file-readable-p (c-companion-file 'name))
86 (file-name-nondirectory (c-companion-file 'name))) & ?\"
87 | -10)
89 ("[Mm]akefile\\'" . "makefile.inc")
91 (html-mode . (lambda () (sgml-tag "html")))
93 (plain-tex-mode . "tex-insert.tex")
94 (bibtex-mode . "tex-insert.tex")
95 (latex-mode
96 ;; should try to offer completing read for these
97 "options, RET: "
98 "\\documentstyle[" str & ?\] | -1
99 ?{ (read-string "class: ") "}\n"
100 ("package, %s: "
101 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
102 _ "\n\\begin{document}\n" _
103 "\n\\end{document}")
105 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
106 lambda ()
107 (if (eq major-mode default-major-mode)
108 (sh-mode)))
110 (ada-mode . ada-header)
112 (("\\.el\\'" . "Emacs Lisp header")
113 "Short description: "
114 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
116 ;; Copyright (C) " (substring (current-time-string) -4) " by "
117 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
119 ;; Author: " (user-full-name)
120 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t)
121 (replace-match (capitalize (user-login-name)) t t))
122 '(end-of-line 1) " <" (user-login-name) ?@ (system-name) ">
123 ;; Keywords: "
124 '(require 'finder)
125 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
126 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
127 finder-known-keywords)
128 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
129 finder-known-keywords
130 "\n"))
131 ((let ((minibuffer-help-form v2))
132 (completing-read "Keyword, C-h: " v1 nil t))
133 str ", ") & -2 "
135 ;; This file is part of GNU Emacs.
137 ;; GNU Emacs is free software; you can redistribute it and/or modify
138 ;; it under the terms of the GNU General Public License as published by
139 ;; the Free Software Foundation; either version 2, or (at your option)
140 ;; any later version.
142 ;; GNU Emacs is distributed in the hope that it will be useful,
143 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
144 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145 ;; GNU General Public License for more details.
147 ;; You should have received a copy of the GNU General Public License
148 ;; along with GNU Emacs; see the file COPYING. If not, write to
149 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
150 ;; Boston, MA 02111-1307, USA.
152 ;;; Commentary:
154 ;; " _ "
156 ;;; Code:
160 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
161 "A list specifying text to insert by default into a new file.
162 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
163 CONDITION maybe a regexp that must match the new file's name, or it may be
164 a symbol that must match the major mode for this element to apply.
165 Only the first matching element is effective.
166 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
167 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
168 file-name or one relative to `auto-insert-directory' or a function to call.
169 ACTION may also be a vector containing several successive single actions as
170 described above, e.g. [\"header.insert\" date-and-author-update].")
173 ;; Establish a default value for auto-insert-directory
174 (defvar auto-insert-directory "~/insert/"
175 "*Directory from which auto-inserted files are taken.")
178 ;;;###autoload
179 (defun auto-insert ()
180 "Insert default contents into a new file if `auto-insert' is non-nil.
181 Matches the visited file name against the elements of `auto-insert-alist'."
182 (interactive)
183 (and (not buffer-read-only)
184 (or (eq this-command 'auto-insert)
185 (and auto-insert
186 (bobp) (eobp)))
187 (let ((alist auto-insert-alist)
188 case-fold-search cond desc action)
189 (goto-char 1)
190 ;; find first matching alist entry
191 (while alist
192 (if (atom (setq cond (car (car alist))))
193 (setq desc cond)
194 (setq desc (cdr cond)
195 cond (car cond)))
196 (if (if (symbolp cond)
197 (eq cond major-mode)
198 (string-match cond buffer-file-name))
199 (setq action (cdr (car alist))
200 alist nil)
201 (setq alist (cdr alist))))
203 ;; Now, if we found something, do it
204 (and action
205 (if (stringp action)
206 (file-readable-p (concat auto-insert-directory action))
208 (if auto-insert-query
209 (or (if (eq auto-insert-query 'function)
210 (eq this-command 'auto-insert))
211 (y-or-n-p (format auto-insert-prompt desc)))
213 (mapcar
214 (lambda (action)
215 (if (stringp action)
216 (if (file-readable-p
217 (setq action (concat auto-insert-directory action)))
218 (insert-file-contents action))
219 (save-window-excursion
220 ;; make buffer visible before skeleton or function
221 ;; which might ask the user for something
222 (switch-to-buffer (current-buffer))
223 (if (and (consp action)
224 (not (eq (car action) 'lambda)))
225 (skeleton-insert action)
226 (funcall action)))))
227 (if (vectorp action)
228 action
229 (vector action))))
230 (and (buffer-modified-p)
231 (not (eq this-command 'auto-insert))
232 (set-buffer-modified-p (eq auto-insert t))))))
235 ;;;###autoload
236 (defun define-auto-insert (key action &optional after)
237 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
238 Optional AFTER means to insert action after all existing actions for CONDITION,
239 or if CONDITION had no actions, after all other CONDITIONs."
240 (let ((elt (assoc key auto-insert-alist)))
241 (if elt
242 (setcdr elt
243 (if (vectorp (cdr elt))
244 (vconcat (if after (cdr elt))
245 (if (vectorp action) action (vector action))
246 (if after () (cdr elt)))
247 (if after
248 (vector (cdr elt) action)
249 (vector action (cdr elt)))))
250 (if after
251 (nconc auto-insert-alist (list (cons key action)))
252 (setq auto-insert-alist (cons (cons key action)
253 auto-insert-alist))))))
255 ;;; autoinsert.el ends here