Commit | Line | Data |
---|---|---|
b22c9ebf RS |
1 | ;;; forms.el -- Forms mode: edit a file as a form to fill in. |
2 | ;;; Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | ||
4 | ;;; Author: Johan Vromans | |
5 | ||
6 | ;; This file is part of GNU Emacs. | |
7 | ||
8 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 | ;; it under the terms of the GNU General Public License as published by | |
10 | ;; the Free Software Foundation; either version 2, or (at your option) | |
11 | ;; any later version. | |
12 | ||
13 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
c1110355 | 17 | |
b22c9ebf RS |
18 | ;; You should have received a copy of the GNU General Public License |
19 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | ;;; Commentary: | |
c1110355 BP |
23 | |
24 | ;;; Visit a file using a form. | |
25 | ;;; | |
26 | ;;; === Naming conventions | |
27 | ;;; | |
28 | ;;; The names of all variables and functions start with 'form-'. | |
29 | ;;; Names which start with 'form--' are intended for internal use, and | |
30 | ;;; should *NOT* be used from the outside. | |
31 | ;;; | |
32 | ;;; All variables are buffer-local, to enable multiple forms visits | |
33 | ;;; simultaneously. | |
34 | ;;; Variable 'forms--mode-setup' is local to *ALL* buffers, for it | |
35 | ;;; controls if forms-mode has been enabled in a buffer. | |
36 | ;;; | |
37 | ;;; === How it works === | |
38 | ;;; | |
39 | ;;; Forms mode means visiting a data file which is supposed to consist | |
40 | ;;; of records each containing a number of fields. The records are | |
41 | ;;; separated by a newline, the fields are separated by a user-defined | |
42 | ;;; field separater (default: TAB). | |
43 | ;;; When shown, a record is transferred to an emacs buffer and | |
44 | ;;; presented using a user-defined form. One record is shown at a | |
45 | ;;; time. | |
46 | ;;; | |
47 | ;;; Forms mode is a composite mode. It involves two files, and two | |
48 | ;;; buffers. | |
49 | ;;; The first file, called the control file, defines the name of the | |
50 | ;;; data file and the forms format. This file buffer will be used to | |
51 | ;;; present the forms. | |
52 | ;;; The second file holds the actual data. The buffer of this file | |
53 | ;;; will be buried, for it is never accessed directly. | |
54 | ;;; | |
55 | ;;; Forms mode is invoked using "forms-find-file control-file". | |
56 | ;;; Alternativily forms-find-file-other-window can be used. | |
57 | ;;; | |
58 | ;;; You may also visit the control file, and switch to forms mode by hand | |
59 | ;;; with M-x forms-mode . | |
60 | ;;; | |
61 | ;;; Automatic mode switching is supported, so you may use "find-file" | |
62 | ;;; if you specify "-*- forms -*-" in the first line of the control file. | |
63 | ;;; | |
64 | ;;; The control file is visited, evaluated using | |
65 | ;;; eval-current-buffer, and should set at least the following | |
66 | ;;; variables: | |
67 | ;;; | |
68 | ;;; forms-file [string] the name of the data file. | |
69 | ;;; | |
70 | ;;; forms-number-of-fields [integer] | |
71 | ;;; The number of fields in each record. | |
72 | ;;; | |
73 | ;;; forms-format-list [list] formatting instructions. | |
74 | ;;; | |
75 | ;;; The forms-format-list should be a list, each element containing | |
76 | ;;; | |
01a45313 | 77 | ;;; - a string, e.g. "hello" (which is inserted \"as is\"), |
c1110355 BP |
78 | ;;; |
79 | ;;; - an integer, denoting a field number. The contents of the field | |
80 | ;;; are inserted at this point. | |
81 | ;;; The first field has number one. | |
82 | ;;; | |
01a45313 RS |
83 | ;;; - a function call, e.g. (insert "text"). This function call is |
84 | ;;; dynamically evaluated and should return a string. It should *NOT* | |
85 | ;;; have side-effects on the forms being constructed. | |
86 | ;;; The current fields are available to the function in the variable | |
87 | ;;; forms-fields, they should *NOT* be modified. | |
88 | ;;; | |
89 | ;;; - a lisp symbol, that must evaluate to one of the above. | |
90 | ;;; | |
c1110355 BP |
91 | ;;; Optional variables which may be set in the control file: |
92 | ;;; | |
93 | ;;; forms-field-sep [string, default TAB] | |
94 | ;;; The field separator used to separate the | |
95 | ;;; fields in the data file. It may be a string. | |
96 | ;;; | |
97 | ;;; forms-read-only [bool, default nil] | |
98 | ;;; 't' means that the data file is visited read-only. | |
99 | ;;; If no write access to the data file is | |
100 | ;;; possible, read-only mode is enforced. | |
101 | ;;; | |
102 | ;;; forms-multi-line [string, default "^K"] | |
103 | ;;; If non-null the records of the data file may | |
104 | ;;; contain fields which span multiple lines in | |
105 | ;;; the form. | |
106 | ;;; This variable denoted the separator character | |
107 | ;;; to be used for this purpose. Upon display, all | |
108 | ;;; occurrencies of this character are translated | |
109 | ;;; to newlines. Upon storage they are translated | |
110 | ;;; back to the separator. | |
111 | ;;; | |
112 | ;;; forms-forms-scroll [bool, default t] | |
113 | ;;; If non-nil: redefine scroll-up/down to perform | |
114 | ;;; forms-next/prev-field if in forms mode. | |
115 | ;;; | |
116 | ;;; forms-forms-jump [bool, default t] | |
117 | ;;; If non-nil: redefine beginning/end-of-buffer | |
118 | ;;; to performs forms-first/last-field if in | |
119 | ;;; forms mode. | |
120 | ;;; | |
01a45313 RS |
121 | ;;; forms-new-record-filter [symbol, no default] |
122 | ;;; If defined: this should be the name of a | |
123 | ;;; function that is called when a new | |
c1110355 BP |
124 | ;;; record is created. It can be used to fill in |
125 | ;;; the new record with default fields, for example. | |
01a45313 RS |
126 | ;;; Instead of the name of the function, it may |
127 | ;;; be the function itself. | |
128 | ;;; | |
129 | ;;; forms-modified-record-filter [symbol, no default] | |
130 | ;;; If defined: this should be the name of a | |
131 | ;;; function that is called when a record has | |
132 | ;;; been modified. It is called after the fields | |
133 | ;;; are parsed. It can be used to register | |
134 | ;;; modification dates, for example. | |
135 | ;;; Instead of the name of the function, it may | |
136 | ;;; be the function itself. | |
c1110355 BP |
137 | ;;; |
138 | ;;; After evaluating the control file, its buffer is cleared and used | |
139 | ;;; for further processing. | |
140 | ;;; The data file (as designated by "forms-file") is visited in a buffer | |
141 | ;;; (forms--file-buffer) which will not normally be shown. | |
142 | ;;; Great malfunctioning may be expected if this file/buffer is modified | |
143 | ;;; outside of this package while it's being visited! | |
144 | ;;; | |
145 | ;;; A record from the data file is transferred from the data file, | |
146 | ;;; split into fields (into forms--the-record-list), and displayed using | |
147 | ;;; the specs in forms-format-list. | |
01a45313 | 148 | ;;; A format routine 'forms--format' is built upon startup to format |
c1110355 BP |
149 | ;;; the records. |
150 | ;;; | |
151 | ;;; When a form is changed the record is updated as soon as this form | |
152 | ;;; is left. The contents of the form are parsed using forms-format-list, | |
153 | ;;; and the fields which are deduced from the form are modified. So, | |
154 | ;;; fields not shown on the forms retain their origional values. | |
155 | ;;; The newly formed record and replaces the contents of the | |
156 | ;;; old record in forms--file-buffer. | |
01a45313 | 157 | ;;; A parse routine 'forms--parser' is built upon startup to parse |
c1110355 BP |
158 | ;;; the records. |
159 | ;;; | |
160 | ;;; Two exit functions exist: forms-exit (which saves) and forms-exit-no-save | |
161 | ;;; (which doesn't). However, if forms-exit-no-save is executed and the file | |
162 | ;;; buffer has been modified, emacs will ask questions. | |
163 | ;;; | |
164 | ;;; Other functions are: | |
165 | ;;; | |
166 | ;;; paging (forward, backward) by record | |
167 | ;;; jumping (first, last, random number) | |
168 | ;;; searching | |
169 | ;;; creating and deleting records | |
170 | ;;; reverting the form (NOT the file buffer) | |
171 | ;;; switching edit <-> view mode v.v. | |
172 | ;;; jumping from field to field | |
173 | ;;; | |
174 | ;;; As an documented side-effect: jumping to the last record in the | |
175 | ;;; file (using forms-last-record) will adjust forms--total-records if | |
176 | ;;; needed. | |
177 | ;;; | |
178 | ;;; Commands and keymaps: | |
179 | ;;; | |
180 | ;;; A local keymap 'forms-mode-map' is used in the forms buffer. | |
181 | ;;; As conventional, this map can be accessed with C-c prefix. | |
182 | ;;; In read-only mode, the C-c prefix must be omitted. | |
183 | ;;; | |
184 | ;;; Default bindings: | |
185 | ;;; | |
186 | ;;; \C-c forms-mode-map | |
187 | ;;; TAB forms-next-field | |
188 | ;;; SPC forms-next-record | |
189 | ;;; < forms-first-record | |
190 | ;;; > forms-last-record | |
191 | ;;; ? describe-mode | |
192 | ;;; d forms-delete-record | |
193 | ;;; e forms-edit-mode | |
194 | ;;; i forms-insert-record | |
195 | ;;; j forms-jump-record | |
196 | ;;; n forms-next-record | |
197 | ;;; p forms-prev-record | |
198 | ;;; q forms-exit | |
199 | ;;; s forms-search | |
200 | ;;; v forms-view-mode | |
201 | ;;; x forms-exit-no-save | |
202 | ;;; DEL forms-prev-record | |
203 | ;;; | |
204 | ;;; Standard functions scroll-up, scroll-down, beginning-of-buffer and | |
205 | ;;; end-of-buffer are wrapped with re-definitions, which map them to | |
206 | ;;; next/prev record and first/last record. | |
207 | ;;; Buffer-local variables forms-forms-scroll and forms-forms-jump | |
208 | ;;; may be used to control these redefinitions. | |
209 | ;;; | |
210 | ;;; Function save-buffer is also wrapped to perform a sensible action. | |
211 | ;;; A revert-file-hook is defined to revert a forms to original. | |
212 | ;;; | |
213 | ;;; For convenience, TAB is always bound to forms-next-field, so you | |
214 | ;;; don't need the C-c prefix for this command. | |
b22c9ebf RS |
215 | \f |
216 | ;;; Code: | |
217 | ||
c1110355 BP |
218 | ;;; Global variables and constants |
219 | ||
b22c9ebf RS |
220 | (provide 'forms) ;;; official |
221 | (provide 'forms-mode) ;;; for compatibility | |
222 | ||
223 | (defconst forms-version "1.2.10" | |
224 | "Version of forms-mode implementation.") | |
c1110355 BP |
225 | |
226 | (defvar forms-forms-scrolls t | |
b22c9ebf | 227 | "If non-null: redefine scroll-up/down to be used with Forms mode.") |
c1110355 BP |
228 | |
229 | (defvar forms-forms-jumps t | |
b22c9ebf | 230 | "If non-null: redefine beginning/end-of-buffer to be used with Forms mode.") |
c1110355 BP |
231 | |
232 | (defvar forms-mode-hooks nil | |
b22c9ebf RS |
233 | "Hook functions to be run upon entering Forms mode.") |
234 | \f | |
c1110355 BP |
235 | ;;; Mandatory variables - must be set by evaluating the control file |
236 | ||
237 | (defvar forms-file nil | |
01a45313 | 238 | "Name of the file holding the data.") |
c1110355 BP |
239 | |
240 | (defvar forms-format-list nil | |
01a45313 | 241 | "List of formatting specifications.") |
c1110355 BP |
242 | |
243 | (defvar forms-number-of-fields nil | |
244 | "Number of fields per record.") | |
245 | ||
b22c9ebf | 246 | \f |
c1110355 BP |
247 | ;;; Optional variables with default values |
248 | ||
249 | (defvar forms-field-sep "\t" | |
b22c9ebf | 250 | "Field separator character (default TAB).") |
c1110355 BP |
251 | |
252 | (defvar forms-read-only nil | |
253 | "Read-only mode (defaults to the write access on the data file).") | |
254 | ||
255 | (defvar forms-multi-line "\C-k" | |
b22c9ebf | 256 | "Character to separate multi-line fields (default C-k)") |
c1110355 BP |
257 | |
258 | (defvar forms-forms-scroll t | |
b22c9ebf | 259 | "Redefine scroll-up/down to perform forms-next/prev-record in Forms mode.") |
c1110355 BP |
260 | |
261 | (defvar forms-forms-jump t | |
b22c9ebf | 262 | "Redefine beginning/end-of-buffer to perform forms-first/last-record in Forms mode.") |
c1110355 | 263 | |
b22c9ebf | 264 | \f |
c1110355 BP |
265 | ;;; Internal variables. |
266 | ||
267 | (defvar forms--file-buffer nil | |
268 | "Buffer which holds the file data") | |
269 | ||
270 | (defvar forms--total-records 0 | |
271 | "Total number of records in the data file.") | |
272 | ||
273 | (defvar forms--current-record 0 | |
274 | "Number of the record currently on the screen.") | |
275 | ||
276 | (defvar forms-mode-map nil ; yes - this one is global | |
277 | "Keymap for form buffer.") | |
278 | ||
279 | (defvar forms--markers nil | |
280 | "Field markers in the screen.") | |
281 | ||
282 | (defvar forms--number-of-markers 0 | |
283 | "Number of fields on screen.") | |
284 | ||
285 | (defvar forms--the-record-list nil | |
286 | "List of strings of the current record, as parsed from the file.") | |
287 | ||
288 | (defvar forms--search-regexp nil | |
289 | "Last regexp used by forms-search.") | |
290 | ||
291 | (defvar forms--format nil | |
292 | "Formatting routine.") | |
293 | ||
294 | (defvar forms--parser nil | |
295 | "Forms parser routine.") | |
296 | ||
297 | (defvar forms--mode-setup nil | |
298 | "Internal - keeps track of forms-mode being set-up.") | |
299 | (make-variable-buffer-local 'forms--mode-setup) | |
300 | ||
301 | (defvar forms--new-record-filter nil | |
302 | "Internal - set if a new record filter has been defined.") | |
303 | ||
01a45313 RS |
304 | (defvar forms--modified-record-filter nil |
305 | "Internal - set if a modified record filter has been defined.") | |
306 | ||
307 | (defvar forms--dynamic-text nil | |
308 | "Internal - holds dynamic text to insert between fields.") | |
309 | ||
310 | (defvar forms-fields nil | |
311 | "List with fields of the current forms. First field has number 1.") | |
312 | ||
b22c9ebf RS |
313 | (defvar forms-new-record-filter |
314 | "The name of a function that is called when a new record is created.") | |
315 | ||
316 | (defvar forms-modified-record-filter | |
317 | "The name of a function that is called when a record has been modified.") | |
318 | \f | |
c1110355 BP |
319 | ;;; forms-mode |
320 | ;;; | |
321 | ;;; This is not a simple major mode, as usual. Therefore, forms-mode | |
322 | ;;; takes an optional argument 'primary' which is used for the initial | |
323 | ;;; set-up. Normal use would leave 'primary' to nil. | |
324 | ;;; | |
325 | ;;; A global buffer-local variable 'forms--mode-setup' has the same effect | |
326 | ;;; but makes it possible to auto-invoke forms-mode using find-file. | |
327 | ;;; | |
328 | ;;; Note: although it seems logical to have (make-local-variable) executed | |
329 | ;;; where the variable is first needed, I deliberately placed all calls | |
330 | ;;; in the forms-mode function. | |
331 | ||
332 | (defun forms-mode (&optional primary) | |
333 | "Major mode to visit files in a field-structured manner using a form. | |
334 | ||
335 | Commands (prefix with C-c if not in read-only mode): | |
336 | \\{forms-mode-map}" | |
337 | ||
338 | (interactive) ; no - 'primary' is not prefix arg | |
339 | ||
340 | ;; Primary set-up: evaluate buffer and check if the mandatory | |
341 | ;; variables have been set. | |
342 | (if (or primary (not forms--mode-setup)) | |
343 | (progn | |
344 | (kill-all-local-variables) | |
345 | ||
346 | ;; make mandatory variables | |
347 | (make-local-variable 'forms-file) | |
348 | (make-local-variable 'forms-number-of-fields) | |
349 | (make-local-variable 'forms-format-list) | |
350 | ||
351 | ;; make optional variables | |
352 | (make-local-variable 'forms-field-sep) | |
353 | (make-local-variable 'forms-read-only) | |
354 | (make-local-variable 'forms-multi-line) | |
355 | (make-local-variable 'forms-forms-scroll) | |
356 | (make-local-variable 'forms-forms-jump) | |
357 | (fmakunbound 'forms-new-record-filter) | |
358 | ||
359 | ;; eval the buffer, should set variables | |
360 | (eval-current-buffer) | |
361 | ||
362 | ;; check if the mandatory variables make sense. | |
363 | (or forms-file | |
364 | (error "'forms-file' has not been set")) | |
365 | (or forms-number-of-fields | |
366 | (error "'forms-number-of-fields' has not been set")) | |
367 | (or (> forms-number-of-fields 0) | |
368 | (error "'forms-number-of-fields' must be > 0") | |
369 | (or (stringp forms-field-sep)) | |
370 | (error "'forms-field-sep' is not a string")) | |
371 | (if forms-multi-line | |
372 | (if (and (stringp forms-multi-line) | |
373 | (eq (length forms-multi-line) 1)) | |
374 | (if (string= forms-multi-line forms-field-sep) | |
375 | (error "'forms-multi-line' is equal to 'forms-field-sep'")) | |
376 | (error "'forms-multi-line' must be nil or a one-character string"))) | |
377 | ||
378 | ;; validate and process forms-format-list | |
379 | (make-local-variable 'forms--number-of-markers) | |
380 | (make-local-variable 'forms--markers) | |
381 | (forms--process-format-list) | |
382 | ||
383 | ;; build the formatter and parser | |
384 | (make-local-variable 'forms--format) | |
385 | (forms--make-format) | |
386 | (make-local-variable 'forms--parser) | |
387 | (forms--make-parser) | |
388 | ||
01a45313 | 389 | ;; check if record filters are defined |
c1110355 BP |
390 | (make-local-variable 'forms--new-record-filter) |
391 | (setq forms--new-record-filter | |
01a45313 RS |
392 | (cond |
393 | ((fboundp 'forms-new-record-filter) | |
394 | (symbol-function 'forms-new-record-filter)) | |
395 | ((and (boundp 'forms-new-record-filter) | |
396 | (fboundp forms-new-record-filter)) | |
397 | forms-new-record-filter))) | |
c1110355 | 398 | (fmakunbound 'forms-new-record-filter) |
01a45313 RS |
399 | (make-local-variable 'forms--modified-record-filter) |
400 | (setq forms--modified-record-filter | |
401 | (cond | |
402 | ((fboundp 'forms-modified-record-filter) | |
403 | (symbol-function 'forms-modified-record-filter)) | |
404 | ((and (boundp 'forms-modified-record-filter) | |
405 | (fboundp forms-modified-record-filter)) | |
406 | forms-modified-record-filter))) | |
407 | (fmakunbound 'forms-modified-record-filter) | |
408 | ||
409 | ;; dynamic text support | |
410 | (make-local-variable 'forms--dynamic-text) | |
411 | (make-local-variable 'forms-fields) | |
c1110355 BP |
412 | |
413 | ;; prepare this buffer for further processing | |
414 | (setq buffer-read-only nil) | |
415 | ||
416 | ;; prevent accidental overwrite of the control file and autosave | |
417 | (setq buffer-file-name nil) | |
418 | (auto-save-mode nil) | |
419 | ||
420 | ;; and clean it | |
421 | (erase-buffer))) | |
422 | ||
b22c9ebf | 423 | ;; Make more local variables |
c1110355 BP |
424 | (make-local-variable 'forms--file-buffer) |
425 | (make-local-variable 'forms--total-records) | |
426 | (make-local-variable 'forms--current-record) | |
427 | (make-local-variable 'forms--the-record-list) | |
428 | (make-local-variable 'forms--search-rexexp) | |
429 | ||
430 | ;; A bug in the current Emacs release prevents a keymap | |
431 | ;; which is buffer-local from being used by 'describe-mode'. | |
432 | ;; Hence we'll leave it global. | |
433 | ;;(make-local-variable 'forms-mode-map) | |
434 | (if forms-mode-map ; already defined | |
435 | nil | |
436 | (setq forms-mode-map (make-keymap)) | |
437 | (forms--mode-commands forms-mode-map) | |
438 | (forms--change-commands)) | |
439 | ||
440 | ;; find the data file | |
441 | (setq forms--file-buffer (find-file-noselect forms-file)) | |
442 | ||
443 | ;; count the number of records, and set see if it may be modified | |
444 | (let (ro) | |
445 | (setq forms--total-records | |
446 | (save-excursion | |
447 | (set-buffer forms--file-buffer) | |
448 | (bury-buffer (current-buffer)) | |
449 | (setq ro buffer-read-only) | |
450 | (count-lines (point-min) (point-max)))) | |
451 | (if ro | |
452 | (setq forms-read-only t))) | |
453 | ||
454 | ;; set the major mode indicator | |
455 | (setq major-mode 'forms-mode) | |
456 | (setq mode-name "Forms") | |
457 | (make-local-variable 'minor-mode-alist) ; needed? | |
458 | (forms--set-minor-mode) | |
459 | (forms--set-keymaps) | |
460 | ||
461 | (set-buffer-modified-p nil) | |
462 | ||
463 | ;; We have our own revert function - use it | |
464 | (make-local-variable 'revert-buffer-function) | |
465 | (setq revert-buffer-function 'forms-revert-buffer) | |
466 | ||
467 | ;; setup the first (or current) record to show | |
468 | (if (< forms--current-record 1) | |
469 | (setq forms--current-record 1)) | |
470 | (forms-jump-record forms--current-record) | |
471 | ||
472 | ;; user customising | |
473 | (run-hooks 'forms-mode-hooks) | |
474 | ||
475 | ;; be helpful | |
476 | (forms--help) | |
477 | ||
478 | ;; initialization done | |
479 | (setq forms--mode-setup t)) | |
480 | ||
b22c9ebf | 481 | \f |
c1110355 BP |
482 | ;;; forms-process-format-list |
483 | ;;; | |
484 | ;;; Validates forms-format-list. | |
485 | ;;; | |
486 | ;;; Sets forms--number-of-markers and forms--markers. | |
487 | ||
488 | (defun forms--process-format-list () | |
489 | "Validate forms-format-list and set some global variables." | |
490 | ||
01a45313 RS |
491 | (forms--debug "forms-forms-list before 1st pass:\n" |
492 | 'forms-format-list) | |
493 | ||
c1110355 BP |
494 | ;; it must be non-nil |
495 | (or forms-format-list | |
496 | (error "'forms-format-list' has not been set")) | |
497 | ;; it must be a list ... | |
498 | (or (listp forms-format-list) | |
499 | (error "'forms-format-list' is not a list")) | |
500 | ||
501 | (setq forms--number-of-markers 0) | |
502 | ||
503 | (let ((the-list forms-format-list) ; the list of format elements | |
01a45313 | 504 | (this-item 0) ; element in list |
c1110355 BP |
505 | (field-num 0)) ; highest field number |
506 | ||
01a45313 RS |
507 | (setq forms-format-list nil) ; gonna rebuild |
508 | ||
c1110355 BP |
509 | (while the-list |
510 | ||
511 | (let ((el (car-safe the-list)) | |
512 | (rem (cdr-safe the-list))) | |
513 | ||
01a45313 RS |
514 | ;; if it is a symbol, eval it first |
515 | (if (and (symbolp el) | |
516 | (boundp el)) | |
517 | (setq el (eval el))) | |
518 | ||
c1110355 BP |
519 | (cond |
520 | ||
521 | ;; try string ... | |
522 | ((stringp el)) ; string is OK | |
523 | ||
01a45313 RS |
524 | ;; try numeric ... |
525 | ((numberp el) | |
c1110355 BP |
526 | |
527 | (if (or (<= el 0) | |
528 | (> el forms-number-of-fields)) | |
529 | (error | |
01a45313 | 530 | "Forms error: field number %d out of range 1..%d" |
c1110355 BP |
531 | el forms-number-of-fields)) |
532 | ||
533 | (setq forms--number-of-markers (1+ forms--number-of-markers)) | |
534 | (if (> el field-num) | |
535 | (setq field-num el))) | |
536 | ||
01a45313 RS |
537 | ;; try function |
538 | ((listp el) | |
539 | (or (fboundp (car-safe el)) | |
540 | (error | |
541 | "Forms error: not a function: %s" | |
542 | (prin1-to-string (car-safe el))))) | |
543 | ||
c1110355 BP |
544 | ;; else |
545 | (t | |
01a45313 RS |
546 | (error "Invalid element in 'forms-format-list': %s" |
547 | (prin1-to-string el)))) | |
c1110355 | 548 | |
01a45313 RS |
549 | ;; advance to next element of the list |
550 | (setq the-list rem) | |
551 | (setq forms-format-list | |
552 | (append forms-format-list (list el) nil))))) | |
c1110355 | 553 | |
01a45313 RS |
554 | (forms--debug "forms-forms-list after 1st pass:\n" |
555 | 'forms-format-list) | |
c1110355 | 556 | |
01a45313 RS |
557 | ;; concat adjacent strings |
558 | (setq forms-format-list (forms--concat-adjacent forms-format-list)) | |
c1110355 | 559 | |
01a45313 RS |
560 | (forms--debug "forms-forms-list after 2nd pass:\n" |
561 | 'forms-format-list | |
562 | 'forms--number-of-markers) | |
c1110355 BP |
563 | |
564 | (setq forms--markers (make-vector forms--number-of-markers nil))) | |
565 | ||
566 | ||
b22c9ebf | 567 | \f |
c1110355 BP |
568 | ;;; Build the format routine from forms-format-list. |
569 | ;;; | |
570 | ;;; The format routine (forms--format) will look like | |
571 | ;;; | |
572 | ;;; (lambda (arg) | |
01a45313 | 573 | ;;; (setq forms--dynamic-text nil) |
c1110355 BP |
574 | ;;; ;; "text: " |
575 | ;;; (insert "text: ") | |
576 | ;;; ;; 6 | |
577 | ;;; (aset forms--markers 0 (point-marker)) | |
578 | ;;; (insert (elt arg 5)) | |
579 | ;;; ;; "\nmore text: " | |
580 | ;;; (insert "\nmore text: ") | |
01a45313 RS |
581 | ;;; ;; (tocol 40) |
582 | ;;; (let ((the-dyntext (tocol 40))) | |
583 | ;;; (insert the-dyntext) | |
584 | ;;; (setq forms--dynamic-text (append forms--dynamic-text | |
585 | ;;; (list the-dyntext)))) | |
c1110355 BP |
586 | ;;; ;; 9 |
587 | ;;; (aset forms--markers 1 (point-marker)) | |
588 | ;;; (insert (elt arg 8)) | |
589 | ;;; | |
590 | ;;; ... ) | |
591 | ;;; | |
592 | ||
593 | (defun forms--make-format () | |
01a45313 RS |
594 | "Generate format function for forms" |
595 | (setq forms--format (forms--format-maker forms-format-list)) | |
596 | (forms--debug 'forms--format)) | |
c1110355 BP |
597 | |
598 | (defun forms--format-maker (the-format-list) | |
599 | "Returns the parser function for forms" | |
600 | (let ((the-marker 0)) | |
601 | (` (lambda (arg) | |
01a45313 | 602 | (setq forms--dynamic-text nil) |
c1110355 | 603 | (,@ (apply 'append |
01a45313 | 604 | (mapcar 'forms--make-format-elt the-format-list))))))) |
c1110355 BP |
605 | |
606 | (defun forms--make-format-elt (el) | |
b22c9ebf RS |
607 | (cond |
608 | ((stringp el) | |
609 | (` ((insert (, el))))) | |
610 | ((numberp el) | |
611 | (prog1 | |
612 | (` ((aset forms--markers (, the-marker) (point-marker)) | |
613 | (insert (elt arg (, (1- el)))))) | |
614 | (setq the-marker (1+ the-marker)))) | |
615 | ((listp el) | |
616 | (prog1 | |
617 | (` ((let ((the-dyntext (, el))) | |
618 | (insert the-dyntext) | |
619 | (setq forms--dynamic-text (append forms--dynamic-text | |
620 | (list the-dyntext))))) | |
621 | ))))) | |
c1110355 BP |
622 | |
623 | (defun forms--concat-adjacent (the-list) | |
624 | "Concatenate adjacent strings in the-list and return the resulting list" | |
625 | (if (consp the-list) | |
626 | (let ((the-rest (forms--concat-adjacent (cdr the-list)))) | |
627 | (if (and (stringp (car the-list)) (stringp (car the-rest))) | |
628 | (cons (concat (car the-list) (car the-rest)) | |
629 | (cdr the-rest)) | |
630 | (cons (car the-list) the-rest))) | |
631 | the-list)) | |
b22c9ebf | 632 | \f |
c1110355 BP |
633 | ;;; forms--make-parser. |
634 | ;;; | |
635 | ;;; Generate parse routine from forms-format-list. | |
636 | ;;; | |
637 | ;;; The parse routine (forms--parser) will look like (give or take | |
638 | ;;; a few " " . | |
639 | ;;; | |
640 | ;;; (lambda nil | |
641 | ;;; (let (here) | |
642 | ;;; (goto-char (point-min)) | |
643 | ;;; | |
644 | ;;; ;; "text: " | |
645 | ;;; (if (not (looking-at "text: ")) | |
01a45313 | 646 | ;;; (error "Parse error: cannot find \"text: \"")) |
c1110355 BP |
647 | ;;; (forward-char 6) ; past "text: " |
648 | ;;; | |
649 | ;;; ;; 6 | |
650 | ;;; ;; "\nmore text: " | |
651 | ;;; (setq here (point)) | |
652 | ;;; (if (not (search-forward "\nmore text: " nil t nil)) | |
01a45313 | 653 | ;;; (error "Parse error: cannot find \"\\nmore text: \"")) |
c1110355 | 654 | ;;; (aset the-recordv 5 (buffer-substring here (- (point) 12))) |
01a45313 RS |
655 | ;;; |
656 | ;;; ;; (tocol 40) | |
657 | ;;; (let ((the-dyntext (car-safe forms--dynamic-text))) | |
658 | ;;; (if (not (looking-at (regexp-quote the-dyntext))) | |
659 | ;;; (error "Parse error: not looking at \"%s\"" the-dyntext)) | |
660 | ;;; (forward-char (length the-dyntext)) | |
661 | ;;; (setq forms--dynamic-text (cdr-safe forms--dynamic-text))) | |
c1110355 BP |
662 | ;;; ... |
663 | ;;; ;; final flush (due to terminator sentinel, see below) | |
664 | ;;; (aset the-recordv 7 (buffer-substring (point) (point-max))) | |
665 | ;;; | |
666 | ||
667 | (defun forms--make-parser () | |
668 | "Generate parser function for forms" | |
01a45313 RS |
669 | (setq forms--parser (forms--parser-maker forms-format-list)) |
670 | (forms--debug 'forms--parser)) | |
c1110355 BP |
671 | |
672 | (defun forms--parser-maker (the-format-list) | |
673 | "Returns the parser function for forms" | |
674 | (let ((the-field nil) | |
675 | (seen-text nil) | |
676 | the--format-list) | |
01a45313 RS |
677 | ;; add a terminator sentinel |
678 | (setq the--format-list (append the-format-list (list nil))) | |
c1110355 BP |
679 | (` (lambda nil |
680 | (let (here) | |
681 | (goto-char (point-min)) | |
682 | (,@ (apply 'append | |
683 | (mapcar 'forms--make-parser-elt the--format-list)))))))) | |
684 | ||
685 | (defun forms--make-parser-elt (el) | |
01a45313 RS |
686 | (cond |
687 | ((stringp el) | |
688 | (prog1 | |
689 | (if the-field | |
690 | (` ((setq here (point)) | |
691 | (if (not (search-forward (, el) nil t nil)) | |
692 | (error "Parse error: cannot find \"%s\"" (, el))) | |
693 | (aset the-recordv (, (1- the-field)) | |
694 | (buffer-substring here | |
695 | (- (point) (, (length el))))))) | |
696 | (` ((if (not (looking-at (, (regexp-quote el)))) | |
697 | (error "Parse error: not looking at \"%s\"" (, el))) | |
698 | (forward-char (, (length el)))))) | |
699 | (setq seen-text t) | |
700 | (setq the-field nil))) | |
701 | ((numberp el) | |
702 | (if the-field | |
703 | (error "Cannot parse adjacent fields %d and %d" | |
704 | the-field el) | |
705 | (setq the-field el) | |
706 | nil)) | |
707 | ((null el) | |
708 | (if the-field | |
709 | (` ((aset the-recordv (, (1- the-field)) | |
710 | (buffer-substring (point) (point-max))))))) | |
711 | ((listp el) | |
712 | (prog1 | |
713 | (if the-field | |
714 | (` ((let ((here (point)) | |
715 | (the-dyntext (car-safe forms--dynamic-text))) | |
716 | (if (not (search-forward the-dyntext nil t nil)) | |
717 | (error "Parse error: cannot find \"%s\"" the-dyntext)) | |
718 | (aset the-recordv (, (1- the-field)) | |
719 | (buffer-substring here | |
720 | (- (point) (length the-dyntext)))) | |
721 | (setq forms--dynamic-text (cdr-safe forms--dynamic-text))))) | |
722 | (` ((let ((the-dyntext (car-safe forms--dynamic-text))) | |
723 | (if (not (looking-at (regexp-quote the-dyntext))) | |
724 | (error "Parse error: not looking at \"%s\"" the-dyntext)) | |
725 | (forward-char (length the-dyntext)) | |
726 | (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))))) | |
727 | (setq seen-text t) | |
728 | (setq the-field nil))) | |
729 | )) | |
b22c9ebf | 730 | \f |
c1110355 BP |
731 | |
732 | (defun forms--set-minor-mode () | |
733 | (setq minor-mode-alist | |
734 | (if forms-read-only | |
735 | " View" | |
736 | nil))) | |
737 | ||
738 | (defun forms--set-keymaps () | |
739 | "Set the keymaps used in this mode." | |
740 | ||
741 | (if forms-read-only | |
742 | (use-local-map forms-mode-map) | |
743 | (use-local-map (make-sparse-keymap)) | |
744 | (define-key (current-local-map) "\C-c" forms-mode-map) | |
745 | (define-key (current-local-map) "\t" 'forms-next-field))) | |
746 | ||
747 | (defun forms--mode-commands (map) | |
748 | "Fill map with all commands." | |
749 | (define-key map "\t" 'forms-next-field) | |
750 | (define-key map " " 'forms-next-record) | |
751 | (define-key map "d" 'forms-delete-record) | |
752 | (define-key map "e" 'forms-edit-mode) | |
753 | (define-key map "i" 'forms-insert-record) | |
754 | (define-key map "j" 'forms-jump-record) | |
755 | (define-key map "n" 'forms-next-record) | |
756 | (define-key map "p" 'forms-prev-record) | |
757 | (define-key map "q" 'forms-exit) | |
758 | (define-key map "s" 'forms-search) | |
759 | (define-key map "v" 'forms-view-mode) | |
760 | (define-key map "x" 'forms-exit-no-save) | |
761 | (define-key map "<" 'forms-first-record) | |
762 | (define-key map ">" 'forms-last-record) | |
763 | (define-key map "?" 'describe-mode) | |
764 | (define-key map "\177" 'forms-prev-record) | |
765 | ; (define-key map "\C-c" map) | |
766 | (define-key map "\e" 'ESC-prefix) | |
767 | (define-key map "\C-x" ctl-x-map) | |
768 | (define-key map "\C-u" 'universal-argument) | |
769 | (define-key map "\C-h" help-map) | |
770 | ) | |
b22c9ebf | 771 | \f |
c1110355 BP |
772 | ;;; Changed functions |
773 | ;;; | |
774 | ;;; Emacs (as of 18.55) lacks the functionality of buffer-local | |
775 | ;;; funtions. Therefore we save the original meaning of some handy | |
776 | ;;; functions, and replace them with a wrapper. | |
777 | ||
778 | (defun forms--change-commands () | |
779 | "Localize some commands." | |
780 | ;; | |
781 | ;; scroll-down -> forms-prev-record | |
782 | ;; | |
783 | (if (fboundp 'forms--scroll-down) | |
784 | nil | |
785 | (fset 'forms--scroll-down (symbol-function 'scroll-down)) | |
786 | (fset 'scroll-down | |
b22c9ebf RS |
787 | (function |
788 | (lambda (&optional arg) | |
c1110355 BP |
789 | (interactive "P") |
790 | (if (and forms--mode-setup | |
791 | forms-forms-scroll) | |
792 | (forms-prev-record arg) | |
b22c9ebf | 793 | (forms--scroll-down arg)))))) |
c1110355 BP |
794 | ;; |
795 | ;; scroll-up -> forms-next-record | |
796 | ;; | |
797 | (if (fboundp 'forms--scroll-up) | |
798 | nil | |
799 | (fset 'forms--scroll-up (symbol-function 'scroll-up)) | |
800 | (fset 'scroll-up | |
b22c9ebf RS |
801 | (function |
802 | (lambda (&optional arg) | |
c1110355 BP |
803 | (interactive "P") |
804 | (if (and forms--mode-setup | |
805 | forms-forms-scroll) | |
806 | (forms-next-record arg) | |
b22c9ebf | 807 | (forms--scroll-up arg)))))) |
c1110355 BP |
808 | ;; |
809 | ;; beginning-of-buffer -> forms-first-record | |
810 | ;; | |
811 | (if (fboundp 'forms--beginning-of-buffer) | |
812 | nil | |
813 | (fset 'forms--beginning-of-buffer (symbol-function 'beginning-of-buffer)) | |
814 | (fset 'beginning-of-buffer | |
b22c9ebf RS |
815 | (function |
816 | (lambda () | |
c1110355 BP |
817 | (interactive) |
818 | (if (and forms--mode-setup | |
819 | forms-forms-jump) | |
820 | (forms-first-record) | |
b22c9ebf | 821 | (forms--beginning-of-buffer)))))) |
c1110355 BP |
822 | ;; |
823 | ;; end-of-buffer -> forms-end-record | |
824 | ;; | |
825 | (if (fboundp 'forms--end-of-buffer) | |
826 | nil | |
827 | (fset 'forms--end-of-buffer (symbol-function 'end-of-buffer)) | |
828 | (fset 'end-of-buffer | |
b22c9ebf RS |
829 | (function |
830 | (lambda () | |
c1110355 BP |
831 | (interactive) |
832 | (if (and forms--mode-setup | |
833 | forms-forms-jump) | |
834 | (forms-last-record) | |
b22c9ebf | 835 | (forms--end-of-buffer)))))) |
c1110355 BP |
836 | ;; |
837 | ;; save-buffer -> forms--save-buffer | |
838 | ;; | |
839 | (if (fboundp 'forms--save-buffer) | |
840 | nil | |
841 | (fset 'forms--save-buffer (symbol-function 'save-buffer)) | |
842 | (fset 'save-buffer | |
b22c9ebf RS |
843 | (function |
844 | (lambda (&optional arg) | |
c1110355 BP |
845 | (interactive "p") |
846 | (if forms--mode-setup | |
847 | (progn | |
848 | (forms--checkmod) | |
849 | (save-excursion | |
850 | (set-buffer forms--file-buffer) | |
851 | (forms--save-buffer arg))) | |
b22c9ebf | 852 | (forms--save-buffer arg)))))) |
c1110355 BP |
853 | ;; |
854 | ) | |
855 | ||
856 | (defun forms--help () | |
857 | "Initial help." | |
858 | ;; We should use | |
859 | ;;(message (substitute-command-keys (concat | |
860 | ;;"\\[forms-next-record]:next" | |
861 | ;;" \\[forms-prev-record]:prev" | |
862 | ;;" \\[forms-first-record]:first" | |
863 | ;;" \\[forms-last-record]:last" | |
864 | ;;" \\[describe-mode]:help" | |
865 | ;;" \\[forms-exit]:exit"))) | |
866 | ;; but it's too slow .... | |
867 | (if forms-read-only | |
868 | (message "SPC:next DEL:prev <:first >:last ?:help q:exit") | |
869 | (message "C-c n:next C-c p:prev C-c <:first C-c >:last C-c ?:help C-c q:exit"))) | |
870 | ||
871 | (defun forms--trans (subj arg rep) | |
872 | "Translate in SUBJ all chars ARG into char REP. ARG and REP should | |
873 | be single-char strings." | |
874 | (let ((i 0) | |
875 | (x (length subj)) | |
876 | (re (regexp-quote arg)) | |
877 | (k (string-to-char rep))) | |
878 | (while (setq i (string-match re subj i)) | |
879 | (aset subj i k) | |
880 | (setq i (1+ i))))) | |
881 | ||
882 | (defun forms--exit (query &optional save) | |
883 | (let ((buf (buffer-name forms--file-buffer))) | |
884 | (forms--checkmod) | |
885 | (if (and save | |
886 | (buffer-modified-p forms--file-buffer)) | |
887 | (save-excursion | |
888 | (set-buffer forms--file-buffer) | |
889 | (save-buffer))) | |
890 | (save-excursion | |
891 | (set-buffer forms--file-buffer) | |
892 | (delete-auto-save-file-if-necessary) | |
893 | (kill-buffer (current-buffer))) | |
894 | (if (get-buffer buf) ; not killed??? | |
895 | (if save | |
896 | (progn | |
897 | (beep) | |
898 | (message "Problem saving buffers?"))) | |
899 | (delete-auto-save-file-if-necessary) | |
900 | (kill-buffer (current-buffer))))) | |
901 | ||
902 | (defun forms--get-record () | |
903 | "Fetch the current record from the file buffer." | |
904 | ;; | |
905 | ;; This function is executed in the context of the forms--file-buffer. | |
906 | ;; | |
907 | (or (bolp) | |
908 | (beginning-of-line nil)) | |
909 | (let ((here (point))) | |
910 | (prog2 | |
911 | (end-of-line) | |
912 | (buffer-substring here (point)) | |
913 | (goto-char here)))) | |
914 | ||
915 | (defun forms--show-record (the-record) | |
916 | "Format THE-RECORD according to forms-format-list, | |
917 | and display it in the current buffer." | |
918 | ||
919 | ;; split the-record | |
920 | (let (the-result | |
921 | (start-pos 0) | |
922 | found-pos | |
923 | (field-sep-length (length forms-field-sep))) | |
924 | (if forms-multi-line | |
925 | (forms--trans the-record forms-multi-line "\n")) | |
926 | ;; add an extra separator (makes splitting easy) | |
927 | (setq the-record (concat the-record forms-field-sep)) | |
928 | (while (setq found-pos (string-match forms-field-sep the-record start-pos)) | |
929 | (let ((ent (substring the-record start-pos found-pos))) | |
930 | (setq the-result | |
931 | (append the-result (list ent))) | |
932 | (setq start-pos (+ field-sep-length found-pos)))) | |
933 | (setq forms--the-record-list the-result)) | |
934 | ||
935 | (setq buffer-read-only nil) | |
936 | (erase-buffer) | |
937 | ||
938 | ;; verify the number of fields, extend forms--the-record-list if needed | |
939 | (if (= (length forms--the-record-list) forms-number-of-fields) | |
940 | nil | |
941 | (beep) | |
942 | (message "Record has %d fields instead of %d." | |
943 | (length forms--the-record-list) forms-number-of-fields) | |
944 | (if (< (length forms--the-record-list) forms-number-of-fields) | |
945 | (setq forms--the-record-list | |
946 | (append forms--the-record-list | |
947 | (make-list | |
948 | (- forms-number-of-fields | |
949 | (length forms--the-record-list)) | |
950 | ""))))) | |
951 | ||
952 | ;; call the formatter function | |
01a45313 | 953 | (setq forms-fields (append (list nil) forms--the-record-list nil)) |
c1110355 BP |
954 | (funcall forms--format forms--the-record-list) |
955 | ||
956 | ;; prepare | |
957 | (goto-char (point-min)) | |
958 | (set-buffer-modified-p nil) | |
959 | (setq buffer-read-only forms-read-only) | |
960 | (setq mode-line-process | |
961 | (concat " " forms--current-record "/" forms--total-records))) | |
962 | ||
963 | (defun forms--parse-form () | |
964 | "Parse contents of form into list of strings." | |
965 | ;; The contents of the form are parsed, and a new list of strings | |
966 | ;; is constructed. | |
967 | ;; A vector with the strings from the original record is | |
968 | ;; constructed, which is updated with the new contents. Therefore | |
969 | ;; fields which were not in the form are not modified. | |
970 | ;; Finally, the vector is transformed into a list for further processing. | |
971 | ||
972 | (let (the-recordv) | |
973 | ||
974 | ;; build the vector | |
975 | (setq the-recordv (vconcat forms--the-record-list)) | |
976 | ||
977 | ;; parse the form and update the vector | |
01a45313 RS |
978 | (let ((forms--dynamic-text forms--dynamic-text)) |
979 | (funcall forms--parser)) | |
c1110355 | 980 | |
01a45313 RS |
981 | (if forms--modified-record-filter |
982 | ;; As a service to the user, we add a zeroth element so she | |
983 | ;; can use the same indices as in the forms definition. | |
984 | (let ((the-fields (vconcat [nil] the-recordv))) | |
985 | (setq the-fields (funcall forms--modified-record-filter the-fields)) | |
986 | (cdr (append the-fields nil))) | |
987 | ||
988 | ;; transform to a list and return | |
989 | (append the-recordv nil)))) | |
c1110355 BP |
990 | |
991 | (defun forms--update () | |
992 | "Update current record with contents of form. As a side effect: sets | |
993 | forms--the-record-list ." | |
994 | (if forms-read-only | |
995 | (progn | |
996 | (message "Read-only buffer!") | |
997 | (beep)) | |
998 | ||
999 | (let (the-record) | |
1000 | ;; build new record | |
1001 | (setq forms--the-record-list (forms--parse-form)) | |
1002 | (setq the-record | |
1003 | (mapconcat 'identity forms--the-record-list forms-field-sep)) | |
1004 | ||
1005 | ;; handle multi-line fields, if allowed | |
1006 | (if forms-multi-line | |
1007 | (forms--trans the-record "\n" forms-multi-line)) | |
1008 | ||
1009 | ;; a final sanity check before updating | |
1010 | (if (string-match "\n" the-record) | |
1011 | (progn | |
1012 | (message "Multi-line fields in this record - update refused!") | |
1013 | (beep)) | |
1014 | ||
1015 | (save-excursion | |
1016 | (set-buffer forms--file-buffer) | |
1017 | ;; Insert something before kill-line is called. See kill-line | |
1018 | ;; doc. Bugfix provided by Ignatios Souvatzis. | |
1019 | (insert "*") | |
1020 | (beginning-of-line) | |
1021 | (kill-line nil) | |
1022 | (insert the-record) | |
1023 | (beginning-of-line)))))) | |
1024 | ||
1025 | (defun forms--checkmod () | |
1026 | "Check if this form has been modified, and call forms--update if so." | |
1027 | (if (buffer-modified-p nil) | |
1028 | (let ((here (point))) | |
1029 | (forms--update) | |
1030 | (set-buffer-modified-p nil) | |
1031 | (goto-char here)))) | |
1032 | ||
b22c9ebf | 1033 | \f |
c1110355 BP |
1034 | ;;; Start and exit |
1035 | (defun forms-find-file (fn) | |
1036 | "Visit file FN in forms mode" | |
1037 | (interactive "fForms file: ") | |
1038 | (find-file-read-only fn) | |
1039 | (or forms--mode-setup (forms-mode t))) | |
1040 | ||
1041 | (defun forms-find-file-other-window (fn) | |
1042 | "Visit file FN in form mode in other window" | |
1043 | (interactive "fFbrowse file in other window: ") | |
1044 | (find-file-other-window fn) | |
1045 | (eval-current-buffer) | |
1046 | (or forms--mode-setup (forms-mode t))) | |
1047 | ||
1048 | (defun forms-exit (query) | |
1049 | "Normal exit. Modified buffers are saved." | |
1050 | (interactive "P") | |
1051 | (forms--exit query t)) | |
1052 | ||
1053 | (defun forms-exit-no-save (query) | |
1054 | "Exit without saving buffers." | |
1055 | (interactive "P") | |
1056 | (forms--exit query nil)) | |
1057 | ||
b22c9ebf | 1058 | \f |
c1110355 BP |
1059 | ;;; Navigating commands |
1060 | ||
1061 | (defun forms-next-record (arg) | |
1062 | "Advance to the ARGth following record." | |
1063 | (interactive "P") | |
1064 | (forms-jump-record (+ forms--current-record (prefix-numeric-value arg)) t)) | |
1065 | ||
1066 | (defun forms-prev-record (arg) | |
1067 | "Advance to the ARGth previous record." | |
1068 | (interactive "P") | |
1069 | (forms-jump-record (- forms--current-record (prefix-numeric-value arg)) t)) | |
1070 | ||
1071 | (defun forms-jump-record (arg &optional relative) | |
1072 | "Jump to a random record." | |
1073 | (interactive "NRecord number: ") | |
1074 | ||
1075 | ;; verify that the record number is within range | |
1076 | (if (or (> arg forms--total-records) | |
1077 | (<= arg 0)) | |
1078 | (progn | |
1079 | (beep) | |
1080 | ;; don't give the message if just paging | |
1081 | (if (not relative) | |
1082 | (message "Record number %d out of range 1..%d" | |
1083 | arg forms--total-records)) | |
1084 | ) | |
1085 | ||
1086 | ;; flush | |
1087 | (forms--checkmod) | |
1088 | ||
1089 | ;; calculate displacement | |
1090 | (let ((disp (- arg forms--current-record)) | |
1091 | (cur forms--current-record)) | |
1092 | ||
1093 | ;; forms--show-record needs it now | |
1094 | (setq forms--current-record arg) | |
1095 | ||
1096 | ;; get the record and show it | |
1097 | (forms--show-record | |
1098 | (save-excursion | |
1099 | (set-buffer forms--file-buffer) | |
1100 | (beginning-of-line) | |
1101 | ||
1102 | ;; move, and adjust the amount if needed (shouldn't happen) | |
1103 | (if relative | |
1104 | (if (zerop disp) | |
1105 | nil | |
1106 | (setq cur (+ cur disp (- (forward-line disp))))) | |
1107 | (setq cur (+ cur disp (- (goto-line arg))))) | |
1108 | ||
1109 | (forms--get-record))) | |
1110 | ||
1111 | ;; this shouldn't happen | |
1112 | (if (/= forms--current-record cur) | |
1113 | (progn | |
1114 | (setq forms--current-record cur) | |
1115 | (beep) | |
1116 | (message "Stuck at record %d." cur)))))) | |
1117 | ||
1118 | (defun forms-first-record () | |
1119 | "Jump to first record." | |
1120 | (interactive) | |
1121 | (forms-jump-record 1)) | |
1122 | ||
1123 | (defun forms-last-record () | |
1124 | "Jump to last record. As a side effect: re-calculates the number | |
1125 | of records in the data file." | |
1126 | (interactive) | |
1127 | (let | |
1128 | ((numrec | |
1129 | (save-excursion | |
1130 | (set-buffer forms--file-buffer) | |
1131 | (count-lines (point-min) (point-max))))) | |
1132 | (if (= numrec forms--total-records) | |
1133 | nil | |
1134 | (beep) | |
1135 | (setq forms--total-records numrec) | |
1136 | (message "Number of records reset to %d." forms--total-records))) | |
1137 | (forms-jump-record forms--total-records)) | |
1138 | ||
b22c9ebf | 1139 | \f |
c1110355 BP |
1140 | ;;; Other commands |
1141 | (defun forms-view-mode () | |
1142 | "Visit buffer read-only." | |
1143 | (interactive) | |
1144 | (if forms-read-only | |
1145 | nil | |
1146 | (forms--checkmod) ; sync | |
1147 | (setq forms-read-only t) | |
1148 | (forms-mode))) | |
1149 | ||
1150 | (defun forms-edit-mode () | |
1151 | "Make form suitable for editing, if possible." | |
1152 | (interactive) | |
1153 | (let ((ro forms-read-only)) | |
1154 | (if (save-excursion | |
1155 | (set-buffer forms--file-buffer) | |
1156 | buffer-read-only) | |
1157 | (progn | |
1158 | (setq forms-read-only t) | |
1159 | (message "No write access to \"%s\"" forms-file) | |
1160 | (beep)) | |
1161 | (setq forms-read-only nil)) | |
1162 | (if (equal ro forms-read-only) | |
1163 | nil | |
1164 | (forms-mode)))) | |
1165 | ||
1166 | ;; Sample: | |
01a45313 | 1167 | ;; (defun my-new-record-filter (the-fields) |
c1110355 BP |
1168 | ;; ;; numbers are relative to 1 |
1169 | ;; (aset the-fields 4 (current-time-string)) | |
1170 | ;; (aset the-fields 6 (user-login-name)) | |
1171 | ;; the-list) | |
01a45313 | 1172 | ;; (setq forms-new-record-filter 'my-new-record-filter) |
c1110355 BP |
1173 | |
1174 | (defun forms-insert-record (arg) | |
1175 | "Create a new record before the current one. With ARG: store the | |
1176 | record after the current one. | |
01a45313 RS |
1177 | If a function forms-new-record-filter is defined, or forms-new-record-filter |
1178 | contains the name of a function, it is called to | |
c1110355 BP |
1179 | fill (some of) the fields with default values." |
1180 | ; The above doc is not true, but for documentary purposes only | |
1181 | ||
1182 | (interactive "P") | |
1183 | ||
1184 | (let ((ln (if arg (1+ forms--current-record) forms--current-record)) | |
1185 | the-list the-record) | |
1186 | ||
1187 | (forms--checkmod) | |
1188 | (if forms--new-record-filter | |
1189 | ;; As a service to the user, we add a zeroth element so she | |
1190 | ;; can use the same indices as in the forms definition. | |
1191 | (let ((the-fields (make-vector (1+ forms-number-of-fields) ""))) | |
1192 | (setq the-fields (funcall forms--new-record-filter the-fields)) | |
1193 | (setq the-list (cdr (append the-fields nil)))) | |
1194 | (setq the-list (make-list forms-number-of-fields ""))) | |
1195 | ||
1196 | (setq the-record | |
1197 | (mapconcat | |
1198 | 'identity | |
1199 | the-list | |
1200 | forms-field-sep)) | |
1201 | ||
1202 | (save-excursion | |
1203 | (set-buffer forms--file-buffer) | |
1204 | (goto-line ln) | |
1205 | (open-line 1) | |
1206 | (insert the-record) | |
1207 | (beginning-of-line)) | |
1208 | ||
1209 | (setq forms--current-record ln)) | |
1210 | ||
1211 | (setq forms--total-records (1+ forms--total-records)) | |
1212 | (forms-jump-record forms--current-record)) | |
1213 | ||
1214 | (defun forms-delete-record (arg) | |
1215 | "Deletes a record. With ARG: don't ask." | |
1216 | (interactive "P") | |
1217 | (forms--checkmod) | |
1218 | (if (or arg | |
1219 | (y-or-n-p "Really delete this record? ")) | |
1220 | (let ((ln forms--current-record)) | |
1221 | (save-excursion | |
1222 | (set-buffer forms--file-buffer) | |
1223 | (goto-line ln) | |
1224 | (kill-line 1)) | |
1225 | (setq forms--total-records (1- forms--total-records)) | |
1226 | (if (> forms--current-record forms--total-records) | |
1227 | (setq forms--current-record forms--total-records)) | |
1228 | (forms-jump-record forms--current-record))) | |
1229 | (message "")) | |
1230 | ||
1231 | (defun forms-search (regexp) | |
1232 | "Search REGEXP in file buffer." | |
1233 | (interactive | |
1234 | (list (read-string (concat "Search for" | |
1235 | (if forms--search-regexp | |
1236 | (concat " (" | |
1237 | forms--search-regexp | |
1238 | ")")) | |
1239 | ": ")))) | |
1240 | (if (equal "" regexp) | |
1241 | (setq regexp forms--search-regexp)) | |
1242 | (forms--checkmod) | |
1243 | ||
1244 | (let (the-line the-record here | |
1245 | (fld-sep forms-field-sep)) | |
1246 | (if (save-excursion | |
1247 | (set-buffer forms--file-buffer) | |
1248 | (setq here (point)) | |
1249 | (end-of-line) | |
1250 | (if (null (re-search-forward regexp nil t)) | |
1251 | (progn | |
1252 | (goto-char here) | |
1253 | (message (concat "\"" regexp "\" not found.")) | |
1254 | nil) | |
1255 | (setq the-record (forms--get-record)) | |
1256 | (setq the-line (1+ (count-lines (point-min) (point)))))) | |
1257 | (progn | |
1258 | (setq forms--current-record the-line) | |
1259 | (forms--show-record the-record) | |
1260 | (re-search-forward regexp nil t)))) | |
1261 | (setq forms--search-regexp regexp)) | |
1262 | ||
1263 | (defun forms-revert-buffer (&optional arg noconfirm) | |
1264 | "Reverts current form to un-modified." | |
1265 | (interactive "P") | |
1266 | (if (or noconfirm | |
1267 | (yes-or-no-p "Revert form to unmodified? ")) | |
1268 | (progn | |
1269 | (set-buffer-modified-p nil) | |
1270 | (forms-jump-record forms--current-record)))) | |
1271 | ||
1272 | (defun forms-next-field (arg) | |
1273 | "Jump to ARG-th next field." | |
1274 | (interactive "p") | |
1275 | ||
1276 | (let ((i 0) | |
1277 | (here (point)) | |
1278 | there | |
1279 | (cnt 0)) | |
1280 | ||
1281 | (if (zerop arg) | |
1282 | (setq cnt 1) | |
1283 | (setq cnt (+ cnt arg))) | |
1284 | ||
1285 | (if (catch 'done | |
1286 | (while (< i forms--number-of-markers) | |
1287 | (if (or (null (setq there (aref forms--markers i))) | |
1288 | (<= there here)) | |
1289 | nil | |
1290 | (if (<= (setq cnt (1- cnt)) 0) | |
1291 | (progn | |
1292 | (goto-char there) | |
1293 | (throw 'done t)))) | |
1294 | (setq i (1+ i)))) | |
1295 | nil | |
1296 | (goto-char (aref forms--markers 0))))) | |
01a45313 RS |
1297 | |
1298 | ;;; | |
1299 | ;;; Special service | |
1300 | ;;; | |
1301 | (defun forms-enumerate (the-fields) | |
1302 | "Take a quoted list of symbols, and set their values to the numbers | |
1303 | 1, 2 and so on. Returns the higest number. | |
1304 | ||
1305 | Usage: (setq forms-number-of-fields | |
1306 | (forms-enumerate | |
1307 | '(field1 field2 field2 ...)))" | |
1308 | ||
1309 | (let ((the-index 0)) | |
1310 | (while the-fields | |
1311 | (setq the-index (1+ the-index)) | |
1312 | (let ((el (car-safe the-fields))) | |
1313 | (setq the-fields (cdr-safe the-fields)) | |
1314 | (set el the-index))) | |
1315 | the-index)) | |
b22c9ebf | 1316 | \f |
01a45313 RS |
1317 | ;;; Debugging |
1318 | ;;; | |
1319 | (defvar forms--debug nil | |
1320 | "*Enables forms-mode debugging if not nil.") | |
1321 | ||
1322 | (defun forms--debug (&rest args) | |
1323 | "Internal - debugging routine" | |
1324 | (if forms--debug | |
1325 | (let ((ret nil)) | |
1326 | (while args | |
1327 | (let ((el (car-safe args))) | |
1328 | (setq args (cdr-safe args)) | |
1329 | (if (stringp el) | |
1330 | (setq ret (concat ret el)) | |
1331 | (setq ret (concat ret (prin1-to-string el) " = ")) | |
1332 | (if (boundp el) | |
1333 | (let ((vel (eval el))) | |
1334 | (setq ret (concat ret (prin1-to-string vel) "\n"))) | |
1335 | (setq ret (concat ret "<unbound>" "\n"))) | |
1336 | (if (fboundp el) | |
1337 | (setq ret (concat ret (prin1-to-string (symbol-function el)) | |
1338 | "\n")))))) | |
1339 | (save-excursion | |
1340 | (set-buffer (get-buffer-create "*forms-mode debug*")) | |
1341 | (goto-char (point-max)) | |
1342 | (insert ret))))) | |
1343 | ||
b22c9ebf | 1344 | ;;; Disabled Local Variables: |
01a45313 RS |
1345 | ;;; eval: (headers) |
1346 | ;;; eval: (setq comment-start ";;; ") | |
1347 | ;;; End: | |
b22c9ebf RS |
1348 | |
1349 | ;;; forms.el ends here. |