UP | HOME

+emacs+让beancount插件支持中文账户名

[2025-01-13 周一 23:55]

beancoun使用时,只需要确保首项固定大类和次项以英文/数字开头即可,后续账户可用纯中文。

使用package安装beancount.el,然后在use-package中如下配置(包含了指定本地账户文件):

(use-package beancount
  :demand t
  :mode
  ("\\.bean\\(?:count\\)?\\'" . beancount-mode)
  :bind
  (:map beancount-mode-map
        ("C-c C-d" . beancount-insert-chosen-date)
        ("C-c C-w" . beancount-refile-to-eof))
  ;; ("\C-c TAB" . beancount-insert-account)
  :config
  ;;为账户名匹配添加中文支持
  (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]*\\)+)?")
  ;; 以下是一次性定义好的变量,需要重新载入
  (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)
          ))
  (defvar beancount-account-files nil
    "List of account files")
  (defun 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))))
  ;; 指定账户补全文件路径
  (setq beancount-account-files '("~/.emacs.d/org/bill/accounts.bean"))
  )

账户补全原理及解说参考这个链接

▲ 编辑于 [2025-01-16 周四 16:06] | © Published by Emacs 31.0.50 (Org mode 9.7.19) on [2025-01-16 周四 16:07] | RSS