-
Notifications
You must be signed in to change notification settings - Fork 4
/
obvz.el
491 lines (350 loc) · 13.4 KB
/
obvz.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
(require 'cl)
(require 'json)
(require 'dbus)
(defun obvz-flatten-list (mylist)
"flatten list function because somehow only there in emacs 27?"
(cond
((null mylist) nil)
((atom mylist) (list mylist))
(t
(append (obvz-flatten-list (car mylist)) (obvz-flatten-list (cdr mylist))))))
(defun obvz-create-children-links (parent child)
"""basic hierarchical function"""
(concat (obvz-get-node-name parent) " -- " (obvz-get-node-name child) " -- isa"))
(defun obvz-is-node-headline (node)
"check whether node is headline entry"
(let ((node-state nil))
(if (equal (type-of node) 'cons)
(setq node-state t)
(setq node-state nil))
node-state))
(defun obvz-get-node-name (node)
"get actual name of node (not identical when headline entry)"
(let ((node-name ""))
(if (obvz-is-node-headline node)
(setq node-name (nth 1 node))
(setq node-name node))
node-name))
(defun obvz-loop-over-upper-level-nodes (parent)
"""get nodes and hierarchical links of parent node, now also for headline entries"""
(let ((childrenx (org-brain-children parent))
(node-names)
(res ()))
(push childrenx res)
;; (setq childrenx2 (mapcar 'obvz-get-node-name childrenx))
(cl-delete-if (lambda (k) (obvz-is-node-cls-node k)) childrenx)
;; prevents hierarchical links from nodes to cls_ nodes
;; also need other way: prevent cls_nodes as parents of other nodes
;; if class node: push empty list to res
(if (obvz-is-node-cls-node parent)
(push () res)
(push (mapcar (lambda (child) (funcall #'obvz-create-children-links parent child)) childrenx) res))
res))
(defun obvz-is-node-cls-node (node)
"check if node is a class node (now can check both file and headline entries"
(if (obvz-is-node-headline node)
(obvz-is-nodename-cls-node (nth 1 node))
(obvz-is-nodename-cls-node node)))
(defun obvz-is-nodename-cls-node (node)
"""checks if the name of a node is a class (cls_)"""
(let ((node-state nil))
(if (> (length node) 4)
(if (equal (substring node 0 4) "cls_")
(setq node-state t)))
node-state))
(defun children-specific-depth-let (node depth)
"retrieves children and hierarchical links of node $node to level $depth"
(let (
(nodes-upper-level (list node))
(all-nodes ())
;; (all-nodes (list node))
(all-links ())
(temp-res ())
(all-res ())
)
;; push node to all nodes when not class node
(when (not (obvz-is-node-cls-node node))
(push node all-nodes))
;; for each depth step
(dotimes (i depth)
;; temp-res: contains 2 lists (nodes/links) for each upper-level-node
;; idk it's a bit ugly
;; but idk how to work without temp-res
(push (mapcar 'loop-over-upper-level-nodes nodes-upper-level) temp-res)
;; push nodes/links to local vars
(mapc (lambda (nodex) (push nodex all-nodes)) (obvz-flatten-list (mapcar 'cdr (car temp-res))))
(mapc (lambda (linkx) (push linkx all-links)) (obvz-flatten-list (mapcar 'car (car temp-res))))
(setq nodes-upper-level (obvz-flatten-list (mapcar 'cdr (car temp-res))))
(setq temp-res ())
)
(push all-links all-res)
(push all-nodes all-res)
all-res
)
)
(defun obvz-children-specific-depth (node depth)
"retrieves children and hierarchical links of node $node to level $depth"
(let (
(nodes-upper-level (list node))
(all-nodes ())
;; (all-nodes (list node))
(all-links ())
(temp-res ())
(all-res ()))
;; push node to all nodes when not class node
(when (not (obvz-is-node-cls-node node))
(push node all-nodes))
;; for each depth step
(dotimes (i depth)
(setq temp-res (mapcar 'obvz-loop-over-upper-level-nodes nodes-upper-level))
(setq temp-nodes (mapcar 'cdr temp-res))
(setq temp-nodes2 (mapcar 'car temp-nodes))
;; need to deal with lists of lists, but can't aggressively flatten
(mapcar (lambda (i) (mapcar (lambda (k) (push k all-nodes)) i)) temp-nodes2)
;; deal with links
(mapc (lambda (linkx) (push linkx all-links)) (obvz-flatten-list (mapcar 'car temp-res)))
;; (setq nodes-upper-level (obvz-flatten-list (mapcar 'cdr (car temp-res))))
(setq nodes-upper-level ())
(mapcar (lambda (i) (mapcar (lambda (k) (push k nodes-upper-level)) i)) temp-nodes2)
(setq nodes-upper-level2 nodes-upper-level)
)
(push all-links all-res)
(push all-nodes all-res)
all-res))
(defun obvz-get-friend-links (nodes)
"""get links between nodes"""
(let ((friend-links ())
(friend-nodes ())
(node ())
(friends ())
(friend ())
(edge-annot)
(res ())
)
(while nodes
(setq node (car nodes))
;; (print node)
(setq friends (org-brain-friends node))
(while friends
(setq friend (car friends))
(setq edge-annot (org-brain-get-edge-annotation node friend))
;; check edge annotation requirement
(if (equal obvz-only-use-annotated-edges t)
(progn
(if (not (equal edge-annot nil))
(progn
(push (concat (obvz-get-node-name node) " -- " (obvz-get-node-name friend) " -- " edge-annot) friend-links)
(push friend friend-nodes))))
;; if requirement nil: use friend regardless of annotation
(progn
(push (concat (obvz-get-node-name node) " -- " (obvz-get-node-name friend) " -- " edge-annot) friend-links)
(push friend friend-nodes)))
(setq friends (cdr friends)))
(setq nodes (cdr nodes)))
(push friend-links res)
(push friend-nodes res)
;; friend-links
res))
(defun obvz-create-graph-dict (obvz-include-node-texts)
"""generate graph dict from pins"""
(interactive)
(let (
(rel-nodes org-brain-pins)
(total-nodes ())
(total-links ())
(node-res ())
(rel-node ())
(uniq-nodes ())
(uniq-nodes-hierarchy ())
(friend-res ())
(class-nodes ())
(friend-links ())
(friend-nodes ())
(all-links ())
(link-string ())
(node-texts ())
(node-text-alist ())
(graph-dict ())
(current-node ())
)
;; get hierarchical relations
(while rel-nodes
(setq rel-node (car rel-nodes))
;; (print rel-node)
(setq node-res (obvz-children-specific-depth rel-node 8))
;; (push (car node-res) total-nodes)
(mapc (lambda (i) (push i total-nodes)) (car node-res))
(push (cdr node-res) total-links)
(setq rel-nodes (cdr rel-nodes))
)
;; (print total-nodes)
;; (setq uniq-nodes (remove-duplicates (flatten-list total-nodes)))
(setq uniq-nodes (delete-dups total-nodes))
;; (cl-delete-if (lambda (k) (string-match-p "cls_" k)) uniq-nodes)
(cl-delete-if (lambda (k) (obvz-is-node-cls-node k)) uniq-nodes)
;; (print uniq-nodes)
;; (setq uniq-nodes (cl-delete-if (lambda (k) (string-match-p "cls_" k)) uniq-nodes))
;; handle friend links
(setq friend-res (obvz-get-friend-links uniq-nodes))
(setq friend-links (cdr friend-res))
(push friend-links total-links)
(setq all-links (obvz-flatten-list total-links))
(setq link-string (mapconcat 'identity all-links ";"))
;; handle friend nodes
(setq friend-nodes (car friend-res))
(mapc (lambda (i) (push i uniq-nodes)) friend-nodes)
;; (setq uniq-nodes (delete-dups (flatten-list (append uniq-nodes friend-nodes))))
(delete-dups uniq-nodes)
;; (setq uniq-nodes (remove-duplicates (flatten-list (append uniq-nodes friend-nodes))))
;; delete cls_nodes from being there if alone
;; not clear if effective: what if referred to as friend?
;; (message "all nodes there")
;; (setq node-string (mapconcat 'identity uniq-nodes ";"))
;; include (or not) node texts
(if (equal obvz-include-node-texts t)
(progn
(setq node-texts (mapcar 'org-brain-text uniq-nodes))
(setq node-text-alist (mapcar* #'cons (mapcar 'obvz-get-node-name uniq-nodes) node-texts))
)
(setq node-text-alist (mapcar* #'cons (mapcar 'obvz-get-node-name uniq-nodes) (make-list (length uniq-nodes) "")))
)
;; (message "node texts there")
;; get current node
(if (equal obvz-highlight-current-node t)
(setq current-node (obvz-get-node-name (org-brain-entry-at-pt)))
(setq current-node nil))
(setq graph-dict
`(("links" . ,all-links)
("nodes" . ,(mapcar 'obvz-get-node-name uniq-nodes))
("cur_node" . ,current-node)
("node_texts" . ,node-text-alist)
("draw_arrow_toggle" . ,obvz-draw-arrow)
("layout_type" . ,obvz-layout-type)
("use_edge_labels" . ,obvz-use-edge-labels)
)
)
graph-dict
)
)
;; data structure: dict with key node, value text
;; is itself dict in graph_dict
(defun obvz-switch-node-text-inclusion()
"toggle node text inclusion on or off"
(interactive)
(if (equal obvz-include-node-texts t)
(setq obvz-include-node-texts nil)
(setq obvz-include-node-texts t)
)
(obvz-update-graph)
)
(defun obvz-toggle-edge-labels ()
"toggle node labels on/off"
(interactive)
(if (equal obvz-use-edge-labels t)
(setq obvz-use-edge-labels nil)
(setq obvz-use-edge-labels t))
(obvz-update-graph-hard))
(defun obvz-reposition-nodes()
"redraw layout, either soft (apply forces to current layout) or hard (from random starting positions)"
(interactive)
(let ((called-prefix current-prefix-arg)
(obvz-redraw-alist ())
)
(if (equal called-prefix nil)
(setq obvz-redraw-alist '(("redraw" . "soft")))
(setq obvz-redraw-alist '(("redraw" . "hard")))
)
;; (zmq-send sock (json-encode-alist obvz-redraw-alist))
(obvz-send-to-python (json-encode-alist obvz-redraw-alist))
)
)
(defun obvz-update-graph ()
"update graph; intended for changed node text and after restarting"
(interactive)
(setq obvz-current-config (obvz-create-graph-dict obvz-include-node-texts))
(if (not (equal obvz-current-config obvz-most-recent-config))
(progn
(setq obvz-most-recent-config obvz-current-config)
;; (zmq-send sock (json-encode-alist obvz-current-config))
(obvz-send-to-python (json-encode-alist obvz-current-config))
)
)
)
(defun obvz-update-graph-hard ()
"update graph in any case (maybe necessary); intended for changed node text and after restarting"
(interactive)
(setq obvz-current-config (obvz-create-graph-dict obvz-include-node-texts))
(setq obvz-most-recent-config obvz-current-config)
(obvz-send-to-python (json-encode-alist obvz-current-config))
)
(defun obvz-start ()
(interactive)
(add-hook 'org-brain-after-visualize-hook 'obvz-update-graph)
(shell-command (mapconcat 'identity `("cd" ,obvz-dir "&&" ,obvz-python-version "obvz.py" ,obvz-connection-type ,obvz-layout-type "&") " "))
(setq obvz-most-recent-config ()))
(defun obvz-stop ()
(interactive)
(remove-hook 'org-brain-after-visualize-hook 'obvz-update-graph))
(defun obvz-export ()
"""exports the currently visualized graph to dot or svg."""
(interactive)
(let ((obvz-export-format (completing-read "Set export format: " '("dot" "svg")))
(obvz-export-file (expand-file-name (read-file-name "Export file: ")))
(export-dict ()))
(setq export-dict `(("export_type" . ,obvz-export-format)
("export_file" . ,obvz-export-file)))
(obvz-send-to-python (json-encode `(("export" . ,export-dict))))))
(defun obvz-set-layout-type ()
"set the layout algorithm; choices are graphviz dot and a custom force-directed algorithm"
(interactive)
(setq obvz-layout-type (completing-read "Set layout type: " '("dot" "force")))
(obvz-send-to-python (json-encode `(("layout_type" . ,obvz-layout-type))))
)
(setq obvz-setting-options-alist
`(("font size" .
(
("python-name" . "font_size")
("entry-func" . 'read-number)
))))
(defun obvz-change-setting ()
"change settings in python"
(interactive)
(let ((setting-dict `(("font_size" . ,(read-number "new font size: ")))))
(obvz-send-to-python (json-encode setting-dict))))
;; connection functions
(defun obvz-send-to-python (dict-str-to-send)
"""general function to send stuff python, input is json-encoded dict string"""
(when (equal obvz-connection-type "dbus")
(obvz-dbus-caller "echo" dict-str-to-send))
(when (equal obvz-connection-type "zmq")
(zmq-send sock dict-str-to-send))
)
(defun obvz-dbus-caller (method &rest args)
"call the tomboy method METHOD with ARGS over dbus"
(apply 'dbus-call-method
:session ; use the session (not system) bus
"com.qtpad.dbus" ; service name
"/cli" ; path name
"com.qtpad.dbus" ; interface name
method args))
(add-hook 'org-brain-after-visualize-hook 'obvz-update-graph) ;; automatic redrawing with org-brain
(setq obvz-connection-type "dbus")
(setq obvz-include-node-texts nil)
(setq obvz-only-use-annotated-edges nil)
(setq obvz-most-recent-config ())
(setq obvz-draw-arrow t)
(setq obvz-highlight-current-node t)
(setq obvz-layout-type "force")
(setq obvz-use-edge-labels t)
(define-key org-brain-visualize-mode-map "N" 'obvz-switch-node-text-inclusion)
(define-key org-brain-visualize-mode-map "R" 'obvz-reposition-nodes)
(define-key org-brain-visualize-mode-map "U" 'obvz-update-graph-hard)
(define-key org-brain-visualize-mode-map "E" 'obvz-toggle-edge-labels)
;; ========== set these before loading the functions ==============
(setq obvz-python-version "python3.8")
;; (setq obvz-dir "~/obvz/")
;; ============== zmq section ================
;; (require 'zmq)
;; (setq sock (zmq-socket (zmq-context) zmq-PUB))
;; (zmq-bind sock "tcp://127.0.0.1:5556")
;; (setq obvz-connection-type "zmq")