annotate lisp/utils/skeleton.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 ;;; skeleton.el --- Lisp language extension for writing statement skeletons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Copyright (C) 1993, 1994, 1995 by 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: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Maintainer: FSF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Keywords: extensions, abbrev, languages, tools
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Synched up with: FSF 19.30.
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 ;; A very concise language extension for writing structured statement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; skeleton insertion commands for programming language modes. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; originated in shell-script mode and was applied to ada-mode's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; commands which shrunk to one third. And these commands are now
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; user configurable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; page 1: statement skeleton language definition & interpreter
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; page 2: paired insertion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; page 3: mirror-mode, an example for setting up paired insertion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (defvar skeleton-transformation nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 "*If non-nil, function applied to literal strings before they are inserted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 It should take strings and characters and return them transformed, or nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 which means no transformation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 Typical examples might be `upcase' or `capitalize'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ; this should be a fourth argument to defvar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (put 'skeleton-transformation 'variable-interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 "aTransformation function: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (defvar skeleton-end-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (or (eolp) (newline-and-indent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 "Hook called at end of skeleton but before going to point of interest.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 By default this moves out anything following to next line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 The variables `v1' and `v2' are still set when calling this.")
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 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (defvar skeleton-filter 'identity
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 "Function for transforming a skeleton-proxy's aliases' variable value.")
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 skeleton-untabify t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 "When non-`nil' untabifies when deleting backwards with element -ARG.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (defvar skeleton-newline-indent-rigidly nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 "When non-`nil', indent rigidly under current line for element `\\n'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 Else use mode's `indent-line-function'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (defvar skeleton-further-elements ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 "A buffer-local varlist (see `let') of mode specific skeleton elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 These variables are bound while interpreting a skeleton. Their value may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 in turn be any valid skeleton element if they are themselves to be used as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 skeleton elements.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (make-variable-buffer-local 'skeleton-further-elements)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (defvar skeleton-subprompt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (substitute-command-keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 "RET, \\<minibuffer-local-map>\\[abort-recursive-edit] or \\[help-command]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 "*Replacement for %s in prompts of recursive subskeletons.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (defvar skeleton-abbrev-cleanup nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (eval-and-compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (defvar skeleton-debug nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 "*If non-nil `define-skeleton' will override previous definition."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;; reduce the number of compiler warnings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (defvar skeleton)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (defvar skeleton-modified)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (defvar skeleton-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (defvar skeleton-regions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (defmacro define-skeleton (command documentation &rest skeleton)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 "Define a user-configurable COMMAND that enters a statement skeleton.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 DOCUMENTATION is that of the command, while the variable of the same name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 which contains the skeleton, has a documentation to that effect.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 INTERACTOR and ELEMENT ... are as defined under `skeleton-insert'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (if skeleton-debug
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (set command skeleton))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 `(progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (defvar ,command ',skeleton ,documentation)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (defalias ',command 'skeleton-proxy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;; This command isn't meant to be called, only it's aliases with meaningful
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 ;; names are.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (defun skeleton-proxy (&optional str arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 "Insert skeleton defined by variable of same name (see `skeleton-insert').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 Prefix ARG allows wrapping around words or regions (see `skeleton-insert').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 This command can also be an abbrev expansion (3rd and 4th columns in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 \\[edit-abbrevs] buffer: \"\" command-name).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 When called as a function, optional first argument STR may also be a string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 which will be the value of `str' whereas the skeleton's interactor is then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 ignored."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (interactive "*P\nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (let ((function (nth 1 (backtrace-frame 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (if (eq function 'nth) ; uncompiled lisp function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (setq function (nth 1 (backtrace-frame 5)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (if (eq function 'byte-code) ; tracing byte-compiled function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (setq function (nth 1 (backtrace-frame 2)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (if (not (setq function (funcall skeleton-filter (symbol-value function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (if (memq this-command '(self-insert-command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 skeleton-pair-insert-maybe
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 expand-abbrev))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (setq buffer-undo-list (primitive-undo 1 buffer-undo-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (skeleton-insert function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (if (setq skeleton-abbrev-cleanup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (or (eq this-command 'self-insert-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (eq this-command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 'skeleton-pair-insert-maybe)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;; Pretend C-x a e passed its prefix arg to us
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (if (or arg current-prefix-arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (prefix-numeric-value (or arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 current-prefix-arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (if (stringp str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if skeleton-abbrev-cleanup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (setq deferred-action-list t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 deferred-action-function 'skeleton-abbrev-cleanup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 skeleton-abbrev-cleanup (point))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (defun skeleton-abbrev-cleanup (&rest list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 "Value for `post-command-hook' to remove char that expanded abbrev."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (if (integerp skeleton-abbrev-cleanup)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (delete-region skeleton-abbrev-cleanup (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (setq deferred-action-list ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 deferred-action-function nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 skeleton-abbrev-cleanup nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (defun skeleton-insert (skeleton &optional skeleton-regions str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 "Insert the complex statement skeleton SKELETON describes very concisely.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 With optional third REGIONS wrap first interesting point (`_') in skeleton
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 around next REGIONS words, if REGIONS is positive. If REGIONS is negative,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 wrap REGIONS preceding interregions into first REGIONS interesting positions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 \(successive `_'s) in skeleton. An interregion is the stretch of text between
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 two contiguous marked points. If you marked A B C [] (where [] is the cursor)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 in alphabetical order, the 3 interregions are simply the last 3 regions. But
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 if you marked B A [] C, the interregions are B-A, A-[], []-C.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 Optional fourth STR is the value for the variable `str' within the skeleton.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 When this is non-`nil' the interactor gets ignored, and this should be a valid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 skeleton element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 SKELETON is made up as (INTERACTOR ELEMENT ...). INTERACTOR may be nil if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 not needed, a prompt-string or an expression for complex read functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 If ELEMENT is a string or a character it gets inserted (see also
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 `skeleton-transformation'). Other possibilities are:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 \\n go to next line and indent according to mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 _ interesting point, interregion here, point after termination
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 > indent line (or interregion if > _) according to major mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 & do next ELEMENT if previous moved point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 | do next ELEMENT if previous didn't move point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 -num delete num preceding characters (see `skeleton-untabify')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 resume: skipped, continue here if quit is signaled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 nil skipped
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 Further elements can be defined via `skeleton-further-elements'. ELEMENT may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 itself be a SKELETON with an INTERACTOR. The user is prompted repeatedly for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 different inputs. The SKELETON is processed as often as the user enters a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 non-empty string. \\[keyboard-quit] terminates skeleton insertion, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 continues after `resume:' and positions at `_' if any. If INTERACTOR in such
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 a subskeleton is a prompt-string which contains a \".. %s ..\" it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 formatted with `skeleton-subprompt'. Such an INTERACTOR may also a list of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 strings with the subskeleton being repeated once for each string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 Quoted lisp-expressions are evaluated evaluated for their side-effect.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 Other lisp-expressions are evaluated and the value treated as above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 Note that expressions may not return `t' since this impplies an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 endless loop. Modes can define other symbols by locally setting them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 to any valid skeleton element. The following local variables are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 available:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 str first time: read a string according to INTERACTOR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 then: insert previously read string once more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 help help-form during interaction with the user or `nil'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 input initial input (string or cons with index) while reading str
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 v1, v2 local variables for memorising anything you want
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 When done with skeleton, but before going back to `_'-point call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 `skeleton-end-hook' if that is non-`nil'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (and skeleton-regions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (setq skeleton-regions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (if (> skeleton-regions 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (list (point-marker)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (save-excursion (forward-word skeleton-regions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (point-marker)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (setq skeleton-regions (- skeleton-regions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 ;; copy skeleton-regions - 1 elements from `mark-ring'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (let ((l1 (cons (mark-marker) mark-ring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (l2 (list (point-marker))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (while (and l1 (> skeleton-regions 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (setq l2 (cons (car l1) l2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 skeleton-regions (1- skeleton-regions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 l1 (cdr l1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (sort l2 '<))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (goto-char (car skeleton-regions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (setq skeleton-regions (cdr skeleton-regions)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (let ((beg (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 skeleton-modified skeleton-point resume: help input v1 v2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (eval `(let ,skeleton-further-elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (skeleton-internal-list skeleton str)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (run-hooks 'skeleton-end-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (sit-for 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (or (pos-visible-in-window-p beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (goto-char beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (recenter 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (if skeleton-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (goto-char skeleton-point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (defun skeleton-read (str &optional initial-input recursive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 "Function for reading a string from the minibuffer within skeletons.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 PROMPT may contain a `%s' which will be replaced by `skeleton-subprompt'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 If non-`nil' second arg INITIAL-INPUT or variable `input' is a string or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 cons with index to insert before reading. If third arg RECURSIVE is non-`nil'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 i.e. we are handling the iterator of a subskeleton, returns empty string if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 user didn't modify input.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 While reading, the value of `minibuffer-help-form' is variable `help' if that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 is non-`nil' or a default string."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (let ((minibuffer-help-form (or (if (boundp 'help) (symbol-value 'help))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (if recursive "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 As long as you provide input you will insert another subskeleton.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 If you enter the empty string, the loop inserting subskeletons is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 left, and the current one is removed as far as it has been entered.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 If you quit, the current subskeleton is removed as far as it has been
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 entered. No more of the skeleton will be inserted, except maybe for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 syntactically necessary termination."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 You are inserting a skeleton. Standard text gets inserted into the buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 automatically, and you are prompted to fill in the variable parts.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (eolp (eolp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 ;; since Emacs doesn't show main window's cursor, do something noticeable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (or eolp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (open-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (setq str (if (stringp str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (read-string (format str skeleton-subprompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (setq initial-input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (or initial-input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (symbol-value 'input))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (eval str)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (or eolp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (delete-char 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (if (and recursive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (or (null str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (string= str "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (equal str initial-input)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (equal str (car-safe initial-input))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (signal 'quit t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (defun skeleton-internal-list (skeleton &optional str recursive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (let* ((start (save-excursion (beginning-of-line) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (column (current-column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (line (buffer-substring start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (save-excursion (end-of-line) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (or str
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (setq str `(setq str (skeleton-read ',(car skeleton) nil ,recursive))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (while (setq skeleton-modified (eq opoint (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 opoint (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 skeleton (cdr skeleton))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (condition-case quit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (skeleton-internal-1 (car skeleton))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (quit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (if (eq (cdr quit) 'recursive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (setq recursive 'quit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 skeleton (memq 'resume: skeleton))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 ;; remove the subskeleton as far as it has been shown
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 ;; the subskeleton shouldn't have deleted outside current line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (delete-region start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (insert line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (move-to-column column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (if (cdr quit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (setq skeleton ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 recursive nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (signal 'quit 'recursive)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 ;; maybe continue loop or go on to next outer resume: section
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (if (eq recursive 'quit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (signal 'quit 'recursive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 recursive))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (defun skeleton-internal-1 (element &optional literal)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (cond ((char-or-string-p element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (if (and (integerp element) ; -num
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (< element 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (if skeleton-untabify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (backward-delete-char-untabify (- element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (delete-backward-char (- element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (insert-before-markers (if (and skeleton-transformation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (not literal))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (funcall skeleton-transformation element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 element))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 ((eq element '\n) ; actually (eq '\n 'n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (if (and skeleton-regions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (eq (nth 1 skeleton) '_))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 (or (eolp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (newline))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (indent-region (point) (car skeleton-regions) nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (if skeleton-newline-indent-rigidly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (indent-to (prog1 (current-indentation)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (newline)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (indent-according-to-mode))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 ((eq element '>)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (if (and skeleton-regions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (eq (nth 1 skeleton) '_))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (indent-region (point) (car skeleton-regions) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (indent-according-to-mode)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 ((eq element '_)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (if skeleton-regions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (goto-char (car skeleton-regions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (setq skeleton-regions (cdr skeleton-regions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (and (<= (current-column) (current-indentation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (eq (nth 1 skeleton) '\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (end-of-line 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (or skeleton-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (setq skeleton-point (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 ((eq element '&)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (if skeleton-modified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (setq skeleton (cdr skeleton))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 ((eq element '|)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (or skeleton-modified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (setq skeleton (cdr skeleton))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 ((eq 'quote (car-safe element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (eval (nth 1 element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 ((or (stringp (car-safe element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (consp (car-safe element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (if (symbolp (car-safe (car element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (while (skeleton-internal-list element nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (setq literal (car element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (while literal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (skeleton-internal-list element (car literal))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (setq literal (cdr literal)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 ((null element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 ((skeleton-internal-1 (eval element) t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 ;; Maybe belongs into simple.el or elsewhere
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (define-skeleton local-variables-section
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 "Insert a local variables section. Use current comment syntax if any."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 '(save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (if (re-search-forward page-delimiter nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (error "Not on last page.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 comment-start "Local Variables:" comment-end \n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 comment-start "mode: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (completing-read "Mode: " obarray
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 (lambda (symbol)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (if (commandp symbol)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (string-match "-mode$" (symbol-name symbol))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 & -5 | '(kill-line 0) & -1 | comment-end \n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 ( (completing-read (format "Variable, %s: " skeleton-subprompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 obarray
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (lambda (symbol)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 (or (eq symbol 'eval)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 (user-variable-p symbol)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 comment-start str ": "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 (read-from-minibuffer "Expression: " nil read-expression-map nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 'read-expression-history) | _
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 comment-end \n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 resume:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 comment-start "End:" comment-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;; Variables and command for automatically inserting pairs like () or "".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (defvar skeleton-pair nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 "*If this is nil pairing is turned off, no matter what else is set.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 Otherwise modes with `skeleton-pair-insert-maybe' on some keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 will attempt to insert pairs of matching characters.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (defvar skeleton-pair-on-word nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 "*If this is nil, paired insertion is inhibited before or inside a word.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (defvar skeleton-pair-filter (lambda ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 "Attempt paired insertion if this function returns nil, before inserting.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 This allows for context-sensitive checking whether pairing is appropriate.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (defvar skeleton-pair-alist ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 "An override alist of pairing partners matched against `last-command-char'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 Each alist element, which looks like (ELEMENT ...), is passed to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 `skeleton-insert' with no interactor. Variable `str' does nothing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 Elements might be (?` ?` _ \"''\"), (?\\( ? _ \" )\") or (?{ \\n > _ \\n ?} >).")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (defun skeleton-pair-insert-maybe (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 "Insert the character you type ARG times.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 With no ARG, if `skeleton-pair' is non-nil, and if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 `skeleton-pair-on-word' is non-nil or we are not before or inside a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 word, and if `skeleton-pair-filter' returns nil, pairing is performed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 If a match is found in `skeleton-pair-alist', that is inserted, else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 the defaults are used. These are (), [], {}, <> and `' for the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 symmetrical ones, and the same character twice for the others."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 (interactive "*P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 (if (or arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 overwrite-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (not skeleton-pair)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (if (not skeleton-pair-on-word) (looking-at "\\w"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (funcall skeleton-pair-filter))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (self-insert-command (prefix-numeric-value arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (self-insert-command 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 (if skeleton-abbrev-cleanup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 ;; (preceding-char) is stripped of any Meta-stuff in last-command-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 (if (setq arg (assq (preceding-char) skeleton-pair-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 ;; typed char is inserted (car is no real interactor)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (let (skeleton-end-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (skeleton-insert arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (insert (or (cdr (assq (preceding-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 '((?( . ?))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (?[ . ?])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (?{ . ?})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (?< . ?>)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (?` . ?'))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 last-command-char)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ;;; ;; A more serious example can be found in sh-script.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 ;;; ;; The quote before (defun prevents this from being byte-compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 ;;;(defun mirror-mode ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 ;;; "This major mode is an amusing little example of paired insertion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 ;;;All printable characters do a paired self insert, while the other commands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 ;;;work normally."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 ;;; (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 ;;; (kill-all-local-variables)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 ;;; (make-local-variable 'pair)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 ;;; (make-local-variable 'pair-on-word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 ;;; (make-local-variable 'pair-filter)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 ;;; (make-local-variable 'pair-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 ;;; (setq major-mode 'mirror-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 ;;; mode-name "Mirror"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 ;;; pair-on-word t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 ;;; ;; in the middle column insert one or none if odd window-width
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 ;;; pair-filter (lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 ;;; (if (>= (current-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 ;;; (/ (window-width) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 ;;; ;; insert both on next line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 ;;; (next-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 ;;; ;; insert one or both?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 ;;; (= (* 2 (1+ (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 ;;; (window-width))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 ;;; ;; mirror these the other way round as well
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 ;;; pair-alist '((?) _ ?()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 ;;; (?] _ ?[)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 ;;; (?} _ ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 ;;; (?> _ ?<)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 ;;; (?/ _ ?\\)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 ;;; (?\\ _ ?/)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 ;;; (?` ?` _ "''")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 ;;; (?' ?' _ "``"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 ;;; ;; in this mode we exceptionally ignore the user, else it's no fun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 ;;; pair t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 ;;; (let ((map (make-keymap))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 ;;; (i ? ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 ;;; (use-local-map map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 ;;; (setq map (car (cdr map)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 ;;; (while (< i ?\^?)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 ;;; (aset map i 'skeleton-pair-insert-maybe)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 ;;; (setq i (1+ i))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 ;;; (run-hooks 'mirror-mode-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 ;; skeleton.el ends here