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