comparison lisp/autoinsert.el @ 12502:8bb3424bf745

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