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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
|
;;; account.el --- My configurations for recording my acconuts -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Jean Sévère Durand
;; Author: Jean Sévère Durand <durand@jsdurand.xyz>
;; Keywords: convenience, files
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file implements my preferred interface to record expenses and
;; to review my expenses.
;;
;; This is assumed to be called from the file "org-conf.el", so is not
;; an independent configuration file.
;;; Code:
;;; Recording account information
(defvar account-date-heading-regex nil
"A regular expression that matches the heading of a date line.
The first capture group is the date string.")
(setq account-date-heading-regex
(rx-to-string
(list
'seq
'bol
"* "
(list
'group-n 1
(list '= 4 'digit)
?-
(list '= 2 'digit)
?-
(list '= 2 'digit))
'eol)
t))
(defvar account-shop-heading-regex nil
"A regular expression that matches the heading of a shop.
The first capture group is the name of the shop.")
(setq account-shop-heading-regex
(rx-to-string
(list
'seq
'bol
"** "
(list
'group-n 1
(list 'zero-or-more 'not-newline))
'eol)
t))
(defvar account-item-heading-regex nil
"A regular expression that matches the heading of an item.
The first capture group is the name of the item.")
(setq account-item-heading-regex
(rx-to-string
(list
'seq
'bol
"*** "
(list
'group-n 1
(list 'zero-or-more 'not-newline))
'eol)
t))
(defvar account-purposes-list
(list
"breakfast" "brunch" "brunverage" "lunch"
"dinner" "beverage" "snack" "fruit" "book"
"misc"
"transportation")
"The list of purposes of accounts.")
(defun account-shop-item-alist ()
"Return an associative list of all shops and associated items."
(save-excursion
(save-match-data
(save-restriction
(widen)
(goto-char (point-min))
(let ((result-table (make-hash-table :test #'equal :size 650))
result-alist
shop-end shop
item item-set item-list item-cost
next-shop next-date)
(while (re-search-forward account-shop-heading-regex nil t)
(setq shop (match-string-no-properties 1))
(setq item-set (cond ((gethash shop result-table))
((make-hash-table :test #'equal))))
(setq
next-shop
(save-match-data
(save-excursion
(cond
((re-search-forward
account-shop-heading-regex nil t)
(forward-line 0)
(point))))))
(setq
next-date
(save-match-data
(save-excursion
(cond
((re-search-forward
account-date-heading-regex nil t)
(forward-line 0)
(point))))))
(setq
shop-end
(cond
((and next-shop next-date) (min next-shop next-date))
(next-shop)
(next-date)))
(while (re-search-forward
account-item-heading-regex shop-end t)
(setq item (match-string-no-properties 1))
(forward-line 1)
(setq item-cost nil)
(cond
((looking-at-p
(rx-to-string
(list 'seq 'bol ":PROPERTIES:" 'eol)
t))
(forward-line 1)
(cond
((looking-at
(rx-to-string
(list 'seq 'bol ":cost: "
(list 'zero-or-more 'space)
(list
'group-n 1
(list 'one-or-more (list 'any 'digit ?.)))
'eol)
t))
(setq item-cost
(string-to-number
(match-string-no-properties 1)))))))
(puthash item item-cost item-set))
(puthash shop item-set result-table))
(maphash
(lambda (k v)
(setq item-list nil)
(maphash
(lambda (vk vv)
(setq item-list (cons (cons vk vv) item-list)))
v)
(setq result-alist
(cons (cons k item-list) result-alist)))
result-table)
result-alist)))))
(defun account-one-newline ()
"Newline but enforce to have no blank lines around the point.
The function adds two newline characters, and deletes any blank
lines around the point."
(interactive)
(newline 2)
(delete-blank-lines))
(defvar account-capture-history nil
"History for capturing account information.")
(defvar account-capture-item-cost-history nil
"History for recording the costs of items.")
(defun account-capture-content ()
"Org capture template for account."
(let* ((shops-and-items
(with-temp-buffer
(insert-file-contents
(expand-file-name "account/account.org" org-directory))
(account-shop-item-alist)))
(shops (mapcar #'car shops-and-items))
(shop (completing-read
"Which shop? " shops
nil nil nil 'account-capture-history))
(purpose (completing-read
"Purpose? " account-purposes-list nil t))
(record-time (format-time-string "[%Y-%m-%d %a %H:%M]"))
(from (completing-read
"From: "
(list "cash" "card" "Line Pay Money"))))
(format
"** %s\n:PROPERTIES:\n:purpose: %s\n:from: %s\n\
:record_time: %s\n:END:\n"
shop purpose from record-time)))
(defun account-capture-location ()
(let ((date-string (format "* %s" (org-read-date))))
(save-match-data
(goto-char (point-min))
(cond
((re-search-forward
(rx-to-string (list 'seq 'bol date-string 'eol)
t)
nil t)
(cond
((re-search-forward account-date-heading-regex nil t)
(forward-line 0))
((goto-char (point-max))))
(account-one-newline))
((goto-char (point-max))
(account-one-newline)
(insert date-string)
(account-one-newline))))))
(defvar account-capture-template
(list "Account" 'plain
(list
'file+function
"account/account.org" #'account-capture-location)
(list 'function #'account-capture-content)
:jump-to-captured t
:empty-lines 0
:hook #'account-capture-mode)
"The template for capturing account information.")
(require 'org-capture)
(cond
((assoc "m" org-capture-templates #'equal)
(setcdr (assoc "m" org-capture-templates #'equal)
account-capture-template))
((add-to-list 'org-capture-templates
(cons "m" account-capture-template))))
(defvar account-capture-mode-map (make-sparse-keymap)
"The keymap that provides useful functions for recording account \
information.")
(define-minor-mode account-capture-mode
"A minor mode for recording account information."
:keymap account-capture-mode-map)
(define-key account-capture-mode-map (vector 3 ?i)
#'account-capture-add-item)
(defun account-capture-add-item ()
"Add an item when recording account information."
(interactive)
(let* ((shops (account-shop-item-alist))
(shop (save-match-data
(goto-char (point-min))
(cond
((re-search-forward
account-shop-heading-regex nil t)
(match-string-no-properties 1)))))
(items (cond (shop (cdr (assoc shop shops #'equal)))))
(item (completing-read "Item: " items))
(item-cost (cdr (assoc item items #'equal)))
(cost (read-number
"Cost: "
item-cost
'account-capture-item-cost-history)))
(goto-char (point-max))
(account-one-newline)
(insert (format
"*** %s\n:PROPERTIES:\n:cost: %d\n:END:\n"
item cost))))
;;; Reviewing account information
;; I remember I had written this display once, but I cannot find the
;; codes now, so I have to write it again.
(defun account-is-item-p (obj)
"Return t if and only if OBJ is a valid item.
An item is a list whose `car' is the symbol `account-item' and
whose `cdr' is a property list with the following keys.
- date
- shop name
- item name
- cost
- purpose
- payment method (from)
- record-time"
(and
(consp obj)
(eq (car obj) 'account-item)
(let ((date (plist-get (cdr obj) :date)))
(type-break-timep date))
(let ((shop (plist-get (cdr obj) :shop-name)))
(and (stringp shop) (not (string= shop ""))))
(let ((item (plist-get (cdr obj) :item-name)))
(and (stringp item) (not (string= item ""))))
(let ((cost (plist-get (cdr obj) :cost)))
(and (numberp cost) (>= cost 0)))
(let ((purpose (plist-get (cdr obj) :purpose)))
(and (stringp purpose) (not (string= purpose ""))))
(let ((method (or (plist-get (cdr obj) :payment-method)
(plist-get (cdr obj) :from))))
(and (stringp method) (not (string= method ""))))
(let ((time (plist-get (cdr obj) :record-time)))
(type-break-timep time))
t))
(defun account-find-next-heading ()
"Go to the next heading and return the number of leading stars.
If no heading is found, the point will not be moved, and nil will
be returned.
If a heading is found, the point will be placed one character
after the leading stars."
(cond
((save-match-data
(re-search-forward
(rx-to-string
(list 'seq 'bol (list 'or "*" "**" "***") 32)
t)
nil t))
(- (point) (pos-bol) 1))))
(defun account-collect-into-vec (vec collector)
"Return the vector of items in the current buffer.
See `account-is-item-p' for the definition of a valid item.
VEC is a vector of titles.
The COLLECTOR is responsible for collecting the items. More
precisely, it should be a function with one argument, which is an
item, and the collector should return a list of the form:
\(n value shop-name)
Here N is the index in the vector, and VALUE is the cost of the
item.
The N-th element of the vector VEC will be a list
\(TITLE VALUE ALIST-OF-SHOPS),
where TITLE is the original title, VALUE is the sum of all items
for which the collector gives N as its index, and ALIST-OF-SHOPS
is the associative list of all associated shop-names and
respective costs, if SHOP-NAME is non-nil.
In addition, an item is collected if and only if the function
`account-match-item' returns non-nil when called on
`account-agenda-filters' and the item.
This function does not move point, nor does it modify match data."
(save-match-data
(save-excursion
(goto-char (point-min))
(let* ((len (length vec))
(result (make-vector len nil))
(date-markers
(make-hash-table :size account-number-of-days
:test #'equal))
account-item
shop-marker date-marker
level date shop item cost purpose from time)
(let ((n 0))
(while (< n len)
(let ((title (aref vec n)))
(aset result n (list title 0 nil)))
(setq n (1+ n))))
(while (setq level (account-find-next-heading))
(save-match-data
(cond
((= level 1)
(cond
((looking-at
(rx-to-string
(list
'seq
(list 'group-n 1 (list '= 4 'digit))
?-
(list 'group-n 2 (list '= 2 'digit))
?-
(list 'group-n 3 (list '= 2 'digit)))
t))
(setq date (encode-time
(list
0 0 0
(string-to-number
(match-string-no-properties 3))
(string-to-number
(match-string-no-properties 2))
(string-to-number
(match-string-no-properties 1)))))
(setq
date-marker
(save-excursion
(goto-char (pos-bol)) (point-marker)))
(puthash date date-marker date-markers))
((user-error "Level one without date?"))))
((= level 2)
(setq shop (buffer-substring-no-properties
(point) (pos-eol)))
(setq shop-marker
(save-excursion
(goto-char (pos-bol))
(point-marker)))
(setq purpose nil)
(setq from nil)
(setq time nil)
(forward-line 1)
(cond
((not (looking-at-p "^:PROPERTIES:$"))
(user-error
"No properties for shop %s found" shop)))
(forward-line 1)
(cond
((not (looking-at-p "^:purpose: "))
(user-error
"No purposes for shop %s found" shop))
((setq purpose (buffer-substring-no-properties
(+ (point) 10) (pos-eol)))))
(forward-line 1)
(setq
from
(cond
((looking-at-p "^:from: ")
(buffer-substring-no-properties
(+ (point) 7) (pos-eol)))))
(forward-line 1)
(setq
time
(cond
((looking-at-p "^:record_time: ")
(encode-time
(take
6
(parse-time-string
(buffer-substring-no-properties
(+ (point) 15) (1- (pos-eol))))))))))
((= level 3)
(setq item (buffer-substring-no-properties
(point) (pos-eol)))
(forward-line 1)
(cond
((not (looking-at-p "^:PROPERTIES:$"))
(user-error
"No properties for item %s found" item)))
(forward-line 1)
(cond
((not (looking-at-p "^:cost: "))
(user-error "No cost for item %s found" item))
((setq cost (string-to-number
(buffer-substring-no-properties
(+ (point) 7) (pos-eol))))))
(setq
account-item
(list 'account-item
:date date
:shop-name shop
:item-name item
:cost cost
:purpose purpose
:payment-method from
:record-time time))
(let* ((collector-result
(funcall collector account-item))
(n (car collector-result))
(value (cadr collector-result))
(name (caddr collector-result)))
(cond
(collector-result
(let* ((res-element (aref result n))
(res-value (cadr res-element))
(res-list (caddr res-element))
(name-old-value
(or (car-safe
(alist-get name res-list))
0)))
(cond
((account-match-item
account-agenda-match account-item)
(setcar (cdr res-element)
(+ res-value value))
(cond
(name
(setf
(alist-get name (car (cddr res-element)))
(cons
(+ name-old-value value)
shop-marker)))))))))))
((user-error "Invalid level %d" level)))))
(let ((n 0))
(while (< n len)
(let* ((element (aref result n))
(title (car element))
(date
(get-text-property 0 'date title))
(date-encoded
(ignore-errors
(encode-time
(list 0 0 0
(cadr date)
(car date)
(caddr date)))))
(date-marker
(gethash date-encoded date-markers)))
(cond
(date-marker
(aset
result n
(cons
(propertize title 'org-marker date-marker)
(cdr element))))))
(setq n (1+ n))))
result))))
(defun account-agenda-command (match)
"The command for inserting acccount information to agenda.
The vector of titles is provided by the variable
`account-day-offsets' and the collector function is provided by
the variable `account-agenda-collector'.
MATCH is required by Org-agenda, and relentlessly ignored by us."
(ignore match)
(setq
account-day-offsets
(number-sequence 0 (1- account-number-of-days)))
(insert
(propertize
"Account:"
'face 'org-agenda-structure
'org-agenda-structural-header t
'org-agenda-type 'account
'org-date-line t))
(newline)
(let* ((titles (make-vector (length account-day-offsets) nil))
(all-cost 0)
(file (expand-file-name
"account.org"
(expand-file-name "account" org-directory)))
(buffer (progn (org-check-agenda-file file)
(org-get-agenda-file-buffer file)))
info)
(mapc
(lambda (n)
(let* ((date (decode-time))
(day (nth 3 date))
(month (nth 4 date))
(year (nth 5 date)))
(aset
titles
n
(account-agenda-format-date
(calendar-gregorian-from-absolute
(calendar-absolute-from-gregorian
(list month (- day n) year)))))))
account-day-offsets)
(setq
info
(with-current-buffer buffer
(account-collect-into-vec
titles account-agenda-collector)))
(mapc
(lambda (element)
(setq all-cost (+ all-cost (car (cdr element)))))
info)
(insert
(propertize
(format "all: %d" all-cost)
'face 'org-agenda-calendar-event))
(newline 2)
(mapc
(lambda (element)
(let* ((title (car element))
(cost (car (cdr element)))
(date (get-text-property 0 'date title))
(date-face (org-agenda-get-day-face date)))
(insert (propertize title 'face date-face))
(newline)
(mapc
(lambda (shop-info)
(insert
(propertize
(format
" %s: %d"
(car shop-info)
(cadr shop-info))
'face 'org-agenda-calendar-event
'org-marker (cddr shop-info)))
(newline))
(reverse (caddr element)))
(insert
(propertize
(format " all: %d" cost)
'face 'org-agenda-calendar-event
'daily-cost cost
'date date)))
(newline))
info)
(newline)
(goto-char (point-min))
(account-mode)))
(defun account-agenda-shop-command (match)
"The command for inserting acccount information to agenda.
The vector of titles is provided by the variable
`account-day-offsets' and the collector function is provided by
the variable `account-agenda-collector'
The difference with `account-agenda-command' is that this
function displays account information by shops, instead of by
days.
MATCH is required by Org-agenda, and relentlessly ignored by us."
(ignore match)
(setq
account-day-offsets
(number-sequence 0 (1- account-number-of-days)))
(insert
(propertize
"Account:"
'face 'org-agenda-structure
'org-agenda-structural-header t
'org-agenda-type 'account
'org-date-line t))
(newline)
(let* ((titles (make-vector (length account-day-offsets) nil))
(all-cost 0)
(file (expand-file-name
"account.org"
(expand-file-name "account" org-directory)))
(buffer (progn (org-check-agenda-file file)
(org-get-agenda-file-buffer file)))
info shop-cost-alist)
(mapc
(lambda (n)
(let* ((date (decode-time))
(day (nth 3 date))
(month (nth 4 date))
(year (nth 5 date)))
(aset
titles
n
(account-agenda-format-date
(calendar-gregorian-from-absolute
(calendar-absolute-from-gregorian
(list month (- day n) year)))))))
account-day-offsets)
(setq
info
(with-current-buffer buffer
(account-collect-into-vec
titles account-agenda-collector)))
(mapc
(lambda (element)
(let ((cost (car (cdr element))))
(setq all-cost (+ all-cost cost))
(mapc
(lambda (shop-info)
(let ((assoc-result
(assoc (car shop-info) shop-cost-alist #'equal)))
(cond
(assoc-result
(setcdr assoc-result
(+ (cadr shop-info)
(cdr assoc-result))))
((setq shop-cost-alist
(cons
(cons (car shop-info) (cadr shop-info))
shop-cost-alist))))))
(caddr element))))
info)
(setq shop-cost-alist
(sort
shop-cost-alist
(lambda (cell1 cell2)
(>= (cdr cell1) (cdr cell2)))))
(insert
(propertize
(format "all: %d" all-cost)
'face 'org-agenda-calendar-event))
(newline 2)
(mapc
(lambda (cell)
(insert
(propertize
(format
"%s: %d"
(car cell)
(cdr cell))
'face 'org-agenda-calendar-event))
(newline))
shop-cost-alist)
(newline)
(goto-char (point-min))
(account-mode)))
(defvar account-number-of-days 7
"The number of days to display.")
(defvar account-day-offsets nil
"The list of offsets for getting days to display in agenda.")
(setq
account-day-offsets
(number-sequence 0 (1- account-number-of-days)))
(defvar account-agenda-collector #'account-agenda-last-n-days
"The function for collecting account information.
See the function `account-collect-into-vec' for how the collector
should behave.")
(defun account-agenda-last-n-days (item)
"Collect ITEM if its date is within last `account-number-of-days' \
days."
(let ((days
(days-between
(format-time-string "%F" (current-time))
(format-time-string "%F" (plist-get (cdr item) :date)))))
(cond ((and (<= 0 days) (< days account-number-of-days))
(list
days
(plist-get (cdr item) :cost)
(plist-get (cdr item) :shop-name))))))
(defun account-agenda-format-date (date)
"Return the string for the DATE.
DATE should be a list (MONTH DAY YEAR) as returned from the
function `calendar-gregorian-from-absolute'."
(require 'cal-iso)
(let* ((calendar-day-name-array
(vector
"Dimanche" "Lundi" "Mardi" "Mercredi" "Jeudi" "Vendredi"
"Samedi"))
(calendar-month-name-array
(vector
"Janvier" "Février" "Mars" "Avril" "Mai" "Juin" "Juillet"
"Août" "Septembre" "Octobre" "Novembre" "Décembre"))
(dayname (calendar-day-name date))
(day (car (cdr date)))
(day-of-week (calendar-day-of-week date))
(month (car date))
(month-name (calendar-month-name month))
(year (car (cdr (cdr date))))
(iso-week (org-days-to-iso-week
(calendar-absolute-from-gregorian date)))
(weekstring (cond ((= day-of-week 1)
;; S stands for Semaine
(format " S%02d" iso-week))
(""))))
(propertize
(format "%-11s%2d %s %4d%s"
dayname day month-name year weekstring)
'date date)))
(define-derived-mode account-mode org-agenda-mode "Account"
"Major mode for viewing my account information.
The main purpose is to provide a dedicated keymap to manipulate
the displayed information.")
(define-key account-mode-map (vector 3 5)
#'account-agenda-export-to-svg)
(add-to-list 'org-agenda-custom-commands
(cons "m" "account related views"))
(add-to-list
'org-agenda-custom-commands
'("mm" "account default view" ((account-agenda-command))))
(add-to-list
'org-agenda-custom-commands
(list "ms" "account view by shop" '((account-agenda-shop-command))))
;;; Filtering account information
(defvar account-agenda-match nil
"The conditions to filter account information for display.
See the documentation of the function `account-match-item' for
the valid value.")
(defun account-match-item (match item)
"Return non-nil if and only if MATCH matches ITEM.
ITEM is a valid item if and only if the function
`account-is-item-p' returns t.
MATCH is a list of cons cells, whose `car' is one of the
following symbols:
- date
- shop
- item
- cost
- purpose
- method (from)
- time
The `cdr' of the cell can be of the following types:
- nil: the item must have nil value to match
- string: a regular expression to match the item value
- function: return non-nil if and only if item value matches.
The first element of MATCH can be the special symbol 'or', in
which case the item matches if one of the conditions matches;
otherwise all conditions must match for the item to be considered
a match."
(let ((temp match)
(result t)
car-temp item-value cdar-temp
temp-result or-mode)
(cond
((eq (car-safe temp) 'or)
(setq temp (cdr temp))
(setq or-mode t)))
(while (consp temp)
(setq car-temp (car temp))
(setq cdar-temp (cdr car-temp))
(setq temp (cdr temp))
(cond
((eq (car car-temp) 'date)
(setq item-value (plist-get (cdr item) :date)))
((eq (car car-temp) 'shop)
(setq item-value (plist-get (cdr item) :shop-name)))
((eq (car car-temp) 'item)
(setq item-value (plist-get (cdr item) :item-name)))
((eq (car car-temp) 'cost)
(setq item-value (plist-get (cdr item) :cost)))
((eq (car car-temp) 'purpose)
(setq item-value (plist-get (cdr item) :purpose)))
((or (eq (car car-temp) 'method)
(eq (car car-temp) 'from))
(setq item-value (plist-get (cdr item) :payment-method)))
((eq (car car-temp) 'time)
(setq item-value (plist-get (cdr item) :record-time)))
((error "Wrong symbol %S" (car car-temp))))
(cond
((null cdar-temp) (setq temp-result (null item-value)))
((stringp cdar-temp)
(setq temp-result
(and (stringp item-value)
(string-match-p cdar-temp item-value))))
((functionp cdar-temp)
(setq temp-result (funcall cdar-temp item-value)))
((error "Wrong condition: %S" cdar-temp)))
(cond
(or-mode
(cond
(temp-result
(setq result t)
(setq temp nil))))
(t
(cond
((not temp-result)
(setq result nil)
(setq temp nil))))))
result))
(defun account-agenda-add-filter (prop value)
"Add a filtering condition for PROP matching VALUE."
(interactive
(let* ((prop
(completing-read
"Property: "
(list
"date" "shop" "item" "purpose" "cost" "from" "time")
nil t))
(val (read-string (format "Match %s: " prop))))
(list prop val)))
(let ((prop (intern prop)))
(cond
((eq (car-safe account-agenda-match) 'or)
(setcdr account-agenda-match
(cons
(cons prop value)
(cdr account-agenda-match))))
((setq account-agenda-match
(cons
(cons prop value)
account-agenda-match))))))
(defun account-agenda-remove-filter ()
"Set `account-agenda-match' to nil."
(interactive)
(setq account-agenda-match nil))
(define-key account-mode-map (vector ?/) #'account-agenda-add-filter)
(define-key account-mode-map (vector ?|)
#'account-agenda-remove-filter)
;;; Exporting information as graphs
(defun account-agenda-export-to-svg ()
"Export the current agenda into an SVG image."
(interactive)
(save-restriction
(save-excursion
(save-match-data
(widen)
(goto-char (point-min))
(let ((svg-buffer-name "*account-svg*")
(max-cost 0)
prop cost date result temp)
(while (setq
prop
(text-property-search-forward
'daily-cost nil
(lambda (_ val)
(and (numberp val) (>= val 0)))
t))
(forward-char -1)
(setq cost (prop-match-value prop))
(setq max-cost (max max-cost cost))
(setq date (get-text-property (point) 'date))
(cond
((= cost 0))
((setq result
(cons (cons date cost)
result)))))
(let ((temp result) temp-cost)
(while (consp temp)
(setq temp-cost (cdr (car temp)))
(setcdr
(car temp)
(cons
(* (/ (float temp-cost) max-cost) 600)
temp-cost))
(setq temp (cdr temp))))
(require 'svg)
(let* ((len (length result))
(block-width 115)
(width (ceiling (* block-width len)))
(index 0)
(start 0)
(svg (svg-create width 700 :stroke-width 10)))
(svg-circle
svg
(ceiling width 2) 350 (max width 1000)
:fill-color "white")
(mapc
(lambda (cell)
(setq start (+ (* block-width index) 10))
(svg-rectangle
svg
start (- 650 (cadr cell))
(- block-width 20) (cadr cell)
:stroke-width 2
:stroke-color "blue"
:fill-color "blue")
(svg-text
svg
(format-time-string
"%F"
(encode-time
(list
0 0 0
(cadr (car cell))
(car (car cell))
(caddr (car cell)))))
:text-anchor "middle"
:font-size "15"
:font-weight "bold"
:stroke "red"
:fill "red"
:letter-spacing "1pt"
:x (+ start (/ (- block-width 20) 2.0))
:y 675
:stroke-width 1)
(svg-text
svg
(format "%d" (cddr cell))
:text-anchor "middle"
:font-size "15"
:font-weight "bold"
:stroke "red"
:fill "red"
:letter-spacing "1pt"
:x (+ start (/ (- block-width 20) 2.0))
:y (- 625 (cadr cell))
:stroke-width 1)
(setq index (1+ index)))
result)
(with-current-buffer
(get-buffer-create svg-buffer-name)
(let ((inhibit-read-only t))
(erase-buffer)
(svg-print svg)
(image-mode)))
(display-buffer
svg-buffer-name
(list
(list #'display-buffer-in-direction)
(cons 'direction 'right)
(cons 'window 'main)
(cons 'window-width 0.75)))
(select-window (get-buffer-window svg-buffer-name))))))))
(defun account-agenda-export-to-chart ()
"Export the current agenda into a plain-text chart."
(interactive)
(let ((chart-buffer-name "account-chart")
prop cost date result)
(save-restriction
(save-excursion
(save-match-data
(widen)
(goto-char (point-min))
(while (setq
prop
(text-property-search-forward
'daily-cost nil
(lambda (_ val)
(and (numberp val) (>= val 0)))
t))
(forward-char -1)
(setq cost (prop-match-value prop))
(setq date (get-text-property (point) 'date))
(cond
((= cost 0))
((setq result
(cons (cons date cost)
result))))))))
(require 'chart)
(let ((nc (make-instance
'chart-bar
:title chart-buffer-name
:key-label "8-m"
:direction 'horizontal)))
(chart-add-sequence
nc
(make-instance
'chart-sequence
:data (mapcar
(lambda (cell)
(account-agenda-format-date (car cell)))
result)
:name "Dates")
'y-axis)
(chart-add-sequence
nc
(make-instance
'chart-sequence
:data (mapcar #'cdr result)
:name "Costs")
'x-axis)
(let ((buffer (get-buffer-create
(format "*%s*" chart-buffer-name))))
(display-buffer
buffer
(list
(list #'display-buffer-in-direction)
(cons 'direction 'right)
(cons 'window 'main)
(cons 'window-width 0.75)))
(select-window (get-buffer-window buffer))
(switch-to-buffer buffer)
(chart-mode)
(setq chart-local-object nc)
(oset
nc x-width
(- (window-width (get-buffer-window buffer)) 10))
(oset
nc y-width
(* 3 (length result)))
(chart-draw nc)))))
(provide 'account)
;;; account.el ends here
|