annotate lisp/packages/autoinsert.el @ 189:489f57a838ef r20-3b21

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