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