Mercurial > emacs
annotate lisp/forms.el @ 3711:5acf09035d16
(shut_down_emacs): New arg STUFF. Stuff input here.
(Fkill_emacs, fatal_error_signal): Pass new arg.
(main) [GETPGRP_NO_ARG]: Really pass no arg to getpgrp.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 13 Jun 1993 02:06:51 +0000 |
parents | 994bb6dc9249 |
children | 4089cf0e3f06 |
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. |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
2 ;;; Copyright (C) 1991 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 ;;; | |
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. | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
215 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
216 ;;; Code: |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
217 |
276 | 218 ;;; Global variables and constants |
219 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
220 (provide 'forms) ;;; official |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
221 (provide 'forms-mode) ;;; for compatibility |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
222 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
223 (defconst forms-version "1.2.10" |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
224 "Version of forms-mode implementation.") |
276 | 225 |
226 (defvar forms-forms-scrolls t | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
227 "If non-null: redefine scroll-up/down to be used with Forms mode.") |
276 | 228 |
229 (defvar forms-forms-jumps t | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
230 "If non-null: redefine beginning/end-of-buffer to be used with Forms mode.") |
276 | 231 |
232 (defvar forms-mode-hooks nil | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
233 "Hook functions to be run upon entering Forms mode.") |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
234 |
276 | 235 ;;; Mandatory variables - must be set by evaluating the control file |
236 | |
237 (defvar forms-file nil | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
238 "Name of the file holding the data.") |
276 | 239 |
240 (defvar forms-format-list nil | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
241 "List of formatting specifications.") |
276 | 242 |
243 (defvar forms-number-of-fields nil | |
244 "Number of fields per record.") | |
245 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
246 |
276 | 247 ;;; Optional variables with default values |
248 | |
249 (defvar forms-field-sep "\t" | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
250 "Field separator character (default TAB).") |
276 | 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" | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
256 "Character to separate multi-line fields (default C-k)") |
276 | 257 |
258 (defvar forms-forms-scroll t | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
259 "Redefine scroll-up/down to perform forms-next/prev-record in Forms mode.") |
276 | 260 |
261 (defvar forms-forms-jump t | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
262 "Redefine beginning/end-of-buffer to perform forms-first/last-record in Forms mode.") |
276 | 263 |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
264 |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
304 (defvar forms--modified-record-filter nil |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
305 "Internal - set if a modified record filter has been defined.") |
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--dynamic-text nil |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
308 "Internal - holds dynamic text to insert between fields.") |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
309 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
310 (defvar forms-fields nil |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
311 "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
|
312 |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
313 (defvar forms-new-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 new record is created.") |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
315 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
316 (defvar forms-modified-record-filter |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
317 "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
|
318 |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
389 ;; check if record filters are defined |
276 | 390 (make-local-variable 'forms--new-record-filter) |
391 (setq forms--new-record-filter | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
392 (cond |
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 (symbol-function 'forms-new-record-filter)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
395 ((and (boundp 'forms-new-record-filter) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
396 (fboundp forms-new-record-filter)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
397 forms-new-record-filter))) |
276 | 398 (fmakunbound 'forms-new-record-filter) |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
399 (make-local-variable 'forms--modified-record-filter) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
400 (setq forms--modified-record-filter |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
401 (cond |
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 (symbol-function 'forms-modified-record-filter)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
404 ((and (boundp 'forms-modified-record-filter) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
405 (fboundp forms-modified-record-filter)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
406 forms-modified-record-filter))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
407 (fmakunbound 'forms-modified-record-filter) |
276 | 408 |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
409 ;; dynamic text support |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
410 (make-local-variable 'forms--dynamic-text) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
411 (make-local-variable 'forms-fields) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
423 ;; Make more local variables |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
481 |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
491 (forms--debug "forms-forms-list before 1st pass:\n" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
492 'forms-format-list) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
493 |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
504 (this-item 0) ; element in list |
276 | 505 (field-num 0)) ; highest field number |
506 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
507 (setq forms-format-list nil) ; gonna rebuild |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
508 |
276 | 509 (while the-list |
510 | |
511 (let ((el (car-safe the-list)) | |
512 (rem (cdr-safe the-list))) | |
513 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
514 ;; if it is a symbol, eval it first |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
515 (if (and (symbolp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
516 (boundp el)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
517 (setq el (eval el))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
518 |
276 | 519 (cond |
520 | |
521 ;; try string ... | |
522 ((stringp el)) ; string is OK | |
523 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
524 ;; try numeric ... |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
525 ((numberp el) |
276 | 526 |
527 (if (or (<= el 0) | |
528 (> el forms-number-of-fields)) | |
529 (error | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
530 "Forms error: field number %d out of range 1..%d" |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
537 ;; try function |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
538 ((listp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
539 (or (fboundp (car-safe el)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
540 (error |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
541 "Forms error: not a function: %s" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
542 (prin1-to-string (car-safe el))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
543 |
276 | 544 ;; else |
545 (t | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
546 (error "Invalid element in 'forms-format-list': %s" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
547 (prin1-to-string el)))) |
276 | 548 |
549 ;; advance to next element of the list | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
550 (setq the-list rem) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
551 (setq forms-format-list |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
552 (append forms-format-list (list el) nil))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
553 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
554 (forms--debug "forms-forms-list after 1st pass:\n" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
555 'forms-format-list) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
556 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
557 ;; concat adjacent strings |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
558 (setq forms-format-list (forms--concat-adjacent forms-format-list)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
559 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
560 (forms--debug "forms-forms-list after 2nd pass:\n" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
561 'forms-format-list |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
562 'forms--number-of-markers) |
276 | 563 |
564 (setq forms--markers (make-vector forms--number-of-markers nil))) | |
565 | |
566 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
567 |
276 | 568 ;;; Build the format routine from forms-format-list. |
569 ;;; | |
570 ;;; The format routine (forms--format) will look like | |
571 ;;; | |
572 ;;; (lambda (arg) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
573 ;;; (setq forms--dynamic-text nil) |
276 | 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: ") | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
581 ;;; ;; (tocol 40) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
582 ;;; (let ((the-dyntext (tocol 40))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
583 ;;; (insert the-dyntext) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
584 ;;; (setq forms--dynamic-text (append forms--dynamic-text |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
585 ;;; (list the-dyntext)))) |
276 | 586 ;;; ;; 9 |
587 ;;; (aset forms--markers 1 (point-marker)) | |
588 ;;; (insert (elt arg 8)) | |
589 ;;; | |
590 ;;; ... ) | |
591 ;;; | |
592 | |
593 (defun forms--make-format () | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
594 "Generate format function for forms" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
595 (setq forms--format (forms--format-maker forms-format-list)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
596 (forms--debug 'forms--format)) |
276 | 597 |
598 (defun forms--format-maker (the-format-list) | |
599 "Returns the parser function for forms" | |
600 (let ((the-marker 0)) | |
601 (` (lambda (arg) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
602 (setq forms--dynamic-text nil) |
276 | 603 (,@ (apply 'append |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
604 (mapcar 'forms--make-format-elt the-format-list))))))) |
276 | 605 |
606 (defun forms--make-format-elt (el) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
607 (cond |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
608 ((stringp el) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
609 (` ((insert (, el))))) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
610 ((numberp el) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
611 (prog1 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
612 (` ((aset forms--markers (, the-marker) (point-marker)) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
613 (insert (elt arg (, (1- el)))))) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
614 (setq the-marker (1+ the-marker)))) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
615 ((listp el) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
616 (prog1 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
617 (` ((let ((the-dyntext (, el))) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
618 (insert the-dyntext) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
619 (setq forms--dynamic-text (append forms--dynamic-text |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
620 (list the-dyntext))))) |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
621 ))))) |
276 | 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)) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
632 |
276 | 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: ")) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
646 ;;; (error "Parse error: cannot find \"text: \"")) |
276 | 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)) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
653 ;;; (error "Parse error: cannot find \"\\nmore text: \"")) |
276 | 654 ;;; (aset the-recordv 5 (buffer-substring here (- (point) 12))) |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
655 ;;; |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
656 ;;; ;; (tocol 40) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
657 ;;; (let ((the-dyntext (car-safe forms--dynamic-text))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
658 ;;; (if (not (looking-at (regexp-quote the-dyntext))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
659 ;;; (error "Parse error: not looking at \"%s\"" the-dyntext)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
660 ;;; (forward-char (length the-dyntext)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
661 ;;; (setq forms--dynamic-text (cdr-safe forms--dynamic-text))) |
276 | 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" | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
669 (setq forms--parser (forms--parser-maker forms-format-list)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
670 (forms--debug 'forms--parser)) |
276 | 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) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
677 ;; add a terminator sentinel |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
678 (setq the--format-list (append the-format-list (list nil))) |
276 | 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) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
686 (cond |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
687 ((stringp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
688 (prog1 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
689 (if the-field |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
690 (` ((setq here (point)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
691 (if (not (search-forward (, el) nil t nil)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
692 (error "Parse error: cannot find \"%s\"" (, el))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
693 (aset the-recordv (, (1- the-field)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
694 (buffer-substring here |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
695 (- (point) (, (length el))))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
696 (` ((if (not (looking-at (, (regexp-quote el)))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
697 (error "Parse error: not looking at \"%s\"" (, el))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
698 (forward-char (, (length el)))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
699 (setq seen-text t) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
700 (setq the-field nil))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
701 ((numberp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
702 (if the-field |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
703 (error "Cannot parse adjacent fields %d and %d" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
704 the-field el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
705 (setq the-field el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
706 nil)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
707 ((null el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
708 (if the-field |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
709 (` ((aset the-recordv (, (1- the-field)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
710 (buffer-substring (point) (point-max))))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
711 ((listp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
712 (prog1 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
713 (if the-field |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
714 (` ((let ((here (point)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
715 (the-dyntext (car-safe forms--dynamic-text))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
716 (if (not (search-forward the-dyntext nil t nil)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
717 (error "Parse error: cannot find \"%s\"" the-dyntext)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
718 (aset the-recordv (, (1- the-field)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
719 (buffer-substring here |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
720 (- (point) (length the-dyntext)))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
721 (setq forms--dynamic-text (cdr-safe forms--dynamic-text))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
722 (` ((let ((the-dyntext (car-safe forms--dynamic-text))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
723 (if (not (looking-at (regexp-quote the-dyntext))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
724 (error "Parse error: not looking at \"%s\"" the-dyntext)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
725 (forward-char (length the-dyntext)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
726 (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
727 (setq seen-text t) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
728 (setq the-field nil))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
729 )) |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
730 |
276 | 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 ) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
771 |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
787 (function |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
788 (lambda (&optional arg) |
276 | 789 (interactive "P") |
790 (if (and forms--mode-setup | |
791 forms-forms-scroll) | |
792 (forms-prev-record arg) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
793 (forms--scroll-down arg)))))) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
801 (function |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
802 (lambda (&optional arg) |
276 | 803 (interactive "P") |
804 (if (and forms--mode-setup | |
805 forms-forms-scroll) | |
806 (forms-next-record arg) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
807 (forms--scroll-up arg)))))) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
815 (function |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
816 (lambda () |
276 | 817 (interactive) |
818 (if (and forms--mode-setup | |
819 forms-forms-jump) | |
820 (forms-first-record) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
821 (forms--beginning-of-buffer)))))) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
829 (function |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
830 (lambda () |
276 | 831 (interactive) |
832 (if (and forms--mode-setup | |
833 forms-forms-jump) | |
834 (forms-last-record) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
835 (forms--end-of-buffer)))))) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
843 (function |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
844 (lambda (&optional arg) |
276 | 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))) | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
852 (forms--save-buffer arg)))))) |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
953 (setq forms-fields (append (list nil) forms--the-record-list nil)) |
276 | 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 | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
978 (let ((forms--dynamic-text forms--dynamic-text)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
979 (funcall forms--parser)) |
276 | 980 |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
981 (if forms--modified-record-filter |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
982 ;; 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
|
983 ;; can use the same indices as in the forms definition. |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
984 (let ((the-fields (vconcat [nil] the-recordv))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
985 (setq the-fields (funcall forms--modified-record-filter the-fields)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
986 (cdr (append the-fields nil))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
987 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
988 ;; transform to a list and return |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
989 (append the-recordv nil)))) |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1033 |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1058 |
276 | 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 | |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1139 |
276 | 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: | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1167 ;; (defun my-new-record-filter (the-fields) |
276 | 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) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1172 ;; (setq forms-new-record-filter 'my-new-record-filter) |
276 | 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. | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1177 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
|
1178 contains the name of a function, it is called to |
276 | 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))))) | |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1297 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1298 ;;; |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1299 ;;; Special service |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1300 ;;; |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1301 (defun forms-enumerate (the-fields) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1302 "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
|
1303 1, 2 and so on. Returns the higest number. |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1304 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1305 Usage: (setq forms-number-of-fields |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1306 (forms-enumerate |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1307 '(field1 field2 field2 ...)))" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1308 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1309 (let ((the-index 0)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1310 (while the-fields |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1311 (setq the-index (1+ the-index)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1312 (let ((el (car-safe the-fields))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1313 (setq the-fields (cdr-safe the-fields)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1314 (set el the-index))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1315 the-index)) |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1316 |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1317 ;;; Debugging |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1318 ;;; |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1319 (defvar forms--debug nil |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1320 "*Enables forms-mode debugging if not nil.") |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1321 |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1322 (defun forms--debug (&rest args) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1323 "Internal - debugging routine" |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1324 (if forms--debug |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1325 (let ((ret nil)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1326 (while args |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1327 (let ((el (car-safe args))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1328 (setq args (cdr-safe args)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1329 (if (stringp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1330 (setq ret (concat ret el)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1331 (setq ret (concat ret (prin1-to-string el) " = ")) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1332 (if (boundp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1333 (let ((vel (eval el))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1334 (setq ret (concat ret (prin1-to-string vel) "\n"))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1335 (setq ret (concat ret "<unbound>" "\n"))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1336 (if (fboundp el) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1337 (setq ret (concat ret (prin1-to-string (symbol-function el)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1338 "\n")))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1339 (save-excursion |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1340 (set-buffer (get-buffer-create "*forms-mode debug*")) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1341 (goto-char (point-max)) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1342 (insert ret))))) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1343 |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1344 ;;; Disabled Local Variables: |
307
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1345 ;;; eval: (headers) |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1346 ;;; eval: (setq comment-start ";;; ") |
7fede845e304
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
276
diff
changeset
|
1347 ;;; End: |
3697
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1348 |
994bb6dc9249
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
307
diff
changeset
|
1349 ;;; forms.el ends here. |