Code: Select all
(defun ledger-next-record-function ()
(if (re-search-forward
(concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+"
"\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)") nil t)
(goto-char (match-beginning 0))
(goto-char (point-max))))
(defun ledger-end-record-function ()
(forward-paragraph))
(defun ledger-sort-region (beg end)
(interactive "r") ;load beg and end from point and mark automagically
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(message "%s %s %s" beg end (point-min))
(let ((inhibit-field-text-motion t))
(sort-subr
nil
'ledger-next-record-function
'ledger-end-record-function)))))
(defun ledger-sort-buffer ()
(interactive)
(ledger-sort-region (point-min) (point-max)))
Here is some sample data for the sort function:
Code: Select all
2013/01/30 * (PYP) EricSMG
Expenses:Auto:Hobbies $ 180.00
Assets:VWCU:Joint
2013/01/29 * (XFR) Buy FBALX
Assets:Investments:401K:Deferred 10.000 FBALX @@ $ 100.00
Assets:Investments:401K:Deferred $ -100.00
Assets:Investments:401K:Match 5.000 FBALX @@ $ 50.00
Income:Exempt:Match
2013/01/30 (POS) CosmoProf
Expenses:Clothing $ 30.00
Assets:VWCU:1234
2013/01/28 * (ONL) Intuit
Expenses:Home $ 19.99
Assets:VWCU:2341