forked from org-roam/org-roam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-roam-reverie.el
133 lines (119 loc) · 4.88 KB
/
org-roam-reverie.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
(defconst org-roam-reverie-property-created-time "CREATED_TIME")
(defconst org-roam-reverie-roam-exclude "ROAM_EXCLUDE")
(defun reverie-init-node ()
"init org-roam headline node"
(interactive)
(progn (org-id-get-create)
(org-entry-put nil org-roam-reverie-property-created-time (iso8601-format (current-time)))))
(defun reverie-init-id-headline ()
"init simple headline node"
(interactive)
(progn (org-id-get-create)
(org-entry-put nil org-roam-reverie-property-created-time (iso8601-format (current-time)))
(org-entry-put nil org-roam-reverie-roam-exclude "t")))
(defun reverie-init-headline-id ()
"process every headline, add id if not exist"
(interactive)
(org-with-point-at 1
(org-map-entries
(lambda ()
(let ((id (org-id-get)))
(when (not id)
(org-id-get-create)))))))
(defun reverie-brother-headline ()
"insert same level headline node"
(interactive)
(progn (org-insert-heading-respect-content)
(org-id-get-create)
(org-entry-put nil org-roam-reverie-property-created-time (iso8601-format (current-time)))))
(defun reverie-father-headline ()
"insert same level headline node"
(interactive)
(progn (org-insert-heading-respect-content)
(org-do-promote)
(org-id-get-create)
(org-entry-put nil org-roam-reverie-property-created-time (iso8601-format (current-time)))))
(defun reverie-child-headline ()
"insert next level headline node"
(interactive)
(progn (org-insert-heading-respect-content)
(org-do-demote)
(org-id-get-create)
(org-entry-put nil org-roam-reverie-property-created-time (iso8601-format (current-time)))))
(defun reverie-child-headline-simple ()
"insert next level headline node"
(interactive)
(progn (org-insert-heading-respect-content)
(org-do-demote)))
(defun reverie-exclude-headline ()
"exclude headline node"
(interactive)
(when (and (org-at-heading-p (org-back-to-heading t)) (org-id-get))
(when (not (cdr (assoc org-roam-reverie-roam-exclude (org-entry-properties))))
(org-entry-put nil org-roam-reverie-roam-exclude "t"))))
(defun reverie-toggle-exclude-headline ()
"toggle exclude headline node"
(interactive)
(when (and (org-at-heading-p (org-back-to-heading t)) (org-id-get))
(if (cdr (assoc org-roam-reverie-roam-exclude (org-entry-properties)))
(org-entry-delete nil org-roam-reverie-roam-exclude)
(org-entry-put nil org-roam-reverie-roam-exclude "t"))))
(defun reverie-collect-data ()
(let ((id (cdr (assoc "ID" (org-entry-properties))))
(title (nth 4 (org-heading-components))))
(if id
(cons id title)
nil)))
(defun org-dblock-write:reverie-insert-children-nodes (param)
"dynamic block function, insert subheading nodes"
(interactive)
(let* ((before-first (org-before-first-heading-p))
(level (if before-first
0
(nth 1 (org-heading-components))))
(scope (if before-first
'file
'tree))
(match (format "+ID={.+}+LEVEL=%d+%s<>{.+}" (+ level 1) org-roam-reverie-roam-exclude)))
(save-excursion
(let ((heading-nodes (org-map-entries #'reverie-collect-data match scope)))
(when (> (length heading-nodes) 0)
(insert (mapconcat (lambda (node) (org-link-make-string
(concat "id:" (car node))
(cdr node)))
heading-nodes "\n")))))))
(defun reverie-insert-dblock ()
"insert insert children nodes dblock"
(interactive)
(progn (newline)
(previous-line)
(insert "#+BEGIN: reverie-insert-children-nodes :parameter1 value1\n")
(newline)
(previous-line)
(insert "#+END")
(org-dblock-update)))
(defun reverie-insert-children-nodes ()
"insert subheading nodes"
(interactive)
(let* ((before-first (org-before-first-heading-p))
(level (if before-first
0
(nth 1 (org-heading-components))))
(scope (if before-first
'file
'tree))
(match (format "+ID={.+}+LEVEL=%d+%s<>{.+}" (+ level 1) org-roam-reverie-roam-exclude)))
(save-excursion
(when (not before-first)
(org-back-to-heading t))
(let ((heading-nodes (org-map-entries #'reverie-collect-data match scope)))
(when (> (length heading-nodes) 0)
(mapcar (lambda (node) (progn (org-end-of-meta-data t)
(newline)
(previous-line)
(insert (org-link-make-string
(concat "id:" (car node))
(cdr node)))
(newline)))
heading-nodes))))))
(provide 'org-roam-reverie)