annotate lisp/autoinsert.el @ 12824:9302372b1ec1

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