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
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
2
|
23 ;;; Synched up with: FSF 19.34.
|
0
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
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.
|
|
32 ;;
|
|
33 ;; To use:
|
|
34 ;; (add-hook 'find-file-hooks 'auto-insert)
|
|
35 ;; setq auto-insert-directory to an appropriate slash-terminated value
|
|
36 ;;
|
|
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)
|
|
44
|
|
45 ;;; Code:
|
|
46
|
|
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'.")
|
|
58
|
|
59
|
|
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.")
|
|
63
|
|
64
|
|
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.")
|
|
68
|
|
69
|
|
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")
|
|
79
|
|
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)
|
|
88
|
|
89 ("[Mm]akefile\\'" . "makefile.inc")
|
|
90
|
2
|
91 (html-mode . (lambda () (sgml-tag "html")))
|
|
92
|
0
|
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}")
|
|
104
|
|
105 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
|
|
106 lambda ()
|
|
107 (if (eq major-mode default-major-mode)
|
|
108 (sh-mode)))
|
|
109
|
|
110 (ada-mode . ada-header)
|
|
111
|
|
112 (("\\.el\\'" . "Emacs Lisp header")
|
|
113 "Short description: "
|
|
114 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
|
|
115
|
|
116 ;; Copyright (C) " (substring (current-time-string) -4) " by "
|
|
117 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
|
|
118
|
|
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 "
|
|
134
|
2
|
135 ;; This file is part of XEmacs.
|
0
|
136
|
2
|
137 ;; XEmacs is free software; you can redistribute it and/or modify
|
0
|
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.
|
|
141
|
2
|
142 ;; XEmacs is distributed in the hope that it will be useful,
|
0
|
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.
|
|
146
|
|
147 ;; You should have received a copy of the GNU General Public License
|
2
|
148 ;; along with XEmacs; see the file COPYING. If not, write to
|
0
|
149 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
150
|
|
151 ;;; Commentary:
|
|
152
|
|
153 ;; " _ "
|
|
154
|
|
155 ;;; Code:
|
|
156
|
|
157
|
|
158
|
|
159 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
|
|
160 "A list specifying text to insert by default into a new file.
|
|
161 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
|
|
162 CONDITION maybe a regexp that must match the new file's name, or it may be
|
|
163 a symbol that must match the major mode for this element to apply.
|
|
164 Only the first matching element is effective.
|
|
165 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
|
|
166 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
|
|
167 file-name or one relative to `auto-insert-directory' or a function to call.
|
|
168 ACTION may also be a vector containing several successive single actions as
|
|
169 described above, e.g. [\"header.insert\" date-and-author-update].")
|
|
170
|
|
171
|
|
172 ;; Establish a default value for auto-insert-directory
|
|
173 (defvar auto-insert-directory "~/insert/"
|
|
174 "*Directory from which auto-inserted files are taken.")
|
|
175
|
|
176
|
|
177 ;;;###autoload
|
|
178 (defun auto-insert ()
|
|
179 "Insert default contents into a new file if `auto-insert' is non-nil.
|
|
180 Matches the visited file name against the elements of `auto-insert-alist'."
|
|
181 (interactive)
|
|
182 (and (not buffer-read-only)
|
|
183 (or (eq this-command 'auto-insert)
|
|
184 (and auto-insert
|
|
185 (bobp) (eobp)))
|
|
186 (let ((alist auto-insert-alist)
|
|
187 case-fold-search cond desc action)
|
|
188 (goto-char 1)
|
|
189 ;; find first matching alist entry
|
|
190 (while alist
|
|
191 (if (atom (setq cond (car (car alist))))
|
|
192 (setq desc cond)
|
|
193 (setq desc (cdr cond)
|
|
194 cond (car cond)))
|
|
195 (if (if (symbolp cond)
|
|
196 (eq cond major-mode)
|
|
197 (string-match cond buffer-file-name))
|
|
198 (setq action (cdr (car alist))
|
|
199 alist nil)
|
|
200 (setq alist (cdr alist))))
|
|
201
|
|
202 ;; Now, if we found something, do it
|
|
203 (and action
|
|
204 (if (stringp action)
|
|
205 (file-readable-p (concat auto-insert-directory action))
|
|
206 t)
|
|
207 (if auto-insert-query
|
|
208 (or (if (eq auto-insert-query 'function)
|
|
209 (eq this-command 'auto-insert))
|
|
210 (y-or-n-p (format auto-insert-prompt desc)))
|
|
211 t)
|
|
212 (mapcar
|
|
213 (lambda (action)
|
|
214 (if (stringp action)
|
|
215 (if (file-readable-p
|
|
216 (setq action (concat auto-insert-directory action)))
|
|
217 (insert-file-contents action))
|
|
218 (save-window-excursion
|
|
219 ;; make buffer visible before skeleton or function
|
|
220 ;; which might ask the user for something
|
|
221 (switch-to-buffer (current-buffer))
|
|
222 (if (and (consp action)
|
|
223 (not (eq (car action) 'lambda)))
|
|
224 (skeleton-insert action)
|
|
225 (funcall action)))))
|
|
226 (if (vectorp action)
|
|
227 action
|
|
228 (vector action))))
|
|
229 (and (buffer-modified-p)
|
|
230 (not (eq this-command 'auto-insert))
|
|
231 (set-buffer-modified-p (eq auto-insert t))))))
|
|
232
|
|
233
|
|
234 ;;;###autoload
|
|
235 (defun define-auto-insert (key action &optional after)
|
|
236 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
|
|
237 Optional AFTER means to insert action after all existing actions for CONDITION,
|
|
238 or if CONDITION had no actions, after all other CONDITIONs."
|
|
239 (let ((elt (assoc key auto-insert-alist)))
|
|
240 (if elt
|
|
241 (setcdr elt
|
|
242 (if (vectorp (cdr elt))
|
|
243 (vconcat (if after (cdr elt))
|
|
244 (if (vectorp action) action (vector action))
|
|
245 (if after () (cdr elt)))
|
|
246 (if after
|
|
247 (vector (cdr elt) action)
|
|
248 (vector action (cdr elt)))))
|
|
249 (if after
|
|
250 (nconc auto-insert-alist (list (cons key action)))
|
|
251 (setq auto-insert-alist (cons (cons key action)
|
|
252 auto-insert-alist))))))
|
|
253
|
|
254 ;;; autoinsert.el ends here
|