-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathledger-config.el
281 lines (260 loc) · 12 KB
/
ledger-config.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
;;; ledger-config.el --- extend ledger-mode -*- lexical-binding: t -*-
;; Code:
(require 'ledger-mode)
(require 'helm-lib)
(require 'helm-mode)
(define-key ledger-mode-map (kbd "C-c a l") 'ledger-align-device)
(defvar ledger-default-device "€")
;; «Redefine-ledger-reconcile-visit» (to ".Redefine-ledger-reconcile-visit")
(defun ledger-reconcile-visit ()
(interactive)
(let ((where (get-text-property (point) 'where)))
(when (equal (car where) "/dev/stdin")
(switch-to-buffer-other-window ledger-buf)
(goto-char (cdr where)))))
;; «Align-euro-device» (to ".Align-euro-device")
;;;###autoload
(defun ledger-align-device (&optional column)
(interactive "p")
(when (= column 1) (setq column 48))
(save-excursion
(goto-char (point-min))
(while (search-forward ledger-default-device nil t)
(backward-char)
(let ((col (current-column))
(beg (point))
target-col len)
(skip-chars-forward (concat "-" ledger-default-device "0-9,."))
(setq len (- (point) beg))
(setq target-col (- column len))
(if (< col target-col)
(progn
(goto-char beg)
(insert (make-string (- target-col col) ? )))
(move-to-column target-col)
(if (looking-back " " (1- (point)))
(delete-char (- col target-col))
(skip-chars-forward "^ \t")
(delete-horizontal-space)
(insert " ")))
(forward-line)))))
;; «ledger-position-at-point» (to ".ledger-position-at-point")
;;;###autoload
(defun ledger-position (arg)
"Show ledger balance, with prefix-arg insert it at point."
(interactive "P")
(let* ((bal (with-temp-buffer
(apply #'call-process "ledger" nil t nil
(list "-C" "bal" "socgen"))
(split-string (buffer-string) "\n" t)))
(result (car (last bal))))
(when (string-match "€ [0-9.]*" result)
(setq result (match-string 0 result))
(if arg
(insert (format "[%s]" result))
(message "ledger balance: %s" result)))))
(defun tv:advice-ledger-reconcile-refresh (&rest _args)
"Align euros in reconcile buffer after refreshing with `C-l'."
(save-excursion
(let ((inhibit-read-only t))
(align-regexp (point-min) (point-max) "\\(\\s-*\\)€" 1 1 nil))))
(advice-add 'ledger-reconcile-refresh :after #'tv:advice-ledger-reconcile-refresh)
(defun ledger-reverse-date-from-regexp (regexp)
(let ((ledger-file (getenv "LEDGER_FILE")))
(cl-assert ledger-file nil "No ledger file found, check your ENV var LEDGER_FILE")
(with-current-buffer (find-file-noselect ledger-file)
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(let* ((dt (match-string-no-properties 0))
(split (reverse (split-string dt "/")))
(new-dt (mapconcat 'identity split "/")))
(delete-region (point-at-bol) (point))
(insert new-dt))))))
;;;###autoload
(defun ledger-reverse-date-to-us ()
(interactive)
(ledger-reverse-date-from-regexp
"^[0-9]\\{2\\}/[0-9]\\{2\\}/[0-9]\\{4\\}"))
;;;###autoload
(defun ledger-reverse-date-to-fr ()
(interactive)
(ledger-reverse-date-from-regexp
"^[0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\}"))
;;;###autoload
(defun ledger-add-expense (date payee categorie type amount)
(interactive
(list (read-string "Date: " (format-time-string "%Y/%m/%d"))
(read-string "Payee: ")
(helm-comp-read "Categorie: " (ledger-collect-categories))
(helm-comp-read "Type: " '("Visa" "Check" "Tip" "Prelevement"))
(read-string "Amount: ")))
(let ((ledger-file (getenv "LEDGER_FILE"))
numcheck defnumcheck)
(with-current-buffer (find-file-noselect ledger-file)
(goto-char (point-max))
(when (string= type "Check")
(setq defnumcheck (save-excursion
(when
(re-search-backward
"\\(^[0-9]\\{4\\}/[0-9/]*\\)\\(.*\\)\\(\([0-9]*\)\\)" nil t)
(replace-regexp-in-string "\(\\|\)" "" (match-string 3)))))
(setq defnumcheck (int-to-string (1+ (string-to-number defnumcheck))))
(setq numcheck (read-string "CheckNumber: " defnumcheck)))
(insert (concat
date " " payee (or (and numcheck (concat " (" numcheck ")")) "") "\n "
"Expenses:" categorie (make-string 8 ? ) "€ " amount "\n "
"Liabilities:Socgen:" type "\n\n"))
(goto-char (point-min))
(ledger-align-device 1)
(save-buffer)
(pop-to-buffer ledger-file))))
;;;###autoload
(defun ledger-add-income (date payee categorie account amount)
(interactive
(list (read-string "Date: " (format-time-string "%Y/%m/%d"))
(read-string "Payee: ")
(helm-comp-read "Categorie: " (ledger-collect-categories))
(helm-comp-read "Account: " '("socgen:checking" "socgen:prelevement")) ;; TODO add completion here
(read-string "Amount: ")))
(let ((ledger-file (getenv "LEDGER_FILE")))
(with-current-buffer (find-file-noselect ledger-file)
(goto-char (point-max))
(insert (concat
date " " payee "\n "
(if (string= account "socgen:checking") "Assets:" "Liabilities:")
account (make-string 8 ? ) "€ "
(if (string= account "socgen:checking")
amount (int-to-string (- (string-to-number amount))))
"\n Income:" categorie "\n\n"))
(goto-char (point-min))
(ledger-align-device 1)
(save-buffer)
(pop-to-buffer ledger-file))))
(defun ledger-collect-categories ()
(let ((categories '("Alimentation" "Impots"
"Auto:gasoil" "Auto:garage"
"Voyages" "Escalade"
"Livres" "Informatique"
"Loisirs" "Divers"
"Loyers:immovar" "Loyers:big"))
result)
(with-current-buffer (find-file-noselect (getenv "LEDGER_FILE"))
(goto-char (point-min))
(save-excursion
(while
(re-search-forward
"\\(^ *Expenses\\|Income\\):\\([^ €0-9\n]*\\)" (point-max) t)
(setq result (match-string 2))
(unless (or (member result categories)
(string= result ""))
(push result categories))))
categories)))
;;;###autoload
(defun ledger-point-entries-in-buffer ()
"Point entries from point to end of buffer.
Like C-c C-e but on all entries.
If entries are already pointed, skip."
(interactive)
(while (re-search-forward "^[0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\}" nil t)
(forward-char 1) (unless (looking-at "[*]") (insert "* "))))
(defvar csv2ledger-default-input-dir "~/Téléchargements/")
(defvar csv2ledger-default-output-dir "~/finance")
;;;###autoload
(defun csv2ledger (account infile ofile)
(interactive (list (completing-read "Account: " '("Socgen" "Paypal" "livretA"))
(read-file-name "Input cvs file: "
csv2ledger-default-input-dir
nil nil nil (lambda (f)
(or (file-directory-p f)
(string= (file-name-extension f) "csv"))))
(read-file-name "Output file (.dat): "
csv2ledger-default-output-dir
nil nil nil (lambda (f)
(or (file-directory-p f)
(string= (file-name-extension f) "dat"))))))
(let ((ibuf (find-file-noselect infile))
(obuf (find-file-noselect ofile))
curpos beg ov)
(with-current-buffer obuf
(setq curpos (point))
(goto-char (point-max))
(setq beg (point))
(text-mode))
(with-current-buffer ibuf
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^[0-9]+/" nil t)
(let* ((split (split-string (buffer-substring (point-at-bol) (point-at-eol)) ";" t))
(date (car split))
(payee (nth 2 split))
(amountstr (replace-regexp-in-string "," "." (nth 3 split)))
(amountnum (string-to-number amountstr))
(deb (< amountnum 0))
(cred (> amountnum 0)))
(setq amountstr (replace-regexp-in-string "-" "" amountstr))
(with-current-buffer obuf
(save-excursion
(insert
(concat date " * " payee "\n "
(if deb
(format "Expenses:unknown € %s\n Liabilities:socgen\n\n" amountstr)
(format "Assets:%s:checking € %s\n Income\n\n" account amountstr))))))))))
(with-current-buffer obuf
(ledger-mode)
(remove-overlays)
(setq ov (make-overlay beg (point-max)))
(overlay-put ov 'face '((:background "DarkSlateGray" :extend t))))
(switch-to-buffer obuf)
(ledger-reverse-date-to-us)))
;;;###autoload
(defun ledger-exchange-point-an-mark-or-overlay ()
(interactive)
(if (region-active-p)
(exchange-point-and-mark)
(helm-aif (overlays-at (point))
(cond ((eq (overlay-start (car it)) (point))
(goto-char (next-overlay-change (point)))
(goto-char (next-single-char-property-change (point) 'face))
(forward-line -1))
(t
(goto-char (previous-overlay-change (point)))))
(and (delq nil (overlay-lists))
(goto-char (next-overlay-change (point)))))))
(define-key ledger-mode-map (kbd "C-x C-x") 'ledger-exchange-point-an-mark-or-overlay)
(defvar ledger/associations '(("PRELEVEMENT A LA SOURCE REVENUS" . ":impot:prelevement_source")
("SOLDE IMPOT REVENUS" . ":impot:impot_revenu")
("MOTIF: CARAC" . ":carac")
("MOTIF: DECLIC" . ":codevi")
("IMMOB PATRIMOINE ET FINANCES" . ":loyers:immovar")
("VIR GIE AFER" . ":afer:racp")
("PIERVAL SANTE" . ":scpi:pierval")
("LFREM DISTRI SCPI" . ":scpi:lfrem")
("AMAZON PAYMENTS COMMERCE ELECTRONIQUE" . ":shopping:amazon")
("LES QUATRE VALLEES" . ":vétérinaire")
("CERUTI PONS" . ":vétérinaire")
("VETO GUILLESTRE" . ":vétérinaire")
("SAMSE" . ":brico:mont-dauphin")
("WELDOM EMBRUN" . ":brico:mont-dauphin")
("GEMAP" . ":brico:mont-dauphin")
("LOGITEL POUR: Rachel Bourdin" . ":rachel:virement")
("VIR PERM POUR: Rachel Bourdin" . ":rachel:virement:mensuel")
("Solimut" . ":assurance:maladie:mutuelle")
("CARTE X3077" . ":rachel:carte3077")
("CARTE X8911" . ":thierry:carte8911")
("CARTE X1965" . ":thierry:carte1965")
("J LOUIS DAVID" . ":rachel:carte3077:coiffeur_rachel")
("DELOMPRE CURBILL" . ":rachel:carte3077:estheticienne")))
;;;###autoload
(defun ledger/update-associations ()
(interactive)
(with-current-buffer (find-file-noselect (getenv "LEDGER_FILE"))
(save-excursion
(cl-loop for (regexp . tag) in ledger/associations
do (progn
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(when (re-search-forward "\\(Expenses\\|Income\\)\\(:?[^ \n]*\\)\\( *€? *[0-9.]*\\)$" nil t)
(unless (string= tag (match-string 2))
(replace-match tag t nil nil 2)))))))))
(provide 'ledger-config)
;;; ledger-config.el ends here