Code cleanup in ldg-exec and ledger-split-commodity

This commit is contained in:
Craig Earls 2013-03-25 00:04:43 -04:00
parent 15efb41aba
commit cc62e6a886
2 changed files with 22 additions and 32 deletions

View file

@ -21,7 +21,7 @@
;;; Commentary: ;;; Commentary:
;; Helper functions to deal with commoditized numbers. A commoditized ;; Helper functions to deal with commoditized numbers. A commoditized
;; number will be a cons of value and string where the string contains ;; number will be a list of value and string where the string contains
;; the commodity ;; the commodity
;;; Code: ;;; Code:
@ -32,30 +32,29 @@
:group 'ledger-reconcile) :group 'ledger-reconcile)
(defun ledger-split-commodity-string (str) (defun ledger-split-commodity-string (str)
"Split a commoditized amount into two parts" "Split a commoditized amount into two parts"
(if (> (length str) 0) (if (> (length str) 0)
(let (val comm number-regex) (let ((number-regex (if (assoc "decimal-comma" ledger-environment-alist)
"-?[1-9][0-9.]*[,]?[0-9]*"
"-?[1-9][0-9,]*[.]?[0-9]*")))
(with-temp-buffer (with-temp-buffer
(insert str) (insert str)
(goto-char (point-min)) (goto-char (point-min))
(if (assoc "decimal-comma" ledger-environment-alist)
(setq number-regex "-?[1-9][0-9.]*[,]?[0-9]*")
(setq number-regex "-?[1-9][0-9,]*[.]?[0-9]*"))
(cond ((re-search-forward number-regex nil t) (cond ((re-search-forward number-regex nil t)
;; found a decimal number ;; found a number in the current locale, return it in
(setq val ;; the car. Anything left over is annotation,
(string-to-number ;; the first thing should be the commodity, separated
(ledger-commodity-string-number-decimalize ;; by whitespace, return it in the cdr. I can't think of any
(delete-and-extract-region (match-beginning 0) (match-end 0)) :from-user))) ;; counterexamples
(goto-char (point-min)) (list
(setq comm (nth 0 (split-string (buffer-substring (point-min) (point-max))))) (string-to-number
(list val comm)) (ledger-commodity-string-number-decimalize
(delete-and-extract-region (match-beginning 0) (match-end 0)) :from-user))
(nth 0 (split-string (buffer-substring (point-min) (point-max))))))
((re-search-forward "0" nil t) ((re-search-forward "0" nil t)
;; couldn't find a decimal number, look for a single 0, ;; couldn't find a decimal number, look for a single 0,
;; indicating account with zero balance ;; indicating account with zero balance
(list 0 ledger-reconcile-default-commodity)) (list 0 ledger-reconcile-default-commodity)))))
)))
;; nothing found, return 0 ;; nothing found, return 0
(list 0 ledger-reconcile-default-commodity))) (list 0 ledger-reconcile-default-commodity)))
@ -68,7 +67,6 @@
(ledger-split-commodity-string str)) (ledger-split-commodity-string str))
fields))) fields)))
(defun -commodity (c1 c2) (defun -commodity (c1 c2)
"Subtract C2 from C1, ensuring their commodities match." "Subtract C2 from C1, ensuring their commodities match."
(if (string= (cadr c1) (cadr c2)) (if (string= (cadr c1) (cadr c2))
@ -108,7 +106,7 @@ which must be translated both directions."
(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 ones are after the value."
(let ((val (ledger-commodity-string-number-decimalize (let ((val (ledger-commodity-string-number-decimalize
(number-to-string (car c1)) :to-user)) (number-to-string (car c1)) :to-user))
(commodity (cadr c1))) (commodity (cadr c1)))
@ -122,12 +120,13 @@ Assumes a space between the value and the commodity."
(let ((parts (split-string (read-from-minibuffer (let ((parts (split-string (read-from-minibuffer
(concat prompt " (" ledger-reconcile-default-commodity "): "))))) (concat prompt " (" ledger-reconcile-default-commodity "): ")))))
(if parts (if parts
(if (/= (length parts) 2) ;;assume a number was entered and use default commodity (if (/= (length parts) 2) ;;assume a number was entered and
;;use default commodity
(list (string-to-number (car parts)) (list (string-to-number (car parts))
ledger-reconcile-default-commodity) ledger-reconcile-default-commodity)
(let ((valp1 (string-to-number (car parts))) (let ((valp1 (string-to-number (car parts)))
(valp2 (string-to-number (cadr parts)))) (valp2 (string-to-number (cadr parts))))
(cond ((and (= valp1 valp2) (= 0 valp1));; means neither contained a valid number (both = 0) (cond ((and (= valp1 valp2) (= 0 valp1)) ;; means neither contained a valid number (both = 0)
(list 0 "")) (list 0 ""))
((and (/= 0 valp1) (= valp2 0)) ((and (/= 0 valp1) (= valp2 0))
(list valp1 (cadr parts))) (list valp1 (cadr parts)))

View file

@ -74,15 +74,6 @@
outbuf outbuf
(ledger-exec-handle-error outbuf)))))) (ledger-exec-handle-error outbuf))))))
;; (defun ledger-exec-read (&optional input-buffer &rest args)
;; "Run ledger from option INPUT-BUFFER using ARGS, return a list structure of the ledger Emacs output."
;; (with-current-buffer
;; (apply #'ledger-exec-ledger input-buffer nil "emacs" args)
;; (goto-char (point-min))
;; (prog1
;; (read (current-buffer))
;; (kill-buffer (current-buffer)))))
(defun ledger-version-greater-p (needed) (defun ledger-version-greater-p (needed)
"Verify the ledger binary is usable for `ledger-mode' (version greater than NEEDED)." "Verify the ledger binary is usable for `ledger-mode' (version greater than NEEDED)."
(let ((buffer ledger-buf) (let ((buffer ledger-buf)