请问beancount如何补全多个账户文件?

(defvar beancount-account-files nil
  "List of account files")

(defun adv/beancount-collect-pos-alist (regexp n)
  "Return a list of conses mapping matches of REGEXP group N in the
  current buffer to a position of the match beginning."
  (let ((result))
    ;; collect accounts from files
    (save-excursion
      (dolist (f beancount-account-files)
        (with-current-buffer (find-file-noselect f)
          (goto-char (point-min))
          (while (re-search-forward regexp nil t)
            (push (cons (match-string-no-properties n) (match-beginning 0))
                  result))))
      (nreverse result))))

(advice-add #'beancount-collect-pos-alist :override #'adv/beancount-collect-pos-alist)

;; 指定账户补全文件路径
(setq beancount-account-files '("~/.emacs.d/org/bill/accounts.bean"))

另外还可以通过以下代码添加中文子账户支持

;;为账户名匹配添加中文支持
(setq beancount-account-chars "[:alnum:]-_\\cC:")
;; 同上
(setq beancount-account-regexp
      (concat (regexp-opt beancount-account-categories)
              "\\(?::[[:alnum:]-_\\cC]+\\)+"))
;; 现在可以匹配加减乘和括号,但是不支持千分位逗号
(setq beancount-number-regexp "[-]?(?\\(?:[-+*]?[0-9]+\\.?[0-9]*\\)+)?")
;; 以下是在'beancoun.el'中一次性定义好的变量,需要重新载入
(setq beancount-posting-regexp
      (concat "^\\s-+"
              "\\(" beancount-account-regexp "\\)"
              "\\(?:\\s-+\\(\\(" beancount-number-regexp "\\)"
              "\\s-+\\(" beancount-currency-regexp "\\)\\)\\)?"))
(setq beancount-balance-regexp
      (concat "^" beancount-date-regexp "\\s-+balance\\s-+"
              "\\(" beancount-account-regexp "\\)\\s-+"
              "\\(\\(" beancount-number-regexp "\\)\\s-+\\(" beancount-currency-regexp "\\)\\)"))
(setq beancount-font-lock-keywords
      `((,beancount-transaction-regexp (1 'beancount-date)
                                       (2 (beancount-face-by-state (match-string 2)) t)
                                       (3 (beancount-face-by-state (match-string 2)) t))
        (,beancount-posting-regexp (1 'beancount-account)
                                   (2 'beancount-amount nil :lax))
        (,beancount-metadata-regexp (1 'beancount-metadata)
                                    (2 'beancount-metadata t))
        (,beancount-directive-regexp (1 'beancount-directive))
        (,beancount-timestamped-directive-regexp (1 'beancount-date)
                                                 (2 'beancount-directive))
        ;; Fontify section headers when composed with outline-minor-mode.
        (,(concat "^\\(" beancount-outline-regexp "\\).*") (0 (beancount-outline-face)))
        ;; Tags and links.
        (,(concat "\\#[" beancount-tag-chars "]*") . 'beancount-tag)
        (,(concat "\\^[" beancount-tag-chars "]*") . 'beancount-link)
        ;; Accounts not covered by previous rules.
        (,beancount-account-regexp . 'beancount-account)
        ;; Number followed by currency not covered by previous rules.
        (,(concat beancount-number-regexp "\\s-+" beancount-currency-regexp) . 'beancount-amount)
        ))
1 个赞