home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / utils / forms.el < prev    next >
Encoding:
Text File  |  1995-04-17  |  64.1 KB  |  2,023 lines

  1. ;;; forms.el --- Forms mode: edit a file as a form to fill in.
  2.  
  3. ;;; Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Johan Vromans <jv@nl.net>
  6. ;; Version: Revision: 2.10 
  7. ;; Keywords: extensions
  8. ;; hacked on by jwz for XEmacs
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;;; Visit a file using a form.
  29. ;;;
  30. ;;; === Naming conventions
  31. ;;;
  32. ;;; The names of all variables and functions start with 'forms-'.
  33. ;;; Names which start with 'forms--' are intended for internal use, and
  34. ;;; should *NOT* be used from the outside.
  35. ;;;
  36. ;;; All variables are buffer-local, to enable multiple forms visits 
  37. ;;; simultaneously.
  38. ;;; Variable `forms--mode-setup' is local to *ALL* buffers, for it 
  39. ;;; controls if forms-mode has been enabled in a buffer.
  40. ;;;
  41. ;;; === How it works ===
  42. ;;;
  43. ;;; Forms mode means visiting a data file which is supposed to consist
  44. ;;; of records each containing a number of fields.  The records are
  45. ;;; separated by a newline, the fields are separated by a user-defined
  46. ;;; field separater (default: TAB).
  47. ;;; When shown, a record is transferred to an Emacs buffer and
  48. ;;; presented using a user-defined form.  One record is shown at a
  49. ;;; time.
  50. ;;;
  51. ;;; Forms mode is a composite mode.  It involves two files, and two
  52. ;;; buffers.
  53. ;;; The first file, called the control file, defines the name of the
  54. ;;; data file and the forms format.  This file buffer will be used to
  55. ;;; present the forms.
  56. ;;; The second file holds the actual data.  The buffer of this file
  57. ;;; will be buried, for it is never accessed directly.
  58. ;;;
  59. ;;; Forms mode is invoked using M-x forms-find-file control-file .
  60. ;;; Alternativily `forms-find-file-other-window' can be used.
  61. ;;;
  62. ;;; You may also visit the control file, and switch to forms mode by hand
  63. ;;; with M-x forms-mode .
  64. ;;;
  65. ;;; Automatic mode switching is supported if you specify 
  66. ;;; "-*- forms -*-" in the first line of the control file.
  67. ;;; 
  68. ;;; The control file is visited, evaluated using `eval-current-buffer',
  69. ;;; and should set at least the following variables:
  70. ;;;
  71. ;;;    forms-file                [string]
  72. ;;;            The name of the data file.
  73. ;;;
  74. ;;;    forms-number-of-fields            [integer]
  75. ;;;            The number of fields in each record.
  76. ;;;
  77. ;;;    forms-format-list            [list]
  78. ;;;            Formatting instructions.
  79. ;;;
  80. ;;; `forms-format-list' should be a list, each element containing
  81. ;;;
  82. ;;;   - a string, e.g. "hello".  The string is inserted in the forms
  83. ;;;    "as is".
  84. ;;;   
  85. ;;;   - an integer, denoting a field number.
  86. ;;;    The contents of this field are inserted at this point.
  87. ;;;     Fields are numbered starting with number one.
  88. ;;;   
  89. ;;;   - a function call, e.g. (insert "text").
  90. ;;;    This function call is dynamically evaluated and should return a
  91. ;;;     string.  It should *NOT* have side-effects on the forms being
  92. ;;;     constructed.  The current fields are available to the function
  93. ;;;     in the variable `forms-fields', they should *NOT* be modified.
  94. ;;;   
  95. ;;;   - a lisp symbol, that must evaluate to one of the above.
  96. ;;;
  97. ;;; Optional variables which may be set in the control file:
  98. ;;;
  99. ;;;    forms-field-sep                [string, default TAB]
  100. ;;;            The field separator used to separate the
  101. ;;;            fields in the data file.  It may be a string.
  102. ;;;
  103. ;;;    forms-read-only                [bool, default nil]
  104. ;;;            Non-nil means that the data file is visited
  105. ;;;            read-only (view mode) as opposed to edit mode.
  106. ;;;            If no write access to the data file is
  107. ;;;            possible, view mode is enforced. 
  108. ;;;
  109. ;;;    forms-multi-line            [string, default "^K"]
  110. ;;;            If non-null the records of the data file may
  111. ;;;            contain fields that can span multiple lines in
  112. ;;;            the form.
  113. ;;;            This variable denotes the separator character
  114. ;;;            to be used for this purpose.  Upon display, all
  115. ;;;            occurrencies of this character are translated
  116. ;;;            to newlines.  Upon storage they are translated
  117. ;;;            back to the separator character.
  118. ;;;
  119. ;;;    forms-forms-scroll            [bool, default nil]
  120. ;;;            Non-nil means: rebind locally the commands that
  121. ;;;            perform `scroll-up' or `scroll-down' to use
  122. ;;;            `forms-next-field' resp. `forms-prev-field'.
  123. ;;;
  124. ;;;    forms-forms-jump            [bool, default nil]
  125. ;;;            Non-nil means: rebind locally the commands that
  126. ;;;            perform `beginning-of-buffer' or `end-of-buffer'
  127. ;;;            to perform `forms-first-field' resp. `forms-last-field'.
  128. ;;;
  129. ;;;    forms-read-file-filter            [symbol, default nil]
  130. ;;;            If not nil: this should be the name of a 
  131. ;;;            function that is called after the forms data file
  132. ;;;            has been read.  It can be used to transform
  133. ;;;            the contents of the file into a format more suitable
  134. ;;;            for forms-mode processing.
  135. ;;;
  136. ;;;    forms-write-file-filter            [symbol, default nil]
  137. ;;;            If not nil: this should be the name of a 
  138. ;;;            function that is called before the forms data file
  139. ;;;            is written (saved) to disk.  It can be used to undo
  140. ;;;            the effects of `forms-read-file-filter', if any.
  141. ;;;
  142. ;;;    forms-new-record-filter            [symbol, default nil]
  143. ;;;            If not nil: this should be the name of a 
  144. ;;;            function that is called when a new
  145. ;;;            record is created.  It can be used to fill in
  146. ;;;            the new record with default fields, for example.
  147. ;;;
  148. ;;;    forms-modified-record-filter        [symbol, default nil]
  149. ;;;            If not nil: this should be the name of a 
  150. ;;;            function that is called when a record has
  151. ;;;            been modified.  It is called after the fields
  152. ;;;            are parsed.  It can be used to register
  153. ;;;            modification dates, for example.
  154. ;;;
  155. ;;;    forms-use-extents            [bool, see text for default]
  156. ;;;    forms-use-text-properties        [bool, see text for default]
  157. ;;;            These variables control if forms mode should use
  158. ;;;            text properties or extents to protect the form text
  159. ;;;            from being modified (using text-property `read-only').
  160. ;;;            Also, the read-write fields are shown using a
  161. ;;;            distinct face, if possible.
  162. ;;;            One of these variables defaults to t if running 
  163. ;;;            FSF or Lucid Emacs 19.
  164. ;;;
  165. ;;;    forms-ro-face                 [symbol, default 'default]
  166. ;;;            This is the face that is used to show
  167. ;;;            read-only text on the screen.If used, this
  168. ;;;            variable should be set to a symbol that is a
  169. ;;;            valid face.
  170. ;;;            E.g.
  171. ;;;              (make-face 'my-face)
  172. ;;;              (setq forms-ro-face 'my-face)
  173. ;;;
  174. ;;;    forms-rw-face                [symbol, default 'region]
  175. ;;;            This is the face that is used to show
  176. ;;;            read-write text on the screen.
  177. ;;;
  178. ;;; After evaluating the control file, its buffer is cleared and used
  179. ;;; for further processing.
  180. ;;; The data file (as designated by `forms-file') is visited in a buffer
  181. ;;; `forms--file-buffer' which will not normally be shown.
  182. ;;; Great malfunctioning may be expected if this file/buffer is modified
  183. ;;; outside of this package while it is being visited!
  184. ;;;
  185. ;;; Normal operation is to transfer one line (record) from the data file,
  186. ;;; split it into fields (into `forms--the-record-list'), and display it
  187. ;;; using the specs in `forms-format-list'.
  188. ;;; A format routine `forms--format' is built upon startup to format 
  189. ;;; the records according to `forms-format-list'.
  190. ;;;
  191. ;;; When a form is changed the record is updated as soon as this form
  192. ;;; is left.  The contents of the form are parsed using information
  193. ;;; obtained from `forms-format-list', and the fields which are
  194. ;;; deduced from the form are modified.  Fields not shown on the forms
  195. ;;; retain their origional values.  The newly formed record then
  196. ;;; replaces the contents of the old record in `forms--file-buffer'.
  197. ;;; A parse routine `forms--parser' is built upon startup to parse
  198. ;;; the records.
  199. ;;;
  200. ;;; Two exit functions exist: `forms-exit' and `forms-exit-no-save'.
  201. ;;; `forms-exit' saves the data to the file, if modified.
  202. ;;; `forms-exit-no-save` does not.  However, if `forms-exit-no-save'
  203. ;;; is executed and the file buffer has been modified, Emacs will ask
  204. ;;; questions anyway.
  205. ;;;
  206. ;;; Other functions provided by forms mode are:
  207. ;;;
  208. ;;;    paging (forward, backward) by record
  209. ;;;    jumping (first, last, random number)
  210. ;;;    searching
  211. ;;;    creating and deleting records
  212. ;;;    reverting the form (NOT the file buffer)
  213. ;;;    switching edit <-> view mode v.v.
  214. ;;;    jumping from field to field
  215. ;;;
  216. ;;; As an documented side-effect: jumping to the last record in the
  217. ;;; file (using forms-last-record) will adjust forms--total-records if
  218. ;;; needed.
  219. ;;;
  220. ;;; The forms buffer can be in on eof two modes: edit mode or view
  221. ;;; mode.  View mode is a read-only mode, you cannot modify the
  222. ;;; contents of the buffer.
  223. ;;;
  224. ;;; Edit mode commands:
  225. ;;; 
  226. ;;; TAB         forms-next-field
  227. ;;; \C-c TAB     forms-next-field
  228. ;;; \C-c <     forms-first-record
  229. ;;; \C-c >     forms-last-record
  230. ;;; \C-c ?     describe-mode
  231. ;;; \C-c \C-k     forms-delete-record
  232. ;;; \C-c \C-q     forms-toggle-read-only
  233. ;;; \C-c \C-o     forms-insert-record
  234. ;;; \C-c \C-l     forms-jump-record
  235. ;;; \C-c \C-n     forms-next-record
  236. ;;; \C-c \C-p     forms-prev-record
  237. ;;; \C-c \C-s     forms-search
  238. ;;; \C-c \C-x     forms-exit
  239. ;;; 
  240. ;;; Read-only mode commands:
  241. ;;; 
  242. ;;; SPC      forms-next-record
  243. ;;; DEL     forms-prev-record
  244. ;;; ?     describe-mode
  245. ;;; \C-q forms-toggle-read-only
  246. ;;; l     forms-jump-record
  247. ;;; n     forms-next-record
  248. ;;; p     forms-prev-record
  249. ;;; s     forms-search
  250. ;;; x     forms-exit
  251. ;;; 
  252. ;;; Of course, it is also possible to use the \C-c prefix to obtain the
  253. ;;; same command keys as in edit mode.
  254. ;;; 
  255. ;;; The following bindings are available, independent of the mode: 
  256. ;;; 
  257. ;;; [next]      forms-next-record
  258. ;;; [prior]      forms-prev-record
  259. ;;; [begin]      forms-first-record
  260. ;;; [end]      forms-last-record
  261. ;;; [S-TAB]      forms-prev-field
  262. ;;; [backtab] forms-prev-field
  263. ;;;
  264. ;;; For convenience, TAB is always bound to `forms-next-field', so you
  265. ;;; don't need the C-c prefix for this command.
  266. ;;;
  267. ;;; As mentioned above (see `forms-forms-scroll' and `forms-forms-jump')
  268. ;;; the bindings of standard functions `scroll-up', `scroll-down',
  269. ;;; `beginning-of-buffer' and `end-of-buffer' can be locally replaced with
  270. ;;; forms mode functions next/prev record and first/last
  271. ;;; record.
  272. ;;;
  273. ;;; `local-write-file hook' is defined to save the actual data file
  274. ;;; instead of the buffer data, `revert-file-hook' is defined to
  275. ;;; revert a forms to original.
  276.  
  277. ;;; Code:
  278.  
  279. ;;; Global variables and constants:
  280.  
  281. (provide 'forms)            ;;; official
  282. (provide 'forms-mode)            ;;; for compatibility
  283.  
  284. (defconst forms-version (substring "!Revision: 2.10 !" 11 -2)
  285.   "The version number of forms-mode (as string).  The complete RCS id is:
  286.  
  287.   !Id: forms.el,v 2.10 1994/07/26 21:31:13 rms Exp !")
  288.  
  289. (defvar forms-mode-hooks nil
  290.   "Hook functions to be run upon entering Forms mode.")
  291.  
  292. ;;; Mandatory variables - must be set by evaluating the control file.
  293.  
  294. (defvar forms-file nil
  295.   "Name of the file holding the data.")
  296.  
  297. (defvar forms-format-list nil
  298.   "List of formatting specifications.")
  299.  
  300. (defvar forms-number-of-fields nil
  301.   "Number of fields per record.")
  302.  
  303. ;;; Optional variables with default values.
  304.  
  305. (defvar forms-field-sep "\t"
  306.   "Field separator character (default TAB).")
  307.  
  308. (defvar forms-read-only nil
  309.   "Non-nil means: visit the file in view (read-only) mode.
  310. (Defaults to the write access on the data file).")
  311.  
  312. (defvar forms-multi-line "\C-k"
  313.   "If not nil: use this character to separate multi-line fields (default C-k).")
  314.  
  315. (defvar forms-forms-scroll nil
  316.   "*Non-nil means replace scroll-up/down commands in Forms mode.
  317. The replacement commands performs forms-next/prev-record.")
  318.  
  319. (defvar forms-forms-jump nil
  320.   "*Non-nil means redefine beginning/end-of-buffer in Forms mode.
  321. The replacement commands performs forms-first/last-record.")
  322.  
  323. (defvar forms-read-file-filter nil
  324.   "The name of a function that is called after reading the data file.
  325. This can be used to change the contents of the file to something more
  326. suitable for forms processing.")
  327.  
  328. (defvar forms-write-file-filter nil
  329.   "The name of a function that is called before writing the data file.
  330. This can be used to undo the effects of form-read-file-hook.")
  331.  
  332. (defvar forms-new-record-filter nil
  333.   "The name of a function that is called when a new record is created.")
  334.  
  335. (defvar forms-modified-record-filter nil
  336.   "The name of a function that is called when a record has been modified.")
  337.  
  338. (defvar forms-fields nil
  339.   "List with fields of the current forms.  First field has number 1.
  340. This variable is for use by the filter routines only. 
  341. The contents may NOT be modified.")
  342.  
  343. (defvar forms-use-extents (fboundp 'set-extent-property) ; XEmacs 19.9+
  344.   "*Non-nil means: use XEmacs/Lucid Emacs extents.
  345. Defaults to t if this emacs is capable of handling text properties.")
  346.  
  347. (defvar forms-use-text-properties (and (fboundp 'set-text-properties)
  348.                        (not forms-use-extents))
  349.   "*Non-nil means: use emacs-19 text properties.
  350. Defaults to t if this emacs is capable of handling text properties.")
  351.  
  352. (defvar forms-ro-face (if (string-match "XEmacs" emacs-version)
  353.               'forms-label-face
  354.             'default)
  355.   "The face (a symbol) that is used to display read-only text on the screen.")
  356.  
  357. (defvar forms-rw-face (if (string-match "XEmacs" emacs-version)
  358.               'forms-field-face
  359.             'region)
  360.   "The face (a symbol) that is used to display read-write text on the screen.")
  361.  
  362. ;;; Internal variables.
  363.  
  364. (defvar forms--lemacs-p (string-match "XEmacs" emacs-version))
  365.  
  366. (defvar forms--file-buffer nil
  367.   "Buffer which holds the file data")
  368.  
  369. (defvar forms--total-records 0
  370.   "Total number of records in the data file.")
  371.  
  372. (defvar forms--current-record 0
  373.   "Number of the record currently on the screen.")
  374.  
  375. (defvar forms-mode-map nil
  376.    "Keymap for form buffer.")
  377. (defvar forms-mode-ro-map nil
  378.    "Keymap for form buffer in view mode.")
  379. (defvar forms-mode-edit-map nil
  380.    "Keymap for form buffer in edit mode.")
  381.  
  382. (defvar forms--markers nil
  383.   "Field markers in the screen.")
  384.  
  385. (defvar forms--dyntexts nil
  386.   "Dynamic texts (resulting from function calls) on the screen.")
  387.  
  388. (defvar forms--the-record-list nil 
  389.    "List of strings of the current record, as parsed from the file.")
  390.  
  391. (defvar forms--search-regexp nil
  392.   "Last regexp used by forms-search.")
  393.  
  394. (defvar forms--format nil
  395.   "Formatting routine.")
  396.  
  397. (defvar forms--parser nil
  398.   "Forms parser routine.")
  399.  
  400. (defvar forms--mode-setup nil
  401.   "To keep track of forms-mode being set-up.")
  402. (make-variable-buffer-local 'forms--mode-setup)
  403.  
  404. (defvar forms--dynamic-text nil
  405.   "Array that holds dynamic texts to insert between fields.")
  406.  
  407. (defvar forms--elements nil
  408.   "Array with the order in which the fields are displayed.")
  409.  
  410. (defvar forms--ro-face nil
  411.   "Face used to represent read-only data on the screen.")
  412.  
  413. (defvar forms--rw-face nil
  414.   "Face used to represent read-write data on the screen.")
  415.  
  416.  
  417. ;;;###autoload 
  418. (defun forms-mode (&optional primary)
  419.   "Major mode to visit files in a field-structured manner using a form.
  420.  
  421. Commands:                        Equivalent keys in read-only mode:
  422.  
  423.  TAB          forms-next-field           TAB
  424.  C-c TAB      forms-next-field
  425.  C-c <        forms-first-record         <
  426.  C-c >        forms-last-record          >
  427.  C-c ?        describe-mode              ?
  428.  C-c C-k      forms-delete-record
  429.  C-c C-q      forms-toggle-read-only     q
  430.  C-c C-o      forms-insert-record
  431.  C-c C-l      forms-jump-record          l
  432.  C-c C-n      forms-next-record          n
  433.  C-c C-p      forms-prev-record          p
  434.  C-c C-s      forms-search               s
  435.  C-c C-x      forms-exit                 x"
  436.   (interactive)
  437.  
  438.   ;; This is not a simple major mode, as usual.  Therefore, forms-mode
  439.   ;; takes an optional argument `primary' which is used for the
  440.   ;; initial set-up.  Normal use would leave `primary' to nil.
  441.   ;; A global buffer-local variable `forms--mode-setup' has the same
  442.   ;; effect but makes it possible to auto-invoke forms-mode using
  443.   ;; `find-file'.
  444.   ;; Note: although it seems logical to have `make-local-variable'
  445.   ;; executed where the variable is first needed, I have deliberately
  446.   ;; placed all calls in this function.
  447.  
  448.   ;; Primary set-up: evaluate buffer and check if the mandatory
  449.   ;; variables have been set.
  450.   (if (or primary (not forms--mode-setup))
  451.       (progn
  452.     ;;(message "forms: setting up...")
  453.     (kill-all-local-variables)
  454.  
  455.     ;; Make mandatory variables.
  456.     (make-local-variable 'forms-file)
  457.     (make-local-variable 'forms-number-of-fields)
  458.     (make-local-variable 'forms-format-list)
  459.  
  460.     ;; Make optional variables.
  461.     (make-local-variable 'forms-field-sep)
  462.         (make-local-variable 'forms-read-only)
  463.         (make-local-variable 'forms-multi-line)
  464.     (make-local-variable 'forms-forms-scroll)
  465.     (make-local-variable 'forms-forms-jump)
  466. ;;    (make-local-variable 'forms-use-text-properties)
  467.  
  468.     ;; Filter functions.
  469.     (make-local-variable 'forms-read-file-filter)
  470.     (make-local-variable 'forms-write-file-filter)
  471.     (make-local-variable 'forms-new-record-filter)
  472.     (make-local-variable 'forms-modified-record-filter)
  473.  
  474.     ;; Make sure no filters exist.
  475.     (setq forms-read-file-filter nil)
  476.     (setq forms-write-file-filter nil)
  477.     (setq forms-new-record-filter nil)
  478.     (setq forms-modified-record-filter nil)
  479.  
  480.     (if forms--lemacs-p
  481.         (progn
  482.           ;; forms-field-face defaults to bold.
  483.           ;; forms-label-face defaults to no attributes
  484.           ;;   (inherit from default.)
  485.           (make-face 'forms-field-face)
  486.           (make-face 'forms-label-face)
  487.           (if (face-differs-from-default-p 'forms-field-face)
  488.           nil
  489.         (copy-face 'bold 'forms-field-face)
  490.         ;;(set-face-underline-p 'forms-field-face t)
  491.         )))
  492.  
  493.     ;; If running Emacs 19 under X, setup faces to show read-only and 
  494.     ;; read-write fields.
  495.     (if (fboundp 'make-face)
  496.         (progn
  497.           (make-local-variable 'forms-ro-face)
  498.           (make-local-variable 'forms-rw-face)))
  499.  
  500.     ;; eval the buffer, should set variables
  501.     ;;(message "forms: processing control file...")
  502.     ;; If enable-local-eval is not set to t the user is asked first.
  503.     (if (or (eq enable-local-eval t)
  504.         (yes-or-no-p 
  505.          (concat "Evaluate lisp code in buffer "
  506.              (buffer-name) " to display forms ")))
  507.         (eval-current-buffer)
  508.       (error "`enable-local-eval' inhibits buffer evaluation"))
  509.  
  510.     ;; Check if the mandatory variables make sense.
  511.     (or forms-file
  512.         (error (concat "Forms control file error: " 
  513.                "'forms-file' has not been set")))
  514.  
  515.     ;; Check forms-field-sep first, since it can be needed to
  516.     ;; construct a default format list.
  517.     (or (stringp forms-field-sep)
  518.         (error (concat "Forms control file error: "
  519.                "'forms-field-sep' is not a string")))
  520.  
  521.     (if forms-number-of-fields
  522.         (or (and (numberp forms-number-of-fields)
  523.              (> forms-number-of-fields 0))
  524.         (error (concat "Forms control file error: "
  525.                    "'forms-number-of-fields' must be a number > 0")))
  526.       (or (null forms-format-list)
  527.           (error (concat "Forms control file error: "
  528.                  "'forms-number-of-fields' has not been set"))))
  529.  
  530.     (or forms-format-list
  531.         (forms--intuit-from-file))
  532.  
  533.     (if forms-multi-line
  534.         (if (and (stringp forms-multi-line)
  535.              (eq (length forms-multi-line) 1))
  536.         (if (string= forms-multi-line forms-field-sep)
  537.             (error (concat "Forms control file error: " 
  538.                    "'forms-multi-line' is equal to 'forms-field-sep'")))
  539.           (error (concat "Forms control file error: "
  540.                  "'forms-multi-line' must be nil or a one-character string"))))
  541. ;;    (or (fboundp 'set-text-properties)
  542. ;;        (setq forms-use-text-properties nil))
  543.         
  544.     ;; Validate and process forms-format-list.
  545.     ;;(message "forms: pre-processing format list...")
  546.     (forms--process-format-list)
  547.  
  548.     ;; Build the formatter and parser.
  549.     ;;(message "forms: building formatter...")
  550.     (make-local-variable 'forms--format)
  551.     (make-local-variable 'forms--markers)
  552.     (make-local-variable 'forms--dyntexts)
  553.     (make-local-variable 'forms--elements)
  554.     ;;(message "forms: building parser...")
  555.     (forms--make-format)
  556.     (make-local-variable 'forms--parser)
  557.     (forms--make-parser)
  558.     ;;(message "forms: building parser... done.")
  559.  
  560.     ;; Check if record filters are defined.
  561.     (if (and forms-new-record-filter
  562.          (not (fboundp forms-new-record-filter)))
  563.         (error (concat "Forms control file error: "
  564.                "'forms-new-record-filter' is not a function")))
  565.  
  566.     (if (and forms-modified-record-filter
  567.          (not (fboundp forms-modified-record-filter)))
  568.         (error (concat "Forms control file error: "
  569.                "'forms-modified-record-filter' is not a function")))
  570.  
  571.     ;; The filters acces the contents of the forms using `forms-fields'.
  572.     (make-local-variable 'forms-fields)
  573.  
  574.     ;; Dynamic text support.
  575.     (make-local-variable 'forms--dynamic-text)
  576.  
  577.     ;; Prevent accidental overwrite of the control file and autosave.
  578.     (setq buffer-file-name nil)
  579.     (auto-save-mode nil)
  580.  
  581.     ;; Prepare this buffer for further processing.
  582.     (setq buffer-read-only nil)
  583.     (erase-buffer)
  584.  
  585.     ;;(message "forms: setting up... done.")
  586.     ))
  587.  
  588.   ;; initialization done
  589.   (setq forms--mode-setup t)
  590.  
  591.   ;; Copy desired faces to the actual variables used by the forms formatter.
  592.   (if (fboundp 'make-face)
  593.       (progn
  594.     (make-local-variable 'forms--ro-face)
  595.     (make-local-variable 'forms--rw-face)
  596.     (if forms-read-only
  597.         (progn
  598.           (setq forms--ro-face forms-ro-face)
  599.           (setq forms--rw-face forms-ro-face))
  600.       (setq forms--ro-face forms-ro-face)
  601.       (setq forms--rw-face forms-rw-face))))
  602.  
  603.   ;; Make more local variables.
  604.   (make-local-variable 'forms--file-buffer)
  605.   (make-local-variable 'forms--total-records)
  606.   (make-local-variable 'forms--current-record)
  607.   (make-local-variable 'forms--the-record-list)
  608.   (make-local-variable 'forms--search-regexp)
  609.  
  610.   ; The keymaps are global, so multiple forms mode buffers can share them.
  611.   ;(make-local-variable 'forms-mode-map)
  612.   ;(make-local-variable 'forms-mode-ro-map)
  613.   ;(make-local-variable 'forms-mode-edit-map)
  614.   (if forms-mode-map            ; already defined
  615.       nil
  616.     ;;(message "forms: building keymap...")
  617.     (forms--mode-commands)
  618.     ;;(message "forms: building keymap... done.")
  619.     )
  620.  
  621.   ;; find the data file
  622.   (setq forms--file-buffer (find-file-noselect forms-file))
  623.  
  624.   ;; Pre-transform.
  625.   (let ((read-file-filter forms-read-file-filter)
  626.     (write-file-filter forms-write-file-filter))
  627.     (if read-file-filter
  628.     (save-excursion
  629.       (set-buffer forms--file-buffer)
  630.       (let ((inhibit-read-only t))
  631.         (run-hooks 'read-file-filter))
  632.       (set-buffer-modified-p nil)
  633.       (if write-file-filter
  634.           (progn
  635.         (make-variable-buffer-local 'local-write-file-hooks)
  636.         (setq local-write-file-hooks (list write-file-filter)))))
  637.       (if write-file-filter
  638.       (save-excursion
  639.         (set-buffer forms--file-buffer)
  640.         (make-variable-buffer-local 'local-write-file-hooks)
  641.         (setq local-write-file-hooks write-file-filter)))))
  642.  
  643.   ;; count the number of records, and set see if it may be modified
  644.   (let (ro)
  645.     (setq forms--total-records
  646.       (save-excursion
  647.         (prog1
  648.         (progn
  649.           ;;(message "forms: counting records...")
  650.           (set-buffer forms--file-buffer)
  651.           (bury-buffer (current-buffer))
  652.           (setq ro buffer-read-only)
  653.           (count-lines (point-min) (point-max)))
  654.           ;;(message "forms: counting records... done.")
  655.           )))
  656.     (if ro
  657.     (setq forms-read-only t)))
  658.  
  659.   ;;(message "forms: proceeding setup...")
  660.   ;; set the major mode indicator
  661.   (setq major-mode 'forms-mode)
  662.   (setq mode-name "Forms")
  663.  
  664.   ;; Since we aren't really implementing a minor mode, we hack the modeline
  665.   ;; directly to get the text " View " into forms-read-only form buffers.  For
  666.   ;; that reason, this variable must be buffer only.
  667.   (make-local-variable 'minor-mode-alist)
  668.   (setq minor-mode-alist (list (list 'forms-read-only " View")))
  669.  
  670.   ;;(message "forms: proceeding setup (keymaps)...")
  671.   (forms--set-keymaps)
  672.   ;;(message "forms: proceeding setup (commands)...")
  673.   (forms--change-commands)
  674.  
  675.   ;;(message "forms: proceeding setup (buffer)...")
  676.   (set-buffer-modified-p nil)
  677.  
  678.   (if (= forms--total-records 0)
  679.       ;;(message "forms: proceeding setup (new file)...")
  680.       (progn
  681.     (insert 
  682.      "GNU Emacs Forms Mode version " forms-version "\n\n"
  683.      (if (file-exists-p forms-file)
  684.          (concat "No records available in file \"" forms-file "\".\n\n")
  685.        (format "Creating new file \"%s\"\nwith %d field%s per record.\n\n"
  686.            forms-file forms-number-of-fields
  687.            (if (= 1 forms-number-of-fields) "" "s")))
  688.      "Use " (substitute-command-keys "\\[forms-insert-record]")
  689.      " to create new records.\n")
  690.     (setq forms--current-record 1)
  691.     (setq buffer-read-only t)
  692.     (set-buffer-modified-p nil))
  693.  
  694.     ;; setup the first (or current) record to show
  695.     (if (< forms--current-record 1)
  696.     (setq forms--current-record 1))
  697.     (forms-jump-record forms--current-record)
  698.     )
  699.  
  700.   ;; user customising
  701.   ;;(message "forms: proceeding setup (user hooks)...")
  702.   (run-hooks 'forms-mode-hooks)
  703.   ;;(message "forms: setting up... done.")
  704.  
  705.   ;; be helpful
  706.   (forms--help)
  707. )
  708.  
  709. (defun forms--process-format-list ()
  710.   ;; Validate `forms-format-list' and set some global variables.
  711.   ;; Symbols in the list are evaluated, and consecutive strings are
  712.   ;; concatenated.
  713.   ;; Array `forms--elements' is constructed that contains the order
  714.   ;; of the fields on the display. This array is used by 
  715.   ;; `forms--parser-using-text-properties' to extract the fields data
  716.   ;; from the form on the screen.
  717.   ;; Upon completion, `forms-format-list' is garanteed correct, so
  718.   ;; `forms--make-format' and `forms--make-parser' do not need to perform
  719.   ;; any checks.
  720.  
  721.   ;; Verify that `forms-format-list' is not nil.
  722.   (or forms-format-list
  723.       (error (concat "Forms control file error: "
  724.              "'forms-format-list' has not been set")))
  725.   ;; It must be a list.
  726.   (or (listp forms-format-list)
  727.       (error (concat "Forms control file error: "
  728.              "'forms-format-list' is not a list")))
  729.  
  730.   ;; Assume every field is painted once.
  731.   ;; `forms--elements' will grow if needed.
  732.   (setq forms--elements (make-vector forms-number-of-fields nil))
  733.  
  734.   (let ((the-list forms-format-list)    ; the list of format elements
  735.     (this-item 0)            ; element in list
  736.     (prev-item nil)
  737.     (field-num 0))            ; highest field number 
  738.  
  739.     (setq forms-format-list nil)    ; gonna rebuild
  740.  
  741.     (while the-list
  742.  
  743.       (let ((el (car-safe the-list))
  744.         (rem (cdr-safe the-list)))
  745.  
  746.     ;; If it is a symbol, eval it first.
  747.     (if (and (symbolp el)
  748.          (boundp el))
  749.         (setq el (eval el)))
  750.  
  751.     (cond
  752.  
  753.      ;; Try string ...
  754.      ((stringp el)
  755.       (if (stringp prev-item)    ; try to concatenate strings
  756.           (setq prev-item (concat prev-item el))
  757.         (if prev-item
  758.         (setq forms-format-list
  759.               (append forms-format-list (list prev-item) nil)))
  760.         (setq prev-item el)))
  761.  
  762.      ;; Try numeric ...
  763.      ((numberp el) 
  764.  
  765.       ;; Validate range.
  766.       (if (or (<= el 0)
  767.           (> el forms-number-of-fields))
  768.           (error (concat "Forms format error: "
  769.                  "field number %d out of range 1..%d")
  770.              el forms-number-of-fields))
  771.  
  772.       ;; Store forms order.
  773.       (if (> field-num (length forms--elements))
  774.           (setq forms--elements (vconcat forms--elements (1- el)))
  775.         (aset forms--elements field-num (1- el)))
  776.       (setq field-num (1+ field-num))
  777.  
  778.       (if prev-item
  779.           (setq forms-format-list
  780.             (append forms-format-list (list prev-item) nil)))
  781.       (setq prev-item el))
  782.  
  783.      ;; Try function ...
  784.      ((listp el)
  785.  
  786.       ;; Validate.
  787.       (or (fboundp (car-safe el))
  788.           (error (concat "Forms format error: "
  789.                  "not a function "
  790.                  (prin1-to-string (car-safe el)))))
  791.  
  792.       ;; Shift.
  793.       (if prev-item
  794.           (setq forms-format-list
  795.             (append forms-format-list (list prev-item) nil)))
  796.       (setq prev-item el))
  797.  
  798.      ;; else
  799.      (t
  800.       (error (concat "Forms format error: "
  801.              "invalid element "
  802.              (prin1-to-string el)))))
  803.  
  804.     ;; Advance to next element of the list.
  805.     (setq the-list rem)))
  806.  
  807.     ;; Append last item.
  808.     (if prev-item
  809.     (progn
  810.       (setq forms-format-list
  811.         (append forms-format-list (list prev-item) nil))
  812.       ;; Append a newline if the last item is a field.
  813.       ;; This prevents parsing problems.
  814.       ;; Also it makes it possible to insert an empty last field.
  815.       (if (numberp prev-item)
  816.           (setq forms-format-list
  817.             (append forms-format-list (list "\n") nil))))))
  818.  
  819.   (forms--debug 'forms-format-list
  820.         'forms--elements))
  821.  
  822. ;; Special treatment for read-only segments.  (FSF19 only)
  823. ;;
  824. ;; If text is inserted between two read-only segments, it inherits the
  825. ;; read-only properties.  This is not what we want.
  826. ;; To solve this, read-only segments get the `insert-in-front-hooks'
  827. ;; property set with a function that temporarily switches the properties
  828. ;; of the first character of the segment to read-write, so the new
  829. ;; text gets the right properties.
  830. ;; The `post-command-hook' is used to restore the original properties.
  831.  
  832. (defvar forms--iif-start nil
  833.   "Record start of modification command.")
  834. (defvar forms--iif-properties nil
  835.   "Original properties of the character being overridden.")
  836.  
  837. (defun forms--iif-hook (begin end)
  838.   "`insert-in-front-hooks' function for read-only segments."
  839.  
  840.   ;; Note start location.  By making it a marker that points one 
  841.   ;; character beyond the actual location, it is guaranteed to move 
  842.   ;; correctly if text is inserted.
  843.   (or forms--iif-start
  844.       (setq forms--iif-start (copy-marker (1+ (point)))))
  845.  
  846.   ;; Check if there is special treatment required.
  847.   (if (or (<= forms--iif-start 2)
  848.       (get-text-property (- forms--iif-start 2)
  849.                  'read-only))
  850.       (progn
  851.     ;; Fetch current properties.
  852.     (setq forms--iif-properties 
  853.           (text-properties-at (1- forms--iif-start)))
  854.  
  855.     ;; Replace them.
  856.     (let ((inhibit-read-only t))
  857.       (set-text-properties 
  858.        (1- forms--iif-start) forms--iif-start
  859.        (list 'face forms--rw-face 'front-sticky '(face))))
  860.  
  861.     ;; Enable `post-command-hook' to restore the properties.
  862.     (setq post-command-hook
  863.           (append (list 'forms--iif-post-command-hook) post-command-hook)))
  864.  
  865.     ;; No action needed.  Clear marker.
  866.     (setq forms--iif-start nil)))
  867.  
  868. (defun forms--iif-post-command-hook ()
  869.   "`post-command-hook' function for read-only segments."
  870.  
  871.   ;; Disable `post-command-hook'.
  872.   (setq post-command-hook
  873.     (delq 'forms--iif-hook-post-command-hook post-command-hook))
  874.  
  875.   ;; Restore properties.
  876.   (if forms--iif-start
  877.       (let ((inhibit-read-only t))
  878.     (set-text-properties 
  879.      (1- forms--iif-start) forms--iif-start
  880.      forms--iif-properties)))
  881.  
  882.   ;; Cleanup.
  883.   (setq forms--iif-start nil))
  884.  
  885. (defvar forms--marker)
  886. (defvar forms--dyntext)
  887.  
  888. (defun forms--make-format ()
  889.   "Generate `forms--format' using the information in `forms-format-list'."
  890.  
  891.   ;; The real work is done using a mapcar of `forms--make-format-elt' on
  892.   ;; `forms-format-list'.
  893.   ;; This function sets up the necessary environment, and decides
  894.   ;; which function to mapcar.
  895.  
  896.   (let ((forms--marker 0)
  897.     (forms--dyntext 0))
  898.     (setq 
  899.      forms--format
  900.      (if forms-use-text-properties 
  901.      (` (lambda (arg)
  902.           (let ((inhibit-read-only t))
  903.         (,@ (apply 'append
  904.                (mapcar 'forms--make-format-elt-using-text-properties
  905.                    forms-format-list)))
  906.         ;; Prevent insertion before the first text.
  907.         (,@ (if (numberp (car forms-format-list))
  908.             nil
  909.               '((add-text-properties (point-min) (1+ (point-min))
  910.                          '(front-sticky (read-only))))))
  911.         ;; Prevent insertion after the last text.
  912.         (remove-text-properties (1- (point)) (point)
  913.                     '(rear-nonsticky)))
  914.           (setq forms--iif-start nil)))
  915.        (if forms-use-extents
  916.        (` (lambda (arg)
  917.         (,@ (apply 'append
  918.                (mapcar 'forms--make-format-elt-using-extents
  919.                    forms-format-list)))
  920.  
  921.         ;; After creating all the extents, set their endpoint behavior.
  922.         ;; We can't do this when creating the extents, because
  923.         ;; otherwise the text we insert for the labels would be
  924.         ;; interpreted as user input, and would alter the endpoints
  925.         ;; of the previous extents we created (the text-entry fields
  926.         ;; would be extended by the following static-text areas.)
  927.         (map-extents
  928.          (function
  929.           (lambda (extent ignore)
  930.             (cond
  931.              ((not (extent-property extent 'forms))
  932.               ;; it's not one of ours; leave it alone.
  933.               nil)
  934.              ((not (extent-property extent 'read-only))
  935.               ;; text-entry fields should be [closed,closed] so that
  936.               ;; characters at either boundary go into them.
  937.               (set-extent-property extent 'end-open nil))
  938.              ;; Read-only fields should be (open,open) so that a
  939.              ;; read-only error isn't signalled when characters are
  940.              ;; inserted adjascent to them.  However, the very first
  941.              ;; label should be [closed,open) so that one can't
  942.              ;; insert text at point-min before the first label,
  943.              ;; and the very last should be (open,closed] for the
  944.              ;; same reason.
  945.              ((= (point-min) (extent-start-position extent))
  946.               (set-extent-property extent 'start-open nil)
  947.               (set-extent-property extent 'end-open t))
  948.              ((= (point-max) (extent-end-position extent))
  949.               (set-extent-property extent 'start-open t)
  950.               (set-extent-property extent 'end-open nil))
  951.              (t
  952.               (set-extent-property extent 'start-open t)
  953.               (set-extent-property extent 'end-open t)))
  954.             ;; return nil to continue mapping.
  955.             nil))
  956.          (current-buffer) (point-min) (point-max))
  957.         ))
  958.      (` (lambda (arg)
  959.           (,@ (apply 'append
  960.              (mapcar 'forms--make-format-elt
  961.                  forms-format-list))))))))
  962.  
  963.     ;; We have tallied the number of markers and dynamic texts,
  964.     ;; so we can allocate the arrays now.
  965.     (setq forms--markers (make-vector forms--marker nil))
  966.     (setq forms--dyntexts (make-vector forms--dyntext nil)))
  967.   (forms--debug 'forms--format))
  968.  
  969. (defun forms--make-format-elt-using-text-properties (el)
  970.   "Helper routine to generate format function."
  971.  
  972.   ;; The format routine `forms--format' will look like
  973.   ;;
  974.   ;; ;; preamble
  975.   ;; (lambda (arg)
  976.   ;;   (let ((inhibit-read-only t))
  977.   ;;
  978.   ;;     ;; A string, e.g. "text: ".
  979.   ;;     (set-text-properties 
  980.   ;;      (point)
  981.   ;;      (progn (insert "text: ") (point)) 
  982.   ;;      (list 'face forms--ro-face
  983.   ;;        'read-only 1
  984.   ;;        'insert-in-front-hooks 'forms--iif-hook
  985.   ;;        'rear-nonsticky '(read-only face insert-in-front-hooks)))
  986.   ;;
  987.   ;;     ;; A field, e.g. 6.
  988.   ;;     (let ((here (point)))
  989.   ;;       (aset forms--markers 0 (point-marker))
  990.   ;;       (insert (elt arg 5))
  991.   ;;       (or (= (point) here)
  992.   ;;       (set-text-properties 
  993.   ;;        here (point)
  994.   ;;        (list 'face forms--rw-face
  995.   ;;         'front-sticky '(face))))
  996.   ;;
  997.   ;;     ;; Another string, e.g. "\nmore text: ".
  998.   ;;     (set-text-properties
  999.   ;;      (point)
  1000.   ;;      (progn (insert "\nmore text: ") (point))
  1001.   ;;      (list 'face forms--ro-face
  1002.   ;;        'read-only 2
  1003.   ;;        'insert-in-front-hooks 'forms--iif-hook
  1004.   ;;        'rear-nonsticky '(read-only face insert-in-front-hooks)))
  1005.   ;;
  1006.   ;;     ;; A function, e.g. (tocol 40).
  1007.   ;;     (set-text-properties
  1008.   ;;      (point)
  1009.   ;;      (progn
  1010.   ;;        (insert (aset forms--dyntexts 0 (tocol 40)))
  1011.   ;;        (point))
  1012.   ;;      (list 'face forms--ro-face
  1013.   ;;        'read-only 2
  1014.   ;;        'insert-in-front-hooks 'forms--iif-hook
  1015.   ;;        'rear-nonsticky '(read-only face insert-in-front-hooks)))
  1016.   ;;
  1017.   ;;     ;; Prevent insertion before the first text.
  1018.   ;;     (add-text-properties (point-min) (1+ (point-min))
  1019.   ;;                  '(front-sticky (read-only))))))
  1020.   ;;     ;; Prevent insertion after the last text.
  1021.   ;;     (remove-text-properties (1- (point)) (point)
  1022.   ;;                  '(rear-nonsticky)))
  1023.   ;;
  1024.   ;;     ;; wrap up
  1025.   ;;     (setq forms--iif-start nil)
  1026.   ;;     ))
  1027.  
  1028.   (cond
  1029.    ((stringp el)
  1030.     
  1031.     (` ((set-text-properties 
  1032.      (point)            ; start at point
  1033.      (progn                ; until after insertion
  1034.        (insert (, el))
  1035.        (point))
  1036.      (list 'face forms--ro-face    ; read-only appearance
  1037.            'read-only (,@ (list (1+ forms--marker)))
  1038.            'insert-in-front-hooks '(forms--iif-hook)
  1039.            'rear-nonsticky '(face read-only insert-in-front-hooks))))))
  1040.     
  1041.    ((numberp el)
  1042.     (` ((let ((here (point)))
  1043.       (aset forms--markers 
  1044.         (, (prog1 forms--marker
  1045.              (setq forms--marker (1+ forms--marker))))
  1046.         (point-marker))
  1047.       (insert (elt arg (, (1- el))))
  1048.       (or (= (point) here)
  1049.           (set-text-properties 
  1050.            here (point)
  1051.            (list 'face forms--rw-face
  1052.              'front-sticky '(face))))))))
  1053.  
  1054.    ((listp el)
  1055.     (` ((set-text-properties
  1056.      (point)
  1057.      (progn
  1058.        (insert (aset forms--dyntexts 
  1059.              (, (prog1 forms--dyntext
  1060.                   (setq forms--dyntext (1+ forms--dyntext))))
  1061.              (, el)))
  1062.        (point))
  1063.      (list 'face forms--ro-face
  1064.            'read-only (,@ (list (1+ forms--marker)))
  1065.            'insert-in-front-hooks '(forms--iif-hook)
  1066.            'rear-nonsticky '(read-only face insert-in-front-hooks))))))
  1067.  
  1068.    ;; end of cond
  1069.    ))
  1070.  
  1071. (defun forms--make-format-elt-using-extents (el)
  1072.   "Helper routine to generate format function."
  1073.  
  1074.   ;; The format routine `forms--format' will look like
  1075.   ;;
  1076.   ;; ;; preamble
  1077.   ;;  (lambda (arg)
  1078.   ;;  
  1079.   ;;    ;; A string, e.g. "text: ".
  1080.   ;;    (let ((extent (make-extent
  1081.   ;;                   (point)
  1082.   ;;                   (progn (insert "text: ") (point)))))
  1083.   ;;      (set-extent-face extent forms--ro-face)
  1084.   ;;      (set-extent-property extent 'read-only t)
  1085.   ;;      (set-extent-property extent 'forms t)
  1086.   ;;      )
  1087.   ;;
  1088.   ;;    ;; A field, e.g. 6.
  1089.   ;;    (let ((here (point)))
  1090.   ;;      (aset forms--markers 0 (point-marker))
  1091.   ;;      (insert (elt arg 5))
  1092.   ;;      (if (= (point) here)
  1093.   ;;          nil
  1094.   ;;        (let ((extent (make-extent here (point))))
  1095.   ;;          (set-extent-face extent forms--rw-face)
  1096.   ;;          (set-extent-property extent 'forms t)
  1097.   ;;          )))
  1098.   ;;  
  1099.   ;;    ;; A function, e.g. (tocol 40).
  1100.   ;;    (let ((extent (make-extent
  1101.   ;;                   (point)
  1102.   ;;                   (progn
  1103.   ;;                     (insert (aset forms--dyntexts 0 (tocol 40)))
  1104.   ;;                     (point)))))
  1105.   ;;      (set-extent-face extent forms--ro-face)
  1106.   ;;      (set-extent-property extent 'read-only t)
  1107.   ;;      (set-extent-property extent 'forms t)
  1108.   ;;      )
  1109.   ;;    
  1110.   ;;    ;; wrap up
  1111.   ;;    (setq forms--iif-start nil)
  1112.   ;;    )
  1113.  
  1114.   (cond
  1115.    ((stringp el)
  1116.     
  1117.     (` ((let ((extent (make-extent
  1118.                (point)            ; start at point
  1119.                (progn            ; until after insertion
  1120.              (insert (, el))
  1121.              (point)))))
  1122.       (set-extent-face extent forms--ro-face)
  1123.       (set-extent-property extent 'forms t)
  1124.       (set-extent-property extent 'read-only t)
  1125.       ))))
  1126.  
  1127.    ((numberp el)
  1128.     (` ((let ((here (point)))
  1129.       (aset forms--markers 
  1130.         (, (prog1 forms--marker
  1131.              (setq forms--marker (1+ forms--marker))))
  1132.         (point-marker))
  1133.       (insert (elt arg (, (1- el))))
  1134.       (if (= (point) here)
  1135.           nil
  1136.         (let ((extent (make-extent here (point))))
  1137.           (set-extent-face extent forms--rw-face)
  1138.           (set-extent-property extent 'forms t)
  1139.           ))))))
  1140.    
  1141.    ((listp el)
  1142.     (` ((let ((extent
  1143.            (make-extent
  1144.         (point)
  1145.         (progn
  1146.           (insert (aset forms--dyntexts 
  1147.                 (, (prog1 forms--dyntext
  1148.                      (setq forms--dyntext
  1149.                        (1+ forms--dyntext))))
  1150.                 (, el)))
  1151.           (point)))))
  1152.       (set-extent-face extent forms--ro-face)
  1153.       (set-extent-property extent 'forms t)
  1154.       (set-extent-property extent 'read-only t)
  1155.       ))))
  1156.    
  1157.    ;; end of cond
  1158.    ))
  1159.  
  1160. (defun forms--make-format-elt (el)
  1161.   "Helper routine to generate format function."
  1162.  
  1163.   ;; If we're not using text properties, the format routine
  1164.   ;; `forms--format' will look like
  1165.   ;;
  1166.   ;; (lambda (arg)
  1167.   ;;   ;; a string, e.g. "text: "
  1168.   ;;   (insert "text: ")
  1169.   ;;   ;; a field, e.g. 6
  1170.   ;;   (aset forms--markers 0 (point-marker))
  1171.   ;;   (insert (elt arg 5))
  1172.   ;;   ;; another string, e.g. "\nmore text: "
  1173.   ;;   (insert "\nmore text: ")
  1174.   ;;   ;; a function, e.g. (tocol 40)
  1175.   ;;   (insert (aset forms--dyntexts 0 (tocol 40)))
  1176.   ;;   ... )
  1177.  
  1178.   (cond 
  1179.    ((stringp el)
  1180.     (` ((insert (, el)))))
  1181.    ((numberp el)
  1182.     (prog1
  1183.     (` ((aset forms--markers (, forms--marker) (point-marker))
  1184.         (insert (elt arg (, (1- el))))))
  1185.       (setq forms--marker (1+ forms--marker))))
  1186.    ((listp el)
  1187.     (prog1
  1188.     (` ((insert (aset forms--dyntexts (, forms--dyntext) (, el)))))
  1189.       (setq forms--dyntext (1+ forms--dyntext))))))
  1190.  
  1191. (defvar forms--field)
  1192. (defvar forms--recordv)
  1193. (defvar forms--seen-text)
  1194.  
  1195. (defun forms--make-parser ()
  1196.   "Generate `forms--parser' from the information in `forms-format-list'."
  1197.  
  1198.   ;; If we can use text properties, we simply set it to
  1199.   ;; `forms--parser-using-text-properties'.
  1200.   ;; Otherwise, the function is constructed using a mapcar of
  1201.   ;; `forms--make-parser-elt on `forms-format-list'.
  1202.  
  1203.   (setq
  1204.    forms--parser
  1205.    (if forms-use-text-properties
  1206.        (function forms--parser-using-text-properties)
  1207.      (let ((forms--field nil)
  1208.        (forms--seen-text nil)
  1209.        (forms--dyntext 0))
  1210.  
  1211.        ;; Note: we add a nil element to the list passed to `mapcar',
  1212.        ;; see `forms--make-parser-elt' for details.
  1213.        (` (lambda nil
  1214.         (let (here)
  1215.           (goto-char (point-min))
  1216.           (,@ (apply 'append
  1217.              (mapcar 
  1218.               'forms--make-parser-elt 
  1219.               (append forms-format-list (list nil)))))))))))
  1220.  
  1221.   (forms--debug 'forms--parser))
  1222.  
  1223. (defun forms--parser-using-text-properties ()
  1224.   "Extract field info from forms when using text properties."
  1225.  
  1226.   ;; Using text properties, we can simply jump to the markers, and
  1227.   ;; extract the information up to the following read-only segment.
  1228.  
  1229.   (let ((i 0)
  1230.     here there)
  1231.     (while (< i (length forms--markers))
  1232.       (goto-char (setq here (aref forms--markers i)))
  1233.       (if (get-text-property here 'read-only)
  1234.       (aset forms--recordv (aref forms--elements i) nil)
  1235.     (if (setq there 
  1236.           (next-single-property-change here 'read-only))
  1237.         (aset forms--recordv (aref forms--elements i)
  1238.           (buffer-substring here there))
  1239.       (aset forms--recordv (aref forms--elements i)
  1240.         (buffer-substring here (point-max)))))
  1241.       (setq i (1+ i)))))
  1242.  
  1243. (defun forms--make-parser-elt (el)
  1244.   "Helper routine to generate forms parser function."
  1245.  
  1246.   ;; The parse routine will look like:
  1247.   ;;
  1248.   ;; (lambda nil
  1249.   ;;   (let (here)
  1250.   ;;     (goto-char (point-min))
  1251.   ;; 
  1252.   ;;     ;;  "text: "
  1253.   ;;     (if (not (looking-at "text: "))
  1254.   ;;         (error "Parse error: cannot find \"text: \""))
  1255.   ;;     (forward-char 6)    ; past "text: "
  1256.   ;; 
  1257.   ;;     ;;  6
  1258.   ;;     ;;  "\nmore text: "
  1259.   ;;     (setq here (point))
  1260.   ;;     (if (not (search-forward "\nmore text: " nil t nil))
  1261.   ;;         (error "Parse error: cannot find \"\\nmore text: \""))
  1262.   ;;     (aset forms--recordv 5 (buffer-substring here (- (point) 12)))
  1263.   ;;
  1264.   ;;     ;;  (tocol 40)
  1265.   ;;    (let ((forms--dyntext (car-safe forms--dynamic-text)))
  1266.   ;;      (if (not (looking-at (regexp-quote forms--dyntext)))
  1267.   ;;          (error "Parse error: not looking at \"%s\"" forms--dyntext))
  1268.   ;;      (forward-char (length forms--dyntext))
  1269.   ;;      (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))
  1270.   ;;     ... 
  1271.   ;;     ;; final flush (due to terminator sentinel, see below)
  1272.   ;;    (aset forms--recordv 7 (buffer-substring (point) (point-max)))
  1273.  
  1274.   (cond
  1275.    ((stringp el)
  1276.     (prog1
  1277.     (if forms--field
  1278.         (` ((setq here (point))
  1279.         (if (not (search-forward (, el) nil t nil))
  1280.             (error "Parse error: cannot find \"%s\"" (, el)))
  1281.         (aset forms--recordv (, (1- forms--field))
  1282.               (buffer-substring here
  1283.                     (- (point) (, (length el)))))))
  1284.       (` ((if (not (looking-at (, (regexp-quote el))))
  1285.           (error "Parse error: not looking at \"%s\"" (, el)))
  1286.           (forward-char (, (length el))))))
  1287.       (setq forms--seen-text t)
  1288.       (setq forms--field nil)))
  1289.    ((numberp el)
  1290.     (if forms--field
  1291.     (error "Cannot parse adjacent fields %d and %d"
  1292.            forms--field el)
  1293.       (setq forms--field el)
  1294.       nil))
  1295.    ((null el)
  1296.     (if forms--field
  1297.     (` ((aset forms--recordv (, (1- forms--field))
  1298.           (buffer-substring (point) (point-max)))))))
  1299.    ((listp el)
  1300.     (prog1
  1301.     (if forms--field
  1302.         (` ((let ((here (point))
  1303.               (forms--dyntext (aref forms--dyntexts (, forms--dyntext))))
  1304.           (if (not (search-forward forms--dyntext nil t nil))
  1305.               (error "Parse error: cannot find \"%s\"" forms--dyntext))
  1306.           (aset forms--recordv (, (1- forms--field))
  1307.             (buffer-substring here
  1308.                       (- (point) (length forms--dyntext)))))))
  1309.       (` ((let ((forms--dyntext (aref forms--dyntexts (, forms--dyntext))))
  1310.         (if (not (looking-at (regexp-quote forms--dyntext)))
  1311.             (error "Parse error: not looking at \"%s\"" forms--dyntext))
  1312.         (forward-char (length forms--dyntext))))))
  1313.       (setq forms--dyntext (1+ forms--dyntext))
  1314.       (setq forms--seen-text t)
  1315.       (setq forms--field nil)))
  1316.    ))
  1317.  
  1318. (defun forms--intuit-from-file ()
  1319.   "Get number of fields and a default form using the data file."
  1320.  
  1321.   ;; If `forms-number-of-fields' is not set, get it from the data file.
  1322.   (if (null forms-number-of-fields)
  1323.  
  1324.       ;; Need a file to do this.
  1325.       (if (not (file-exists-p forms-file))
  1326.       (error "Need existing file or explicit 'forms-number-of-records'.")
  1327.  
  1328.     ;; Visit the file and extract the first record.
  1329.     (setq forms--file-buffer (find-file-noselect forms-file))
  1330.     (let ((read-file-filter forms-read-file-filter)
  1331.           (the-record))
  1332.       (setq the-record
  1333.         (save-excursion
  1334.           (set-buffer forms--file-buffer)
  1335.           (let ((inhibit-read-only t))
  1336.             (run-hooks 'read-file-filter))
  1337.           (goto-char (point-min))
  1338.           (forms--get-record)))
  1339.  
  1340.       ;; This may be overkill, but try to avoid interference with 
  1341.       ;; the normal processing.
  1342.       (kill-buffer forms--file-buffer)
  1343.  
  1344.       ;; Count the number of fields in `the-record'.
  1345.       (let (the-result
  1346.         (start-pos 0)
  1347.         found-pos
  1348.         (field-sep-length (length forms-field-sep)))
  1349.         (setq forms-number-of-fields 1)
  1350.         (while (setq found-pos
  1351.              (string-match forms-field-sep the-record start-pos))
  1352.           (progn
  1353.         (setq forms-number-of-fields (1+ forms-number-of-fields))
  1354.         (setq start-pos (+ field-sep-length found-pos))))))))
  1355.  
  1356.   ;; Construct default format list.
  1357.   (setq forms-format-list (list "Forms file \"" forms-file "\".\n\n"))
  1358.   (let ((i 0))
  1359.     (while (<= (setq i (1+ i)) forms-number-of-fields)
  1360.       (setq forms-format-list
  1361.         (append forms-format-list
  1362.             (list (format "%4d: " i) i "\n"))))))
  1363.  
  1364. (defun forms--set-keymaps ()
  1365.   "Set the keymaps used in this mode."
  1366.  
  1367.   (use-local-map (if forms-read-only 
  1368.              forms-mode-ro-map 
  1369.            forms-mode-edit-map)))
  1370.  
  1371. (defun forms--mode-commands ()
  1372.   "Fill the Forms mode keymaps."
  1373.  
  1374.   ;; `forms-mode-map' is always accessible via \C-c prefix.
  1375.   (setq forms-mode-map (make-keymap))
  1376.   (define-key forms-mode-map "\t" 'forms-next-field)
  1377.   (define-key forms-mode-map "\C-k" 'forms-delete-record)
  1378.   (define-key forms-mode-map "\C-q" 'forms-toggle-read-only)
  1379.   (define-key forms-mode-map "\C-o" 'forms-insert-record)
  1380.   (define-key forms-mode-map "\C-l" 'forms-jump-record)
  1381.   (define-key forms-mode-map "\C-n" 'forms-next-record)
  1382.   (define-key forms-mode-map "\C-p" 'forms-prev-record)
  1383.   (define-key forms-mode-map "\C-s" 'forms-search)
  1384.   (define-key forms-mode-map "\C-x" 'forms-exit)
  1385.   (define-key forms-mode-map "<" 'forms-first-record)
  1386.   (define-key forms-mode-map ">" 'forms-last-record)
  1387.   (define-key forms-mode-map "?" 'describe-mode)
  1388.   (define-key forms-mode-map "\C-?" 'forms-prev-record)
  1389.  
  1390.   ;; `forms-mode-ro-map' replaces the local map when in read-only mode.
  1391.   (setq forms-mode-ro-map (make-keymap))
  1392.   (suppress-keymap forms-mode-ro-map)
  1393.   (define-key forms-mode-ro-map "\C-c" forms-mode-map)
  1394.   (define-key forms-mode-ro-map "\t" 'forms-next-field)
  1395.   (define-key forms-mode-ro-map "q" 'forms-toggle-read-only)
  1396.   (define-key forms-mode-ro-map "l" 'forms-jump-record)
  1397.   (define-key forms-mode-ro-map "n" 'forms-next-record)
  1398.   (define-key forms-mode-ro-map "p" 'forms-prev-record)
  1399.   (define-key forms-mode-ro-map "s" 'forms-search)
  1400.   (define-key forms-mode-ro-map "x" 'forms-exit)
  1401.   (define-key forms-mode-ro-map "<" 'forms-first-record)
  1402.   (define-key forms-mode-ro-map ">" 'forms-last-record)
  1403.   (define-key forms-mode-ro-map "?" 'describe-mode)
  1404.   (define-key forms-mode-ro-map " " 'forms-next-record)
  1405.   (forms--mode-commands1 forms-mode-ro-map)
  1406.  
  1407.   ;; This is the normal, local map.
  1408.   (setq forms-mode-edit-map (make-keymap))
  1409.   (define-key forms-mode-edit-map "\t"   'forms-next-field)
  1410.   (define-key forms-mode-edit-map "\C-c" forms-mode-map)
  1411.   (forms--mode-commands1 forms-mode-edit-map)
  1412.   )
  1413.  
  1414. (defun forms--mode-commands1 (map)
  1415.   "Helper routine to define keys."
  1416.   (if forms--lemacs-p
  1417.       (progn
  1418.     (define-key map [tab] 'forms-next-field)
  1419.     (define-key map [(shift tab)] 'forms-prev-field))
  1420.     (define-key map [TAB] 'forms-next-field)
  1421.     (define-key map [S-tab] 'forms-prev-field))
  1422.   (define-key map [next] 'forms-next-record)
  1423.   (define-key map [prior] 'forms-prev-record)
  1424.   (define-key map [begin] 'forms-first-record)
  1425.   (define-key map [last] 'forms-last-record)
  1426.   (define-key map [backtab] 'forms-prev-field)
  1427.   )
  1428.  
  1429. ;;; Changed functions
  1430.  
  1431. (defun forms--change-commands ()
  1432.   "Localize some commands for Forms mode."
  1433.  
  1434.   ;; scroll-down -> forms-prev-record
  1435.   ;; scroll-up -> forms-next-record
  1436.   (if forms-forms-scroll
  1437.       (progn
  1438.     (substitute-key-definition 'scroll-up 'forms-next-record
  1439.                    (current-local-map)
  1440.                    ;;(current-global-map)
  1441.                    )
  1442.     (substitute-key-definition 'scroll-down 'forms-prev-record
  1443.                    (current-local-map)
  1444.                    ;;(current-global-map)
  1445.                    )))
  1446.   ;;
  1447.   ;; beginning-of-buffer -> forms-first-record
  1448.   ;; end-of-buffer -> forms-end-record
  1449.   (if forms-forms-jump
  1450.       (progn
  1451.     (substitute-key-definition 'beginning-of-buffer 'forms-first-record
  1452.                    (current-local-map)
  1453.                    ;;(current-global-map)
  1454.                    )
  1455.     (substitute-key-definition 'end-of-buffer 'forms-last-record
  1456.                    (current-local-map)
  1457.                    ;;(current-global-map)
  1458.                    )))
  1459.   ;;
  1460.   ;; Save buffer
  1461.   (local-set-key "\C-x\C-s" 'forms-save-buffer)
  1462.   ;;
  1463.   ;; We have our own revert function - use it.
  1464.   (make-local-variable 'revert-buffer-function)
  1465.   (setq revert-buffer-function 'forms--revert-buffer)
  1466.  
  1467.   t)
  1468.  
  1469. (defun forms--help ()
  1470.   "Initial help for Forms mode."
  1471.   (message (substitute-command-keys (concat
  1472.   "\\[forms-next-record]:next"
  1473.   "   \\[forms-prev-record]:prev"
  1474.   "   \\[forms-first-record]:first"
  1475.   "   \\[forms-last-record]:last"
  1476.   "   \\[describe-mode]:help"))))
  1477.  
  1478. (defun forms--trans (subj arg rep)
  1479.   "Translate in SUBJ all chars ARG into char REP.  ARG and REP should
  1480.  be single-char strings."
  1481.   (let ((i 0)
  1482.     (x (length subj))
  1483.     (re (regexp-quote arg))
  1484.     (k (string-to-char rep)))
  1485.     (while (setq i (string-match re subj i))
  1486.       (aset subj i k)
  1487.       (setq i (1+ i)))))
  1488.  
  1489. (defun forms--exit (query &optional save)
  1490.   "Internal exit from forms mode function."
  1491.  
  1492.   (let ((buf (buffer-name forms--file-buffer)))
  1493.     (forms--checkmod)
  1494.     (if (and save
  1495.          (buffer-modified-p forms--file-buffer))
  1496.     (forms-save-buffer))
  1497.     (save-excursion
  1498.       (set-buffer forms--file-buffer)
  1499.       (delete-auto-save-file-if-necessary)
  1500.       (kill-buffer (current-buffer)))
  1501.     (if (get-buffer buf)    ; not killed???
  1502.       (if save
  1503.       (progn
  1504.         (beep)
  1505.         (message "Problem saving buffers?")))
  1506.       (delete-auto-save-file-if-necessary)
  1507.       (kill-buffer (current-buffer)))))
  1508.  
  1509. (defun forms--get-record ()
  1510.   "Fetch the current record from the file buffer."
  1511.  
  1512.   ;; This function is executed in the context of the `forms--file-buffer'.
  1513.  
  1514.   (or (bolp)
  1515.       (beginning-of-line nil))
  1516.   (let ((here (point)))
  1517.     (prog2
  1518.      (end-of-line)
  1519.      (buffer-substring here (point))
  1520.      (goto-char here))))
  1521.  
  1522. (defun forms--show-record (the-record)
  1523.   "Format THE-RECORD and display it in the current buffer."
  1524.  
  1525.   ;; Split the-record.
  1526.   (let (the-result
  1527.     (start-pos 0)
  1528.     found-pos
  1529.     (field-sep-length (length forms-field-sep)))
  1530.     (if forms-multi-line
  1531.     (forms--trans the-record forms-multi-line "\n"))
  1532.     ;; Add an extra separator (makes splitting easy).
  1533.     (setq the-record (concat the-record forms-field-sep))
  1534.     (while (setq found-pos (string-match forms-field-sep the-record start-pos))
  1535.       (let ((ent (substring the-record start-pos found-pos)))
  1536.     (setq the-result
  1537.           (append the-result (list ent)))
  1538.     (setq start-pos (+ field-sep-length found-pos))))
  1539.     (setq forms--the-record-list the-result))
  1540.  
  1541.   (setq buffer-read-only nil)
  1542.   (if forms-use-text-properties
  1543.       (let ((inhibit-read-only t))
  1544.     (set-text-properties (point-min) (point-max) nil)))
  1545.   (erase-buffer)
  1546.  
  1547.   ;; Verify the number of fields, extend forms--the-record-list if needed.
  1548.   (if (= (length forms--the-record-list) forms-number-of-fields)
  1549.       nil
  1550.     (beep)
  1551.     (message "Warning: this record has %d fields instead of %d"
  1552.          (length forms--the-record-list) forms-number-of-fields)
  1553.     (if (< (length forms--the-record-list) forms-number-of-fields)
  1554.     (setq forms--the-record-list 
  1555.           (append forms--the-record-list
  1556.               (make-list 
  1557.                (- forms-number-of-fields 
  1558.               (length forms--the-record-list))
  1559.                "")))))
  1560.  
  1561.   ;; Call the formatter function.
  1562.   (setq forms-fields (append (list nil) forms--the-record-list nil))
  1563.   (funcall forms--format forms--the-record-list)
  1564.  
  1565.   ;; Prepare.
  1566.   (goto-char (point-min))
  1567.   (set-buffer-modified-p nil)
  1568.   (setq buffer-read-only forms-read-only)
  1569.   (setq mode-line-process
  1570.     (format " %d/%d" forms--current-record forms--total-records)))
  1571.  
  1572. (defun forms--parse-form ()
  1573.   "Parse contents of form into list of strings."
  1574.   ;; The contents of the form are parsed, and a new list of strings
  1575.   ;; is constructed.
  1576.   ;; A vector with the strings from the original record is 
  1577.   ;; constructed, which is updated with the new contents.  Therefore
  1578.   ;; fields which were not in the form are not modified.
  1579.   ;; Finally, the vector is transformed into a list for further processing.
  1580.  
  1581.   (let (forms--recordv)
  1582.  
  1583.     ;; Build the vector.
  1584.     (setq forms--recordv (vconcat forms--the-record-list))
  1585.  
  1586.     ;; Parse the form and update the vector.
  1587.     (let ((forms--dynamic-text forms--dynamic-text))
  1588.       (funcall forms--parser))
  1589.  
  1590.     (if forms-modified-record-filter
  1591.     ;; As a service to the user, we add a zeroth element so she
  1592.     ;; can use the same indices as in the forms definition.
  1593.     (let ((the-fields (vconcat [nil] forms--recordv)))
  1594.       (setq the-fields (funcall forms-modified-record-filter the-fields))
  1595.       (cdr (append the-fields nil)))
  1596.  
  1597.       ;; Transform to a list and return.
  1598.       (append forms--recordv nil))))
  1599.  
  1600. (defun forms--update ()
  1601.   "Update current record with contents of form.
  1602. As a side effect: sets `forms--the-record-list'."
  1603.  
  1604.   (if forms-read-only
  1605.       (progn
  1606.     (message "Read-only buffer!")
  1607.     (beep))
  1608.  
  1609.     (let (the-record)
  1610.       ;; Build new record.
  1611.       (setq forms--the-record-list (forms--parse-form))
  1612.       (setq the-record
  1613.         (mapconcat 'identity forms--the-record-list forms-field-sep))
  1614.  
  1615.       (if (string-match (regexp-quote forms-field-sep)
  1616.             (mapconcat 'identity forms--the-record-list ""))
  1617.       (error "Field separator occurs in record - update refused!"))
  1618.  
  1619.       ;; Handle multi-line fields, if allowed.
  1620.       (if forms-multi-line
  1621.       (forms--trans the-record "\n" forms-multi-line))
  1622.  
  1623.       ;; A final sanity check before updating.
  1624.       (if (string-match "\n" the-record)
  1625.       (progn
  1626.         (message "Multi-line fields in this record - update refused!")
  1627.         (beep))
  1628.  
  1629.     (save-excursion
  1630.       (set-buffer forms--file-buffer)
  1631.       ;; Use delete-region instead of kill-region, to avoid
  1632.       ;; adding junk to the kill-ring.
  1633.       (delete-region (save-excursion (beginning-of-line) (point))
  1634.              (save-excursion (end-of-line) (point)))
  1635.       (insert the-record)
  1636.       (beginning-of-line))))))
  1637.  
  1638. (defun forms--checkmod ()
  1639.   "Check if this form has been modified, and call forms--update if so."
  1640.   (if (buffer-modified-p nil)
  1641.       (let ((here (point)))
  1642.     (forms--update)
  1643.     (set-buffer-modified-p nil)
  1644.     (goto-char here))))
  1645.  
  1646. ;;; Start and exit
  1647.  
  1648. ;;;###autoload
  1649. (defun forms-find-file (fn)
  1650.   "Visit a file in Forms mode."
  1651.   (interactive "fForms file: ")
  1652.   (let ((enable-local-eval t)
  1653.     (enable-local-variables t))
  1654.     (find-file-read-only fn)
  1655.     (or forms--mode-setup (forms-mode t))))
  1656.  
  1657. ;;;###autoload
  1658. (defun forms-find-file-other-window (fn)
  1659.   "Visit a file in Forms mode in other window."
  1660.   (interactive "fFbrowse file in other window: ")
  1661.   (let ((enable-local-eval t)
  1662.     (enable-local-variables t))
  1663.     (find-file-other-window fn)
  1664.     (or forms--mode-setup (forms-mode t))))
  1665.  
  1666. (defun forms-exit (query)
  1667.   "Normal exit from Forms mode.  Modified buffers are saved."
  1668.   (interactive "P")
  1669.   (forms--exit query t))
  1670.  
  1671. (defun forms-exit-no-save (query)
  1672.   "Exit from Forms mode without saving buffers."
  1673.   (interactive "P")
  1674.   (forms--exit query nil))
  1675.  
  1676. ;;; Navigating commands
  1677.  
  1678. (defun forms-next-record (arg)
  1679.   "Advance to the ARGth following record."
  1680.   (interactive "P")
  1681.   (forms-jump-record (+ forms--current-record (prefix-numeric-value arg)) t))
  1682.  
  1683. (defun forms-prev-record (arg)
  1684.   "Advance to the ARGth previous record."
  1685.   (interactive "P")
  1686.   (forms-jump-record (- forms--current-record (prefix-numeric-value arg)) t))
  1687.  
  1688. (defun forms-jump-record (arg &optional relative)
  1689.   "Jump to a random record."
  1690.   (interactive "NRecord number: ")
  1691.  
  1692.   ;; Verify that the record number is within range.
  1693.   (if (or (> arg forms--total-records)
  1694.       (<= arg 0))
  1695.     (progn
  1696.       (beep)
  1697.       ;; Don't give the message if just paging.
  1698.       (if (not relative)
  1699.       (message "Record number %d out of range 1..%d"
  1700.            arg forms--total-records))
  1701.       )
  1702.  
  1703.     ;; Flush.
  1704.     (forms--checkmod)
  1705.  
  1706.     ;; Calculate displacement.
  1707.     (let ((disp (- arg forms--current-record))
  1708.       (cur forms--current-record))
  1709.  
  1710.       ;; `forms--show-record' needs it now.
  1711.       (setq forms--current-record arg)
  1712.  
  1713.       ;; Get the record and show it.
  1714.       (forms--show-record
  1715.        (save-excursion
  1716.      (set-buffer forms--file-buffer)
  1717.      (beginning-of-line)
  1718.  
  1719.      ;; Move, and adjust the amount if needed (shouldn't happen).
  1720.      (if relative
  1721.          (if (zerop disp)
  1722.          nil
  1723.            (setq cur (+ cur disp (- (forward-line disp)))))
  1724.        (setq cur (+ cur disp (- (goto-line arg)))))
  1725.  
  1726.      (forms--get-record)))
  1727.  
  1728.       ;; This shouldn't happen.
  1729.       (if (/= forms--current-record cur)
  1730.       (progn
  1731.         (setq forms--current-record cur)
  1732.         (beep)
  1733.         (message "Stuck at record %d" cur))))))
  1734.  
  1735. (defun forms-first-record ()
  1736.   "Jump to first record."
  1737.   (interactive)
  1738.   (forms-jump-record 1))
  1739.  
  1740. (defun forms-last-record ()
  1741.   "Jump to last record.
  1742. As a side effect: re-calculates the number of records in the data file."
  1743.   (interactive)
  1744.   (let
  1745.       ((numrec 
  1746.     (save-excursion
  1747.       (set-buffer forms--file-buffer)
  1748.       (count-lines (point-min) (point-max)))))
  1749.     (if (= numrec forms--total-records)
  1750.     nil
  1751.       (beep)
  1752.       (setq forms--total-records numrec)
  1753.       (message "Warning: number of records changed to %d" forms--total-records)))
  1754.   (forms-jump-record forms--total-records))
  1755.  
  1756. ;;; Other commands
  1757.  
  1758. (defun forms-toggle-read-only (arg)
  1759.   "Toggles read-only mode of a forms mode buffer.
  1760. With an argument, enables read-only mode if the argument is positive.
  1761. Otherwise enables edit mode if the visited file is writeable."
  1762.  
  1763.   (interactive "P")
  1764.  
  1765.   (if (if arg
  1766.       ;; Negative arg means switch it off.
  1767.       (<= (prefix-numeric-value arg) 0)
  1768.     ;; No arg means toggle.
  1769.     forms-read-only)
  1770.  
  1771.       ;; Enable edit mode, if possible.
  1772.       (let ((ro forms-read-only))
  1773.     (if (save-excursion
  1774.           (set-buffer forms--file-buffer)
  1775.           buffer-read-only)
  1776.         (progn
  1777.           (setq forms-read-only t)
  1778.           (message "No write access to \"%s\"" forms-file)
  1779.           (beep))
  1780.       (setq forms-read-only nil))
  1781.     (if (equal ro forms-read-only)
  1782.         nil
  1783.       (forms-mode)))
  1784.  
  1785.     ;; Enable view mode.
  1786.     (if forms-read-only
  1787.     nil
  1788.       (forms--checkmod)            ; sync
  1789.       (setq forms-read-only t)
  1790.       (forms-mode))))
  1791.  
  1792. ;; Sample:
  1793. ;; (defun my-new-record-filter (the-fields)
  1794. ;;   ;; numbers are relative to 1
  1795. ;;   (aset the-fields 4 (current-time-string))
  1796. ;;   (aset the-fields 6 (user-login-name))
  1797. ;;   the-list)
  1798. ;; (setq forms-new-record-filter 'my-new-record-filter)
  1799.  
  1800. (defun forms-insert-record (arg)
  1801.   "Create a new record before the current one.
  1802. With ARG: store the record after the current one.
  1803. If `forms-new-record-filter' contains the name of a function, 
  1804. it is called to fill (some of) the fields with default values."
  1805.  
  1806.   (interactive "P")
  1807.  
  1808.   (if forms-read-only
  1809.       (error ""))
  1810.  
  1811.   (let ((ln (if arg (1+ forms--current-record) forms--current-record))
  1812.         the-list the-record)
  1813.  
  1814.     (forms--checkmod)
  1815.     (if forms-new-record-filter
  1816.     ;; As a service to the user, we add a zeroth element so she
  1817.     ;; can use the same indices as in the forms definition.
  1818.     (let ((the-fields (make-vector (1+ forms-number-of-fields) "")))
  1819.       (setq the-fields (funcall forms-new-record-filter the-fields))
  1820.       (setq the-list (cdr (append the-fields nil))))
  1821.       (setq the-list (make-list forms-number-of-fields "")))
  1822.  
  1823.     (setq the-record
  1824.       (mapconcat
  1825.       'identity
  1826.       the-list
  1827.       forms-field-sep))
  1828.  
  1829.     (save-excursion
  1830.       (set-buffer forms--file-buffer)
  1831.       (goto-line ln)
  1832.       (open-line 1)
  1833.       (insert the-record)
  1834.       (beginning-of-line))
  1835.     
  1836.     (setq forms--current-record ln))
  1837.  
  1838.   (setq forms--total-records (1+ forms--total-records))
  1839.   (forms-jump-record forms--current-record))
  1840.  
  1841. (defun forms-delete-record (arg)
  1842.   "Deletes a record.  With a prefix argument: don't ask."
  1843.   (interactive "P")
  1844.  
  1845.   (if forms-read-only
  1846.       (error ""))
  1847.  
  1848.   (forms--checkmod)
  1849.   (if (or arg
  1850.       (y-or-n-p "Really delete this record? "))
  1851.       (let ((ln forms--current-record))
  1852.     (save-excursion
  1853.       (set-buffer forms--file-buffer)
  1854.       (goto-line ln)
  1855.       ;; Use delete-region instead of kill-region, to avoid
  1856.       ;; adding junk to the kill-ring.
  1857.       (delete-region (progn (beginning-of-line) (point))
  1858.              (progn (beginning-of-line 2) (point))))
  1859.     (setq forms--total-records (1- forms--total-records))
  1860.     (if (> forms--current-record forms--total-records)
  1861.         (setq forms--current-record forms--total-records))
  1862.     (forms-jump-record forms--current-record)))
  1863.   (message ""))
  1864.  
  1865. (defun forms-search (regexp)
  1866.   "Search REGEXP in file buffer."
  1867.   (interactive 
  1868.    (list (read-string (concat "Search for" 
  1869.                   (if forms--search-regexp
  1870.                    (concat " ("
  1871.                        forms--search-regexp
  1872.                        ")"))
  1873.                   ": "))))
  1874.   (if (equal "" regexp)
  1875.       (setq regexp forms--search-regexp))
  1876.   (forms--checkmod)
  1877.  
  1878.   (let (the-line the-record here
  1879.          (fld-sep forms-field-sep))
  1880.     (if (save-excursion
  1881.       (set-buffer forms--file-buffer)
  1882.       (setq here (point))
  1883.       (end-of-line)
  1884.       (if (null (re-search-forward regexp nil t))
  1885.           (progn
  1886.         (goto-char here)
  1887.         (message (concat "\"" regexp "\" not found."))
  1888.         nil)
  1889.         (setq the-record (forms--get-record))
  1890.         (setq the-line (1+ (count-lines (point-min) (point))))))
  1891.     (progn
  1892.       (setq forms--current-record the-line)
  1893.       (forms--show-record the-record)
  1894.       (re-search-forward regexp nil t))))
  1895.   (setq forms--search-regexp regexp))
  1896.  
  1897. (defun forms-save-buffer (&optional args)
  1898.   "Forms mode replacement for save-buffer.
  1899. It saves the data buffer instead of the forms buffer.
  1900. Calls `forms-write-file-filter' before writing out the data."
  1901.   (interactive "p")
  1902.   (forms--checkmod)
  1903.   (let ((read-file-filter forms-read-file-filter))
  1904.     (save-excursion
  1905.       (set-buffer forms--file-buffer)
  1906.       (let ((inhibit-read-only t))
  1907.     (save-buffer args)
  1908.     (if read-file-filter
  1909.         (run-hooks 'read-file-filter))
  1910.     (set-buffer-modified-p nil))))
  1911.   t)
  1912.  
  1913. (defun forms--revert-buffer (&optional arg noconfirm)
  1914.   "Reverts current form to un-modified."
  1915.   (interactive "P")
  1916.   (if (or noconfirm
  1917.       (yes-or-no-p "Revert form to unmodified? "))
  1918.       (progn
  1919.     (set-buffer-modified-p nil)
  1920.     (forms-jump-record forms--current-record))))
  1921.  
  1922. (defun forms-next-field (arg)
  1923.   "Jump to ARG-th next field."
  1924.   (interactive "p")
  1925.  
  1926.   (let ((i 0)
  1927.     (here (point))
  1928.     there
  1929.     (cnt 0))
  1930.  
  1931.     (if (zerop arg)
  1932.     (setq cnt 1)
  1933.       (setq cnt (+ cnt arg)))
  1934.  
  1935.     (if (catch 'done
  1936.       (while (< i (length forms--markers))
  1937.         (if (or (null (setq there (aref forms--markers i)))
  1938.             (<= there here))
  1939.         nil
  1940.           (if (<= (setq cnt (1- cnt)) 0)
  1941.           (progn
  1942.             (goto-char there)
  1943.             (throw 'done t))))
  1944.         (setq i (1+ i))))
  1945.     nil
  1946.       (goto-char (aref forms--markers 0)))))
  1947.  
  1948. (defun forms-prev-field (arg)
  1949.   "Jump to ARG-th previous field."
  1950.   (interactive "p")
  1951.  
  1952.   (let ((i (length forms--markers))
  1953.     (here (point))
  1954.     there
  1955.     (cnt 0))
  1956.  
  1957.     (if (zerop arg)
  1958.     (setq cnt 1)
  1959.       (setq cnt (+ cnt arg)))
  1960.  
  1961.     (if (catch 'done
  1962.       (while (> i 0)
  1963.         (setq i ( 1- i))
  1964.         (if (or (null (setq there (aref forms--markers i)))
  1965.             (>= there here))
  1966.         nil
  1967.           (if (<= (setq cnt (1- cnt)) 0)
  1968.           (progn
  1969.             (goto-char there)
  1970.             (throw 'done t))))))
  1971.     nil
  1972.       (goto-char (aref forms--markers (1- (length forms--markers)))))))
  1973. ;;;
  1974. ;;; Special service
  1975. ;;;
  1976. (defun forms-enumerate (the-fields)
  1977.   "Take a quoted list of symbols, and set their values to sequential numbers.
  1978. The first symbol gets number 1, the second 2 and so on.
  1979. It returns the higest number.
  1980.  
  1981. Usage: (setq forms-number-of-fields
  1982.              (forms-enumerate
  1983.               '(field1 field2 field2 ...)))"
  1984.  
  1985.   (let ((the-index 0))
  1986.     (while the-fields
  1987.       (setq the-index (1+ the-index))
  1988.       (let ((el (car-safe the-fields)))
  1989.     (setq the-fields (cdr-safe the-fields))
  1990.     (set el the-index)))
  1991.     the-index))
  1992.  
  1993. ;;; Debugging
  1994.  
  1995. (defvar forms--debug nil
  1996.   "*Enables forms-mode debugging if not nil.")
  1997.  
  1998. (defun forms--debug (&rest args)
  1999.   "Internal debugging routine."
  2000.   (if forms--debug
  2001.       (let ((ret nil))
  2002.     (while args
  2003.       (let ((el (car-safe args)))
  2004.         (setq args (cdr-safe args))
  2005.         (if (stringp el)
  2006.         (setq ret (concat ret el))
  2007.           (setq ret (concat ret (prin1-to-string el) " = "))
  2008.           (if (boundp el)
  2009.           (let ((vel (eval el)))
  2010.             (setq ret (concat ret (prin1-to-string vel) "\n")))
  2011.         (setq ret (concat ret "<unbound>" "\n")))
  2012.           (if (fboundp el)
  2013.           (setq ret (concat ret (prin1-to-string (symbol-function el)) 
  2014.                     "\n"))))))
  2015.     (save-excursion
  2016.       (set-buffer (get-buffer-create "*forms-mode debug*"))
  2017.       (if (zerop (buffer-size))
  2018.           (emacs-lisp-mode))
  2019.       (goto-char (point-max))
  2020.       (insert ret)))))
  2021.  
  2022. ;;; forms.el ends here.
  2023.