Skip to content

Commit

Permalink
ledger-post-fill: Fix bug
Browse files Browse the repository at this point in the history
Avoid searching for postings with no amount twice. Instead, change
`ledger-post-xact-total` to return positions where amounts should be
inserted.
  • Loading branch information
DamienCassou committed May 5, 2024
1 parent 0c9c847 commit 9e7e340
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
36 changes: 18 additions & 18 deletions ledger-post.el
Original file line number Diff line number Diff line change
Expand Up @@ -193,29 +193,32 @@ the amount and return to ledger."
(calc))))

(defun ledger-post-xact-total ()
"Return (TOTAL . MISSING-COUNT) for the transaction at point.
"Return (TOTAL . MISSING-POSITIONS) for the transaction at point.
TOTAL is a commoditized amount representing the total amount of
the postings in the transaction.
MISSING-COUNT is the number of postings in the transaction that
do not have an amount specified (such postings do not contribute
to TOTAL).
MISSING-POSITIONS is a list of positions in the buffer where the
transaction do not have an amount specified (such postings do not
contribute to TOTAL).
Error if the commodities do not match."
(save-excursion
(pcase-let ((`(,begin ,end) (ledger-navigate-find-xact-extents (point))))
(goto-char begin)
(cl-loop
while (re-search-forward ledger-post-line-regexp end t)
for amount-string = (match-string ledger-regex-post-line-group-amount)
count (not amount-string) into missing-count
if amount-string
for amount-string = (when-let ((amount-string (match-string ledger-regex-post-line-group-amount)))
(unless (string-empty-p (string-trim amount-string))
amount-string))
if (not amount-string)
collect (or (match-end ledger-regex-post-line-group-account) (point)) into missing-positions
else
collect (ledger-split-commodity-string amount-string) into amounts
finally return (cons (if amounts
(cl-reduce #'ledger-add-commodity amounts)
'(0 nil))
missing-count)))))
missing-positions)))))

(defun ledger-post-fill ()
"Find a posting with no amount and insert it.
Expand All @@ -224,22 +227,19 @@ Even if ledger allows for one missing amount per transaction, you
might want to insert it anyway."
(interactive)
(pcase-let* ((`(,begin ,end) (ledger-navigate-find-xact-extents (point)))
(`(,total . ,missing-count) (ledger-post-xact-total))
(`(,total . ,missing-positions) (ledger-post-xact-total))
(missing-amount (ledger-negate-commodity total))
(amounts-balance (< (abs (car missing-amount)) 0.0001)))
(pcase missing-count
(pcase (length missing-positions)
(0 (unless amounts-balance
(user-error "Postings do not balance, but no posting to fill")))
(1 (if amounts-balance
(user-error "Missing amount but amounts balance already")
(goto-char begin)
(cl-loop
while (re-search-forward ledger-post-line-regexp end t)
unless (match-beginning ledger-regex-post-line-group-amount)
do
(goto-char (match-end ledger-regex-post-line-group-account))
(insert " " (ledger-commodity-to-string missing-amount))
and return nil)
(goto-char (car missing-positions))
(insert " " (ledger-commodity-to-string missing-amount))
(just-one-space 0)
(unless (equal (point) (line-end-position))
(just-one-space 2))
(ledger-post-align-xact (point))))
(_ (user-error "More than one posting with missing amount")))))

Expand Down
32 changes: 29 additions & 3 deletions test/post-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,18 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946"

(should
(equal (ledger-post-xact-total)
'((10 "$") . 1))))
'((10 "$") . (83)))))

;; one amount missing with trailing spaces
(ledger-tests-with-temp-file
"\
2013-05-01 foo
Expenses:Foo $10
Assets:Bar \n"

(should
(equal (ledger-post-xact-total)
'((10 "$") . (83)))))

;; all amounts missing
(ledger-tests-with-temp-file
Expand All @@ -480,7 +491,7 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946"

(should
(equal (ledger-post-xact-total)
'((0 nil) . 2))))
'((0 nil) . (32 47)))))

;; no amounts missing
(ledger-tests-with-temp-file
Expand All @@ -492,7 +503,7 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946"

(should
(equal (ledger-post-xact-total)
'((0 "$") . 0)))))
'((0 "$") . nil)))))


(ert-deftest ledger-post/test-post-xact-total-002 ()
Expand Down Expand Up @@ -531,6 +542,21 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946"
Assets:Bar $ -10
")))

;; trailing spaces
(ledger-tests-with-temp-file
"\
2013-05-01 foo
Expenses:Foo $10
Assets:Bar \n"
(ledger-post-fill)
(should
(equal (buffer-string)
"\
2013-05-01 foo
Expenses:Foo $10
Assets:Bar $ -10
")))

;; no commodity
(ledger-tests-with-temp-file
"\
Expand Down

0 comments on commit 9e7e340

Please sign in to comment.