Auto-complete Accounts From Other Beancount Files in Emacs

Auto-complete accounts from another accounts.bean file

If you're using Beancount with Emacs, you may be using beancount-mode. It can auto-complete the accounts defined in the current buffer when we are typing in new transactions so that we can do it more efficiently.

But it can only auto-complete the accounts from the current buffer, which makes it less useful when we have a stand-alone file or a few files of beancount accounts.

Based on cnsunyour's fork, here is how to also auto-complete accounts from other files using the advice mechanism of Emacs:

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

(defun w/beancount--collect-accounts-from-files (oldfun regex n)
  (let ((keys (funcall oldfun regex n))
        (hash (make-hash-table :test 'equal)))
    (dolist (key keys)
      (puthash key nil hash))
    ;; collect accounts from files
      (dolist (f beancount-account-files)
        (with-current-buffer (find-file-noselect f)
          (goto-char (point-min))
          (while (re-search-forward beancount-account-regexp nil t)
            (puthash (match-string-no-properties n) nil hash)))))
    (hash-table-keys hash)))

(advice-add #'beancount-collect
            :around #'w/beancount--collect-accounts-from-files
            '((name . "collect accounts from files as well")))

Put the code in your "init.el", and then define your account files by, for example, (setq beancount-account-files '("/path/to/accounts.bean")). Now it will work perfectly when you M-x beancount-insert-account (Revert the current beancount buffer to make the advice effective if necessary).

P.S. If you prefer not to use the ido style to auto-complete the accounts, you can (setq beancount-use-ido nil) so that a completion frontend like ivy will have the chance to take control.

See also

comments powered by Disqus