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