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
|
|
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
|
2
|
92 (html-mode . (lambda () (sgml-tag "html")))
|
|
93
|
0
|
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
|
2
|
136 ;; This file is part of XEmacs.
|
0
|
137
|
72
|
138 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
139 ;; under the terms of the GNU General Public License as published by
|
0
|
140 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
141 ;; any later version.
|
|
142
|
72
|
143 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
144 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
145 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
146 ;; General Public License for more details.
|
0
|
147
|
|
148 ;; You should have received a copy of the GNU General Public License
|
72
|
149 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
150 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
151 ;; 02111-1307, USA.
|
|
152
|
|
153 ;;; Synched up with: Not in FSF
|
0
|
154
|
|
155 ;;; Commentary:
|
|
156
|
|
157 ;; " _ "
|
|
158
|
|
159 ;;; Code:
|
|
160
|
|
161
|
72
|
162 (provide ' " (file-name-nondirectory (buffer-file-name)) ")
|
0
|
163
|
|
164 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
|
|
165 "A list specifying text to insert by default into a new file.
|
|
166 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
|
|
167 CONDITION maybe a regexp that must match the new file's name, or it may be
|
|
168 a symbol that must match the major mode for this element to apply.
|
|
169 Only the first matching element is effective.
|
|
170 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
|
|
171 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
|
|
172 file-name or one relative to `auto-insert-directory' or a function to call.
|
|
173 ACTION may also be a vector containing several successive single actions as
|
|
174 described above, e.g. [\"header.insert\" date-and-author-update].")
|
|
175
|
|
176
|
|
177 ;; Establish a default value for auto-insert-directory
|
|
178 (defvar auto-insert-directory "~/insert/"
|
|
179 "*Directory from which auto-inserted files are taken.")
|
|
180
|
|
181
|
|
182 ;;;###autoload
|
|
183 (defun auto-insert ()
|
|
184 "Insert default contents into a new file if `auto-insert' is non-nil.
|
|
185 Matches the visited file name against the elements of `auto-insert-alist'."
|
|
186 (interactive)
|
|
187 (and (not buffer-read-only)
|
|
188 (or (eq this-command 'auto-insert)
|
|
189 (and auto-insert
|
|
190 (bobp) (eobp)))
|
|
191 (let ((alist auto-insert-alist)
|
|
192 case-fold-search cond desc action)
|
|
193 (goto-char 1)
|
|
194 ;; find first matching alist entry
|
|
195 (while alist
|
|
196 (if (atom (setq cond (car (car alist))))
|
|
197 (setq desc cond)
|
|
198 (setq desc (cdr cond)
|
|
199 cond (car cond)))
|
|
200 (if (if (symbolp cond)
|
|
201 (eq cond major-mode)
|
|
202 (string-match cond buffer-file-name))
|
|
203 (setq action (cdr (car alist))
|
|
204 alist nil)
|
|
205 (setq alist (cdr alist))))
|
|
206
|
|
207 ;; Now, if we found something, do it
|
|
208 (and action
|
|
209 (if (stringp action)
|
|
210 (file-readable-p (concat auto-insert-directory action))
|
|
211 t)
|
|
212 (if auto-insert-query
|
|
213 (or (if (eq auto-insert-query 'function)
|
|
214 (eq this-command 'auto-insert))
|
|
215 (y-or-n-p (format auto-insert-prompt desc)))
|
|
216 t)
|
|
217 (mapcar
|
|
218 (lambda (action)
|
|
219 (if (stringp action)
|
|
220 (if (file-readable-p
|
|
221 (setq action (concat auto-insert-directory action)))
|
|
222 (insert-file-contents action))
|
|
223 (save-window-excursion
|
|
224 ;; make buffer visible before skeleton or function
|
|
225 ;; which might ask the user for something
|
|
226 (switch-to-buffer (current-buffer))
|
|
227 (if (and (consp action)
|
|
228 (not (eq (car action) 'lambda)))
|
|
229 (skeleton-insert action)
|
|
230 (funcall action)))))
|
|
231 (if (vectorp action)
|
|
232 action
|
|
233 (vector action))))
|
|
234 (and (buffer-modified-p)
|
|
235 (not (eq this-command 'auto-insert))
|
|
236 (set-buffer-modified-p (eq auto-insert t))))))
|
|
237
|
|
238
|
|
239 ;;;###autoload
|
|
240 (defun define-auto-insert (key action &optional after)
|
|
241 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
|
|
242 Optional AFTER means to insert action after all existing actions for CONDITION,
|
|
243 or if CONDITION had no actions, after all other CONDITIONs."
|
|
244 (let ((elt (assoc key auto-insert-alist)))
|
|
245 (if elt
|
|
246 (setcdr elt
|
|
247 (if (vectorp (cdr elt))
|
|
248 (vconcat (if after (cdr elt))
|
|
249 (if (vectorp action) action (vector action))
|
|
250 (if after () (cdr elt)))
|
|
251 (if after
|
|
252 (vector (cdr elt) action)
|
|
253 (vector action (cdr elt)))))
|
|
254 (if after
|
|
255 (nconc auto-insert-alist (list (cons key action)))
|
|
256 (setq auto-insert-alist (cons (cons key action)
|
|
257 auto-insert-alist))))))
|
|
258
|
72
|
259 (provide 'autoinsert)
|
|
260
|
0
|
261 ;;; autoinsert.el ends here
|