(MKTIME_OBJ):
[emacs.git] / lisp / expand.el
blob121a97514bc85dd0c55853a0382f096006c8805d
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>
7 ;; Keywords: abbrev
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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
33 ;; or a string.
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")
50 ;;
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)
55 ;; (expand-mode))))
57 ;; you can also bind jump functions to some keys and init some post-process
58 ;; hooks :
60 ;; (add-hook 'expand-mode-load-hook
61 ;; (function
62 ;; (lambda ()
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))))
68 ;; Remarks:
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.
81 ;;; Constants:
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.")
108 ;;; Samples:
110 (define-skeleton expand-c-for-skeleton "For loop skeleton"
111 "Loop var: "
112 "for(" str _ @ "=0; " str @ "; " str @ ") {" \n
113 @ _ \n
114 "}" >
117 (defconst expand-c-sample-expand-list
118 '(("if" "if () {\n \n} else {\n \n}" (5 10 21))
119 ("ifn" "if () {}" (5 8))
120 ("uns" "unsigned ")
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
132 (list
133 (list
134 "defu"
135 (concat
136 "(defun ()\n"
137 " \"\"\n"
138 " (interactive)\n"
139 " (let* (\n"
140 " )\n"
141 " \n"
142 " ))")
143 (list 8 11 16 32 43 59))
145 (list
146 "defs"
147 (concat
148 "(defsubst ()\n"
149 " \"\"\n"
150 " (interactive)\n"
151 " )")
152 (list 11 14 19 23 39))
154 (list
155 "defm"
156 (concat
157 "(defmacro ()\n"
158 " \"\"\n"
159 " (` \n"
160 " ))")
161 (list 11 13 18 25))
163 (list
164 "defa"
165 (concat
166 "(defadvice (around act)\n"
167 " \"\"\n"
168 " \n"
169 " )")
170 (list 12 22 32 36))
172 (list
173 "defc"
174 "(defconst nil\n \"\")\n"
175 (list 11 13 20))
177 (list
178 "defv"
179 "(defvar nil\n \"\")\n"
180 (list 9 11 18))
182 (list
183 "let"
184 "(let* (\n)\n "
185 (list 8 13))
187 (list
188 "sav"
189 "(save-excursion\n \n)"
190 (list 18))
192 (list
193 "aut"
194 "(autoload ' \"\" t t)\n"
195 (list 12 14))
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
202 (list
203 (list
204 ;; This is default perl4 subroutine template
206 "sub"
207 (concat
208 "#" (make-string 70 ?-) "\n"
209 "sub {\n"
210 " # DESCRIPTION\n"
211 " # \n"
212 " # \n"
213 " # INPUT\n"
214 " # \n"
215 " # \n"
216 " # RETURN\n"
217 " # \n"
218 "\n"
219 " local( $f ) = \"$lib.\";\n" ;; Function name AFTER period
220 " local() = @_;\n" ;; func arguments here
221 " \n"
222 " \n}\n"
224 (list 77 88 120 146 159 176))
226 (list
227 "for" ; foreach
228 (concat
229 "for ( )\n"
230 "{\n\n\}"
232 (list 7 12))
234 (list
235 "whi" ; foreach
236 (concat
237 "while ( )\n"
238 "{\n\n\}"
240 (list 9 15))
243 ;; The normal "if" can be used like
244 ;; print $F "xxxxxx" if defined @arr;
246 (list
247 "iff"
248 (concat
249 "if ( )\n"
250 "{\n\n\}"
252 (list 6 12))
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'.")
276 ;;; Code:
278 ;;;###autoload
279 (defun expand-mode (&optional arg)
280 "Toggle Expand mode.
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."
284 (interactive "P")
285 (setq expand-mode (if (null arg) (not expand-mode)
286 (> (prefix-numeric-value arg) 0)))
287 (if expand-mode
288 (progn
289 (setq abbrev-mode nil)
290 (run-hooks 'expand-mode-hook))))
292 ;;;###autoload
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)
298 minor-mode-alist)))
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)))
304 ;;;###autoload
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."
327 (if (null abbrevs)
328 table
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))
354 expansion))
355 (position (if (and arg string-exp)
356 (if (listp arg)
357 (- (length expansion) (1- (car arg)))
358 (- (length expansion) (1- arg)))
359 0)))
360 (define-abbrev
361 table
362 abbrev
363 (vector string-exp
364 position
365 (if (and (listp arg)
366 (not (null arg)))
367 (cons (length string-exp) arg)
368 nil)
369 (if (and (symbolp expansion) (fboundp expansion))
370 expansion
371 nil)
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.
381 (if (and (eolp)
382 (not (expand-in-literal)))
383 (let ((p (point)))
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))
389 (progn
390 ;; expand-point tells us if we have inserted the text
391 ;; ourself or if it is the hook which has done the job.
392 (if expand-point
393 (progn
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)
400 (setq expand-index 0
401 expand-pos (expand-list-to-markers expand-list)
402 expand-list nil)))
403 (run-hooks 'expand-expand-hook)
404 t))))
407 (defun expand-do-expansion ()
408 (delete-backward-char (length last-abbrev-text))
409 (let* ((vect (symbol-value last-abbrev))
410 (text (aref vect 0))
411 (position (aref vect 1))
412 (jump-args (aref vect 2))
413 (hook (aref vect 3)))
414 (cond (text
415 (insert text)
416 (setq expand-point (point))))
417 (if jump-args
418 (funcall 'expand-build-list (car jump-args) (cdr jump-args)))
419 (if position
420 (backward-char position))
421 (if hook
422 (funcall hook))
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."
436 (save-excursion
437 (let ((p (point)))
438 (backward-word 1)
439 (buffer-substring p (point)))))
441 (defun expand-jump-to-previous-mark ()
442 "Move the cursor to previous mark created by the expansion."
443 (interactive)
444 (if expand-pos
445 (progn
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."
454 (interactive)
455 (if expand-pos
456 (progn
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))
467 (let ((i 0)
468 (lenlist (length expand-list)))
469 (while (< i lenlist)
470 (aset expand-list i (- len (1- (aref expand-list i))))
471 (setq i (1+ i))))
474 (defun expand-build-marks (p)
475 "Transform the offsets vector into a marker vector."
476 (if expand-list
477 (progn
478 (setq expand-index 0)
479 (setq expand-pos (make-vector (length expand-list) nil))
480 (let ((i (1- (length expand-list))))
481 (while (>= i 0)
482 (aset expand-pos i (copy-marker (- p (aref expand-list i))))
483 (setq i (1- i))))
484 (setq expand-list nil))))
486 (defun expand-clear-markers ()
487 "Make the markers point nowhere."
488 (if expand-pos
489 (progn
490 (let ((i (1- (length expand-pos))))
491 (while (>= i 0)
492 (set-marker (aref expand-pos i) nil)
493 (setq i (1- i))))
494 (setq expand-pos nil))))
496 (defun expand-in-literal ()
497 "Test if we are in a comment or in a string."
498 (save-excursion
499 (let* ((lim (or (save-excursion
500 (beginning-of-defun)
501 (point))
502 (point-min)))
503 (here (point))
504 (state (parse-partial-sexp lim (point))))
505 (cond
506 ((nth 3 state) 'string)
507 ((nth 4 state) 'comment)
508 (t nil)))))
510 (defun expand-mode-submit-report ()
511 "Report a problem, a suggestion or a comment about Expand mode."
512 (interactive)
513 (require 'reporter)
514 (reporter-submit-bug-report
515 expand-mode-help-address
516 (concat "expand.el " expand-mode-version)
517 '(expand-mode-name
518 expand-mode-hook
519 expand-mode-load-hook
520 expand-map
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)))
531 (loop len)
532 (v (make-vector (+ len 1) nil)))
533 (while (>= loop 0)
534 (aset v loop (if (markerp (car l)) (car l) (copy-marker (car l))))
535 (setq l (cdr l)
536 loop (1- loop)))
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))
548 (provide 'expand)
550 ;; run load hooks
551 (run-hooks 'expand-mode-load-hook)
553 ;;; expand.el ends here