I usually dedicate an "accounts.bean" to declare all my accounts, and do my
daily financing in another file. Currently `beancount-insert-account`(C-c
') only read candidates from current buffer, so it's inconvenient. This
patch adds an option to allow user to specify a bunch of files to read
completion candidates.
Newly added `files' argument is optional, so won't affect current users.
Similar things could be done for links / tags by adding related options,
but I left it to your judgment whether it's necessary.
--
You received this message because you are subscribed to the Google Groups
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/beancount/1fe63852-7193-4a03-89f2-7b8d126af735%40googlegroups.com.
diff -r 85a205b6b22e -r e1b51acc248c editors/emacs/beancount.el
--- a/editors/emacs/beancount.el Wed Oct 30 09:30:30 2019 -0400
+++ b/editors/emacs/beancount.el Sat Nov 09 12:59:44 2019 +0800
@@ -404,6 +404,9 @@
"A list of the accounts available in this buffer.")
(make-variable-buffer-local 'beancount-accounts)
+(defvar beancount-accounts-files nil
+ "A list of files to provide candidates for accounts completion.")
+
(defun beancount-completion-at-point ()
"Return the completion data relevant for the text at point."
(save-excursion
@@ -479,21 +482,28 @@
(complete-with-action action candidates string pred))))
(list (match-beginning 1) (match-end 1) completion-table))))))))
-(defun beancount-collect (regexp n)
- "Return an unique list of REGEXP group N in the current buffer."
+(defun beancount-collect (regexp n &optional files)
+ "Return a unique list of REGEXP group N in the current buffer. Optionally,
+also look at data in selected files."
(save-excursion
(save-match-data
(let ((hash (make-hash-table :test 'equal)))
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(puthash (match-string-no-properties n) nil hash))
+ ;; If `files' are provided, also look into them.
+ (when files
+ (dolist (f files)
+ (with-current-buffer (find-file-noselect f)
+ (while (re-search-forward regexp nil t)
+ (puthash (match-string-no-properties n) nil hash)))))
(hash-table-keys hash)))))
(defun beancount-account-completion-table (string pred action)
(if (eq action 'metadata) '(metadata (category . beancount-account))
(if (null beancount-accounts)
(setq beancount-accounts
- (sort (delete string (beancount-collect beancount-account-regexp 0)) #'string<)))
+ (sort (delete string (beancount-collect beancount-account-regexp 0 beancount-accounts-files)) #'string<)))
(complete-with-action action beancount-accounts string pred)))
;; Default to substring completion for beancount accounts.
@@ -604,7 +614,7 @@
;; completion tables thus directly build a list of the
;; accounts in the buffer
(let ((beancount-accounts
- (sort (beancount-collect beancount-account-regexp 0) #'string<)))
+ (sort (beancount-collect beancount-account-regexp 0 beancount-accounts-files) #'string<)))
(ido-completing-read "Account: " beancount-accounts
nil nil (thing-at-point 'word)))
(completing-read "Account: " #'beancount-account-completion-table