annotate lisp/packages/autoinsert.el @ 0:376386a54a3c r19-14

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