forms.el 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. ;;; forms.el --- Forms mode: edit a file as a form to fill in
  2. ;; Copyright (C) 1991, 1994-1997, 2001-2015 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. (set-visited-file-name nil)
  522. ;; Prepare this buffer for further processing.
  523. (setq buffer-read-only nil)
  524. (erase-buffer)
  525. ;;(message "forms: setting up... done.")
  526. ))
  527. ;; initialization done
  528. (setq forms--mode-setup t)
  529. ;; Copy desired faces to the actual variables used by the forms formatter.
  530. (if (fboundp 'make-face)
  531. (progn
  532. (make-local-variable 'forms--ro-face)
  533. (make-local-variable 'forms--rw-face)
  534. (if forms-read-only
  535. (progn
  536. (setq forms--ro-face forms-ro-face)
  537. (setq forms--rw-face forms-ro-face))
  538. (setq forms--ro-face forms-ro-face)
  539. (setq forms--rw-face forms-rw-face))))
  540. ;; Make more local variables.
  541. (make-local-variable 'forms--file-buffer)
  542. (make-local-variable 'forms--total-records)
  543. (make-local-variable 'forms--current-record)
  544. (make-local-variable 'forms--the-record-list)
  545. (make-local-variable 'forms--search-regexp)
  546. ; The keymaps are global, so multiple forms mode buffers can share them.
  547. ;(make-local-variable 'forms-mode-map)
  548. ;(make-local-variable 'forms-mode-ro-map)
  549. ;(make-local-variable 'forms-mode-edit-map)
  550. (if forms-mode-map ; already defined
  551. nil
  552. ;;(message "forms: building keymap...")
  553. (forms--mode-commands)
  554. ;;(message "forms: building keymap... done.")
  555. )
  556. ;; set the major mode indicator
  557. (setq major-mode 'forms-mode)
  558. (setq mode-name "Forms")
  559. (cursor-intangible-mode 1)
  560. ;; find the data file
  561. (setq forms--file-buffer (find-file-noselect forms-file))
  562. ;; Pre-transform.
  563. (let ((read-file-filter forms-read-file-filter)
  564. (write-file-filter forms-write-file-filter))
  565. (if read-file-filter
  566. (with-current-buffer forms--file-buffer
  567. (let ((inhibit-read-only t)
  568. (file-modified (buffer-modified-p)))
  569. (mapc #'funcall read-file-filter)
  570. (if (not file-modified) (set-buffer-modified-p nil)))
  571. (if write-file-filter
  572. (add-hook 'write-file-functions write-file-filter nil t)))
  573. (if write-file-filter
  574. (with-current-buffer forms--file-buffer
  575. (add-hook 'write-file-functions write-file-filter nil t)))))
  576. ;; count the number of records, and set see if it may be modified
  577. (let (ro)
  578. (setq forms--total-records
  579. (with-current-buffer forms--file-buffer
  580. (prog1
  581. (progn
  582. ;;(message "forms: counting records...")
  583. (bury-buffer (current-buffer))
  584. (setq ro buffer-read-only)
  585. (count-lines (point-min) (point-max)))
  586. ;;(message "forms: counting records... done.")
  587. )))
  588. (if ro
  589. (setq forms-read-only t)))
  590. ;;(message "forms: proceeding setup...")
  591. ;; Since we aren't really implementing a minor mode, we hack the mode line
  592. ;; directly to get the text " View " into forms-read-only form buffers. For
  593. ;; that reason, this variable must be buffer only.
  594. (make-local-variable 'minor-mode-alist)
  595. (setq minor-mode-alist (list (list 'forms-read-only " View")))
  596. ;;(message "forms: proceeding setup (keymaps)...")
  597. (forms--set-keymaps)
  598. ;;(message "forms: proceeding setup (commands)...")
  599. (forms--change-commands)
  600. ;;(message "forms: proceeding setup (buffer)...")
  601. (set-buffer-modified-p nil)
  602. (if (= forms--total-records 0)
  603. ;;(message "forms: proceeding setup (new file)...")
  604. (progn
  605. (insert
  606. "GNU Emacs Forms Mode\n\n"
  607. (if (file-exists-p forms-file)
  608. (format-message
  609. "No records available in file `%s'\n\n" forms-file)
  610. (format-message
  611. "Creating new file `%s'\nwith %d field%s per record\n\n"
  612. forms-file forms-number-of-fields
  613. (if (= 1 forms-number-of-fields) "" "s")))
  614. "Use " (substitute-command-keys "\\[forms-insert-record]")
  615. " to create new records.\n")
  616. (setq forms--current-record 1)
  617. (setq buffer-read-only t)
  618. (set-buffer-modified-p nil))
  619. ;; setup the first (or current) record to show
  620. (if (< forms--current-record 1)
  621. (setq forms--current-record 1))
  622. (forms-jump-record forms--current-record)
  623. (if forms-insert-after
  624. (forms-last-record)
  625. (forms-first-record))
  626. )
  627. ;; user customizing
  628. ;;(message "forms: proceeding setup (user hooks)...")
  629. (run-mode-hooks 'forms-mode-hook 'forms-mode-hooks)
  630. ;;(message "forms: setting up... done.")
  631. ;; be helpful
  632. (forms--help)
  633. )
  634. (defun forms--process-format-list ()
  635. ;; Validate `forms-format-list' and set some global variables.
  636. ;; Symbols in the list are evaluated, and consecutive strings are
  637. ;; concatenated.
  638. ;; Array `forms--elements' is constructed that contains the order
  639. ;; of the fields on the display. This array is used by
  640. ;; `forms--parser-using-text-properties' to extract the fields data
  641. ;; from the form on the screen.
  642. ;; Upon completion, `forms-format-list' is guaranteed correct, so
  643. ;; `forms--make-format' and `forms--make-parser' do not need to perform
  644. ;; any checks.
  645. ;; Verify that `forms-format-list' is not nil.
  646. (or forms-format-list
  647. (error (concat "Forms control file error: "
  648. "`forms-format-list' has not been set")))
  649. ;; It must be a list.
  650. (or (listp forms-format-list)
  651. (error (concat "Forms control file error: "
  652. "`forms-format-list' is not a list")))
  653. ;; Assume every field is painted once.
  654. ;; `forms--elements' will grow if needed.
  655. (setq forms--elements (make-vector forms-number-of-fields nil))
  656. (let ((the-list forms-format-list) ; the list of format elements
  657. (prev-item nil)
  658. (field-num 0)) ; highest field number
  659. (setq forms-format-list nil) ; gonna rebuild
  660. (while the-list
  661. (let ((el (car-safe the-list))
  662. (rem (cdr-safe the-list)))
  663. ;; If it is a symbol, eval it first.
  664. (if (and (symbolp el)
  665. (boundp el))
  666. (setq el (eval el)))
  667. (cond
  668. ;; Try string ...
  669. ((stringp el)
  670. (if (stringp prev-item) ; try to concatenate strings
  671. (setq prev-item (concat prev-item el))
  672. (if prev-item
  673. (setq forms-format-list
  674. (append forms-format-list (list prev-item) nil)))
  675. (setq prev-item el)))
  676. ;; Try numeric ...
  677. ((numberp el)
  678. ;; Validate range.
  679. (if (or (<= el 0)
  680. (> el forms-number-of-fields))
  681. (error (concat "Forms format error: "
  682. "field number %d out of range 1..%d")
  683. el forms-number-of-fields))
  684. ;; Store forms order.
  685. (if (>= field-num (length forms--elements))
  686. (setq forms--elements (vconcat forms--elements (1- el)))
  687. (aset forms--elements field-num (1- el)))
  688. (setq field-num (1+ field-num))
  689. (if prev-item
  690. (setq forms-format-list
  691. (append forms-format-list (list prev-item) nil)))
  692. (setq prev-item el))
  693. ;; Try function ...
  694. ((listp el)
  695. ;; Validate.
  696. (or (fboundp (car-safe el))
  697. (error (concat "Forms format error: "
  698. "%S is not a function")
  699. (car-safe el)))
  700. ;; Shift.
  701. (if prev-item
  702. (setq forms-format-list
  703. (append forms-format-list (list prev-item) nil)))
  704. (setq prev-item el))
  705. ;; else
  706. (t
  707. (error (concat "Forms format error: "
  708. "invalid element %S")
  709. el)))
  710. ;; Advance to next element of the list.
  711. (setq the-list rem)))
  712. ;; Append last item.
  713. (if prev-item
  714. (progn
  715. (setq forms-format-list
  716. (append forms-format-list (list prev-item) nil))
  717. ;; Append a newline if the last item is a field.
  718. ;; This prevents parsing problems.
  719. ;; Also it makes it possible to insert an empty last field.
  720. (if (numberp prev-item)
  721. (setq forms-format-list
  722. (append forms-format-list (list "\n") nil))))))
  723. (forms--debug 'forms-format-list
  724. 'forms--elements))
  725. ;; Special treatment for read-only segments.
  726. ;;
  727. ;; If text is inserted between two read-only segments, there seems to
  728. ;; be no way to give the newly inserted text the RW face.
  729. ;; To solve this, read-only segments get the `insert-in-front-hooks'
  730. ;; property set with a function that temporarily switches the
  731. ;; properties of the first character of the segment to the RW face, so
  732. ;; the new text gets the right face. The `post-command-hook' is
  733. ;; used to restore the original properties.
  734. (defvar forms--iif-start nil
  735. "Record start of modification command.")
  736. (defvar forms--iif-properties nil
  737. "Original properties of the character being overridden.")
  738. (defun forms--iif-hook (_begin _end)
  739. "`insert-in-front-hooks' function for read-only segments."
  740. ;; Note start location. By making it a marker that points one
  741. ;; character beyond the actual location, it is guaranteed to move
  742. ;; correctly if text is inserted.
  743. (or forms--iif-start
  744. (setq forms--iif-start (copy-marker (1+ (point)))))
  745. ;; Check if there is special treatment required.
  746. (if (or (<= forms--iif-start 2)
  747. (get-text-property (- forms--iif-start 2)
  748. 'read-only))
  749. (progn
  750. ;; Fetch current properties.
  751. (setq forms--iif-properties
  752. (text-properties-at (1- forms--iif-start)))
  753. ;; Replace them.
  754. (let ((inhibit-read-only t))
  755. (set-text-properties
  756. (1- forms--iif-start) forms--iif-start
  757. (list 'face forms--rw-face 'front-sticky '(face))))
  758. ;; Enable `post-command-hook' to restore the properties.
  759. (setq post-command-hook
  760. (append (list 'forms--iif-post-command-hook) post-command-hook)))
  761. ;; No action needed. Clear marker.
  762. (setq forms--iif-start nil)))
  763. (defun forms--iif-post-command-hook ()
  764. "`post-command-hook' function for read-only segments."
  765. ;; Disable `post-command-hook'.
  766. (setq post-command-hook
  767. (delq 'forms--iif-hook-post-command-hook post-command-hook))
  768. ;; Restore properties.
  769. (if forms--iif-start
  770. (let ((inhibit-read-only t))
  771. (set-text-properties
  772. (1- forms--iif-start) forms--iif-start
  773. forms--iif-properties)))
  774. ;; Cleanup.
  775. (setq forms--iif-start nil))
  776. (defvar forms--marker)
  777. (defvar forms--dyntext)
  778. (defun forms--make-format ()
  779. "Generate `forms--format' using the information in `forms-format-list'."
  780. ;; The real work is done using a mapcar of `forms--make-format-elt' on
  781. ;; `forms-format-list'.
  782. ;; This function sets up the necessary environment, and decides
  783. ;; which function to mapcar.
  784. (let ((forms--marker 0)
  785. (forms--dyntext 0))
  786. (setq
  787. forms--format
  788. (if forms-use-text-properties
  789. `(lambda (arg)
  790. (let ((inhibit-read-only t))
  791. ,@(apply 'append
  792. (mapcar #'forms--make-format-elt-using-text-properties
  793. forms-format-list))
  794. ;; Prevent insertion before the first text.
  795. ,@(if (numberp (car forms-format-list))
  796. nil
  797. '((add-text-properties (point-min) (1+ (point-min))
  798. '(front-sticky (read-only cursor-intangible)))))
  799. ;; Prevent insertion after the last text.
  800. (remove-text-properties (1- (point)) (point)
  801. '(rear-nonsticky)))
  802. (setq forms--iif-start nil))
  803. `(lambda (arg)
  804. ,@(apply 'append
  805. (mapcar #'forms--make-format-elt forms-format-list)))))
  806. ;; We have tallied the number of markers and dynamic texts,
  807. ;; so we can allocate the arrays now.
  808. (setq forms--markers (make-vector forms--marker nil))
  809. (setq forms--dyntexts (make-vector forms--dyntext nil)))
  810. (forms--debug 'forms--format))
  811. (defun forms--make-format-elt-using-text-properties (el)
  812. "Helper routine to generate format function."
  813. ;; The format routine `forms--format' will look like
  814. ;;
  815. ;; ;; preamble
  816. ;; (lambda (arg)
  817. ;; (let ((inhibit-read-only t))
  818. ;;
  819. ;; ;; A string, e.g. "text: ".
  820. ;; (set-text-properties
  821. ;; (point)
  822. ;; (progn (insert "text: ") (point))
  823. ;; (list 'face forms--ro-face
  824. ;; 'read-only 1
  825. ;; 'insert-in-front-hooks 'forms--iif-hook
  826. ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
  827. ;;
  828. ;; ;; A field, e.g. 6.
  829. ;; (let ((here (point)))
  830. ;; (aset forms--markers 0 (point-marker))
  831. ;; (insert (elt arg 5))
  832. ;; (or (= (point) here)
  833. ;; (set-text-properties
  834. ;; here (point)
  835. ;; (list 'face forms--rw-face
  836. ;; 'front-sticky '(face))))
  837. ;;
  838. ;; ;; Another string, e.g. "\nmore text: ".
  839. ;; (set-text-properties
  840. ;; (point)
  841. ;; (progn (insert "\nmore text: ") (point))
  842. ;; (list 'face forms--ro-face
  843. ;; 'read-only 2
  844. ;; 'insert-in-front-hooks 'forms--iif-hook
  845. ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
  846. ;;
  847. ;; ;; A function, e.g. (tocol 40).
  848. ;; (set-text-properties
  849. ;; (point)
  850. ;; (progn
  851. ;; (insert (aset forms--dyntexts 0 (tocol 40)))
  852. ;; (point))
  853. ;; (list 'face forms--ro-face
  854. ;; 'read-only 2
  855. ;; 'insert-in-front-hooks 'forms--iif-hook
  856. ;; 'rear-nonsticky '(read-only face insert-in-front-hooks)))
  857. ;;
  858. ;; ;; Prevent insertion before the first text.
  859. ;; (add-text-properties (point-min) (1+ (point-min))
  860. ;; '(front-sticky (read-only))))))
  861. ;; ;; Prevent insertion after the last text.
  862. ;; (remove-text-properties (1- (point)) (point)
  863. ;; '(rear-nonsticky)))
  864. ;;
  865. ;; ;; wrap up
  866. ;; (setq forms--iif-start nil)
  867. ;; ))
  868. (cond
  869. ((stringp el)
  870. `((set-text-properties
  871. (point) ; start at point
  872. (progn ; until after insertion
  873. (insert ,el)
  874. (point))
  875. (list 'face forms--ro-face ; read-only appearance
  876. 'read-only ,@(list (1+ forms--marker))
  877. 'cursor-intangible ,@(list (1+ forms--marker))
  878. 'insert-in-front-hooks '(forms--iif-hook)
  879. 'rear-nonsticky '(face read-only insert-in-front-hooks
  880. cursor-intangible)))))
  881. ((numberp el)
  882. `((let ((here (point)))
  883. (aset forms--markers
  884. ,(prog1 forms--marker
  885. (setq forms--marker (1+ forms--marker)))
  886. (point-marker))
  887. (insert (elt arg ,(1- el)))
  888. (or (= (point) here)
  889. (set-text-properties
  890. here (point)
  891. (list 'face forms--rw-face
  892. 'front-sticky '(face)))))))
  893. ((listp el)
  894. `((set-text-properties
  895. (point)
  896. (progn
  897. (insert (aset forms--dyntexts
  898. ,(prog1 forms--dyntext
  899. (setq forms--dyntext (1+ forms--dyntext)))
  900. ,el))
  901. (point))
  902. (list 'face forms--ro-face
  903. 'read-only ,@(list (1+ forms--marker))
  904. 'cursor-intangible ,@(list (1+ forms--marker))
  905. 'insert-in-front-hooks '(forms--iif-hook)
  906. 'rear-nonsticky '(read-only face insert-in-front-hooks
  907. cursor-intangible)))))
  908. ;; end of cond
  909. ))
  910. (defun forms--make-format-elt (el)
  911. "Helper routine to generate format function."
  912. ;; If we're not using text properties, the format routine
  913. ;; `forms--format' will look like
  914. ;;
  915. ;; (lambda (arg)
  916. ;; ;; a string, e.g. "text: "
  917. ;; (insert "text: ")
  918. ;; ;; a field, e.g. 6
  919. ;; (aset forms--markers 0 (point-marker))
  920. ;; (insert (elt arg 5))
  921. ;; ;; another string, e.g. "\nmore text: "
  922. ;; (insert "\nmore text: ")
  923. ;; ;; a function, e.g. (tocol 40)
  924. ;; (insert (aset forms--dyntexts 0 (tocol 40)))
  925. ;; ... )
  926. (cond
  927. ((stringp el)
  928. `((insert ,el)))
  929. ((numberp el)
  930. (prog1
  931. `((aset forms--markers ,forms--marker (point-marker))
  932. (insert (elt arg ,(1- el))))
  933. (setq forms--marker (1+ forms--marker))))
  934. ((listp el)
  935. (prog1
  936. `((insert (aset forms--dyntexts ,forms--dyntext ,el)))
  937. (setq forms--dyntext (1+ forms--dyntext))))))
  938. (defvar forms--field)
  939. (defvar forms--recordv)
  940. (defvar forms--seen-text)
  941. (defun forms--make-parser ()
  942. "Generate `forms--parser' from the information in `forms-format-list'."
  943. ;; If we can use text properties, we simply set it to
  944. ;; `forms--parser-using-text-properties'.
  945. ;; Otherwise, the function is constructed using a mapcar of
  946. ;; `forms--make-parser-elt on `forms-format-list'.
  947. (setq
  948. forms--parser
  949. (if forms-use-text-properties
  950. (function forms--parser-using-text-properties)
  951. (let ((forms--field nil)
  952. (forms--seen-text nil)
  953. (forms--dyntext 0))
  954. ;; Note: we add a nil element to the list passed to `mapcar',
  955. ;; see `forms--make-parser-elt' for details.
  956. `(lambda nil
  957. (let (here)
  958. (goto-char (point-min))
  959. ,@(apply 'append
  960. (mapcar
  961. #'forms--make-parser-elt
  962. (append forms-format-list (list nil)))))))))
  963. (forms--debug 'forms--parser))
  964. (defun forms--parser-using-text-properties ()
  965. "Extract field info from forms when using text properties."
  966. ;; Using text properties, we can simply jump to the markers, and
  967. ;; extract the information up to the following read-only segment.
  968. (let ((i 0)
  969. here there)
  970. (while (< i (length forms--markers))
  971. (goto-char (setq here (aref forms--markers i)))
  972. (if (get-text-property here 'read-only)
  973. (aset forms--recordv (aref forms--elements i) nil)
  974. (if (setq there
  975. (next-single-property-change here 'read-only))
  976. (aset forms--recordv (aref forms--elements i)
  977. (buffer-substring-no-properties here there))
  978. (aset forms--recordv (aref forms--elements i)
  979. (buffer-substring-no-properties here (point-max)))))
  980. (setq i (1+ i)))))
  981. (defun forms--make-parser-elt (el)
  982. "Helper routine to generate forms parser function."
  983. ;; The parse routine will look like:
  984. ;;
  985. ;; (lambda nil
  986. ;; (let (here)
  987. ;; (goto-char (point-min))
  988. ;;
  989. ;; ;; "text: "
  990. ;; (if (not (looking-at "text: "))
  991. ;; (error "Parse error: cannot find \"text: \""))
  992. ;; (forward-char 6) ; past "text: "
  993. ;;
  994. ;; ;; 6
  995. ;; ;; "\nmore text: "
  996. ;; (setq here (point))
  997. ;; (if (not (search-forward "\nmore text: " nil t nil))
  998. ;; (error "Parse error: cannot find \"\\nmore text: \""))
  999. ;; (aset forms--recordv 5 (buffer-substring-no-properties here (- (point) 12)))
  1000. ;;
  1001. ;; ;; (tocol 40)
  1002. ;; (let ((forms--dyntext (car-safe forms--dynamic-text)))
  1003. ;; (if (not (looking-at (regexp-quote forms--dyntext)))
  1004. ;; (error "Parse error: not looking at \"%s\"" forms--dyntext))
  1005. ;; (forward-char (length forms--dyntext))
  1006. ;; (setq forms--dynamic-text (cdr-safe forms--dynamic-text)))
  1007. ;; ...
  1008. ;; ;; final flush (due to terminator sentinel, see below)
  1009. ;; (aset forms--recordv 7 (buffer-substring-no-properties (point) (point-max)))
  1010. (cond
  1011. ((stringp el)
  1012. (prog1
  1013. (if forms--field
  1014. `((setq here (point))
  1015. (if (not (search-forward ,el nil t nil))
  1016. (error "Parse error: cannot find `%s'" ,el))
  1017. (aset forms--recordv ,(1- forms--field)
  1018. (buffer-substring-no-properties here
  1019. (- (point) ,(length el)))))
  1020. `((if (not (looking-at ,(regexp-quote el)))
  1021. (error "Parse error: not looking at `%s'" ,el))
  1022. (forward-char ,(length el))))
  1023. (setq forms--seen-text t)
  1024. (setq forms--field nil)))
  1025. ((numberp el)
  1026. (if forms--field
  1027. (error "Cannot parse adjacent fields %d and %d"
  1028. forms--field el)
  1029. (setq forms--field el)
  1030. nil))
  1031. ((null el)
  1032. (if forms--field
  1033. `((aset forms--recordv ,(1- forms--field)
  1034. (buffer-substring-no-properties (point) (point-max))))))
  1035. ((listp el)
  1036. (prog1
  1037. (if forms--field
  1038. `((let ((here (point))
  1039. (forms--dyntext (aref forms--dyntexts ,forms--dyntext)))
  1040. (if (not (search-forward forms--dyntext nil t nil))
  1041. (error "Parse error: cannot find `%s'" forms--dyntext))
  1042. (aset forms--recordv ,(1- forms--field)
  1043. (buffer-substring-no-properties here
  1044. (- (point) (length forms--dyntext))))))
  1045. `((let ((forms--dyntext (aref forms--dyntexts ,forms--dyntext)))
  1046. (if (not (looking-at (regexp-quote forms--dyntext)))
  1047. (error "Parse error: not looking at `%s'" forms--dyntext))
  1048. (forward-char (length forms--dyntext)))))
  1049. (setq forms--dyntext (1+ forms--dyntext))
  1050. (setq forms--seen-text t)
  1051. (setq forms--field nil)))
  1052. ))
  1053. (defun forms--intuit-from-file ()
  1054. "Get number of fields and a default form using the data file."
  1055. ;; If `forms-number-of-fields' is not set, get it from the data file.
  1056. (if (null forms-number-of-fields)
  1057. ;; Need a file to do this.
  1058. (if (not (file-exists-p forms-file))
  1059. (error "Need existing file or explicit `forms-number-of-fields'")
  1060. ;; Visit the file and extract the first record.
  1061. (setq forms--file-buffer (find-file-noselect forms-file))
  1062. (let ((read-file-filter forms-read-file-filter)
  1063. (the-record))
  1064. (setq the-record
  1065. (with-current-buffer forms--file-buffer
  1066. (let ((inhibit-read-only t))
  1067. (run-hooks 'read-file-filter))
  1068. (goto-char (point-min))
  1069. (forms--get-record)))
  1070. ;; This may be overkill, but try to avoid interference with
  1071. ;; the normal processing.
  1072. (kill-buffer forms--file-buffer)
  1073. ;; Count the number of fields in `the-record'.
  1074. (let ((start-pos 0)
  1075. found-pos
  1076. (field-sep-length (length forms-field-sep)))
  1077. (setq forms-number-of-fields 1)
  1078. (while (setq found-pos
  1079. (string-match forms-field-sep the-record start-pos))
  1080. (progn
  1081. (setq forms-number-of-fields (1+ forms-number-of-fields))
  1082. (setq start-pos (+ field-sep-length found-pos))))))))
  1083. ;; Construct default format list.
  1084. (setq forms-format-list (list "Forms file \"" forms-file "\".\n\n"))
  1085. (let ((i 0))
  1086. (while (<= (setq i (1+ i)) forms-number-of-fields)
  1087. (setq forms-format-list
  1088. (append forms-format-list
  1089. (list (format "%4d: " i) i "\n"))))))
  1090. (defun forms--set-keymaps ()
  1091. "Set the keymaps used in this mode."
  1092. (use-local-map (if forms-read-only
  1093. forms-mode-ro-map
  1094. forms-mode-edit-map)))
  1095. (defun forms--mode-commands ()
  1096. "Fill the Forms mode keymaps."
  1097. ;; `forms-mode-map' is always accessible via \C-c prefix.
  1098. (setq forms-mode-map (make-keymap))
  1099. (define-key forms-mode-map "\t" 'forms-next-field)
  1100. (define-key forms-mode-map "\C-k" 'forms-delete-record)
  1101. (define-key forms-mode-map "\C-q" 'forms-toggle-read-only)
  1102. (define-key forms-mode-map "\C-o" 'forms-insert-record)
  1103. (define-key forms-mode-map "\C-l" 'forms-jump-record)
  1104. (define-key forms-mode-map "\C-n" 'forms-next-record)
  1105. (define-key forms-mode-map "\C-p" 'forms-prev-record)
  1106. (define-key forms-mode-map "\C-r" 'forms-search-backward)
  1107. (define-key forms-mode-map "\C-s" 'forms-search-forward)
  1108. (define-key forms-mode-map "\C-x" 'forms-exit)
  1109. (define-key forms-mode-map "<" 'forms-first-record)
  1110. (define-key forms-mode-map ">" 'forms-last-record)
  1111. (define-key forms-mode-map "\C-?" 'forms-prev-record)
  1112. ;; `forms-mode-ro-map' replaces the local map when in read-only mode.
  1113. (setq forms-mode-ro-map (make-keymap))
  1114. (suppress-keymap forms-mode-ro-map)
  1115. (define-key forms-mode-ro-map "\C-c" forms-mode-map)
  1116. (define-key forms-mode-ro-map "q" 'forms-toggle-read-only)
  1117. (define-key forms-mode-ro-map "l" 'forms-jump-record)
  1118. (define-key forms-mode-ro-map "n" 'forms-next-record)
  1119. (define-key forms-mode-ro-map "p" 'forms-prev-record)
  1120. (define-key forms-mode-ro-map "r" 'forms-search-backward)
  1121. (define-key forms-mode-ro-map "s" 'forms-search-forward)
  1122. (define-key forms-mode-ro-map "x" 'forms-exit)
  1123. (define-key forms-mode-ro-map "<" 'forms-first-record)
  1124. (define-key forms-mode-ro-map ">" 'forms-last-record)
  1125. (define-key forms-mode-ro-map "?" 'describe-mode)
  1126. (define-key forms-mode-ro-map " " 'forms-next-record)
  1127. (forms--mode-commands1 forms-mode-ro-map)
  1128. (forms--mode-menu-ro forms-mode-ro-map)
  1129. ;; This is the normal, local map.
  1130. (setq forms-mode-edit-map (make-keymap))
  1131. (define-key forms-mode-edit-map "\C-c" forms-mode-map)
  1132. (forms--mode-commands1 forms-mode-edit-map)
  1133. (forms--mode-menu-edit forms-mode-edit-map)
  1134. )
  1135. (defun forms--mode-menu-ro (map)
  1136. ;;; Menu initialization
  1137. ; (define-key map [menu-bar] (make-sparse-keymap))
  1138. (define-key map [menu-bar forms]
  1139. (cons "Forms" (make-sparse-keymap "Forms")))
  1140. (define-key map [menu-bar forms menu-forms-exit]
  1141. '("Exit Forms Mode" . forms-exit))
  1142. (define-key map [menu-bar forms menu-forms-sep1]
  1143. '("----"))
  1144. (define-key map [menu-bar forms menu-forms-save]
  1145. '("Save Data" . forms-save-buffer))
  1146. (define-key map [menu-bar forms menu-forms-print]
  1147. '("Print Data" . forms-print))
  1148. (define-key map [menu-bar forms menu-forms-describe]
  1149. '("Describe Mode" . describe-mode))
  1150. (define-key map [menu-bar forms menu-forms-toggle-ro]
  1151. '("Toggle View/Edit" . forms-toggle-read-only))
  1152. (define-key map [menu-bar forms menu-forms-jump-record]
  1153. '("Jump" . forms-jump-record))
  1154. (define-key map [menu-bar forms menu-forms-search-backward]
  1155. '("Search Backward" . forms-search-backward))
  1156. (define-key map [menu-bar forms menu-forms-search-forward]
  1157. '("Search Forward" . forms-search-forward))
  1158. (define-key map [menu-bar forms menu-forms-delete-record]
  1159. '("Delete" . forms-delete-record))
  1160. (define-key map [menu-bar forms menu-forms-insert-record]
  1161. '("Insert" . forms-insert-record))
  1162. (define-key map [menu-bar forms menu-forms-sep2]
  1163. '("----"))
  1164. (define-key map [menu-bar forms menu-forms-last-record]
  1165. '("Last Record" . forms-last-record))
  1166. (define-key map [menu-bar forms menu-forms-first-record]
  1167. '("First Record" . forms-first-record))
  1168. (define-key map [menu-bar forms menu-forms-prev-record]
  1169. '("Previous Record" . forms-prev-record))
  1170. (define-key map [menu-bar forms menu-forms-next-record]
  1171. '("Next Record" . forms-next-record))
  1172. (define-key map [menu-bar forms menu-forms-sep3]
  1173. '("----"))
  1174. (define-key map [menu-bar forms menu-forms-prev-field]
  1175. '("Previous Field" . forms-prev-field))
  1176. (define-key map [menu-bar forms menu-forms-next-field]
  1177. '("Next Field" . forms-next-field))
  1178. (put 'forms-insert-record 'menu-enable '(not forms-read-only))
  1179. (put 'forms-delete-record 'menu-enable '(not forms-read-only))
  1180. )
  1181. (defun forms--mode-menu-edit (map)
  1182. ;;; Menu initialization
  1183. ; (define-key map [menu-bar] (make-sparse-keymap))
  1184. (define-key map [menu-bar forms]
  1185. (cons "Forms" (make-sparse-keymap "Forms")))
  1186. (define-key map [menu-bar forms menu-forms-edit--exit]
  1187. '("Exit" . forms-exit))
  1188. (define-key map [menu-bar forms menu-forms-edit-sep1]
  1189. '("----"))
  1190. (define-key map [menu-bar forms menu-forms-edit-save]
  1191. '("Save Data" . forms-save-buffer))
  1192. (define-key map [menu-bar forms menu-forms-edit-print]
  1193. '("Print Data" . forms-print))
  1194. (define-key map [menu-bar forms menu-forms-edit-describe]
  1195. '("Describe Mode" . describe-mode))
  1196. (define-key map [menu-bar forms menu-forms-edit-toggle-ro]
  1197. '("Toggle View/Edit" . forms-toggle-read-only))
  1198. (define-key map [menu-bar forms menu-forms-edit-jump-record]
  1199. '("Jump" . forms-jump-record))
  1200. (define-key map [menu-bar forms menu-forms-edit-search-backward]
  1201. '("Search Backward" . forms-search-backward))
  1202. (define-key map [menu-bar forms menu-forms-edit-search-forward]
  1203. '("Search Forward" . forms-search-forward))
  1204. (define-key map [menu-bar forms menu-forms-edit-delete-record]
  1205. '("Delete" . forms-delete-record))
  1206. (define-key map [menu-bar forms menu-forms-edit-insert-record]
  1207. '("Insert" . forms-insert-record))
  1208. (define-key map [menu-bar forms menu-forms-edit-sep2]
  1209. '("----"))
  1210. (define-key map [menu-bar forms menu-forms-edit-last-record]
  1211. '("Last Record" . forms-last-record))
  1212. (define-key map [menu-bar forms menu-forms-edit-first-record]
  1213. '("First Record" . forms-first-record))
  1214. (define-key map [menu-bar forms menu-forms-edit-prev-record]
  1215. '("Previous Record" . forms-prev-record))
  1216. (define-key map [menu-bar forms menu-forms-edit-next-record]
  1217. '("Next Record" . forms-next-record))
  1218. (define-key map [menu-bar forms menu-forms-edit-sep3]
  1219. '("----"))
  1220. (define-key map [menu-bar forms menu-forms-edit-prev-field]
  1221. '("Previous Field" . forms-prev-field))
  1222. (define-key map [menu-bar forms menu-forms-edit-next-field]
  1223. '("Next Field" . forms-next-field))
  1224. (put 'forms-insert-record 'menu-enable '(not forms-read-only))
  1225. (put 'forms-delete-record 'menu-enable '(not forms-read-only))
  1226. )
  1227. (defun forms--mode-commands1 (map)
  1228. "Helper routine to define keys."
  1229. (define-key map "\t" 'forms-next-field)
  1230. (define-key map [S-tab] 'forms-prev-field)
  1231. (define-key map [next] 'forms-next-record)
  1232. (define-key map [prior] 'forms-prev-record)
  1233. (define-key map [begin] 'forms-first-record)
  1234. (define-key map [last] 'forms-last-record)
  1235. (define-key map [backtab] 'forms-prev-field)
  1236. )
  1237. ;;; Changed functions
  1238. (defun forms--change-commands ()
  1239. "Localize some commands for Forms mode."
  1240. ;; scroll-down -> forms-prev-record
  1241. ;; scroll-up -> forms-next-record
  1242. (if forms-forms-scroll
  1243. (progn
  1244. (local-set-key [remap scroll-up] 'forms-next-record)
  1245. (local-set-key [remap scroll-down] 'forms-prev-record)
  1246. (local-set-key [remap scroll-up-command] 'forms-next-record)
  1247. (local-set-key [remap scroll-down-command] 'forms-prev-record)))
  1248. ;;
  1249. ;; beginning-of-buffer -> forms-first-record
  1250. ;; end-of-buffer -> forms-end-record
  1251. (if forms-forms-jump
  1252. (progn
  1253. (local-set-key [remap beginning-of-buffer] 'forms-first-record)
  1254. (local-set-key [remap end-of-buffer] 'forms-last-record)))
  1255. ;;
  1256. ;; Save buffer
  1257. (local-set-key "\C-x\C-s" 'forms-save-buffer)
  1258. ;;
  1259. ;; We have our own revert function - use it.
  1260. (make-local-variable 'revert-buffer-function)
  1261. (setq revert-buffer-function 'forms--revert-buffer)
  1262. t)
  1263. (defun forms--help ()
  1264. "Initial help for Forms mode."
  1265. (message "%s" (substitute-command-keys (concat
  1266. "\\[forms-next-record]:next"
  1267. " \\[forms-prev-record]:prev"
  1268. " \\[forms-first-record]:first"
  1269. " \\[forms-last-record]:last"
  1270. " \\[describe-mode]:help"))))
  1271. (defun forms--trans (subj arg rep)
  1272. "Translate in SUBJ all chars ARG into char REP. ARG and REP should
  1273. be single-char strings."
  1274. (let ((i 0)
  1275. (re (regexp-quote arg))
  1276. (k (string-to-char rep)))
  1277. (while (setq i (string-match re subj i))
  1278. (aset subj i k)
  1279. (setq i (1+ i)))))
  1280. (defun forms--exit (&optional save)
  1281. "Internal exit from forms mode function."
  1282. (let ((buf (buffer-name forms--file-buffer)))
  1283. (forms--checkmod)
  1284. (if (and save
  1285. (buffer-modified-p forms--file-buffer))
  1286. (forms-save-buffer))
  1287. (with-current-buffer forms--file-buffer
  1288. (delete-auto-save-file-if-necessary)
  1289. (kill-buffer (current-buffer)))
  1290. (if (get-buffer buf) ; not killed???
  1291. (if save
  1292. (error "Problem saving buffer %s" (buffer-name buf)))
  1293. (delete-auto-save-file-if-necessary)
  1294. (kill-buffer (current-buffer)))))
  1295. (defun forms--get-record ()
  1296. "Fetch the current record from the file buffer."
  1297. ;; This function is executed in the context of the `forms--file-buffer'.
  1298. (or (bolp)
  1299. (beginning-of-line nil))
  1300. (let ((here (point)))
  1301. (prog2
  1302. (end-of-line)
  1303. (buffer-substring-no-properties here (point))
  1304. (goto-char here))))
  1305. (defun forms--show-record (the-record)
  1306. "Format THE-RECORD and display it in the current buffer."
  1307. ;; Split the-record.
  1308. (let (the-result
  1309. (start-pos 0)
  1310. found-pos
  1311. (field-sep-length (length forms-field-sep)))
  1312. (if forms-multi-line
  1313. (forms--trans the-record forms-multi-line "\n"))
  1314. ;; Add an extra separator (makes splitting easy).
  1315. (setq the-record (concat the-record forms-field-sep))
  1316. (while (setq found-pos (string-match forms-field-sep the-record start-pos))
  1317. (let ((ent (substring the-record start-pos found-pos)))
  1318. (setq the-result
  1319. (append the-result (list ent)))
  1320. (setq start-pos (+ field-sep-length found-pos))))
  1321. (setq forms--the-record-list the-result))
  1322. (setq buffer-read-only nil)
  1323. (if forms-use-text-properties
  1324. (let ((inhibit-read-only t))
  1325. (set-text-properties (point-min) (point-max) nil)))
  1326. (erase-buffer)
  1327. ;; Verify the number of fields, extend forms--the-record-list if needed.
  1328. (if (= (length forms--the-record-list) forms-number-of-fields)
  1329. nil
  1330. (if (null forms-check-number-of-fields)
  1331. nil
  1332. (message "Warning: this record has %d fields instead of %d"
  1333. (length forms--the-record-list) forms-number-of-fields))
  1334. (if (< (length forms--the-record-list) forms-number-of-fields)
  1335. (setq forms--the-record-list
  1336. (append forms--the-record-list
  1337. (make-list
  1338. (- forms-number-of-fields
  1339. (length forms--the-record-list))
  1340. "")))))
  1341. ;; Call the formatter function.
  1342. (setq forms-fields (append (list nil) forms--the-record-list nil))
  1343. (funcall forms--format forms--the-record-list)
  1344. ;; Prepare.
  1345. (goto-char (point-min))
  1346. (set-buffer-modified-p nil)
  1347. (setq buffer-read-only forms-read-only)
  1348. (setq mode-line-process
  1349. (concat " " (int-to-string forms--current-record)
  1350. "/" (int-to-string forms--total-records))))
  1351. (defun forms--parse-form ()
  1352. "Parse contents of form into list of strings."
  1353. ;; The contents of the form are parsed, and a new list of strings
  1354. ;; is constructed.
  1355. ;; A vector with the strings from the original record is
  1356. ;; constructed, which is updated with the new contents. Therefore
  1357. ;; fields which were not in the form are not modified.
  1358. ;; Finally, the vector is transformed into a list for further processing.
  1359. (let (forms--recordv)
  1360. ;; Build the vector.
  1361. (setq forms--recordv (vconcat forms--the-record-list))
  1362. ;; Parse the form and update the vector.
  1363. (let ((forms--dynamic-text forms--dynamic-text))
  1364. (funcall forms--parser))
  1365. (if forms-modified-record-filter
  1366. ;; As a service to the user, we add a zeroth element so she
  1367. ;; can use the same indices as in the forms definition.
  1368. (let ((the-fields (vconcat [nil] forms--recordv)))
  1369. (setq the-fields (funcall forms-modified-record-filter the-fields))
  1370. (cdr (append the-fields nil)))
  1371. ;; Transform to a list and return.
  1372. (append forms--recordv nil))))
  1373. (defun forms--update ()
  1374. "Update current record with contents of form.
  1375. As a side effect: sets `forms--the-record-list'."
  1376. (if forms-read-only
  1377. (error "Buffer is read-only"))
  1378. (let (the-record)
  1379. ;; Build new record.
  1380. (setq forms--the-record-list (forms--parse-form))
  1381. (setq the-record
  1382. (mapconcat #'identity forms--the-record-list forms-field-sep))
  1383. (if (string-match-p (regexp-quote forms-field-sep)
  1384. (mapconcat #'identity forms--the-record-list ""))
  1385. (error "Field separator occurs in record - update refused"))
  1386. ;; Handle multi-line fields, if allowed.
  1387. (if forms-multi-line
  1388. (forms--trans the-record "\n" forms-multi-line))
  1389. ;; A final sanity check before updating.
  1390. (if (string-match-p "\n" the-record)
  1391. (error "Multi-line fields in this record - update refused"))
  1392. (with-current-buffer forms--file-buffer
  1393. ;; Use delete-region instead of kill-region, to avoid
  1394. ;; adding junk to the kill-ring.
  1395. (delete-region (line-beginning-position) (line-end-position))
  1396. (insert the-record)
  1397. (beginning-of-line))))
  1398. (defun forms--checkmod ()
  1399. "Check if this form has been modified, and call forms--update if so."
  1400. (if (buffer-modified-p nil)
  1401. (let ((here (point)))
  1402. (forms--update)
  1403. (set-buffer-modified-p nil)
  1404. (goto-char here))))
  1405. ;;; Start and exit
  1406. ;;;###autoload
  1407. (defun forms-find-file (fn)
  1408. "Visit a file in Forms mode."
  1409. (interactive "fForms file: ")
  1410. (let ((enable-local-eval t)
  1411. (enable-local-variables t))
  1412. (find-file-read-only fn)
  1413. (or forms--mode-setup (forms-mode t))))
  1414. ;;;###autoload
  1415. (defun forms-find-file-other-window (fn)
  1416. "Visit a file in Forms mode in other window."
  1417. (interactive "fFbrowse file in other window: ")
  1418. (let ((enable-local-eval t)
  1419. (enable-local-variables t))
  1420. (find-file-other-window fn)
  1421. (or forms--mode-setup (forms-mode t))))
  1422. (defun forms-exit ()
  1423. "Normal exit from Forms mode. Modified buffers are saved."
  1424. (interactive)
  1425. (forms--exit t))
  1426. (defun forms-exit-no-save ()
  1427. "Exit from Forms mode without saving buffers."
  1428. (interactive)
  1429. (forms--exit nil))
  1430. ;;; Navigating commands
  1431. (defun forms-next-record (arg)
  1432. "Advance to the ARGth following record."
  1433. (interactive "P")
  1434. (forms-jump-record (+ forms--current-record (prefix-numeric-value arg)) t))
  1435. (defun forms-prev-record (arg)
  1436. "Advance to the ARGth previous record."
  1437. (interactive "P")
  1438. (forms-jump-record (- forms--current-record (prefix-numeric-value arg)) t))
  1439. (defun forms--goto-record (rn &optional current)
  1440. "Goto record number RN.
  1441. If CURRENT is provided, it specifies the current record and can be used
  1442. to speed up access to RN. Returns the number of records missing, if any."
  1443. (if current
  1444. (forward-line (- rn current))
  1445. ;; goto-line does not do what we want when the buffer is narrowed.
  1446. (goto-char (point-min))
  1447. (forward-line (1- rn))))
  1448. (defun forms-jump-record (arg &optional relative)
  1449. "Jump to a random record."
  1450. (interactive "NRecord number: ")
  1451. ;; Verify that the record number is within range.
  1452. (if (or (> arg forms--total-records)
  1453. (<= arg 0))
  1454. (error
  1455. ;; Don't give the message if just paging.
  1456. (if (not relative)
  1457. (message "Record number %d out of range 1..%d"
  1458. arg forms--total-records)
  1459. "")))
  1460. ;; Flush.
  1461. (forms--checkmod)
  1462. ;; Calculate displacement.
  1463. (let ((cur forms--current-record))
  1464. ;; `forms--show-record' needs it now.
  1465. (setq forms--current-record arg)
  1466. ;; Get the record and show it.
  1467. (forms--show-record
  1468. (with-current-buffer forms--file-buffer
  1469. (beginning-of-line)
  1470. ;; Move, and adjust the amount if needed (shouldn't happen).
  1471. (setq cur (- arg (forms--goto-record arg (if relative cur))))
  1472. (forms--get-record)))
  1473. ;; This shouldn't happen.
  1474. (if (/= forms--current-record cur)
  1475. (progn
  1476. (setq forms--current-record cur)
  1477. (error "Stuck at record %d" cur)))))
  1478. (defun forms-first-record ()
  1479. "Jump to first record."
  1480. (interactive)
  1481. (forms-jump-record 1))
  1482. (defun forms-last-record ()
  1483. "Jump to last record.
  1484. As a side effect: re-calculates the number of records in the data file."
  1485. (interactive)
  1486. (let
  1487. ((numrec
  1488. (with-current-buffer forms--file-buffer
  1489. (count-lines (point-min) (point-max)))))
  1490. (if (= numrec forms--total-records)
  1491. nil
  1492. (setq forms--total-records numrec)
  1493. (message "Warning: number of records changed to %d" forms--total-records)))
  1494. (forms-jump-record forms--total-records))
  1495. ;;; Other commands
  1496. (defun forms-toggle-read-only (arg)
  1497. "Toggles read-only mode of a forms mode buffer.
  1498. With an argument, enables read-only mode if the argument is positive.
  1499. Otherwise enables edit mode if the visited file is writable."
  1500. (interactive "P")
  1501. (if (if arg
  1502. ;; Negative arg means switch it off.
  1503. (<= (prefix-numeric-value arg) 0)
  1504. ;; No arg means toggle.
  1505. forms-read-only)
  1506. ;; Enable edit mode, if possible.
  1507. (let ((ro forms-read-only))
  1508. (if (with-current-buffer forms--file-buffer
  1509. buffer-read-only)
  1510. (progn
  1511. (setq forms-read-only t)
  1512. (message "No write access to `%s'" forms-file))
  1513. (setq forms-read-only nil))
  1514. (if (equal ro forms-read-only)
  1515. nil
  1516. (forms-mode)))
  1517. ;; Enable view mode.
  1518. (if forms-read-only
  1519. nil
  1520. (forms--checkmod) ; sync
  1521. (setq forms-read-only t)
  1522. (forms-mode))))
  1523. ;; Sample:
  1524. ;; (defun my-new-record-filter (the-fields)
  1525. ;; ;; numbers are relative to 1
  1526. ;; (aset the-fields 4 (current-time-string))
  1527. ;; (aset the-fields 6 (user-login-name))
  1528. ;; the-list)
  1529. ;; (setq forms-new-record-filter 'my-new-record-filter)
  1530. (defun forms-insert-record (arg)
  1531. "Create a new record before the current one.
  1532. With ARG: store the record after the current one.
  1533. If `forms-new-record-filter' contains the name of a function,
  1534. it is called to fill (some of) the fields with default values.
  1535. If `forms-insert-after' is non-nil, the default behavior is to insert
  1536. after the current record."
  1537. (interactive "P")
  1538. (if forms-read-only
  1539. (error ""))
  1540. (let (ln the-list the-record)
  1541. (if (or (and arg forms-insert-after)
  1542. (and (not arg) (not forms-insert-after)))
  1543. (setq ln forms--current-record)
  1544. (setq ln (1+ forms--current-record)))
  1545. (forms--checkmod)
  1546. (if forms-new-record-filter
  1547. ;; As a service to the user, we add a zeroth element so she
  1548. ;; can use the same indices as in the forms definition.
  1549. (let ((the-fields (make-vector (1+ forms-number-of-fields) "")))
  1550. (setq the-fields (funcall forms-new-record-filter the-fields))
  1551. (setq the-list (cdr (append the-fields nil))))
  1552. (setq the-list (make-list forms-number-of-fields "")))
  1553. (setq the-record (mapconcat #'identity the-list forms-field-sep))
  1554. (with-current-buffer forms--file-buffer
  1555. (forms--goto-record ln)
  1556. (open-line 1)
  1557. (insert the-record)
  1558. (beginning-of-line))
  1559. (setq forms--current-record ln))
  1560. (setq forms--total-records (1+ forms--total-records))
  1561. (forms-jump-record forms--current-record))
  1562. (defun forms-delete-record (arg)
  1563. "Deletes a record. With a prefix argument: don't ask."
  1564. (interactive "P")
  1565. (if forms-read-only
  1566. (error ""))
  1567. (forms--checkmod)
  1568. (if (or arg
  1569. (y-or-n-p "Really delete this record? "))
  1570. (let ((ln forms--current-record))
  1571. (with-current-buffer forms--file-buffer
  1572. (forms--goto-record ln)
  1573. ;; Use delete-region instead of kill-region, to avoid
  1574. ;; adding junk to the kill-ring.
  1575. (delete-region (progn (beginning-of-line) (point))
  1576. (progn (beginning-of-line 2) (point))))
  1577. (setq forms--total-records (1- forms--total-records))
  1578. (if (> forms--current-record forms--total-records)
  1579. (setq forms--current-record forms--total-records))
  1580. (forms-jump-record forms--current-record)))
  1581. (message ""))
  1582. (defun forms-search-forward (regexp)
  1583. "Search forward for record containing REGEXP."
  1584. (interactive
  1585. (list (read-string (concat "Search forward for"
  1586. (if forms--search-regexp
  1587. (concat " ("
  1588. forms--search-regexp
  1589. ")"))
  1590. ": "))))
  1591. (if (equal "" regexp)
  1592. (setq regexp forms--search-regexp))
  1593. (forms--checkmod)
  1594. (let (the-line the-record here)
  1595. (with-current-buffer forms--file-buffer
  1596. (end-of-line)
  1597. (setq here (point))
  1598. (if (or (re-search-forward regexp nil t)
  1599. (and (> here (point-min))
  1600. (progn
  1601. (goto-char (point-min))
  1602. (re-search-forward regexp here t))))
  1603. (progn
  1604. (setq the-record (forms--get-record))
  1605. (setq the-line (1+ (count-lines (point-min) (point))))
  1606. (if (< (point) here)
  1607. (message "Wrapped")))
  1608. (goto-char here)
  1609. (error "Search failed: %s" regexp)))
  1610. (setq forms--current-record the-line)
  1611. (forms--show-record the-record))
  1612. (re-search-forward regexp nil t)
  1613. (setq forms--search-regexp regexp))
  1614. (defun forms-search-backward (regexp)
  1615. "Search backward for record containing REGEXP."
  1616. (interactive
  1617. (list (read-string (concat "Search backward for"
  1618. (if forms--search-regexp
  1619. (concat " ("
  1620. forms--search-regexp
  1621. ")"))
  1622. ": "))))
  1623. (if (equal "" regexp)
  1624. (setq regexp forms--search-regexp))
  1625. (forms--checkmod)
  1626. (let (the-line the-record here)
  1627. (with-current-buffer forms--file-buffer
  1628. (beginning-of-line)
  1629. (setq here (point))
  1630. (if (or (re-search-backward regexp nil t)
  1631. (and (< (point) (point-max))
  1632. (progn
  1633. (goto-char (point-max))
  1634. (re-search-backward regexp here t))))
  1635. (progn
  1636. (setq the-record (forms--get-record))
  1637. (setq the-line (1+ (count-lines (point-min) (point))))
  1638. (if (> (point) here)
  1639. (message "Wrapped")))
  1640. (goto-char here)
  1641. (error "Search failed: %s" regexp)))
  1642. (setq forms--current-record the-line)
  1643. (forms--show-record the-record))
  1644. (re-search-forward regexp nil t)
  1645. (setq forms--search-regexp regexp))
  1646. (defun forms-save-buffer (&optional args)
  1647. "Forms mode replacement for save-buffer.
  1648. It saves the data buffer instead of the forms buffer.
  1649. Calls `forms-write-file-filter' before, and `forms-read-file-filter'
  1650. after writing out the data."
  1651. (interactive "p")
  1652. (forms--checkmod)
  1653. (let ((write-file-filter forms-write-file-filter)
  1654. (read-file-filter forms-read-file-filter)
  1655. (cur forms--current-record))
  1656. (with-current-buffer forms--file-buffer
  1657. (let ((inhibit-read-only t))
  1658. ;; Write file hooks are run via write-file-functions.
  1659. ;; (if write-file-filter
  1660. ;; (save-excursion
  1661. ;; (run-hooks 'write-file-filter)))
  1662. ;; If they have a write-file-filter, force the buffer to be
  1663. ;; saved even if it doesn't seem to be changed. First, they
  1664. ;; might have changed the write-file-filter; and second, if
  1665. ;; save-buffer does nothing, write-file-filter won't get run,
  1666. ;; and then read-file-filter will be mightily confused.
  1667. (or (null write-file-filter)
  1668. (set-buffer-modified-p t))
  1669. (save-buffer args)
  1670. (if read-file-filter
  1671. (save-excursion
  1672. (run-hooks 'read-file-filter)))
  1673. (set-buffer-modified-p nil)))
  1674. ;; Make sure we end up with the same record number as we started.
  1675. ;; Since read-file-filter may perform arbitrary transformations on
  1676. ;; the data buffer contents, save-excursion is not enough.
  1677. (forms-jump-record cur))
  1678. t)
  1679. (defun forms--revert-buffer (&optional _arg noconfirm)
  1680. "Reverts current form to un-modified."
  1681. (interactive "P")
  1682. (if (or noconfirm
  1683. (yes-or-no-p "Revert form to unmodified? "))
  1684. (progn
  1685. (set-buffer-modified-p nil)
  1686. (forms-jump-record forms--current-record))))
  1687. (defun forms-next-field (arg)
  1688. "Jump to ARG-th next field."
  1689. (interactive "p")
  1690. (let ((i 0)
  1691. (here (point))
  1692. there
  1693. (cnt 0)
  1694. (inhibit-point-motion-hooks t))
  1695. (if (zerop arg)
  1696. (setq cnt 1)
  1697. (setq cnt (+ cnt arg)))
  1698. (if (catch 'done
  1699. (while (< i (length forms--markers))
  1700. (if (or (null (setq there (aref forms--markers i)))
  1701. (<= there here))
  1702. nil
  1703. (if (<= (setq cnt (1- cnt)) 0)
  1704. (progn
  1705. (goto-char there)
  1706. (throw 'done t))))
  1707. (setq i (1+ i))))
  1708. nil
  1709. (goto-char (aref forms--markers 0)))))
  1710. (defun forms-prev-field (arg)
  1711. "Jump to ARG-th previous field."
  1712. (interactive "p")
  1713. (let ((i (length forms--markers))
  1714. (here (point))
  1715. there
  1716. (cnt 0)
  1717. (inhibit-point-motion-hooks t))
  1718. (if (zerop arg)
  1719. (setq cnt 1)
  1720. (setq cnt (+ cnt arg)))
  1721. (if (catch 'done
  1722. (while (> i 0)
  1723. (setq i ( 1- i))
  1724. (if (or (null (setq there (aref forms--markers i)))
  1725. (>= there here))
  1726. nil
  1727. (if (<= (setq cnt (1- cnt)) 0)
  1728. (progn
  1729. (goto-char there)
  1730. (throw 'done t))))))
  1731. nil
  1732. (goto-char (aref forms--markers (1- (length forms--markers)))))))
  1733. (defun forms-print ()
  1734. "Send the records to the printer with `print-buffer', one record per page."
  1735. (interactive)
  1736. (let ((inhibit-read-only t)
  1737. (save-record forms--current-record)
  1738. (total-nb-records forms--total-records)
  1739. (nb-record 1)
  1740. (record nil))
  1741. (while (<= nb-record forms--total-records)
  1742. (forms-jump-record nb-record)
  1743. (setq record (buffer-string))
  1744. (with-current-buffer (get-buffer-create "*forms-print*")
  1745. (goto-char (buffer-end 1))
  1746. (insert record)
  1747. (setq buffer-read-only nil)
  1748. (if (< nb-record total-nb-records)
  1749. (insert "\n \n")))
  1750. (setq nb-record (1+ nb-record)))
  1751. (with-current-buffer "*forms-print*"
  1752. (print-buffer)
  1753. (set-buffer-modified-p nil)
  1754. (kill-buffer (current-buffer)))
  1755. (forms-jump-record save-record)))
  1756. ;;;
  1757. ;;; Special service
  1758. ;;;
  1759. (defun forms-enumerate (the-fields)
  1760. "Take a quoted list of symbols, and set their values to sequential numbers.
  1761. The first symbol gets number 1, the second 2 and so on.
  1762. It returns the highest number.
  1763. Usage: (setq forms-number-of-fields
  1764. (forms-enumerate
  1765. '(field1 field2 field2 ...)))"
  1766. (let ((the-index 0))
  1767. (while the-fields
  1768. (setq the-index (1+ the-index))
  1769. (let ((el (car-safe the-fields)))
  1770. (setq the-fields (cdr-safe the-fields))
  1771. (set el the-index)))
  1772. the-index))
  1773. ;;; Debugging
  1774. (defcustom forms--debug nil
  1775. "If non-nil, enable Forms mode debugging."
  1776. :type 'boolean
  1777. :group 'forms)
  1778. (defun forms--debug (&rest args)
  1779. "Internal debugging routine."
  1780. (if forms--debug
  1781. (let ((ret nil))
  1782. (while args
  1783. (let ((el (car-safe args)))
  1784. (setq args (cdr-safe args))
  1785. (if (stringp el)
  1786. (setq ret (concat ret el))
  1787. (setq ret (concat ret (prin1-to-string el) " = "))
  1788. (if (boundp el)
  1789. (let ((vel (eval el)))
  1790. (setq ret (concat ret (prin1-to-string vel) "\n")))
  1791. (setq ret (concat ret "<unbound>" "\n")))
  1792. (if (fboundp el)
  1793. (setq ret (concat ret (prin1-to-string (symbol-function el))
  1794. "\n"))))))
  1795. (with-current-buffer (get-buffer-create "*forms-mode debug*")
  1796. (if (zerop (buffer-size))
  1797. (emacs-lisp-mode))
  1798. (goto-char (point-max))
  1799. (insert ret)))))
  1800. (provide 'forms-mode) ; for compatibility
  1801. (provide 'forms)
  1802. ;;; forms.el ends here