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