Trailing whitespace deleted.
[bpt/emacs.git] / lisp / forms.el
index 22f804c..c3cffa3 100644 (file)
@@ -1,6 +1,6 @@
 ;;; forms.el --- Forms mode: edit a file as a form to fill in
 
-;; Copyright (C) 1991, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1991, 1994, 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
 
 ;; Author: Johan Vromans <jvromans@squirrel.nl>
 
@@ -31,9 +31,9 @@
 ;; Names which start with 'forms--' are intended for internal use, and
 ;; should *NOT* be used from the outside.
 ;;
-;; All variables are buffer-local, to enable multiple forms visits 
+;; All variables are buffer-local, to enable multiple forms visits
 ;; simultaneously.
-;; Variable `forms--mode-setup' is local to *ALL* buffers, for it 
+;; Variable `forms--mode-setup' is local to *ALL* buffers, for it
 ;; controls if forms-mode has been enabled in a buffer.
 ;;
 ;; === How it works ===
 ;; The second file holds the actual data.  The buffer of this file
 ;; will be buried, for it is never accessed directly.
 ;;
-;; Forms mode is invoked using M-x forms-find-file control-file .
+;; Forms mode is invoked using M-x `forms-find-file' control-file.
 ;; Alternatively `forms-find-file-other-window' can be used.
 ;;
 ;; You may also visit the control file, and switch to forms mode by hand
-;; with M-x forms-mode .
+;; with M-x `forms-mode'.
 ;;
-;; Automatic mode switching is supported if you specify 
+;; Automatic mode switching is supported if you specify
 ;; "-*- forms -*-" in the first line of the control file.
-;; 
+;;
 ;; The control file is visited, evaluated using `eval-current-buffer',
 ;; and should set at least the following variables:
 ;;
 ;;
 ;;   - a string, e.g. "hello".  The string is inserted in the forms
 ;;     "as is".
-;;   
+;;
 ;;   - an integer, denoting a field number.
 ;;     The contents of this field are inserted at this point.
 ;;     Fields are numbered starting with number one.
-;;   
+;;
 ;;   - a function call, e.g. (insert "text").
 ;;     This function call is dynamically evaluated and should return a
 ;;     string.  It should *NOT* have side-effects on the forms being
 ;;     constructed.  The current fields are available to the function
 ;;     in the variable `forms-fields', they should *NOT* be modified.
-;;   
+;;
 ;;   - a lisp symbol, that must evaluate to one of the above.
 ;;
 ;; Optional variables which may be set in the control file:
 ;;                     Non-nil means that the data file is visited
 ;;                     read-only (view mode) as opposed to edit mode.
 ;;                     If no write access to the data file is
-;;                     possible, view mode is enforced. 
+;;                     possible, view mode is enforced.
 ;;
 ;;     forms-check-number-of-fields            [bool, default t]
 ;;                     If non-nil, a warning will be issued whenever
 ;;                     of fields specified by `forms-number-of-fields'.
 ;;
 ;;     forms-multi-line                        [string, default "^K"]
-;;                     If non-null the records of the data file may
+;;                     If non-null, the records of the data file may
 ;;                     contain fields that can span multiple lines in
 ;;                     the form.
-;;                     This variable denotes the separator character
+;;                     This variable denotes the separator string
 ;;                     to be used for this purpose.  Upon display, all
-;;                     occurrences of this character are translated
+;;                     occurrences of this string are translated
 ;;                     to newlines.  Upon storage they are translated
-;;                     back to the separator character.
+;;                     back to the separator string.
 ;;
 ;;     forms-forms-scroll                      [bool, default nil]
 ;;                     Non-nil means: rebind locally the commands that
 ;;                     `forms-next-field' resp. `forms-prev-field'.
 ;;
 ;;     forms-forms-jump                        [bool, default nil]
-;;                     Non-nil means: rebind locally the commands that
+;;                     Non-nil means: rebind locally the commands
+;;                     `beginning-of-buffer' and `end-of-buffer' to
+;;                     perform, respectively, `forms-first-record' and
+;;                     `forms-last-record' instead.
 ;;
 ;;     forms-insert-after                      [bool, default nil]
-;;                     Non-nil means: inserts of new records go after
-;;                     current record, also initial position is at last
-;;                     record.
+;;                     Non-nil means: insertions of new records go after
+;;                     current record, also initial position is at the
+;;                     last record.  The default is to insert before the
+;;                     current record and the initial position is at the
+;;                     first record.
 ;;
 ;;     forms-read-file-filter                  [symbol, default nil]
-;;                     If not nil: this should be the name of a 
+;;                     If not nil: this should be the name of a
 ;;                     function that is called after the forms data file
 ;;                     has been read.  It can be used to transform
 ;;                     the contents of the file into a format more suitable
 ;;                     for forms-mode processing.
 ;;
 ;;     forms-write-file-filter                 [symbol, default nil]
