   (save-excursion
     (goto-char (point-min))
     (if (ledger-sort-find-end)
-	(delete-region (match-beginning 0) (match-end 0))))
+				(delete-region (match-beginning 0) (match-end 0))))
   (beginning-of-line)
   (insert "\n; Ledger-mode: End sort\n\n"))
 

(defun ledger-sort-region (beg end)
  "Sort the region from BEG to END in chronological order."
  (interactive "r") ;; load beg and end from point and mark
		    ;; automagically
  (let ((new-beg beg)
	(new-end end))
    (setq inhibit-modification-hooks t)
    (save-excursion 
      (save-restriction
	(goto-char beg)
	(ledger-next-record-function) ;; make sure point is at the
				      ;; beginning of a xact
	(setq new-beg (point))
	(goto-char end)
	(ledger-next-record-function) ;; make sure end of region is at
				      ;; the beginning of next record
				      ;; after the region
	(setq new-end (point))
	(narrow-to-region new-beg new-end)
	(goto-char new-beg)

	(let ((inhibit-field-text-motion t))
	  (sort-subr
	   nil
	   'ledger-next-record-function
	   'ledger-end-record-function
	   'ledger-sort-startkey))))
    (setq inhibit-modification-hooks nil)))

(defun ledger-sort-buffer ()
  "Sort the entire buffer."
  (interactive)
  (goto-char (point-min))
  (let ((sort-start (ledger-sort-find-start))
	(sort-end (ledger-sort-find-end)))
    (ledger-sort-region (if sort-start
			    sort-start
			    (point-min)) 
			(if sort-end
			    sort-end
			    (point-max)))))

(provide 'ldg-sort)


  (save-excursion
    (goto-char (point-min))
    (if (ledger-sort-find-start)
				(delete-region (match-beginning 0) (match-end 0))))
  (beginning-of-line)
  (insert "\n; Ledger-mode: Start sort\n\n"))

