662
|
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
|
14169
|
2
|
49024
|
3 ;; Copyright (C) 1985, 86, 87, 94, 95, 98, 2000, 03 Free Software Foundation, Inc.
|
845
|
4
|
807
|
5 ;; Author: Charlie Martin <crm@cs.duke.edu>
|
23869
|
6 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
|
22250
|
7 ;; Keywords: convenience
|
17976
|
8 ;; Maintainer: FSF
|
180
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;; it under the terms of the GNU General Public License as published by
|
807
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
180
|
15 ;; any later version.
|
|
16
|
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
14169
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
180
|
26
|
807
|
27 ;;; Commentary:
|
180
|
28
|
12502
|
29 ;; The following defines an association list for text to be
|
|
30 ;; automatically inserted when a new file is created, and a function
|
|
31 ;; which automatically inserts these files; the idea is to insert
|
|
32 ;; default text much as the mode is automatically set using
|
|
33 ;; auto-mode-alist.
|
|
34 ;;
|
26661
|
35 ;; To use:
|
12502
|
36 ;; (add-hook 'find-file-hooks 'auto-insert)
|
|
37 ;; setq auto-insert-directory to an appropriate slash-terminated value
|
|
38 ;;
|
20809
|
39 ;; You can also customize the variable `auto-insert-mode' to load the
|
|
40 ;; package. Alternatively, add the following to your .emacs file:
|
|
41 ;; (auto-insert-mode 1)
|
|
42 ;;
|
12502
|
43 ;; Author: Charlie Martin
|
|
44 ;; Department of Computer Science and
|
|
45 ;; National Biomedical Simulation Resource
|
|
46 ;; Box 3709
|
|
47 ;; Duke University Medical Center
|
|
48 ;; Durham, NC 27710
|
26661
|
49 ;; (crm@cs.duke.edu,mcnc!duke!crm)
|
180
|
50
|
807
|
51 ;;; Code:
|
|
52
|
20809
|
53 (defgroup auto-insert nil
|
|
54 "Automatic mode-dependent insertion of text into new files."
|
|
55 :prefix "auto-insert-"
|
22250
|
56 :group 'files
|
|
57 :group 'convenience)
|
20809
|
58
|
|
59
|
|
60 (defcustom auto-insert 'not-modified
|
26661
|
61 "*Controls automatic insertion into newly found empty files.
|
|
62 Possible values:
|
12502
|
63 nil do nothing
|
|
64 t insert if possible
|
|
65 other insert if possible, but mark as unmodified.
|
|
66 Insertion is possible when something appropriate is found in
|
|
67 `auto-insert-alist'. When the insertion is marked as unmodified, you can
|
|
68 save it with \\[write-file] RET.
|
31878
|
69 This variable is used when the function `auto-insert' is called, e.g.
|
12502
|
70 when you do (add-hook 'find-file-hooks 'auto-insert).
|
26661
|
71 With \\[auto-insert], this is always treated as if it were t."
|
21287
|
72 :type '(choice (const :tag "Insert if possible" t)
|
|
73 (const :tag "Do nothing" nil)
|
26661
|
74 (other :tag "insert if possible, mark as unmodified."
|
21287
|
75 not-modified))
|
20809
|
76 :group 'auto-insert)
|
|
77
|
|
78 (defcustom auto-insert-query 'function
|
26661
|
79 "*Non-nil means ask user before auto-inserting.
|
20809
|
80 When this is `function', only ask when called non-interactively."
|
22567
|
81 :type '(choice (const :tag "Don't ask" nil)
|
|
82 (const :tag "Ask if called non-interactively" function)
|
|
83 (other :tag "Ask" t))
|
20809
|
84 :group 'auto-insert)
|
|
85
|
|
86 (defcustom auto-insert-prompt "Perform %s auto-insertion? "
|
|
87 "*Prompt to use when querying whether to auto-insert.
|
|
88 If this contains a %s, that will be replaced by the matching rule."
|
|
89 :type 'string
|
|
90 :group 'auto-insert)
|
12502
|
91
|
|
92
|
20809
|
93 (defcustom auto-insert-alist
|
12502
|
94 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
|
|
95 (upcase (concat (file-name-nondirectory
|
|
96 (substring buffer-file-name 0 (match-beginning 0)))
|
|
97 "_"
|
|
98 (substring buffer-file-name (1+ (match-beginning 0)))))
|
|
99 "#ifndef " str \n
|
|
100 "#define " str "\n\n"
|
|
101 _ "\n\n#endif")
|
|
102
|
|
103 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
|
|
104 nil
|
|
105 "#include \""
|
42161
|
106 (let ((stem (file-name-sans-extension buffer-file-name)))
|
|
107 (cond ((file-exists-p (concat stem ".h"))
|
|
108 (file-name-nondirectory (concat stem ".h")))
|
|
109 ((file-exists-p (concat stem ".hh"))
|
|
110 (file-name-nondirectory (concat stem ".hh")))))
|
|
111 & ?\" | -10)
|
12502
|
112
|
42161
|
113 (("[Mm]akefile\\'" . "Makefile") . "makefile.inc")
|
12502
|
114
|
14153
|
115 (html-mode . (lambda () (sgml-tag "html")))
|
49588
|
116
|
12502
|
117 (plain-tex-mode . "tex-insert.tex")
|
|
118 (bibtex-mode . "tex-insert.tex")
|
|
119 (latex-mode
|
|
120 ;; should try to offer completing read for these
|
|
121 "options, RET: "
|
33252
|
122 "\\documentclass[" str & ?\] | -1
|
12502
|
123 ?{ (read-string "class: ") "}\n"
|
|
124 ("package, %s: "
|
|
125 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
|
|
126 _ "\n\\begin{document}\n" _
|
|
127 "\n\\end{document}")
|
|
128
|
|
129 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
|
|
130 lambda ()
|
|
131 (if (eq major-mode default-major-mode)
|
|
132 (sh-mode)))
|
49588
|
133
|
12502
|
134 (ada-mode . ada-header)
|
|
135
|
49024
|
136 (("\\.[1-9]\\'" . "Man page skeleton")
|
|
137 "Short description: "
|
|
138 ".\\\" Copyright (C), " (substring (current-time-string) -4) " "
|
|
139 (getenv "ORGANIZATION") | "Free Software Foundation, Inc."
|
|
140 "
|
|
141 .\\\" You may distribute this file under the terms of the GNU Free
|
|
142 .\\\" Documentation Licence.
|
|
143 .TH " (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
|
|
144 " " (file-name-extension (buffer-file-name))
|
|
145 " " (format-time-string "%Y-%m-%d ")
|
|
146 "\n.SH NAME\n"
|
|
147 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
|
|
148 " \\- " str
|
|
149 "\n.SH SYNOPSIS
|
|
150 .B " (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
|
|
151 "\n"
|
|
152 _
|
|
153 "
|
|
154 .SH DESCRIPTION
|
|
155 .SH OPTIONS
|
|
156 .SH FILES
|
|
157 .SH \"SEE ALSO\"
|
|
158 .SH BUGS
|
|
159 .SH AUTHOR
|
|
160 " (user-full-name)
|
|
161 '(if (search-backward "&" (line-beginning-position) t)
|
|
162 (replace-match (capitalize (user-login-name)) t t))
|
|
163 '(end-of-line 1) " <" (progn user-mail-address) ">\n")
|
|
164
|
12502
|
165 (("\\.el\\'" . "Emacs Lisp header")
|
|
166 "Short description: "
|
|
167 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
|
|
168
|
34380
|
169 ;; Copyright (C) " (substring (current-time-string) -4) " "
|
12502
|
170 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
|
|
171
|
|
172 ;; Author: " (user-full-name)
|
31878
|
173 '(if (search-backward "&" (line-beginning-position) t)
|
12502
|
174 (replace-match (capitalize (user-login-name)) t t))
|
23782
|
175 '(end-of-line 1) " <" (progn user-mail-address) ">
|
12502
|
176 ;; Keywords: "
|
|
177 '(require 'finder)
|
|
178 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
|
|
179 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
|
|
180 finder-known-keywords)
|
|
181 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
|
|
182 finder-known-keywords
|
|
183 "\n"))
|
|
184 ((let ((minibuffer-help-form v2))
|
|
185 (completing-read "Keyword, C-h: " v1 nil t))
|
|
186 str ", ") & -2 "
|
|
187
|
23708
|
188 ;; This file is free software; you can redistribute it and/or modify
|
12502
|
189 ;; it under the terms of the GNU General Public License as published by
|
|
190 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
191 ;; any later version.
|
|
192
|
23708
|
193 ;; This file is distributed in the hope that it will be useful,
|
12502
|
194 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
195 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
196 ;; GNU General Public License for more details.
|
|
197
|
|
198 ;; You should have received a copy of the GNU General Public License
|
|
199 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
14181
|
200 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
201 ;; Boston, MA 02111-1307, USA.
|
12502
|
202
|
|
203 ;;; Commentary:
|
|
204
|
|
205 ;; " _ "
|
|
206
|
|
207 ;;; Code:
|
|
208
|
|
209
|
|
210
|
33794
|
211 \(provide '"
|
|
212 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
|
|
213 ")
|
34380
|
214 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
|
180
|
215 "A list specifying text to insert by default into a new file.
|
12502
|
216 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
|
|
217 CONDITION maybe a regexp that must match the new file's name, or it may be
|
|
218 a symbol that must match the major mode for this element to apply.
|
|
219 Only the first matching element is effective.
|
|
220 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
|
|
221 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
|
|
222 file-name or one relative to `auto-insert-directory' or a function to call.
|
|
223 ACTION may also be a vector containing several successive single actions as
|
20809
|
224 described above, e.g. [\"header.insert\" date-and-author-update]."
|
|
225 :type 'sexp
|
|
226 :group 'auto-insert)
|
180
|
227
|
12502
|
228
|
|
229 ;; Establish a default value for auto-insert-directory
|
20809
|
230 (defcustom auto-insert-directory "~/insert/"
|
44978
|
231 "*Directory from which auto-inserted files are taken.
|
45025
|
232 The value must be an absolute directory name;
|
|
233 thus, on a GNU or Unix system, it must end in a slash."
|
20809
|
234 :type 'directory
|
|
235 :group 'auto-insert)
|
180
|
236
|
12502
|
237
|
|
238 ;;;###autoload
|
|
239 (defun auto-insert ()
|
31878
|
240 "Insert default contents into new files if variable `auto-insert' is non-nil.
|
180
|
241 Matches the visited file name against the elements of `auto-insert-alist'."
|
12502
|
242 (interactive)
|
|
243 (and (not buffer-read-only)
|
|
244 (or (eq this-command 'auto-insert)
|
|
245 (and auto-insert
|
|
246 (bobp) (eobp)))
|
|
247 (let ((alist auto-insert-alist)
|
|
248 case-fold-search cond desc action)
|
|
249 (goto-char 1)
|
|
250 ;; find first matching alist entry
|
|
251 (while alist
|
|
252 (if (atom (setq cond (car (car alist))))
|
|
253 (setq desc cond)
|
|
254 (setq desc (cdr cond)
|
|
255 cond (car cond)))
|
|
256 (if (if (symbolp cond)
|
|
257 (eq cond major-mode)
|
31878
|
258 (and buffer-file-name
|
|
259 (string-match cond buffer-file-name)))
|
12502
|
260 (setq action (cdr (car alist))
|
|
261 alist nil)
|
|
262 (setq alist (cdr alist))))
|
180
|
263
|
12502
|
264 ;; Now, if we found something, do it
|
|
265 (and action
|
|
266 (if (stringp action)
|
|
267 (file-readable-p (concat auto-insert-directory action))
|
|
268 t)
|
|
269 (if auto-insert-query
|
|
270 (or (if (eq auto-insert-query 'function)
|
|
271 (eq this-command 'auto-insert))
|
|
272 (y-or-n-p (format auto-insert-prompt desc)))
|
|
273 t)
|
|
274 (mapcar
|
|
275 (lambda (action)
|
|
276 (if (stringp action)
|
|
277 (if (file-readable-p
|
|
278 (setq action (concat auto-insert-directory action)))
|
|
279 (insert-file-contents action))
|
|
280 (save-window-excursion
|
|
281 ;; make buffer visible before skeleton or function
|
|
282 ;; which might ask the user for something
|
|
283 (switch-to-buffer (current-buffer))
|
|
284 (if (and (consp action)
|
|
285 (not (eq (car action) 'lambda)))
|
|
286 (skeleton-insert action)
|
|
287 (funcall action)))))
|
|
288 (if (vectorp action)
|
|
289 action
|
|
290 (vector action))))
|
|
291 (and (buffer-modified-p)
|
|
292 (not (eq this-command 'auto-insert))
|
26661
|
293 (set-buffer-modified-p (eq auto-insert t)))))
|
|
294 ;; Return nil so that it could be used in
|
|
295 ;; `find-file-not-found-hooks', though that's probably inadvisable.
|
|
296 nil)
|
180
|
297
|
12502
|
298
|
|
299 ;;;###autoload
|
21287
|
300 (defun define-auto-insert (condition action &optional after)
|
12502
|
301 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
|
|
302 Optional AFTER means to insert action after all existing actions for CONDITION,
|
|
303 or if CONDITION had no actions, after all other CONDITIONs."
|
21287
|
304 (let ((elt (assoc condition auto-insert-alist)))
|
12502
|
305 (if elt
|
|
306 (setcdr elt
|
|
307 (if (vectorp (cdr elt))
|
|
308 (vconcat (if after (cdr elt))
|
|
309 (if (vectorp action) action (vector action))
|
|
310 (if after () (cdr elt)))
|
|
311 (if after
|
|
312 (vector (cdr elt) action)
|
|
313 (vector action (cdr elt)))))
|
|
314 (if after
|
21287
|
315 (nconc auto-insert-alist (list (cons condition action)))
|
|
316 (setq auto-insert-alist (cons (cons condition action)
|
12502
|
317 auto-insert-alist))))))
|
662
|
318
|
20809
|
319 ;;;###autoload
|
32025
|
320 (define-minor-mode auto-insert-mode
|
26661
|
321 "Toggle Auto-insert mode.
|
|
322 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
|
|
323 Returns the new status of Auto-insert mode (non-nil means on).
|
20809
|
324
|
26661
|
325 When Auto-insert mode is enabled, when new files are created you can
|
20809
|
326 insert a template for the file depending on the mode of the buffer."
|
33184
|
327 :global t :group 'auto-insert
|
32025
|
328 (if auto-insert-mode
|
|
329 (add-hook 'find-file-hooks 'auto-insert)
|
|
330 (remove-hook 'find-file-hooks 'auto-insert)))
|
20809
|
331
|
18383
|
332 (provide 'autoinsert)
|
|
333
|
52401
|
334 ;;; arch-tag: 5b6630ac-c735-43cf-b097-b78c622af909
|
662
|
335 ;;; autoinsert.el ends here
|