A better try to deal with decimal-comma from ledger
This commit is contained in:
parent
12db87f4c0
commit
2c69aa1ff5
5 changed files with 51 additions and 29 deletions
|
|
@ -31,6 +31,12 @@
|
||||||
:type 'string
|
:type 'string
|
||||||
:group 'ledger)
|
: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)
|
(defun ledger-string-balance-to-commoditized-amount (str)
|
||||||
"Return a commoditized amount (val, 'comm') from STR."
|
"Return a commoditized amount (val, 'comm') from STR."
|
||||||
(let ((fields (split-string str "[\n\r]"))) ; break any balances
|
(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
|
(let* ((parts (split-string str)) ;break into number and commodity string
|
||||||
(first (car parts))
|
(first (car parts))
|
||||||
(second (cadr parts)))
|
(second (cadr parts)))
|
||||||
;"^-*[1-9][0-9]*[.,][0-9]*"
|
|
||||||
(if (string-match "^-*[1-9]+" first)
|
(if (string-match "^-*[1-9]+" first)
|
||||||
(list (string-to-number first) second)
|
(list (string-to-number
|
||||||
(list (string-to-number second) first))))
|
(ledger-commodity-string-number-decimalize first :from-user)) second)
|
||||||
|
(list (string-to-number
|
||||||
|
(ledger-commodity-string-number-decimalize second :from-user)) first))))
|
||||||
fields)))
|
fields)))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -59,15 +66,38 @@
|
||||||
(list (+ (car c1) (car c2)) (cadr c1))
|
(list (+ (car c1) (car c2)) (cadr c1))
|
||||||
(error "Can't add different commodities, %S to %S" c1 c2)))
|
(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)
|
(defun ledger-commodity-to-string (c1)
|
||||||
"Return string representing C1.
|
"Return string representing C1.
|
||||||
Single character commodities are placed ahead of the value,
|
Single character commodities are placed ahead of the value,
|
||||||
longer one are after the value."
|
longer one are after the value."
|
||||||
(let ((val (number-to-string (car c1)))
|
(let ((val (ledger-commodity-string-number-decimalize
|
||||||
(commodity (cadr c1)))
|
(number-to-string (car c1)) :to-user))
|
||||||
(if (> (length commodity) 1)
|
(commodity (cadr c1)))
|
||||||
(concat val " " commodity)
|
(if (> (length commodity) 1)
|
||||||
(concat commodity " " val))))
|
(concat val " " commodity)
|
||||||
|
(concat commodity " " val))))
|
||||||
|
|
||||||
(defun ledger-read-commodity-string (prompt)
|
(defun ledger-read-commodity-string (prompt)
|
||||||
"Return a commoditizd value (val 'comm') from COMM.
|
"Return a commoditizd value (val 'comm') from COMM.
|
||||||
|
|
|
||||||
|
|
@ -164,10 +164,10 @@ Does not use ledger xact"
|
||||||
xacts))
|
xacts))
|
||||||
(forward-line))
|
(forward-line))
|
||||||
(setq xacts (nreverse xacts)))))
|
(setq xacts (nreverse xacts)))))
|
||||||
|
;; Insert rest-of-name and the postings
|
||||||
(when xacts
|
(when xacts
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(insert rest-of-name)
|
(insert rest-of-name ?\n)
|
||||||
(insert ?\n)
|
|
||||||
(while xacts
|
(while xacts
|
||||||
(insert (car xacts) ?\n)
|
(insert (car xacts) ?\n)
|
||||||
(setq xacts (cdr xacts))))
|
(setq xacts (cdr xacts))))
|
||||||
|
|
|
||||||
|
|
@ -51,12 +51,6 @@
|
||||||
:type 'boolean
|
:type 'boolean
|
||||||
:group 'ledger-post)
|
: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 ()
|
(defun ledger-post-all-accounts ()
|
||||||
"Return a list of all accounts in the buffer."
|
"Return a list of all accounts in the buffer."
|
||||||
(let ((origin (point))
|
(let ((origin (point))
|
||||||
|
|
@ -185,7 +179,7 @@ BEG, END, and LEN control how far it can align."
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
(delete-region (match-beginning 0) (match-end 0))
|
(delete-region (match-beginning 0) (match-end 0))
|
||||||
(calc)
|
(calc)
|
||||||
(if ledger-post-use-decimal-comma
|
(if ledger-use-decimal-comma
|
||||||
(progn
|
(progn
|
||||||
(while (string-match "\\." val)
|
(while (string-match "\\." val)
|
||||||
(setq val (replace-match "" nil nil val))) ;; gets rid of periods
|
(setq val (replace-match "" nil nil val))) ;; gets rid of periods
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ numbers"
|
||||||
"Calculate the cleared balance of the account being reconciled."
|
"Calculate the cleared balance of the account being reconciled."
|
||||||
(interactive)
|
(interactive)
|
||||||
(let* ((pending (car (ledger-string-balance-to-commoditized-amount
|
(let* ((pending (car (ledger-string-balance-to-commoditized-amount
|
||||||
(car (ledger-reconcile-get-balances)))))
|
(car (ledger-reconcile-get-balances)))))
|
||||||
(target-delta (if ledger-target
|
(target-delta (if ledger-target
|
||||||
(-commodity ledger-target pending)
|
(-commodity ledger-target pending)
|
||||||
nil)))
|
nil)))
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
|
|
||||||
;;; Commentary:
|
;;; Commentary:
|
||||||
;;
|
;; Provide facilities for running and saving reports in emacs
|
||||||
|
|
||||||
;;; Code:
|
;;; Code:
|
||||||
|
|
||||||
|
|
@ -51,7 +51,8 @@ specifier."
|
||||||
(defcustom ledger-report-format-specifiers
|
(defcustom ledger-report-format-specifiers
|
||||||
'(("ledger-file" . ledger-report-ledger-file-format-specifier)
|
'(("ledger-file" . ledger-report-ledger-file-format-specifier)
|
||||||
("payee" . ledger-report-payee-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.
|
"An alist mapping ledger report format specifiers to implementing functions.
|
||||||
|
|
||||||
The function is called with no parameters and expected to return the
|
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
|
:type 'alist
|
||||||
:group 'ledger)
|
: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-buffer-name "*Ledger Report*")
|
||||||
|
|
||||||
(defvar ledger-report-name nil)
|
(defvar ledger-report-name nil)
|
||||||
|
|
@ -128,6 +120,12 @@ text that should replace the format specifier."
|
||||||
|
|
||||||
(use-local-map map)))
|
(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 ()
|
(defun ledger-report-read-name ()
|
||||||
"Read the name of a ledger report to use, with completion.
|
"Read the name of a ledger report to use, with completion.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue