-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorg-xob.el
1429 lines (1277 loc) · 50.7 KB
/
org-xob.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-xob.el --- advanced knowledge management system in Org-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Willy Rempel
;; Author: Willy Rempel <[email protected]>
;; URL: https://github.com/vv111y/org-xob.el
;; Version: 0.5-pre
;; Keywords:
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Making the most of org-mode. Another attempt at an exo-brain inspired by: zettlekasten, wikis, roam, and all the other ways to organize ourselves.
;;;; Installation
;;;;; MELPA
;; If you installed from MELPA, you're done.
;;;;; Manual
;; Install these required packages:
;; + org
;; + org-element
;; + org-id
;; + org-ql
;; + cl-lib
;; + org-super-links
;; Then put this file in your load-path, and put this in your init
;; file:
;; (require 'org-xob)
;;;; Usage
;; Run one of these commands:
;; `org-xob-command': Frobnicate the flange.
;;;; Tips
;; + You can customize settings in the `org-xob' group.
;;;; Credits
;; This package would not have been possible without the following
;; packages: foo[1], which showed me how to bifurcate, and bar[2],
;; which takes care of flanges.
;;
;; [1] https://example.com/foo.el
;; [2]
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
;;;; Requirements
(require 'org)
(require 'org-element)
(require 'org-id)
(require 'org-ql)
(require 'cl)
(require 'cl-lib)
(require 'org-super-links)
(declare-function org-super-links-link "org-super-links.el")
(declare-function org-super-links-store-link "org-super-links.el")
(declare-function org-super-links-insert-link "org-super-links.el")
(setq org-super-links-backlink-into-drawer t)
(setq org-super-links-link-prefix nil)
(setq org-super-links-link-postfix nil)
(setq org-super-links-backlink-postfix nil)
(setq org-super-links-related-into-drawer nil)
(setq org-super-links-search-function #'org-xob-get-node)
;;;; Customization
(defgroup org-xob nil
"Settings for `org-xob'."
:link '(url-link "http://github.com/vv111y/org-xob.el"))
(defcustom org-xob-something nil
"This setting does something."
:type 'something)
;;;; Variables
(defvar org-xob-on-p nil)
(defvar org-xob-today nil
"The current day node.")
;;;;; hash tables
(defvar org-xob--table-size 1000000
"Size of the hash tables.")
(defvar org-xob--title-id nil)
(defvar org-xob--id-title nil)
;;;;; knowledge base sources
(defvar org-xob-available-sources
"List of context information sources that are available in the xob system.")
(defvar org-xob--source-backlinks
'(:name backlinks
:tags ("KB" "backlinks")
:title nil
:ID nil
:PID nil
:getfn org-xob--node-get-link-entries
:items nil))
(defvar org-xob--source-forlinks
'(:name forlinks
:tags ("KB" "forlinks")
:title nil
:ID nil
:PID nil
:getfn org-xob--node-get-link-entries
:items nil))
;;;;; file variables
(defvar org-xob-dir "~/xob/"
"Core directory for exobrain system.")
(defvar org-xob-max-KB-filesize 524288
"Specifies the largest size the knowledge base org-mode files should grow to. Once the current file reaches the limit, a new file is created.")
(defvar org-xob--KB-filename-prefix "KB-file-"
"suffix for KB filenames. A simple filecount value is appended for a new name")
(defvar org-xob--log-filename-prefix "log-file-"
"suffix for log filenames. A simple filecount value is appended for a new name")
(defvar org-xob--agenda-filename-prefix "agenda-file-"
"suffix for agenda filenames. A simple filecount value is appended for a new name")
(defvar org-xob--archive-filename-prefix "archive-file-"
"suffix for archive filenames. A simple filecount value is appended for a new name")
;; lists of the xob files
(defvar org-xob--KB-files nil
"List of all knowledge base files.")
(defvar org-xob--log-files nil
"List of all xog log files.")
(defvar org-xob--agenda-files nil
"List of all xob agenda files.")
(defvar org-xob--archive-files nil
"List of all xob archive files.")
;; the currently active files
(defvar org-xob--KB-file nil
"The currently active KB file to store previous versions of nodes.")
(defvar org-xob--log-file nil
"The current log file where day nodes and general activity is recorded.")
(defvar org-xob--agenda-file nil
"The current xob agenda file where all activity nodes other than day nodes go.")
;; file header strings
(defvar org-xob--xob-header "#+PROPERTY: xob t\n")
(defvar org-xob--log-header "#+PROPERTY: xob-log t\n")
(defvar org-xob--agenda-header "#+PROPERTY: xob-agenda t\n")
(defvar org-xob--archive-header "#+PROPERTY: xob-archive t\n")
(defvar org-xob--current-header "#+PROPERTY: xob-current-file t\n")
;;;;; node/capture variables
(defvar org-xob-labels '("one" "two" "cat" "dog"))
(defvar org-xob--node-types '("a.day" "a.project" "a.session" "a.log" "a.log.life" "a.log.tools" "a.log.project" "a.todo" "n.n" "n.topic" "n.bib.article" "n.bib.web" "t.free" "t.project"))
(defvar org-xob--auto-templates '("ad" "as" "al" "all" "alit" "alt" "lp" "nt" "na" "nw" "tf" "tp"))
;;;;; Keymaps
(defvar org-xob-map
;; This makes it easy and much less verbose to define keys
(let ((map (make-sparse-keymap "org-xob-map"))
(maps (list
;; Mappings go here, e.g.:
;; "C-RET" #'(lambda () (message "Override!"))
)))
(cl-loop for (key fn) on maps by #'cddr
do (progn
(when (stringp key)
(setq key (kbd key)))
(define-key map key fn)))
map))
(defvar org-xob-context-mode-map
;; This makes it easy and much less verbose to define keys
(let ((map (make-sparse-keymap "org-xob-context-mode-map"))
(maps (list
;; "C-RET" #'(lambda () (message "Override!"))
)))
(cl-loop for (key fn) on maps by #'cddr
do (progn
(when (stringp key)
(setq key (kbd key)))
(define-key map key fn)))
map))
;;;; Minor Mode
;;;###autoload
(define-minor-mode org-xob-mode
"Org-Exobrain Minor Mode. For this release it is only used in the context buffer."
:lighter "Ⓧ"
:keymap (let ((map (make-sparse-keymap))) map)
:group 'org-xob
:require 'org-xob
(progn
(unless org-xob-on-p (org-xob-start))
))
;;;###autoload
(define-minor-mode org-xob-context-mode
"Org-Exobrain Minor Mode. For this release it is only used in the context buffer."
:lighter "ⓧ"
:keymap (let ((map (make-sparse-keymap))) map)
:group 'org-xob
:require 'org-xob
(progn
(unless org-xob-on-p (org-xob-start))
(if org-xob-context-mode
(evil-define-key 'normal 'local (kbd "t") 'org-xob-to-node-tree))
))
;;;; Macros
(defmacro org-xob-with-xob-on (&rest body)
(declare (debug (body)))
`(if org-xob-on-p
(progn
,@body)
(message "xob is not on.")))
(defmacro org-xob-with-xob-buffer (&rest body)
;; (declare (debug (body)))
`(if (or (and (boundp 'bufID)
(org-xob--is-node-p bufID)
(bound-and-true-p org-xob-mode))
(and (boundp 'parent-ID)
(org-xob--is-node-p parent-ID)
(bound-and-true-p org-xob-context-mode)))
(progn
,@body)
(message "Not in a xob buffer.") nil))
(defmacro org-xob-with-context-buffer (&rest body)
`(let ((buf (cond
((and (boundp 'bufID)
(boundp 'org-xob--context-buffer)
(bound-and-true-p org-xob-mode))
org-xob--context-buffer)
((and (boundp 'parent-ID)
(bound-and-true-p org-xob-context-mode))
(current-buffer))
(t nil))))
(if (buffer-live-p buf)
(with-current-buffer buf ,@body))))
(defmacro org-xob-with-edit-buffer (&rest body)
(declare (debug (body)))
`(let ((buf (cond
((and (boundp 'ID)
(boundp 'org-xob--context-buffer)
(bound-and-true-p org-xob-mode))
(current-buffer))
((and (boundp 'parentID)
(bound-and-true-p org-xob-context-mode))
parent-edit-buffer)
(t nil))))
(if (buffer-live-p buf)
(with-current-buffer buf ,@body))))
;;;; Commands
;;;;; Main Commands
;;;###autoload
(defun org-xob-start (&optional arg)
"Start the xob system: load state or initialize new. Open new day node.
Calling with C-u will force a restart."
(interactive "P")
(if (equal arg '(4))
(setq org-xob-on-p nil))
(if (and
(if org-xob-on-p (progn (message "XOB: already started.") nil) t)
(and
(add-hook 'org-capture-prepare-finalize-hook #'org-xob--new-node)
(add-hook 'org-follow-link-hook #'org-xob--link-hook-fn)
(message "XOB: hooks enabled."))
(if (file-directory-p org-xob-dir) (message "XOB: directory found.")
(prog1 (message "XOB: directory not found, creating.")
(make-directory org-xob-dir t)))
(not (setq org-xob--open-nodes nil))
(org-xob--load-state)
(org-xob--register-files)
(org-xob--process-files)
(org-xob--eval-capture-templates)
(org-xob--open-today)
(setq org-xob-new-day-timer
(run-at-time "00:00"
(* 24 60 60)
'org-xob--open-today)))
(progn
(setq org-xob-on-p t)
(message "XOB: started.")
(org-xob-info))
(message "XOB: Unable to (re)start.")))
;;;###autoload
(defun org-xob-stop ()
"Stop xob system: save all state and cleanup."
(interactive)
(if org-xob-on-p
(progn
(org-xob--save-state)
(with-current-buffer org-xob-today-buffer
(save-buffer)
(kill-buffer))
(setq org-xob-today nil)
(setq org-id-extra-files
(set-difference org-id-extra-files
(append org-xob--KB-files
org-xob--agenda-files
org-xob--log-files)))
(setq org-agenda-files
(set-difference org-agenda-files
(append org-agenda-files
org-xob--agenda-files
org-xob--log-files)))
(org-xob--clear-file-variables)
(remove-hook 'org-capture-prepare-finalize-hook #'org-xob--new-node)
(remove-hook 'org-follow-link-hook #'org-xob--link-hook-fn)
(cancel-timer org-xob-new-day-timer)
(setq org-xob-on-p nil)
(message "XOB: stopped."))))
;;;###autoload
(defun org-xob-open-day ()
"Open todays node."
(interactive)
(org-xob-with-xob-on
(condition-case nil
(progn
(org-id-goto org-xob-today)
(org-narrow-to-subtree))
(error (message "todays day node missing.")))))
;;;###autoload
(defun org-xob-get-node ()
"Focus on a node for editing. If it does not exist, create it."
(interactive)
(org-xob-with-xob-on
(pcase-let ((`(,ID ,title) (org-xob--get-create-node)))
(org-xob--edit-node ID title))))
;;;###autoload
(defun org-xob-remove-node (&optional ID)
"Removes node at point from xob system, but does not delete the node itself.
Removes node from the hash tables, and any backlinks in other nodes referencing it.
But ignore any links that reference it. Override xob property.
If called with optional ID argument, then remove the node with that ID."
(interactive)
(org-xob-with-xob-on
(save-window-excursion
(save-excursion
(if ID
(org-id-goto ID))
(let* ((ID (org-id-get (point)))
(title (gethash ID org-xob--id-title))
(forlinks (org-xob--node-get-links 'forlinks))
link-element)
(dolist (el forlinks)
(save-excursion
(org-id-goto el)
(save-restriction
(org-narrow-to-subtree)
(outline-show-all)
(setq link-element (org-super-links--find-link ID))
(if link-element
(org-super-links--delete-link link-element)))))
(remhash ID org-xob--id-title)
(remhash title org-xob--title-id)
(org-entry-delete (point) "TYPE")
(org-entry-delete (point) "xob")
(org-xob--save-state))))))
;;;###autoload
(defun org-xob-insert-link ()
"Inserts a properly formatted xob node link at point. If we are in a xob buffer,
then also update the forlinks source."
(interactive)
(org-xob-with-xob-on
(pcase-let ((`(,ID ,title) (org-xob--get-create-node)))
(org-super-links--insert-link (org-id-find ID 'MARKERP))
(org-xob-with-xob-buffer
(org-xob--source-refresh 'forlinks)))))
;;;###autoload
(defun org-xob-refile-region ()
"Move text in region to the end of the top section of a selected node."
(interactive)
(org-xob-with-xob-on
(if (use-region-p)
(pcase-let ((`(,ID ,title) (org-xob--get-create-node)))
(kill-region (point) (mark))
(save-window-excursion
(org-with-wide-buffer
(org-id-goto ID)
(if (org-goto-first-child)
(progn
(newline 2)
(forward-line -1))
(org-end-of-subtree)
(newline))
(if (org-kill-is-subtree-p)
(org-paste-subtree
(+ 1 (org-current-level)) nil t t)
(yank)
(org-xob--modify-time))))))))
;;;###autoload
(defun org-xob-heading-to-node ()
"Convenience function to convert current subtree into a xob KB node."
(interactive)
(org-xob-with-xob-on
(unless (org-xob--is-node-p)
(org-xob--new-node (point))
(let ((filename (buffer-file-name)))
(unless (member filename org-xob--KB-files)
(save-excursion
(goto-char (point-min))
(insert org-xob--xob-header))
(save-buffer)
(push filename org-xob--KB-files))))))
;;;###autoload
(defun org-xob-add-node-labels ()
"Select labels to apply to node at point, or at optional node specified by ID."
(interactive)
(org-xob-with-xob-on
(if (org-xob--is-node-p)
(helm :buffer "xob labels"
:sources (helm-build-sync-source "xob-labels"
:candidates org-xob-labels
:action (lambda (c)
(org-entry-put (point) "LABELS"
(string-join (helm-marked-candidates) " ")))))
(message "XOB: not on a xob node."))))
;;;###autoload
(defun org-xob-change-node-type ()
"Change the type for node at point, or at optional node specified by ID."
(interactive)
(org-xob-with-xob-on
(if (org-xob--is-node-p)
(helm :buffer "xob types"
:sources (helm-build-sync-source "xob-types"
:candidates org-xob--node-types
:action (lambda (c)
(org-entry-put (point) "TYPE" c)))))))
;;;;; Sideline Commands
;;;###autoload
(defun org-xob-toggle-sideline (&optional flag)
"Toggles display of the sideline window. If flag is 'ON then display sideline
regardless. Likewise with flag 'OFF."
(interactive)
(org-xob-with-xob-on
(save-excursion
(org-xob-with-edit-buffer
(if (boundp 'org-xob--sideline-window)
(if (and org-xob--sideline-window
(window-valid-p org-xob--sideline-window))
(if (not (eq flag 'ON))
(progn
(delete-window org-xob--sideline-window)
(setq org-xob--sideline-window nil)))
(and (not (eq flag 'OFF))
(setq org-xob--sideline-window
(split-window-right))
(set-window-buffer org-xob--sideline-window
org-xob--context-buffer)))
(message "XOB: no sideline window associated with this buffer."))))))
;;;###autoload
(defun org-xob-show-context ()
"Display the context buffer in the sideline window."
(interactive)
(org-xob-with-xob-on
(org-xob-with-edit-buffer
(org-xob-show-side-buffer org-xob--context-buffer))))
;;;;; KB Context Commands
;;;###autoload
(defun org-xob-show-backlinks (&optional arg)
"Add backlinks contents to the context buffer."
(interactive)
(org-xob-with-context-buffer
(unless (or (eq '(4) arg)
;; (local-variable-p 'forlinks)
(cdr-safe (assoc 'backlinks org-xob--node-sources)))
(setq-local backlinks (org-xob--prepare-kb-source
org-xob--source-backlinks arg)))
(org-xob--source-write backlinks))
(org-xob-toggle-sideline 'on))
;;;###autoload
(defun org-xob-show-forlinks (&optional arg)
"Add forlinks contents to the context buffer."
(interactive)
(org-xob-with-context-buffer
(unless (or (eq '(4) arg)
;; (local-variable-p 'forlinks)
(cdr-safe (assoc 'forlinks org-xob--node-sources)))
(setq-local forlinks (org-xob--prepare-kb-source
org-xob--source-forlinks arg)))
(org-xob--source-write forlinks)
(org-xob-toggle-sideline 'on)))
;;;###autoload
(defun org-xob-ql-search ()
"<Unavailable this release>
Use org-ql to search the KB. Creates a new source in the context buffer."
(interactive)
(org-xob-with-xob-buffer
nil))
;;;;; Context Presentation Commands
;;;###autoload
(defun org-xob-refresh-context ()
"Refresh all displayed sources"
(interactive)
(org-xob-with-context-buffer
(dolist (el org-xob--node-sources)
(org-xob--source-refresh el))))
;;;###autoload
(defun org-xob-clear-heading ()
"Clears contents of context entry at point, or for whole context source."
(interactive)
(org-xob--kb-copy-paste))
;;;###autoload
(defun org-xob-to-summary ()
"Show KB node summary. This is defined as the first paragraph if it exists."
(interactive)
(org-xob--kb-copy-paste
#'(lambda () (progn
(org-end-of-meta-data t)
(let ((p (org--paragraph-at-point)))
(if p
(buffer-substring-no-properties
(org-element-property :contents-begin p)
(org-element-property :contents-end p))))))))
;; TEST
;;;###autoload
(defun org-xob-to-node-tree ()
"Show only subheadings of KB node."
(interactive)
(org-xob--kb-copy-paste
#'(lambda ()
(let (lines)
(org-map-tree
(lambda ()
(push (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))
lines)))
(setq lines (nreverse lines))
(pop lines)
(mapconcat 'identity lines "\n")))
#'(lambda (str) (if (org-kill-is-subtree-p str)
(org-paste-subtree 3 str)))))
;; TEST
;;;###autoload
(defun org-xob-to-section ()
"Show the top section of KB node, no subheadings."
(interactive)
(org-xob--kb-copy-paste
#'(lambda () (let ((beg) (end))
(org-end-of-meta-data t)
(setq beg (point))
(outline-next-heading)
(setq end (- (point) 1))
(buffer-substring beg end)))))
;;;###autoload
(defun org-xob-to-full-node ()
"Show the full KB node, excepting properties drawer, planning & clocking information."
(interactive)
(org-xob--kb-copy-paste
#'(lambda ()
(let ((org-yank-folded-subtrees nil)
(org-yank-adjusted-subtrees t))
(org-copy-subtree)
(with-temp-buffer
(org-mode)
(org-paste-subtree 2)
(goto-char (point-min))
(org-mark-subtree)
(org-end-of-meta-data t)
(buffer-substring (point) (mark)))))
#'(lambda (str) (insert str))))
;;;;; Activity Commands
;;;###autoload
(defun org-xob-log-done (&optional ID)
"Convert a complete TODO into a log entry for future reference.
If ID is given, then convert todo with that ID."
(interactive)
(save-excursion
(save-window-excursion
(when ID (org-id-goto ID))
(if (org-entry-is-done-p)
(progn
(org-todo 'none)
(org-schedule '(4))
(org-deadline '(4))
(when-let* ((log-start (org-log-beginning))
(log-entry (org-xob--insert-link-header
(org-id-get)
(nth 4 (org-heading-components))
org-xob-today)))
(goto-char log-start)
(forward-line -1)
(org-mark-element)
(kill-region (point) (mark))
(org-id-goto log-entry)
(org-end-of-meta-data)
(yank)))
(org-entry-put (point) "TYPE" "a.log")
;; TODO move to a KB file?
)
(message: "XOB: todo entry is not done."))))
(defun org-xob-todo-at-point ()
"create a todo entry for node at point. Todo is filed in current inbox."
(interactive)
(org-super-links-store-link)
(save-excursion
(org-id-goto (org-xob--capture "tf"))
(org-end-of-meta-data)
(insert "Re:\n")
(org-super-links--insert-link)))
;;;; Backend
;;;;; Buffer Functions
(defun org-xob--edit-node (ID title)
"Create an indirect buffer of the node with name title."
(let ((short-title (truncate-string-to-width title 20))
place buf)
(if (setq buf (get-buffer short-title))
(switch-to-buffer buf)
(save-window-excursion
(org-with-wide-buffer
(org-id-goto ID)
(setq place (point))
(unless (boundp 'org-xob--edit-buffers)
(setq-local org-xob--edit-buffers nil))
(add-hook 'write-contents-functions #'org-xob--update-modified-time nil t)
(save-excursion
(setq buf (clone-indirect-buffer short-title t)))
(add-to-list 'org-xob--edit-buffers buf)))
(switch-to-buffer buf)
(org-xob-mode 1)
(add-hook 'kill-buffer-hook 'org-xob--cleanup-buffers-hook )
(setq org-xob--open-nodes (append org-xob--open-nodes (list (cons ID ()))))
(goto-char place)
(org-narrow-to-subtree)
(setq-local bufID ID title title short-title short-title
log-entry (org-xob--insert-link-header ID title org-xob-today)
org-xob--context-buffer (get-buffer-create (concat "*context-" title))
org-xob--sideline-window nil)
(org-xob--setup-context-buffer ID
short-title
(current-buffer)))))
(defun org-xob--setup-context-buffer (ID title edit-buffer)
"Create context buffer, leave it empty by default. set title and buffer
local variables for the edit buffer and the back and for links source objects."
(with-current-buffer org-xob--context-buffer
(org-mode)
(org-xob-context-mode 1)
(setq-local parent-ID ID
parent-title title
parent-edit-buffer edit-buffer
org-xob--node-sources nil)))
(defun org-xob--cleanup-buffers-hook ()
"Cleanup when closing a node edit buffer. Close sideline window if open, delete
context buffer, and remove edit buffer from list of open indirect buffers.
Buffer local to edit buffer."
(if (and (boundp 'org-xob--sideline-window)
(window-live-p org-xob--sideline-window))
(delete-window org-xob--sideline-window))
(and (boundp 'org-xob--context-buffer)
(kill-buffer org-xob--context-buffer))
(and (boundp 'bufID)
(assoc-delete-all bufID org-xob--open-nodes)
(let ((selfbuf (current-buffer))
(basebuf (buffer-base-buffer)))
(if basebuf
(with-current-buffer (buffer-base-buffer)
(setq-local org-xob--edit-buffers (cl-delete-if
(lambda (x) (or (not (buffer-live-p x))
(eq selfbuf x) ))
org-xob--edit-buffers)))))) nil)
(defun org-xob--update-modified-time ()
"Hook to update the modified timestamp of all nodes that are being edited when saving.
ID should be buffer local in a xob edit buffer."
(save-window-excursion
(save-excursion
(dolist (buf org-xob--edit-buffers)
(if (buffer-live-p buf)
(with-current-buffer buf
(goto-char (point-min))
(re-search-forward bufID)
(org-back-to-heading t)
(let ((mdate (org-entry-get (point) "MODIFIED")))
(if mdate
(org-entry-put (point) "MODIFIED"
(concat "[" (format-time-string "%F %a %R") "]")))))))
nil)))
(defun org-xob--modify-time ()
"Function to change the modified time for a node. Assumes point is somewhere in the relevant node."
(org-back-to-heading t)
(let ((mdate (org-entry-get (point) "MODIFIED")))
(if mdate
(org-entry-put (point) "MODIFIED"
(concat "[" (format-time-string "%F %a %R") "]")))))
;;;;; Buffer Navigation
(defun org-xob--id-create ()
"Create a UUID formatted ID. org-id will not work with buffers that are
not visiting a file. This function is meant for such a case. Use in conjunction
with org-xob--id-goto to return to this heading.
Returns ID if successful, nil otherwise."
(let ((ID (uuidgen-4)))
(if (org-at-heading-p)
(progn (org-entry-put (point) "ID" ID)
ID)
ID)))
(defun org-xob--id-goto (sID)
"Search buffers for org heading with ID and place point there.
Return point position if found, nil otherwise."
(let (place)
(when (org-not-nil sID)
(or (and (string= sID (org-entry-get (point) "ID"))
(org-back-to-heading)
(point))
(and (setq place (org-find-entry-with-id sID))
(goto-char place))
(and (setq place (with-current-buffer (org-xob--other-buffer)
(org-find-entry-with-id sID)))
(set-buffer org-xob--other-buffer)
(goto-char place))))))
(defun org-xob-show-side-buffer (abuffer)
"Show abuffer in the sideline window."
(if (boundp 'org-xob--sideline-window)
(org-xob-toggle-sideline 'ON)
(save-excursion
(select-window org-xob--sideline-window)
(display-buffer-same-window abuffer nil))))
(defun org-xob--other-buffer ()
"Returns the buffer object corresponding to the other xob buffer of this pair."
(or
(and (boundp 'org-xob--context-buffer)
(bufferp org-xob--context-buffer)
org-xob--context-buffer)
(and (boundp 'parent-edit-buffer)
(bufferp parent-edit-buffer)
parent-edit-buffer)))
(defun org-xob--goto-buffer-heading (ID)
"Go to heading in current buffer with ID. Does not require org-id."
(let ((m (point)))
(org-with-wide-buffer
(goto-char (point-min))
(if (re-search-forward ID nil t)
(org-back-to-heading 'invisible-ok)
(progn
(goto-char m)
(message "%s not found." ID))))))
;;;;; Contexts Functions
(defun org-xob--is-source-p (&optional ID)
"Check if heading at point is a valid xob source. If an ID argument is supplied,
then check the heading associated with it."
(interactive)
(let ((temp (if ID ID
(org-entry-get (point) "ID")))
(pid (or (and (boundp 'bufID)
bufID)
(and (boundp 'parent-ID)
parent-ID))))
(if (and temp
(member temp (assoc pid org-xob--open-nodes))) t nil)))
(defun org-xob--prepare-kb-source (source &optional arg)
"fill in material for a node context source."
(org-xob-with-context-buffer
(let (ID name)
(if arg
(funcall (plist-get source :getfn) source))
(unless (org-xob--id-goto (plist-get source :ID))
(plist-put source :ID (setq ID (org-xob--id-create))))
(setq name (plist-get source :name))
(plist-put source :PID parent-ID)
(plist-put source :title parent-title)
(make-local-variable name)
(nconc (assoc parent-ID org-xob--open-nodes) (list ID))
(push (cons name ID) org-xob--node-sources)
(funcall (plist-get source :getfn) source)
source)))
(defun org-xob--source-write (source)
"Open a source tree into the context buffer. If it is already there,
then refresh it. source items are shown as org headings.
source is a plist that describes the content source."
(org-xob-with-context-buffer
(org-with-wide-buffer
(unless (org-xob--id-goto (plist-get source :ID))
(goto-char (point-max))
(org-insert-heading '(4) 'invisible-ok 'TOP)
(org-edit-headline (plist-get source :title))
(dolist (el (plist-get source :tags))
(org-toggle-tag el 'ON))
(org-entry-put (point) "ID" (plist-get source :ID))
(org-entry-put (point) "PID" (plist-get source :PID)))
(org-xob--source-refresh source))))
(defun org-xob--source-refresh (source)
"Remake source tree. Check if items need to be added or removed."
(org-xob-with-context-buffer
(if (org-xob--id-goto (plist-get source :ID))
(let ((temp (copy-tree (plist-get source :items))))
(org-xob--map-source
(lambda ()
(let ((pid (org-entry-get (point) "PID")))
(if (member pid temp)
(setq temp (delete pid temp))
(progn
(org-mark-subtree)
(call-interactively 'delete-region))))))
(if temp
(dolist (el temp)
(org-xob--source-add-item el)))))))
(defun org-xob--source-add-item (ID)
"Appends a single entry to the end of the source subtree.
Assumes point is on the source heading."
(let ((title (gethash ID org-xob--id-title)))
(save-excursion
(if title
(progn
(org-insert-subheading '(4))
(org-edit-headline title)
(org-entry-put (point) "PID" ID))
(message "no kb node found for ID: %s" ID)))))
(defun org-xob--map-source (func &optional ID)
"Apply the function func to every child-item of a xob source.
If the optional ID of a xob source is given, then apply func to that source.
Otherwise apply to source at point."
(save-excursion
(if ID (org-xob--id-goto ID))
(org-with-wide-buffer
(if (and (org-xob--is-source-p)
(org-goto-first-child))
(while
(progn
(funcall func)
(outline-get-next-sibling)))
(message "XOB: map-source, nothing to do here.") nil))))
;;;;; KB Context Functions
(defun org-xob--node-get-link-entries (source)
"Populates source item list from the node. The items are represented by their
respective node IDs. Two kinds of links are distinguished: backlinks and forlinks
(which are all other links to xob KB nodes). Assumes org-super-links convention
where the backlinks are in a BACKLINKS drawer."
(save-window-excursion
(save-excursion
(org-id-goto (plist-get source :PID))
(plist-put source :items
(org-xob--node-get-links (plist-get source :name))))))
(defun org-xob--node-get-links (linktype)
"Return list of link paths within the node at point. If linktype is 'backlinks'
then return only links in the backlinks drawer. If linktype is 'forlinks'
then return all other links."
(let* ((test (if (eq linktype 'backlinks)
(lambda (x) x)
(if (eq linktype 'forlinks)
(lambda (x) (not x))))))
(save-excursion
(save-restriction
(org-back-to-heading t)
(org-narrow-to-subtree)
(delete-dups
(delq nil
(org-element-map (org-element-parse-buffer) 'link
(lambda (link)
(let (ID)
(if (funcall test (equal (org-element-property
:drawer-name (cadr (org-element-lineage link)))
"BACKLINKS"))
(if (org-xob--is-node-p
(setq ID (org-element-property :path link)))
ID
(message "XOB: invalid link %s" ID) nil)))))))))))
(defun org-xob--kb-copy-paste (&optional selector insertor)
"Wrapper function to display new content in a context item from the
knowledge base. Executes function selector while point is at the heading
of the origin node in the KB. selector must be a lambda that returns
the the contents of interest as a string.
When called with point on the given context item, only that item will be
updated. If called on a context source heading, then the update is applied
to all source items."
(let ((func #'(lambda ()
(let ((pid (org-entry-get (point) "PID")) str)
(unless (not pid)
(save-excursion
(org-back-to-heading t)
(org-mark-subtree)
(org-end-of-meta-data)
(call-interactively #'delete-region)
(deactivate-mark 'force))
(if selector
(progn
(save-excursion
(org-id-goto pid)
(org-with-wide-buffer
(org-save-outline-visibility
(org-narrow-to-subtree)
(setq str (funcall selector))
(deactivate-mark 'force))))
(if (stringp str)
(progn
(org-end-of-subtree)
(newline)
(if insertor
(funcall insertor str)
(insert str)))))))))))
(save-window-excursion
(org-with-wide-buffer
(if (pulse-available-p)
(pulse-momentary-highlight-one-line (point)))
(if (org-xob--is-source-p)
(org-xob--map-source func)
(funcall func))))))
;;;;; Node Functions
(defun org-xob--is-node-p (&optional ID DEEPCHECK)
"Check if a heading is a xob node. Called interactively it defaults to heading at point.
If an ID argument is supplied, then check the heading associated with it.
With option DEEPCHECK, do not use any table lookup, but check whether the heading
has valid UUID formatted ID and xob TYPE properties in the property drawer.
Deepcheck only works on heading at point, any ID argument is ignored."
(interactive)
(let ((temp (if ID ID (org-id-get nil))))
(if temp