-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorg-supertag-tag.el
1250 lines (1149 loc) · 50.1 KB
/
org-supertag-tag.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
;;; org-supertag-tag.el --- Tag relation management for org-supertag -*- lexical-binding: t; -*-
;;; Commentary:
;; Provides tag relationship management functionality
;; Core principle: Connect entities through relationships using type, from, and to
;;----------------------------------------------------------------------
;; Tag Name Operation
;;----------------------------------------------------------------------
(defun org-supertag-sanitize-tag-name (name)
"Convert a name into a valid tag name.
NAME is the name to convert
- Remove leading '#' characters
- Convert spaces to underscores
- Validate non-empty"
(if (or (null name) (string-empty-p name))
(error "Tag name cannot be empty")
(let* ((trimmed (string-trim name))
(without-hash (replace-regexp-in-string "^#+" "" trimmed))
(sanitized (replace-regexp-in-string "\\s-+" "_" without-hash)))
(message "Debug sanitize-tag-name: input=%s output=%s" name sanitized)
(if (string-empty-p sanitized)
(error "Invalid tag name: %s" name)
sanitized))))
(defun org-supertag-tag-exists-p (tag-name)
"Check if a tag exists.
TAG-NAME is the name of the tag to check"
(let ((sanitized-name (org-supertag-sanitize-tag-name tag-name)))
(org-supertag-tag-get sanitized-name)))
;;----------------------------------------------------------------------
;; Tag Base Operation
;;----------------------------------------------------------------------
(defun org-supertag-tag-create (tag-name &rest props)
"Create a new tag.
TAG-NAME: Name of the tag
PROPS: Additional properties"
(let* ((sanitized-name (org-supertag-sanitize-tag-name tag-name))
(fields (or (plist-get props :fields) '()))
(base-props (list :type :tag
:id sanitized-name
:name sanitized-name
:fields fields
:behaviors (plist-get props :behaviors)
:created-at (current-time))))
(message "Creating tag with fields: %S" fields)
(org-supertag-db-add sanitized-name base-props)
(let ((saved-tag (org-supertag-db-get sanitized-name)))
(message "Saved tag: %S" saved-tag)
(unless saved-tag
(error "Failed to create tag: %s" sanitized-name)))
sanitized-name))
(defun org-supertag-tag-get (tag-name)
"Get tag definition.
TAG-NAME is the name of the tag to retrieve.
Returns the tag entity if found and is a valid tag type,
otherwise returns nil."
(let ((entity (org-supertag-db-get tag-name)))
(when (and entity
(eq (plist-get entity :type) :tag))
entity)))
(defun org-supertag-get-all-tags ()
"Get a list of all defined tags."
(let ((tags '()))
(maphash
(lambda (id entity)
(when (eq (plist-get entity :type) :tag)
(push (plist-get entity :name) tags)))
org-supertag-db--object)
(delete-dups tags)))
;;----------------------------------------------------------------------
;; Tag-Node Relation Operation
;;----------------------------------------------------------------------
(defun org-supertag-tag-apply (tag-id)
"Apply tag to the node at current position."
(let* ((tag (org-supertag-db-get tag-id))
(node-id (org-id-get-create)))
(message "Applying tag: %s to node: %s" tag-id node-id)
;; Validation
(unless tag
(error "Tag %s not found" tag-id))
(unless (eq (plist-get tag :type) :tag)
(error "Invalid tag type for %s" tag-id))
(unless (org-supertag-db-get node-id)
(org-supertag--create-node node-id))
;; Link node and tag
(org-supertag-node-db-add-tag node-id tag-id)
;; Apply fields
(when-let ((fields (plist-get tag :fields)))
(message "Processing fields for tag %s: %S" tag-id fields)
(dolist (field fields)
(let* ((field-name (plist-get field :name))
(field-type (plist-get field :type))
(type-def (org-supertag-get-field-type field-type))
(initial-value (org-supertag-field-get-initial-value field)))
(message "Processing field: name=%s type=%s initial=%S"
field-name field-type initial-value)
(unless type-def
(error "Invalid field type: %s" field-type))
(when-let* ((formatter (plist-get type-def :formatter))
(formatted-value (if formatter
(funcall formatter initial-value field)
(format "%s" initial-value))))
(message "Setting field %s = %s" field-name formatted-value)
(org-set-property field-name formatted-value)
(org-supertag-tag--set-field-value
tag-id node-id field-name initial-value)))))
;; Add tag to node tags
(let ((tags (org-get-tags)))
(org-set-tags (cons (concat "#" tag-id) tags)))
;; Behavior Active
(run-hook-with-args 'org-supertag-after-tag-apply-hook node-id)
(org-supertag-behavior--on-tag-change node-id tag-id :add)
(org-supertag-behavior--apply-styles node-id)
node-id))
(defun org-supertag-tag--remove (tag-id node-id)
"Remove a tag from a node.
TAG-ID: The tag identifier
NODE-ID: The node identifier
This function:
1. Removes the tag-node relationship
2. Removes all associated field values"
;; 1. Remove tag-node relationship
(org-supertag-db-remove-link :node-tag node-id tag-id)
;; 2. Remove associated field values
(let ((tag (org-supertag-tag-get tag-id)))
(dolist (field-def (plist-get tag :fields))
(org-supertag-field-remove-value field-def node-id tag-id))))
(defun org-supertag-tag-change-tag ()
"Change an existing tag to another existing tag on current node.
This function:
1. Lists current node's tags for selection
2. Lists available existing tags (excluding current node's tags) as target
3. Removes old tag and applies new tag"
(interactive)
(let* ((node-id (org-id-get))
(current-tags (org-supertag-node-get-tags node-id)))
;; Validation
(unless node-id
(error "Not on a valid node"))
(unless current-tags
(error "No tags on current node"))
;; Select source tag to change
(let* ((source-tag (completing-read "Select tag to change: "
current-tags nil t))
;; Get all existing tags except current node's tags
(all-tags (cl-set-difference
(org-supertag-get-all-tags)
current-tags
:test #'string=))
;; Ensure we have available tags to change to
(_ (unless all-tags
(error "No other existing tags available")))
;; Select target tag
(target-tag (completing-read
(format "Change '%s' to: " source-tag)
all-tags nil t)))
;; Execute tag change
(save-excursion
;; 1. Remove old tag
(org-supertag-tag--remove source-tag node-id)
;; Remove from org tags
(let* ((current-org-tags (org-get-tags))
(new-org-tags (delete (concat "#" source-tag)
current-org-tags)))
(org-set-tags new-org-tags))
;; 2. Apply new tag
(org-supertag-tag-apply target-tag))
(message "Changed tag '%s' to '%s'" source-tag target-tag))))
;;----------------------------------------------------------------------
;; Tag Field Operation
;;----------------------------------------------------------------------
(defun org-supertag--sanitize-field-name (name)
"Convert field name to valid property name.
NAME is the original field name.
This function:
1. Trims leading/trailing whitespace
2. Replaces spaces with underscores
3. Converts to uppercase"
(let ((sanitized (replace-regexp-in-string
"\\s-+" "_" ; Replace spaces with underscores
(upcase (string-trim name))))) ; Convert to uppercase and trim
sanitized))
(defun org-supertag-tag-get-nodes (tag-id)
"Get all node IDs associated with a tag.
TAG-ID: The tag identifier (without 'tag:' prefix)
Returns a list of node IDs that have this tag attached."
(let (result)
(maphash
(lambda (link-id props)
(message "Checking link: %s with props: %s" link-id props)
(when (and (string-prefix-p ":node-tag:" link-id)
(equal (plist-get props :to) tag-id)) ; Direct comparison, no prefix handling
(push (plist-get props :from) result)))
org-supertag-db--link)
(delete-dups result)))
(defun org-supertag-tag-get-fields (tag-id)
"Get list of fields defined for a tag.
TAG-ID: The tag identifier"
(plist-get (org-supertag-tag-get tag-id) :fields))
(defun org-supertag-tag-add-field (tag-id field-def)
"Add a field to a tag.
TAG-ID: The tag identifier
FIELD-DEF: The field definition plist"
(let* ((tag (org-supertag-db-get tag-id))
(fields (plist-get tag :fields))
(new-fields (append fields (list field-def)))
(new-tag (plist-put (copy-sequence tag) :fields new-fields)))
(org-supertag-db-add tag-id new-tag)))
(defun org-supertag-tag-get-field-value (tag field-name)
"Get field value from TAG with FIELD-NAME.
TAG is the tag entity
FIELD-NAME is the name of the field to get"
(when-let* ((fields (plist-get tag :fields))
(field (cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal)))
(plist-get field :value)))
(defun org-supertag-tag--set-field-value (tag-id node-id field-name value)
"Set field value for TAG-ID on NODE-ID.
FIELD-NAME is the name of the field
VALUE is the value to set"
(let ((tag (org-supertag-tag-get tag-id)))
(when tag
;; Update field value in tag definition
(let* ((fields (plist-get tag :fields))
(field (cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal)))
(when field
(setf (plist-get field :value) value)
;; Update tag in database
(org-supertag-db-add tag-id tag)
;; Run hook
(run-hook-with-args 'org-supertag-node-field-updated-hook
node-id field-name value))))))
(defun org-supertag-tag-remove-field (tag-id field-name)
"Remove a field from a tag.
TAG-ID: The tag identifier
FIELD-NAME: The name of the field to remove"
(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 remove: "
(mapcar (lambda (field)
(plist-get field :name))
fields)
nil t)))
(list tag-id field-name)))
(when (yes-or-no-p (format "Remove field '%s' from tag '%s'? This will remove all values of this field."
field-name tag-id))
(let* ((tag (org-supertag-db-get tag-id))
(fields (plist-get tag :fields))
;; Remove field from fields list
(new-fields (cl-remove-if (lambda (field)
(equal (plist-get field :name)
field-name))
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)
;; Remove field values from all nodes
(let ((nodes (org-supertag-tag-get-nodes tag-id)))
(dolist (node-id nodes)
;; Remove from database
(org-supertag-field-db-delete-value node-id field-name tag-id)
;; Remove from org buffer
(when-let ((pos (condition-case nil
(org-id-find node-id t)
(error nil))))
(org-with-point-at pos
(org-delete-property field-name)))))
(message "Removed field '%s' from tag '%s'" field-name tag-id))))
(defun org-supertag-tag-batch-set-fields (tag-id)
"Batch set field values for a tag.
TAG-ID: The tag identifier"
(interactive
(let* ((node-id (org-id-get))
(tags (org-supertag-node-get-tags node-id))
(tag-id (completing-read "Select tag: " tags nil t)))
(list tag-id)))
(let* ((tag (org-supertag-tag-get tag-id))
(fields (plist-get tag :fields))
(nodes (org-supertag-tag-get-nodes tag-id))
(field-values (make-hash-table :test 'equal)))
;; Collect field values
(dolist (field fields)
(let* ((field-name (plist-get field :name))
(field-type (plist-get field :type))
(value (org-supertag-field-read-value field)))
(puthash field-name value field-values)))
;; Apply values to all nodes
(dolist (node-id nodes)
(dolist (field fields)
(let* ((field-name (plist-get field :name))
(value (gethash field-name field-values)))
(org-supertag-field-set-value field value node-id tag-id))))
(message "Batch set field values for tag '%s' completed." tag-id)))
;;----------------------------------------------------------------------
;; Tag User Command
;;----------------------------------------------------------------------
(defun org-supertag-tag-add-tag (tag-name)
"Add a tag to the current headline.
TAG-NAME can be an existing tag or a new tag name.
Will prevent duplicate tag application."
(interactive
(let* ((all-tags (org-supertag-get-all-tags))
(preset-names (mapcar #'car org-supertag-preset-tags))
;; Remove any existing preset tags from all-tags to avoid duplicates
(user-tags (cl-remove-if (lambda (tag) (member tag preset-names)) all-tags))
(candidates (delete-dups
(append
;; Format preset tags with [P] prefix
(mapcar (lambda (name)
(format "[P] %s" name))
preset-names)
;; Regular tags as is
user-tags)))
(input (completing-read
"Enter tag name (TAB: complete): "
candidates nil nil)))
(list
(cond
((string-prefix-p "[P] " input)
(substring input 4)) ; Remove [P] prefix
(t input)))))
(when tag-name
(let* ((node-id (org-id-get))
(sanitized-name (org-supertag-sanitize-tag-name tag-name))
(current-tags (org-supertag-node-get-tags node-id)))
(message "Adding tag: %s" sanitized-name)
;; Check for duplicate tag
(when (member sanitized-name current-tags)
(user-error "Tag '%s' is already applied to this node" sanitized-name))
(let* ((existing-tag (org-supertag-tag-get sanitized-name))
(preset-fields (org-supertag-get-preset-fields sanitized-name))
;; Get or create the tag
(tag-id
(cond
;; If tag exists, use it
(existing-tag
sanitized-name)
;; If it's a preset tag, create with preset fields
(preset-fields
(progn
(message "Creating new tag from preset with fields: %S" preset-fields)
(org-supertag-tag-create sanitized-name :fields preset-fields)))
;; Otherwise create a new empty tag
(t
(if (y-or-n-p (format "Create new tag '%s'? " sanitized-name))
(org-supertag-tag-create sanitized-name)
(user-error "Tag creation cancelled"))))))
;; Apply the tag
(org-supertag-tag-apply tag-id)
;; Skip immediate field value input for preset tags
(message "Tag '%s' applied. Use `org-supertag-tag-set-field-and-value' to set field values." tag-id)))))
(defun org-supertag-tag-batch-add-tag (tag-name)
"Batch add tags to selected headlines.
TAG-NAME is the name of the tag to add."
(interactive
(list (completing-read "Select tag: "
(append
(org-supertag-get-all-tags)
(mapcar #'car org-supertag-preset-tags)))))
(let* ((ast (org-element-parse-buffer))
(headlines '())
(selected-headlines '())
(tag (org-supertag-tag-get tag-name)))
;; Collect all headlines without ID
(org-element-map ast 'headline
(lambda (headline)
(unless (org-element-property :ID headline)
(push headline headlines))))
;; Build choices list
(let ((choices (append
'(("Finish" . :finish)) ; Add finish option
(mapcar (lambda (hl)
(cons (org-element-property :raw-value hl)
hl))
headlines))))
;; Loop until user selects Finish
(while (when-let* ((title (completing-read
(format "Select headline (%d selected): "
(length selected-headlines))
(mapcar #'car choices)
nil t))
(choice (cdr (assoc title choices))))
(unless (eq choice :finish)
;; Add to selected list
(push choice selected-headlines)
;; Remove selected option
(setq choices (assoc-delete-all title choices))
t))) ; Continue loop unless Finish is selected
;; Apply tags to all selected headlines
(when selected-headlines
(dolist (hl selected-headlines)
(save-excursion
(goto-char (org-element-property :begin hl))
(org-supertag-tag-add-tag tag-name)))
(message "Added tag %s to %d headlines"
tag-name
(length selected-headlines))))))
(defun org-supertag-tag-remove ()
"Remove tag from current node.
This function:
1. Removes tag from org tags
2. Calls internal function to remove tag data"
(interactive)
(let* ((node-id (org-id-get))
(tags (org-supertag-node-get-tags node-id))
(tag-id (completing-read "Select tag to remove: " tags nil t)))
(unless node-id
(error "Not on a valid node"))
(unless tags
(error "No tags on current node"))
;; 1. Remove from org tags
(let* ((current-tags (org-get-tags))
(tag-name (concat "#" tag-id))
(new-tags (delete tag-name current-tags)))
(org-set-tags new-tags))
;; 2. Remove tag data using internal function
(org-supertag-tag--remove tag-id node-id)
(message "Removed tag '%s' and its fields" tag-id)))
;;----------------------------------------------------------------------
;; Field Edit Mode
;;----------------------------------------------------------------------
(defun org-supertag-tag-set-field-and-value ()
"Set field values for tags on the current node using a table interface."
(interactive)
(org-supertag-tag-edit-fields-table))
(define-derived-mode org-supertag-field-edit-mode special-mode "OrgSuperTag-FieldEdit"
"Major mode for editing org-supertag fields."
(setq-local buffer-read-only nil)
;; 定义按键绑定
(let ((map org-supertag-field-edit-mode-map))
(define-key map (kbd "n") #'org-supertag-field-edit-next-field)
(define-key map (kbd "p") #'org-supertag-field-edit-prev-field)
(define-key map (kbd "RET") #'org-supertag-field-edit-complete-field)
(define-key map (kbd "C-c C-c") #'org-supertag-field-edit-save)
(define-key map (kbd "C-c C-k") #'org-supertag-field-edit-cancel)))
(defun org-supertag-tag--insert-tag-fields (tag-id source-buffer source-point)
"Insert fields for a tag.
TAG-ID is the tag identifier.
SOURCE-BUFFER is the buffer containing the org node.
SOURCE-POINT is the point in SOURCE-BUFFER."
(let* ((tag (org-supertag-tag-get tag-id))
(fields (plist-get tag :fields))
;; 1. Calculate the maximum field name length
(max-name-length
(if fields
(apply #'max
(mapcar (lambda (field)
(length (plist-get field :name)))
fields))
0))
;; 2. Calculate the maximum type name length
(max-type-length
(if fields
(apply #'max
(mapcar (lambda (field)
(length (symbol-name (plist-get field :type))))
fields))
0)))
(insert (format "Tag: %s\n" tag-id))
(insert "──────────────────────────────────────────────\n")
(if fields
(progn
(dolist (field fields)
(let* ((field-name (plist-get field :name))
(field-type (plist-get field :type))
(current-value
(with-current-buffer source-buffer
(save-excursion
(goto-char source-point)
(org-entry-get (point) field-name nil t))))
;; 3. Use fixed width formatting for fields
(formatted-line
(format (format " %%-%ds [%%-%ds]: %%s\n"
max-name-length
max-type-length)
field-name
(symbol-name field-type)
(or current-value ""))))
(insert formatted-line)))
(insert " [Press 'a' to add, 'e' to edit, 'd' to delete fields]\n"))
;; If no fields defined
(insert " No fields defined\n")
(insert " [Press 'a' to add fields]\n"))))
(defun org-supertag-tag--add-field (tag-id)
"Add a new field to TAG-ID."
(let* ((field-name (read-string "Field name: "))
(all-types (mapcar #'car org-supertag-field-types))
(type-help
(concat
"Available types:\n"
(mapconcat
(lambda (type)
(let ((desc (plist-get (cdr (assq type org-supertag-field-types))
:description)))
(format " %-12s - %s" type desc)))
all-types
"\n")))
(field-type
(minibuffer-with-setup-hook
(lambda ()
(let ((inhibit-read-only t))
(with-current-buffer (get-buffer-create "*Minibuf Help*")
(erase-buffer)
(insert type-help)
(display-buffer (current-buffer)
'((display-buffer-in-side-window)
(side . bottom)
(window-height . fit-window-to-buffer))))))
(intern
(completing-read
"Field type: "
(mapcar #'symbol-name all-types)
nil t))))
(field-def
(list :name field-name
:type field-type)))
;; 清理帮助 buffer
(when-let ((help-buf (get-buffer "*Minibuf Help*")))
(kill-buffer help-buf))
;; Add options for options type
(when (eq field-type 'options)
(let ((options
(split-string
(read-string "Options (comma separated): ")
"," t "[ \t]+")))
(setq field-def
(plist-put field-def :options options))))
;; Add description
(let ((desc (read-string "Description (optional): ")))
(when (not (string-empty-p desc))
(setq field-def
(plist-put field-def :description desc))))
;; Add field to tag
(org-supertag-tag-add-field tag-id field-def)))
(defun org-supertag-tag--refresh-field-table (context)
"Refresh the field table display.
CONTEXT is the edit context plist containing:
- :node-id The node ID
- :node-title The node title
- :tags List of tags
- :source-buffer The source buffer
- :source-point The point in source buffer"
(let ((inhibit-read-only t)
(current-point (point)))
(erase-buffer)
;; Header
(insert (format "Fields for Node: %s\n"
(plist-get context :node-title)))
;; Fields
(dolist (tag-id (plist-get context :tags))
(org-supertag-tag--insert-tag-fields
tag-id
(plist-get context :source-buffer)
(plist-get context :source-point)))
;; Help text
(insert "\nCommands:\n")
(insert "n: Next field p: Previous field\n")
(insert "RET: Edit field value a: Add field\n")
(insert "e: Edit field d: Delete field\n")
(insert "C-c C-c: Save C-c C-k: Cancel\n")
;; Restore point
(goto-char (min current-point (point-max)))))
(defun org-supertag-tag--edit-field (tag-id field-name)
"Edit an existing field in TAG-ID with 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))
(action (completing-read
"Action: "
'("Edit type" "Edit options" "Edit description")
nil t)))
(pcase action
("Edit type"
(let* ((all-types (mapcar #'car org-supertag-field-types))
(type-help
(concat
"Available types:\n"
(mapconcat
(lambda (type)
(let ((desc (plist-get (cdr (assq type org-supertag-field-types))
:description)))
(format " %-12s - %s" type desc)))
all-types
"\n")))
(new-type
(minibuffer-with-setup-hook
(lambda ()
(let ((inhibit-read-only t))
(with-current-buffer (get-buffer-create "*Minibuf Help*")
(erase-buffer)
(insert type-help)
(display-buffer (current-buffer)
'((display-buffer-in-side-window)
(side . bottom)
(window-height . fit-window-to-buffer))))))
(intern
(completing-read
"New type: "
(mapcar #'symbol-name all-types)
nil t)))))
;; 清理帮助 buffer
(when-let ((help-buf (get-buffer "*Minibuf Help*")))
(kill-buffer help-buf))
(setq field (plist-put field :type new-type))
;; 如果新类型是 options,添加选项
(when (eq new-type 'options)
(let ((options
(split-string
(read-string "Options (comma separated): ")
"," t "[ \t]+")))
(setq field (plist-put field :options options))))
;; 更新字段列表
(let ((new-fields (cl-substitute field
(cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal)
fields
:test #'equal)))
;; 更新 tag 定义
(setq tag (plist-put tag :fields new-fields))
(org-supertag-db-add tag-id tag))))
("Edit options"
(when (eq (plist-get field :type) 'options)
(let ((new-options
(split-string
(read-string "New options (comma separated): "
(mapconcat #'identity
(plist-get field :options)
","))
"," t "[ \t]+")))
(setq field (plist-put field :options new-options))
;; 更新字段列表
(let ((new-fields (cl-substitute field
(cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal)
fields
:test #'equal)))
;; 更新 tag 定义
(setq tag (plist-put tag :fields new-fields))
(org-supertag-db-add tag-id tag)))))
("Edit description"
(let ((new-desc (read-string "New description: "
(plist-get field :description))))
(setq field (plist-put field :description new-desc))
;; 更新字段列表
(let ((new-fields (cl-substitute field
(cl-find field-name fields
:key (lambda (f) (plist-get f :name))
:test #'equal)
fields
:test #'equal)))
;; 更新 tag 定义
(setq tag (plist-put tag :fields new-fields))
(org-supertag-db-add tag-id tag)))))))
(defun org-supertag-tag--goto-first-field ()
"Move cursor to the first field in the OrgSuperTag-FieldEdit buffer.
If no field exists, move to the first line after the separator line."
(let ((field-buffer (get-buffer "*Org SuperTag Fields*")))
(when field-buffer
(with-current-buffer field-buffer
(goto-char (point-min))
;; Find first separator line
(if (re-search-forward "^─+$" nil t)
(progn
;; Try to find first field
(forward-line 1)
(if (looking-at "^ \\([^[]+\\)\\[\\([^]]+\\)\\]: \\(.*\\)$")
(beginning-of-line)
;; If no field, stay at current line
t))
;; If no separator found, go to beginning
(goto-char (point-min)))))))
(defun org-supertag-tag-edit-fields-table ()
"Edit fields of current node's tags in a table format."
(interactive)
(let* ((source-buffer (current-buffer))
(source-point (point))
(node-id (org-id-get))
(tags (org-supertag-node-get-tags node-id))
(node-title (org-get-heading t t t t))
(edit-buffer (get-buffer-create "*Org SuperTag Fields*")))
;; Prepare the edit buffer
(with-current-buffer edit-buffer
(let ((inhibit-read-only t))
(erase-buffer)
(org-supertag-field-edit-mode)
;; Store context
(setq-local org-supertag--edit-context
(list :node-id node-id
:node-title node-title
:tags tags
:source-buffer source-buffer
:source-point source-point))
;; Initial content
(insert (format "Fields for Node: %s\n" node-title))
(dolist (tag-id tags)
(org-supertag-tag--insert-tag-fields tag-id source-buffer source-point))
;; Add help text
(insert "\nCommands:\n")
(insert "TAB: Next field S-TAB: Previous field\n")
(insert "RET: Edit field value a: Add fields\n")
(insert "e: Edit field d: Delete field\n")
(insert "C-c C-c: Save C-c C-k: Cancel\n")
;; Add key bindings for field operations
(define-key org-supertag-field-edit-mode-map
(kbd "a")
(lambda ()
(interactive)
(let ((tag-id (save-excursion
(re-search-backward "^Tag: \\(.+\\)$" nil t)
(match-string 1))))
(org-supertag-tag--add-field tag-id)
(org-supertag-tag--refresh-field-table org-supertag--edit-context))))
(define-key org-supertag-field-edit-mode-map
(kbd "e")
(lambda ()
(interactive)
(save-excursion
(beginning-of-line)
(when (looking-at "^ \\([^ ]+\\) \\[")
(let* ((field-name (match-string 1))
(tag-id (save-excursion
(re-search-backward "^Tag: \\(.+\\)$" nil t)
(match-string 1))))
(org-supertag-tag--edit-field tag-id field-name)
(org-supertag-tag--refresh-field-table org-supertag--edit-context))))))
(define-key org-supertag-field-edit-mode-map
(kbd "d")
(lambda ()
(interactive)
(save-excursion
(beginning-of-line)
(when (looking-at "^ \\([^ ]+\\) \\[")
(let* ((field-name (match-string 1))
(tag-id (save-excursion
(re-search-backward "^Tag: \\(.+\\)$" nil t)
(match-string 1))))
(when (yes-or-no-p (format "Delete field '%s'? " field-name))
(org-supertag-tag-remove-field tag-id field-name)
(org-supertag-tag--refresh-field-table org-supertag--edit-context)))))))))
;; Display buffer below current buffer and position cursor
(let ((window (display-buffer edit-buffer
'((display-buffer-below-selected)
(window-height . fit-window-to-buffer)
(preserve-size . (nil . t))
(select . t)))))
(select-window window)
;; 2. Set the cursor position
(goto-char (point-min))
(re-search-forward "^─+$" nil t)
(forward-line 1))))
(defun org-supertag-field-edit-next-field ()
"Move to next editable field."
(interactive)
(forward-line)
(when (looking-at "^$")
(forward-line)))
(defun org-supertag-field-edit-prev-field ()
"Move to previous editable field."
(interactive)
(forward-line -1)
(when (looking-at "^$")
(forward-line -1)))
(defun org-supertag-field-edit-complete-field ()
"Edit the current field value."
(interactive)
(when (looking-at "^ \\([^[]+\\)\\[\\([^]]+\\)\\]: \\(.*\\)$")
(let* ((field-name (match-string 1))
(field-type (match-string 2))
(current-value (match-string 3))
(tag-id (save-excursion
(re-search-backward "^Tag: \\(.+\\)$" nil t)
(match-string 1)))
(tag (org-supertag-tag-get tag-id))
(field (cl-find (string-trim field-name)
(plist-get tag :fields)
:key (lambda (f) (plist-get f :name))
:test #'equal))
(new-value (org-supertag-field-read-value field)))
(when new-value
(save-excursion
(beginning-of-line)
(when (re-search-forward ": .*$" nil t)
(replace-match (format ": %s" new-value))))))))
(defun org-supertag-field-edit-save ()
"Save all field values and close the edit buffer."
(interactive)
(let* ((context org-supertag--edit-context)
(node-id (plist-get context :node-id))
(source-buffer (plist-get context :source-buffer))
(source-point (plist-get context :source-point)))
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^ \\([^[]+\\)\\[\\([^]]+\\)\\]: \\(.*\\)$" nil t)
(let ((field-name (string-trim (match-string 1)))
(new-value (string-trim (match-string 3))))
(unless (string-empty-p new-value)
(with-current-buffer source-buffer
(save-excursion
(goto-char source-point)
(org-set-property field-name new-value)))))))
(quit-window t)))
(defun org-supertag-field-edit-cancel ()
"Cancel editing and close the buffer."
(interactive)
(when (yes-or-no-p "Cancel editing? Changes will be lost.")
(quit-window t)))
;;----------------------------------------------------------------------
;; Preset Tag
;;----------------------------------------------------------------------
(defcustom org-supertag-preset-tags
'(("project" . ((:name "status"
:type options
:options ("planning" "active" "on-hold" "completed" "cancelled")
:description "Project status")
(:name "priority"
:type options
:options ("high" "medium" "low")
:description "Priority level")
(:name "deadline"
:type date
:description "Due date")
(:name "owner"
:type string
:description "Project owner")))
("task" . ((:name "status"
:type options
:options ("todo" "in-progress" "blocked" "done" "cancelled")
:description "Task status")
(:name "priority"
:type options
:options ("A" "B" "C")
:description "Priority level")
(:name "due"
:type date
:description "Due date")
(:name "assignee"
:type string
:description "Assigned to")))
("person" . ((:name "role"
:type string
:description "Role")
(:name "email"
:type string
:description "Email address")
(:name "phone"
:type string
:description "Phone number")))
("meeting" . ((:name "date"
:type date
:description "Meeting date")
(:name "attendees"
:type string
:description "Attendees")
(:name "location"
:type string
:description "Location")))
("place" . ((:name "address"
:type string
:description "Address")
(:name "category"
:type options
:options ("office" "home" "public" "other")
:description "Place type")))
("company" . ((:name "industry"
:type string
:description "Industry")
(:name "website"
:type string
:description "Website")
(:name "contact"
:type string
:description "Contact person")))
("note" . ((:name "category"
:type options
:options ("idea" "reference" "summary" "other")
:description "Note type")
(:name "source"
:type string
:description "Source"))))
"Default preset tags with their field definitions.
Users can override this by setting it in their config files.
Format:
((tag-name . field-definitions) ...)
Each field definition is a plist with properties:
- :name Field name
- :type Field type (string, options, date, etc)
- :options List of options (for options type)
- :description Field description
Example user configuration:
(setq org-supertag-preset-tags
(append org-supertag-preset-tags
'((\"book\" . ((:name \"status\"
:type options
:options (\"reading\" \"completed\" \"want-to-read\")
:description \"Reading status\")
(:name \"author\"
:type string