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