A better try to deal with decimal-comma from ledger

This commit is contained in:
Craig Earls 2013-02-17 19:47:16 -07:00
parent 12db87f4c0
commit 2c69aa1ff5
5 changed files with 51 additions and 29 deletions

View file

@ -31,6 +31,12 @@
:type 'string
:group 'ledger)
(defcustom ledger-use-decimal-comma nil
"If non-nil the use commas as decimal separator.
This only has effect interfacing to calc mode in edit amount"
:type 'boolean
:group 'ledger)
(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
@ -40,10 +46,11 @@
(let* ((parts (split-string str)) ;break into number and commodity string
(first (car parts))
(second (cadr parts)))
;"^-*[1-9][0-9]*[.,][0-9]*"
(if (string-match "^-*[1-9]+" first)
(list (string-to-number first) second)
(list (string-to-number second) 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))))
fields)))
@ -59,15 +66,38 @@
(list (+ (car c1) (car c2)) (cadr c1))
(error "Can't add different commodities, %S to %S" c1 c2)))
(defun ledger-commodity-string-number-decimalize (number-string direction)
"Take NUMBER-STRING and ensure proper decimalization for use by string-to-number and number-to-string.
DIRECTION can be :to-user or :from-user. All math calculations
are done with decimal-period, some users may prefer decimal-comma
which must be translated both directions."
(let ((val number-string))
(if ledger-use-decimal-comma
(cond ((eq direction :from-user)
;; change string to decimal-period
(while (string-match "," val)
(setq val (replace-match "." nil nil val)))) ;; switch to period separator
((eq direction :to-user)
;; change to decimal-comma
(while (string-match "\\." val)
(setq val (replace-match "," nil nil val)))) ;; gets rid of periods
(t
(error "ledger-commodity-string-number-decimalize: direction not properly specified %S" direction))))
val))
(defun ledger-commodity-to-string (c1)
"Return string representing C1.
Single character commodities are placed ahead of the value,
longer one are after the value."
(let ((val (number-to-string (car c1)))
(commodity (cadr c1)))
(if (> (length commodity) 1)
(concat val " " commodity)
(concat commodity " " val))))
(let ((val (ledger-commodity-string-number-decimalize
(number-to-string (car c1)) :to-user))
(commodity (cadr c1)))
(if (> (length commodity) 1)
(concat val " " commodity)
(concat commodity " " val))))
(defun ledger-read-commodity-string (prompt)
"Return a commoditizd value (val 'comm') from COMM.

View file

@ -164,10 +164,10 @@ Does not use ledger xact"
xacts))
(forward-line))
(setq xacts (nreverse xacts)))))
;; Insert rest-of-name and the postings
(when xacts
(save-excursion
(insert rest-of-name)
(insert ?\n)
(insert rest-of-name ?\n)
(while xacts
(insert (car xacts) ?\n)
(setq xacts (cdr xacts))))

View file

@ -51,12 +51,6 @@
:type 'boolean
:group 'ledger-post)
(defcustom ledger-post-use-decimal-comma nil
"If non-nil the use commas as decimal separator.
This only has effect interfacing to calc mode in edit amount"
:type 'boolean
:group 'ledger-post)
(defun ledger-post-all-accounts ()
"Return a list of all accounts in the buffer."
(let ((origin (point))
@ -185,7 +179,7 @@ BEG, END, and LEN control how far it can align."
(goto-char (match-beginning 0))
(delete-region (match-beginning 0) (match-end 0))
(calc)
(if ledger-post-use-decimal-comma
(if ledger-use-decimal-comma
(progn
(while (string-match "\\." val)
(setq val (replace-match "" nil nil val))) ;; gets rid of periods

View file

@ -81,7 +81,7 @@ numbers"
"Calculate the cleared balance of the account being reconciled."
(interactive)
(let* ((pending (car (ledger-string-balance-to-commoditized-amount
(car (ledger-reconcile-get-balances)))))
(car (ledger-reconcile-get-balances)))))
(target-delta (if ledger-target
(-commodity ledger-target pending)
nil)))

View file

@ -21,7 +21,7 @@
;;; Commentary:
;;
;; Provide facilities for running and saving reports in emacs
;;; Code:
@ -51,7 +51,8 @@ specifier."
(defcustom ledger-report-format-specifiers
'(("ledger-file" . ledger-report-ledger-file-format-specifier)
("payee" . ledger-report-payee-format-specifier)
("account" . ledger-report-account-format-specifier))
("account" . ledger-report-account-format-specifier)
("value" . ledger-report-value-format-specifier))
"An alist mapping ledger report format specifiers to implementing functions.
The function is called with no parameters and expected to return the
@ -59,15 +60,6 @@ text that should replace the format specifier."
:type 'alist
:group 'ledger)
;;(define-key map [(control ?c) (control ?o) (control ?r)] 'ledger-report)
;;(define-key map [(control ?c) (control ?o) (control ?g)] 'ledger-report-goto)
;;(define-key map [(control ?c) (control ?o) (control ?a)] 'ledger-report-redo)
;;(define-key map [(control ?c) (control ?o) (control ?s)] 'ledger-report-save)
;;(define-key map [(control ?c) (control ?o) (control ?e)] 'ledger-report-edit)
;;(define-key map [(control ?c) (control ?o) (control ?k)] 'ledger-report-kill)
;; Ledger report mode
(defvar ledger-report-buffer-name "*Ledger Report*")
(defvar ledger-report-name nil)
@ -128,6 +120,12 @@ text that should replace the format specifier."
(use-local-map map)))
(defun ledger-report-value-format-specifier ()
"Return a valid meta-data tag name"
;; It is intended completion should be available on existing account
;; names, but it remains to be implemented.
(ledger-read-string-with-default "Value: " nil))
(defun ledger-report-read-name ()
"Read the name of a ledger report to use, with completion.