662
|
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
|
14169
|
2
|
74439
|
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 1995, 1998, 2000, 2001, 2002,
|
106815
|
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
845
|
5
|
807
|
6 ;; Author: Charlie Martin <crm@cs.duke.edu>
|
23869
|
7 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
|
22250
|
8 ;; Keywords: convenience
|
17976
|
9 ;; Maintainer: FSF
|
180
|
10
|
|
11 ;; This file is part of GNU Emacs.
|
|
12
|
94678
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
180
|
14 ;; it under the terms of the GNU General Public License as published by
|
94678
|
15 ;; the Free Software Foundation, either version 3 of the License, or
|
|
16 ;; (at your option) any later version.
|
180
|
17
|
|
18 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
94678
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
180
|
25
|
807
|
26 ;;; Commentary:
|
180
|
27
|
12502
|
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 ;;
|
26661
|
34 ;; To use:
|
60876
|
35 ;; (add-hook 'find-file-hook 'auto-insert)
|
12502
|
36 ;; setq auto-insert-directory to an appropriate slash-terminated value
|
|
37 ;;
|
20809
|
38 ;; You can also customize the variable `auto-insert-mode' to load the
|
|
39 ;; package. Alternatively, add the following to your .emacs file:
|
|
40 ;; (auto-insert-mode 1)
|
|
41 ;;
|
12502
|
42 ;; Author: Charlie Martin
|
|
43 ;; Department of Computer Science and
|
|
44 ;; National Biomedical Simulation Resource
|
|
45 ;; Box 3709
|
|
46 ;; Duke University Medical Center
|
|
47 ;; Durham, NC 27710
|
26661
|
48 ;; (crm@cs.duke.edu,mcnc!duke!crm)
|
180
|
49
|
807
|
50 ;;; Code:
|
|
51
|
20809
|
52 (defgroup auto-insert nil
|
|
53 "Automatic mode-dependent insertion of text into new files."
|
|
54 :prefix "auto-insert-"
|
22250
|
55 :group 'files
|
95381
|
56 :group 'convenience
|
|
57 :link '(custom-manual "(autotype) Autoinserting"))
|
20809
|
58
|
|
59
|
|
60 (defcustom auto-insert 'not-modified
|
100171
|
61 "Controls automatic insertion into newly found empty files.
|
26661
|
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.
|
60876
|
70 when you do (add-hook 'find-file-hook '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
|
100171
|
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? "
|
100171
|
87 "Prompt to use when querying whether to auto-insert.
|
20809
|
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
|
99723
|
96 (file-name-sans-extension buffer-file-name))
|
12502
|
97 "_"
|
99723
|
98 (file-name-extension buffer-file-name)))
|
12502
|
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
|
109110
|
129 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") .
|
|
130 (lambda ()
|
104682
|
131 (if (eq major-mode (default-value 'major-mode))
|
109110
|
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) " "
|
55040
|
139 (getenv "ORGANIZATION") | (progn user-full-name)
|
49024
|
140 "
|
|
141 .\\\" You may distribute this file under the terms of the GNU Free
|
63623
|
142 .\\\" Documentation License.
|
49024
|
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) " "
|
58242
239eb406bf92
(auto-insert-alist): Insert the user's name in copyright notice,
John Paul Wallington <jpw@pobox.com>
diff
changeset
|
170 (getenv "ORGANIZATION") | (progn user-full-name) "
|
12502
|
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)
|
79586
|
181 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
|
12502
|
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
|
92253
fa0d39c9e1ee
(auto-insert-alist): Change permission text to match FSF's GPLv3 form.
Glenn Morris <rgm@gnu.org>
diff
changeset
|
188 \;; This program is free software; you can redistribute it and/or modify
|
81053
|
189 \;; it under the terms of the GNU General Public License as published by
|
92253
fa0d39c9e1ee
(auto-insert-alist): Change permission text to match FSF's GPLv3 form.
Glenn Morris <rgm@gnu.org>
diff
changeset
|
190 \;; the Free Software Foundation, either version 3 of the License, or
|
fa0d39c9e1ee
(auto-insert-alist): Change permission text to match FSF's GPLv3 form.
Glenn Morris <rgm@gnu.org>
diff
changeset
|
191 \;; (at your option) any later version.
|
12502
|
192
|
92253
fa0d39c9e1ee
(auto-insert-alist): Change permission text to match FSF's GPLv3 form.
Glenn Morris <rgm@gnu.org>
diff
changeset
|
193 \;; This program is distributed in the hope that it will be useful,
|
81053
|
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.
|
12502
|
197
|
81053
|
198 \;; You should have received a copy of the GNU General Public License
|
92253
fa0d39c9e1ee
(auto-insert-alist): Change permission text to match FSF's GPLv3 form.
Glenn Morris <rgm@gnu.org>
diff
changeset
|
199 \;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
12502
|
200
|
81053
|
201 \;;; Commentary:
|
12502
|
202
|
81053
|
203 \;; " _ "
|
12502
|
204
|
81053
|
205 \;;; Code:
|
12502
|
206
|
|
207
|
|
208
|
33794
|
209 \(provide '"
|
|
210 (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))
|
|
211 ")
|
85054
|
212 \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
|
|
213 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
|
|
214 "Title: "
|
|
215 "\\input texinfo @c -*-texinfo-*-
|
|
216 @c %**start of header
|
|
217 @setfilename "
|
|
218 (file-name-sans-extension
|
|
219 (file-name-nondirectory (buffer-file-name))) ".info\n"
|
|
220 "@settitle " str "
|
|
221 @c %**end of header
|
|
222 @copying\n"
|
|
223 (setq short-description (read-string "Short description: "))
|
|
224 ".\n\n"
|
|
225 "Copyright @copyright{} " (substring (current-time-string) -4) " "
|
|
226 (getenv "ORGANIZATION") | (progn user-full-name) "
|
|
227
|
|
228 @quotation
|
|
229 Permission is granted to copy, distribute and/or modify this document
|
99712
|
230 under the terms of the GNU Free Documentation License, Version 1.3
|
92292
|
231 or any later version published by the Free Software Foundation;
|
99712
|
232 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
|
|
233 A copy of the license is included in the section entitled ``GNU
|
92292
|
234 Free Documentation License''.
|
85054
|
235
|
|
236 A copy of the license is also available from the Free Software
|
|
237 Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
|
|
238
|
|
239 @end quotation
|
|
240
|
|
241 The document was typeset with
|
|
242 @uref{http://www.texinfo.org/, GNU Texinfo}.
|
|
243
|
|
244 @end copying
|
|
245
|
|
246 @titlepage
|
|
247 @title " str "
|
|
248 @subtitle " short-description "
|
|
249 @author " (getenv "ORGANIZATION") | (progn user-full-name)
|
|
250 " <" (progn user-mail-address) ">
|
|
251 @page
|
|
252 @vskip 0pt plus 1filll
|
|
253 @insertcopying
|
|
254 @end titlepage
|
|
255
|
|
256 @c Output the table of the contents at the beginning.
|
|
257 @contents
|
|
258
|
|
259 @ifnottex
|
|
260 @node Top
|
|
261 @top " str "
|
|
262
|
|
263 @insertcopying
|
|
264 @end ifnottex
|
|
265
|
|
266 @c Generate the nodes for this menu with `C-c C-u C-m'.
|
|
267 @menu
|
|
268 @end menu
|
|
269
|
|
270 @c Update all node entries with `C-c C-u C-n'.
|
|
271 @c Insert new nodes with `C-c C-c n'.
|
|
272 @node Chapter One
|
|
273 @chapter Chapter One
|
|
274
|
|
275 " _ "
|
|
276
|
|
277 @node Copying This Manual
|
|
278 @appendix Copying This Manual
|
|
279
|
|
280 @menu
|
|
281 * GNU Free Documentation License:: License for copying this manual.
|
|
282 @end menu
|
|
283
|
|
284 @c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
|
|
285 @include fdl.texi
|
|
286
|
|
287 @node Index
|
|
288 @unnumbered Index
|
|
289
|
|
290 @printindex cp
|
|
291
|
|
292 @bye
|
|
293
|
|
294 @c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
|
180
|
295 "A list specifying text to insert by default into a new file.
|
12502
|
296 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
|
73274
|
297 CONDITION may be a regexp that must match the new file's name, or it may be
|
12502
|
298 a symbol that must match the major mode for this element to apply.
|
|
299 Only the first matching element is effective.
|
|
300 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
|
|
301 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
|
|
302 file-name or one relative to `auto-insert-directory' or a function to call.
|
|
303 ACTION may also be a vector containing several successive single actions as
|
20809
|
304 described above, e.g. [\"header.insert\" date-and-author-update]."
|
|
305 :type 'sexp
|
|
306 :group 'auto-insert)
|
180
|
307
|
12502
|
308
|
|
309 ;; Establish a default value for auto-insert-directory
|
20809
|
310 (defcustom auto-insert-directory "~/insert/"
|
100171
|
311 "Directory from which auto-inserted files are taken.
|
45025
|
312 The value must be an absolute directory name;
|
|
313 thus, on a GNU or Unix system, it must end in a slash."
|
20809
|
314 :type 'directory
|
|
315 :group 'auto-insert)
|
180
|
316
|
12502
|
317
|
|
318 ;;;###autoload
|
|
319 (defun auto-insert ()
|
31878
|
320 "Insert default contents into new files if variable `auto-insert' is non-nil.
|
180
|
321 Matches the visited file name against the elements of `auto-insert-alist'."
|
12502
|
322 (interactive)
|
|
323 (and (not buffer-read-only)
|
|
324 (or (eq this-command 'auto-insert)
|
|
325 (and auto-insert
|
|
326 (bobp) (eobp)))
|
|
327 (let ((alist auto-insert-alist)
|
|
328 case-fold-search cond desc action)
|
|
329 (goto-char 1)
|
|
330 ;; find first matching alist entry
|
|
331 (while alist
|
|
332 (if (atom (setq cond (car (car alist))))
|
|
333 (setq desc cond)
|
|
334 (setq desc (cdr cond)
|
|
335 cond (car cond)))
|
|
336 (if (if (symbolp cond)
|
|
337 (eq cond major-mode)
|
31878
|
338 (and buffer-file-name
|
|
339 (string-match cond buffer-file-name)))
|
12502
|
340 (setq action (cdr (car alist))
|
|
341 alist nil)
|
|
342 (setq alist (cdr alist))))
|
180
|
343
|
12502
|
344 ;; Now, if we found something, do it
|
|
345 (and action
|
100278
|
346 (or (not (stringp action))
|
|
347 (file-readable-p (expand-file-name
|
|
348 action auto-insert-directory)))
|
|
349 (or (not auto-insert-query)
|
|
350 (if (eq auto-insert-query 'function)
|
|
351 (eq this-command 'auto-insert))
|
|
352 (y-or-n-p (format auto-insert-prompt desc)))
|
84857
|
353 (mapc
|
12502
|
354 (lambda (action)
|
|
355 (if (stringp action)
|
|
356 (if (file-readable-p
|
100278
|
357 (setq action (expand-file-name
|
|
358 action auto-insert-directory)))
|
12502
|
359 (insert-file-contents action))
|
|
360 (save-window-excursion
|
|
361 ;; make buffer visible before skeleton or function
|
|
362 ;; which might ask the user for something
|
|
363 (switch-to-buffer (current-buffer))
|
|
364 (if (and (consp action)
|
|
365 (not (eq (car action) 'lambda)))
|
|
366 (skeleton-insert action)
|
|
367 (funcall action)))))
|
|
368 (if (vectorp action)
|
|
369 action
|
|
370 (vector action))))
|
|
371 (and (buffer-modified-p)
|
|
372 (not (eq this-command 'auto-insert))
|
26661
|
373 (set-buffer-modified-p (eq auto-insert t)))))
|
|
374 ;; Return nil so that it could be used in
|
|
375 ;; `find-file-not-found-hooks', though that's probably inadvisable.
|
|
376 nil)
|
180
|
377
|
12502
|
378
|
|
379 ;;;###autoload
|
21287
|
380 (defun define-auto-insert (condition action &optional after)
|
12502
|
381 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
|
|
382 Optional AFTER means to insert action after all existing actions for CONDITION,
|
|
383 or if CONDITION had no actions, after all other CONDITIONs."
|
21287
|
384 (let ((elt (assoc condition auto-insert-alist)))
|
12502
|
385 (if elt
|
|
386 (setcdr elt
|
|
387 (if (vectorp (cdr elt))
|
|
388 (vconcat (if after (cdr elt))
|
|
389 (if (vectorp action) action (vector action))
|
|
390 (if after () (cdr elt)))
|
|
391 (if after
|
|
392 (vector (cdr elt) action)
|
|
393 (vector action (cdr elt)))))
|
|
394 (if after
|
21287
|
395 (nconc auto-insert-alist (list (cons condition action)))
|
100278
|
396 (push (cons condition action) auto-insert-alist)))))
|
662
|
397
|
20809
|
398 ;;;###autoload
|
32025
|
399 (define-minor-mode auto-insert-mode
|
26661
|
400 "Toggle Auto-insert mode.
|
|
401 With prefix ARG, turn Auto-insert mode on if and only if ARG is positive.
|
|
402 Returns the new status of Auto-insert mode (non-nil means on).
|
20809
|
403
|
26661
|
404 When Auto-insert mode is enabled, when new files are created you can
|
20809
|
405 insert a template for the file depending on the mode of the buffer."
|
33184
|
406 :global t :group 'auto-insert
|
32025
|
407 (if auto-insert-mode
|
60876
|
408 (add-hook 'find-file-hook 'auto-insert)
|
|
409 (remove-hook 'find-file-hook 'auto-insert)))
|
20809
|
410
|
18383
|
411 (provide 'autoinsert)
|
|
412
|
60876
|
413 ;; arch-tag: 5b6630ac-c735-43cf-b097-b78c622af909
|
662
|
414 ;;; autoinsert.el ends here
|