-
Notifications
You must be signed in to change notification settings - Fork 2
/
org-supertag-behavior-library.el
1282 lines (1171 loc) · 49.7 KB
/
org-supertag-behavior-library.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-behavior-library.el --- Library functions for org-supertag behaviors -*- lexical-binding: t; -*-
;;; Commentary:
;; Provides various library functions to support org-supertag behavior system
;; Each library focuses on functionality in specific domains, supporting behavior implementation and composition
;;
;; Behavior Library Function Development Guide
;; =========================================
;;
;; 1. Function Naming and Documentation
;; ----------------------------------
;; - Use org-supertag-behavior-- prefix
;; - Function names should reflect specific actions
;; - Detailed docstrings including:
;; * Functionality description
;; * Parameter descriptions
;; * Return value descriptions
;; * Usage examples
;;
;; Example:
;; (defun org-supertag-behavior--set-todo (node-id params)
;; "Set TODO state for NODE-ID based on PARAMS.
;; PARAMS is a plist with :state key.
;;
;; Example:
;; (org-supertag-behavior--set-todo node-id '(:state \"DONE\"))")
;;
;; 2. Parameter Handling
;; -------------------
;; - Required parameters:
;; * node-id: Node identifier
;; * params: Parameter plist
;; - Use plist-get to extract parameters
;; - Use when-let* for parameter validation
;;
;; 3. Position Management
;; --------------------
;; - Get node position: (org-supertag-db-get-pos node-id)
;; - Protect current position: (save-excursion ...)
;; - Ensure correct position: (org-with-point-at pos ...)
;;
;; 4. Error Handling
;; ---------------
;; - Use when-let* to handle potential nil values
;; - Add debug info: (message "Debug ...")
;; - Use condition-case to catch errors when needed
;;
;; 5. Best Practices
;; ---------------
;; - Keep functions focused on single responsibility
;; - Prefer existing org-mode functions
;; - Implement complex functionality through composition
;; - Ensure position safety
;; - Add sufficient debug information
;;
;;; Code:
(require 'org)
;;------------------------------------------------------------------------------
;; State Change Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--on-state-change (node-id params)
"Handle state change for NODE-ID based on PARAMS.
PARAMS is a plist with :from and :to keys for state transition.
Optionally :note key for state change note.
Example:
(org-supertag-behavior--on-state-change node-id
'(:from \"TODO\" :to \"DONE\" :note \"Completed task\"))"
(message "Debug state-change - node=%s params=%S" node-id params)
(when-let* ((from (plist-get params :from))
(to (plist-get params :to))
(note (plist-get params :note))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug state-change - Changing state from %s to %s" from to)
(save-excursion
(org-with-point-at pos
;; record state change
(when note
(org-add-log-note))
;; trigger org-trigger-hook
(run-hook-with-args 'org-trigger-hook
(list :type 'state-change
:position pos
:from from
:to to))))))
(defun org-supertag-behavior--update-statistics (node-id _params)
"Update TODO statistics cookies for NODE-ID.
This function doesn't require any parameters but follows the behavior function
signature for consistency.
Example:
(org-supertag-behavior--update-statistics node-id nil)"
(message "Debug update-stats - node=%s" node-id)
(when-let ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(org-update-statistics-cookies nil)
(run-hooks 'org-after-todo-statistics-hook)))))
(defun org-supertag-behavior--toggle-state (node-id params)
"Toggle between two states for NODE-ID based on PARAMS.
PARAMS is a plist with :states (a list of two states) key.
Example:
(org-supertag-behavior--toggle-state node-id
'(:states (\"TODO\" \"DONE\")))"
(message "Debug toggle-state - node=%s params=%S" node-id params)
(when-let* ((states (plist-get params :states))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((current (org-get-todo-state))
(next (if (equal current (car states))
(cadr states)
(car states))))
(message "Debug toggle-state - Toggling from %s to %s"
current next)
(org-todo next))))))
(defun org-supertag-behavior--propagate-state (node-id params)
"Propagate state change to children of NODE-ID based on PARAMS.
PARAMS is a plist with :state key and optional :recursive flag.
Example:
;; Propagate to immediate children
(org-supertag-behavior--propagate-state node-id
'(:state \"DONE\"))
;; Propagate recursively
(org-supertag-behavior--propagate-state node-id
'(:state \"DONE\" :recursive t))"
(message "Debug propagate-state - node=%s params=%S" node-id params)
(when-let* ((state (plist-get params :state))
(recursive (plist-get params :recursive))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let ((level (org-outline-level)))
(save-restriction
(org-narrow-to-subtree)
(while (outline-next-heading)
(when (or (not recursive)
(= (org-outline-level) (1+ level)))
(org-todo state)))))))))
;;------------------------------------------------------------------------------
;; Node Status
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--set-todo (node-id params)
"Set TODO state for NODE-ID based on PARAMS.
PARAMS is a plist with :state key."
(message "Debug set-todo - node=%s params=%S" node-id params)
(when-let* ((state (plist-get params :state))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug set-todo - Setting state to: %s" state)
(save-excursion
(org-with-point-at pos
(org-todo state)))))
(defun org-supertag-behavior--set-priority (node-id params)
"Set priority for NODE-ID based on PARAMS.
PARAMS is a plist with :priority key (A, B, or C).
Example:
(org-supertag-behavior--set-priority node-id '(:priority \"A\"))"
(message "Debug set-priority - node=%s params=%S" node-id params)
(when-let* ((priority (plist-get params :priority))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug set-priority - Setting priority to: %s" priority)
(save-excursion
(org-with-point-at pos
(org-priority (string-to-char priority))))))
;;------------------------------------------------------------------------------
;; Node Property
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--set-property (node-id params)
"Set property for NODE-ID based on PARAMS.
PARAMS is a plist with :name and :value keys.
Example:
(org-supertag-behavior--set-property node-id '(:name \"CREATED\" :value \"2024-01-20\"))"
(message "Debug set-property - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(value (plist-get params :value))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug set-property - Setting %s to: %s" name value)
(save-excursion
(org-with-point-at pos
(org-set-property name value)))))
(defun org-supertag-behavior--delete-property (node-id params)
"Delete property for NODE-ID based on PARAMS.
PARAMS is a plist with :name key.
Example:
(org-supertag-behavior--delete-property node-id '(:name \"CREATED\"))"
(message "Debug delete-property - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug delete-property - Deleting property: %s" name)
(save-excursion
(org-with-point-at pos
(org-delete-property name)))))
;;------------------------------------------------------------------------------
;; Node Heading
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--set-heading (node-id params)
"Set heading for NODE-ID based on PARAMS.
PARAMS is a plist with :title key.
Example:
(org-supertag-behavior--set-heading node-id '(:title \"New Title\"))"
(message "Debug set-heading - node=%s params=%S" node-id params)
(when-let* ((title (plist-get params :title))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug set-heading - Setting title to: %s" title)
(save-excursion
(org-with-point-at pos
(org-edit-headline title)))))
;;------------------------------------------------------------------------------
;; Property Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--get-property (node-id params)
"Get property value for NODE-ID based on PARAMS.
PARAMS is a plist with :name key and optional :inherit flag.
Example:
;; Get local property
(org-supertag-behavior--get-property node-id '(:name \"CATEGORY\"))
;; Get with inheritance
(org-supertag-behavior--get-property node-id '(:name \"CATEGORY\" :inherit t))"
(message "Debug get-property - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(inherit (plist-get params :inherit))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug get-property - Getting %s (inherit=%s)" name inherit)
(save-excursion
(org-with-point-at pos
(if inherit
(org-entry-get nil name t) ; t means inherit
(org-entry-get nil name))))))
(defun org-supertag-behavior--get-properties (node-id params)
"Get all properties for NODE-ID based on PARAMS.
PARAMS is a plist with optional :type key (:all, :inherited, or :local).
Default is :local.
Example:
;; Get local properties
(org-supertag-behavior--get-properties node-id '(:type :local))
;; Get all properties including inherited
(org-supertag-behavior--get-properties node-id '(:type :all))"
(message "Debug get-properties - node=%s params=%S" node-id params)
(when-let* ((type (or (plist-get params :type) :local))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug get-properties - Getting properties type: %s" type)
(save-excursion
(org-with-point-at pos
(pcase type
(:all (org-entry-properties nil))
(:inherited (org-entry-properties nil 'inherited))
(:local (org-entry-properties nil 'standard))
(_ (error "Invalid property type: %s" type)))))))
(defun org-supertag-behavior--copy-property (node-id params)
"Copy property from one entry to another for NODE-ID based on PARAMS.
PARAMS is a plist with :name, :from-id, and optional :if-missing keys.
Example:
;; Copy property if it doesn't exist locally
(org-supertag-behavior--copy-property node-id
'(:name \"CATEGORY\" :from-id \"parent-id\" :if-missing t))"
(message "Debug copy-property - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(from-id (plist-get params :from-id))
(if-missing (plist-get params :if-missing))
(pos (org-supertag-db-get-pos node-id))
(from-pos (org-supertag-db-get-pos from-id)))
(message "Debug copy-property - Copying %s from %s" name from-id)
(save-excursion
(org-with-point-at from-pos
(when-let ((value (org-entry-get nil name)))
(org-with-point-at pos
(when (or (not if-missing)
(not (org-entry-get nil name)))
(org-entry-put nil name value))))))))
(defun org-supertag-behavior--track-ordered-property (node-id params)
"Set ORDERED property and corresponding tag for NODE-ID based on PARAMS.
PARAMS is a plist with :value key (t or nil).
Example:
(org-supertag-behavior--track-ordered-property node-id '(:value t))"
(message "Debug track-ordered - node=%s params=%S" node-id params)
(when-let* ((value (plist-get params :value))
(pos (org-supertag-db-get-pos node-id)))
(message "Debug track-ordered - Setting ORDERED to: %s" value)
(save-excursion
(org-with-point-at pos
;; 设置 ORDERED 属性
(if value
(org-entry-put nil "ORDERED" "t")
(org-entry-delete nil "ORDERED"))
;; 如果启用了 org-track-ordered-property-with-tag
(when org-track-ordered-property-with-tag
(if value
(org-toggle-tag "ORDERED" 'on)
(org-toggle-tag "ORDERED" 'off)))))))
;;------------------------------------------------------------------------------
;; Drawer Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--insert-drawer (node-id params)
"Insert drawer for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :name : Drawer name
- :content : Optional initial content
- :region : Whether to wrap region (t or nil)
Example:
;; Insert empty drawer
(org-supertag-behavior--insert-drawer node-id
'(:name \"NOTES\"))
;; Insert drawer with content
(org-supertag-behavior--insert-drawer node-id
'(:name \"DETAILS\"
:content \"Initial content\"))
;; Insert drawer around region
(org-supertag-behavior--insert-drawer node-id
'(:name \"EXAMPLE\" :region t))"
(message "Debug insert-drawer - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(content (plist-get params :content))
(region (plist-get params :region))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 插入抽屉
(condition-case err
(progn
(if region
(org-insert-drawer)
(insert ":" name ":\n:END:\n")
(forward-line -2))
;; 如果有内容,插入内容
(when content
(forward-line 1)
(insert content "\n")))
(error
(message "Error inserting drawer: %S" err)))))))
(defun org-supertag-behavior--log-into-drawer (node-id params)
"Configure logging into drawer for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :enabled : Whether to enable drawer logging
- :name : Optional custom drawer name
- :note : Optional note to add
Example:
;; Enable logging into default drawer
(org-supertag-behavior--log-into-drawer node-id
'(:enabled t))
;; Enable logging into custom drawer
(org-supertag-behavior--log-into-drawer node-id
'(:enabled t :name \"HISTORY\"))
;; Add note to drawer
(org-supertag-behavior--log-into-drawer node-id
'(:enabled t :note \"Status update\"))"
(message "Debug log-drawer - node=%s params=%S" node-id params)
(when-let* ((enabled (plist-get params :enabled))
(name (plist-get params :name))
(note (plist-get params :note))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 设置抽屉日志
(let ((org-log-into-drawer (if enabled
(or name t)
nil)))
;; 如果需要添加注释
(when note
(org-add-note note)))))))
(defun org-supertag-behavior--format-drawer (node-id params)
"Format drawer for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :name : Drawer name to format
- :format : Format type (:indent, :html, :latex, etc.)
- :style : Optional style properties
Example:
;; Indent drawer
(org-supertag-behavior--format-drawer node-id
'(:name \"NOTES\" :format :indent))
;; Format for HTML
(org-supertag-behavior--format-drawer node-id
'(:name \"DETAILS\"
:format :html
:style (:class \"custom-drawer\")))"
(message "Debug format-drawer - node=%s params=%S" node-id params)
(when-let* ((name (plist-get params :name))
(format-type (plist-get params :format))
(style (plist-get params :style))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(condition-case err
(pcase format-type
(:indent
(org-indent-drawer))
(:html
(let ((org-html-format-drawer-function
(lambda (name contents info)
(format "<div class=\"%s\">%s</div>"
(plist-get style :class)
contents))))
(org-html-format-drawer name "" nil)))
(:latex
(let ((org-latex-format-drawer-function
(lambda (name contents info)
(format "\\begin{%s}\n%s\\end{%s}"
name contents name))))
(org-latex-format-drawer name "" nil)))
(_ (error "Unsupported format type: %s" format-type)))
(error
(message "Error formatting drawer: %S" err)))))))
(defun org-supertag-behavior--export-drawer (node-id params)
"Configure drawer export for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :drawers : List of drawer names or t for all
- :exclude : Whether to exclude listed drawers
- :properties : Whether to include properties drawer
Example:
;; Export all drawers
(org-supertag-behavior--export-drawer node-id
'(:drawers t))
;; Export specific drawers
(org-supertag-behavior--export-drawer node-id
'(:drawers (\"NOTES\" \"DETAILS\")))
;; Exclude specific drawers
(org-supertag-behavior--export-drawer node-id
'(:drawers (\"LOGBOOK\") :exclude t))"
(message "Debug export-drawer - node=%s params=%S" node-id params)
(when-let* ((drawers (plist-get params :drawers))
(exclude (plist-get params :exclude))
(properties (plist-get params :properties))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 设置导出选项
(let ((org-export-with-drawers
(if exclude
(cons 'not drawers)
drawers))
(org-export-with-properties properties))
;; 可以在这里添加其他导出相关的操作
)))))
;;------------------------------------------------------------------------------
;; TODO State Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--set-todo-with-log (node-id params)
"Set TODO state for NODE-ID with logging based on PARAMS.
PARAMS is a plist with keys:
- :state : Target TODO state
- :log-type : Type of logging (:time, :note, or nil)
- :note : Note text when log-type is :note
Example:
;; Set state with timestamp
(org-supertag-behavior--set-todo-with-log node-id
'(:state \"DONE\" :log-type :time))
;; Set state with note
(org-supertag-behavior--set-todo-with-log node-id
'(:state \"DONE\" :log-type :note :note \"Completed early\"))"
(message "Debug set-todo-log - node=%s params=%S" node-id params)
(when-let* ((state (plist-get params :state))
(log-type (plist-get params :log-type))
(note (plist-get params :note))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 临时设置日志选项
(let ((org-log-done
(pcase log-type
(:time 'time)
(:note 'note)
(_ nil))))
;; 设置状态
(org-todo state)
;; 如果需要添加注释
(when (and (eq log-type :note) note)
(org-add-note note)))))))
(defun org-supertag-behavior--set-todo-with-faces (node-id params)
"Set TODO state with custom faces for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :state : Target TODO state
- :face : Face properties plist
Example:
(org-supertag-behavior--set-todo-with-faces node-id
'(:state \"DONE\"
:face (:foreground \"green\" :weight bold)))"
(message "Debug set-todo-faces - node=%s params=%S" node-id params)
(when-let* ((state (plist-get params :state))
(face (plist-get params :face))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 临时设置 face
(let ((org-todo-keyword-faces
(cons (cons state face)
org-todo-keyword-faces)))
(org-todo state))))))
(defun org-supertag-behavior--set-todo-with-state-change (node-id params)
"Set TODO state with state change hook for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :state : Target TODO state
- :hook-fn : Function to run after state change
- :hook-args : Arguments for hook function
Example:
(org-supertag-behavior--set-todo-with-state-change node-id
'(:state \"DONE\"
:hook-fn some-function
:hook-args (arg1 arg2)))"
(message "Debug set-todo-state-change - node=%s params=%S" node-id params)
(when-let* ((state (plist-get params :state))
(hook-fn (plist-get params :hook-fn))
(hook-args (plist-get params :hook-args))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let ((old-state (org-get-todo-state)))
;; 添加临时钩子
(add-hook 'org-todo-state-change-hook
(lambda ()
(apply hook-fn hook-args)))
;; 设置状态
(org-todo state)
;; 移除临时钩子
(remove-hook 'org-todo-state-change-hook
(lambda ()
(apply hook-fn hook-args))))))))
;;------------------------------------------------------------------------------
;; Priority Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--set-priority-with-faces (node-id params)
"Set priority with custom faces for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :priority : Priority character (A, B, or C)
- :face : Face properties plist
Example:
(org-supertag-behavior--set-priority-with-faces node-id
'(:priority \"A\"
:face (:foreground \"red\" :weight bold)))"
(message "Debug priority-faces - node=%s params=%S" node-id params)
(when-let* ((priority (plist-get params :priority))
(face (plist-get params :face))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; 临时设置 face
(let ((org-priority-faces
(cons (cons (string-to-char priority) face)
org-priority-faces)))
(org-priority (string-to-char priority)))))))
(defun org-supertag-behavior--cycle-priority (node-id params)
"Cycle priority for NODE-ID based on PARAMS.
PARAMS is a plist with optional keys:
- :direction : :up or :down for cycling direction
- :start-default : Whether to start from default priority
Example:
;; Cycle up from current priority
(org-supertag-behavior--cycle-priority node-id '(:direction :up))
;; Cycle down starting from default
(org-supertag-behavior--cycle-priority node-id
'(:direction :down :start-default t))"
(message "Debug cycle-priority - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((direction (plist-get params :direction))
(start-default (plist-get params :start-default))
(org-priority-start-cycle-with-default start-default))
(pcase direction
(:up (org-priority-up))
(:down (org-priority-down))
(_ (org-priority))))))))
(defun org-supertag-behavior--set-priority-range (node-id params)
"Set priority range for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :highest : Highest priority character
- :lowest : Lowest priority character
- :default : Default priority character
Example:
(org-supertag-behavior--set-priority-range node-id
'(:highest \"A\" :lowest \"E\" :default \"C\"))"
(message "Debug priority-range - node=%s params=%S" node-id params)
(when-let* ((highest (plist-get params :highest))
(lowest (plist-get params :lowest))
(default (plist-get params :default))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; temporarily set priority range
(let ((org-highest-priority (string-to-char highest))
(org-lowest-priority (string-to-char lowest))
(org-default-priority (string-to-char default)))
;; set current priority to default
(org-priority (string-to-char default)))))))
(defun org-supertag-behavior--priority-with-hook (node-id params)
"Set priority with hook for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :priority : Priority character
- :hook-fn : Function to run after priority change
- :hook-args : Arguments for hook function
Example:
(org-supertag-behavior--priority-with-hook node-id
'(:priority \"A\"
:hook-fn some-function
:hook-args (arg1 arg2)))"
(message "Debug priority-hook - node=%s params=%S" node-id params)
(when-let* ((priority (plist-get params :priority))
(hook-fn (plist-get params :hook-fn))
(hook-args (plist-get params :hook-args))
(pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
;; add temporary hook
(add-hook 'org-after-todo-state-change-hook
(lambda ()
(apply hook-fn hook-args)))
;; set priority
(org-priority (string-to-char priority))
;; remove temporary hook
(remove-hook 'org-after-todo-state-change-hook
(lambda ()
(apply hook-fn hook-args)))))))
;;------------------------------------------------------------------------------
;; Clock Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--clock-in (node-id params)
"Start clock on NODE-ID based on PARAMS.
PARAMS is a plist with optional keys:
- :switch-state : State to switch to when starting clock
- :resume : Whether to resume last clock if exists
- :use-last-clock : Whether to use last clock time as start
Example:
;; Simple clock in
(org-supertag-behavior--clock-in node-id nil)
;; Clock in with state switch
(org-supertag-behavior--clock-in node-id '(:switch-state \"STARTED\"))
;; Resume last clock
(org-supertag-behavior--clock-in node-id '(:resume t))"
(message "Debug clock-in - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((switch-state (plist-get params :switch-state))
(resume (plist-get params :resume))
(use-last-clock (plist-get params :use-last-clock)))
;; set temporary state switch value
(when switch-state
(setq-local org-clock-in-switch-to-state switch-state))
;; set whether to resume
(setq-local org-clock-in-resume resume)
;; set whether to use last clock
(setq-local org-clock-continuously use-last-clock)
;; start clock
(condition-case err
(org-clock-in)
(error
(message "Error starting clock: %S" err))))))))
(defun org-supertag-behavior--clock-out (node-id params)
"Stop clock on NODE-ID based on PARAMS.
PARAMS is a plist with optional keys:
- :switch-state : State to switch to after stopping clock
- :note : Note to add when stopping clock
Example:
;; Simple clock out
(org-supertag-behavior--clock-out node-id nil)
;; Clock out with state change
(org-supertag-behavior--clock-out node-id
'(:switch-state \"DONE\" :note \"Completed task\"))"
(message "Debug clock-out - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((switch-state (plist-get params :switch-state))
(note (plist-get params :note)))
;; set temporary state switch value
(when switch-state
(setq-local org-clock-out-switch-to-state switch-state))
;; stop clock
(condition-case err
(org-clock-out nil nil)
(error
(message "Error stopping clock: %S" err)))
;; add note
(when note
(org-add-note note)))))))
(defun org-supertag-behavior--clock-cancel (node-id _params)
"Cancel clock on NODE-ID.
This function doesn't require any parameters but follows the behavior function
signature for consistency.
Example:
(org-supertag-behavior--clock-cancel node-id nil)"
(message "Debug clock-cancel - node=%s" node-id)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(condition-case err
(org-clock-cancel)
(error
(message "Error canceling clock: %S" err)))))))
(defun org-supertag-behavior--clock-report (node-id params)
"Generate clock report for NODE-ID based on PARAMS.
PARAMS is a plist with optional keys:
- :scope : Report scope (:subtree, :file, or :all)
- :range : Time range (:today, :week, :month, or days-number)
Example:
;; Report for today
(org-supertag-behavior--clock-report node-id
'(:scope :subtree :range :today))
;; Report for last 7 days
(org-supertag-behavior--clock-report node-id
'(:scope :file :range 7))"
(message "Debug clock-report - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id))
(scope (or (plist-get params :scope) :subtree))
(range (or (plist-get params :range) :today)))
(save-excursion
(org-with-point-at pos
(let ((rangeval (pcase range
(:today '(:today))
(:week '(:week))
(:month '(:month))
((pred numberp) `(:tstart
,(- (org-time-today)
(* range 86400))))
(_ '(:today)))))
;; insert or update clock report
(condition-case err
(org-clock-report `(:scope ,scope ,@rangeval))
(error
(message "Error generating clock report: %S" err))))))))
;;------------------------------------------------------------------------------
;; Timer Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--timer-start (node-id params)
"Start a timer for NODE-ID based on PARAMS.
PARAMS is a plist with optional keys:
- :offset : Timer offset in minutes or 'HH:MM:SS' format
- :format : Custom format string for timer display
Example:
;; Start timer with default format
(org-supertag-behavior--timer-start node-id nil)
;; Start with offset
(org-supertag-behavior--timer-start node-id '(:offset \"1:30:00\"))
;; Start with custom format
(org-supertag-behavior--timer-start node-id
'(:format \"Timer: %s\"))"
(message "Debug timer-start - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((offset (plist-get params :offset))
(format (or (plist-get params :format)
org-timer-format)))
;; set temporary format
(setq-local org-timer-format format)
;; start timer
(condition-case err
(if offset
(org-timer-start offset)
(org-timer-start))
(error
(message "Error starting timer: %S" err))))))))
(defun org-supertag-behavior--timer-set (node-id params)
"Set countdown timer for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :duration : Timer duration in minutes or 'HH:MM:SS' format
- :default : Whether to use org-timer-default-timer
- :effort : Whether to use Effort property
- :hook-fn : Function to run when timer expires
Example:
;; Set timer for 30 minutes
(org-supertag-behavior--timer-set node-id '(:duration \"30\"))
;; Use default timer
(org-supertag-behavior--timer-set node-id '(:default t))
;; Use effort property
(org-supertag-behavior--timer-set node-id '(:effort t))"
(message "Debug timer-set - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let* ((duration (plist-get params :duration))
(use-default (plist-get params :default))
(use-effort (plist-get params :effort))
(hook-fn (plist-get params :hook-fn)))
;; set temporary hook
(when hook-fn
(add-hook 'org-timer-done-hook hook-fn))
;; set timer
(condition-case err
(cond
(duration
(org-timer-set-timer duration))
(use-default
(let ((org-timer-default-timer org-timer-default-timer))
(org-timer-set-timer '(16))))
(use-effort
(org-timer-set-timer))
(t
(org-timer-set-timer)))
(error
(message "Error setting timer: %S" err)))
;; remove temporary hook
(when hook-fn
(remove-hook 'org-timer-done-hook hook-fn)))))))
(defun org-supertag-behavior--timer-pause (node-id params)
"Pause or continue timer for NODE-ID based on PARAMS.
PARAMS is a plist with optional :stop flag to completely stop timer.
Example:
;; Pause/continue timer
(org-supertag-behavior--timer-pause node-id nil)
;; Stop timer
(org-supertag-behavior--timer-pause node-id '(:stop t))"
(message "Debug timer-pause - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let ((stop (plist-get params :stop)))
(condition-case err
(org-timer-pause-or-continue stop)
(error
(message "Error pausing timer: %S" err))))))))
(defun org-supertag-behavior--timer-item (node-id params)
"Insert timer item for NODE-ID based on PARAMS.
PARAMS is a plist with optional :prefix key for custom item prefix.
Example:
;; Insert timer item with default prefix
(org-supertag-behavior--timer-item node-id nil)
;; Insert with custom prefix
(org-supertag-behavior--timer-item node-id
'(:prefix \"=> \"))"
(message "Debug timer-item - node=%s params=%S" node-id params)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(let ((prefix (plist-get params :prefix)))
(when prefix
(setq-local org-list-description-max-indent 0)
(setq-local org-timer-item-format prefix))
(condition-case err
(org-timer-item)
(error
(message "Error inserting timer item: %S" err))))))))
(defun org-supertag-behavior--timer-show-remaining (node-id _params)
"Show remaining time for timer on NODE-ID.
This function doesn't require any parameters but follows the behavior function
signature for consistency.
Example:
(org-supertag-behavior--timer-show-remaining node-id nil)"
(message "Debug timer-remaining - node=%s" node-id)
(when-let* ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(org-with-point-at pos
(condition-case err
(org-timer-show-remaining-time)
(error
(message "Error showing remaining time: %S" err)))))))
;;------------------------------------------------------------------------------
;; Node Operations Library
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--get-children (node-id)
"Get direct children of node with NODE-ID.
Returns a list of (heading todo-state) for each child node.
Example:
(org-supertag-behavior--get-children \"20240101T123456\")
;; => ((\"Task 1\" \"TODO\") (\"Task 2\" \"DONE\"))"
(message "\n=== Getting Children for Node %s ===" node-id)
(when-let ((pos (org-supertag-db-get-pos node-id)))
(save-excursion
(cond
((markerp pos) (set-buffer (marker-buffer pos)))
((numberp pos) (goto-char pos)))
(org-back-to-heading t)
(let ((parent-level (org-outline-level))
children)
(message "Parent level: %d at heading: %s"
parent-level
(org-get-heading t t t t))
;; collect direct children using org-map-entries
(save-restriction
(org-narrow-to-subtree)
(let ((parent-pos (point))) ;; remember parent node position
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(let* ((current-level (org-outline-level))
(heading (org-get-heading t t t t))
(todo (org-get-todo-state)))
(message "Found entry - Level: %d, Heading: %s, TODO: %s"
current-level heading todo)
;; collect direct children, no need for ID
(when (= current-level (1+ parent-level))
(message "Adding child: %s" heading)
(push (list heading todo) children))))))
(message "Found children: %S" children)
(nreverse children)))))
(defun org-supertag-behavior--find-parent-with-tag (tag-id &optional action-fn)
"Find nearest parent node with TAG-ID and optionally apply ACTION-FN.
TAG-ID should be the tag identifier (e.g. \"task\").
ACTION-FN is called with parent node-id if found.
Example:
(org-supertag-behavior--find-parent-with-tag
\"task\"
(lambda (parent-id)
(message \"Found parent: %s\" parent-id)))"
(save-excursion
(org-back-to-heading t)
(let ((current-heading (org-get-heading t t t t)))
(message "Current heading: %s" current-heading)
(while (and (> (org-outline-level) 1)
(org-up-heading-safe))
(let* ((tags (org-get-tags))
(heading (org-get-heading t t t t)))
(when (member (concat "#" tag-id) tags)
(when-let ((parent-id (org-id-get)))
(when action-fn
(funcall action-fn parent-id))
parent-id)))))))
;;------------------------------------------------------------------------------
;; Archive Management
;;------------------------------------------------------------------------------
(defun org-supertag-behavior--archive-subtree (node-id params)
"Archive subtree for NODE-ID based on PARAMS.
PARAMS is a plist with keys:
- :location : Archive location (file::headline)
- :mark-done : State to set before archiving
- :save-context : List of context info to save
- :find-done : Find and archive done trees