-
Notifications
You must be signed in to change notification settings - Fork 75
/
ledger-complete.el
396 lines (365 loc) · 16.5 KB
/
ledger-complete.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
;;; ledger-complete.el --- Helper code for use with the "ledger" command-line tool -*- lexical-binding: t; -*-
;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)
;; This file is not part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301 USA.
;;; Commentary:
;; Functions providing payee and account auto complete.
(require 'cl-lib)
(eval-when-compile
(require 'subr-x))
;; In-place completion support
;;; Code:
(require 'ledger-context)
(require 'ledger-xact)
(require 'ledger-schedule)
(defcustom ledger-accounts-file nil
"The path to an optional file in which all accounts are used or declared.
This file will then be used as a source for account name
completions instead of the current file.
See ledger's \"account\" directive."
:type '(choice (const :tag "Use current buffer for completion" nil)
file)
:group 'ledger
:safe #'string-or-null-p)
(defcustom ledger-payees-file nil
"The path to an optional file in which all payees are used or declared.
This file will then be used as a source for payee name
completions instead of the current file.
See ledger's \"payee\" directive."
:type '(choice (const :tag "Use current buffer for completion" nil)
file)
:group 'ledger
:safe #'string-or-null-p)
(defcustom ledger-accounts-exclude-function nil
"Function to exclude accounts from completion.
Should be a predicate function that accepts one argument, an
element of `ledger-accounts-list-in-buffer'."
:type '(choice (const :tag "Do not exclude any accounts from completion" nil)
function)
:group 'ledger
:package-version '(ledger-mode . "2019-08-14"))
(defcustom ledger-complete-in-steps nil
"When non-nil, `ledger-complete-at-point' completes account names in steps.
If nil, full account names are offered for completion."
:type 'boolean
:group 'ledger
:package-version '(ledger-mode . "4.0.0"))
(defun ledger-payees-in-buffer ()
"Scan buffer and return list of all payees."
(let ((origin (point))
payees-list)
(save-excursion
(goto-char (point-min))
(while (re-search-forward ledger-payee-name-or-directive-regex nil t)
(unless (and (>= origin (match-beginning 0))
(< origin (match-end 0)))
(push (or (match-string-no-properties 1) (match-string-no-properties 2))
payees-list))))
;; to the list
(sort (delete-dups payees-list) #'string-lessp)))
(defun ledger-payees-list ()
"Return a list of all known account names as strings.
Looks in `ledger-payees-file' if set, otherwise the current buffer."
(if ledger-payees-file
(let ((f ledger-payees-file))
(with-temp-buffer
(insert-file-contents f)
(ledger-payees-in-buffer)))
(ledger-payees-in-buffer)))
(defun ledger-accounts-in-buffer ()
"Return an alist of accounts in the current buffer.
The `car' of each element is the account name and the `cdr' is an
alist where the key is a subdirective such as \"assert\" and the
value (if any) is the associated data. In other words, if you've
declared an account like so:
account Assets:Checking
assert commodity == \"$\"
default
Then one of the elements this function returns will be
\(\"Assets:Checking\"
(\"default\")
(\"assert\" . \"commodity == \"$\"\"))"
(save-excursion
(goto-char (point-min))
(let (account-list
(seen (make-hash-table :test #'equal :size 1)))
;; First, consider accounts declared with "account" directives, which may or
;; may not have associated data. The data is on the following lines up to a
;; line not starting with whitespace.
(while (re-search-forward ledger-account-directive-regex nil t)
(let ((account (match-string-no-properties 1))
(lines (buffer-substring-no-properties
(point)
(progn (ledger-navigate-next-xact-or-directive)
(point))))
data)
(dolist (d (split-string lines "\n"))
(setq d (string-trim d))
(unless (string= d "")
(if (string-match " " d)
(push (cons (substring d 0 (match-beginning 0))
(substring d (match-end 0) nil))
data)
(push (cons d nil) data))))
(push (cons account data) account-list)
(puthash account t seen)))
;; Next, gather all accounts declared in postings
(unless
;; FIXME: People who have set `ledger-flymake-be-pedantic' to non-nil
;; probably don't want accounts from postings, just those declared
;; with directives. But the name is a little misleading. Should we
;; make a ledger-mode-be-pedantic and use that instead?
(bound-and-true-p ledger-flymake-be-pedantic)
(ledger-xact-iterate-transactions
(lambda (_pos _date _state _payee)
(let ((end (save-excursion (ledger-navigate-end-of-xact))))
(forward-line)
(while (re-search-forward ledger-account-any-status-regex end t)
(let ((account (match-string-no-properties 1)))
(unless (gethash account seen)
(puthash account t seen)
(push (cons account nil) account-list))))))))
(sort account-list (lambda (a b) (string-lessp (car a) (car b)))))))
(defun ledger-accounts-list-in-buffer ()
"Return a list of all known account names in the current buffer as strings.
Considers both accounts listed in postings and those declared
with \"account\" directives."
(let ((accounts (ledger-accounts-in-buffer)))
(when ledger-accounts-exclude-function
(setq accounts (cl-remove-if ledger-accounts-exclude-function accounts)))
(mapcar #'car accounts)))
(defun ledger-accounts-list ()
"Return a list of all known account names as strings.
Looks in `ledger-accounts-file' if set, otherwise the current buffer."
(if ledger-accounts-file
(let ((f ledger-accounts-file))
(with-temp-buffer
(insert-file-contents f)
(ledger-accounts-list-in-buffer)))
(ledger-accounts-list-in-buffer)))
(defun ledger-accounts-tree ()
"Return a tree of all accounts in the buffer.
Each node in the tree is a list (t . CHILDREN), where CHILDREN is
an alist (ACCOUNT-ELEMENT . NODE)."
(let ((account-tree (list t)))
(dolist (account (ledger-accounts-list) account-tree)
(let ((root account-tree)
(account-elements (split-string account ":")))
(dolist (element account-elements)
(let ((node (assoc element root)))
(unless node
(setq node (cons element (list t)))
(nconc root (list node)))
(setq root (cdr node))))))))
(defun ledger-complete-account-next-steps ()
"Return a list of next steps for the account prefix at point."
;; FIXME: This function is called from `ledger-complete-at-point' which
;; already knows the bounds of the account name to complete. Computing it
;; again here is wasteful.
(let* ((current (buffer-substring
(save-excursion
(unless (eq 'posting (ledger-thing-at-point))
(error "Not on a posting line"))
(point))
(point)))
(elements (and current (split-string current ":")))
(root (ledger-accounts-tree))
(prefix nil))
(while (cdr elements)
(let ((xact (assoc (car elements) root)))
(if xact
(setq prefix (concat prefix (and prefix ":")
(car elements))
root (cdr xact))
(setq root nil elements nil)))
(setq elements (cdr elements)))
(setq root (delete (list (car elements) t) root))
(and root
(sort
(mapcar (function
(lambda (x)
(let ((term (if prefix
(concat prefix ":" (car x))
(car x))))
(if (> (length (cdr x)) 1)
(concat term ":")
term))))
(cdr root))
'string-lessp))))
(defvar ledger-complete--current-time-for-testing nil
"Internal, used for testing only.")
(defun ledger-complete-date (month-string day-string date-at-eol-p)
"Complete a date."
(let* ((now (or ledger-complete--current-time-for-testing (current-time)))
(decoded (decode-time now))
(this-month (nth 4 decoded))
(this-year (nth 5 decoded))
(last-month (if (> this-month 1) (1- this-month) 12))
(last-year (1- this-year))
(last-month-year (if (> this-month 1) this-year last-year))
(month (and month-string
(string-to-number month-string)))
(day (string-to-number day-string))
(dates (list (encode-time 0 0 0 day (or month this-month) this-year)
(if month
(encode-time 0 0 0 day month last-year)
(encode-time 0 0 0 day last-month last-month-year)))))
(let ((collection
(list (concat (ledger-format-date
(cl-find-if (lambda (date) (not (time-less-p now date))) dates))
(when date-at-eol-p " ")))))
(lambda (string predicate action)
(if (eq action 'metadata)
'(metadata (category . ledger-date))
(complete-with-action action collection string predicate))))))
(defun ledger-complete-effective-date
(tx-year-string tx-month-string tx-day-string
month-string day-string
date-at-eol-p)
"Complete an effective date."
(let* ((tx-year (string-to-number tx-year-string))
(tx-month (string-to-number tx-month-string))
(tx-day (string-to-number tx-day-string))
(tx-date (encode-time 0 0 0 tx-day tx-month tx-year))
(next-month (if (< tx-month 12) (1+ tx-month) 1))
(next-year (1+ tx-year))
(next-month-year (if (< tx-month 12) tx-year next-year))
(month (and month-string
(string-to-number month-string)))
(day (string-to-number day-string))
(dates (list (encode-time 0 0 0 day (or month tx-month) tx-year)
(if month
(encode-time 0 0 0 day month next-year)
(encode-time 0 0 0 day next-month next-month-year)))))
(let ((collection
(list (concat (ledger-format-date
(cl-find-if (lambda (date) (not (time-less-p date tx-date))) dates))
(when date-at-eol-p " ")))))
(lambda (string predicate action)
(if (eq action 'metadata)
'(metadata (category . ledger-date))
(complete-with-action action collection string predicate))))))
(defun ledger-complete-at-point ()
"Do appropriate completion for the thing at point."
(let ((end (point))
start collection
realign-after
delete-suffix)
(cond (;; Date
(save-excursion
(skip-chars-forward "0-9/-")
(looking-back (concat "^" ledger-incomplete-date-regexp) (line-beginning-position)))
(setq collection (ledger-complete-date (match-string 1)
(match-string 2)
(= (line-end-position) (match-end 0)))
start (match-beginning 0)
delete-suffix (save-match-data
(when (looking-at (rx (one-or-more (or digit (any ?/ ?-)))))
(length (match-string 0))))))
(;; Effective dates
(save-excursion
(skip-chars-forward "0-9/-")
(looking-back (concat "^" ledger-iso-date-regexp "=" ledger-incomplete-date-regexp)
(line-beginning-position)))
(setq start (line-beginning-position))
(setq collection (ledger-complete-effective-date
(match-string 2) (match-string 3) (match-string 4)
(match-string 5) (match-string 6)
(= (line-end-position) (match-end 0)))))
(;; Payees
(eq 'transaction
(save-excursion
(prog1 (ledger-thing-at-point)
(setq start (point)))))
(setq collection (cons 'nullary #'ledger-payees-list)))
(;; Accounts
(save-excursion
(back-to-indentation)
(skip-chars-forward "([") ;; for virtual accounts
(setq start (point)))
(setq delete-suffix (save-excursion
(when (search-forward-regexp (rx (or eol (or ?\t (repeat 2 space)))) (line-end-position) t)
(- (match-beginning 0) end)))
realign-after t
collection (cons 'nullary
(if ledger-complete-in-steps
#'ledger-complete-account-next-steps
#'ledger-accounts-list)))))
(when collection
(let ((prefix (buffer-substring-no-properties start end)))
(list start end
(pcase collection
;; `func-arity' isn't available until Emacs 26, so we have to
;; manually track the arity of the functions.
(`(nullary . ,f)
;; a nullary function that returns a completion collection
(completion-table-with-cache
(lambda (_)
(cl-remove-if (apply-partially 'string= prefix) (funcall f)))))
((pred functionp)
;; a completion table
collection)
(_
;; a static completion collection
collection))
:exit-function (lambda (&rest _)
(when delete-suffix
(delete-char delete-suffix))
(when (and realign-after ledger-post-auto-align)
(ledger-post-align-postings (line-beginning-position) (line-end-position)))))))))
(defun ledger-trim-trailing-whitespace (str)
(replace-regexp-in-string "[ \t]*$" "" str))
(defun ledger-fully-complete-xact ()
"Completes a transaction if there is another matching payee in the buffer.
Interactively, if point is after a payee, complete the
transaction with the details from the last transaction to that
payee."
(interactive)
(let* ((name (ledger-trim-trailing-whitespace
(buffer-substring
(save-excursion
(unless (eq (ledger-thing-at-point) 'transaction)
(user-error "Cannot fully complete xact here"))
(point))
(point))))
(rest-of-name name)
xacts)
(save-excursion
(when (eq 'transaction (ledger-thing-at-point))
(delete-region (point) (+ (length name) (point)))
;; Search backward for a matching payee
(when (re-search-backward
(concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+\\(.*"
(regexp-quote name) ".*\\)")
nil t)
(setq rest-of-name (match-string 3))
;; Start copying the postings
(forward-line)
(setq xacts (buffer-substring-no-properties (point) (ledger-navigate-end-of-xact))))))
;; Insert rest-of-name and the postings
(save-excursion
(insert rest-of-name ?\n)
(insert xacts)
(unless (looking-at-p "\n\n")
(insert "\n")))
(forward-line)
(end-of-line)
;; Move to amount on first posting line
(when (re-search-backward "\t\\| [ \t]" nil t)
(goto-char (match-end 0)))))
(add-to-list 'completion-category-defaults '(ledger-date (styles . (substring))))
(provide 'ledger-complete)
;;; ledger-complete.el ends here