-;;                     If not nil: this should be the name of a 
+;;                     If not nil: this should be the name of a
 ;;                     function that is called before the forms data file
 ;;                     is written (saved) to disk.  It can be used to undo
 ;;                     the effects of `forms-read-file-filter', if any.
 ;;
 ;;     forms-new-record-filter                 [symbol, default nil]
-;;                     If not nil: this should be the name of a 
+;;                     If not nil: this should be the name of a
 ;;                     function that is called when a new
 ;;                     record is created.  It can be used to fill in
 ;;                     the new record with default fields, for example.
 ;;
 ;;     forms-modified-record-filter            [symbol, default nil]
-;;                     If not nil: this should be the name of a 
+;;                     If not nil: this should be the name of a
 ;;                     function that is called when a record has
 ;;                     been modified.  It is called after the fields
 ;;                     are parsed.  It can be used to register
 ;;                     distinct face, if possible.
 ;;                     As of emacs 19.29, the `intangible' text property
 ;;                     is used to prevent moving into read-only fields.
-;;                     This variable defaults to t if running Emacs 19
-;;                     with text properties.
+;;                     This variable defaults to t if running Emacs 19 or
+;;                     later with text properties.
 ;;                     The default face to show read-write fields is
 ;;                     copied from face `region'.
 ;;
 ;;     forms-ro-face                           [symbol, default 'default]
 ;;                     This is the face that is used to show
-;;                     read-only text on the screen.If used, this
+;;                     read-only text on the screen.  If used, this
 ;;                     variable should be set to a symbol that is a
 ;;                     valid face.
 ;;                     E.g.
 ;; After evaluating the control file, its buffer is cleared and used
 ;; for further processing.
 ;; The data file (as designated by `forms-file') is visited in a buffer
