-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorg-supertag-node.el
executable file
·1101 lines (961 loc) · 40 KB
/
org-supertag-node.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-node.el --- Node management for org-supertag -*- lexical-binding: t; -*-
;;; Commentary:
;; Provides core node functionality including:
;; - Node structure definition
;; - Node operations (create, move, copy, delete)
;; - Node monitoring
;; - Node reference relationships
;; Implementation notes:
;; - Remember to implement org-supertag-node--handle-delete and org-supertag-node--handle-create
;; - Use org-supertag-node-cache
;; - Node updates require two layers: 1. Data layer 2. Entity layer
(require 'org)
(require 'org-element)
(require 'org-id)
(require 'org-supertag-db)
(require 'org-supertag-query)
;;------------------------------------------------------------------------------
;; Database Operations
;;------------------------------------------------------------------------------
(defun org-supertag-node-db-create (id props)
"Create a new node in the database.
ID is the unique node identifier
PROPS are the node properties
Notes:
1. Validates required properties
2. Adds system properties
3. Creates node record
4. Triggers creation event"
;; Validate required properties
(unless (plist-get props :title)
(error "Node must have a title"))
;; Build complete node properties
(let ((node-props (append
(list :id id
:type :node
:created-at (current-time))
props)))
;; Store node
(org-supertag-db-add id node-props)
;; Trigger event
(run-hook-with-args 'org-supertag-node-created-hook id node-props)
;; Return node properties
node-props))
(defun org-supertag-node-db-update (id props)
"Update an existing node in the database.
ID is the node identifier
PROPS are the properties to update"
(when-let ((node (org-supertag-db-get id)))
(let ((new-props nil))
;; 1. Preserve creation time
(setq new-props (list :created-at (plist-get node :created-at)))
;; 2. Add modification time
(setq new-props (plist-put new-props :modified-at (current-time)))
;; 3. Clean input properties (remove system props)
(let ((clean-props (copy-sequence props)))
(while (or (plist-member clean-props :created-at)
(plist-member clean-props :modified-at))
(setq clean-props (org-plist-delete
(org-plist-delete clean-props :created-at)
:modified-at)))
;; 4. Merge properties
(setq new-props (append new-props clean-props)))
;; 5. Update database
(org-supertag-db-add id new-props)
;; 6. Trigger event
(org-supertag-emit 'node:updated id new-props)
;; 7. Return updated properties
new-props)))
(defun org-supertag-node-db-exists-p (id)
"Check if node exists in database.
ID is the unique node identifier
Returns:
- t if node exists and type is :node
- nil if node doesn't exist or type isn't :node"
(when-let ((node (org-supertag-db-get id)))
(eq (plist-get node :type) :node)))
(defun org-supertag-node-db-get-tags (id)
"Get all tags for a node.
ID is the unique node identifier
Returns:
List of tag IDs, or nil if node doesn't exist"
(when (org-supertag-node-db-exists-p id)
(mapcar #'cdr ; Get tag IDs
(org-supertag-db-get-links-by-type :node-tag
:from id))))
(defun org-supertag-node-db-add-tag (node-id tag-id)
"Add a tag to a node.
NODE-ID is the node identifier
TAG-ID is the tag identifier"
(when (and (org-supertag-node-db-exists-p node-id)
(org-supertag-db-exists-p tag-id))
;; Add relationship, note parameter order
(org-supertag-db-link :node-tag
node-id
tag-id
;; Add relationship properties
`(:created-at ,(current-time)))
;; Trigger event
(run-hook-with-args 'org-supertag-node-tag-added-hook
node-id tag-id)))
(defun org-supertag-node-db-remove-tag (node-id tag-id)
"Remove a tag from a node.
NODE-ID is the node identifier
TAG-ID is the tag identifier"
(when (and (org-supertag-node-db-exists-p node-id)
(org-supertag-db-exists-p tag-id))
;; Remove relationship
(org-supertag-db-remove-link :node-tag node-id tag-id)
;; Trigger event
(run-hook-with-args 'org-supertag-node-tag-removed-hook
node-id tag-id)))
(defun org-supertag-node-db--get-candidates ()
"Get list of all referenceable node candidates.
Returns:
Association list of ((title . id) ...) where:
- title is the node title
- id is the unique node identifier
Notes:
1. Traverses all nodes in database
2. Filters for type :node entities
3. Builds title and ID associations"
(let (candidates)
(maphash (lambda (id node)
(when (eq (plist-get node :type) :node)
(push (cons (plist-get node :title) id)
candidates)))
org-supertag-db--object)
;; Sort by title
(sort candidates
(lambda (a b)
(string< (car a) (car b))))))
(defun org-supertag-node-get-tags (node-id)
"Get list of all tag IDs associated with a node.
NODE-ID is the node identifier"
(let ((result nil))
(maphash
(lambda (link-id props)
(when (and (string-prefix-p ":node-tag:" link-id)
(equal (plist-get props :from) node-id))
(push (plist-get props :to) result)))
org-supertag-db--link)
(nreverse result)))
(defun org-supertag-node-get-tag (node-id)
"Get tag ID for NODE-ID.
Returns the first tag ID found for the node."
(when-let* ((node-tags (org-supertag-node-get-tags node-id)))
(car node-tags)))
;;------------------------------------------------------------------------------
;; Node Field Relations
;;------------------------------------------------------------------------------
(defun org-supertag-node-db-set-field (node-id field-id value)
"Set field value for a node.
NODE-ID is the node identifier
FIELD-ID is the field identifier
VALUE is the field value
Notes:
1. Creates/updates field relationship
2. Stores field value
3. Triggers field update event"
;; Add/update field relationship
(org-supertag-db-link
:type :node-field
:from node-id
:to field-id
:value value)
;; Clear cache
(org-supertag-db--cache-remove 'query
(format "node-fields:%s" node-id))
;; Trigger event
(run-hook-with-args 'org-supertag-node-field-updated-hook
node-id field-id value))
(defun org-supertag-node-db-get-fields (node-id)
"Get all field values for a node.
NODE-ID is the node identifier
Returns:
List of field values, each element is (field-id . value)"
(or (org-supertag-db--cache-get 'query
(format "node-fields:%s" node-id))
(when (org-supertag-node-db-exists-p node-id)
(let ((fields (org-supertag-db-get-links-by-type
:node-field :from node-id)))
(org-supertag-db--cache-set 'query
(format "node-fields:%s" node-id)
fields)
fields))))
;;------------------------------------------------------------------------------
;; Operation Functions
;;------------------------------------------------------------------------------
(defun org-supertag-node-sync-at-point ()
"Synchronize current node with database."
(when (org-at-heading-p)
(let* ((node-id (org-id-get-create))
(old-node (org-supertag-db-get node-id))
(title (org-get-heading t t t t))
(file-path (buffer-file-name))
(pos (point))
(level (org-outline-level))
(olp (org-get-outline-path))
;; 获取节点内容
(content (save-excursion
(let* ((begin (progn
(org-end-of-meta-data t)
(point)))
(end (org-entry-end-position)))
(buffer-substring-no-properties begin end)))))
(message "Debug - title: %s, level: %d, pos: %d" title level pos)
;; 更新所有属性,包括内容
(let ((props (list :type :node
:id node-id
:title title
:file-path file-path
:pos pos
:level level
:olp olp
:content content
:ref-to (when old-node
(plist-get old-node :ref-to))
:ref-from (when old-node
(plist-get old-node :ref-from))
:ref-count (when old-node
(plist-get old-node :ref-count))
:created-at (or (and old-node
(plist-get old-node :created-at))
(current-time)))))
(message "Debug - props: %S" props)
;; Update database
(org-supertag-db-add node-id props)
;; Return node ID
node-id))))
(defun org-supertag-node-get-props ()
"Get properties of current node."
(org-supertag-db--parse-node-at-point))
(defun org-supertag-node-sync-display ()
"Synchronize node display state.
Process:
1. Ensure node has ID property
2. Update fields in properties drawer
3. Update tag display
4. Update todo state display
Notes:
- Keeps display state consistent with database
- Does not trigger database updates
- Only updates display-related properties"
(when-let ((node-id (org-id-get)))
(when-let ((node (org-supertag-db-get node-id)))
;; Update properties drawer
(org-set-property "ID" node-id)
;; Update other display states
(let ((tags (plist-get node :tags))
(todo (plist-get node :todo)))
(when tags
(org-set-tags tags))
(when todo
(org-todo todo))))))
(defun org-supertag-node-move (node-id target-file &optional target-level target-point)
"Move node to target file.
NODE-ID is the node identifier
TARGET-FILE is the target file path
TARGET-LEVEL is the target heading level
TARGET-POINT is buffer position to insert (nil means end of file)
Returns:
- t if move successful
- nil if move failed"
(when-let* ((content (org-supertag-get-node-content node-id)))
;; 1. Delete node from source
(when (org-supertag-delete-node-content node-id)
;; 2. Insert to target and update database
(with-current-buffer (find-file-noselect target-file)
(save-excursion
;; Move to target position
(if target-point
(goto-char target-point)
(goto-char (point-max)))
;; Ensure we're at the beginning of a line
(beginning-of-line)
;; Temporarily disable folding
(let ((org-fold-core-style 'overlays)) ; 使用 overlay 样式避免一些问题
;; Insert with proper level adjustment
(let ((adjusted-content
(if (and target-level (> target-level 0))
(condition-case err
(org-supertag-adjust-node-level content target-level)
(error
(message "Level adjustment failed: %s" (error-message-string err))
content))
content)))
;; Ensure proper spacing before insertion
(unless (or (bobp) (looking-back "\n\n" 2))
(insert "\n"))
;; Insert content
(let ((insert-point (point)))
(insert adjusted-content)
;; Ensure proper spacing after insertion
(unless (looking-at "\n")
(insert "\n"))
;; Move back to inserted heading and sync node
(goto-char insert-point)
(when (re-search-forward (format "^[ \t]*:ID:[ \t]+%s[ \t]*$" node-id) nil t)
(org-back-to-heading t)
;; 确保正确展开新插入的节点
(org-show-subtree)
(org-supertag-node-sync-at-point))
;; Save buffer
(save-buffer)
t))))))))
(defun org-supertag-node-move-node ()
"Interactive command to move current node to another location.
The command will:
1. Check if current position is in a valid node, if not create one
2. Ask user to select target file
3. Ask user to select insert position in target file
4. Move the node to target location"
(interactive)
(unless (org-at-heading-p)
(user-error "Must be on a heading"))
;; Get or create node ID
(let ((node-id (or (org-id-get)
(progn
(org-supertag-node-create)
(org-id-get)))))
(unless (org-supertag-db-get node-id)
(user-error "Current node not found in database"))
;; Get target file and position
(let* ((target-file (read-file-name "Move to file: "))
(insert-pos (org-supertag-query--get-insert-position target-file))
(target-pos (car insert-pos))
(level-adj (cdr insert-pos)))
(if (org-supertag-node-move node-id target-file level-adj target-pos)
(message "Node moved successfully")
(message "Failed to move node")))))
(defun org-supertag-find-file-content-start ()
"Find the position where actual content should start in current buffer.
This skips over the org-mode file header (#+TITLE:, #+AUTHOR:, etc.)
and returns the position where new content should be inserted."
(save-excursion
(goto-char (point-min))
(while (and (not (eobp))
(looking-at "^#\\+\\|^$"))
(forward-line 1))
(point)))
(defun org-supertag-adjust-node-level (content target-level)
"Adjust heading level of node content.
CONTENT is the node content string
TARGET-LEVEL is the desired heading level (integer)
Returns adjusted content string with proper heading levels."
(with-temp-buffer
(org-mode)
(insert content)
(goto-char (point-min))
(when (and target-level (> target-level 0))
;; Find and get current level
(when (re-search-forward "^\\(\\*+\\)\\( .*\\)" nil t)
(let* ((current-level (length (match-string 1)))
(let* ((level-diff (- target-level current-level)))
(unless (zerop level-diff)
(goto-char (point-min))
;; Adjust all heading levels
(while (re-search-forward "^\\(\\*+\\)\\( .*\\)" nil t)
(let* ((stars (match-string 1))
(text (match-string 2))
(current-stars-len (length stars))
(new-level (+ current-stars-len level-diff))
(new-stars (make-string (max 1 new-level) ?*)))
(replace-match (concat new-stars text)))))))
(buffer-string))))))
(defun org-supertag--move-node-and-link (node-id target-file &optional target-level)
"Internal function to move a node and create link.
NODE-ID is the ID of the node to move.
TARGET-FILE is the destination file path.
TARGET-LEVEL is the optional target heading level."
;; 1. 获取源节点信息
(let* ((node (org-supertag-db-get node-id))
(source-file (or (plist-get node :file-path)
(buffer-file-name)))
(source-pos (or (plist-get node :pos)
(point)))
(source-element (with-current-buffer (find-file-noselect source-file)
(save-excursion
(goto-char source-pos)
(org-element-at-point))))
(title (org-element-property :raw-value source-element))
(content (buffer-substring (org-element-property :contents-begin source-element)
(org-element-property :contents-end source-element)))
(properties (org-element-property :PROPERTY source-element))
(level (or target-level
(org-element-property :level source-element)
1)))
;; 2. 在目标位置创建完整节点
(with-current-buffer (find-file-noselect target-file)
(save-excursion
(let* ((insert-pos (org-supertag-query--get-insert-position target-file))
(target-pos (car insert-pos)))
(goto-char target-pos)
;; 插入完整节点内容
(insert (make-string level ?*)
" " title "\n"
content "\n")
(save-buffer))))
;; 3. 修改源节点为链接
(with-current-buffer (find-file-noselect source-file)
(save-excursion
(goto-char source-pos)
(org-narrow-to-subtree)
(delete-region (point) (point-max))
(beginning-of-line) ;; 移动到行头
(delete-region (point) (line-end-position)) ;; 清理从行头到行末的内容
(insert (make-string level ?*)
" " (org-link-make-string (concat "id:" node-id) title) "\n")
(widen)
(save-buffer)))))
(defun org-supertag-move-node-and-link ()
"Interactive command to move a node and create link.
Prompts user for node and target file from a list of Org files."
(interactive)
;; 1. 获取当前节点信息
(unless (org-id-get)
(org-id-get-create))
(let* ((node-id (org-id-get))
(title (org-get-heading t t t t))
(target-file (completing-read
"Move node to file: "
(org-supertag--get-org-file-list) ;; 获取 Org 文件列表
nil t)) ;; 强制用户选择一个文件
(target-level (when current-prefix-arg
(read-number "Target level: " (org-outline-level)))))
;; 2. 调用内部函数
(org-supertag--move-node-and-link node-id target-file target-level)
;; 3. 显示成功消息
(message "Moved node '%s' to %s" title target-file)))
(defun org-supertag--get-org-file-list ()
"Return a list of Org files in the specified directory.
The directory is determined by `org-directory' or `default-directory'."
(let* ((dir (or (if (boundp 'org-directory)
org-directory
default-directory)))
(files (directory-files-recursively dir "\.org$" t))) ;; 添加 t 参数以确保返回完整路径
;; 过滤掉备份文件和隐藏文件
(seq-filter (lambda (file)
(and (not (string-match-p "/\\.#" file)) ;; 排除备份文件
(not (string-match-p "/\\.org$" file)) ;; 排除隐藏文件
(file-regular-p file))) ;; 确保是文件而不是目录
files)))
;;------------------------------------------------------------------------------
;; Node Commands
;;------------------------------------------------------------------------------
(defun org-supertag-node-create ()
"Create a new supertag node.
Two use cases:
1. At blank position - Create new heading with ID
2. At existing heading without ID - Add ID to heading
Returns:
- Node ID on success
- Throws error on failure"
(interactive)
(let ((at-heading (org-at-heading-p)))
(cond
;; Case 1: At existing heading
((and at-heading (org-id-get))
(user-error "Heading already has a node"))
;; Case 2: At existing heading without ID
(at-heading
(org-supertag-node-sync-at-point))
;; Case 3: At blank position
(t
(let* ((title (read-string "Node title: "))
;; Ensure we're at beginning of line
(_ (beginning-of-line))
;; Get current level or default to 1
(current-level (if (org-at-heading-p)
(org-outline-level)
1)))
;; Insert new heading
(insert (make-string current-level ?*) " " title "\n")
;; Move back to heading
(forward-line -1)
;; Create node
(org-supertag-node-sync-at-point))))))
(defun org-supertag-node-delete ()
"Delete current node and all its associated data.
This includes:
1. Delete the org headline
2. Remove associated tags
3. Remove associated fields (properties)
4. Clean up database entries
5. Remove all references to this node"
(interactive)
(let ((node-id (org-id-get)))
(unless node-id
(user-error "Current position is not in a node"))
(unless (org-supertag-db-get node-id)
(user-error "Current node is not registered in database"))
(when (yes-or-no-p "Really delete this node and all its data? ")
;; 1. Get node info before deletion
(let* ((node (org-supertag-db-get node-id))
(tags (org-supertag-node-get-tags node-id))
(ref-from (plist-get node :ref-from)))
;; 2. Remove all tags
(dolist (tag-id tags)
(org-supertag-tag--remove tag-id node-id))
;; 3. Remove all references to this node
(dolist (ref-node-id ref-from)
(when-let* ((ref-file (plist-get (org-supertag-db-get ref-node-id) :file-path)))
(with-current-buffer (find-file-noselect ref-file)
(save-excursion
(goto-char (point-min))
(while (re-search-forward (format "\\[\\[id:%s\\]\\[[^]]*\\]\\]" node-id) nil t)
(delete-region (match-beginning 0) (match-end 0)))
(save-buffer)))))
;; 4. Delete the headline content including properties
(org-back-to-heading t)
(let* ((element (org-element-at-point))
(begin (org-element-property :begin element))
(end (org-element-property :end element)))
;; Delete the entire heading including properties
(delete-region begin end)
(when (looking-at "\n") (delete-char 1)))
;; 5. Remove from database
(remhash node-id org-supertag-db--object)
;; 6. Clear database caches
(org-supertag-db--cache-remove 'entity node-id)
(org-supertag-db--cache-remove 'query (format "type:%s" :node))
(org-supertag-db--cache-remove 'query (format "node-tags:%s" node-id))
(org-supertag-db--cache-remove 'query (format "node-fields:%s" node-id))
(org-supertag-db--cache-remove 'query (format "node-refs:%s" node-id))
;; 7. Save changes
(save-buffer)
(org-supertag-db-save)
;; 8. Run hooks
(run-hook-with-args 'org-supertag-node-deleted-hook node-id)
(message "Node deleted: %s" node-id)))))
(defun org-supertag-node--ensure-id-system ()
"Ensure org-id system is properly initialized."
(require 'org-id)
(unless (and (boundp 'org-id-locations)
(hash-table-p org-id-locations))
(setq org-id-locations (make-hash-table :test 'equal))
(when (file-exists-p org-id-locations-file)
(org-id-locations-load))))
(defun org-supertag--create-node (node-id)
"Create a new node with NODE-ID."
(org-supertag-node--ensure-id-system)
(save-excursion
(unless (org-at-heading-p)
(org-back-to-heading t))
(let ((current-id (org-id-get)))
(when (and current-id (not (equal current-id node-id)))
(error "Position mismatch in create-node: Expected %s but at %s"
node-id current-id)))
(let ((props (org-supertag-db--parse-node-at-point)))
(unless props
(error "Failed to parse node properties at point"))
(org-supertag-db-add node-id props))))
(defun org-supertag-node-get-props-at-point ()
"Get node properties at current point.
Returns property list if successful, nil if failed."
(condition-case err
(progn
(unless (org-at-heading-p)
(error "Not at a heading"))
(org-supertag-db--parse-node-at-point))
(error
(message "Failed to get node properties: %s" (error-message-string err))
nil)))
(defun org-supertag-node-update ()
"Update node at current position.
Use cases:
1. Manual update after node content changes
2. Force node data synchronization
Prerequisites:
1. Cursor must be on a heading
2. Heading must have an associated node ID
Returns:
- Node ID on success
- Throws error on failure"
(interactive)
(unless (org-at-heading-p)
(user-error "Cursor must be on a heading"))
(let ((id (org-id-get)))
(unless id
(user-error "Heading has no associated node"))
;; Get node properties
(let ((props (org-supertag-node-get-props-at-point)))
(unless props
(error "Unable to get node properties"))
;; Update database
(condition-case err
(progn
(org-supertag-node-db-update id props)
(message "Node updated successfully: %s" id)
id)
(error
(message "Node update failed: %s" (error-message-string err))
(signal (car err) (cdr err)))))))
(defun org-supertag-node-db-update (id props)
"Update node in database.
ID is the node identifier
PROPS is the property list to update with"
(unless (and id props)
(error "Invalid input: id=%s props=%s" id props))
(org-supertag-db-add id props))
(defun org-supertag-node-remove-tag (node-id tag-id)
"Remove tag association from a node.
NODE-ID is the node identifier
TAG-ID is the tag identifier"
(let ((link-id (format ":node-tag:%s->%s" node-id tag-id)))
;; Remove association from database
(remhash link-id org-supertag-db--link)
;; Clear caches
(org-supertag-db--cache-remove 'query (format "node-tags:%s" node-id))
(org-supertag-db--cache-remove 'query (format "tag-nodes:%s" tag-id))
;; Schedule save
(org-supertag-db-save)))
(defun org-supertag-node-at-valid-heading-p ()
"Check if point is at a valid heading for tag operations.
Returns t if:
1. Point is at a heading
2. Heading is not commented out
3. Heading is not archived
4. Heading is not in COMMENT tree"
(let ((at-heading (org-at-heading-p)))
(message "Checking heading validity:")
(message " At heading: %s" (if at-heading "yes" "no"))
(when at-heading
(message " Heading text: '%s'" (org-get-heading t t t t))
(message " Point: %d" (point))
(message " In commented heading: %s"
(if (org-in-commented-heading-p) "yes" "no"))
(message " In archived heading: %s"
(if (org-in-archived-heading-p) "yes" "no")))
at-heading))
(defun org-supertag-node--format-path (file-path olp title)
"Format node path for display.
FILE-PATH is the path to the file
OLP is the outline path list
TITLE is the node title"
(let* ((file-name (if file-path
(file-name-nondirectory file-path)
"<no file>"))
(path-parts (append (list file-name)
(or olp nil)
(list (or title "<no title>")))))
(mapconcat #'identity
(remove nil path-parts)
" / ")))
(defun org-supertag-node--collect-nodes ()
"Collect all nodes with their paths.
Returns a list of (node-id . display-string) pairs."
(let (nodes)
(maphash
(lambda (id props)
(when (eq (plist-get props :type) :node)
(let* ((file-path (plist-get props :file-path))
(title (or (plist-get props :title) "<untitled>"))
(olp (plist-get props :olp))
;; 确保所有字符串属性都是纯文本
(clean-title (if (stringp title)
(substring-no-properties title)
"<invalid title>"))
(display (org-supertag-node--format-path file-path olp clean-title)))
(push (cons id display) nodes))))
org-supertag-db--object)
;; 按显示字符串排序
(sort nodes (lambda (a b)
(string< (cdr a) (cdr b))))))
;;;###autoload
(defun org-supertag-node-find ()
"Find and jump to a node using completion.
Displays nodes with their full paths in format:
filename / outline-path / title"
(interactive)
(let* ((nodes (org-supertag-node--collect-nodes))
(candidates (mapcar #'cdr nodes))
(choice (completing-read "Find node: " candidates nil t))
(node-id (car (rassoc choice nodes))))
(if node-id
(if (org-supertag-query--find-node node-id)
(message "Jumped to node: %s" choice)
(message "Error: Could not find node location"))
(message "No matching node found"))))
;;;###autoload
(defun org-supertag-node-find-other-window ()
"Like `org-supertag-node-find' but display node in other window."
(interactive)
(let* ((nodes (org-supertag-node--collect-nodes))
(candidates (mapcar #'cdr nodes))
(choice (completing-read "Find node: " candidates nil t))
(node-id (car (rassoc choice nodes))))
(when node-id
(when-let* ((props (org-supertag-db-get node-id))
(file-path (plist-get props :file-path))
((file-exists-p file-path)))
;; Open file in other window
(find-file-other-window file-path)
;; Find and show node
(widen)
(when-let ((marker (org-id-find-id-in-file node-id file-path)))
(goto-char (cdr marker))
(org-show-entry)
(org-show-children)
(recenter)
(message "Jumped to node: %s" choice))))))
;;------------------------------------------------------------------------------
;; Node Relations
;;------------------------------------------------------------------------------
(defun org-supertag-node--in-node-p ()
"Check if current position is within a valid org-supertag node.
Valid node requirements:
1. Current position can reach a heading
2. Heading has an ID property
3. ID exists in database
Returns:
- (id . title) if in valid node
- nil if not in valid node, with appropriate message"
(condition-case nil
(save-excursion
(org-back-to-heading t)
(let ((id (org-entry-get nil "ID"))
(title (org-get-heading t t t t)))
(cond
;; No ID
((null id)
(message "Current node not created, use org-supertag-node-create command")
nil)
;; ID not in database
((not (org-supertag-node-db-exists-p id))
(message "Node ID not registered in database, use org-supertag-node-create command")
nil)
;; Valid node
(t (cons id title)))))
;; Not under any heading
(error
(message "Current position not within any node")
nil)))
(defun org-supertag-node-db-add-reference (from-id to-id)
"Add reference relationship between nodes.
FROM-ID is the source node
TO-ID is the target node
Notes:
1. Checks node existence
2. Creates reference association
3. Triggers event"
(when (and (org-supertag-node-db-exists-p from-id)
(org-supertag-node-db-exists-p to-id))
;; Add reference relationship
(org-supertag-db-link
:type :node-ref
:from from-id
:to to-id)
;; Clear cache
(org-supertag-db--cache-remove 'query
(format "node-refs:%s" from-id))
;; Trigger event
(run-hook-with-args 'org-supertag-node-reference-added-hook
from-id to-id)))
;; 3. Relationship queries
(defun org-supertag-node-db-get-reference (node-id &optional direction)
"Get node reference relationships.
NODE-ID is the node identifier
DIRECTION specifies reference direction:
- 'to get nodes referenced by this node
- 'from get nodes referencing this node
- nil get all related references
Returns:
List of node IDs"
(or (org-supertag-db--cache-get 'query
(format "node-refs:%s:%s" node-id direction))
(when-let ((node (org-supertag-db-get node-id)))
(let ((refs (pcase direction
('to (plist-get node :refs-to))
('from (plist-get node :refs-from))
(_ (append (plist-get node :refs-to)
(plist-get node :refs-from))))))
(org-supertag-db--cache-set 'query
(format "node-refs:%s:%s" node-id direction)
refs)
refs))))
;; 4. Relationship cleanup
(defun org-supertag-node-db-remove-reference (from-id to-id)
"Remove reference relationship between nodes.
FROM-ID is the source node
TO-ID is the target node"
(when-let* ((from-node (org-supertag-db-get from-id))
(refs-to (plist-get from-node :refs-to)))
;; Use database reference handling mechanism
(org-supertag-db--handle-refs-updated
from-id
(delete to-id refs-to))))
;;------------------------------------------------------------------------------
;; Node Reference Functions
;;------------------------------------------------------------------------------
(defun org-supertag-node--insert-reference (node-id)
"Insert node reference at current position and update relationships.
NODE-ID is the referenced node's identifier"
(let* ((target-node (org-supertag-db-get node-id))
(target-title (plist-get target-node :title))
(link-text (format "[[id:%s][%s]]" node-id target-title)))
;; 1. Insert link
(insert link-text)
;; 2. Update reference relationships
(let* ((current-node-id (org-id-get))
(current-node (org-supertag-db-get current-node-id)))
;; 2.1 Update current node's ref-to
(let* ((current-refs (or (plist-get current-node :ref-to) nil)))
(setq current-refs (cl-adjoin node-id current-refs :test #'string=))
(org-supertag-db-add
current-node-id
(append current-node
(list :ref-to current-refs
:ref-count (length current-refs)))))
;; 2.2 Update target node's ref-from
(let* ((target-refs (or (plist-get target-node :ref-from) nil)))
(setq target-refs (cl-adjoin current-node-id target-refs :test #'string=))
(org-supertag-db-add
node-id
(append target-node
(list :ref-from target-refs
:ref-count (length target-refs))))))))
(defun org-supertag-node-add-reference ()
"Add node reference.
Insert reference to selected node at current position and update relationships.
Displays nodes with their full paths in format:
filename / outline-path / title"
(interactive)
(unless (org-supertag-node--in-node-p)
(user-error "Must be within a node"))
(let* ((nodes (org-supertag-node--collect-nodes))
(candidates (mapcar #'cdr nodes))
(choice (completing-read "Reference node: " candidates nil t))
(node-id (car (rassoc choice nodes))))
(when node-id
;; Insert reference
(org-supertag-node--insert-reference node-id)
;; Update reference relationships
(org-supertag-db--parse-node-all-ref)
(message "Added reference to: %s" choice))))
(defun org-supertag-node-remove-reference (node-id)
"Remove reference to specified node.
NODE-ID is the referenced node identifier."
(interactive
(let* ((refs (org-supertag-node--collect-reference-id-title))
(selected (completing-read
"Remove reference: "
(mapcar #'car refs)
nil t)))
(list (cdr (assoc selected refs)))))
(let ((start (org-entry-beginning-position))
(end (org-entry-end-position))
(removed 0))
;; 1. delete link
(save-excursion
(goto-char start)
(while (< (point) end)
(let ((element (org-element-context)))
(when (and (eq (org-element-type element) 'link)
(equal (org-element-property :type element) "id")
(equal (org-element-property :path element) node-id))
(delete-region (org-element-property :begin element)
(org-element-property :end element))
(cl-incf removed)))
(forward-char)))
;; 2. update reference
(when (> removed 0)
(let* ((current-node-id (org-id-get))
(current-node (org-supertag-db-get current-node-id))
(target-node (org-supertag-db-get node-id)))
;; 2.1 update current node ref-to
(let* ((current-refs (plist-get current-node :ref-to))
(new-refs (delete node-id current-refs)))
(org-supertag-db-add
current-node-id
(append
(list :type :node)
(plist-put
(plist-put current-node :ref-to new-refs)
:ref-count (length new-refs)))))
;; 2.2 update target node ref-from
(let* ((target-refs (plist-get target-node :ref-from))
(new-target-refs (delete current-node-id target-refs)))
(org-supertag-db-add
node-id