Skip to content

Commit f626098

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 d2fbf3a commit f626098

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
(goto-char (point-min))
151149
(while (re-search-forward ledger-account-name-or-directive-regex nil t)
152150
(let ((account (match-string-no-properties 1)))
@@ -155,16 +153,18 @@ Then one of the elements this function returns will be
155153
(push (cons account nil) account-list)))))
156154
(sort account-list (lambda (a b) (string-lessp (car a) (car b)))))))
157155

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

167-
(defun ledger-accounts-list ()
167+
(defun ledger-accounts-list (&optional include-postings)
168168
"Return a list of all known account names as strings.
169169
Looks in `ledger-accounts-file' if set, otherwise the current buffer."
170170
(if ledger-accounts-file
@@ -174,7 +174,7 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
174174
(ledger-accounts-list-in-buffer)))
175175
(ledger-accounts-list-in-buffer)))
176176

177-
(defun ledger-find-accounts-in-buffer ()
177+
(defun ledger-find-accounts-in-buffer (&optional include-postings)
178178
(let ((account-tree (list t))
179179
(account-elements nil)
180180
(prefix ""))
@@ -183,7 +183,7 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
183183

184184
(dolist (account
185185
(cl-remove-if-not (lambda (c) (string-prefix-p prefix c))
186-
(ledger-accounts-list)))
186+
(ledger-accounts-list include-postings)))
187187
(let ((root account-tree))
188188
(setq account-elements
189189
(split-string
@@ -198,11 +198,11 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer."
198198
(setq account-elements (cdr account-elements))))))
199199
account-tree))
200200

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

0 commit comments

Comments
 (0)