Commit | Line | Data |
---|---|---|
c0274f38 | 1 | ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files |
b578f267 | 2 | |
c11af8a5 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1994, 1995 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 | 5 | ;; Author: Charlie Martin <crm@cs.duke.edu> |
c11af8a5 | 6 | ;; Adapted-By: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389 |
4228277d | 7 | ;; Maintainer: FSF |
b1d6ae0b JB |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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 | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
b1d6ae0b JB |
14 | ;; any later version. |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
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. | |
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 | ;; | |
34 | ;; To use: | |
35 | ;; (add-hook 'find-file-hooks 'auto-insert) | |
36 | ;; setq auto-insert-directory to an appropriate slash-terminated value | |
37 | ;; | |
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) | |
b1d6ae0b | 45 | |
e5167999 ER |
46 | ;;; Code: |
47 | ||
c11af8a5 KH |
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'.") | |
59 | ||
60 | ||
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.") | |
64 | ||
65 | ||
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.") | |
69 | ||
70 | ||
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") | |
80 | ||
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) | |
89 | ||
90 | ("[Mm]akefile\\'" . "makefile.inc") | |
91 | ||
d60ae75c | 92 | (html-mode . (lambda () (sgml-tag "html"))) |
c11af8a5 KH |
93 | |
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}") | |
105 | ||
106 | (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") | |
107 | lambda () | |
108 | (if (eq major-mode default-major-mode) | |
109 | (sh-mode))) | |
110 | ||
111 | (ada-mode . ada-header) | |
112 | ||
113 | (("\\.el\\'" . "Emacs Lisp header") | |
114 | "Short description: " | |
115 | ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str " | |
116 | ||
117 | ;; Copyright (C) " (substring (current-time-string) -4) " by " | |
118 | (getenv "ORGANIZATION") | "Free Software Foundation, Inc." " | |
119 | ||
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 " | |
135 | ||
136 | ;; This file is part of GNU Emacs. | |
137 | ||
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. | |
142 | ||
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. | |
147 | ||
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 | |
01bb7524 EN |
150 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
151 | ;; Boston, MA 02111-1307, USA. | |
c11af8a5 KH |
152 | |
153 | ;;; Commentary: | |
154 | ||
155 | ;; " _ " | |
156 | ||
157 | ;;; Code: | |
158 | ||
159 | ||
160 | ||
161 | ;;; " (file-name-nondirectory (buffer-file-name)) " ends here")) | |
b1d6ae0b | 162 | "A list specifying text to insert by default into a new file. |
c11af8a5 KH |
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].") | |
b1d6ae0b | 172 | |
c11af8a5 KH |
173 | |
174 | ;; Establish a default value for auto-insert-directory | |
b1d6ae0b JB |
175 | (defvar auto-insert-directory "~/insert/" |
176 | "*Directory from which auto-inserted files are taken.") | |
177 | ||
c11af8a5 KH |
178 | |
179 | ;;;###autoload | |
180 | (defun auto-insert () | |
181 | "Insert default contents into a new file if `auto-insert' is non-nil. | |
b1d6ae0b | 182 | Matches the visited file name against the elements of `auto-insert-alist'." |
c11af8a5 KH |
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)))) | |
203 | ||
204 | ;; Now, if we found something, do it | |
205 | (and action | |
206 | (if (stringp action) | |
207 | (file-readable-p (concat auto-insert-directory action)) | |
208 | t) | |
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))) | |
213 | t) | |
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)))))) | |
234 | ||
235 | ||
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)))))) | |
c0274f38 | 255 | |
896546cd RS |
256 | (provide 'autoinsert) |
257 | ||
c0274f38 | 258 | ;;; autoinsert.el ends here |