 ;;
 (defvar auto-items)
 
-(defun ledger-schedule-test-setup ()
-  (setq auto-items 
-	(ledger-schedule-scan-transactions ledger-schedule-file)))
+(defun ledger-schedule-test ( early horizon)
+   (ledger-schedule-create-auto-buffer 
+    (ledger-schedule-scan-transactions ledger-schedule-file) 
+    early 
+    horizon 
+    (get-buffer "2013.ledger")))
 
 
 (defun ledger-schedule-test-predict ()

    
    ;; read the descriptor string into a lisp object the transform the
    ;; string descriptor into useable things
    (ledger-transform-auto-tree 
     (read (buffer-substring-no-properties (point-min) (point-max))))))

(defun ledger-transform-auto-tree (tree)
"Takes a lisp list of date descriptor strings, TREE, and returns a string with a lambda function of date."
;; use funcall to use the lambda function spit out here
  (if (consp tree)
      (let (result)
	(while (consp tree)
	  (let ((newcar (car tree)))
	    (if (consp newcar)
		(setq newcar (ledger-transform-auto-tree (car tree))))
	    (if (consp newcar) 
		(push newcar result)
		(push (ledger-schedule-parse-date-descriptor newcar) result)) )
	  (setq tree (cdr tree)))

	;; tie up all the clauses in a big or and lambda
	`(lambda (date) 
	   ,(nconc (list 'or) (nreverse result) tree)))))

(defun ledger-schedule-split-constraints (descriptor-string)
  "Return a list with the year, month and day fields split"
  (let ((fields (split-string descriptor-string "[/\\-]" t))
	constrain-year constrain-month constrain-day)
    (if (string= (car fields) "*")
	(setq constrain-year nil)
	(setq constrain-year (car fields)))
    (if (string= (cadr fields) "*")
	(setq constrain-month nil)
	(setq constrain-month (cadr fields)))
    (if (string= (nth 2 fields) "*")
	(setq constrain-day nil)
	(setq constrain-day (nth 2 fields)))
    (list constrain-year constrain-month constrain-day)))

(defun ledger-string-to-number-or-nil (str)
  (if str
      (string-to-number str)
      nil))

(defun ledger-schedule-compile-constraints (constraint-list)
  (let ((year-constraint (ledger-string-to-number-or-nil (nth 0 constraint-list)))
	(month-constraint (ledger-string-to-number-or-nil (nth 1 constraint-list)))
	(day-constraint (ledger-string-to-number-or-nil (nth 2 constraint-list))))
    (ledger-schedule-constrain-numerical-date-macro 
     year-constraint
     month-constraint

	    (between (eval day) 1 (ledger-schedule-days-in-month (eval month) (eval year))))
       (between (eval day) 1 31))  ;; no month specified, assume 31 days.
   `'(and ,(if (eval year)  
	       `(eq (nth 5 (decode-time date)) ,(eval year))
	       `t)
	   ,(if (eval month)
		`(eq (nth 4 (decode-time date)) ,(eval month))
		`t)
	   ,(if (eval day)
		`(eq (nth 3 (decode-time date)) ,(eval day))))
   (error "ledger-schedule-constraint-numerical-date-macro: date out of range %S %S %S" (eval year) (eval month) (eval day))))


