-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorg-supertag-field.el
908 lines (833 loc) · 35.2 KB
/
org-supertag-field.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
;;; org-supertag-field.el --- Field system for org-supertag -*- lexical-binding: t; -*-
;;; Commentary:
;; Core functionality for field system:
;; - Field type definitions
;; - Field value access
;; - Org integration
;; - Field synchronization
;;; Code:
(require 'cl-lib)
(require 'org)
(require 'org-element)
(require 'org-supertag-db)
;;------------------------------------------------------------------------------
;; Field Value Database Operations
;;------------------------------------------------------------------------------
(defun org-supertag-field-db-set-value (node-id field-name value tag-id)
"Set field value to database.
NODE-ID: Node identifier
FIELD-NAME: Field name
VALUE: Field value
TAG-ID: Associated tag identifier"
(let ((link-id (format ":node-field:%s->%s" node-id field-name))
(props (list :from node-id
:to field-name
:tag-id tag-id
:value value
:created-at (current-time))))
(puthash link-id props org-supertag-db--link)))
(defun org-supertag-field-db-get-value (node-id field-name tag-id)
"Get field value from database.
NODE-ID: Node identifier
FIELD-NAME: Field name
TAG-ID: Associated tag identifier"
(let* ((link-id (format ":node-field:%s->%s" node-id field-name))
(link (gethash link-id org-supertag-db--link)))
(when (and link
(equal (plist-get link :tag-id) tag-id))
(plist-get link :value))))
(defun org-supertag-field-db-delete-value (node-id field-name tag-id)
"Delete field value from database.
NODE-ID: Node identifier
FIELD-NAME: Field name
TAG-ID: Associated tag identifier"
(let ((link-id (format ":node-field:%s->%s" node-id field-name)))
(remhash link-id org-supertag-db--link)))
;;----------------------------------------------------------------------
;; Field Value Entity Operations
;;----------------------------------------------------------------------
(defun org-supertag-field-set-value (field-def value node-id tag-id)
"Set field value for node.
FIELD-DEF is the field definition
VALUE is the field value
NODE-ID is the target node ID
TAG-ID is the tag ID"
(condition-case err
(let* ((field-name (plist-get field-def :name))
(field-type (plist-get field-def :type))
(type-def (org-supertag-get-field-type field-type))
;; Ensure value is not nil
(processed-value (if type-def
(progn
(org-supertag-field-validate field-def value)
(let* ((formatter (plist-get type-def :formatter)))
(if formatter
(funcall formatter value field-def)
value)))
value)))
;; Store value
(org-supertag-field-db-set-value node-id field-name processed-value tag-id)
;; Sync to org buffer
(when-let ((pos (condition-case nil
(org-id-find node-id t)
(error nil))))
(condition-case sync-err
(org-with-point-at pos
;; Special handling for tag-reference type
(if (eq field-type 'tag-reference)
(when-let* ((ref-result (org-supertag-field-get-reference-value node-id tag-id field-def))
(ref-field (plist-get ref-result :field))
(ref-value (plist-get ref-result :value)))
;; Set the property with referenced value
(org-set-property field-name ref-value))
;; Normal handling for other types
(org-set-property field-name processed-value)))
(error
(message "Failed to sync field value to org buffer: %s"
(error-message-string sync-err))))))
(error
(message "Error in field set-value operation: %s"
(error-message-string err))
(signal (car err) (cdr err)))))
(defun org-supertag-field-validate (field value)
"Validate field value.
FIELD: Field definition
VALUE: Value to validate
Returns t if valid, otherwise throws error"
(let* ((name (plist-get field :name))
(type (plist-get field :type))
(required (plist-get field :required))
(type-def (org-supertag-get-field-type type))
(validator (plist-get type-def :validator)))
;; Debug output
(message "Debug - Validating field: name=%S, type=%S, value=%S" name type value)
;; Check required field
(when (and required (null value))
(error "Field '%s' is required" name))
;; Allow nil for non-required fields
(if (null value)
t
;; Type-specific validation
(progn
;; Basic type check for strings
(when (and (eq type 'string) (not (stringp value)))
(error "Field '%s' requires string type, got %S" name (type-of value)))
;; Call type-specific validator if exists
(when validator
(condition-case err
(if (not (funcall validator value))
(error "Value '%s' does not meet validation requirements" value))
(error
(error "Error validating field '%s': %s" name (error-message-string err)))))
t))))
(defun org-supertag-field-get-value (field-def node-id tag-id)
"Get field value.
FIELD-DEF: field definition
NODE-ID: node ID
TAG-ID: tag ID"
(let* ((field-name (plist-get field-def :name))
(type (plist-get field-def :type))
(type-spec (org-supertag-get-field-type type))
(value (org-supertag-field-db-get-value node-id field-name tag-id)))
(when (and value
(plist-get type-spec :formatter))
(setq value (funcall (plist-get type-spec :formatter) value field-def)))
value))
(defun org-supertag-field-remove-value (field-def node-id tag-id)
"Remove field value.
FIELD-DEF: field definition
NODE-ID: node ID
TAG-ID: tag ID"
(let ((field-name (plist-get field-def :name)))
(org-supertag-db-remove-link :node-field node-id field-name
`(:tag-id ,tag-id))
(org-with-point-at (org-id-find node-id t)
(org-entry-delete nil field-name))))
(defun org-supertag-field-batch-set-value (field-def-list value-list node-id tag-id)
"Set multiple field values in batch.
FIELD-DEF-LIST: List of field definitions to set
VALUE-LIST: List of values corresponding to the fields
NODE-ID: ID of the target node
TAG-ID: ID of the associated tag"
(cl-loop for field-def in field-def-list
for value in value-list
do (org-supertag-field-set-value field-def value node-id tag-id)))
(defun org-supertag-field-find-nodes (field-name value &optional tag-id)
"Find nodes that have a specific field value.
FIELD-NAME: Name of the field to search
VALUE: Value to match against
TAG-ID: Optional tag ID to limit search scope
Returns a list of nodes that match the criteria."
(let ((links (org-supertag-db-get-links :node-field)))
(cl-remove-if-not
(lambda (link)
(and (equal (car link) field-name) ; Match field name
(equal (plist-get (cadr link) :value) value) ; Match value
(or (null tag-id) ; If no tag ID specified
(equal (plist-get (cadr link) :tag-id) tag-id)))) ; Match tag ID
links)))
;;----------------------------------------------------------------------
;; Field Type System
;;----------------------------------------------------------------------
(defvar org-supertag-field-types
'((string . (:validator org-supertag-validate-string
:formatter org-supertag-format-string
:reader org-supertag-read-string-field
:description "Plain text"))
(options . (:validator org-supertag-validate-options
:formatter org-supertag-format-options
:reader org-supertag-read-options-field
:description "Options"))
(number . (:validator org-supertag-validate-number
:formatter org-supertag-format-number
:reader org-supertag-read-number-field
:description "Number"))
(date . (:validator org-supertag-validate-date
:formatter org-supertag-format-date
:reader org-supertag-read-date-field
:description "Date"))
(timestamp . (:validator org-supertag-validate-timestamp
:formatter org-supertag-format-timestamp
:reader org-supertag-read-timestamp-field
:description "Timestamp"))
(tag-reference . (:validator org-supertag-validate-tag-reference
:reader org-supertag-read-tag-reference-field
:description "Tag Reference"))
(list . (:validator org-supertag-validate-list
:formatter org-supertag-format-list
:reader org-supertag-read-list-field
:description "List"))
(range . (:validator org-supertag-validate-range
:formatter org-supertag-format-range
:reader org-supertag-read-range-field
:description "Number Range"))
(behavior . (:validator org-supertag-validate-behavior
:formatter org-supertag-format-behavior
:reader org-supertag-read-behavior-field
:description "Tag Behavior")))
"Field type definition.
Each type is a cons cell, car is the type name (symbol), cdr is the type definition plist, containing:
- :validator Function to validate field value
- :formatter Function to format field value
- :reader Function to read field value
- :description Description of the field type")
(defun org-supertag-get-field-types ()
"Get all supported field types.
Return a list of ((display-name . type-symbol) ...)."
(mapcar (lambda (type-def)
(let* ((type-name (car type-def))
(type-spec (cdr type-def))
(description (plist-get type-spec :description)))
(cons (or description (symbol-name type-name))
type-name)))
org-supertag-field-types))
(defun org-supertag-get-field-type (type)
"Get field type definition.
TYPE is the type symbol.
Return the type definition plist, containing :validator, :formatter, :reader and :description."
(message "Debug - Getting field type: %S" type)
(let ((type-def (alist-get type org-supertag-field-types)))
(message "Debug - Field type definition: %S" type-def)
type-def))
;;----------------------------------------------------------------------
;; Field Value Validators and Formatters
;;----------------------------------------------------------------------
(defun org-supertag-validate-string (value)
"Validate string value.
VALUE: Value to validate.
Return t if VALUE is nil or a non-empty string."
(or (null value) ; Allow nil value
(and (stringp value) ; Must be string if not nil
(not (string-empty-p (string-trim value))))))
(defun org-supertag-format-string (value field)
"Format string VALUE.
VALUE is the value to format.
FIELD is the field definition."
(message "Debug - Formatting string value: %S" value)
(when value
(string-trim value)))
(defun org-supertag-validate-date (value)
"Validate date VALUE.
VALUE can be either:
- An org timestamp string (e.g. \"[2025-01-14 Tue]\" or \"<2025-01-14 Tue>\")
- A time value from org-read-date
- A standard date string (e.g. \"2025-01-14\")"
(message "Debug - Validating date value: %S" value)
(condition-case err
(cond
;; Handle org timestamp string
((and (stringp value)
(string-match org-ts-regexp0 value))
t)
;; Handle time value from org-read-date
((listp value)
t)
;; Handle standard date string
((stringp value)
(org-parse-time-string value)
t)
(t nil))
(error
(message "Date validation error: %S" err)
nil)))
(defun org-supertag-format-date (value &optional _field)
"Format date value to org active timestamp format.
VALUE can be either:
- An org timestamp string (e.g. \"[2025-01-14 Tue]\" or \"<2025-01-14 Tue>\")
- A time value from org-read-date
- A standard date string (e.g. \"2025-01-14\")"
(message "Debug - Formatting date value: %S" value)
(condition-case err
(cond
;; Already in org timestamp format - convert to active if needed
((and (stringp value)
(string-match org-ts-regexp0 value))
(if (string-prefix-p "[" value)
(concat "<" (substring value 1 -1) ">")
value))
;; Handle time value from org-read-date
((listp value)
(format-time-string "<%Y-%m-%d %a>" value))
;; Handle standard date string
((stringp value)
(let ((time (org-parse-time-string value)))
(format-time-string "<%Y-%m-%d %a>"
(apply #'encode-time time time))))
(t value))
(error
(message "Date formatting error: %S" err)
value)))
(defun org-supertag-validate-timestamp (value &optional _field-def)
"Validate timestamp value.
VALUE can be either:
- An org timestamp string (e.g. \"[2025-01-14 Tue 10:30]\" or \"<2025-01-14 Tue 10:30>\")
- A time value from org-read-date
- A standard timestamp string (e.g. \"2025-01-14 10:30\")"
(condition-case err
(cond
;; Handle org timestamp string
((and (stringp value)
(string-match org-ts-regexp value))
t)
;; Handle time value from org-read-date
((listp value)
t)
;; Handle standard timestamp string
((stringp value)
(org-parse-time-string value)
t)
(t nil))
(error nil)))
(defun org-supertag-format-timestamp (value field-def)
"Format timestamp value to org inactive timestamp format.
VALUE can be either:
- An org timestamp string (e.g. \"[2025-01-14 Tue 10:30]\" or \"<2025-01-14 Tue 10:30>\")
- A time value from org-read-date
- A standard timestamp string (e.g. \"2025-01-14 10:30\")"
(when value
(condition-case err
(cond
;; Already in org timestamp format - convert to inactive if needed
((and (stringp value)
(string-match org-ts-regexp value))
(if (string-prefix-p "<" value)
(concat "[" (substring value 1 -1) "]")
value))
;; Handle time value from org-read-date
((listp value)
(format-time-string "[%Y-%m-%d %a %H:%M]" value))
;; Handle standard timestamp string
((stringp value)
(let ((time (org-parse-time-string value)))
(format-time-string "[%Y-%m-%d %a %H:%M]"
(apply #'encode-time time))))
(t value))
(error value))))
(defun org-supertag-validate-email (value)
"Validate email VALUE."
(and (stringp value)
(string-match-p "^[^@]+@[^@]+\\.[^@]+$" value)))
(defun org-supertag-format-email (value)
"Format email VALUE."
(string-trim value))
(defun org-supertag-validate-url (value)
"Validate URL VALUE."
(and (stringp value)
(string-match-p "^https?://" value)))
(defun org-supertag-format-url (value)
"Format URL VALUE."
(string-trim value))
(defun org-supertag-validate-tag-reference (value)
"Validate tag reference value.
VALUE should be a plist with :tag-id and :field."
(when value
(let ((tag-id (plist-get value :tag-id))
(field-name (plist-get value :field)))
(and (stringp tag-id)
(org-supertag-tag-exists-p tag-id)
(stringp field-name)))))
(defun org-supertag-read-tag-reference-field (prompt)
"Read tag reference field value.
PROMPT is the prompt message."
(let* ((tags (org-supertag-get-all-tags))
(tag-id (completing-read "Select tag to reference: "
tags
nil t))
(tag (org-supertag-tag-get tag-id))
(available-fields (mapcar (lambda (field)
(plist-get field :name))
(plist-get tag :fields)))
(field-name (completing-read
"Select field to reference: "
available-fields
nil t)))
(list :tag-id tag-id :field field-name)))
(defun org-supertag-field-get-reference-value (node-id tag-id field)
"Get referenced field value and type.
NODE-ID: Current node ID
TAG-ID: Current tag ID
FIELD: Field definition containing tag reference"
(when-let* ((ref-value (org-supertag-field-get-value field node-id tag-id))
(ref-tag-id (plist-get ref-value :tag-id))
(ref-field-name (plist-get ref-value :field))
(ref-tag (org-supertag-tag-get ref-tag-id))
;; Get complete field definition
(ref-field (cl-find ref-field-name
(plist-get ref-tag :fields)
:key (lambda (f)
(plist-get f :name))
:test #'equal)))
;; Return both field definition and value
(list :field ref-field
:value (org-supertag-field-get-value ref-field node-id ref-tag-id))))
(defun org-supertag-field-inherit-values (node-id tag-id field)
"Inherit field values from referenced tag.
NODE-ID is the current node ID
TAG-ID is the current tag ID
FIELD is the field definition containing tag reference"
(when-let* ((value (org-supertag-field-get-value field node-id tag-id))
(ref-tag-id (plist-get value :tag-id))
(ref-fields (plist-get value :fields))
(ref-tag (org-supertag-tag-get ref-tag-id)))
(dolist (field-name ref-fields)
(when-let* ((ref-field (cl-find field-name
(plist-get ref-tag :fields)
:key (lambda (f)
(plist-get f :name))
:test #'equal))
(ref-value (org-supertag-field-get-value
ref-field node-id ref-tag-id)))
;; Inherit the field value
(org-supertag-field-set-value
(list :name field-name
:type (plist-get ref-field :type))
ref-value
node-id
tag-id)))))
(defun org-supertag-validate-options (value &optional field-def)
"Validate options value.
VALUE is the value to validate.
FIELD-DEF is the optional field definition containing :options."
(let ((options (when field-def (plist-get field-def :options))))
(message "Debug - Validating options: value=%S, options=%S" value options)
(cond
((null options)
(stringp value))
(t (and (stringp value)
(member value options))))))
(defun org-supertag-format-options (value field)
"Format options value.
VALUE is the value to format.
FIELD is the field definition.
Returns a trimmed string if VALUE is a string, otherwise returns nil."
(message "Debug - Formatting options value: %S" value)
(when (and value (stringp value)) ; Only process if value is a non-nil string
(string-trim value)))
(defun org-supertag-validate-number (value)
"Validate numeric value.
VALUE can be a number or numeric string."
(or (numberp value)
(and (stringp value)
(string-match-p "^[0-9.]+$" value))))
(defun org-supertag-format-number (value &optional _field)
"Format numeric value.
VALUE can be a number or numeric string.
FIELD is the field definition."
(message "Debug - Formatting number value: %S" value)
(when value
(if (numberp value)
(number-to-string value)
(if (string-match-p "^[0-9.]+$" value)
value
(number-to-string (string-to-number value))))))
(defun org-supertag-validate-list (value)
"Validate list value.
VALUE is the value to validate."
(message "Debug - Validating list value: %S" value)
(or (listp value)
(and (stringp value)
(string-match-p "^\\[.*\\]$" value))))
(defun org-supertag-format-list (value &optional _field)
"Format list value.
VALUE is the value to format.
FIELD is the field definition."
(message "Debug - Formatting list value: %S" value)
(cond
((null value) "[]")
((listp value)
(format "[%s]"
(mapconcat (lambda (item)
(format "\"%s\"" item))
value ",")))
((stringp value)
(if (string-match-p "^\\[.*\\]$" value)
(let ((json-array-type 'list)
(json-false nil))
(condition-case err
(let ((parsed (json-read-from-string value)))
(org-supertag-format-list parsed))
(error value))) ; If parsing fails, return original
(format "[%s]" value)))
(t (format "[%s]" value))))
(defun org-supertag-read-list-field (prompt)
"Read list field value.
PROMPT is the prompt message."
(message "Debug - Reading list field with prompt: %s" prompt)
(let* ((input (read-string (format "%s (comma separated): " prompt)))
(values (split-string input "," t "[ \t\n\r]+")))
(message "Debug - List field input: %S -> %S" input values)
values))
(defun org-supertag-validate-range (value)
"Validate range value.
VALUE should be in 'min-max' format."
(when value ; Allow nil value
(condition-case nil
(let* ((parts (split-string value "-"))
(min (string-to-number (car parts)))
(max (string-to-number (cadr parts))))
(and (= (length parts) 2) ; Must have two parts
(numberp min) ; Min must be number
(numberp max) ; Max must be number
(< min max))) ; Min must be less than max
(error nil))))
(defun org-supertag-format-range (value field-def)
"Format range value.
VALUE is the range value.
FIELD-DEF is the field definition."
value) ; Return value as-is
(defun org-supertag-read-range-field (field-def)
"Read range value.
FIELD-DEF is the field definition."
(let ((value (read-string
(format "Enter range (format: min-max): "))))
(if (org-supertag-validate-range value)
value
(error "Invalid range format, use 'min-max' format like '1-10'"))))
;;---------------------------------------------------------------------------
;; Get Field Initial Value
;;---------------------------------------------------------------------------
(defun org-supertag-field-get-initial-value (field)
"Get initial value for FIELD."
(let ((field-type (plist-get field :type))
(field-value (plist-get field :value))
(field-options (plist-get field :options)))
(message "Getting initial value for field: %S" field)
(let ((initial-value
(cond
((eq field-type 'behavior)
field-value)
((and (eq field-type 'options) field-options)
(car field-options))
((eq field-type 'date)
(format-time-string "%Y-%m-%d"))
((eq field-type 'string)
" ")
(t nil))))
(message "Initial value for field %s (%s): %S"
(plist-get field :name)
field-type
initial-value)
initial-value)))
;;------------------------------------------------------------------------------
;; Behavior Field Type
;;------------------------------------------------------------------------------
(defun org-supertag-validate-behavior (value)
"Validate behavior field value.
VALUE should be a plist with :trigger and either :action or :style."
(and (plistp value)
(plist-member value :trigger)
(memq (plist-get value :trigger)
'(:on-add :on-remove :on-change :on-schedule :always))
(or (functionp (plist-get value :action))
(plistp (plist-get value :style)))))
(defun org-supertag-format-behavior (value &optional _field)
"Format behavior field value for display.
VALUE can be either:
- A string (behavior name)
- A behavior plist
Format example:
- Behavior[@important]
- Behavior[:on-add +action +style:📦]
- Behavior[:always +action]"
(cond
;; String: behavior name
((stringp value)
(if-let ((behavior (gethash value org-supertag-behavior-registry)))
;; If behavior definition found, use complete format
(let ((trigger (plist-get behavior :trigger))
(has-action (plist-get behavior :action))
(style (plist-get behavior :style)))
(format "Behavior[%s%s%s]"
(or trigger "nil")
(if has-action " +action" "")
(if style
(format " +style:%s"
(or (plist-get style :prefix) ""))
"")))
;; If behavior definition not found, only show behavior name
(format "Behavior[%s]" value)))
;; plist: complete behavior definition
((and (listp value) (keywordp (car value)))
(let ((trigger (plist-get value :trigger))
(has-action (plist-get value :action))
(style (plist-get value :style)))
(format "Behavior[%s%s%s]"
(or trigger "nil")
(if has-action " +action" "")
(if style
(format " +style:%s"
(or (plist-get style :prefix) ""))
""))))
;; Other cases
(t "Behavior[nil]")))
(defun org-supertag-read-behavior-field (prompt &optional initial)
"Read behavior field value.
PROMPT is the prompt string
INITIAL is the initial value."
(let* ((trigger (intern
(completing-read
"Trigger: "
'("on-add" "on-remove" "on-change" "on-schedule" "always")
nil t nil nil "on-add")))
(has-action (y-or-n-p "Add action? "))
(action (when has-action
(read-from-minibuffer "Action (lambda form): "
"(lambda (node-id)\n )")))
(has-style (y-or-n-p "Add style? "))
(style (when has-style
(list :face (read-from-minibuffer "Face properties: "
"(:foreground \"blue\")")
:prefix (read-string "Prefix: " "📋")))))
(list :trigger trigger
:action (when has-action (eval (read action)))
:style style)))
;;----------------------------------------------------------------------
;; Read Field Value
;;----------------------------------------------------------------------
(defun org-supertag-field-read-value (field)
"Read field value.
FIELD is the field definition."
(let* ((name (plist-get field :name))
(type (plist-get field :type))
(type-def (org-supertag-get-field-type type))
(reader (plist-get type-def :reader))
(formatter (plist-get type-def :formatter))
(required (plist-get field :required))
(options (plist-get field :options)))
;; 1. Ensure reader exists
(unless reader
(error "Field type %s has no reader function" type))
;; 2. For options type, ensure options exist
(when (and (eq type 'options) (not options))
(let* ((options-input (read-string (format "%s options (comma separated): " name)))
(options-values (split-string options-input "," t "[ \t\n\r]+")))
(setq options options-values)
(setq field (plist-put field :options options-values))))
;; 3. Read value
(catch 'done
(while t
(condition-case err
(let* ((input-value
(if (eq type 'options)
(funcall reader name options)
(funcall reader name)))
(typed-value (org-supertag-field--convert-value type input-value)))
;; 4. Validate and format
(if (org-supertag-field-validate field typed-value)
(throw 'done
(if formatter
(funcall formatter typed-value field)
typed-value))
(when (or required
(y-or-n-p (format "Field %s validation failed. Retry? " name)))
(sit-for 1))))
(error
(let ((err-msg (error-message-string err)))
(message "Error - Processing field %s: %s" name err-msg)
(when (or required
(y-or-n-p (format "Error processing field %s. Retry? " name)))
(sit-for 1)))))))))
(defun org-supertag-field--convert-value (type value)
"Convert value to specified type.
TYPE: Target type.
VALUE: Value to convert."
(let ((type-spec (org-supertag-get-field-type type)))
(when-let ((formatter (plist-get type-spec :formatter)))
(funcall formatter value nil))))
(defun org-supertag-read-string-field (prompt)
"Read string field value.
PROMPT is the prompt message."
(message "Debug - Reading string field with prompt: %s" prompt)
(let* ((raw-input (read-string (format "%s: " prompt)))
(trimmed-input (string-trim raw-input))
(result (if (string-empty-p trimmed-input)
nil
trimmed-input)))
(message "Debug - String field: raw-input=%S, trimmed=%S, result=%S"
raw-input trimmed-input result)
result))
(defun org-supertag-read-date-field (prompt &optional default)
"Read date field value using org-mode's date reader.
PROMPT is the prompt message.
DEFAULT is the default value."
(let* ((input (org-read-date nil t nil prompt nil default))
(formatted-date (format-time-string "<%Y-%m-%d %a>" input)))
(message "Debug - Date input from org-read-date: %S -> %S" input formatted-date)
formatted-date))
(defun org-supertag-read-timestamp-field (prompt)
"Read timestamp field value using org-mode's date reader.
PROMPT is the prompt message."
(let* ((time (org-read-date t t)) ; Use org-mode time reader with time
(formatted-ts (format-time-string "[%Y-%m-%d %a %H:%M]" time)))
formatted-ts))
(defun org-supertag-read-email-field (prompt &optional default)
"Read email field value.
PROMPT is the prompt message.
DEFAULT is the default value."
(let ((input (read-string (format "%s ([email protected])%s: "
prompt
(if default
(format " (default: %s)" default)
""))
nil nil default)))
(if (org-supertag-validate-email input)
input
(progn
(message "Invalid email address, please try again")
(sit-for 1)
(org-supertag-read-email-field prompt default)))))
(defun org-supertag-read-url-field (prompt &optional default)
"Read URL field value.
PROMPT is the prompt message
DEFAULT is the default value"
(let ((input (read-string (format "%s (https://example.com)%s: "
prompt
(if default
(format " (default: %s)" default)
""))
nil nil default)))
(if (org-supertag-validate-url input)
input
(progn
(message "Invalid URL, please try again")
(sit-for 1)
(org-supertag-read-url-field prompt default)))))
(defun org-supertag-read-options-field (prompt options)
"Read options field value.
PROMPT is the prompt message
OPTIONS is the list of available options."
(unless options
(error "Options type field requires predefined options"))
(let ((input (completing-read (format "%s (%s): "
prompt
(mapconcat #'identity options "/"))
options
nil t)))
(if (member input options)
input
(progn
(message "Please select from the given options")
(sit-for 1)
(org-supertag-read-options-field prompt options)))))
(defun org-supertag-read-number-field (prompt)
"Read numeric field value.
PROMPT is the prompt message"
(let ((input-str (read-string (format "%s: " prompt))))
;; Validate input contains only digits and decimal point
(if (string-match-p "^[0-9.]+$" input-str)
input-str ; Return number as string
(progn
(message "Please enter a valid number")
(sit-for 1)
(org-supertag-read-number-field prompt)))))
;;----------------------------------------------------------------------
;; Field Modification
;;----------------------------------------------------------------------
(defun org-supertag-tag-modify-field (tag-id field-name)
"Modify field definition for TAG-ID's FIELD-NAME.
This will clear all existing values of this field across all nodes."
(interactive
(let* ((node-id (org-id-get))
(tags (org-supertag-node-get-tags node-id))
(tag-id (completing-read "Select tag: " tags nil t))
(tag (org-supertag-tag-get tag-id))
(fields (plist-get tag :fields))
(field-name (completing-read
"Select field to modify: "
(mapcar (lambda (field)
(plist-get field :name))
fields)
nil t)))
(list tag-id field-name)))
(let* ((tag (org-supertag-tag-get tag-id))
(fields (plist-get tag :fields))
(field (cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal))
(current-type (plist-get field :type))
;; Get new type with completion
(type-choices
(mapcar (lambda (type-def)
(let ((type-name (car type-def))
(type-spec (cdr type-def)))
(format "%s - %s"
(symbol-name type-name)
(plist-get type-spec :description))))
org-supertag-field-types))
(new-type-choice
(completing-read
(format "Current type is %s. Select new type: " current-type)
type-choices
nil t nil nil
(symbol-name current-type)))
(new-type (intern (car (split-string new-type-choice " - "))))
;; Get new name with completion and current as default
(new-name (read-string
(format "Field name (current: %s): " field-name)
field-name)))
;; Confirm with user about value clearing
(when (yes-or-no-p
(format "Modifying field will clear all existing values. Continue? "))
;; Create new field definition
(let* ((new-field (list :name new-name
:type new-type))
;; Update fields list
(new-fields (mapcar
(lambda (f)
(if (equal (plist-get f :name) field-name)
new-field
f))
fields))
;; Update tag
(new-tag (plist-put (copy-sequence tag)
:fields new-fields)))
;; Update tag in database
(org-supertag-db-add tag-id new-tag)
;; Clear field values from all nodes
(let ((nodes (org-supertag-tag-get-nodes tag-id)))
(dolist (node-id nodes)
(org-supertag-field-remove-value field node-id tag-id)))
(message "Field '%s' modified. You may now set new values."
new-name)))))
(provide 'org-supertag-field)
;;; org-supertag-field.el ends here