From 7d81963b956f83d7616eb9759becd7c7ee872b7f Mon Sep 17 00:00:00 2001 From: Fredrik Salomonsson Date: Tue, 5 Mar 2019 21:44:13 -0800 Subject: [PATCH] Added support to align amounts that are an expression. Will pick the last value to align against. --- ledger-regex.el | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/ledger-regex.el b/ledger-regex.el index 9d858c808..4373cf994 100644 --- a/ledger-regex.el +++ b/ledger-regex.el @@ -23,19 +23,36 @@ (require 'cl-lib) (defconst ledger-amount-regex - (concat "\\( \\|\t\\| \t\\)[ \t]*-?" - "\\([A-Z$€£₹_(]+ *\\)?" - ;; We either match just a number after the commodity with no - ;; decimal or thousand separators or a number with thousand - ;; separators. If we have a decimal part starting with `,' - ;; or `.', because the match is non-greedy, it must leave at - ;; least one of those symbols for the following capture - ;; group, which then finishes the decimal part. - "\\(-?\\(?:[0-9]+\\|[0-9,.]+?\\)\\)" - "\\([,.][0-9)]+\\)?" - "\\( *[[:word:]€£₹_\"]+\\)?" - "\\([ \t]*[@={]@?[^\n;]+?\\)?" - "\\([ \t]+;.+?\\|[ \t]*\\)?$")) + + (let ((prefix-commodity-re "[A-Z$€£₹_(]+ *") + (suffix-commodity-re " *[[:word:]€£₹_\"]+") + (value-re "-?\\(?:[0-9]+\\|[0-9,.]+?\\)") + (decimal-re "[,.][0-9)]+")) + (concat "\\( \\|\t\\| \t\\)[ \t]*-?" + "(?[ \t]*" + + ;; If it's an expression pick the last value. + "\\(?:" + "\\(?:" prefix-commodity-re "\\)?" + "\\(?:" value-re "\\)" + "\\(?:" decimal-re "\\)?" + "\\(?:" suffix-commodity-re "\\)?" + "\\(?:[ \t]*[+*/-][ \t]*\\)?" + "\\)*?" + + "\\(" prefix-commodity-re "\\)?" + ;; We either match just a number after the commodity with no + ;; decimal or thousand separators or a number with thousand + ;; separators. If we have a decimal part starting with `,' + ;; or `.', because the match is non-greedy, it must leave at + ;; least one of those symbols for the following capture + ;; group, which then finishes the decimal part. + "\\(" value-re "\\)" + "\\(" decimal-re "\\)?" + "\\(" suffix-commodity-re "\\)?" + "[ \t]*)?" + "\\([ \t]*[@={]@?[^\n;]+?\\)?" + "\\([ \t]+;.+?\\|[ \t]*\\)?$"))) (defconst ledger-amount-decimal-comma-regex "-?[1-9][0-9.]*[,]?[0-9]*")