From 702bbed157a8c4a3f59e07d4db7c805951453384 Mon Sep 17 00:00:00 2001 From: "Kevin J. Foley" Date: Sat, 9 Nov 2019 13:32:19 -0500 Subject: [PATCH] Handle prefixed negation on commoditized amounts This includes prefixed `-` characters when matching commoditized amounts. --- ledger-regex.el | 23 ++++++++++++++++------- test/context-test.el | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ledger-regex.el b/ledger-regex.el index ab8f7522f..06a925582 100644 --- a/ledger-regex.el +++ b/ledger-regex.el @@ -323,13 +323,22 @@ (ledger-define-regexp commoditized-amount (macroexpand `(rx (group - (or (and (regexp ,ledger-commodity-no-group-regexp) - (*? blank) - (regexp ,ledger-amount-no-group-regexp)) - (and (regexp ,ledger-amount-no-group-regexp) - (*? blank) - (regexp ,ledger-commodity-no-group-regexp)))))) - "") + (or + ;; Match commodity before amount, with optional + ;; minus sign allowed before commodity. + + ;; Ex: "$100" or "-$100" + (and (opt ?-) + (regexp ,ledger-commodity-no-group-regexp) + (*? blank) + (regexp ,ledger-amount-no-group-regexp)) + ;; Match commodity after amount + + ;; Ex: "100 Dollars" + (and (regexp ,ledger-amount-no-group-regexp) + (*? blank) + (regexp ,ledger-commodity-no-group-regexp)))))) + "Regexp to match a commodity with ammount such as \"$100\" or \"100 Dollars\"") (ledger-define-regexp commodity-annotations (macroexpand diff --git a/test/context-test.el b/test/context-test.el index ca14614d3..30d403b5e 100644 --- a/test/context-test.el +++ b/test/context-test.el @@ -268,6 +268,22 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=257" (should (equal "$1,000.00" (ledger-context-field-value context 'commoditized-amount)))))) +(ert-deftest ledger-context/test-009 () + "Regress test for #211 +https://github.com/ledger/ledger-mode/issues/211" + :tags '(context regress) + + (ledger-tests-with-temp-file + "2004/05/01 * Checking balance + Assets:Bank:Checking -$1,000.00 + Equity:Opening Balances +" + (goto-char 65) ; on the amount 1,000.00 + (let ((context (ledger-context-at-point))) + (should (eq (ledger-context-current-field context) 'commoditized-amount)) + (should (equal "-$1,000.00" + (ledger-context-field-value context 'commoditized-amount)))))) + (provide 'context-test)