forms.el 67 KB

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