Skip to content

Commit 7b9a668

Browse files
committed
ledger-accounts-in-buffer: decide explicitly whether to include postings
The fact that ledger-accounts-in-buffer, even when called from Lisp, depends on a user customization related to flymake, is kind of an ugly hack. Here, we move it to the completion function instead, which at least has a better excuse for its behavior to depend on customizations.
1 parent 6f61d24 commit 7b9a668

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

ledger-complete.el

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ If nil, full account names are offered for completion."
100100
;; to the list
101101
(sort (delete-dups payees-list) #'string-lessp)))
102102

103-
(defun ledger-accounts-in-buffer ()
103+
(defun ledger-accounts-in-buffer (&optional include-postings)
104104
"Return an alist of accounts in the current buffer.
105105
The `car' of each element is the account name and the `cdr' is an
106106
alist where the key is a subdirective such as \"assert\" and the
@@ -114,7 +114,11 @@ account Assets:Checking
114114
Then one of the elements this function returns will be
115115
\(\"Assets:Checking\"
116116
(\"default\")
117-
(\"assert\" . \"commodity == \"$\"\"))"
117+
(\"assert\" . \"commodity == \"$\"\"))
118+
119+
If INCLUDE-POSTINGS is non-nil, this alist will also include
120+
accounts seen in postings that aren't explicitly declared with
121+
\"account\" directives."
118122
(save-excursion
119123
(goto-char (point-min))
120124
(let (account-list
@@ -139,14 +143,8 @@ Then one of the elements this function returns will be
139143
(push (cons d nil) data))))
140144
(push (cons account data) account-list)
141145
(puthash account t seen)))
142-
;; Next, gather all accounts declared in postings
143-
(unless
144-
;; FIXME: People who have set `ledger-flymake-be-pedantic' to non-nil
145-
;; probably don't want accounts from postings, just those declared
146-
;; with directives. But the name is a little misleading. Should we
147-
;; make a ledger-mode-be-pedantic and use that instead?
148-
(or (bound-and-true-p ledger-flymake-be-pedantic)
149-
(bound-and-true-p flycheck-ledger-pedantic))
146+
;; Next, gather all accounts declared in postings if requested
147+
(when include-postings
150148
(ledger-xact-iterate-transactions
151149
(lambda (_pos _date _state _payee)
152150
(let ((end (save-excursion (ledger-navigate-end-of-xact))))
@@ -158,16 +156,18 @@ Then one of the elements this function returns will be
158156
(push (cons account nil) account-list))))))))
159157
(sort account-list (lambda (a b) (string-lessp (car a) (car b)))))))
160158

161-
(defun ledger-accounts-list-in-buffer ()
159+
(defun ledger-accounts-list-in-buffer (&optional include-postings)
162160
"Return a list of all known account names in the current buffer as strings.
163-
Considers both accounts listed in postings and those declared
164-
with \"account\" directives."
165-
(let ((accounts (ledger-accounts-in-buffer)))
161+
162+
If INCLUDE-POSTINGS is non-nil, considers both accounts listed in
163+
postings and those declared with \"account\" directives.
164+
Otherwise, only considers the latter."
165+
(let ((accounts (ledger-accounts-in-buffer include-postings)))
166166
(when ledger-accounts-exclude-function
167167
(setq accounts (cl-remove-if ledger-accounts-exclude-function accounts)))
168168
(mapcar #'car accounts)))
169169

170-
(defun ledger-accounts-list ()
170+
(defun ledger-accounts-list (&optional include-postings)
171171
"Return a list of all known account names as strings.
172172
Looks in `ledger-accounts-file' if set, otherwise the current buffer."
173173
(if ledger-accounts-file
@@ -177,7 +177,7 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
177177
(ledger-accounts-list-in-buffer)))
178178
(ledger-accounts-list-in-buffer)))
179179

180-
(defun ledger-find-accounts-in-buffer ()
180+
(defun ledger-find-accounts-in-buffer (&optional include-postings)
181181
(let ((account-tree (list t))
182182
(account-elements nil)
183183
(prefix ""))
@@ -186,7 +186,7 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
186186

187187
(dolist (account
188188
(cl-remove-if-not (lambda (c) (string-prefix-p prefix c))
189-
(ledger-accounts-list)))
189+
(ledger-accounts-list include-postings)))
190190
(let ((root account-tree))
191191
(setq account-elements
192192
(split-string
@@ -201,11 +201,11 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
201201
(setq account-elements (cdr account-elements))))))
202202
account-tree))
203203

204-
(defun ledger-accounts-tree ()
204+
(defun ledger-accounts-tree (&optional include-postings)
205205
"Return a tree of all accounts in the buffer."
206206
(let* ((current (caar (ledger-parse-arguments)))
207207
(elements (and current (split-string current ":")))
208-
(root (ledger-find-accounts-in-buffer))
208+
(root (ledger-find-accounts-in-buffer include-postings))
209209
(prefix nil))
210210
(while (cdr elements)
211211
(let ((xact (assoc (car elements) root)))
@@ -308,9 +308,18 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
308308
(when (search-forward-regexp (rx (or eol (or ?\t (repeat 2 space)))) (line-end-position) t)
309309
(- (match-beginning 0) end)))
310310
realign-after t
311-
collection (if ledger-complete-in-steps
312-
#'ledger-accounts-tree
313-
#'ledger-accounts-list))))
311+
collection
312+
;; FIXME: People who have set `ledger-flymake-be-pedantic' or
313+
;; `flycheck-ledger-pedantic' to non-nil probably don't want
314+
;; accounts from postings, just those declared with directives.
315+
;; But the name is a little misleading. Should we make a
316+
;; ledger-mode-be-pedantic and use that instead?
317+
(let ((include-postings
318+
(not (or (bound-and-true-p ledger-flymake-be-pedantic)
319+
(bound-and-true-p flycheck-ledger-pedantic)))))
320+
(if ledger-complete-in-steps
321+
(lambda () (ledger-accounts-tree include-postings))
322+
(lambda () (ledger-accounts-list include-postings)))))))
314323
(when collection
315324
(let ((prefix (buffer-substring-no-properties start end)))
316325
(list start end

0 commit comments

Comments
 (0)