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