forms.el 68 KB

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