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