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