;;; -*- Mode: Emacs-Lisp -*- 
;;; arb-demo.dba

;;; This database auxiliary file is for use with the database file arb-demo.

(database-set-fieldnames-to-list database '(place time purpose))
(database-set-print-name database "Mike's Time & Place Database")

;; Separating
(sepinfo-set-sep-string (database-record-sepinfo database) "\n\n")

;; Reading
(database-set-read-record-from-region database 'arb-demo-rrfr)
      
;; Writing
(database-set-write-region-from-record database 'arb-demo-wrfr)

;; Extra characters at end of file 
(sepinfo-set-post-last-string (database-record-sepinfo database) "\n")


;; This function (correctly) assumes the buffer is narrowed to the region.
(defun arb-demo-rrfr ()
  (goto-char (point-min))
  (if (re-search-forward
       "Place:[ \t]*\\(.*\\)\nTime:[ \t]*\\(.*\\)\nPurpose:[ \t]*\\(.*\\)" nil t)
      (let ((result-record (make-record database)))
	(record-set-field result-record 'place (match-string 1) database)
	(record-set-field result-record 'time (match-string 2) database)
	(record-set-field result-record 'purpose (match-string 3) database)
	result-record)
    (error "This didn't look right to me.")))

(defun arb-demo-wrfr (record)
  (insert "Place:   " (record-field record 'place database)
	  "\nTime:    " (record-field record 'time database)
	  "\nPurpose: " (record-field record 'purpose database)))

;; Automatically choose the appropriate format for the current record.
(defun arb-demo-set-format-from-data (record)
  ;; (message "place = %s" (record-field record 'place dbc-database))
  (if (string= "Home" (record-field record 'place dbc-database))
      (db-alternate-format "home format")
    (db-alternate-format "non-home format")))

(setq dbf-alternate-format-names
      '(("home format" . "arb-demo.home-fmt")
	("non-home format" . "arb-demo.fmt")))

(setq dbf-before-display-record-function 'arb-demo-set-format-from-data)
