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