annotate lisp/packages/autoinsert.el @ 76:c0c698873ce1 r20-0b33

Import from CVS: tag r20-0b33
author cvs
date Mon, 13 Aug 2007 09:05:10 +0200
parents b9518feda344
children 489f57a838ef
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (defvar auto-insert 'not-modified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 "*Controls automatic insertion into newly found empty files:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 nil do nothing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 t insert if possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 other insert if possible, but mark as unmodified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 Insertion is possible when something appropriate is found in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 `auto-insert-alist'. When the insertion is marked as unmodified, you can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 save it with \\[write-file] RET.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 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
57 when you do (add-hook 'find-file-hooks 'auto-insert).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 With \\[auto-insert], this is always treated as if it were `t'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (defvar auto-insert-query 'function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 "*If non-`nil', ask user before auto-inserting.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 When this is `function', only ask when called non-interactively.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (defvar auto-insert-prompt "Perform %s auto-insertion? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 "*Prompt to use when querying whether to auto-insert.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 If this contains a %s, that will be replaced by the matching rule.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (defvar auto-insert-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (upcase (concat (file-name-nondirectory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (substring buffer-file-name 0 (match-beginning 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 "_"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (substring buffer-file-name (1+ (match-beginning 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 "#ifndef " str \n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 "#define " str "\n\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 _ "\n\n#endif")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 "#include \""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;; nop without latest cc-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (and (fboundp 'c-companion-file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;(file-readable-p (c-companion-file 'name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (file-name-nondirectory (c-companion-file 'name))) & ?\"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 | -10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ("[Mm]akefile\\'" . "makefile.inc")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
92 (html-mode . (lambda () (sgml-tag "html")))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
93
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (plain-tex-mode . "tex-insert.tex")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (bibtex-mode . "tex-insert.tex")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (latex-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;; should try to offer completing read for these
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 "options, RET: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 "\\documentstyle[" str & ?\] | -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ?{ (read-string "class: ") "}\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ("package, %s: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 _ "\n\\begin{document}\n" _
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 "\n\\end{document}")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (if (eq major-mode default-major-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (sh-mode)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (ada-mode . ada-header)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (("\\.el\\'" . "Emacs Lisp header")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 "Short description: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; Copyright (C) " (substring (current-time-string) -4) " by "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (getenv "ORGANIZATION") | "Free Software Foundation, Inc." "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; Author: " (user-full-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 '(if (search-backward "&" (save-excursion (beginning-of-line 1) (point)) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (replace-match (capitalize (user-login-name)) t t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 '(end-of-line 1) " <" (user-login-name) ?@ (system-name) ">
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 ;; Keywords: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 '(require 'finder)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 finder-known-keywords)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 v2 (mapconcat (lambda (x) (format "%10.0s: %s" (car x) (cdr x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 finder-known-keywords
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ((let ((minibuffer-help-form v2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (completing-read "Keyword, C-h: " v1 nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 str ", ") & -2 "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
136 ;; This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
138 ;; XEmacs is free software; you can redistribute it and/or modify it
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
139 ;; under the terms of the GNU General Public License as published by
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
143 ;; XEmacs is distributed in the hope that it will be useful, but
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
144 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
145 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
146 ;; General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 ;; 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
149 ;; 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
150 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
151 ;; 02111-1307, USA.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
152
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
153 ;;; Synched up with: Not in FSF
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ;; " _ "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
162 (provide ' " (file-name-nondirectory (buffer-file-name)) ")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;;; " (file-name-nondirectory (buffer-file-name)) " ends here"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 "A list specifying text to insert by default into a new file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 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
168 a symbol that must match the major mode for this element to apply.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 Only the first matching element is effective.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 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
173 ACTION may also be a vector containing several successive single actions as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 described above, e.g. [\"header.insert\" date-and-author-update].")
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 ;; Establish a default value for auto-insert-directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (defvar auto-insert-directory "~/insert/"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 "*Directory from which auto-inserted files are taken.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (defun auto-insert ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 "Insert default contents into a new file if `auto-insert' is non-nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 Matches the visited file name against the elements of `auto-insert-alist'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (and (not buffer-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (or (eq this-command 'auto-insert)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (and auto-insert
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (bobp) (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (let ((alist auto-insert-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 case-fold-search cond desc action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (goto-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 ;; find first matching alist entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (while alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (if (atom (setq cond (car (car alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (setq desc cond)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (setq desc (cdr cond)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 cond (car cond)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (if (if (symbolp cond)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (eq cond major-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (string-match cond buffer-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (setq action (cdr (car alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 alist nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (setq alist (cdr alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 ;; Now, if we found something, do it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (and action
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (if (stringp action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (file-readable-p (concat auto-insert-directory action))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (if auto-insert-query
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (or (if (eq auto-insert-query 'function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (eq this-command 'auto-insert))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (y-or-n-p (format auto-insert-prompt desc)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (lambda (action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (if (stringp action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (if (file-readable-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (setq action (concat auto-insert-directory action)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (insert-file-contents action))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 ;; make buffer visible before skeleton or function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 ;; which might ask the user for something
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (switch-to-buffer (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (if (and (consp action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (not (eq (car action) 'lambda)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (skeleton-insert action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (funcall action)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (if (vectorp action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 action
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (vector action))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (and (buffer-modified-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (not (eq this-command 'auto-insert))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (set-buffer-modified-p (eq auto-insert t))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (defun define-auto-insert (key action &optional after)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 Optional AFTER means to insert action after all existing actions for CONDITION,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 or if CONDITION had no actions, after all other CONDITIONs."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (let ((elt (assoc key auto-insert-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (if elt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (setcdr elt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (if (vectorp (cdr elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (vconcat (if after (cdr elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (if (vectorp action) action (vector action))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (if after () (cdr elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (if after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (vector (cdr elt) action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (vector action (cdr elt)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (if after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (nconc auto-insert-alist (list (cons key action)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (setq auto-insert-alist (cons (cons key action)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 auto-insert-alist))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
259 (provide 'autoinsert)
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
260
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 ;;; autoinsert.el ends here