;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with ‘C-x C-f’ and enter text in its buffer.
;;2026-01-01 open Assets:SaveAccount
;;2026-01-01 open Assets:CheckAccount
;;2026-01-01 open Assets:Plan
(use-package ivy :ensure t)
(defun my-string-match-all (r data)
(let ((start 0)
matches)
(while (string-match r data start)
(push (match-string 1 data) matches)
(setq start (match-end 0)))
(nreverse matches)))
(defun beancount--list-account (prompt)
(ivy-read prompt
(let ((bill (buffer-substring-no-properties (point-min) (point-max))))
(my-string-match-all "[0-9]*-[0-9]*-[0-9]* open \\(.*\\);?" bill)
)
:action (lambda (s) (insert s) (message "=> %s" s))))
(defun beancount--find-account ()
(interactive)
(beancount--list-account "beancount account:: "))
(defvar beancount--deal-default-unit "CNY" "the default unit CNY for beancount--deal function")
(defun beancount--deal ()
"Beancount, generate a bill which fit the beancount bill"
(interactive)
;; read the bill desc
(let* ((date-now (format-time-string "%Y-%m-%d"))
(date-read-t (format "Bill Date [%s]: " date-now))
(bill-date
(read-string date-read-t nil nil (format-time-string "%Y-%m-%d")))
;; bill date complete
(title (read-string "Bill Title: "))
(detail (read-string "Bill Detail: "))
(bill-desc (format "%s * \"%s\" \"%s\"" bill-date title detail)))
(newline)
;; insert the title complete
(insert bill-desc))
;; loop until done, how to complete it?
(let ((i 1)
(sum-amount 0))
(while (not (= i sum-amount))
(setq i 0)
(newline)
(insert
(beancount--list-account "Deal Account: "))
(insert "\t")
(let* ((amount (read-string (format "amount[rest: %s] (unit: CNY) " sum-amount)))
(amount-text (format "%s %s" amount beancount--deal-default-unit)))
(setq sum-amount (+ sum-amount (string-to-number amount)))
(insert amount-text)))))