very strange sorting problem

Discussion of Emacs Lisp
Post Reply
[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

very strange sorting problem

Post by [email protected] » Thu Jan 31, 2013 2:49 pm

I have struggled with this one for too long. I am writing a simple function to sort the records in a file. Specifically, financial transactions for the Ledger accounting program. I am using sort-subr and have defined the nextrec and endrec functions properly. This is verified by using sort-subr on the entire buffer, and it works flawlessly. When I try to sort only the records in the active region it ignores the first record and sorts all of the following records properly. I have to assume that afer all this time sort.el is well proven and therefore blameless. Here is my code, along with a snippet of data for it to munch on. Any hints would be greatly appreciated.

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)))
(ledger-sort-buffer) works fine. (ledger-sort-region) ignores the first record in the region and sorts all the others.

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
 

[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: very strange sorting problem

Post by [email protected] » Thu Feb 07, 2013 12:06 pm

Just for completeness I neve could figure this out, but it started working. So, I had some conflicting data somewhere.

Post Reply