diff options
author | Craig Earls <enderw88@gmail.com> | 2013-02-19 16:44:53 -0700 |
---|---|---|
committer | Craig Earls <enderw88@gmail.com> | 2013-02-19 16:44:53 -0700 |
commit | 4ebd17efb391b5236c69f5d7eb3b5852a962fe58 (patch) | |
tree | e7ca890324bffaa5d79710f3eff12d6105ae54f1 | |
parent | 4f9c12454072190f7e0f26ebbfc6af35a69fd8f3 (diff) | |
download | fork-ledger-4ebd17efb391b5236c69f5d7eb3b5852a962fe58.tar.gz fork-ledger-4ebd17efb391b5236c69f5d7eb3b5852a962fe58.tar.bz2 fork-ledger-4ebd17efb391b5236c69f5d7eb3b5852a962fe58.zip |
Better way of splitting the commodity from the value.
Should allow no spaces between commodities and values.
-rw-r--r-- | lisp/ldg-commodities.el | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/lisp/ldg-commodities.el b/lisp/ldg-commodities.el index 04dc23de..1b6b332a 100644 --- a/lisp/ldg-commodities.el +++ b/lisp/ldg-commodities.el @@ -37,20 +37,30 @@ This only has effect interfacing to calc mode in edit amount" :type 'boolean :group 'ledger) +(defun ledger-split-commodity-string (str) + "Split a commoditized amount into two parts" + (let (val + comm) + (with-temp-buffer + (insert str) + (goto-char (point-min)) + (re-search-forward "-?[1-9][0-9]*[.,][0-9]*") + (setq val + (string-to-number + (ledger-commodity-string-number-decimalize + (delete-and-extract-region (match-beginning 0) (match-end 0)) :from-user))) + (delete-trailing-whitespace) + (setq comm (buffer-substring (point-min) (point-max))) + (list val comm)))) + + (defun ledger-string-balance-to-commoditized-amount (str) "Return a commoditized amount (val, 'comm') from STR." (let ((fields (split-string str "[\n\r]"))) ; break any balances ; with multi commodities ; into a list (mapcar '(lambda (str) - (let* ((parts (split-string str)) ;break into number and commodity string - (first (car parts)) - (second (cadr parts))) - (if (string-match "^-*[1-9]+" first) - (list (string-to-number - (ledger-commodity-string-number-decimalize first :from-user)) second) - (list (string-to-number - (ledger-commodity-string-number-decimalize second :from-user)) first)))) + (ledger-split-commodity-string str)) fields))) |