X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/e26f93cc0db7edeb0eaaf93d9a9f34398b1153c2..d4b9aae98f4f5298eaef096e42ac6aca02d9ee31:/lisp/expand.el diff --git a/lisp/expand.el b/lisp/expand.el index 3046361ad4..aeb9ec83b1 100644 --- a/lisp/expand.el +++ b/lisp/expand.el @@ -1,6 +1,7 @@ -;; expand.el --- minor mode to make abbreviations more usable. +;;; expand.el --- make abbreviations more usable -;; Copyright (C) 1995, 1996 Free Software Foundation, Inc. +;; Copyright (C) 1995, 1996, 2002, 2003, 2004, +;; 2005 Free Software Foundation, Inc. ;; Author: Frederic Lepied ;; Maintainer: Frederic Lepied @@ -20,25 +21,17 @@ ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, -;; Boston, MA 02111-1307, USA. +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; -;; Purpose of this package: -;; 1. Expand abbreviations only when they are at the end of a line and not -;; in a comment or in a string. -;; 2. Position the cursor after expansion to a place specified by advance. -;; 3. Indent the expanded region. -;; 4. If a list of points as been provided with the abbreviation definition, -;; the functions expand-jump-to-previous-mark and expand-jump-to-next-mark -;; moved from mark to mark. -;; -;; Installation: -;; * store this file somewhere in your load-path and byte compile it. -;; * put (require 'expand) in your .emacs or in site-start.el or generate -;; autoloads. -;; * and according to the mode install your expansion table. +;; This package defines abbrevs which expand into structured constructs +;; for certain languages. The construct is indented for you, +;; and contains slots for you to fill in other text. + +;; These abbrevs expand only at the end of a line and when not in a comment +;; or a string. ;; ;; Look at the Sample: section for emacs-lisp, perl and c expand lists. ;; For example for c-mode, you could declare your abbrev table with : @@ -55,63 +48,52 @@ ;; ("default" "default:\n\nbreak;" 10) ;; ("main" "int\nmain(int argc, char * argv[])\n{\n\n}\n" 37)) ;; "Expansions for C mode") -;; -;; and enter Expand mode with the following hook : ;; -;; (add-hook 'c-mode-hook (function (lambda() +;; and enter Abbrev mode with the following hook : +;; +;; (add-hook 'c-mode-hook (function (lambda () ;; (expand-add-abbrevs c-mode-abbrev-table c-expand-list) -;; (expand-mode)))) +;; (abbrev-mode)))) ;; -;; you can also bind jump functions to some keys and init some post-process -;; hooks : +;; you can also init some post-process hooks : ;; -;; (add-hook 'expand-mode-load-hook +;; (add-hook 'expand-load-hook ;; (function -;; (lambda() +;; (lambda () ;; (add-hook 'expand-expand-hook 'indent-according-to-mode) -;; (add-hook 'expand-jump-hook 'indent-according-to-mode) -;; (define-key expand-map '[(control tab)] 'expand-jump-to-next-mark) -;; (define-key expand-map '[(control shift tab)] 'expand-jump-to-previous-mark)))) +;; (add-hook 'expand-jump-hook 'indent-according-to-mode)))) ;; ;; Remarks: ;; -;; Has been tested under emacs 19.28-19.34 and XEmacs 19.11. ;; Many thanks to Heddy Boubaker , ;; Jerome Santini , ;; Jari Aalto . ;; -;; Please send me a word to give me your feeling about this mode or +;; Please send me a word to give me your feeling about this feature or ;; to explain me how you use it (your expansions table for example) using -;; the function expand-mode-submit-report. +;; the function expand-submit-report. +;;; Code: -;; Expand mode is not a replacement for abbrev it is just a layer above it. - ;;; Constants: -(defconst expand-mode-version "$Id: expand.el,v 1.13 1996/11/23 18:59:53 fred Exp $" - "Version tag for expand.el.") - -(defconst expand-mode-help-address "expand-help@sugix.frmug.org" - "Email address to send requests, comments or bug reports.") - -(defvar expand-mode nil - "Status variable for Expand mode.") -(make-variable-buffer-local 'expand-mode) - -(defvar expand-mode-name " Expand" - "Name of mode displayed in the modeline for Expand mode.") +(defgroup expand nil + "Make abbreviations more usable." + :group 'abbrev) -(defvar expand-mode-hook nil - "Hooks run when Expand mode is enabled.") +(defcustom expand-load-hook nil + "Hooks run when `expand.el' is loaded." + :type 'hook + :group 'expand) -(defvar expand-mode-load-hook nil - "Hooks run when expand is loaded.") +(defcustom expand-expand-hook nil + "Hooks run when an abbrev made by `expand-add-abbrevs' is expanded." + :type 'hook + :group 'expand) -(defvar expand-expand-hook nil - "Hooks run when expansion is done.") - -(defvar expand-jump-hook nil - "Hooks run when jump to mark occurs.") +(defcustom expand-jump-hook nil + "Hooks run by `expand-jump-to-previous-slot' and `expand-jump-to-next-slot'." + :type 'hook + :group 'expand) ;;; Samples: @@ -119,8 +101,7 @@ "Loop var: " "for(" str _ @ "=0; " str @ "; " str @ ") {" \n @ _ \n - "}" > - ) + "}" > \n) (defconst expand-c-sample-expand-list '(("if" "if () {\n \n} else {\n \n}" (5 10 21)) @@ -164,7 +145,7 @@ (concat "(defmacro ()\n" " \"\"\n" - " (` \n" + " `( \n" " ))") (list 11 13 18 25)) @@ -204,7 +185,7 @@ ) "Expansions for Lisp mode. See `expand-add-abbrevs'.") - + ;; perl example from Jari Aalto (defconst expand-sample-perl-mode-expand-list (list @@ -283,58 +264,36 @@ ;;; Code: -;;;###autoload -(defun expand-mode(&optional arg) - "Toggle Expand mode. -With argument ARG, turn Expand mode on if ARG is positive. -In Expand mode, inserting an abbreviation at the end of a line -causes it to expand and be replaced by its expansion." - (interactive "P") - (setq expand-mode (if (null arg) (not expand-mode) - (> (prefix-numeric-value arg) 0))) - (if expand-mode - (progn - (setq abbrev-mode nil) - (run-hooks 'expand-mode-hook)))) - -;;;###autoload -(defvar expand-map (make-sparse-keymap) - "Key map used in Expand mode.") -(define-key expand-map " " 'expand-template-abbreviation) - -(or (assq 'expand-mode minor-mode-alist) - (setq minor-mode-alist (cons (list 'expand-mode expand-mode-name) - minor-mode-alist))) - -(or (assq 'expand-mode minor-mode-map-alist) - (setq minor-mode-map-alist (cons (cons 'expand-mode expand-map) - minor-mode-map-alist))) - ;;;###autoload (defun expand-add-abbrevs (table abbrevs) - "Add a list of abbrev to the table. -Each abbrev description entry has the following format : - (abbrev expansion arg) -where - abbrev is the abbreviation to replace. - expansion is the replacement string or a function which will make -the expansion. For example you could use the DMacros or skeleton packages + "Add a list of abbrev to abbrev table TABLE. +ABBREVS is a list of abbrev definitions; each abbrev description entry +has the form (ABBREV EXPANSION ARG). + +ABBREV is the abbreviation to replace. + +EXPANSION is the replacement string or a function which will make the +expansion. For example you, could use the DMacros or skeleton packages to generate such functions. - arg is an optional element which can be a number or a list of -numbers. If arg is a number, the cursor will be placed at arg chars -from the beginning of the expanded text. If expansion is a list of -numbers the cursor will be placed according to the first number of the -list from the beginning of the expanded text and marks will be placed -and you will be able to visit them cyclicaly with the functions -expand-jump-to-previous-mark and expand-jump-to-next-mark. If arg is -omitted, the cursor will be placed at the end of the expanded text." + +ARG is an optional argument which can be a number or a list of +numbers. If ARG is a number, point is placed ARG chars from the +beginning of the expanded text. + +If ARG is a list of numbers, point is placed according to the first +member of the list, but you can visit the other specified positions +cyclicaly with the functions `expand-jump-to-previous-slot' and +`expand-jump-to-next-slot'. + +If ARG is omitted, point is placed at the end of the expanded text." + (if (null abbrevs) table (expand-add-abbrev table (nth 0 (car abbrevs)) (nth 1 (car abbrevs)) (nth 2 (car abbrevs))) (expand-add-abbrevs table (cdr abbrevs)))) -(defvar expand-list nil "Temporary variable used by Expand mode.") +(defvar expand-list nil "Temporary variable used by the Expand package.") (defvar expand-pos nil "If non nil, stores a vector containing markers to positions defined by the last expansion. @@ -342,7 +301,7 @@ This variable is local to a buffer.") (make-variable-buffer-local 'expand-pos) (defvar expand-index 0 - "Index of the last marker used in expand-pos. + "Index of the last marker used in `expand-pos'. This variable is local to a buffer.") (make-variable-buffer-local 'expand-index) @@ -376,25 +335,10 @@ This variable is local to a buffer.") ) 'expand-abbrev-hook))) -;;;###autoload -(defun expand-template-abbreviation(arg) - "Do the expansion job if we are at the end of a line or insert space." - (interactive "p") - (or (expand-try-to-expand) - (self-insert-command arg))) - -;; Try to expand an abbrev. On success check if it is an Expand mode abbrev -;; else undo the expansion. -(defun expand-try-to-expand() - (if (not (expand-abbrev)) - nil - (if (not (stringp (symbol-value last-abbrev))) - t - (unexpand-abbrev) - nil))) - -(defun expand-abbrev-hook() - "Abbrev hook used to do the expansion job of expand abbrevs. See `expand-add-abbrevs'." +(put 'expand-abbrev-hook 'no-self-insert t) +(defun expand-abbrev-hook () + "Abbrev hook used to do the expansion job of expand abbrevs. +See `expand-add-abbrevs'. Value is non-nil if expansion was done." ;; Expand only at the end of a line if we are near a word that has ;; an abbrev built from expand-add-abbrev. (if (and (eolp) @@ -420,10 +364,11 @@ This variable is local to a buffer.") expand-pos (expand-list-to-markers expand-list) expand-list nil))) (run-hooks 'expand-expand-hook) - t)))) - ) + t) + nil)) + nil)) -(defun expand-do-expansion() +(defun expand-do-expansion () (delete-backward-char (length last-abbrev-text)) (let* ((vect (symbol-value last-abbrev)) (text (aref vect 0)) @@ -442,7 +387,7 @@ This variable is local to a buffer.") t) ) -(defun expand-abbrev-from-expand(word) +(defun expand-abbrev-from-expand (word) "Test if an abbrev has a hook." (or (and (intern-soft word local-abbrev-table) @@ -457,8 +402,10 @@ This variable is local to a buffer.") (backward-word 1) (buffer-substring p (point))))) -(defun expand-jump-to-previous-mark() - "Move the cursor to previous mark created by the expansion." +;;;###autoload +(defun expand-jump-to-previous-slot () + "Move the cursor to the previous slot in the last abbrev expansion. +This is used only in conjunction with `expand-add-abbrevs'." (interactive) (if expand-pos (progn @@ -468,8 +415,10 @@ This variable is local to a buffer.") (goto-char (aref expand-pos expand-index)) (run-hooks 'expand-jump-hook)))) -(defun expand-jump-to-next-mark() - "Move the cursor to next mark created by the expansion." +;;;###autoload +(defun expand-jump-to-next-slot () + "Move the cursor to the next slot in the last abbrev expansion. +This is used only in conjunction with `expand-add-abbrevs'." (interactive) (if expand-pos (progn @@ -479,6 +428,9 @@ This variable is local to a buffer.") (goto-char (aref expand-pos expand-index)) (run-hooks 'expand-jump-hook)))) +;;;###autoload (define-key ctl-x-map "ap" 'expand-jump-to-previous-slot) +;;;###autoload (define-key ctl-x-map "an" 'expand-jump-to-next-slot) + (defun expand-build-list (len l) "Build a vector of offset positions from the list of positions." (expand-clear-markers) @@ -526,22 +478,6 @@ This variable is local to a buffer.") ((nth 4 state) 'comment) (t nil))))) -(defun expand-mode-submit-report () - "Report a problem, a suggestion or a comment about Expand mode." - (interactive) - (require 'reporter) - (reporter-submit-bug-report - expand-mode-help-address - (concat "expand.el " expand-mode-version) - '(expand-mode-name - expand-mode-hook - expand-mode-load-hook - expand-map - ) - nil - nil - "Dear expand.el maintainer,")) - ;; support functions to add marks to jump from outside function (defun expand-list-to-markers (l) @@ -556,16 +492,18 @@ This variable is local to a buffer.") v)) ;; integration with skeleton.el +;; Used in `skeleton-end-hook' to fetch the positions for @ skeleton tags. +;; See `skeleton-insert'. (defun expand-skeleton-end-hook () - "`skeleton-end-hook' to enable expand marks jumps for @ skeleton tags see `skeleton-insert'." - (if skeleton-marks - (setq expand-list skeleton-marks))) - + (if skeleton-positions + (setq expand-list skeleton-positions))) + (add-hook 'skeleton-end-hook (function expand-skeleton-end-hook)) (provide 'expand) ;; run load hooks -(run-hooks 'expand-mode-load-hook) +(run-hooks 'expand-load-hook) +;;; arch-tag: fee53e9e-30e3-4ef3-b191-9785e1f8e885 ;;; expand.el ends here