-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vino.el
1812 lines (1568 loc) · 57.3 KB
/
vino.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
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
;;; vino.el --- Cellar tracking with vulpea -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2020-2022 Boris Buliga
;;
;; Author: Boris Buliga <boris@d12frosted.io>
;; Maintainer: Boris Buliga <boris@d12frosted.io>
;; Version: 0.3.0
;; Package-Requires: ((emacs "28.1") (vulpea "0.1") (org-roam "2.0.0"))
;;
;; 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
;; <http://www.gnu.org/licenses/>.
;;
;; License: GPLv3
;;
;; Created: 09 Jan 2021
;;
;; URL: https://github.com/d12frosted/vino
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; It's your cellar, your dear cantina.
;;
;;; Code:
(require 'vulpea)
(require 'org-roam)
;;; Configurations variables
;;;###autoload
(defvar vino-carbonation-types
'(still
sparkling)
"List of valid carbonation types.")
;;;###autoload
(defvar vino-colour-types
'(red
white
rose)
"List of valid colour types.
Orange wine is marked as white.")
;;;###autoload
(defvar vino-sweetness-levels
(list 'still '(dry
semi-dry
semi-sweet
sweet)
'sparkling '(brut-nature
extra-brut
brut
extra-dry
dry
demi-sec
doux))
"List of valid sweetness levels per carbonation type.")
(defvar vino-db-location (expand-file-name
"vino.db"
user-emacs-directory)
"The full path to file where the `vino' database is stored.
If this is non-nil, the `vino' sqlite database is saved here.")
(defvar vino-db-gc-threshold gc-cons-threshold
"The value to temporarily set the `gc-cons-threshold' threshold to.
During large, heavy operations like `vino-db-sync', many
GC operations happen because of the large number of temporary
structures generated (e.g. parsed ASTs). Temporarily increasing
`gc-cons-threshold' will help reduce the number of GC operations,
at the cost of temporary memory usage.
This defaults to the original value of `gc-cons-threshold', but
tweaking this number may lead to better overall performance. For
example, to reduce the number of GCs, one may set it to a large
value like `most-positive-fixnum'.")
;;; Availability
(defvar vino-availability-fn nil
"Function to check availability of `vino-entry'.
Function is called with ID of `vino-entry' and returns a cons of
acquired and consumed numbers.")
(defvar vino-availability-add-fn nil
"Function to add AMOUNT of `vino-entry' to inventory.
Function is called with ID of `vino-entry', AMOUNT, SOURCE and
DATE arguments.")
(defvar vino-availability-sub-fn nil
"Function to subtract AMOUNT of `vino-entry' from inventory.
Function is called with ID of `vino-entry', AMOUNT, ACTION and
DATE arguments.")
(defvar vino-sources-fn nil
"Function to get the list of sources.
Function is called with ID of `vino-entry'.")
;;; Setup
(defun vino-setup ()
"Setup `vino' library."
(seq-each
(lambda (x)
(add-to-list 'org-tags-exclude-from-inheritance x))
'("cellar"
"rating"
"producer"
"grape"
"region"
"appellation"))
(add-hook 'find-file-hook #'vino-db--update-on-save-h)
(add-hook 'kill-emacs-hook #'vino-db--close-all))
;;; Compat
;; Remove with support of Emacs 27.
(defmacro vino-dlet (binders &rest body)
"Like `let*' but using dynamic scoping.
Dynamically bind the BINDERS and evaluate the BODY."
(declare (indent 1) (debug let))
;; (defvar FOO) only affects the current scope, but in order for
;; this not to affect code after the `let*' we need to create a new
;; scope, which is what the surrounding `let' is for.
;; FIXME: (let () ...) currently doesn't actually create a new
;; scope, which is why we use (let (_) ...).
`(let (_)
,@(mapcar (lambda (binder)
`(defvar ,(if (consp binder) (car binder) binder)))
binders)
(let* ,binders ,@body)))
;;; Rating
;;;###autoload
(cl-defstruct (vino-rating
(:constructor
make-vino-rating
(&key wine date version values meta
&aux
(score
(seq-reduce
#'+
(seq-map
(lambda (value)
(nth 1 value))
values)
0))
(score-max
(seq-reduce
#'+
(seq-map
(lambda (value)
(nth 2 value))
values)
0))
(total (* 10.0
(/ (float score)
(float score-max)))))))
(wine
nil
:type vulpea-note
:documentation "Associated wine note.")
(date
nil
:type string
:documentation "Rating date in format YYYY-MM-DD.")
(version
nil
:type number
:documentation "Rating version.")
(score
nil
:type number
:documentation "Resulting score of the rating based on VALUES.")
(score-max
nil
:type number
:documentation "Max score of the rating based on VALUES.")
(total
nil
:type number
:documentation "Normalised SCORE from 0 to 10.")
(values
nil
:type list
:documentation "Rating values based on `vino-rating-props'.")
(meta
nil
:type list
:documentation "Extra meta defined by `vino-rating-extra-meta'."))
;;;###autoload
(defvar vino-rating-template
(list :file-name "wine/rating/${id}.org")
"Capture template for rating entry.
Template is a property list accepting following values:
:file-name (mandatory) - file name relative to
`org-roam-directory'
:head (optional) - extra note header
:body (optional) - note body
:properties (optional) - extra properties to put in PROPERTIES
block
:context (optional) - extra variables for :file-name, :head,
:body
:tags (optional) - extra tags in addition to wine and rating
See `vulpea-create' for more information.")
(defvar vino-rating-props nil
"Rating properties per version.
`vino-entry-rate' uses the latest element from list to read the
rating. Each rating contains the rating system version, so when
the rating is updated/refreshed `vino' knows what properties to
use and how to use them.
This variable is a list of all rating systems, stating with the
first version up to the current one. This variable has the
following format:
'((1 . PROPS)
(2 . PROPS)
(3 . PROPS)
...)
And PROPS defines a specific version of rating system:
((\"PROP_1\" . PROP)
(\"PROP_2\" . PROP)
(\"PROP_3\" . PROP)
...)
Each PROP can be of one of the following types:
- NUMBER - then the property value is a number between 0 (inclusively)
and PROP (exclusively), user is prompted for a number using
`read-number' during `vino-entry-rate';
Example: '(\"prop_number\" . 10)
- LIST - then the property value is a number between 0 (inclusively)
and the `length' of PROP (exclusively), user is prompted to select
one element from the list `car's using `completing-read' during
`vino-entry-rate' and the `cdr' of selected element is used as
value;
Example: '(\"prop_list\" . ((\"max\" . 2)
(\"avg\" . 1)
(\"min\" . 0)))
- FUNCTION - then the property value is a number between 0 and `cdr'
of PROP result, function is called with without arguments during
`vino-entry-rate' and `car' of the result is used as value.
Example: '(\"prop_function\" . (lambda () (cons 42 100))")
(defvar vino-rating-extra-meta nil
"Extra `vulpea-meta' associated with rating.
The value is a list, with each element being a plist of the
following form:
(:name :read-fn :mode :type)
where
:mode can be either single or multiple.
:type is any type supported by `vulpea-meta-get'
This is not stored in `vino-db', but can be retrieved from
`vulpea-db' if you properly configured meta persistence.")
;;;###autoload
(defun vino-rating-get-by-id (note-or-id)
"Get `vino-rating' represented by NOTE-OR-ID."
(unless vino-rating-props
(user-error "Expected vino-rating-props to be non-nil"))
(let ((note (if (stringp note-or-id)
(vulpea-db-get-by-id note-or-id)
note-or-id)))
(when (and note (vino-rating-note-p note))
(let*
((wine (vulpea-note-meta-get note "wine" 'note))
(date (vulpea-note-meta-get note "date"))
(version (vulpea-note-meta-get note "version" 'number))
(info (seq-find
(lambda (x) (equal (car x) version))
vino-rating-props))
(_ (unless info
(user-error
"Could not find ratings props of version %s"
version)))
(props (cdr info))
(values (seq-map
(lambda (cfg)
(let ((name (downcase (car cfg))))
(list
name
(vulpea-note-meta-get
note name 'number)
(vulpea-note-meta-get
note (concat name "_max") 'number))))
props))
(extra-meta (vino-rating--meta note)))
(make-vino-rating
:wine wine
:date date
:version version
:values values
:meta extra-meta)))))
;;;###autoload
(defun vino-rating-note-p (note)
"Return non-nil if NOTE represents `vino-rating'."
(when-let* ((tags (vulpea-note-tags note))
(level (vulpea-note-level note)))
(and (equal level 0)
(seq-contains-p tags "wine")
(seq-contains-p tags "rating"))))
;;;###autoload
(defun vino-rating-update (note-or-id)
"Refresh rating represented by NOTE-OR-ID."
(let* ((note (if (stringp note-or-id)
(vulpea-db-get-by-id note-or-id)
note-or-id))
(rating (vino-rating-get-by-id note)))
(vulpea-meta-set note "score"
(vino-rating-score rating) 'append)
(vulpea-meta-set note "score_max"
(vino-rating-score-max rating) 'append)
(vulpea-meta-set note "total"
(vino-rating-total rating) 'append)))
(defun vino-rating--read-value (prop)
"Read a rating value defined by PROP.
See `vino-rating-props' for specification.
Return a list (name score score-max)."
(cond
((functionp (cdr prop))
(let ((res (funcall (cdr prop))))
(list (car prop)
(car res)
(cdr res))))
((numberp (cdr prop))
(list (car prop)
(min
(cdr prop)
(max
0
(read-number
(format "%s: (0 to %i): "
(vino--format-prop (car prop))
(cdr prop)))))
(cdr prop)))
((listp (cdr prop))
(let* ((ans (completing-read
(concat (vino--format-prop (car prop))
": ")
(seq-map #'car (cdr prop))))
(res (assoc ans (cdr prop))))
(list (car prop)
(cdr res)
(- (length (cdr prop)) 1))))))
(defun vino-rating--meta (note)
"Extract meta from NOTE according to `vino-rating-extra-meta'."
(seq-filter
#'identity
(seq-map
(lambda (x)
(let* ((name (plist-get x :name))
(type (or (plist-get x :type)))
(mode (or (plist-get x :mode)
'single))
(value (pcase mode
(`single
(when-let ((v (vulpea-note-meta-get
note name type)))
(list v)))
(`multiple
(vulpea-note-meta-get-list
note name type)))))
(when value
(cons name value))))
vino-rating-extra-meta)))
(defun vino-rating--read-meta ()
"Read extra meta defined by `vino-rating-extra-meta'."
(seq-map
(lambda (x)
(let ((name (plist-get x :name))
(read-fn (plist-get x :read-fn))
(mode (or (plist-get x :mode)
'single)))
(cons
name
(pcase mode
(`single (list (funcall read-fn)))
(`multiple (vino--collect-while read-fn nil))))))
vino-rating-extra-meta))
(defun vino-rating--create (rating &optional id)
"Create a note for RATING.
ID is generated unless passed."
(when-let*
((wine-note (vino-rating-wine rating))
(vino-entry (vino-entry-get-by-id wine-note))
(producer (vino-entry-producer vino-entry))
(date-str (vino-rating-date rating))
(version (vino-rating-version rating))
(values (vino-rating-values rating))
(title (format "%s %s %s - %s"
(vulpea-note-title producer)
(vino-entry-name vino-entry)
(or (vino-entry-vintage vino-entry) "NV")
date-str))
(body (with-temp-buffer
;; TODO: performance of multiple `vulpea-meta-set'
(vulpea-buffer-meta-set "wine" wine-note 'append)
(vulpea-buffer-meta-set "date" date-str 'append)
(vulpea-buffer-meta-set "version" version 'append)
(seq-do (lambda (data)
(vulpea-buffer-meta-set
(downcase (nth 0 data))
(nth 1 data)
'append)
(vulpea-buffer-meta-set
(downcase (concat (nth 0 data) "_MAX"))
(nth 2 data)
'append))
values)
(vulpea-buffer-meta-set
"score" (vino-rating-score rating) 'append)
(vulpea-buffer-meta-set
"score_max" (vino-rating-score-max rating) 'append)
(vulpea-buffer-meta-set
"total" (vino-rating-total rating) 'append)
(seq-do (lambda (meta)
(vulpea-buffer-meta-set
(car meta) (cdr meta) 'append))
(vino-rating-meta rating))
(buffer-substring (point-min)
(point-max))))
(note (vulpea-create
title
(plist-get vino-rating-template :file-name)
:id id
:tags (seq-union (plist-get vino-rating-template :tags)
'("wine" "rating"))
:head (plist-get vino-rating-template :head)
:body (concat (plist-get vino-rating-template :body)
body)
:context (plist-get vino-rating-template :context)
:properties (plist-get vino-rating-template :properties)
:unnarrowed t
:immediate-finish t)))
(vulpea-meta-set
wine-note
"ratings"
(cons note
(vulpea-meta-get-list wine-note "ratings" 'note))
'append)
(vino-db-update-rating rating note)
(vino-entry-update wine-note)
note))
;;; Entry
;;;###autoload
(defvar vino-entry-template
(list :file-name "wine/cellar/${id}.org")
"Capture template for wine entry.
Template is a property list accepting following values:
:file-name (mandatory) - file name relative to
`org-roam-directory'
:head (optional) - extra note header
:body (optional) - note body
:properties (optional) - extra properties to put in PROPERTIES
block
:context (optional) - extra variables for :file-name, :head,
:body
:tags (optional) - extra tags in addition to wine and cellar
See `vulpea-create' for more information.")
;;;###autoload
(cl-defstruct vino-entry
carbonation
colour
sweetness
producer
name
vintage
appellation
region
grapes
alcohol
sugar
resources
price
acquired
consumed
rating
ratings)
;;;###autoload
(defun vino-entry-find-file ()
"Select and find vino note."
(interactive)
(find-file (vulpea-note-path (vino-entry-note-select))))
;;;###autoload
(defun vino-entry-read ()
"Read a `vino-entry'."
(let* ((producer (vino-producer-select))
(name (vino--repeat-while
#'read-string
#'string-empty-p
"Name: "))
(vintage (vino--repeat-while
#'read-number
(lambda (v) (< v 1900))
"Vintage (C-g for NV): "))
(rora (vino-region-select))
(region (when (seq-contains-p (vulpea-note-tags rora)
"region")
rora))
(appellation (when (seq-contains-p (vulpea-note-tags rora)
"appellation")
rora))
(grapes (vino--collect-while #'vino-grape-select nil))
(alcohol (vino--repeat-while
#'read-number
(lambda (v) (< v 0))
"Alcohol: "))
(sugar (vino--repeat-while
#'read-number
(lambda (v) (< v 0))
"Sugar g/l (C-g for N/A): "))
(colour (intern
(completing-read
"Colour: "
vino-colour-types
nil
'require-match)))
(carbonation (intern
(completing-read
"Carbonation: "
vino-carbonation-types
nil
'require-match)))
(sweetness (intern
(completing-read
"Sweetness:"
(plist-get vino-sweetness-levels
carbonation)
nil
'require-match)))
(resources (vino--collect-while
#'read-string
(lambda (v) (not (string-empty-p v)))
"Resource: "))
(price (read-string "Price: ")))
(make-vino-entry
:carbonation carbonation
:colour colour
:sweetness sweetness
:producer producer
:name name
:vintage vintage
:appellation appellation
:region region
:grapes grapes
:alcohol alcohol
:sugar sugar
:resources resources
:price price
:acquired 0
:consumed 0
:rating nil
:ratings nil)))
;;;###autoload
(defun vino-entry-get-by-id (note-or-id)
"Get `vino-entry' by NOTE-OR-ID."
(let ((note (if (stringp note-or-id)
(vulpea-db-get-by-id note-or-id)
note-or-id)))
(when (and note (vino-entry-note-p note))
(let ((meta (vulpea-meta note)))
(make-vino-entry
:carbonation (vulpea-buffer-meta-get!
meta "carbonation" 'symbol)
:colour (vulpea-buffer-meta-get! meta "colour" 'symbol)
:sweetness (vulpea-buffer-meta-get!
meta "sweetness" 'symbol)
:producer (if-let ((producer (vulpea-buffer-meta-get!
meta "producer" 'note)))
producer
(lwarn 'vino :error
"Producer is not an existing note in entry %s"
(vulpea-note-id note)))
:name (vulpea-buffer-meta-get! meta "name" 'string)
:vintage (vino--parse-opt-number
(vulpea-buffer-meta-get! meta "vintage")
"NV")
:appellation (vulpea-buffer-meta-get!
meta "appellation" 'note)
:region (vulpea-buffer-meta-get! meta "region" 'note)
:grapes (vulpea-buffer-meta-get-list! meta "grapes" 'note)
:alcohol (vulpea-buffer-meta-get! meta "alcohol" 'number)
:sugar (vulpea-buffer-meta-get! meta "sugar" 'number)
:acquired (vulpea-buffer-meta-get! meta "acquired" 'number)
:consumed (vulpea-buffer-meta-get! meta "consumed" 'number)
:resources (vulpea-buffer-meta-get-list!
meta "resources" 'link)
:price (vulpea-buffer-meta-get-list! meta "price" 'string)
:rating (vino--parse-opt-number
(vulpea-buffer-meta-get! meta "rating" 'string)
"NA")
:ratings (vulpea-buffer-meta-get-list!
meta "ratings" 'note))))))
;;;###autoload
(defun vino-entry-create ()
"Create a `vino-entry'."
(interactive)
(let ((note (vino-entry--create (vino-entry-read))))
(when (y-or-n-p "Acquire? ")
(vino-entry-acquire note))))
(defun vino-entry--create (vino &optional id)
"Create an entry for VINO.
ID is generated unless passed."
(let* ((producer (vino-entry-producer vino))
(producer (if (vulpea-note-p producer)
producer
(vulpea-db-get-by-id producer)))
(vintage (vino-entry-vintage vino))
(title (concat (vulpea-note-title producer)
" "
(vino-entry-name vino)
" "
(if (numberp vintage)
(number-to-string vintage)
vintage)))
(body
(with-temp-buffer
;; TODO: optimize multiple calls
(vulpea-buffer-meta-set
"carbonation" (vino-entry-carbonation vino) 'append)
(vulpea-buffer-meta-set
"colour" (vino-entry-colour vino) 'append)
(vulpea-buffer-meta-set
"sweetness" (vino-entry-sweetness vino) 'append)
(vulpea-buffer-meta-set
"producer" (vino-entry-producer vino) 'append)
(vulpea-buffer-meta-set
"name" (vino-entry-name vino) 'append)
(when-let ((vintage (vino-entry-vintage vino)))
(vulpea-buffer-meta-set "vintage" vintage 'append))
(when-let ((appellation (vino-entry-appellation vino)))
(vulpea-buffer-meta-set
"appellation" appellation 'append))
(when-let ((region (vino-entry-region vino)))
(vulpea-buffer-meta-set "region" region 'append))
(vulpea-buffer-meta-set
"grapes" (vino-entry-grapes vino) 'append)
(let ((alcohol (vino-entry-alcohol vino)))
(when (and alcohol
(> alcohol 0))
(vulpea-buffer-meta-set "alcohol" alcohol 'append)))
(let ((sugar (vino-entry-sugar vino)))
(when (and sugar (>= sugar 0))
(vulpea-buffer-meta-set "sugar" sugar 'append)))
(when (vino-entry-price vino)
(vulpea-buffer-meta-set
"price" (vino-entry-price vino) 'append))
(let ((acquired (or (vino-entry-acquired vino) 0))
(consumed (or (vino-entry-consumed vino) 0)))
(vulpea-buffer-meta-set "acquired" acquired 'append)
(vulpea-buffer-meta-set "consumed" consumed 'append)
(vulpea-buffer-meta-set
"available" (- acquired consumed) 'append))
(vulpea-buffer-meta-set
"resources" (vino-entry-resources vino) 'append)
(vulpea-buffer-meta-set
"rating" (or (vino-entry-rating vino) "NA") 'append)
(vulpea-buffer-meta-set
"ratings" (vino-entry-ratings vino) 'append)
(buffer-substring (point-min)
(point-max))))
(note
(vulpea-create
title
(plist-get vino-entry-template :file-name)
:id id
:tags (seq-union (plist-get vino-entry-template :tags)
'("wine" "cellar"))
:head (plist-get vino-entry-template :head)
:body (concat (plist-get vino-entry-template :body)
body)
:context (plist-get vino-entry-template :context)
:properties (plist-get vino-entry-template :properties)
:unnarrowed t
:immediate-finish t)))
(vino-db-update-entry vino note)
note))
;;;###autoload
(defun vino-entry-set-grapes (&optional note-or-id grapes)
"Set GRAPES to `vino-entry'.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id))
(grapes (or grapes
(vino--collect-while #'vino-grape-select nil))))
(vulpea-meta-set note "grapes" grapes 'append)))
;;;###autoload
(defun vino-entry-set-region (&optional note-or-id region)
"Set REGION to `vino-entry'.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly.
REGION may be either region or appellation."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id))
(region (cond
((stringp region) (vulpea-db-get-by-id region))
((vulpea-note-p region) region)
(t (vino-region-select)))))
(when (seq-contains-p (vulpea-note-tags region) "region")
(vulpea-meta-remove note "appellation")
(vulpea-meta-set note "region" region 'append))
(when (seq-contains-p (vulpea-note-tags region) "appellation")
(vulpea-meta-remove note "region")
(vulpea-meta-set note "appellation" region 'append))
;; TODO: sort metadata
))
;;;###autoload
(defun vino-entry-update (&optional note-or-id)
"Update `vino-entry'.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly.
The following things are updated:
- total score of each linked ratings;
- rating of the `vino-entry';
- availability of `vino-entry'."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id)))
(vino-entry-update-rating note)
(vino-entry-update-availability note)))
;;;###autoload
(defun vino-entry-update-title (&optional note-or-id)
"Update title of `vino-entry'..
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly.
The following things are updated:
- link description of producer;
- title of the `vino-entry';
- title of every linked rating."
(interactive)
;; TODO: vulpea-meta performance
(let* ((note (vino-entry-note-get-dwim note-or-id))
(meta (vulpea-meta note))
(title (format
"%s %s %s"
(vulpea-note-title
(vulpea-buffer-meta-get! meta "producer" 'note))
(vulpea-buffer-meta-get! meta "name")
(or (vulpea-buffer-meta-get! meta "vintage") "NV"))))
(vulpea-utils-with-note note
(vulpea-buffer-title-set title)
(save-buffer))
(vulpea-db-update note)
(setq note (vulpea-db-get-by-id (vulpea-note-id note)))
(vulpea-meta-set
note
"ratings"
(seq-map
(lambda (rn)
(vulpea-utils-with-note rn
(vulpea-buffer-title-set
(format "%s - %s"
title
(vulpea-meta-get rn "date")))
(save-buffer))
(vulpea-db-update rn)
(vulpea-meta-set rn "wine" note)
(vulpea-db-get-by-id (vulpea-note-id rn)))
(vulpea-meta-get-list note "ratings" 'note)))))
;;;###autoload
(defun vino-entry-update-rating (note-or-id)
"Update rating metadata of `vino-entry'.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly.
- total score of each linked ratings;
- rating of the `vino-entry'."
(let* ((note (vino-entry-note-get-dwim note-or-id))
(ratings (vulpea-meta-get-list note "ratings" 'note))
(values (seq-map
(lambda (rn)
(vino-rating-update rn)
(vulpea-meta-get rn "total" 'number))
ratings))
(rating (if (null values)
0
(/ (apply #'+ values)
(float (length values))))))
(vulpea-meta-set note "rating" rating 'append)
(vulpea-meta-set
note
"ratings"
(seq-sort-by #'vulpea-note-title
#'string<
ratings)
'append)))
;;;###autoload
(defun vino-entry-update-availability (note-or-id)
"Update available metadata of `vino-entry'.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly."
(unless vino-availability-fn
(user-error "`vino-availability-fn' is nil"))
(let* ((note (vino-entry-note-get-dwim note-or-id))
(res (funcall vino-availability-fn (vulpea-note-id note)))
(in (car res))
(out (cdr res))
(cur (- in out)))
(vulpea-meta-set note "acquired" in 'append)
(vulpea-meta-set note "consumed" out 'append)
(vulpea-meta-set note "available" cur 'append)))
;;;###autoload
(defun vino-entry-acquire (&optional note-or-id
amount source price date)
"Acquire AMOUNT of `vino-entry' from SOURCE for PRICE at DATE.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id))
(id (vulpea-note-id note))
(vino (vino-entry-get-by-id id))
(source (or source
(if vino-sources-fn
(completing-read
"Source: "
(funcall vino-sources-fn id))
(read-string "Source: "))))
(amount (or amount (read-number "Amount: " 1)))
(price (or price (vino-price-read vino)))
(date (or date (org-read-date nil t))))
(funcall vino-availability-add-fn
id amount source date)
(let ((prices (vino-entry-price vino)))
(unless (seq-contains-p prices price)
(vulpea-meta-set note "price" (cons price prices))))
(vino-entry-update-availability id)))
;;;###autoload
(defun vino-entry-consume (&optional note-or-id amount action date)
"Consume AMOUNT of `vino-entry' because of ACTION at DATE.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id))
(id (vulpea-note-id note))
(action (or action (read-string "Action: " "consume")))
(amount (or amount
(read-number
"Amount: "
(or (min 1.0 (vulpea-meta-get
note "available" 'number))
1))))
(date (or date (org-read-date nil t))))
(funcall vino-availability-sub-fn
id amount action date)
(vino-entry-update-availability id)
(when (and (string-equal action "consume")
(y-or-n-p "Rate? "))
(vino-entry-rate id date))))
;;;###autoload
(defun vino-entry-rate (&optional note-or-id date)
"Rate a `vino-entry' on DATE.
When NOTE-OR-ID is non-nil, it is used to get `vino-entry'.
Otherwise if current buffer is visiting `vino-entry', it used
instead. Otherwise user is prompted to select a `vino-entry'
explicitly."
(interactive)
(when-let* ((note (vino-entry-note-get-dwim note-or-id))
(date (or date (org-read-date nil t)))
(info (car (last vino-rating-props)))
(version (car info))
(props (cdr info))
(values (seq-map #'vino-rating--read-value props))
(extra-meta (vino-rating--read-meta)))
(vino-rating--create
(make-vino-rating
:wine note
:date (format-time-string "%Y-%m-%d" date)
:version version
:values values
:meta extra-meta))))
;;; Vino entry note
;;;###autoload
(defun vino-entry-note-p (note)
"Return non-nil if NOTE represents vino entry."
(when-let ((tags (vulpea-note-tags note))
(level (vulpea-note-level note)))
(and (equal level 0)
(seq-contains-p tags "wine")