 	  (insert "  ")))
       (forward-line))))
 
-(defun ledger-run-ledger (&rest args)
+;; General helper functions
+
+(defun ledger-run-ledger (buffer &rest args)
   "run ledger with supplied arguments"
-  (let ((command
-	 (mapconcat 'identity
-		    (append (list ledger-binary-path
-				  "-f" ledger-data-file) args) " ")))
-    (insert (shell-command-to-string command)))
-  0)
+  (let ((buf (current-buffer)))
+    (with-current-buffer buffer
+      (apply #'call-process-region
+	     (append (list (point-min) (point-max)
+			   ledger-binary-path nil buf nil "-f" "-")
+		     args)))))
 
 (defun ledger-set-year (newyear)
   "Set ledger's idea of the current year to the prefix argument."

   (list
    (read-string "Entry: " (concat ledger-year "/" ledger-month "/"))))
  (let* ((date (car (split-string entry-text)))
	 (insert-year t) exit-code)
    (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
	(setq date (encode-time 0 0 0 (string-to-int (match-string 3 date))
				(string-to-int (match-string 2 date))

	 (kill-buffer (current-buffer)))))
    (use-local-map map)))

;; A sample function for $ users

(defun ledger-align-dollars (&optional column)
  (interactive "p")
