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