Align post speed improvements after adding the long account name handling.

This commit is contained in:
Craig Earls 2013-03-27 20:02:11 -07:00
parent 4ca0e8916b
commit 7fea9d21fb
2 changed files with 27 additions and 24 deletions

View file

@ -136,21 +136,17 @@ point at beginning of the commodity."
(match-end 3)) (point)))) (match-end 3)) (point))))
(defvar ledger-post-account-regex (defvar ledger-post-account-regex
"\\(^[ \t]+\\)\\([\\[(;!*A-Za-z0-9]\\)\\(.+?\\)\\( \\|\n\\)") "\\(^[ \t]+\\)\\(.+?\\)\\( \\|\n\\)")
(defun ledger-next-account (&optional end) (defun ledger-next-account (&optional end)
"Move point to the beginning of the next account, or status marker (!*), as long as it is not past END. "Move point to the beginning of the next account, or status marker (!*), as long as it is not past END.
Return the column of the beginning of the account and leave point Return the column of the beginning of the account and leave point
at beginning of account" at beginning of account"
(if (> end (point)) (if (> end (point))
(when (re-search-forward ledger-post-account-regex (1+ end) t) (when (re-search-forward ledger-post-account-regex (1+ end) t)
;; the 1+ is to make sure we can catch the newline
(goto-char (match-beginning 2)) (goto-char (match-beginning 2))
(list (current-column) (length (match-string-no-properties 3)))))) (current-column))))
(defsubst ledger-post-adjust (adjust-by)
(if (> adjust-by 0)
(insert (make-string adjust-by ? ))
(delete-char adjust-by)))
(defun ledger-post-align-postings (&optional beg end) (defun ledger-post-align-postings (&optional beg end)
"Align all accounts and amounts within region, if there is no "Align all accounts and amounts within region, if there is no
@ -178,24 +174,31 @@ region align the posting on the current line."
(goto-char (goto-char
(setq begin-region (setq begin-region
(line-beginning-position))) (line-beginning-position)))
;; This is the guts of the alignment loop ;; This is the guts of the alignment loop
(while (and (or (setq acc-col (ledger-next-account (line-end-position))) (while (and (or (setq acc-col (ledger-next-account (line-end-position)))
lines-left) lines-left)
(< (point) end-region)) (< (point) end-region))
(when acc-col (when acc-col
(if (/= (setq acc-adjust (- ledger-post-account-alignment-column (car acc-col))) 0) (when (/= (setq acc-adjust (- ledger-post-account-alignment-column acc-col)) 0)
(ledger-post-adjust acc-adjust)) (if (> acc-adjust 0)
(insert (make-string acc-adjust ? ))
(when (and (delete-char acc-adjust)))
(> ledger-post-amount-alignment-column (+ ledger-post-account-alignment-column (cadr acc-col))) (when (setq amt-offset (ledger-next-amount (line-end-position)))
(setq amt-offset (ledger-next-amount (line-end-position)))) (let* ((amt-adjust (- ledger-post-amount-alignment-column
(let* ((amt-adjust (- ledger-post-amount-alignment-column amt-offset
amt-offset (current-column))))
(current-column)))) (if (/= amt-adjust 0)
(if (/= amt-adjust 0) (if (> amt-adjust 0)
(ledger-post-adjust amt-adjust))))) (insert (make-string amt-adjust ? ))
(forward-line) (let ((curpoint (point)))
(beginning-of-line)
(ledger-next-account (line-end-position))
(when (> (+ curpoint amt-adjust)
(match-end 2))
(goto-char curpoint)
(delete-char amt-adjust))))))))
(forward-line)
(setq lines-left (not (eobp)))) (setq lines-left (not (eobp))))
(setq inhibit-modification-hooks nil)))) (setq inhibit-modification-hooks nil))))

View file

@ -21,7 +21,7 @@
;;; Commentary: ;;; Commentary:
;; Utilites for running ledger synchronously. ;; Utilities for running ledger synchronously.
;;; Code: ;;; Code: