1 ;; expand.el --- minor mode to make abbreviations more usable.
3 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Frederic Lepied <Frederic.Lepied@sugix.frmug.org>
6 ;; Maintainer: Frederic Lepied <Frederic.Lepied@sugix.frmug.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package defines abbrevs which expand into structured constructs
29 ;; for certain languages. The construct is indented for you,
30 ;; and contains points for you to ;; fill in other text.
32 ;; These abbrevs expand only at the end of a line and when not in a comment
35 ;; Look at the Sample: section for emacs-lisp, perl and c expand lists.
36 ;; For example for c-mode, you could declare your abbrev table with :
38 ;; (defconst c-expand-list
39 ;; '(("if" "if () {\n \n} else {\n \n}" (5 10 21))
40 ;; ("ifn" "if () {}" (5 8))
41 ;; ("uns" "unsigned ")
42 ;; ("for" "for(; ; ) {\n\n}" (5 7 9 13))
43 ;; ("switch" "switch () {\n\n}" (9 13))
44 ;; ("case" "case :\n\nbreak;\n" (6 8 16))
45 ;; ("do" "do {\n\n} while ();" (6 16))
46 ;; ("while" "while () {\n\n}" (8 12))
47 ;; ("default" "default:\n\nbreak;" 10)
48 ;; ("main" "int\nmain(int argc, char * argv[])\n{\n\n}\n" 37))
49 ;; "Expansions for C mode")
51 ;; and enter Expand mode with the following hook :
53 ;; (add-hook 'c-mode-hook (function (lambda ()
54 ;; (expand-add-abbrevs c-mode-abbrev-table c-expand-list)
57 ;; you can also bind jump functions to some keys and init some post-process
60 ;; (add-hook 'expand-mode-load-hook
63 ;; (add-hook 'expand-expand-hook 'indent-according-to-mode)
64 ;; (add-hook 'expand-jump-hook 'indent-according-to-mode)
65 ;; (define-key expand-map '[(control tab)] 'expand-jump-to-next-mark)
66 ;; (define-key expand-map '[(control shift tab)] 'expand-jump-to-previous-mark))))
70 ;; Has been tested under emacs 19.28-19.34 and XEmacs 19.11.
71 ;; Many thanks to Heddy Boubaker <boubaker@cenatls.cena.dgac.fr>,
72 ;; Jerome Santini <santini@chambord.univ-orleans.fr>,
73 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>.
75 ;; Please send me a word to give me your feeling about this mode or
76 ;; to explain me how you use it (your expansions table for example) using
77 ;; the function expand-mode-submit-report.
79 ;; Expand mode is not a replacement for abbrev it is just a layer above it.
83 (defconst expand-mode-version
"$Id: expand.el,v 1.1 1996/12/28 19:41:45 rms Exp rms $"
84 "Version tag for expand.el.")
86 (defconst expand-mode-help-address
"expand-help@sugix.frmug.org"
87 "Email address to send requests, comments or bug reports.")
89 (defvar expand-mode nil
90 "Status variable for Expand mode.")
91 (make-variable-buffer-local 'expand-mode
)
93 (defvar expand-mode-name
" Expand"
94 "Name of mode displayed in the modeline for Expand mode.")
96 (defvar expand-mode-hook nil
97 "Hooks run when Expand mode is enabled.")
99 (defvar expand-mode-load-hook nil
100 "Hooks run when expand is loaded.")
102 (defvar expand-expand-hook nil
103 "Hooks run when expansion is done.")
105 (defvar expand-jump-hook nil
106 "Hooks run when jump to mark occurs.")
110 (define-skeleton expand-c-for-skeleton
"For loop skeleton"
112 "for(" str _
@ "=0; " str
@ "; " str
@ ") {" \n
117 (defconst expand-c-sample-expand-list
118 '(("if" "if () {\n \n} else {\n \n}" (5 10 21))
119 ("ifn" "if () {}" (5 8))
121 ("for" expand-c-for-skeleton
)
122 ("switch" "switch () {\n\n}" (9 13))
123 ("case" "case :\n\nbreak;\n" (6 8 16))
124 ("do" "do {\n\n} while ();" (6 16))
125 ("while" "while () {\n\n}" (8 12))
126 ("default" "default:\n\nbreak;" 10)
127 ("main" "int\nmain(int argc, char * argv[])\n{\n\n}\n" 37))
128 "Expansions for C mode. See `expand-add-abbrevs'.")
130 ;; lisp example from Jari Aalto <jaalto@tre.tele.nokia.fi>
131 (defconst expand-sample-lisp-mode-expand-list
143 (list 8 11 16 32 43 59))
152 (list 11 14 19 23 39))
166 "(defadvice (around act)\n"
174 "(defconst nil\n \"\")\n"
179 "(defvar nil\n \"\")\n"
189 "(save-excursion\n \n)"
194 "(autoload ' \"\" t t)\n"
198 "Expansions for Lisp mode. See `expand-add-abbrevs'.")
200 ;; perl example from Jari Aalto <jaalto@tre.tele.nokia.fi>
201 (defconst expand-sample-perl-mode-expand-list
204 ;; This is default perl4 subroutine template
208 "#" (make-string 70 ?-
) "\n"
219 " local( $f ) = \"$lib.\";\n" ;; Function name AFTER period
220 " local() = @_;\n" ;; func arguments here
224 (list 77 88 120 146 159 176))
243 ;; The normal "if" can be used like
244 ;; print $F "xxxxxx" if defined @arr;
254 (list "loc" "local( $ );" (list 9))
255 (list "my" "my( $ );" (list 6))
256 (list "ope" "open(,\"\")\t|| die \"$f: Can't open [$]\";" (list 6 8 36))
257 (list "clo" "close ;" 7)
258 (list "def" "defined " (list 9))
259 (list "und" "undef ;" (list 7))
261 ;; There is no ending colon, because they can be in statement
262 ;; defined $REXP_NOT_NEW && (print "xxxxx" );
264 (list "pr" "print " 7)
265 (list "pf" "printf " 8)
268 (list "gre" "grep( //, );" (list 8 11))
269 (list "pus" "push( , );" (list 7 9))
270 (list "joi" "join( '', );" (list 7 11))
271 (list "rtu" "return ;" (list 8))
274 "Expansions for Perl mode. See `expand-add-abbrevs'.")
279 (defun expand-mode (&optional arg
)
281 With argument ARG, turn Expand mode on if ARG is positive.
282 In Expand mode, inserting an abbreviation at the end of a line
283 causes it to expand and be replaced by its expansion."
285 (setq expand-mode
(if (null arg
) (not expand-mode
)
286 (> (prefix-numeric-value arg
) 0)))
289 (setq abbrev-mode nil
)
290 (run-hooks 'expand-mode-hook
))))
293 (defvar expand-map
(make-sparse-keymap)
294 "Key map used in Expand mode.")
296 (or (assq 'expand-mode minor-mode-alist
)
297 (setq minor-mode-alist
(cons (list 'expand-mode expand-mode-name
)
300 (or (assq 'expand-mode minor-mode-map-alist
)
301 (setq minor-mode-map-alist
(cons (cons 'expand-mode expand-map
)
302 minor-mode-map-alist
)))
305 (defun expand-add-abbrevs (table abbrevs
)
306 "Add a list of abbrev to abbrev table TABLE.
307 ABBREVS is a list of abbrev definitions; each abbrev description entry
308 has the form (ABBREV EXPANSION ARG).
310 ABBREV is the abbreviation to replace.
312 EXPANSION is the replacement string or a function which will make the
313 expansion. For example you, could use the DMacros or skeleton packages
314 to generate such functions.
316 ARG is an optional argument which can be a number or a list of
317 numbers. If ARG is a number, point is placed ARG chars from the
318 beginning of the expanded text.
320 If ARG is a list of numbers, point is placed according to the first
321 member of the list, but you can visit the other specified positions
322 cyclicaly with the functions `expand-jump-to-previous-mark' and
323 `expand-jump-to-next-mark'.
325 If ARG is omitted, point is placed at the end of the expanded text."
329 (expand-add-abbrev table
(nth 0 (car abbrevs
)) (nth 1 (car abbrevs
))
330 (nth 2 (car abbrevs
)))
331 (expand-add-abbrevs table
(cdr abbrevs
))))
333 (defvar expand-list nil
"Temporary variable used by Expand mode.")
335 (defvar expand-pos nil
336 "If non nil, stores a vector containing markers to positions defined by the last expansion.
337 This variable is local to a buffer.")
338 (make-variable-buffer-local 'expand-pos
)
340 (defvar expand-index
0
341 "Index of the last marker used in `expand-pos'.
342 This variable is local to a buffer.")
343 (make-variable-buffer-local 'expand-index
)
345 (defvar expand-point nil
346 "End of the expanded region.
347 This variable is local to a buffer.")
348 (make-variable-buffer-local 'expand-point
)
350 (defun expand-add-abbrev (table abbrev expansion arg
)
351 "Add one abbreviation and provide the hook to move to the specified positions."
352 (let* ((string-exp (if (and (symbolp expansion
) (fboundp expansion
))
355 (position (if (and arg string-exp
)
357 (- (length expansion
) (1- (car arg
)))
358 (- (length expansion
) (1- arg
)))
367 (cons (length string-exp
) arg
)
369 (if (and (symbolp expansion
) (fboundp expansion
))
373 'expand-abbrev-hook
)))
375 (put 'expand-abbrev-hook
'no-self-insert t
)
376 (defun expand-abbrev-hook ()
377 "Abbrev hook used to do the expansion job of expand abbrevs.
378 See `expand-add-abbrevs'."
379 ;; Expand only at the end of a line if we are near a word that has
380 ;; an abbrev built from expand-add-abbrev.
382 (not (expand-in-literal)))
384 (setq expand-point nil
)
385 ;; don't expand if the preceding char isn't a word constituent
386 (if (and (eq (char-syntax (preceding-char))
388 (expand-do-expansion))
390 ;; expand-point tells us if we have inserted the text
391 ;; ourself or if it is the hook which has done the job.
394 (if (vectorp expand-list
)
395 (expand-build-marks expand-point
))
396 (indent-region p expand-point nil
))
397 ;; an outside function can set expand-list to a list of
398 ;; markers in reverse order.
399 (if (listp expand-list
)
401 expand-pos
(expand-list-to-markers expand-list
)
403 (run-hooks 'expand-expand-hook
)
407 (defun expand-do-expansion ()
408 (delete-backward-char (length last-abbrev-text
))
409 (let* ((vect (symbol-value last-abbrev
))
411 (position (aref vect
1))
412 (jump-args (aref vect
2))
413 (hook (aref vect
3)))
416 (setq expand-point
(point))))
418 (funcall 'expand-build-list
(car jump-args
) (cdr jump-args
)))
420 (backward-char position
))
426 (defun expand-abbrev-from-expand (word)
427 "Test if an abbrev has a hook."
429 (and (intern-soft word local-abbrev-table
)
430 (symbol-function (intern-soft word local-abbrev-table
)))
431 (and (intern-soft word global-abbrev-table
)
432 (symbol-function (intern-soft word global-abbrev-table
)))))
434 (defun expand-previous-word ()
435 "Return the previous word."
439 (buffer-substring p
(point)))))
441 (defun expand-jump-to-previous-mark ()
442 "Move the cursor to previous mark created by the expansion."
446 (setq expand-index
(1- expand-index
))
447 (if (< expand-index
0)
448 (setq expand-index
(1- (length expand-pos
))))
449 (goto-char (aref expand-pos expand-index
))
450 (run-hooks 'expand-jump-hook
))))
452 (defun expand-jump-to-next-mark ()
453 "Move the cursor to next mark created by the expansion."
457 (setq expand-index
(1+ expand-index
))
458 (if (>= expand-index
(length expand-pos
))
459 (setq expand-index
0))
460 (goto-char (aref expand-pos expand-index
))
461 (run-hooks 'expand-jump-hook
))))
463 (defun expand-build-list (len l
)
464 "Build a vector of offset positions from the list of positions."
465 (expand-clear-markers)
466 (setq expand-list
(vconcat l
))
468 (lenlist (length expand-list
)))
470 (aset expand-list i
(- len
(1- (aref expand-list i
))))
474 (defun expand-build-marks (p)
475 "Transform the offsets vector into a marker vector."
478 (setq expand-index
0)
479 (setq expand-pos
(make-vector (length expand-list
) nil
))
480 (let ((i (1- (length expand-list
))))
482 (aset expand-pos i
(copy-marker (- p
(aref expand-list i
))))
484 (setq expand-list nil
))))
486 (defun expand-clear-markers ()
487 "Make the markers point nowhere."
490 (let ((i (1- (length expand-pos
))))
492 (set-marker (aref expand-pos i
) nil
)
494 (setq expand-pos nil
))))
496 (defun expand-in-literal ()
497 "Test if we are in a comment or in a string."
499 (let* ((lim (or (save-excursion
504 (state (parse-partial-sexp lim
(point))))
506 ((nth 3 state
) 'string
)
507 ((nth 4 state
) 'comment
)
510 (defun expand-mode-submit-report ()
511 "Report a problem, a suggestion or a comment about Expand mode."
514 (reporter-submit-bug-report
515 expand-mode-help-address
516 (concat "expand.el " expand-mode-version
)
519 expand-mode-load-hook
524 "Dear expand.el maintainer,"))
526 ;; support functions to add marks to jump from outside function
528 (defun expand-list-to-markers (l)
529 "Transform a list of markers in reverse order into a vector in the correct order."
530 (let* ((len (1- (length l
)))
532 (v (make-vector (+ len
1) nil
)))
534 (aset v loop
(if (markerp (car l
)) (car l
) (copy-marker (car l
))))
539 ;; integration with skeleton.el
540 ;; Used in `skeleton-end-hook' to fetch the positions for @ skeleton tags.
541 ;; See `skeleton-insert'.
542 (defun expand-skeleton-end-hook ()
543 (if skeleton-positions
544 (setq expand-list skeleton-positions
)))
546 (add-hook 'skeleton-end-hook
(function expand-skeleton-end-hook
))
551 (run-hooks 'expand-mode-load-hook
)
553 ;;; expand.el ends here