update packages
This commit is contained in:
@@ -26,6 +26,11 @@
|
||||
(require 'ledger-regex)
|
||||
(require 'ledger-navigate)
|
||||
|
||||
(declare-function calc-renumber-stack "calc" ())
|
||||
(declare-function ledger-add-commodity "ledger-commodities" (c1 c2))
|
||||
(declare-function ledger-commodity-to-string "ledger-commodities" (c1))
|
||||
(declare-function ledger-negate-commodity "ledger-commodities" (c))
|
||||
(declare-function ledger-split-commodity-string "ledger-commodities" (str))
|
||||
(declare-function ledger-string-to-number "ledger-commodities" (str &optional decimal-comma))
|
||||
|
||||
;;; Code:
|
||||
@@ -161,28 +166,81 @@ regular text."
|
||||
(t (call-interactively 'ledger-post-align-xact))))
|
||||
|
||||
(defun ledger-post-edit-amount ()
|
||||
"Call `calc-mode' and push the amount in the posting to the top of stack."
|
||||
"Call `calc' and push the amount in the posting to the top of stack, if any.
|
||||
|
||||
In the calc buffer, press y to use the top value in the stack as
|
||||
the amount and return to ledger."
|
||||
(interactive)
|
||||
(goto-char (line-beginning-position))
|
||||
(beginning-of-line)
|
||||
(when (re-search-forward ledger-post-line-regexp (line-end-position) t)
|
||||
(goto-char (match-end ledger-regex-post-line-group-account)) ;; go to the and of the account
|
||||
(let ((end-of-amount (re-search-forward "[-.,0-9]+" (line-end-position) t)))
|
||||
;; determine if there is an amount to edit
|
||||
(if end-of-amount
|
||||
(let ((val-string (match-string 0)))
|
||||
(goto-char (match-beginning 0))
|
||||
(delete-region (match-beginning 0) (match-end 0))
|
||||
(push-mark)
|
||||
(calc)
|
||||
;; edit the amount, first removing thousands separators and
|
||||
;; converting decimal commas to calc's input format
|
||||
(calc-eval (number-to-string (ledger-string-to-number val-string)) 'push))
|
||||
(progn ;;make sure there are two spaces after the account name and go to calc
|
||||
(if (search-backward " " (- (point) 3) t)
|
||||
(goto-char (line-end-position))
|
||||
(insert " "))
|
||||
(push-mark)
|
||||
(calc))))))
|
||||
(goto-char (match-end ledger-regex-post-line-group-account)) ;; go to the end of the account
|
||||
;; determine if there is an amount to edit
|
||||
(if (re-search-forward ledger-amount-regexp (line-end-position) t)
|
||||
(let ((val-string (match-string 0)))
|
||||
(goto-char (match-beginning 0))
|
||||
(delete-region (match-beginning 0) (match-end 0))
|
||||
(push-mark (point) 'nomsg)
|
||||
(calc)
|
||||
;; edit the amount, first removing thousands separators and converting
|
||||
;; decimal commas to calc's input format
|
||||
(calc-eval (number-to-string (ledger-string-to-number val-string)) 'push)
|
||||
(calc-renumber-stack))
|
||||
;; make sure there are two spaces after the account name and go to calc
|
||||
(if (search-backward " " (- (point) 3) t)
|
||||
(end-of-line)
|
||||
(insert " "))
|
||||
(push-mark (point) 'nomsg)
|
||||
(calc))))
|
||||
|
||||
(defun ledger-post-xact-total ()
|
||||
"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-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). Specifically, the positions are at the end
|
||||
of the account name on such posting lines.
|
||||
|
||||
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 account-end = (match-end ledger-regex-post-line-group-account)
|
||||
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 account-end 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-positions)))))
|
||||
|
||||
(defun ledger-post-fill ()
|
||||
"Find a posting with no amount and insert it.
|
||||
|
||||
Even if ledger allows for one missing amount per transaction, you
|
||||
might want to insert it anyway."
|
||||
(interactive)
|
||||
(pcase-let* ((`(,total . ,missing-positions) (ledger-post-xact-total))
|
||||
(missing-amount (ledger-negate-commodity total))
|
||||
(amounts-balance (< (abs (car missing-amount)) 0.0001)))
|
||||
(pcase missing-positions
|
||||
('() (unless amounts-balance
|
||||
(user-error "Postings do not balance, but no posting to fill")))
|
||||
(`(,missing-pos)
|
||||
(if amounts-balance
|
||||
(user-error "Missing amount but amounts balance already")
|
||||
(goto-char missing-pos)
|
||||
(insert " " (ledger-commodity-to-string missing-amount))
|
||||
(ledger-post-align-xact (point))))
|
||||
(_ (user-error "More than one posting with missing amount")))))
|
||||
|
||||
(provide 'ledger-post)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user