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