1 ;;; forms.el --- Forms mode: edit a file as a form to fill in
3 ;; Copyright (C) 1991, 1994-1997, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Johan Vromans <jvromans@squirrel.nl>
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Visit a file using a form. See forms-d2.el for examples.
26 ;; === Naming conventions
28 ;; The names of all variables and functions start with 'forms-'.
29 ;; Names which start with 'forms--' are intended for internal use, and
30 ;; should *NOT* be used from the outside.
32 ;; All variables are buffer-local, to enable multiple forms visits
34 ;; Variable `forms--mode-setup' is local to *ALL* buffers, for it
35 ;; controls if forms-mode has been enabled in a buffer.
37 ;; === How it works ===
39 ;; Forms mode means visiting a data file which is supposed to consist
40 ;; of records each containing a number of fields. The records are
41 ;; separated by a newline, the fields are separated by a user-defined
42 ;; field separator (default: TAB).
43 ;; When shown, a record is transferred to an Emacs buffer and
44 ;; presented using a user-defined form. One record is shown at a
47 ;; Forms mode is a composite mode. It involves two files, and two
49 ;; The first file, called the control file, defines the name of the
50 ;; data file and the forms format. This file buffer will be used to
52 ;; The second file holds the actual data. The buffer of this file
53 ;; will be buried, for it is never accessed directly.
55 ;; Forms mode is invoked using M-x `forms-find-file' control-file.
56 ;; Alternatively `forms-find-file-other-window' can be used.
58 ;; You may also visit the control file, and switch to forms mode by hand
59 ;; with M-x `forms-mode'.
61 ;; Automatic mode switching is supported if you specify
62 ;; "-*- forms -*-" in the first line of the control file.
64 ;; The control file is visited, evaluated using `eval-buffer',
65 ;; and should set at least the following variables:
67 ;; forms-file [string]
68 ;; The name of the data file.
70 ;; forms-number-of-fields [integer]
71 ;; The number of fields in each record.
73 ;; forms-format-list [list]
74 ;; Formatting instructions.
76 ;; `forms-format-list' should be a list, each element containing
78 ;; - a string, e.g. "hello". The string is inserted in the forms
81 ;; - an integer, denoting a field number.
82 ;; The contents of this field are inserted at this point.
83 ;; Fields are numbered starting with number one.
85 ;; - a function call, e.g. (insert "text").
86 ;; This function call is dynamically evaluated and should return a
87 ;; string. It should *NOT* have side-effects on the forms being
88 ;; constructed. The current fields are available to the function
89 ;; in the variable `forms-fields', they should *NOT* be modified.
91 ;; - a lisp symbol, that must evaluate to one of the above.
93 ;; Optional variables which may be set in the control file:
95 ;; forms-field-sep [string, default TAB]
96 ;; The field separator used to separate the
97 ;; fields in the data file. It may be a string.
99 ;; forms-read-only [bool, default nil]
100 ;; Non-nil means that the data file is visited
101 ;; read-only (view mode) as opposed to edit mode.
102 ;; If no write access to the data file is
103 ;; possible, view mode is enforced.
105 ;; forms-check-number-of-fields [bool, default t]
106 ;; If non-nil, a warning will be issued whenever
107 ;; a record is found that does not have the number
108 ;; of fields specified by `forms-number-of-fields'.
110 ;; forms-multi-line [string, default "^K"]
111 ;; If non-null, the records of the data file may
112 ;; contain fields that can span multiple lines in
114 ;; This variable denotes the separator string
115 ;; to be used for this purpose. Upon display, all
116 ;; occurrences of this string are translated
117 ;; to newlines. Upon storage they are translated
118 ;; back to the separator string.
120 ;; forms-forms-scroll [bool, default nil]
121 ;; Non-nil means: rebind locally the commands that
122 ;; perform `scroll-up' or `scroll-down' to use
123 ;; `forms-next-field' resp. `forms-prev-field'.
125 ;; forms-forms-jump [bool, default nil]
126 ;; Non-nil means: rebind locally the commands
127 ;; `beginning-of-buffer' and `end-of-buffer' to
128 ;; perform, respectively, `forms-first-record' and
129 ;; `forms-last-record' instead.
131 ;; forms-insert-after [bool, default nil]
132 ;; Non-nil means: insertions of new records go after
133 ;; current record, also initial position is at the
134 ;; last record. The default is to insert before the
135 ;; current record and the initial position is at the
138 ;; forms-read-file-filter [symbol, default nil]
139 ;; If not nil: this should be the name of a
140 ;; function that is called after the forms data file
141 ;; has been read. It can be used to transform
142 ;; the contents of the file into a format more suitable
143 ;; for forms-mode processing.
145 ;; forms-write-file-filter [symbol, default nil]
146 ;; If not nil: this should be the name of a
147 ;; function that is called before the forms data file
148 ;; is written (saved) to disk. It can be used to undo
149 ;; the effects of `forms-read-file-filter', if any.
151 ;; forms-new-record-filter [symbol, default nil]
152 ;; If not nil: this should be the name of a
153 ;; function that is called when a new
154 ;; record is created. It can be used to fill in
155 ;; the new record with default fields, for example.
157 ;; forms-modified-record-filter [symbol, default nil]
158 ;; If not nil: this should be the name of a
159 ;; function that is called when a record has
160 ;; been modified. It is called after the fields
161 ;; are parsed. It can be used to register
162 ;; modification dates, for example.
164 ;; forms-use-text-properties [bool, see text for default]
165 ;; This variable controls if forms mode should use
166 ;; text properties to protect the form text from being
167 ;; modified (using text-property `read-only').
168 ;; Also, the read-write fields are shown using a
169 ;; distinct face, if possible.
170 ;; As of emacs 19.29, the `intangible' text property
171 ;; is used to prevent moving into read-only fields.
172 ;; This variable defaults to t if running Emacs 19 or
173 ;; later with text properties.
174 ;; The default face to show read-write fields is
175 ;; copied from face `region'.
177 ;; forms-ro-face [symbol, default 'default]
178 ;; This is the face that is used to show
179 ;; read-only text on the screen. If used, this
180 ;; variable should be set to a symbol that is a
183 ;; (make-face 'my-face)
184 ;; (setq forms-ro-face 'my-face)
186 ;; forms-rw-face [symbol, default 'region]
187 ;; This is the face that is used to show
188 ;; read-write text on the screen.
190 ;; After evaluating the control file, its buffer is cleared and used
191 ;; for further processing.
192 ;; The data file (as designated by `forms-file') is visited in a buffer
193 ;; `forms--file-buffer' which normally will not be shown.
194 ;; Great malfunctioning may be expected if this file/buffer is modified
195 ;; outside of this package while it is being visited!
197 ;; Normal operation is to transfer one line (record) from the data file,
198 ;; split it into fields (into `forms--the-record-list'), and display it
199 ;; using the specs in `forms-format-list'.
200 ;; A format routine `forms--format' is built upon startup to format
201 ;; the records according to `forms-format-list'.
203 ;; When a form is changed the record is updated as soon as this form
204 ;; is left. The contents of the form are parsed using information
205 ;; obtained from `forms-format-list', and the fields which are
206 ;; deduced from the form are modified. Fields not shown on the forms
207 ;; retain their original values. The newly formed record then
208 ;; replaces the contents of the old record in `forms--file-buffer'.
209 ;; A parse routine `forms--parser' is built upon startup to parse
212 ;; Two exit functions exist: `forms-exit' and `forms-exit-no-save'.
213 ;; `forms-exit' saves the data to the file, if modified.
214 ;; `forms-exit-no-save' does not. However, if `forms-exit-no-save'
215 ;; is executed and the file buffer has been modified, Emacs will ask
218 ;; Other functions provided by forms mode are:
220 ;; paging (forward, backward) by record
221 ;; jumping (first, last, random number)
223 ;; creating and deleting records
224 ;; reverting the form (NOT the file buffer)
225 ;; switching edit <-> view mode v.v.
226 ;; jumping from field to field
228 ;; As a documented side-effect: jumping to the last record in the
229 ;; file (using forms-last-record) will adjust forms--total-records if
232 ;; The forms buffer can be in one of two modes: edit mode or view
233 ;; mode. View mode is a read-only mode, whereby you cannot modify the
234 ;; contents of the buffer.
236 ;; Edit mode commands:
238 ;; TAB forms-next-field
239 ;; \C-c TAB forms-next-field
240 ;; \C-c < forms-first-record
241 ;; \C-c > forms-last-record
242 ;; \C-c ? describe-mode
243 ;; \C-c \C-k forms-delete-record
244 ;; \C-c \C-q forms-toggle-read-only
245 ;; \C-c \C-o forms-insert-record
246 ;; \C-c \C-l forms-jump-record
247 ;; \C-c \C-n forms-next-record
248 ;; \C-c \C-p forms-prev-record
249 ;; \C-c \C-r forms-search-backward
250 ;; \C-c \C-s forms-search-forward
251 ;; \C-c \C-x forms-exit
253 ;; Read-only mode commands:
255 ;; SPC forms-next-record
256 ;; DEL forms-prev-record
258 ;; \C-q forms-toggle-read-only
259 ;; l forms-jump-record
260 ;; n forms-next-record
261 ;; p forms-prev-record
262 ;; r forms-search-backward
263 ;; s forms-search-forward
266 ;; Of course, it is also possible to use the \C-c prefix to obtain the
267 ;; same command keys as in edit mode.
269 ;; The following bindings are available, independent of the mode:
271 ;; [next] forms-next-record
272 ;; [prior] forms-prev-record
273 ;; [begin] forms-first-record
274 ;; [end] forms-last-record
275 ;; [S-TAB] forms-prev-field
276 ;; [backtab] forms-prev-field
278 ;; For convenience, TAB is always bound to `forms-next-field', so you
279 ;; don't need the C-c prefix for this command.
281 ;; As mentioned above (see `forms-forms-scroll' and `forms-forms-jump'),
282 ;; the bindings of standard functions `scroll-up', `scroll-down',
283 ;; `beginning-of-buffer' and `end-of-buffer' can be locally replaced with
284 ;; forms mode functions next/prev record and first/last
287 ;; `write-file-functions' is defined to save the actual data file
288 ;; instead of the buffer data, `revert-file-hook' is defined to
289 ;; revert a forms to original.
294 "Edit a file as a form to fill in."
297 ;;; Global variables and constants:
299 (provide 'forms
) ;;; official
300 (provide 'forms-mode
) ;;; for compatibility
302 (defcustom forms-mode-hook nil
303 "Hook run upon entering Forms mode."
307 ;;; Mandatory variables - must be set by evaluating the control file.
309 (defvar forms-file nil
310 "Name of the file holding the data.")
312 (defvar forms-format-list nil
313 "List of formatting specifications.")
315 (defvar forms-number-of-fields nil
316 "Number of fields per record.")
318 ;;; Optional variables with default values.
320 (defcustom forms-check-number-of-fields t
321 "If non-nil, warn about records with wrong number of fields."
325 (defvar forms-field-sep
"\t"
326 "Field separator character (default TAB).")
328 (defvar forms-read-only nil
329 "Non-nil means: visit the file in view (read-only) mode.
330 This is set automatically if the file permissions don't let you write it.")
332 (defvar forms-multi-line
"\C-k" "\
333 If not nil: use this character to separate multi-line fields (default C-k).")
335 (defcustom forms-forms-scroll nil
336 "Non-nil means replace scroll-up/down commands in Forms mode.
337 The replacement commands performs forms-next/prev-record."
341 (defcustom forms-forms-jump nil
342 "Non-nil means redefine beginning/end-of-buffer in Forms mode.
343 The replacement commands performs forms-first/last-record."
347 (defvar forms-read-file-filter nil
348 "The name of a function that is called after reading the data file.
349 This can be used to change the contents of the file to something more
350 suitable for forms processing.")
352 (defvar forms-write-file-filter nil
353 "The name of a function that is called before writing the data file.
354 This can be used to undo the effects of `form-read-file-hook'.")
356 (defvar forms-new-record-filter nil
357 "The name of a function that is called when a new record is created.")
359 (defvar forms-modified-record-filter nil
360 "The name of a function that is called when a record has been modified.")
362 (defvar forms-fields nil
363 "List with fields of the current forms. First field has number 1.
364 This variable is for use by the filter routines only.
365 The contents may NOT be modified.")
367 (defcustom forms-use-text-properties t
368 "Non-nil means: use text properties.
369 Defaults to t if this Emacs is capable of handling text properties."
373 (defcustom forms-insert-after nil
374 "Non-nil means: inserts of new records go after current record.
375 Also, initial position is at last record."
379 (defcustom forms-ro-face
'default
380 "The face (a symbol) that is used to display read-only text on the screen."
384 (defcustom forms-rw-face
'region
385 "The face (a symbol) that is used to display read-write text on the screen."
389 ;;; Internal variables.
391 (defvar forms--file-buffer nil
392 "Buffer which holds the file data")
394 (defvar forms--total-records
0
395 "Total number of records in the data file.")
397 (defvar forms--current-record
0
398 "Number of the record currently on the screen.")
400 (defvar forms-mode-map nil
401 "Keymap for form buffer.")
402 (defvar forms-mode-ro-map nil
403 "Keymap for form buffer in view mode.")
404 (defvar forms-mode-edit-map nil
405 "Keymap for form buffer in edit mode.")
407 (defvar forms--markers nil
408 "Field markers in the screen.")
410 (defvar forms--dyntexts nil
411 "Dynamic texts (resulting from function calls) on the screen.")
413 (defvar forms--the-record-list nil
414 "List of strings of the current record, as parsed from the file.")
416 (defvar forms--search-regexp nil
417 "Last regexp used by forms-search functions.")
419 (defvar forms--format nil
420 "Formatting routine.")
422 (defvar forms--parser nil
423 "Forms parser routine.")
425 (defvar forms--mode-setup nil
426 "To keep track of forms-mode being set-up.")
427 (make-variable-buffer-local 'forms--mode-setup
)
429 (defvar forms--dynamic-text nil
430 "Array that holds dynamic texts to insert between fields.")
432 (defvar forms--elements nil
433 "Array with the order in which the fields are displayed.")
435 (defvar forms--ro-face nil
436 "Face used to represent read-only data on the screen.")
438 (defvar forms--rw-face nil
439 "Face used to represent read-write data on the screen.")
442 (defun forms-mode (&optional primary
)
443 "Major mode to visit files in a field-structured manner using a form.
445 Commands: Equivalent keys in read-only mode:
446 TAB forms-next-field TAB
447 C-c TAB forms-next-field
448 C-c < forms-first-record <
449 C-c > forms-last-record >
450 C-c ? describe-mode ?
451 C-c C-k forms-delete-record
452 C-c C-q forms-toggle-read-only q
453 C-c C-o forms-insert-record
454 C-c C-l forms-jump-record l
455 C-c C-n forms-next-record n
456 C-c C-p forms-prev-record p
457 C-c C-r forms-search-reverse r
458 C-c C-s forms-search-forward s
463 ;; This is not a simple major mode, as usual. Therefore, forms-mode
464 ;; takes an optional argument `primary' which is used for the
465 ;; initial set-up. Normal use would leave `primary' to nil.
466 ;; A global buffer-local variable `forms--mode-setup' has the same
467 ;; effect but makes it possible to auto-invoke forms-mode using
469 ;; Note: although it seems logical to have `make-local-variable'
470 ;; executed where the variable is first needed, I have deliberately
471 ;; placed all calls in this function.
473 ;; Primary set-up: evaluate buffer and check if the mandatory
474 ;; variables have been set.
475 (if (or primary
(not forms--mode-setup
))
477 ;;(message "forms: setting up...")
478 (kill-all-local-variables)
480 ;; Make mandatory variables.
481 (make-local-variable 'forms-file
)
482 (make-local-variable 'forms-number-of-fields
)
483 (make-local-variable 'forms-format-list
)
485 ;; Make optional variables.
486 (make-local-variable 'forms-field-sep
)
487 (make-local-variable 'forms-read-only
)
488 (make-local-variable 'forms-multi-line
)
489 (make-local-variable 'forms-forms-scroll
)
490 (make-local-variable 'forms-forms-jump
)
491 (make-local-variable 'forms-insert-after
)
492 (make-local-variable 'forms-use-text-properties
)
495 (make-local-variable 'forms-read-file-filter
)
496 (make-local-variable 'forms-write-file-filter
)
497 (make-local-variable 'forms-new-record-filter
)
498 (make-local-variable 'forms-modified-record-filter
)
500 ;; Make sure no filters exist.
501 (setq forms-read-file-filter nil
)
502 (setq forms-write-file-filter nil
)
503 (setq forms-new-record-filter nil
)
504 (setq forms-modified-record-filter nil
)
506 ;; If running Emacs 19 under X, setup faces to show read-only and
507 ;; read-write fields.
508 (if (fboundp 'make-face
)
510 (make-local-variable 'forms-ro-face
)
511 (make-local-variable 'forms-rw-face
)))
513 ;; eval the buffer, should set variables
514 ;;(message "forms: processing control file...")
515 ;; If enable-local-eval is not set to t the user is asked first.
516 (if (or (eq enable-local-eval t
)
518 (concat "Evaluate lisp code in buffer "
519 (buffer-name) " to display forms? ")))
521 (error "`enable-local-eval' inhibits buffer evaluation"))
523 ;; Check if the mandatory variables make sense.
525 (error (concat "Forms control file error: "
526 "`forms-file' has not been set")))
528 ;; Check forms-field-sep first, since it can be needed to
529 ;; construct a default format list.
530 (or (stringp forms-field-sep
)
531 (error (concat "Forms control file error: "
532 "`forms-field-sep' is not a string")))
534 (if forms-number-of-fields
535 (or (and (numberp forms-number-of-fields
)
536 (> forms-number-of-fields
0))
537 (error (concat "Forms control file error: "
538 "`forms-number-of-fields' must be a number > 0")))
539 (or (null forms-format-list
)
540 (error (concat "Forms control file error: "
541 "`forms-number-of-fields' has not been set"))))
543 (or forms-format-list
544 (forms--intuit-from-file))
547 (if (and (stringp forms-multi-line
)
548 (eq (length forms-multi-line
) 1))
549 (if (string= forms-multi-line forms-field-sep
)
550 (error (concat "Forms control file error: "
551 "`forms-multi-line' is equal to `forms-field-sep'")))
552 (error (concat "Forms control file error: "
553 "`forms-multi-line' must be nil or a one-character string"))))
554 (or (fboundp 'set-text-properties
)
555 (setq forms-use-text-properties nil
))
557 ;; Validate and process forms-format-list.
558 ;;(message "forms: pre-processing format list...")
559 (make-local-variable 'forms--elements
)
560 (forms--process-format-list)
562 ;; Build the formatter and parser.
563 ;;(message "forms: building formatter...")
564 (make-local-variable 'forms--format
)
565 (make-local-variable 'forms--markers
)
566 (make-local-variable 'forms--dyntexts
)
567 ;;(message "forms: building parser...")
569 (make-local-variable 'forms--parser
)
571 ;;(message "forms: building parser... done.")
573 ;; Check if record filters are defined.
574 (if (and forms-new-record-filter
575 (not (fboundp forms-new-record-filter
)))
576 (error (concat "Forms control file error: "
577 "`forms-new-record-filter' is not a function")))
579 (if (and forms-modified-record-filter
580 (not (fboundp forms-modified-record-filter
)))
581 (error (concat "Forms control file error: "
582 "`forms-modified-record-filter' is not a function")))
584 ;; The filters acces the contents of the forms using `forms-fields'.
585 (make-local-variable 'forms-fields
)
587 ;; Dynamic text support.
588 (make-local-variable 'forms--dynamic-text
)
590 ;; Prevent accidental overwrite of the control file and auto-save.
591 (set-visited-file-name nil
)
593 ;; Prepare this buffer for further processing.
594 (setq buffer-read-only nil
)
597 ;;(message "forms: setting up... done.")
600 ;; initialization done
601 (setq forms--mode-setup t
)
603 ;; Copy desired faces to the actual variables used by the forms formatter.
604 (if (fboundp 'make-face
)
606 (make-local-variable 'forms--ro-face
)
607 (make-local-variable 'forms--rw-face
)
610 (setq forms--ro-face forms-ro-face
)
611 (setq forms--rw-face forms-ro-face
))
612 (setq forms--ro-face forms-ro-face
)
613 (setq forms--rw-face forms-rw-face
))))
615 ;; Make more local variables.
616 (make-local-variable 'forms--file-buffer
)
617 (make-local-variable 'forms--total-records
)
618 (make-local-variable 'forms--current-record
)
619 (make-local-variable 'forms--the-record-list
)
620 (make-local-variable 'forms--search-regexp
)
622 ; The keymaps are global, so multiple forms mode buffers can share them.
623 ;(make-local-variable 'forms-mode-map)
624 ;(make-local-variable 'forms-mode-ro-map)
625 ;(make-local-variable 'forms-mode-edit-map)
626 (if forms-mode-map
; already defined
628 ;;(message "forms: building keymap...")
629 (forms--mode-commands)
630 ;;(message "forms: building keymap... done.")
633 ;; set the major mode indicator
634 (setq major-mode
'forms-mode
)
635 (setq mode-name
"Forms")
637 ;; find the data file
638 (setq forms--file-buffer
(find-file-noselect forms-file
))
641 (let ((read-file-filter forms-read-file-filter
)
642 (write-file-filter forms-write-file-filter
))
644 (with-current-buffer forms--file-buffer
645 (let ((inhibit-read-only t
)
646 (file-modified (buffer-modified-p)))
647 (run-hooks 'read-file-filter
)
648 (if (not file-modified
) (set-buffer-modified-p nil
)))
649 (if write-file-filter
650 (add-hook 'write-file-functions write-file-filter nil t
)))
651 (if write-file-filter
652 (with-current-buffer forms--file-buffer
653 (add-hook 'write-file-functions write-file-filter nil t
)))))
655 ;; count the number of records, and set see if it may be modified
657 (setq forms--total-records
658 (with-current-buffer forms--file-buffer
661 ;;(message "forms: counting records...")
662 (bury-buffer (current-buffer))
663 (setq ro buffer-read-only
)
664 (count-lines (point-min) (point-max)))
665 ;;(message "forms: counting records... done.")
668 (setq forms-read-only t
)))
670 ;;(message "forms: proceeding setup...")
672 ;; Since we aren't really implementing a minor mode, we hack the modeline
673 ;; directly to get the text " View " into forms-read-only form buffers. For
674 ;; that reason, this variable must be buffer only.
675 (make-local-variable 'minor-mode-alist
)
676 (setq minor-mode-alist
(list (list 'forms-read-only
" View")))
678 ;;(message "forms: proceeding setup (keymaps)...")
680 ;;(message "forms: proceeding setup (commands)...")
681 (forms--change-commands)
683 ;;(message "forms: proceeding setup (buffer)...")
684 (set-buffer-modified-p nil
)
686 (if (= forms--total-records
0)
687 ;;(message "forms: proceeding setup (new file)...")
690 "GNU Emacs Forms Mode\n\n"
691 (if (file-exists-p forms-file
)
692 (concat "No records available in file `" forms-file
"'\n\n")
693 (format "Creating new file `%s'\nwith %d field%s per record\n\n"
694 forms-file forms-number-of-fields
695 (if (= 1 forms-number-of-fields
) "" "s")))
696 "Use " (substitute-command-keys "\\[forms-insert-record]")
697 " to create new records.\n")
698 (setq forms--current-record
1)
699 (setq buffer-read-only t
)
700 (set-buffer-modified-p nil
))
702 ;; setup the first (or current) record to show
703 (if (< forms--current-record
1)
704 (setq forms--current-record
1))
705 (forms-jump-record forms--current-record
)
707 (if forms-insert-after
709 (forms-first-record))
713 ;;(message "forms: proceeding setup (user hooks)...")
714 (run-mode-hooks 'forms-mode-hook
'forms-mode-hooks
)
715 ;;(message "forms: setting up... done.")
721 (defun forms--process-format-list ()
722 ;; Validate `forms-format-list' and set some global variables.
723 ;; Symbols in the list are evaluated, and consecutive strings are
725 ;; Array `forms--elements' is constructed that contains the order
726 ;; of the fields on the display. This array is used by
727 ;; `forms--parser-using-text-properties' to extract the fields data
728 ;; from the form on the screen.
729 ;; Upon completion, `forms-format-list' is guaranteed correct, so
730 ;; `forms--make-format' and `forms--make-parser' do not need to perform
733 ;; Verify that `forms-format-list' is not nil.
734 (or forms-format-list
735 (error (concat "Forms control file error: "
736 "`forms-format-list' has not been set")))
737 ;; It must be a list.
738 (or (listp forms-format-list
)
739 (error (concat "Forms control file error: "
740 "`forms-format-list' is not a list")))
742 ;; Assume every field is painted once.
743 ;; `forms--elements' will grow if needed.
744 (setq forms--elements
(make-vector forms-number-of-fields nil
))
746 (let ((the-list forms-format-list
) ; the list of format elements
748 (field-num 0)) ; highest field number
750 (setq forms-format-list nil
) ; gonna rebuild
754 (let ((el (car-safe the-list
))
755 (rem (cdr-safe the-list
)))
757 ;; If it is a symbol, eval it first.
758 (if (and (symbolp el
)
766 (if (stringp prev-item
) ; try to concatenate strings
767 (setq prev-item
(concat prev-item el
))
769 (setq forms-format-list
770 (append forms-format-list
(list prev-item
) nil
)))
771 (setq prev-item el
)))
778 (> el forms-number-of-fields
))
779 (error (concat "Forms format error: "
780 "field number %d out of range 1..%d")
781 el forms-number-of-fields
))
783 ;; Store forms order.
784 (if (>= field-num
(length forms--elements
))
785 (setq forms--elements
(vconcat forms--elements
(1- el
)))
786 (aset forms--elements field-num
(1- el
)))
787 (setq field-num
(1+ field-num
))
790 (setq forms-format-list
791 (append forms-format-list
(list prev-item
) nil
)))
798 (or (fboundp (car-safe el
))
799 (error (concat "Forms format error: "
800 "%S is not a function")
805 (setq forms-format-list
806 (append forms-format-list
(list prev-item
) nil
)))
811 (error (concat "Forms format error: "
812 "invalid element %S")
815 ;; Advance to next element of the list.
816 (setq the-list rem
)))
821 (setq forms-format-list
822 (append forms-format-list
(list prev-item
) nil
))
823 ;; Append a newline if the last item is a field.
824 ;; This prevents parsing problems.
825 ;; Also it makes it possible to insert an empty last field.
826 (if (numberp prev-item
)
827 (setq forms-format-list
828 (append forms-format-list
(list "\n") nil
))))))
830 (forms--debug 'forms-format-list
833 ;; Special treatment for read-only segments.
835 ;; If text is inserted between two read-only segments, there seems to
836 ;; be no way to give the newly inserted text the RW face.
837 ;; To solve this, read-only segments get the `insert-in-front-hooks'
838 ;; property set with a function that temporarily switches the
839 ;; properties of the first character of the segment to the RW face, so
840 ;; the new text gets the right face. The `post-command-hook' is
841 ;; used to restore the original properties.
843 (defvar forms--iif-start nil
844 "Record start of modification command.")
845 (defvar forms--iif-properties nil
846 "Original properties of the character being overridden.")
848 (defun forms--iif-hook (begin end
)
849 "`insert-in-front-hooks' function for read-only segments."
851 ;; Note start location. By making it a marker that points one
852 ;; character beyond the actual location, it is guaranteed to move
853 ;; correctly if text is inserted.
855 (setq forms--iif-start
(copy-marker (1+ (point)))))
857 ;; Check if there is special treatment required.
858 (if (or (<= forms--iif-start
2)
859 (get-text-property (- forms--iif-start
2)
862 ;; Fetch current properties.
863 (setq forms--iif-properties
864 (text-properties-at (1- forms--iif-start
)))
867 (let ((inhibit-read-only t
))
869 (1- forms--iif-start
) forms--iif-start
870 (list 'face forms--rw-face
'front-sticky
'(face))))
872 ;; Enable `post-command-hook' to restore the properties.
873 (setq post-command-hook
874 (append (list 'forms--iif-post-command-hook
) post-command-hook
)))
876 ;; No action needed. Clear marker.
877 (setq forms--iif-start nil
)))
879 (defun forms--iif-post-command-hook ()
880 "`post-command-hook' function for read-only segments."
882 ;; Disable `post-command-hook'.
883 (setq post-command-hook
884 (delq 'forms--iif-hook-post-command-hook post-command-hook
))
886 ;; Restore properties.
888 (let ((inhibit-read-only t
))
890 (1- forms--iif-start
) forms--iif-start
891 forms--iif-properties
)))
894 (setq forms--iif-start nil
))
896 (defvar forms--marker
)
897 (defvar forms--dyntext
)
899 (defun forms--make-format ()
900 "Generate `forms--format' using the information in `forms-format-list'."
902 ;; The real work is done using a mapcar of `forms--make-format-elt' on
903 ;; `forms-format-list'.
904 ;; This function sets up the necessary environment, and decides
905 ;; which function to mapcar.
907 (let ((forms--marker 0)
911 (if forms-use-text-properties
913 (let ((inhibit-read-only t
))
915 (mapcar 'forms--make-format-elt-using-text-properties
917 ;; Prevent insertion before the first text.
918 ,@(if (numberp (car forms-format-list
))
920 '((add-text-properties (point-min) (1+ (point-min))
921 '(front-sticky (read-only intangible
)))))
922 ;; Prevent insertion after the last text.
923 (remove-text-properties (1- (point)) (point)
925 (setq forms--iif-start nil
))
928 (mapcar 'forms--make-format-elt forms-format-list
)))))
930 ;; We have tallied the number of markers and dynamic texts,
931 ;; so we can allocate the arrays now.
932 (setq forms--markers
(make-vector forms--marker nil
))
933 (setq forms--dyntexts
(make-vector forms--dyntext nil
)))
934 (forms--debug 'forms--format
))
936 (defun forms--make-format-elt-using-text-properties (el)
937 "Helper routine to generate format function."
939 ;; The format routine `forms--format' will look like
943 ;; (let ((inhibit-read-only t))
945 ;; ;; A string, e.g. "text: ".
946 ;; (set-text-properties
948 ;; (progn (insert "text: ") (point))
949 ;; (list 'face forms--ro-face
951 ;; 'insert-in-front-hooks 'forms--iif-hook
952 ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
954 ;; ;; A field, e.g. 6.
955 ;; (let ((here (point)))
956 ;; (aset forms--markers 0 (point-marker))
957 ;; (insert (elt arg 5))
958 ;; (or (= (point) here)
959 ;; (set-text-properties
961 ;; (list 'face forms--rw-face
962 ;; 'front-sticky '(face))))
964 ;; ;; Another string, e.g. "\nmore text: ".
965 ;; (set-text-properties
967 ;; (progn (insert "\nmore text: ") (point))
968 ;; (list 'face forms--ro-face
970 ;; 'insert-in-front-hooks 'forms--iif-hook
971 ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
973 ;; ;; A function, e.g. (tocol 40).
974 ;; (set-text-properties
977 ;; (insert (aset forms--dyntexts 0 (tocol 40)))
979 ;; (list 'face forms--ro-face
981 ;; 'insert-in-front-hooks 'forms--iif-hook
982 ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
984 ;; ;; Prevent insertion before the first text.
985 ;; (add-text-properties (point-min) (1+ (point-min))
986 ;; '(front-sticky (read-only))))))
987 ;; ;; Prevent insertion after the last text.
988 ;; (remove-text-properties (1- (point)) (point)
989 ;; '(rear-nonsticky)))
992 ;; (setq forms--iif-start nil)
998 `((set-text-properties
999 (point) ; start at point
1000 (progn ; until after insertion
1003 (list 'face forms--ro-face
; read-only appearance
1004 'read-only
,@(list (1+ forms--marker
))
1005 'intangible
,@(list (1+ forms--marker
))
1006 'insert-in-front-hooks
'(forms--iif-hook)
1007 'rear-nonsticky
'(face read-only insert-in-front-hooks
1011 `((let ((here (point)))
1012 (aset forms--markers
1013 ,(prog1 forms--marker
1014 (setq forms--marker
(1+ forms--marker
)))
1016 (insert (elt arg
,(1- el
)))
1017 (or (= (point) here
)
1018 (set-text-properties
1020 (list 'face forms--rw-face
1021 'front-sticky
'(face)))))))
1024 `((set-text-properties
1027 (insert (aset forms--dyntexts
1028 ,(prog1 forms--dyntext
1029 (setq forms--dyntext
(1+ forms--dyntext
)))
1032 (list 'face forms--ro-face
1033 'read-only
,@(list (1+ forms--marker
))
1034 'intangible
,@(list (1+ forms--marker
))
1035 'insert-in-front-hooks
'(forms--iif-hook)
1036 'rear-nonsticky
'(read-only face insert-in-front-hooks
1042 (defun forms--make-format-elt (el)
1043 "Helper routine to generate format function."
1045 ;; If we're not using text properties, the format routine
1046 ;; `forms--format' will look like
1049 ;; ;; a string, e.g. "text: "
1050 ;; (insert "text: ")
1051 ;; ;; a field, e.g. 6
1052 ;; (aset forms--markers 0 (point-marker))
1053 ;; (insert (elt arg 5))
1054 ;; ;; another string, e.g. "\nmore text: "
1055 ;; (insert "\nmore text: ")
1056 ;; ;; a function, e.g. (tocol 40)
1057 ;; (insert (aset forms--dyntexts 0 (tocol 40)))
1065 `((aset forms--markers
,forms--marker
(point-marker))
1066 (insert (elt arg
,(1- el
))))
1067 (setq forms--marker
(1+ forms--marker
))))
1070 `((insert (aset forms--dyntexts
,forms--dyntext
,el
)))
1071 (setq forms--dyntext
(1+ forms--dyntext
))))))
1073 (defvar forms--field
)
1074 (defvar forms--recordv
)
1075 (defvar forms--seen-text
)
1077 (defun forms--make-parser ()
1078 "Generate `forms--parser' from the information in `forms-format-list'."
1080 ;; If we can use text properties, we simply set it to
1081 ;; `forms--parser-using-text-properties'.
1082 ;; Otherwise, the function is constructed using a mapcar of
1083 ;; `forms--make-parser-elt on `forms-format-list'.
1087 (if forms-use-text-properties
1088 (function forms--parser-using-text-properties
)
1089 (let ((forms--field nil
)
1090 (forms--seen-text nil
)
1093 ;; Note: we add a nil element to the list passed to `mapcar',
1094 ;; see `forms--make-parser-elt' for details.
1097 (goto-char (point-min))
1100 'forms--make-parser-elt
1101 (append forms-format-list
(list nil
)))))))))
1103 (forms--debug 'forms--parser
))
1105 (defun forms--parser-using-text-properties ()
1106 "Extract field info from forms when using text properties."
1108 ;; Using text properties, we can simply jump to the markers, and
1109 ;; extract the information up to the following read-only segment.
1113 (while (< i
(length forms--markers
))
1114 (goto-char (setq here
(aref forms--markers i
)))
1115 (if (get-text-property here
'read-only
)
1116 (aset forms--recordv
(aref forms--elements i
) nil
)
1118 (next-single-property-change here
'read-only
))
1119 (aset forms--recordv
(aref forms--elements i
)
1120 (buffer-substring-no-properties here there
))
1121 (aset forms--recordv
(aref forms--elements i
)
1122 (buffer-substring-no-properties here
(point-max)))))
1125 (defun forms--make-parser-elt (el)
1126 "Helper routine to generate forms parser function."
1128 ;; The parse routine will look like:
1132 ;; (goto-char (point-min))
1135 ;; (if (not (looking-at "text: "))
1136 ;; (error "Parse error: cannot find \"text: \""))
1137 ;; (forward-char 6) ; past "text: "
1140 ;; ;; "\nmore text: "
1141 ;; (setq here (point))
1142 ;; (if (not (search-forward "\nmore text: " nil t nil))
1143 ;; (error "Parse error: cannot find \"\\nmore text: \""))
1144 ;; (aset forms--recordv 5 (buffer-substring-no-properties here (- (point) 12)))
1147 ;; (let ((forms--dyntext (car-safe forms--dynamic-text)))
1148 ;; (if (not (looking-at (regexp-quote forms--dyntext)))
1149 ;; (error "Parse error: not looking at \"%s\"" forms--dyntext))
1150 ;; (forward-char (length forms--dyntext))
1151 ;; (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))
1153 ;; ;; final flush (due to terminator sentinel, see below)
1154 ;; (aset forms--recordv 7 (buffer-substring-no-properties (point) (point-max)))
1160 `((setq here
(point))
1161 (if (not (search-forward ,el nil t nil
))
1162 (error "Parse error: cannot find `%s'" ,el
))
1163 (aset forms--recordv
,(1- forms--field
)
1164 (buffer-substring-no-properties here
1165 (- (point) ,(length el
)))))
1166 `((if (not (looking-at ,(regexp-quote el
)))
1167 (error "Parse error: not looking at `%s'" ,el
))
1168 (forward-char ,(length el
))))
1169 (setq forms--seen-text t
)
1170 (setq forms--field nil
)))
1173 (error "Cannot parse adjacent fields %d and %d"
1175 (setq forms--field el
)
1179 `((aset forms--recordv
,(1- forms--field
)
1180 (buffer-substring-no-properties (point) (point-max))))))
1184 `((let ((here (point))
1185 (forms--dyntext (aref forms--dyntexts
,forms--dyntext
)))
1186 (if (not (search-forward forms--dyntext nil t nil
))
1187 (error "Parse error: cannot find `%s'" forms--dyntext
))
1188 (aset forms--recordv
,(1- forms--field
)
1189 (buffer-substring-no-properties here
1190 (- (point) (length forms--dyntext
))))))
1191 `((let ((forms--dyntext (aref forms--dyntexts
,forms--dyntext
)))
1192 (if (not (looking-at (regexp-quote forms--dyntext
)))
1193 (error "Parse error: not looking at `%s'" forms--dyntext
))
1194 (forward-char (length forms--dyntext
)))))
1195 (setq forms--dyntext
(1+ forms--dyntext
))
1196 (setq forms--seen-text t
)
1197 (setq forms--field nil
)))
1200 (defun forms--intuit-from-file ()
1201 "Get number of fields and a default form using the data file."
1203 ;; If `forms-number-of-fields' is not set, get it from the data file.
1204 (if (null forms-number-of-fields
)
1206 ;; Need a file to do this.
1207 (if (not (file-exists-p forms-file
))
1208 (error "Need existing file or explicit `forms-number-of-fields'")
1210 ;; Visit the file and extract the first record.
1211 (setq forms--file-buffer
(find-file-noselect forms-file
))
1212 (let ((read-file-filter forms-read-file-filter
)
1215 (with-current-buffer forms--file-buffer
1216 (let ((inhibit-read-only t
))
1217 (run-hooks 'read-file-filter
))
1218 (goto-char (point-min))
1219 (forms--get-record)))
1221 ;; This may be overkill, but try to avoid interference with
1222 ;; the normal processing.
1223 (kill-buffer forms--file-buffer
)
1225 ;; Count the number of fields in `the-record'.
1228 (field-sep-length (length forms-field-sep
)))
1229 (setq forms-number-of-fields
1)
1230 (while (setq found-pos
1231 (string-match forms-field-sep the-record start-pos
))
1233 (setq forms-number-of-fields
(1+ forms-number-of-fields
))
1234 (setq start-pos
(+ field-sep-length found-pos
))))))))
1236 ;; Construct default format list.
1237 (setq forms-format-list
(list "Forms file \"" forms-file
"\".\n\n"))
1239 (while (<= (setq i
(1+ i
)) forms-number-of-fields
)
1240 (setq forms-format-list
1241 (append forms-format-list
1242 (list (format "%4d: " i
) i
"\n"))))))
1244 (defun forms--set-keymaps ()
1245 "Set the keymaps used in this mode."
1247 (use-local-map (if forms-read-only
1249 forms-mode-edit-map
)))
1251 (defun forms--mode-commands ()
1252 "Fill the Forms mode keymaps."
1254 ;; `forms-mode-map' is always accessible via \C-c prefix.
1255 (setq forms-mode-map
(make-keymap))
1256 (define-key forms-mode-map
"\t" 'forms-next-field
)
1257 (define-key forms-mode-map
"\C-k" 'forms-delete-record
)
1258 (define-key forms-mode-map
"\C-q" 'forms-toggle-read-only
)
1259 (define-key forms-mode-map
"\C-o" 'forms-insert-record
)
1260 (define-key forms-mode-map
"\C-l" 'forms-jump-record
)
1261 (define-key forms-mode-map
"\C-n" 'forms-next-record
)
1262 (define-key forms-mode-map
"\C-p" 'forms-prev-record
)
1263 (define-key forms-mode-map
"\C-r" 'forms-search-backward
)
1264 (define-key forms-mode-map
"\C-s" 'forms-search-forward
)
1265 (define-key forms-mode-map
"\C-x" 'forms-exit
)
1266 (define-key forms-mode-map
"<" 'forms-first-record
)
1267 (define-key forms-mode-map
">" 'forms-last-record
)
1268 (define-key forms-mode-map
"\C-?" 'forms-prev-record
)
1270 ;; `forms-mode-ro-map' replaces the local map when in read-only mode.
1271 (setq forms-mode-ro-map
(make-keymap))
1272 (suppress-keymap forms-mode-ro-map
)
1273 (define-key forms-mode-ro-map
"\C-c" forms-mode-map
)
1274 (define-key forms-mode-ro-map
"q" 'forms-toggle-read-only
)
1275 (define-key forms-mode-ro-map
"l" 'forms-jump-record
)
1276 (define-key forms-mode-ro-map
"n" 'forms-next-record
)
1277 (define-key forms-mode-ro-map
"p" 'forms-prev-record
)
1278 (define-key forms-mode-ro-map
"r" 'forms-search-backward
)
1279 (define-key forms-mode-ro-map
"s" 'forms-search-forward
)
1280 (define-key forms-mode-ro-map
"x" 'forms-exit
)
1281 (define-key forms-mode-ro-map
"<" 'forms-first-record
)
1282 (define-key forms-mode-ro-map
">" 'forms-last-record
)
1283 (define-key forms-mode-ro-map
"?" 'describe-mode
)
1284 (define-key forms-mode-ro-map
" " 'forms-next-record
)
1285 (forms--mode-commands1 forms-mode-ro-map
)
1286 (forms--mode-menu-ro forms-mode-ro-map
)
1288 ;; This is the normal, local map.
1289 (setq forms-mode-edit-map
(make-keymap))
1290 (define-key forms-mode-edit-map
"\C-c" forms-mode-map
)
1291 (forms--mode-commands1 forms-mode-edit-map
)
1292 (forms--mode-menu-edit forms-mode-edit-map
)
1295 (defun forms--mode-menu-ro (map)
1296 ;;; Menu initialisation
1297 ; (define-key map [menu-bar] (make-sparse-keymap))
1298 (define-key map
[menu-bar forms
]
1299 (cons "Forms" (make-sparse-keymap "Forms")))
1300 (define-key map
[menu-bar forms menu-forms-exit
]
1301 '("Exit Forms Mode" . forms-exit
))
1302 (define-key map
[menu-bar forms menu-forms-sep1
]
1304 (define-key map
[menu-bar forms menu-forms-save
]
1305 '("Save Data" . forms-save-buffer
))
1306 (define-key map
[menu-bar forms menu-forms-print
]
1307 '("Print Data" . forms-print
))
1308 (define-key map
[menu-bar forms menu-forms-describe
]
1309 '("Describe Mode" . describe-mode
))
1310 (define-key map
[menu-bar forms menu-forms-toggle-ro
]
1311 '("Toggle View/Edit" . forms-toggle-read-only
))
1312 (define-key map
[menu-bar forms menu-forms-jump-record
]
1313 '("Jump" . forms-jump-record
))
1314 (define-key map
[menu-bar forms menu-forms-search-backward
]
1315 '("Search Backward" . forms-search-backward
))
1316 (define-key map
[menu-bar forms menu-forms-search-forward
]
1317 '("Search Forward" . forms-search-forward
))
1318 (define-key map
[menu-bar forms menu-forms-delete-record
]
1319 '("Delete" . forms-delete-record
))
1320 (define-key map
[menu-bar forms menu-forms-insert-record
]
1321 '("Insert" . forms-insert-record
))
1322 (define-key map
[menu-bar forms menu-forms-sep2
]
1324 (define-key map
[menu-bar forms menu-forms-last-record
]
1325 '("Last Record" . forms-last-record
))
1326 (define-key map
[menu-bar forms menu-forms-first-record
]
1327 '("First Record" . forms-first-record
))
1328 (define-key map
[menu-bar forms menu-forms-prev-record
]
1329 '("Previous Record" . forms-prev-record
))
1330 (define-key map
[menu-bar forms menu-forms-next-record
]
1331 '("Next Record" . forms-next-record
))
1332 (define-key map
[menu-bar forms menu-forms-sep3
]
1334 (define-key map
[menu-bar forms menu-forms-prev-field
]
1335 '("Previous Field" . forms-prev-field
))
1336 (define-key map
[menu-bar forms menu-forms-next-field
]
1337 '("Next Field" . forms-next-field
))
1338 (put 'forms-insert-record
'menu-enable
'(not forms-read-only
))
1339 (put 'forms-delete-record
'menu-enable
'(not forms-read-only
))
1341 (defun forms--mode-menu-edit (map)
1342 ;;; Menu initialisation
1343 ; (define-key map [menu-bar] (make-sparse-keymap))
1344 (define-key map
[menu-bar forms
]
1345 (cons "Forms" (make-sparse-keymap "Forms")))
1346 (define-key map
[menu-bar forms menu-forms-edit--exit
]
1347 '("Exit" . forms-exit
))
1348 (define-key map
[menu-bar forms menu-forms-edit-sep1
]
1350 (define-key map
[menu-bar forms menu-forms-edit-save
]
1351 '("Save Data" . forms-save-buffer
))
1352 (define-key map
[menu-bar forms menu-forms-edit-print
]
1353 '("Print Data" . forms-print
))
1354 (define-key map
[menu-bar forms menu-forms-edit-describe
]
1355 '("Describe Mode" . describe-mode
))
1356 (define-key map
[menu-bar forms menu-forms-edit-toggle-ro
]
1357 '("Toggle View/Edit" . forms-toggle-read-only
))
1358 (define-key map
[menu-bar forms menu-forms-edit-jump-record
]
1359 '("Jump" . forms-jump-record
))
1360 (define-key map
[menu-bar forms menu-forms-edit-search-backward
]
1361 '("Search Backward" . forms-search-backward
))
1362 (define-key map
[menu-bar forms menu-forms-edit-search-forward
]
1363 '("Search Forward" . forms-search-forward
))
1364 (define-key map
[menu-bar forms menu-forms-edit-delete-record
]
1365 '("Delete" . forms-delete-record
))
1366 (define-key map
[menu-bar forms menu-forms-edit-insert-record
]
1367 '("Insert" . forms-insert-record
))
1368 (define-key map
[menu-bar forms menu-forms-edit-sep2
]
1370 (define-key map
[menu-bar forms menu-forms-edit-last-record
]
1371 '("Last Record" . forms-last-record
))
1372 (define-key map
[menu-bar forms menu-forms-edit-first-record
]
1373 '("First Record" . forms-first-record
))
1374 (define-key map
[menu-bar forms menu-forms-edit-prev-record
]
1375 '("Previous Record" . forms-prev-record
))
1376 (define-key map
[menu-bar forms menu-forms-edit-next-record
]
1377 '("Next Record" . forms-next-record
))
1378 (define-key map
[menu-bar forms menu-forms-edit-sep3
]
1380 (define-key map
[menu-bar forms menu-forms-edit-prev-field
]
1381 '("Previous Field" . forms-prev-field
))
1382 (define-key map
[menu-bar forms menu-forms-edit-next-field
]
1383 '("Next Field" . forms-next-field
))
1384 (put 'forms-insert-record
'menu-enable
'(not forms-read-only
))
1385 (put 'forms-delete-record
'menu-enable
'(not forms-read-only
))
1388 (defun forms--mode-commands1 (map)
1389 "Helper routine to define keys."
1390 (define-key map
"\t" 'forms-next-field
)
1391 (define-key map
[S-tab
] 'forms-prev-field
)
1392 (define-key map
[next] 'forms-next-record)
1393 (define-key map [prior] 'forms-prev-record)
1394 (define-key map [begin] 'forms-first-record)
1395 (define-key map [last] 'forms-last-record)
1396 (define-key map [backtab] 'forms-prev-field)
1399 ;;; Changed functions
1401 (defun forms--change-commands ()
1402 "Localize some commands for Forms mode."
1404 ;; scroll-down -> forms-prev-record
1405 ;; scroll-up -> forms-next-record
1406 (if forms-forms-scroll
1408 (local-set-key [remap scroll-up] 'forms-next-record)
1409 (local-set-key [remap scroll-down] 'forms-prev-record)
1410 (local-set-key [remap scroll-up-command] 'forms-next-record)
1411 (local-set-key [remap scroll-down-command] 'forms-prev-record)))
1413 ;; beginning-of-buffer -> forms-first-record
1414 ;; end-of-buffer -> forms-end-record
1415 (if forms-forms-jump
1417 (local-set-key [remap beginning-of-buffer] 'forms-first-record)
1418 (local-set-key [remap end-of-buffer] 'forms-last-record)))
1421 (local-set-key "\C-x\C-s" 'forms-save-buffer)
1423 ;; We have our own revert function - use it.
1424 (make-local-variable 'revert-buffer-function)
1425 (setq revert-buffer-function 'forms--revert-buffer)
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"))))
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."
1442 (re (regexp-quote arg))
1443 (k (string-to-char rep)))
1444 (while (setq i (string-match re subj i))
1448 (defun forms--exit (&optional save)
1449 "Internal exit from forms mode function."
1451 (let ((buf (buffer-name forms--file-buffer)))
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???
1461 (error "Problem saving buffer %s" (buffer-name buf)))
1462 (delete-auto-save-file-if-necessary)
1463 (kill-buffer (current-buffer)))))
1465 (defun forms--get-record ()
1466 "Fetch the current record from the file buffer."
1468 ;; This function is executed in the context of the `forms--file-buffer'.
1471 (beginning-of-line nil))
1472 (let ((here (point)))
1475 (buffer-substring-no-properties here (point))
1478 (defun forms--show-record (the-record)
1479 "Format THE-RECORD and display it in the current buffer."
1481 ;; Split the-record.
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)))
1493 (append the-result (list ent)))
1494 (setq start-pos (+ field-sep-length found-pos))))
1495 (setq forms--the-record-list the-result))
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)))
1503 ;; Verify the number of fields, extend forms--the-record-list if needed.
1504 (if (= (length forms--the-record-list) forms-number-of-fields)
1506 (if (null forms-check-number-of-fields)
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
1514 (- forms-number-of-fields
1515 (length forms--the-record-list))
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)
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))))
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
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.
1539 (let (forms--recordv)
1541 ;; Build the vector.
1542 (setq forms--recordv (vconcat forms--the-record-list))
1544 ;; Parse the form and update the vector.
1545 (let ((forms--dynamic-text forms--dynamic-text))
1546 (funcall forms--parser))
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)))
1555 ;; Transform to a list and return.
1556 (append forms--recordv nil))))
1558 (defun forms--update ()
1559 "Update current record with contents of form.
1560 As a side effect: sets `forms--the-record-list'."
1563 (error "Buffer is read-only"))
1566 ;; Build new record.
1567 (setq forms--the-record-list (forms--parse-form))
1569 (mapconcat 'identity forms--the-record-list forms-field-sep))
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"))
1575 ;; Handle multi-line fields, if allowed.
1576 (if forms-multi-line
1577 (forms--trans the-record "\n" forms-multi-line))
1579 ;; A final sanity check before updating.
1580 (if (string-match "\n" the-record)
1581 (error "Multi-line fields in this record - update refused"))
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))
1588 (beginning-of-line))))
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)))
1595 (set-buffer-modified-p nil)
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))))
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))))
1618 (defun forms-exit ()
1619 "Normal exit from Forms mode. Modified buffers are saved."
1623 (defun forms-exit-no-save ()
1624 "Exit from Forms mode without saving buffers."
1628 ;;; Navigating commands
1630 (defun forms-next-record (arg)
1631 "Advance to the ARGth following record."
1633 (forms-jump-record (+ forms--current-record (prefix-numeric-value arg)) t))
1635 (defun forms-prev-record (arg)
1636 "Advance to the ARGth previous record."
1638 (forms-jump-record (- forms--current-record (prefix-numeric-value arg)) t))
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."
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))))
1650 (defun forms-jump-record (arg &optional relative)
1651 "Jump to a random record."
1652 (interactive "NRecord number: ")
1654 ;; Verify that the record number is within range.
1655 (if (or (> arg forms--total-records)
1658 ;; Don't give the message if just paging.
1660 (message "Record number %d out of range 1..%d"
1661 arg forms--total-records)
1667 ;; Calculate displacement.
1668 (let ((cur forms--current-record))
1670 ;; `forms--show-record' needs it now.
1671 (setq forms--current-record arg)
1673 ;; Get the record and show it.
1675 (with-current-buffer forms--file-buffer
1678 ;; Move, and adjust the amount if needed (shouldn't happen).
1679 (setq cur (- arg (forms--goto-record arg (if relative cur))))
1681 (forms--get-record)))
1683 ;; This shouldn't happen.
1684 (if (/= forms--current-record cur)
1686 (setq forms--current-record cur)
1687 (error "Stuck at record %d" cur)))))
1689 (defun forms-first-record ()
1690 "Jump to first record."
1692 (forms-jump-record 1))
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."
1700 (with-current-buffer forms--file-buffer
1701 (count-lines (point-min) (point-max)))))
1702 (if (= numrec forms--total-records)
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))
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."
1718 ;; Negative arg means switch it off.
1719 (<= (prefix-numeric-value arg) 0)
1720 ;; No arg means toggle.
1723 ;; Enable edit mode, if possible.
1724 (let ((ro forms-read-only))
1725 (if (with-current-buffer forms--file-buffer
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)
1735 ;; Enable view mode.
1738 (forms--checkmod) ; sync
1739 (setq forms-read-only t)
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))
1748 ;; (setq forms-new-record-filter 'my-new-record-filter)
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."
1763 (let (ln the-list the-record)
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)))
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 "")))
1785 (with-current-buffer forms--file-buffer
1786 (forms--goto-record ln)
1789 (beginning-of-line))
1791 (setq forms--current-record ln))
1793 (setq forms--total-records (1+ forms--total-records))
1794 (forms-jump-record forms--current-record))
1796 (defun forms-delete-record (arg)
1797 "Deletes a record. With a prefix argument: don't ask."
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)))
1819 (defun forms-search-forward (regexp)
1820 "Search forward for record containing REGEXP."
1822 (list (read-string (concat "Search forward for"
1823 (if forms--search-regexp
1825 forms--search-regexp
1828 (if (equal "" regexp)
1829 (setq regexp forms--search-regexp))
1832 (let (the-line the-record here)
1833 (with-current-buffer forms--file-buffer
1836 (if (or (re-search-forward regexp nil t)
1837 (and (> here (point-min))
1839 (goto-char (point-min))
1840 (re-search-forward regexp here t))))
1842 (setq the-record (forms--get-record))
1843 (setq the-line (1+ (count-lines (point-min) (point))))
1844 (if (< (point) here)
1845 (message "Wrapped")))
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))
1853 (defun forms-search-backward (regexp)
1854 "Search backward for record containing REGEXP."
1856 (list (read-string (concat "Search backward for"
1857 (if forms--search-regexp
1859 forms--search-regexp
1862 (if (equal "" regexp)
1863 (setq regexp forms--search-regexp))
1866 (let (the-line the-record here)
1867 (with-current-buffer forms--file-buffer
1870 (if (or (re-search-backward regexp nil t)
1871 (and (< (point) (point-max))
1873 (goto-char (point-max))
1874 (re-search-backward regexp here t))))
1876 (setq the-record (forms--get-record))
1877 (setq the-line (1+ (count-lines (point-min) (point))))
1878 (if (> (point) here)
1879 (message "Wrapped")))
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))
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."
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
1902 ;; (run-hooks 'write-file-filter)))
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))
1912 (if read-file-filter
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))
1922 (defun forms--revert-buffer (&optional arg noconfirm)
1923 "Reverts current form to un-modified."
1926 (yes-or-no-p "Revert form to unmodified? "))
1928 (set-buffer-modified-p nil)
1929 (forms-jump-record forms--current-record))))
1931 (defun forms-next-field (arg)
1932 "Jump to ARG-th next field."
1939 (inhibit-point-motion-hooks t))
1943 (setq cnt (+ cnt arg)))
1946 (while (< i (length forms--markers))
1947 (if (or (null (setq there (aref forms--markers i)))
1950 (if (<= (setq cnt (1- cnt)) 0)
1956 (goto-char (aref forms--markers 0)))))
1958 (defun forms-prev-field (arg)
1959 "Jump to ARG-th previous field."
1962 (let ((i (length forms--markers))
1966 (inhibit-point-motion-hooks t))
1970 (setq cnt (+ cnt arg)))
1975 (if (or (null (setq there (aref forms--markers i)))
1978 (if (<= (setq cnt (1- cnt)) 0)
1981 (throw 'done t))))))
1983 (goto-char (aref forms--markers (1- (length forms--markers)))))))
1985 (defun forms-print ()
1986 "Send the records to the printer with `print-buffer', one record per page."
1988 (let ((inhibit-read-only t)
1989 (save-record forms--current-record)
1990 (total-nb-records forms--total-records)
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))
1999 (setq buffer-read-only nil)
2000 (if (< nb-record total-nb-records)
2002 (setq nb-record (1+ nb-record)))
2003 (with-current-buffer "*forms-print*"
2005 (set-buffer-modified-p nil)
2006 (kill-buffer (current-buffer)))
2007 (forms-jump-record save-record)))
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.
2017 Usage: (setq forms-number-of-fields
2019 '(field1 field2 field2 ...)))"
2021 (let ((the-index 0))
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)))
2031 (defvar forms--debug nil
2032 "*Enables forms-mode debugging if not nil.")
2034 (defun forms--debug (&rest args)
2035 "Internal debugging routine."
2039 (let ((el (car-safe args)))
2040 (setq args (cdr-safe args))
2042 (setq ret (concat ret el))
2043 (setq ret (concat ret (prin1-to-string el) " = "))
2045 (let ((vel (eval el)))
2046 (setq ret (concat ret (prin1-to-string vel) "\n")))
2047 (setq ret (concat ret "<unbound>" "\n")))
2049 (setq ret (concat ret (prin1-to-string (symbol-function el))
2051 (with-current-buffer (get-buffer-create "*forms-mode debug*")
2052 (if (zerop (buffer-size))
2054 (goto-char (point-max))
2057 ;;; forms.el ends here