-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vino.el
1566 lines (1300 loc) · 51 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 <[email protected]>
;; Maintainer: Boris Buliga <[email protected]>
;; Version: 0.4.0
;; Package-Requires: ((emacs "29.1") (vulpea "0.3") (org-roam "2.0.0") (dash "2.19.1") (s "1.13.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-carbonation-methods
'(traditional
charmat
ancestral
transfer
injection
unknown)
"List of valid carbonation methods.")
;;;###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.")
;;; Hooks
(defvar vino-entry-create-handle-functions nil
"Abnormal hooks to run after vino entry is created.
Each function accepts a `vulpea-note'.")
(defvar vino-entry-update-handle-functions nil
"Abnormal hooks to run after vino rating is updated.
Each function accepts a `vulpea-note'.")
(defvar vino-rating-create-handle-functions nil
"Abnormal hooks to run after vino rating is created.
Each function accepts a `vulpea-note' and extra-data passed to
`vino-entry-rate'.")
;;; Selection config
(defvar vino-origin-select-fn #'vino-origin-select-by-country
"Interactive function to select wine's origin.
The function doesn't receive any arguments and must return an alist. The
expectation is that resulting alist contains country and optionally it
may include region and/or appellation. The function may return other
keys as well, but they don't bear any meaning for vino library - this is
used only for custom extensions.
Vino comes with two functions that can be used as the value:
- `vino-origin-select-flat' - it just lists union of all regions and
appellations, and when user picks
region, it returns (country region) where country is country
meta value of the selected region
appellation, it returns (country appellation) where country is
country meta value of the selected appellation;
- `vino-origin-select-by-country' - this is a multi-step selection,
where user is asked to pick a country first; and then is presented
with a union of all regions and appellations that have country meta
value link to selected country.
You can implement your own, more sophisticated selection function based
on your situation. For example, you can pick extra routes based on the
country.")
;;; Setup
(defun vino-setup ()
"Setup `vino' library."
(seq-each
(lambda (x)
(add-to-list 'org-tags-exclude-from-inheritance x))
'("cellar"
"rating"
"producer"
"grape"
"country"
"region"
"appellation")))
;;; Rating
;;;###autoload
(cl-defstruct (vino-rating
(:constructor
make-vino-rating
(&key wine date version values
&aux
(score (vino-rating--score values))
(score-max (vino-rating--score-max values))
(total (vino-rating--total score 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'."))
(defvar vino-rating-scale 5.0
"Scale used for rating calculations.
There are a few commonly used scales:
- 5.0 - as used in applications like Vivino;
- 10.0 - as used in Italian schools;
- 20.0 - as used by Jancis Robinson;
- 100 - as used by many professionals.")
(defvar vino-rating-precision 1
"Precision used for rating calculations.
When set to number, all ratings are __rounded__ to N digits after
point.
When the value is nil, no rounding happens.")
(defvar vino-rating-precision--fmt nil)
(defun vino-rating--round (num)
"Round NUM using `vino-rating-precision'."
(when (numberp vino-rating-precision)
(unless vino-rating-precision--fmt
(setq vino-rating-precision--fmt
(concat "%." (number-to-string vino-rating-precision) "f"))))
(if vino-rating-precision--fmt
(string-to-number (format vino-rating-precision--fmt num))
num))
(defun vino-rating--score (values)
"Calculate score of a rating from VALUES."
(vino-rating--round
(seq-reduce
#'+
(seq-map
(lambda (value)
(nth 1 value))
values)
0)))
(defun vino-rating--score-max (values)
"Calculate max score of a rating from VALUES."
(vino-rating--round
(seq-reduce
#'+
(seq-map
(lambda (value)
(nth 2 value))
values)
0)))
(defun vino-rating--total (score score-max)
"Calculate total score of a rating from SCORE and SCORE-MAX."
(vino-rating--round
(* vino-rating-scale
(/ (float score)
(float score-max)))))
;;;###autoload
(defvar vino-rating-template
'(:file-name "wine/rating/${id}.org"
:tags ("wine" "rating"))
"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 `((1 . (("SCORE" . ,vino-rating-scale))))
"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 (inclusively), 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))")
;;;###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)))
(make-vino-rating
:wine wine
:date date
:version version
:values values)))))
(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"))))
(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--create (rating &optional id extra-data)
"Create a note for RATING.
ID is generated unless passed.
EXTRA-DATA is passed to `vino-rating-create-handle-functions'."
(when-let*
((wine-note (vino-rating-wine rating))
(date-str (vino-rating-date rating))
(version (vino-rating-version rating))
(values (vino-rating-values rating))
(title (format "%s - %s" (vulpea-note-title wine-note) 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)
(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-utils-with-note wine-note
(vulpea-buffer-meta-set
"ratings"
(cons note (vulpea-note-meta-get-list wine-note "ratings" 'note)))
(save-buffer))
(vino-entry-update (vulpea-db-get-by-id (vulpea-note-id wine-note)))
(run-hook-with-args 'vino-rating-create-handle-functions note extra-data)
note))
;;; Entry
;;;###autoload
(defvar vino-entry-template
'(:file-name "wine/cellar/${id}.org"
:tags ("wine" "cellar"))
"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
(defvar vino-entry-rating-average-method 'amean
"Method to calculate rating of vino entry.
All ratings are averaged using selected method and then the result is
rounded to respect `vino-rating-precision'.
Supported methods:
- amean (arithmetic mean)
- min
- max
- oldest - when multiple ratings share the date, result is not defined
- latest - when multiple ratings share the date, result is not defined
The value can be function that takes a list of ratings (numbers) and
returns a single number. Even if a custom function is used, the
resulting rating is still rounded to respect `vino-rating-precision'.")
(defvar vino-entry-meta-props-order
'("carbonation"
"carbonation method"
"colour"
"sweetness"
"producer"
"name"
"vintage"
"base"
"sur lie"
"degorgee"
"volume"
"country"
"region"
"appellation"
"grapes"
"alcohol"
"sugar"
"price"
"acquired"
"consumed"
"available"
"rating"
"ratings"))
;;;###autoload
(defun vino-entry-find-file ()
"Select and find vino note."
(interactive)
(find-file (vulpea-note-path (vino-entry-note-select))))
(defun vino-entry-insert ()
"Select and insert vino note."
(interactive)
(unwind-protect
(atomic-change-group
(let* (region-text
beg end
(_ (when (region-active-p)
(setq
beg (set-marker
(make-marker) (region-beginning))
end (set-marker
(make-marker) (region-end))
region-text
(org-link-display-format
(buffer-substring-no-properties
beg end)))))
(note (vino-entry-note-select region-text))
(description (or region-text (vulpea-note-title note))))
(unless (vulpea-note-id note)
(setq note (vino-entry-create)))
(when region-text
(delete-region beg end)
(set-marker beg nil)
(set-marker end nil))
(insert (org-link-make-string
(concat "id:" (vulpea-note-id note))
description))
(run-hook-with-args 'vulpea-insert-handle-functions note)))
(deactivate-mark)))
;;;###autoload
(defun vino-entry-create ()
"Create a `vino-entry'.
The `vulpea-insert-handle-functions' are called with the created
note as the only argument."
(interactive)
(let* ((producer (vino-producer-select))
(name (vino--repeat-while
#'read-string
#'string-empty-p
"Name: "))
(colour (intern
(completing-read
"Colour: "
vino-colour-types
nil
'require-match)))
(carbonation (intern
(completing-read
"Carbonation: "
vino-carbonation-types
nil
'require-match)))
(carbonation-method (when (eq carbonation 'sparkling)
(intern
(completing-read
"Carbonation Method: "
vino-carbonation-methods
nil
'require-match))))
(sweetness (intern
(completing-read
"Sweetness:"
(plist-get vino-sweetness-levels
carbonation)
nil
'require-match)))
(vintage (vino--repeat-while
#'read-number
(lambda (v) (< v 1900))
"Vintage (C-g for NV): "))
(base-vintage (when (and (eq carbonation-method 'traditional)
(not vintage))
(vino--repeat-while
#'read-number
(lambda (v) (< v 1900))
"Base vintage (C-g for NV): ")))
(sur-lie (when (eq carbonation-method 'traditional)
(let ((x (read-string "Sur lie: ")))
(if (string-empty-p x)
"N/A"
x))))
(degorgee (when (eq carbonation-method 'traditional)
(let ((x (read-string "Degorgee: ")))
(if (string-empty-p x)
"N/A"
x))))
(volume (vino--repeat-while
#'read-number
(lambda (v) (< v 1))
"Volume mL: " 750))
(origin (funcall-interactively vino-origin-select-fn))
(grapes (vino-grapes-select))
(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): "))
(price (s-presence (read-string "Price: ")))
(title (format "%s %s %s"
(vulpea-note-title producer)
name
(or vintage "NV")))
(note (vulpea-create
title
(plist-get vino-entry-template :file-name)
:tags (seq-union (plist-get vino-entry-template :tags)
'("wine" "cellar"))
:head (plist-get vino-entry-template :head)
:body (plist-get vino-entry-template :body)
:context (plist-get vino-entry-template :context)
:properties (plist-get vino-entry-template :properties)
:meta (-filter
#'cdr
(append
`(("carbonation" . ,carbonation)
("carbonation method" . ,carbonation-method)
("colour" . ,colour)
("sweetness" . ,sweetness)
("producer" . ,producer)
("name" . ,name)
("vintage" . ,vintage)
("base" . ,base-vintage)
("sur lie" . ,sur-lie)
("degorgee" . ,degorgee)
("volume" . ,volume))
origin
`(("grapes" . ,grapes)
("alcohol" . ,alcohol)
("sugar" . ,sugar)
("price" . ,price)
("acquired" . 0)
("consumed" . 0)
("available" . 0)
("rating" . "NA"))))
:unnarrowed t
:immediate-finish t)))
(run-hook-with-args 'vino-entry-create-handle-functions 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-grapes-select))))
(vulpea-utils-with-note note
(vulpea-buffer-meta-set "grapes" grapes 'append)
(save-buffer))))
;;;###autoload
(defun vino-entry-set-region (&optional note-or-id)
"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))
(origin (funcall-interactively vino-origin-select-fn)))
(vulpea-utils-with-note note
(--each origin
(vulpea-buffer-meta-set (car it) (cdr it)))
(vulpea-buffer-meta-sort vino-entry-meta-props-order)
(save-buffer))))
;;;###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';
- title of the `vino-entry';
- description of the linked producer and ratings;
- title of all linked ratings."
(interactive)
(let* ((note (vino-entry-note-get-dwim note-or-id))
(producer (vulpea-note-meta-get note "producer" 'note))
(title (format
"%s %s %s"
(vulpea-note-title producer)
(vulpea-note-meta-get note "name")
(or (vulpea-note-meta-get note "vintage") "NV")))
;; update linked ratings
rating-title rating
(ratings (->>
(vulpea-note-meta-get-list note "ratings" 'note)
(--map
(vulpea-utils-with-note it
;; set title
(setq rating-title (format "%s - %s" title (vulpea-note-meta-get it "date")))
(vulpea-buffer-title-set rating-title)
(vulpea-buffer-meta-set "wine" (vulpea-utils-link-make-string note title))
;; update rating
(setq rating (vino-rating-get-by-id it))
(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)
;; save buffer
(save-buffer)
;; return note id, so we can reload latest state from db;
;; we could optimize title, but meta is a little bit more messy
(vulpea-note-id it)))
(vulpea-db-query-by-ids)
(-remove #'vulpea-note-primary-title)
(seq-sort-by #'vulpea-note-title #'string<)))
;; calculate rating
(values (--map (vulpea-meta-get it "total" 'number) ratings))
(rating (if (functionp vino-entry-rating-average-method)
(funcall vino-entry-rating-average-method values)
(when values
(pcase vino-entry-rating-average-method
(`amean (/ (apply #'+ values)
(float (length values))))
(`min (-min values))
(`max (-max values))
(`oldest (car values))
(`latest (car (reverse values)))
(_ (user-error "Unexpected value of `vino-entry-rating-average-method': %S"
vino-entry-rating-average-method))))))
(rating (if rating (vino-rating--round rating) "NA")))
;; update wine entry note
(vulpea-utils-with-note note
(vulpea-buffer-title-set title)
(vulpea-buffer-meta-set "producer" producer)
(vulpea-buffer-meta-set "ratings" ratings)
(vulpea-buffer-meta-set "rating" rating 'append)
(vulpea-buffer-meta-sort vino-entry-meta-props-order)
(save-buffer))
;; run hook
(run-hook-with-args 'vino-entry-update-handle-functions note)))
;;;###autoload
(defun vino-entry-rate (&optional note-or-id date extra-data)
"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.
EXTRA-DATA is passed to `vino-rating-create-handle-functions'."
(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)))
(vino-rating--create
(make-vino-rating
:wine note
:date (format-time-string "%Y-%m-%d" date)
:version version
:values values)
nil
extra-data)))
;;; 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")
(seq-contains-p tags "cellar"))))
;;;###autoload
(defun vino-entry-note-select (&optional initial-prompt)
"Select and return a `vulpea-note' representing `vino-entry'.
Optionally provide INITIAL-PROMPT."
(vulpea-select-from
"Wine"
(vulpea-db-query-by-tags-every '("wine" "cellar"))
:require-match t
:initial-prompt initial-prompt))
;;;###autoload
(defun vino-entry-note-get-dwim (&optional note-or-id)
"Get a `vulpea-note' representing `vino-entry' in a dwim style.
If NOTE-OR-ID is an ID, then get a note by that ID. Throws error
if extracted note does not represent a `vino-entry'.
If NOTE-OR-ID is a note, then return it. Throws error
if extracted note does not represent a `vino-entry'.
If NOTE-OR-ID is nil, try to extract a note from current buffer
or ask for user to select a note. Extractions happens from
`vino-entry' buffer or from `vino-rating' buffer. In the latter
case, linked `vino-entry' is extracted."
(cond
((null note-or-id)
(if (eq major-mode 'org-mode)
(let* ((id (save-excursion
(goto-char (point-min))
(org-id-get)))
(note (vulpea-db-get-by-id id)))
(cond
((vino-entry-note-p note) note)
((vino-rating-note-p note) (vulpea-note-meta-get note "wine" 'note))
(t (vino-entry-note-select))))
(vino-entry-note-select)))
((stringp note-or-id)
(let ((note (vulpea-db-get-by-id note-or-id)))
(if (and note (vino-entry-note-p note))
note
(user-error
"Note with id %s does not represent a vino entry"
note-or-id))))
((vulpea-note-p note-or-id)
(if (vino-entry-note-p note-or-id)
note-or-id
(user-error
"Note %s does not represent a vino entry"
note-or-id)))
(t
(user-error "Unsupported type of %s" note-or-id))))
;;; Country, region and appellation
;;;###autoload
(defvar vino-country-template
'(:file-name "wine/country/%<%Y%m%d%H%M%S>-${slug}.org"
:tags ("wine" "country"))
"Capture template for region 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
:meta (optional) - metadata (associative list)
: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 country
See `vulpea-create' for more information.")
;;;###autoload
(defvar vino-region-template
'(:file-name "wine/region/${country}/%<%Y%m%d%H%M%S>-${slug}.org"
:tags ("wine" "region"))
"Capture template for region 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
:meta (optional) - metadata (associative list)
: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 region
See `vulpea-create' for more information.")
;;;###autoload
(defvar vino-appellation-template
'(:file-name "wine/appellation/${country}/%<%Y%m%d%H%M%S>-${slug}.org"
:tags ("wine" "appellation"))
"Capture template for appellation 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
:meta (optional) - metadata (associative list)
: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
appellation
See `vulpea-create' for more information.")
;;;###autoload
(cl-defun vino-country-create (&key title)
"Create a country note using `vino-country-template'.
Unless TITLE is specified, user is prompted to provide one.
Return `vulpea-note'."
(interactive)
(let ((title (or title (read-string "Country: "))))
(vulpea-create
title
(plist-get vino-country-template :file-name)
:tags (seq-union (plist-get vino-country-template :tags)
'("wine" "country"))
:head (plist-get vino-country-template :head)
:meta (plist-get vino-country-template :meta)
:body (plist-get vino-country-template :body)
:context (plist-get vino-country-template :context)
:properties (plist-get vino-country-template :properties)
:unnarrowed t
:immediate-finish t)))
;;;###autoload
(cl-defun vino-region-create (&key title country parent capture-properties)
"Create a region note using `vino-region-template'.
Unless TITLE is provided, user is prompted to provide one.