-;; `forms--file-buffer' which will not normally be shown.
+;; `forms--file-buffer' which normally will not be shown.
 ;; Great malfunctioning may be expected if this file/buffer is modified
 ;; outside of this package while it is being visited!
 ;;
 ;; Normal operation is to transfer one line (record) from the data file,
 ;; split it into fields (into `forms--the-record-list'), and display it
 ;; using the specs in `forms-format-list'.
-;; A format routine `forms--format' is built upon startup to format 
+;; A format routine `forms--format' is built upon startup to format
 ;; the records according to `forms-format-list'.
 ;;
 ;; When a form is changed the record is updated as soon as this form
 ;;
 ;; Two exit functions exist: `forms-exit' and `forms-exit-no-save'.
 ;; `forms-exit' saves the data to the file, if modified.
-;; `forms-exit-no-save` does not.  However, if `forms-exit-no-save'
+;; `forms-exit-no-save' does not.  However, if `forms-exit-no-save'
 ;; is executed and the file buffer has been modified, Emacs will ask
 ;; questions anyway.
 ;;
 ;;     switching edit <-> view mode v.v.
 ;;     jumping from field to field
 ;;
-;; As an documented side-effect: jumping to the last record in the
+;; As a documented side-effect: jumping to the last record in the
 ;; file (using forms-last-record) will adjust forms--total-records if
 ;; needed.
 ;;
-;; The forms buffer can be in on eof two modes: edit mode or view
-;; mode.  View mode is a read-only mode, you cannot modify the
+;; The forms buffer can be in onof two modes: edit mode or view
+;; mode.  View mode is a read-only mode, whereby you cannot modify the
 ;; contents of the buffer.
 ;;
 ;; Edit mode commands:
-;; 
+;;
 ;; TAB          forms-next-field
 ;; \C-c TAB     forms-next-field
 ;; \C-c <       forms-first-record
 ;; \C-c \C-r    forms-search-backward
 ;; \C-c \C-s    forms-search-forward
 ;; \C-c \C-x    forms-exit
-;; 
+;;
 ;; Read-only mode commands:
-;; 
+;;
 ;; SPC          forms-next-record
 ;; DEL  forms-prev-record
 ;; ?    describe-mode
-;; \C-q forms-toggle-read-only
+;; \C-q  forms-toggle-read-only
 ;; l    forms-jump-record
 ;; n    forms-next-record
 ;; p    forms-prev-record
 ;; r    forms-search-backward
 ;; s    forms-search-forward
 ;; x    forms-exit
-;; 
+;;
 ;; Of course, it is also possible to use the \C-c prefix to obtain the
 ;; same command keys as in edit mode.
-;; 
-;; The following bindings are available, independent of the mode: 
-;; 
+;;
+;; The following bindings are available, independent of the mode:
+;;
 ;; [next]        forms-next-record
 ;; [prior]       forms-prev-record
 ;; [begin]       forms-first-record
 ;; [end]         forms-last-record
 ;; [S-TAB]       forms-prev-field
-;; [backtab] forms-prev-field
+;; [backtab]     forms-prev-field
 ;;
 ;; For convenience, TAB is always bound to `forms-next-field', so you
 ;; don't need the C-c prefix for this command.
 ;;
-;; As mentioned above (see `forms-forms-scroll' and `forms-forms-jump')
+;; As mentioned above (see `forms-forms-scroll' and `forms-forms-jump'),
 ;; the bindings of standard functions `scroll-up', `scroll-down',
 ;; `beginning-of-buffer' and `end-of-buffer' can be locally replaced with
 ;; forms mode functions next/prev record and first/last
 ;; record.
 ;;
-;; `local-write-file hook' is defined to save the actual data file
+;; `local-write-file-hooks' is defined to save the actual data file
 ;; instead of the buffer data, `revert-file-hook' is defined to
 ;; revert a forms to original.
 \f
 (provide 'forms)                       ;;; official
 (provide 'forms-mode)                  ;;; for compatibility
 
-(defconst forms-version (substring "$Revision: 2.33 $" 11 -2)
+(defconst forms-version (substring "$Revision: 2.44 $" 11 -2)
   "The version number of forms-mode (as string).  The complete RCS id is:
 
-  $Id: forms.el,v 2.33 1997/08/27 06:47:51 rms Exp rms $")
+  $Id: forms.el,v 2.44 2003/01/12 20:47:48 schwab Exp $")
 
 (defcustom forms-mode-hooks nil
-  "Hook functions to be run upon entering Forms mode."
+  "Hook run upon entering Forms mode."
   :group 'forms
-  :type 'function)
+  :type 'hook)
 \f
 ;;; Mandatory variables - must be set by evaluating the control file.
 
@@ -363,7 +368,7 @@ This can be used to undo the effects of `form-read-file-hook'.")
 
 (defvar forms-fields nil
   "List with fields of the current forms.  First field has number 1.
-This variable is for use by the filter routines only. 
+This variable is for use by the filter routines only.
 The contents may NOT be modified.")
 
 (defcustom forms-use-text-properties t
@@ -412,7 +417,7 @@ Also, initial position is at last record."
 (defvar forms--dyntexts nil
   "Dynamic texts (resulting from function calls) on the screen.")
 
-(defvar forms--the-record-list nil 
+(defvar forms--the-record-list nil
    "List of strings of the current record, as parsed from the file.")
 
 (defvar forms--search-regexp nil
@@ -440,25 +445,25 @@ Also, initial position is at last record."
 (defvar forms--rw-face nil
   "Face used to represent read-write data on the screen.")
 \f
-;;;###autoload 
+;;;###autoload
 (defun forms-mode (&optional primary)
   "Major mode to visit files in a field-structured manner using a form.
 
 Commands:                        Equivalent keys in read-only mode:
  TAB            forms-next-field          TAB
- \\C-c TAB       forms-next-field          
\\C-c <         forms-first-record         <
\\C-c >         forms-last-record          >
\\C-c ?         describe-mode              ?
\\C-c \\C-k      forms-delete-record
\\C-c \\C-q      forms-toggle-read-only     q
\\C-c \\C-o      forms-insert-record
\\C-c \\C-l      forms-jump-record          l
\\C-c \\C-n      forms-next-record          n
\\C-c \\C-p      forms-prev-record          p
\\C-c \\C-r      forms-search-reverse       r
\\C-c \\C-s      forms-search-forward       s
\\C-c \\C-x      forms-exit                 x
+ C-c TAB        forms-next-field
C-c <          forms-first-record         <
C-c >          forms-last-record          >
C-c ?          describe-mode              ?
C-c C-k        forms-delete-record
C-c C-q        forms-toggle-read-only     q
C-c C-o        forms-insert-record
C-c C-l        forms-jump-record          l
C-c C-n        forms-next-record          n
C-c C-p        forms-prev-record          p
C-c C-r        forms-search-reverse       r
C-c C-s        forms-search-forward       s
C-c C-x        forms-exit                 x
 "
   (interactive)
 
@@ -505,7 +510,7 @@ Commands:                        Equivalent keys in read-only mode:
        (setq forms-new-record-filter nil)
        (setq forms-modified-record-filter nil)
 
-       ;; If running Emacs 19 under X, setup faces to show read-only and 
+       ;; If running Emacs 19 under X, setup faces to show read-only and
        ;; read-write fields.
        (if (fboundp 'make-face)
            (progn
@@ -516,7 +521,7 @@ Commands:                        Equivalent keys in read-only mode:
        ;;(message "forms: processing control file...")
        ;; If enable-local-eval is not set to t the user is asked first.
        (if (or (eq enable-local-eval t)
-               (yes-or-no-p 
+               (yes-or-no-p
                 (concat "Evaluate lisp code in buffer "
                         (buffer-name) " to display forms ")))
            (eval-current-buffer)
@@ -524,7 +529,7 @@ Commands:                        Equivalent keys in read-only mode:
 
        ;; Check if the mandatory variables make sense.
        (or forms-file
-           (error (concat "Forms control file error: " 
+           (error (concat "Forms control file error: "
                           "`forms-file' has not been set")))
 
        ;; Check forms-field-sep first, since it can be needed to
@@ -549,13 +554,13 @@ Commands:                        Equivalent keys in read-only mode:
            (if (and (stringp forms-multi-line)
                     (eq (length forms-multi-line) 1))
                (if (string= forms-multi-line forms-field-sep)
-                   (error (concat "Forms control file error: " 
+                   (error (concat "Forms control file error: "
                                   "`forms-multi-line' is equal to 'forms-field-sep'")))
              (error (concat "Forms control file error: "
                             "`forms-multi-line' must be nil or a one-character string"))))
        (or (fboundp 'set-text-properties)
            (setq forms-use-text-properties nil))
-           
+
        ;; Validate and process forms-format-list.
        ;;(message "forms: pre-processing format list...")
        (make-local-variable 'forms--elements)
@@ -589,7 +594,7 @@ Commands:                        Equivalent keys in read-only mode:
        ;; Dynamic text support.
        (make-local-variable 'forms--dynamic-text)
 
-       ;; Prevent accidental overwrite of the control file and autosave.
+       ;; Prevent accidental overwrite of the control file and auto-save.
        (set-visited-file-name nil)
 
        ;; Prepare this buffer for further processing.
@@ -694,7 +699,7 @@ Commands:                        Equivalent keys in read-only mode:
   (if (= forms--total-records 0)
       ;;(message "forms: proceeding setup (new file)...")
       (progn
-       (insert 
+       (insert
         "GNU Emacs Forms Mode version " forms-version "\n\n"
         (if (file-exists-p forms-file)
             (concat "No records available in file `" forms-file "'\n\n")
@@ -711,11 +716,11 @@ Commands:                        Equivalent keys in read-only mode:
     (if (< forms--current-record 1)
        (setq forms--current-record 1))
     (forms-jump-record forms--current-record)
-    )
 
-  (if forms-insert-after
-      (forms-last-record)
-    (forms-first-record))
+    (if forms-insert-after
+       (forms-last-record)
+      (forms-first-record))
+    )
 
   ;; user customising
   ;;(message "forms: proceeding setup (user hooks)...")
@@ -731,7 +736,7 @@ Commands:                        Equivalent keys in read-only mode:
   ;; Symbols in the list are evaluated, and consecutive strings are
   ;; concatenated.
   ;; Array `forms--elements' is constructed that contains the order
-  ;; of the fields on the display. This array is used by 
+  ;; of the fields on the display. This array is used by
   ;; `forms--parser-using-text-properties' to extract the fields data
   ;; from the form on the screen.
   ;; Upon completion, `forms-format-list' is guaranteed correct, so
@@ -754,7 +759,7 @@ Commands:                        Equivalent keys in read-only mode:
   (let ((the-list forms-format-list)   ; the list of format elements
        (this-item 0)                   ; element in list
        (prev-item nil)
-       (field-num 0))                  ; highest field number 
+       (field-num 0))                  ; highest field number
 
     (setq forms-format-list nil)       ; gonna rebuild
 
@@ -780,7 +785,7 @@ Commands:                        Equivalent keys in read-only mode:
            (setq prev-item el)))
 
         ;; Try numeric ...
-        ((numberp el) 
+        ((numberp el)
 
          ;; Validate range.
          (if (or (<= el 0)
@@ -857,8 +862,8 @@ Commands:                        Equivalent keys in read-only mode:
 (defun forms--iif-hook (begin end)
   "`insert-in-front-hooks' function for read-only segments."
 
-  ;; Note start location.  By making it a marker that points one 
-  ;; character beyond the actual location, it is guaranteed to move 
+  ;; Note start location.  By making it a marker that points one
+  ;; character beyond the actual location, it is guaranteed to move
   ;; correctly if text is inserted.
   (or forms--iif-start
       (setq forms--iif-start (copy-marker (1+ (point)))))
@@ -869,12 +874,12 @@ Commands:                        Equivalent keys in read-only mode:
                             'read-only))
       (progn
        ;; Fetch current properties.
-       (setq forms--iif-properties 
+       (setq forms--iif-properties
              (text-properties-at (1- forms--iif-start)))
 
        ;; Replace them.
        (let ((inhibit-read-only t))
-         (set-text-properties 
+         (set-text-properties
           (1- forms--iif-start) forms--iif-start
           (list 'face forms--rw-face 'front-sticky '(face))))
 
@@ -895,7 +900,7 @@ Commands:                        Equivalent keys in read-only mode:
   ;; Restore properties.
   (if forms--iif-start
       (let ((inhibit-read-only t))
-       (set-text-properties 
+       (set-text-properties
         (1- forms--iif-start) forms--iif-start
         forms--iif-properties)))
 
@@ -915,26 +920,26 @@ Commands:                        Equivalent keys in read-only mode:
 
   (let ((forms--marker 0)
        (forms--dyntext 0))
-    (setq 
+    (setq
      forms--format
-     (if forms-use-text-properties 
-        (` (lambda (arg)
-             (let ((inhibit-read-only t))
-               (,@ (apply 'append
-                          (mapcar 'forms--make-format-elt-using-text-properties
-                                  forms-format-list)))
-               ;; Prevent insertion before the first text.
-               (,@ (if (numberp (car forms-format-list))
-                       nil
-                     '((add-text-properties (point-min) (1+ (point-min))
-                                            '(front-sticky (read-only intangible))))))
-               ;; Prevent insertion after the last text.
-               (remove-text-properties (1- (point)) (point)
-                                       '(rear-nonsticky)))
-             (setq forms--iif-start nil)))
-       (` (lambda (arg)
-           (,@ (apply 'append
-                      (mapcar 'forms--make-format-elt forms-format-list)))))))
+     (if forms-use-text-properties
+        `(lambda (arg)
+           (let ((inhibit-read-only t))
+             ,@(apply 'append
+                      (mapcar 'forms--make-format-elt-using-text-properties
+                              forms-format-list))
+             ;; Prevent insertion before the first text.
+             ,@(if (numberp (car forms-format-list))
+                   nil
+                 '((add-text-properties (point-min) (1+ (point-min))
+                                        '(front-sticky (read-only intangible)))))
+             ;; Prevent insertion after the last text.
+             (remove-text-properties (1- (point)) (point)
+                                     '(rear-nonsticky)))
+           (setq forms--iif-start nil))
+       `(lambda (arg)
+         ,@(apply 'append
+                  (mapcar 'forms--make-format-elt forms-format-list)))))
 
     ;; We have tallied the number of markers and dynamic texts,
     ;; so we can allocate the arrays now.
@@ -952,9 +957,9 @@ Commands:                        Equivalent keys in read-only mode:
   ;;   (let ((inhibit-read-only t))
   ;;
   ;;     ;; A string, e.g. "text: ".
-  ;;     (set-text-properties 
+  ;;     (set-text-properties
   ;;      (point)
-  ;;      (progn (insert "text: ") (point)) 
+  ;;      (progn (insert "text: ") (point))
   ;;      (list 'face forms--ro-face
   ;;           'read-only 1
   ;;           'insert-in-front-hooks 'forms--iif-hook
@@ -965,7 +970,7 @@ Commands:                        Equivalent keys in read-only mode:
   ;;       (aset forms--markers 0 (point-marker))
   ;;       (insert (elt arg 5))
   ;;       (or (= (point) here)
-  ;;     (set-text-properties 
+  ;;     (set-text-properties
   ;;      here (point)
   ;;      (list 'face forms--rw-face
   ;;            'front-sticky '(face))))
@@ -1003,47 +1008,47 @@ Commands:                        Equivalent keys in read-only mode:
 
   (cond
    ((stringp el)
-    
-    (` ((set-text-properties 
-        (point)                        ; start at point
-        (progn                         ; until after insertion
-          (insert (, el))
-          (point))
-        (list 'face forms--ro-face     ; read-only appearance
-              'read-only (,@ (list (1+ forms--marker)))
-              'intangible t
-              'insert-in-front-hooks '(forms--iif-hook)
-              'rear-nonsticky '(face read-only insert-in-front-hooks
-                                intangible))))))
-    
+
+    `((set-text-properties
+       (point)                         ; start at point
+       (progn                          ; until after insertion
+        (insert ,el)
+        (point))
+       (list 'face forms--ro-face      ; read-only appearance
+            'read-only ,@(list (1+ forms--marker))
+            'intangible ,@(list (1+ forms--marker))
+            'insert-in-front-hooks '(forms--iif-hook)
+            'rear-nonsticky '(face read-only insert-in-front-hooks
+                                   intangible)))))
+
    ((numberp el)
-    (` ((let ((here (point)))
-         (aset forms--markers 
-               (, (prog1 forms--marker
-                    (setq forms--marker (1+ forms--marker))))
-               (point-marker))
-         (insert (elt arg (, (1- el))))
-         (or (= (point) here)
-             (set-text-properties 
-              here (point)
-              (list 'face forms--rw-face
-                    'front-sticky '(face))))))))
+    `((let ((here (point)))
+       (aset forms--markers
+             ,(prog1 forms--marker
+                (setq forms--marker (1+ forms--marker)))
+             (point-marker))
+       (insert (elt arg ,(1- el)))
+       (or (= (point) here)
+           (set-text-properties
+            here (point)
+            (list 'face forms--rw-face
+                  'front-sticky '(face)))))))
 
    ((listp el)
-    (` ((set-text-properties
-        (point)
-        (progn
-          (insert (aset forms--dyntexts 
-                        (, (prog1 forms--dyntext
-                             (setq forms--dyntext (1+ forms--dyntext))))
-                        (, el)))
-          (point))
-        (list 'face forms--ro-face
-              'read-only (,@ (list (1+ forms--marker)))
-              'intangible t
-              'insert-in-front-hooks '(forms--iif-hook)
-              'rear-nonsticky '(read-only face insert-in-front-hooks
-                                intangible))))))
+    `((set-text-properties
+       (point)
+       (progn
+        (insert (aset forms--dyntexts
+                      ,(prog1 forms--dyntext
+                         (setq forms--dyntext (1+ forms--dyntext)))
+                      ,el))
+        (point))
+       (list 'face forms--ro-face
+            'read-only ,@(list (1+ forms--marker))
+            'intangible ,@(list (1+ forms--marker))
+            'insert-in-front-hooks '(forms--iif-hook)
+            'rear-nonsticky '(read-only face insert-in-front-hooks
+                                        intangible)))))
 
    ;; end of cond
    ))
@@ -1066,17 +1071,17 @@ Commands:                        Equivalent keys in read-only mode:
   ;;   (insert (aset forms--dyntexts 0 (tocol 40)))
   ;;   ... )
 
-  (cond 
+  (cond
    ((stringp el)
-    (` ((insert (, el)))))
+    `((insert ,el)))
    ((numberp el)
     (prog1
-       (` ((aset forms--markers (, forms--marker) (point-marker))
-           (insert (elt arg (, (1- el))))))
+       `((aset forms--markers ,forms--marker (point-marker))
+         (insert (elt arg ,(1- el))))
       (setq forms--marker (1+ forms--marker))))
    ((listp el)
     (prog1
-       (` ((insert (aset forms--dyntexts (, forms--dyntext) (, el)))))
+       `((insert (aset forms--dyntexts ,forms--dyntext ,el)))
       (setq forms--dyntext (1+ forms--dyntext))))))
 \f
 (defvar forms--field)
@@ -1101,13 +1106,13 @@ Commands:                        Equivalent keys in read-only mode:
 
        ;; Note: we add a nil element to the list passed to `mapcar',
        ;; see `forms--make-parser-elt' for details.
-       (` (lambda nil
-           (let (here)
-             (goto-char (point-min))
-             (,@ (apply 'append
-                        (mapcar 
-                         'forms--make-parser-elt 
-                         (append forms-format-list (list nil)))))))))))
+       `(lambda nil
+         (let (here)
+           (goto-char (point-min))
+           ,@(apply 'append
+                    (mapcar
+                     'forms--make-parser-elt
+                     (append forms-format-list (list nil)))))))))
 
   (forms--debug 'forms--parser))
 
@@ -1123,7 +1128,7 @@ Commands:                        Equivalent keys in read-only mode:
       (goto-char (setq here (aref forms--markers i)))
       (if (get-text-property here 'read-only)
          (aset forms--recordv (aref forms--elements i) nil)
-       (if (setq there 
+       (if (setq there
                  (next-single-property-change here 'read-only))
            (aset forms--recordv (aref forms--elements i)
                  (buffer-substring-no-properties here there))
@@ -1139,12 +1144,12 @@ Commands:                        Equivalent keys in read-only mode:
   ;; (lambda nil
   ;;   (let (here)
   ;;     (goto-char (point-min))
-  ;; 
+  ;;
   ;;    ;;  "text: "
   ;;     (if (not (looking-at "text: "))
   ;;       (error "Parse error: cannot find \"text: \""))
   ;;     (forward-char 6)      ; past "text: "
-  ;; 
+  ;;
   ;;     ;;  6
   ;;    ;;  "\nmore text: "
   ;;     (setq here (point))
@@ -1158,7 +1163,7 @@ Commands:                        Equivalent keys in read-only mode:
   ;;         (error "Parse error: not looking at \"%s\"" forms--dyntext))
   ;;     (forward-char (length forms--dyntext))
   ;;     (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))
-  ;;     ... 
+  ;;     ...
   ;;     ;; final flush (due to terminator sentinel, see below)
   ;;   (aset forms--recordv 7 (buffer-substring-no-properties (point) (point-max)))
 
@@ -1166,15 +1171,15 @@ Commands:                        Equivalent keys in read-only mode:
    ((stringp el)
     (prog1
        (if forms--field
-           (` ((setq here (point))
-               (if (not (search-forward (, el) nil t nil))
-                   (error "Parse error: cannot find `%s'" (, el)))
-               (aset forms--recordv (, (1- forms--field))
-                     (buffer-substring-no-properties here
-                                       (- (point) (, (length el)))))))
-         (` ((if (not (looking-at (, (regexp-quote el))))
-                 (error "Parse error: not looking at `%s'" (, el)))
-             (forward-char (, (length el))))))
+           `((setq here (point))
+             (if (not (search-forward ,el nil t nil))
+                 (error "Parse error: cannot find `%s'" ,el))
+             (aset forms--recordv ,(1- forms--field)
+                   (buffer-substring-no-properties here
+                                                   (- (point) ,(length el)))))
+         `((if (not (looking-at ,(regexp-quote el)))
+               (error "Parse error: not looking at `%s'" ,el))
+           (forward-char ,(length el))))
       (setq forms--seen-text t)
       (setq forms--field nil)))
    ((numberp el)
@@ -1185,22 +1190,22 @@ Commands:                        Equivalent keys in read-only mode:
       nil))
    ((null el)
     (if forms--field
-       (` ((aset forms--recordv (, (1- forms--field))
-                 (buffer-substring-no-properties (point) (point-max)))))))
+       `((aset forms--recordv ,(1- forms--field)
+               (buffer-substring-no-properties (point) (point-max))))))
    ((listp el)
     (prog1
        (if forms--field
-           (` ((let ((here (point))
-                     (forms--dyntext (aref forms--dyntexts (, forms--dyntext))))
-                 (if (not (search-forward forms--dyntext nil t nil))
-                     (error "Parse error: cannot find `%s'" forms--dyntext))
-                 (aset forms--recordv (, (1- forms--field))
-                       (buffer-substring-no-properties here
-                                         (- (point) (length forms--dyntext)))))))
-         (` ((let ((forms--dyntext (aref forms--dyntexts (, forms--dyntext))))
-               (if (not (looking-at (regexp-quote forms--dyntext)))
-                   (error "Parse error: not looking at `%s'" forms--dyntext))
-               (forward-char (length forms--dyntext))))))
+           `((let ((here (point))
+                   (forms--dyntext (aref forms--dyntexts ,forms--dyntext)))
+               (if (not (search-forward forms--dyntext nil t nil))
+                   (error "Parse error: cannot find `%s'" forms--dyntext))
+               (aset forms--recordv ,(1- forms--field)
+                     (buffer-substring-no-properties here
+                                                     (- (point) (length forms--dyntext))))))
+         `((let ((forms--dyntext (aref forms--dyntexts ,forms--dyntext)))
+             (if (not (looking-at (regexp-quote forms--dyntext)))
+                 (error "Parse error: not looking at `%s'" forms--dyntext))
+             (forward-char (length forms--dyntext)))))
       (setq forms--dyntext (1+ forms--dyntext))
       (setq forms--seen-text t)
       (setq forms--field nil)))
@@ -1214,7 +1219,7 @@ Commands:                        Equivalent keys in read-only mode:
 
       ;; Need a file to do this.
       (if (not (file-exists-p forms-file))
-         (error "Need existing file or explicit 'forms-number-of-records'.")
+         (error "Need existing file or explicit 'forms-number-of-records'")
 
        ;; Visit the file and extract the first record.
        (setq forms--file-buffer (find-file-noselect forms-file))
@@ -1228,7 +1233,7 @@ Commands:                        Equivalent keys in read-only mode:
                  (goto-char (point-min))
                  (forms--get-record)))
 
-         ;; This may be overkill, but try to avoid interference with 
+         ;; This may be overkill, but try to avoid interference with
          ;; the normal processing.
          (kill-buffer forms--file-buffer)
 
@@ -1255,8 +1260,8 @@ Commands:                        Equivalent keys in read-only mode:
 (defun forms--set-keymaps ()
   "Set the keymaps used in this mode."
 
-  (use-local-map (if forms-read-only 
-                    forms-mode-ro-map 
+  (use-local-map (if forms-read-only
+                    forms-mode-ro-map
                   forms-mode-edit-map)))
 
 (defun forms--mode-commands ()
@@ -1398,7 +1403,7 @@ Commands:                        Equivalent keys in read-only mode:
   (put 'forms-delete-record 'menu-enable '(not forms-read-only))
 )
 
-(defun forms--mode-commands1 (map) 
+(defun forms--mode-commands1 (map)
   "Helper routine to define keys."
   (define-key map [TAB] 'forms-next-field)
   (define-key map [S-tab] 'forms-prev-field)
@@ -1418,23 +1423,15 @@ Commands:                        Equivalent keys in read-only mode:
   ;; scroll-up -> forms-next-record
   (if forms-forms-scroll
       (progn
-       (substitute-key-definition 'scroll-up 'forms-next-record
-                                  (current-local-map)
-                                  (current-global-map))
-       (substitute-key-definition 'scroll-down 'forms-prev-record
-                                  (current-local-map)
-                                  (current-global-map))))
+       (local-set-key [remap scroll-up] 'forms-next-record)
+       (local-set-key [remap scroll-down] 'forms-prev-record)))
   ;;
   ;; beginning-of-buffer -> forms-first-record
   ;; end-of-buffer -> forms-end-record
   (if forms-forms-jump
       (progn
-       (substitute-key-definition 'beginning-of-buffer 'forms-first-record
-                                  (current-local-map)
-                                  (current-global-map))
-       (substitute-key-definition 'end-of-buffer 'forms-last-record
-                                  (current-local-map)
-                                  (current-global-map))))
+       (local-set-key [remap beginning-of-buffer] 'forms-first-record)
+       (local-set-key [remap end-of-buffer] 'forms-last-record)))
   ;;
   ;; Save buffer
   (local-set-key "\C-x\C-s" 'forms-save-buffer)
@@ -1529,10 +1526,10 @@ Commands:                        Equivalent keys in read-only mode:
       (message "Warning: this record has %d fields instead of %d"
               (length forms--the-record-list) forms-number-of-fields))
     (if (< (length forms--the-record-list) forms-number-of-fields)
-       (setq forms--the-record-list 
+       (setq forms--the-record-list
              (append forms--the-record-list
-                     (make-list 
-                      (- forms-number-of-fields 
+                     (make-list
+                      (- forms-number-of-fields
                          (length forms--the-record-list))
                       "")))))
 
@@ -1545,13 +1542,14 @@ Commands:                        Equivalent keys in read-only mode:
   (set-buffer-modified-p nil)
   (setq buffer-read-only forms-read-only)
   (setq mode-line-process
-       (concat " " forms--current-record "/" forms--total-records)))
+       (concat " " (int-to-string forms--current-record)
+               "/" (int-to-string forms--total-records))))
 
 (defun forms--parse-form ()
   "Parse contents of form into list of strings."
   ;; The contents of the form are parsed, and a new list of strings
   ;; is constructed.
-  ;; A vector with the strings from the original record is 
+  ;; A vector with the strings from the original record is
   ;; constructed, which is updated with the new contents.  Therefore
   ;; fields which were not in the form are not modified.
   ;; Finally, the vector is transformed into a list for further processing.
@@ -1587,11 +1585,11 @@ As a side effect: sets `forms--the-record-list'."
     (setq forms--the-record-list (forms--parse-form))
     (setq the-record
          (mapconcat 'identity forms--the-record-list forms-field-sep))
-    
+
     (if (string-match (regexp-quote forms-field-sep)
                      (mapconcat 'identity forms--the-record-list ""))
        (error "Field separator occurs in record - update refused"))
-    
+
     ;; Handle multi-line fields, if allowed.
     (if forms-multi-line
        (forms--trans the-record "\n" forms-multi-line))
@@ -1694,7 +1692,8 @@ As a side effect: sets `forms--the-record-list'."
           (if (zerop disp)
               nil
             (setq cur (+ cur disp (- (forward-line disp)))))
-        (setq cur (+ cur disp (- (goto-line arg)))))
+        (goto-char (point-min))
+        (setq cur (+ cur disp (- (forward-line (1- arg))))))
 
        (forms--get-record)))
 
@@ -1714,7 +1713,7 @@ As a side effect: sets `forms--the-record-list'."
 As a side effect: re-calculates the number of records in the data file."
   (interactive)
   (let
-      ((numrec 
+      ((numrec
        (save-excursion
          (set-buffer forms--file-buffer)
          (count-lines (point-min) (point-max)))))
@@ -1770,7 +1769,7 @@ Otherwise enables edit mode if the visited file is writable."
 (defun forms-insert-record (arg)
   "Create a new record before the current one.
 With ARG: store the record after the current one.
-If `forms-new-record-filter' contains the name of a function, 
+If `forms-new-record-filter' contains the name of a function,
 it is called to fill (some of) the fields with default values.
 If `forms-insert-after is non-nil, the default behavior is to insert
 after the current record."
@@ -1804,11 +1803,12 @@ after the current record."
 
     (save-excursion
       (set-buffer forms--file-buffer)
-      (goto-line ln)
+      (goto-char (point-min))
+      (forward-line (1- ln))
       (open-line 1)
       (insert the-record)
       (beginning-of-line))
-    
+
     (setq forms--current-record ln))
 
   (setq forms--total-records (1+ forms--total-records))
@@ -1827,7 +1827,8 @@ after the current record."
       (let ((ln forms--current-record))
        (save-excursion
          (set-buffer forms--file-buffer)
-         (goto-line ln)
+         (goto-char (point-min))
+         (forward-line (1- ln))
          ;; Use delete-region instead of kill-region, to avoid
          ;; adding junk to the kill-ring.
          (delete-region (progn (beginning-of-line) (point))
@@ -1840,8 +1841,8 @@ after the current record."
 
 (defun forms-search-forward (regexp)
   "Search forward for record containing REGEXP."
-  (interactive 
-   (list (read-string (concat "Search forward for" 
+  (interactive
+   (list (read-string (concat "Search forward for"
                                  (if forms--search-regexp
                                   (concat " ("
                                           forms--search-regexp
@@ -1876,8 +1877,8 @@ after the current record."
 
 (defun forms-search-backward (regexp)
   "Search backward for record containing REGEXP."
-  (interactive 
-   (list (read-string (concat "Search backward for" 
+  (interactive
+   (list (read-string (concat "Search backward for"
                                  (if forms--search-regexp
                                   (concat " ("
                                           forms--search-regexp
@@ -1918,19 +1919,32 @@ after writing out the data."
   (interactive "p")
   (forms--checkmod)
   (let ((write-file-filter forms-write-file-filter)
-       (read-file-filter forms-read-file-filter))
+       (read-file-filter forms-read-file-filter)
+       (cur forms--current-record))
     (save-excursion
       (set-buffer forms--file-buffer)
       (let ((inhibit-read-only t))
        ;; Write file hooks are run via local-write-file-hooks.
-       ;; (if write-file-filter 
-       ;;  (save-excursion 
-       ;;   (run-hooks 'write-file-filter))) 
+       ;; (if write-file-filter
+       ;;  (save-excursion
+       ;;   (run-hooks 'write-file-filter)))
+
+       ;; If they have a write-file-filter, force the buffer to be
+       ;; saved even if it doesn't seem to be changed.  First, they
+       ;; might have changed the write-file-filter; and second, if
+       ;; save-buffer does nothing, write-file-filter won't get run,
+       ;; and then read-file-filter will be mightily confused.
+       (or (null write-file-filter)
+           (set-buffer-modified-p t))
        (save-buffer args)
        (if read-file-filter
           (save-excursion
             (run-hooks 'read-file-filter)))
-       (set-buffer-modified-p nil))))
+       (set-buffer-modified-p nil)))
+    ;; Make sure we end up with the same record number as we started.
+    ;; Since read-file-filter may perform arbitrary transformations on
+    ;; the data buffer contents, save-excursion is not enough.
+    (forms-jump-record cur))
   t)
 
 (defun forms--revert-buffer (&optional arg noconfirm)
@@ -2001,6 +2015,7 @@ after writing out the data."
   (interactive)
   (let ((inhibit-read-only t)
        (save-record forms--current-record)
+       (total-nb-records forms--total-records)
        (nb-record 1)
        (record nil))
     (while (<= nb-record forms--total-records)
@@ -2011,7 +2026,7 @@ after writing out the data."
        (goto-char (buffer-end 1))
        (insert record)
        (setq buffer-read-only nil)
-       (if (< nb-record forms--total-records)
+       (if (< nb-record total-nb-records)
            (insert "\n\f\n")))
       (setq nb-record (1+ nb-record)))
     (save-excursion
@@ -2061,7 +2076,7 @@ Usage: (setq forms-number-of-fields
                    (setq ret (concat ret (prin1-to-string vel) "\n")))
                (setq ret (concat ret "<unbound>" "\n")))
              (if (fboundp el)
-                 (setq ret (concat ret (prin1-to-string (symbol-function el)) 
+                 (setq ret (concat ret (prin1-to-string (symbol-function el))
                                    "\n"))))))
        (save-excursion
          (set-buffer (get-buffer-create "*forms-mode debug*"))
@@ -2070,4 +2085,4 @@ Usage: (setq forms-number-of-fields
          (goto-char (point-max))
          (insert ret)))))
 
-;;; forms.el ends here.
+;;; forms.el ends here