Skip to content

Commit 78e7394

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 8f06415 commit 78e7394

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
@@ -101,7 +101,7 @@ If nil, full account names are offered for completion."
101101
;; to the list
102102
(sort (delete-dups payees-list) #'string-lessp)))
103103

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

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

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

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

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

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

0 commit comments

Comments
 (0)