-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.el
1793 lines (1565 loc) · 76.1 KB
/
config.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
;;; -*- lexical-binding: t; -*-
(general-auto-unbind-keys)
(map! [escape] #'keyboard-quit)
(setq gc-cons-threshold 100000000
read-process-output-max (* 1024 1024))
(setq large-file-warning-threshold nil)
(setq
doom-font (font-spec :family "SauceCodePro Nerd Font Mono" :size 18)
doom-big-font (font-spec :family "Ubuntu" :size 15)
doom-big-font (font-spec :family "SauceCodePro Nerd Font Mono" :size 24))
(defvar my/theme-light 'doom-one-light "The default light theme")
(defvar my/theme-dark 'doom-one "The default dark theme")
(defvar my/theme-selected 'doom-one "The selected theme")
(defun my/toggle-theme ()
"Toggle between light and dark theme."
(interactive)
(if (eq my/theme-selected my/theme-dark)
(setq my/theme-selected my/theme-light)
(setq my/theme-selected my/theme-dark))
(load-theme my/theme-selected))
(defun my/laptop-mode()
"Modify theme for latpop use."
(interactive)
(set-face-attribute 'default nil :height 75)
(when (boundp 'treemacs-root-face)
(set-face-attribute 'treemacs-root-face nil :height 90)))
(defun my/desktop-mode()
"Modify theme for latpop use."
(interactive)
(set-face-attribute 'default nil :height 130)
(when (boundp 'treemacs-root-face)
(set-face-attribute 'treemacs-root-face nil :height 130)))
(defun my/comf-mode()
"Modify theme for comfortable use."
(interactive)
(set-face-attribute 'default nil :height 150)
(set-face-attribute 'default nil :width 'ultra-expanded)
(when (boundp 'treemacs-root-face)
(set-face-attribute 'treemacs-root-face nil :height 160)))
(defun my/presentation-mode()
"Modify theme for presentations use."
(interactive)
(set-face-attribute 'default nil :height 250)
(when (boundp 'treemacs-root-face)
(set-face-attribute 'treemacs-root-face nil :height 260)))
(defun my/asciinema-mode()
"Modify theme for asciinema use."
(interactive)
(setq mode-line-format nil)
(setq-default mode-line-format nil)
(setq idee-tree-enabled nil)
(setq inhibit-message t)
(setq-default inhibit-message t)
(set-face-attribute 'default nil :height 150)
(when (boundp 'treemacs-root-face)
(set-face-attribute 'treemacs-root-face nil :height 160)))
(defvar my/selected-screen-mode 'my/comf-mode "The current screen mode to use.")
(funcall my/selected-screen-mode)
(display-battery-mode 1)
(map! "C-x C-k" #'kill-current-buffer)
(map! "C-x C-b" #'switch-to-buffer)
(defun my/consult-line-with-selection (&optional start end)
"Swiper variation using selected from START to END text as initial input."
(interactive (if (use-region-p) (list (region-beginning) (region-end))))
(let (
(start (or start (if (use-region-p) (region-beginning) nil)))
(end (or end (if (use-region-p) (region-end) nil))))
(if (and (use-region-p) start end)
(consult-line (buffer-substring start end))
(consult-line))))
(map! "C-s" #'my/consult-line-with-selection)
(map! "C-c i g" #'+vertico/project-search)
(map! :leader
"s b" #'my/consult-line-with-selection
"s m" #'consult-mark
"s M" #'consult-bookmark
"s r" #'vertico-repeat)
(setq display-line-numbers-type 'relative)
(map!
"C-q" #'er/expand-region
"C-c m m" #'mc/mark-next-like-this
"C-c m u" #'mc/umark-next-like-this
"C-c m s" #'mc/skip-next-like-this
"C-c m e" #'mc/edit-lines)
(use-package! dired-subtree
:commands (dired-subtree-toggle dired-subtree-cycle)
:config
:bind (:map dired-mode-map
("<tab>" . dired-subtree-toggle)
("<backtab>" . dired-subtree-cycle)))
(defun my/dired-expand-all ()
(interactive)
"Expand all subtrees in the dired buffer."
(let ((has-more t))
(while has-more
(condition-case ex
(progn
(dired-next-dirline 1)
(dired-subtree-toggle))
('error (setq has-more nil))))))
(map! :map dired-mode-map "S-<tab>" #'my/dired-expand-all)
(map! (:leader "p a" #'projectile-add-known-project))
(map! (:leader "p p" #'projectile-switch-project))
(map! (:leader "SPC" #'projectile-find-file-dwim))
(after! projectile
(setq! projectile-project-root-functions '(projectile-root-local projectile-root-bottom-up))
(setq! projectile-project-root-files-bottom-up
(append '(".projectile" ".git"))))
(setq! auth-sources '("~/.authinfo" "~/.authinfo.gpg" "~/.netrc"))
(setq! +doom-dashboard-menu-sections
'(("Reload last session"
:icon (all-the-icons-octicon "history" :face 'doom-dashboard-menu-title)
:when (cond ((modulep! :ui workspaces)
(file-exists-p (expand-file-name persp-auto-save-fname persp-save-dir)))
((require 'desktop nil t)
(file-exists-p (desktop-full-file-name))))
:face (:inherit (doom-dashboard-menu-title bold))
:action doom/quickload-session)
("Open agenda"
:icon (all-the-icons-octicon "calendar" :face 'doom-dashboard-menu-title)
:when (fboundp 'org-agenda)
:action org-agenda)
("Open roam"
:icon (all-the-icons-octicon "book" :face 'doom-dashboard-menu-title)
:when (fboundp 'org-roam-node-find)
:action org-roam-node-find)
("Recently opened files"
:icon (all-the-icons-octicon "file-text" :face 'doom-dashboard-menu-title)
:action recentf-open-files)
("Open project"
:icon (all-the-icons-octicon "briefcase" :face 'doom-dashboard-menu-title)
:action projectile-switch-project)
("Jump to bookmark"
:icon (all-the-icons-octicon "bookmark" :face 'doom-dashboard-menu-title)
:action bookmark-jump)
("Open private configuration"
:icon (all-the-icons-octicon "tools" :face 'doom-dashboard-menu-title)
:when (file-directory-p doom-user-dir)
:action doom/open-private-config)
("Open documentation"
:icon (all-the-icons-octicon "book" :face 'doom-dashboard-menu-title)
:action doom/help)))
(defadvice! horizontal-split-and-follow (&rest args)
"Switch focus to the newly created window when splitting horizontally."
:after '(split-window-horizontally split-window-right)
(balance-windows)
(other-window 1))
(defadvice! vertical-split-and-follow (&rest args)
"Switch focus to the newly created window when splitting vertically."
:after '(split-window-vertically split-window-below)
(balance-windows)
(other-window 1))
(map! "M-o" #'evil-window-next)
(setq org-agenda-files (append
'("~/Documents/org/quickmarks.org"
"~/Documents/org/github.org"
"~/Documents/org/habits.org"
"~/Documents/org/nutrition.org"
"~/Documents/org/roam/Inbox.org")
(directory-files-recursively "~/Documents/org/jira" "\.org$")))
(defun my/org-agenda-browse-at-point ()
"Browse the url of the specified item."
(interactive)
(let ((agenda-window-configuration (current-window-configuration)))
(org-agenda-switch-to)
(let ((url (car
(mapcar (lambda (p) (replace-regexp-in-string (regexp-quote "\"") "" (org-entry-get (point) p)))
(seq-filter (lambda (n) (string-suffix-p "url" n t))
(mapcar (lambda (e) (car e)) (org-entry-properties)))))))
(when url (browse-url url)))
(set-window-configuration agenda-window-configuration)))
(defun my/org-agenda-archive-at-point ()
"Browse the url of the specified item."
(interactive)
(let ((agenda-window-configuration (current-window-configuration)))
(org-agenda-switch-to)
(my/org-archive)
(set-window-configuration agenda-window-configuration)))
(defun my/org-agenda-export ()
"Export the content of org-agenda"
(interactive)
(org-eval-in-environment (org-make-parameter-alist
`(org-agenda-span 'day
org-agenda-use-time-grid t
org-agenda-remove-tags t
org-agenda-window-setup 'nope))
(let* ((wins (current-window-configuration))
org-agenda-sticky)
(save-excursion
(with-current-buffer
(get-buffer-create org-agenda-buffer-name)
(pop-to-buffer (current-buffer))
(org-agenda nil "t")
(let ((result (buffer-string)))
(with-temp-file "~/.agenda" (insert result)))))
(set-window-configuration wins))))
(use-package! org-super-agenda
:commands (my/org-agenda-browse-at-point my/org-agenda-archive-at-point my/org-agenda-export my/org-archive my/org-refile)
:config
(setq org-super-agenda-groups '((:name "Events" :time-grid t :todo "TODAY")
(:name "Habbits" :tag "habit" :todo "TODAY")
(:name "Due" :deadline past)
(:name "Jira" :tag "jira")
(:name "Email" :tag "email")
(:name "Github pulls" :tag "pull")
(:name "Github issues" :tag "issue"))
;; agenda
org-agenda-scheduled-leaders '("" "")
org-agenda-tag-filter-preset '("-drill")
org-agenda-start-day "+0"
org-agenda-start-on-weekday nil
org-agenda-span 2
org-agenda-files (append
(directory-files-recursively "~/Documents/org/jira" "\.org$")
'("~/Documents/org/roam/Inbox.org" "~/Documents/org/habits.org" "~/Documents/org/github.org" "~/Documents/org/nutrition.org"))
;; Refile
org-refile-targets '(
;; P.A.R.A
("~/Documents/org/roam/Projects.org" :maxlevel . 10)
("~/Documents/org/roam/Areas.org" :maxlevel . 10)
("~/Documents/org/roam/Resources.org" :maxlevel . 10)
("~/Documents/org/roam/Archives.org" :maxlevel . 10)))
:hook (org-agenda-mode . org-super-agenda-mode)
:bind (:map org-agenda-mode-map
("C-a" . my/org-agenda-archive-at-point)
("C-b" . my/org-agenda-browse-at-point)))
(map!
:map evil-motion-state-map
"C-b" nil
:map org-agenda-keymap
"j" #'org-agenda-next-line
"k" #'org-agenda-previous-line
:map org-agenda-mode-map
"j" #'org-agenda-next-line
"k" #'org-agenda-previous-line
:map org-super-agenda-header-map
"j" nil
"k" nil)
(setq! org-roam-directory "~/Documents/org/roam")
(setq! org-roam-capture-templates '(("d" "default" plain "%?" :target (file+head "${title}.org" "#+title: ${title}\n") :unnarrowed t)))
(setq! org-roam-dailies-capture-templates `(("d" "default" entry "* %?" :target (file+head "%<%Y-%m-%d>.org"
,(concat "#+title: %<%Y-%m-%d>\n"
"* Daily Checklist\n"
"** TODO Log weight\n"
"** TODO Check emails\n"
"** TODO Check github issues / pull requests"
)))))
(defun my/org-roam-extract-subtree-and-insert ()
"Convert current subtree at point to a node, extract it into a new file and insert a ref to it."
(interactive)
(save-excursion
(org-back-to-heading-or-point-min t)
;; Get the stars of the heading
(let ((stars (car (split-string (buffer-substring (bol) (eol))))))
(when (bobp) (user-error "Already a top-level node"))
(org-id-get-create)
(save-buffer)
(org-roam-db-update-file)
(let* ((template-info nil)
(node (org-roam-node-at-point))
(template (org-roam-format-template
(string-trim (org-capture-fill-template org-roam-extract-new-file-path))
(lambda (key default-val)
(let ((fn (intern key))
(node-fn (intern (concat "org-roam-node-" key)))
(ksym (intern (concat ":" key))))
(cond
((fboundp fn)
(funcall fn node))
((fboundp node-fn)
(funcall node-fn node))
(t (let ((r (read-from-minibuffer (format "%s: " key) default-val)))
(plist-put template-info ksym r)
r)))))))
(file-path
(expand-file-name
(read-file-name "Extract node to: " (file-name-as-directory org-roam-directory) template nil template)
org-roam-directory)))
(when (file-exists-p file-path)
(user-error "%s exists. Aborting" file-path))
(org-cut-subtree)
(save-buffer)
(with-current-buffer (find-file-noselect file-path)
(org-paste-subtree)
(while (> (org-current-level) 1) (org-promote-subtree))
(save-buffer)
(org-roam-promote-entire-buffer)
(save-buffer))
;; Insert a link to the extracted node
(insert (format "%s [[id:%s][%s]]\n" stars (org-roam-node-id node) (org-roam-node-title node)))))))
(after! f
(defvar my/logseq-folder "~/Documents/logseq/BJJORG")
;; You probably don't need to change these values
(defvar my/logseq-pages (f-expand (f-join my/logseq-folder "pages")))
(defvar my/logseq-journals (f-expand (f-join my/logseq-folder "journals")))
;;(defvar my/rich-text-types [bold italic subscript link strike-through superscript underline inline-src-block footnote-reference inline-babel-call entity])
(defvar my/rich-text-types '(bold italic subscript link strike-through superscript underline inline-src-block))
(defun my/textify (headline)
(save-excursion
(apply 'concat (flatten-list
(my/textify-all (org-element-property :title headline))))))
(defun my/textify-all (nodes) (mapcar 'my/subtextify nodes))
(defun my/with-length (str) (cons (length str) str))
(defun my/subtextify (node)
(cond ((not node) "")
((stringp node) (substring-no-properties node))
((member (org-element-type node) my/rich-text-types)
(list (my/textify-all (cddr node))
(if (> (org-element-property :post-blank node))
(make-string (org-element-property :post-blank node) ?\s)
"")))
(t "")))
(defun my/logseq-journal-p (file) (string-match-p (concat "^" my/logseq-journals) file))
(defun my/ensure-file-id (file)
"Visit an existing file, ensure it has an id, return whether the a new buffer was created"
(setq file (f-expand file))
(if (my/logseq-journal-p file)
`(nil . nil)
(let* ((buf (get-file-buffer file))
(was-modified (buffer-modified-p buf))
(new-buf nil)
has-data
org
changed
sec-end)
(when (not buf)
(setq buf (find-file-noselect file))
(setq new-buf t))
(set-buffer buf)
(setq org (org-element-parse-buffer))
(setq has-data (cddr org))
(goto-char 1)
(when (not (and (eq 'section (org-element-type (nth 2 org))) (org-roam-id-at-point)))
;; this file has no file id
(setq changed t)
(when (eq 'headline (org-element-type (nth 2 org)))
;; if there's no section before the first headline, add one
(insert "\n")
(goto-char 1))
(org-id-get-create)
(setq org (org-element-parse-buffer)))
(when (nth 3 org)
(when (not (org-collect-keywords ["title"]))
;; no title -- ensure there's a blank line at the section end
(setq changed t)
(setq sec-end (org-element-property :end (nth 2 org)))
(goto-char (1- sec-end))
(when (and (not (equal "\n\n" (buffer-substring-no-properties (- sec-end 2) sec-end))))
(insert "\n")
(goto-char (1- (point)))
(setq org (org-element-parse-buffer)))
;; copy the first headline to the title
(insert (format "#+title: %s" (string-trim (my/textify (nth 3 org)))))))
;; ensure org-roam knows about the new id and/or title
(when changed (save-buffer))
(cons new-buf buf))))
(defun my/logseq-to-roam-buffer (buffer)
"Convert BUFFER links from using logseq format to org-roam.
Logseq is using file references, which org-roam is using ids.
This function covnerts fuzzy anf file: links to id links."
(save-excursion
(let* (changed
link)
(set-buffer buffer)
(goto-char 1)
(while (search-forward "[[" nil t)
(setq link (org-element-context))
(setq newlink (my/logseq-to-roam-link link))
(when newlink
(setq changed t)
(goto-char (org-element-property :begin link))
(delete-region (org-element-property :begin link) (org-element-property :end link))
;; note, this format string is reall =[[%s][%s]]= but =%= is a markup char so one's hidden
(insert newlink)))
;; ensure org-roam knows about the changed links
(when changed (save-buffer)))))
(defun my/logseq-to-roam ()
"Convert the current buffer from logseq to roam."
(interactive)
(my/logseq-to-roam-buffer (current-buffer)))
(defun my/roam-to-logseq-buffer (buffer)
"Convert BUFFER links from using logseq format to org-roam.
Logseq is using file references, which org-roam is using ids.
This function covnerts fuzzy anf file: links to id links."
(save-excursion
(let* (changed)
(with-current-buffer buffer
(goto-char 1)
(while (search-forward "[[id:" nil t)
(let* ((id (car (split-string (buffer-substring-no-properties (point) (eol)) "]")))
(node (org-roam-node-from-id id))
(title (org-roam-node-title node)))
(when title
(setq file (car (org-id-find id)))
(setq link (org-element-context))
(setq newlink (format "[[%s]]" title))
(when newlink
(setq changed t)
(goto-char (org-element-property :begin link))
(delete-region (org-element-property :begin link) (org-element-property :end link))
;; note, this format string is reall =[[%s][%s]]= but =%= is a markup char so one's hidden
(insert newlink)))
;; ensure org-roam knows about the changed links
(when changed (save-buffer))))))))
(defun my/roam-to-logseq ()
"Convert the current buffer from roam to logseq."
(interactive)
(my/roam-to-logseq-buffer (current-buffer)))
(defun my/logseq-to-roam-link (link)
"Convert the LINK from logseq format to roam.
Logseq is using file references, which org-roam is using ids.
This function covnerts fuzzy anf file: links to id links."
(let (filename
id
linktext
newlink)
(when (eq 'link (org-element-type link))
(when (equal "fuzzy" (org-element-property :type link))
(setq filename (f-expand (f-join my/logseq-pages
(concat (org-element-property :path link) ".org"))))
(setq linktext (org-element-property :raw-link link)))
(when (equal "file" (org-element-property :type link))
(setq filename (f-expand (org-element-property :path link)))
(if (org-element-property :contents-begin link)
(setq linktext (buffer-substring-no-properties
(org-element-property :contents-begin link)
(org-element-property :contents-end link)))
(setq linktext (buffer-substring-no-properties
(+ (org-element-property :begin link) 2)
(- (org-element-property :end link) 2)))))
(when (and filename (f-exists-p filename))
(setq id (caar (org-roam-db-query [:select id :from nodes :where (like file $s1)]
filename)))
(when id
(setq newlink (format "[[id:%s][%s]]%s"
id
linktext
(if (> (org-element-property :post-blank link))
(make-string (org-element-property :post-blank link) ?\)
""))))
(when (not (equal newlink
(buffer-substring-no-properties
(org-element-property :begin link)
(org-element-property :end link))))
newlink))))))
(defun my/roam-file-modified-p (file-path)
(let ((content-hash (org-roam-db--file-hash file-path))
(db-hash (caar (org-roam-db-query [:select hash :from files
:where (= file $s1)] file-path))))
(not (string= content-hash db-hash))))
(defun my/modified-logseq-files ()
(emacsql-with-transaction (org-roam-db)
(seq-filter 'my/roam-file-modified-p
(org-roam--list-files my/logseq-folder))))
(defun my/check-logseq ()
(interactive)
(let (created
files
bufs
unmodified
cur
bad
buf)
(setq files (org-roam--list-files my/logseq-folder))
;; make sure all the files have file ids
(dolist (file-path files)
(setq file-path (f-expand file-path))
(setq cur (my/ensure-file-id file-path))
(setq buf (cdr cur))
(push buf bufs)
(when (and (not (my/logseq-journal-p file-path)) (not buf))
(push file-path bad))
(when (not (buffer-modified-p buf))
(push buf unmodified))
(when (car cur)
(push buf created)))
;; patch fuzzy links
(mapc 'my/logseq-to-roam-buffer (seq-filter 'identity bufs))
(dolist (buf unmodified)
(when (buffer-modified-p buf)
(save-buffer unmodified)))
(mapc 'kill-buffer created)
(when bad
(message "Bad items: %s" bad))
nil)))
(use-package! org-transclusion
:after org
:init
(map!
:leader
:prefix "n"
:desc "Org Transclusion Mode" "t" #'org-transclusion-mode))
(defun my/org-current-level ()
"Get the level of the current heading."
(save-excursion
(outline-previous-heading)
(let* ((line (buffer-substring-no-properties (bol) (eol)))
(stars (car (split-string line " ")))
(level (length stars)))
level)))
(defun my/org-roam-transclusion-insert ()
"Insert / Embed an node using org-transclusion."
(interactive)
(let ((level (my/org-current-level)))
(insert "#+transclude: ")
(org-roam-node-insert)
(when level (insert (format " :level %s" (+ 1 level))))
(org-transclusion-add)))
(map! :map org-mode-map
(:leader
(:prefix "n"
(:prefix "r"
:desc "Transclude an org-roam node" "t" #'my/org-roam-transclusion-insert))))
(setq my/inbox-file "~/Documents/org/roam/Inbox.org")
(setq my/archive-file "~/Documents/org/roam/Archives.org")
(defun my/org-find-archive-target (tag)
"Find the archive target for the specified TAG.
The idea is that the archive file has multiple headings one for each category.
When a tagged item is archived it should go to an archive with at least one matching tag
or to the 'Unsorted' when none is matched. Archives are expected to be tagged with the archive tag."
(or (car
(car
(org-ql-query
:select '(list (substring-no-properties (org-get-heading t t)))
:from my/archive-file
:where `(tags "archive" ,tag))))
"Unsorted"))
(defun my/org-refile (file headline &optional new-state)
"Refile item to the target FILE under the HEADLINE and set the NEW-STATE."
(let ((pos (save-excursion
(find-file file)
(org-find-exact-headline-in-buffer headline))))
(save-excursion
(org-refile nil nil (list headline file nil pos))
(org-refile-goto-last-stored)
(when new-state (org-todo new-state)))))
(defun my/org-archive ()
"Mark item as complete and refile to archieve."
(interactive)
(save-window-excursion
(when (equal "*Org Agenda*" (buffer-name)) (org-agenda-goto))
(let* ((tags (org-get-tags))
(headline (if tags (car (mapcar (lambda (tag) (my/org-find-archive-target tag)) tags)) nil)))
(my/org-refile my/archive-file headline "DONE")))
;; Redo the agenda
(when (equal "*Org Agenda*" (buffer-name)) (org-agenda-redo)))
(defun my/org-auto-archive ()
"Archieve all completed items in my inbox."
(interactive)
(save-window-excursion
(find-file my/inbox-file)
(goto-char 0)
(let ((pos))
(while (not (eq (point) pos))
(setq pos (point))
(outline-next-heading)
(let* ((line (buffer-substring-no-properties (bol) (eol)))
(line-without-stars (replace-regexp-in-string "^[\\*]+ " "" line)))
(when (string-prefix-p "DONE" line-without-stars)
(my/org-archive)
(goto-char 0) ;; We need to go back from the beggining to avoid loosing entries
(save-buffer)))))))
(after! 'org
(org-babel-do-load-languages 'org-babel-load-languages '((shell .t)
(ruby . t)
(java . t)
(javascript . t)
(typescript . t)
(plantuml . t))))
;;
;; To allow yas snippet integration with org babel and avoid org-mode shadowing the block mode (when it comes to snippets)
;;
(defun my/yas-org-babel-integration-hook ()
(setq-local yas-buffer-local-condition
'(not (org-in-src-block-p t))))
(add-hook 'org-mode-hook #'my/yas-org-babel-integration-hook)
(defvar my/last-tangle-source-buffer nil "Holds the last buffer edited by org-tangle.")
(defvar my/last-tangle-source-buffer-point 0 "Holds the original cursor position before tangle was called.")
(defun my/org-tangle-prepare ()
"Replace the comment references to standard noweb ones.
Comment format is '(//|#|;;)add: <reference id>'."
(setq my/last-tangle-source-buffer (current-buffer))
(setq my/last-tangle-source-buffer-point (point))
(get-buffer-create "**tangle**")
(copy-to-buffer "**tangle**" (point-min) (point-max))
(goto-char (point-min))
(while (re-search-forward "//add:\\([a-zA-Z0-9_-]+\\)" nil t)
(let* ((text (buffer-substring (match-beginning 1) (match-end 1)))
(new-text (format "<<%s>>" text)))
(replace-match new-text))))
(defun my/org-tangle-restore (&rest args)
"Restore the original buffer as it was before 'my/org-tangle-prepare'."
(with-current-buffer "**tangle**"
(copy-to-buffer my/last-tangle-source-buffer (point-min) (point-max)))
(switch-to-buffer my/last-tangle-source-buffer)
(goto-char my/last-tangle-source-buffer-point))
(add-hook 'org-babel-pre-tangle-hook #'my/org-tangle-prepare)
(advice-add 'org-babel-tangle :after #'my/org-tangle-restore)
(require 'ob-shell)
(use-package! org-babel-eval-in-repl
:custom (eir-shell-type 'vterm)
:bind (:map org-mode-map
("M-e" . ober-eval-block-in-repl)))
(defun my/not-empty (s)
"Returns non-nil if S is not empty."
(and s (stringp s) (not (= (length s) 0))))
;; Let's intercept eir-insert to make sure the text entered is trimmed.
(defun my/eir-insert-trimmed (orig string)
"Eir insert but with trimmed arguments."
(let ((trimmed (replace-regexp-in-string "^[ \t\n]+" "" (replace-regexp-in-string "[ \n]+$" "" string))))
(when (my/not-empty trimmed)
(apply orig (list trimmed)))))
(defun my/eir-send-not-empty-to-repl (orig fun-change-to-repl fun-execute region-string)
"Eir send to repl but ignore empty commands."
(when (my/not-empty region-string)
(apply orig (list fun-change-to-repl fun-execute region-string))))
(advice-add 'eir-insert :around #'my/eir-insert-trimmed)
(advice-add 'eir-send-to-repl :around #'my/eir-send-not-empty-to-repl)
(advice-add 'ober-eval-block-in-repl :before #'my/ensure-in-code-block)
(advice-add 'ober-eval-block-in-repl :after #'my/next-code-block)
(require 'ob-java)
(defun org-babel-expand-body:java (body params)
"Expand BODY with PARAMS.
BODY could be a few statements, or could include a full class
definition specifying package, imports, and class. Because we
allow this flexibility in what the source block can contain, it
is simplest to expand the code block from the inside out."
(let* ((fullclassname (or (cdr (assq :classname params)) ; class and package
(org-babel-java-find-classname body)))
(classname (car (last (split-string fullclassname "\\.")))) ; just class name
(packagename (if (string-match-p "\\." fullclassname) ; just package name
(file-name-base fullclassname)))
(var-lines (org-babel-variable-assignments:java params))
(imports-val (assq :imports params))
(imports (if imports-val
(split-string (org-babel-read (cdr imports-val) nil) " ")
nil)))
(with-temp-buffer
(insert body)
;; insert variables from source block headers
(when var-lines
(goto-char (point-min))
(org-babel-java--move-past org-babel-java--class-re) ; move inside class
(insert (mapconcat 'identity var-lines "\n"))
(insert "\n"))
;; add imports from source block headers
(when imports
(goto-char (point-min))
(org-babel-java--move-past org-babel-java--package-re) ; if package is defined, move past it
(insert (mapconcat (lambda (package) (concat "import " package ";")) imports "\n") "\n"))
;; add package at the top
(goto-char (point-min))
(when (and packagename (not (re-search-forward org-babel-java--package-re nil t)))
(insert (concat "package " packagename ";\n")))
;; return expanded body
(buffer-string))))
(setq! plantuml-default-exec-mode 'jar)
(when (not (file-exists-p plantuml-jar-path)) (plantuml-download-jar))
(after! org
(setq! org-capture-templates
'(
("c" "Calendar")
("cw" "Work Event" entry (file "~/Documents/org/calendars/work.org") "* %?\n\n%^T\n\n:PROPERTIES:\n\n:END:\n\n")
("cp" "Personal Event" entry (file "~/Documents/org/calendars/personal.org") "* %?\n\n%^T\n\n:PROPERTIES:\n\n:END:\n\n")
("i" "Inbox")
("iw" "Work Inbox" entry (file+olp "~/Documents/org/roam/Inbox.org" "Work") "* TODO %?\nSCHEDULED: %(org-insert-time-stamp (org-read-date nil t \"+0d\"))\n%a\n" :prepend t)
("ip" "Personal Inbox" entry (file+olp "~/Documents/org/roam/Inbox.org" "Personal") "* TODO %?\nSCHEDULED: %(org-insert-time-stamp (org-read-date nil t \"+0d\"))\n%a\n" :prepend t)
("e" "Email Workflow")
("ef" "Follow Up" entry (file+olp "~/Documents/org/raom/Inbox.org" "Email" "Follow Up") "* TODO Follow up with %:fromname on %a :email:\nSCHEDULED:%t\nDEADLINE: %(org-insert-time-stamp (org-read-date nil t \"+2d\"))\n\n%i" :immediate-finish t)
("er" "Read Later" entry (file+olp "~/Documents/org/roam/Inbox.org" "Email" "Read Later") "* TODO Read %:subject :email: \nSCHEDULED:%t\nDEADLINE: %(org-insert-time-stamp (org-read-date nil t \"+2d\"))\n\n%a\n\n%i" :immediate-finish t)
("p" "Project" entry (file+headline "~/Documents/org/roam/Projects.org" "Projects")(file "~/Documents/org/templates/project.orgtmpl"))
("d" "System design" entry (file+headline "~/Documents/org/system-design/system-design.org" "System Design") (file "~/Documents/org/templates/system-design.orgtmpl"))
("b" "BJJ")
("bm" "Moves" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Moves")(file "~/Documents/org/templates/bjj-move.orgtmpl"))
("bs" "Submission" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Submissions")(file "~/Documents/org/templates/bjj-submission.orgtmpl"))
("bc" "Choke" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Chokes")(file "~/Documents/org/templates/bjj-choke.orgtmpl"))
("bw" "Sweeps" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Sweeps")(file "~/Documents/org/templates/bjj-sweep.orgtmpl"))
("be" "Escapes" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Escapes")(file "~/Documents/org/templates/bjj-escape.orgtmpl"))
("bt" "Takedowns" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Takedowns")(file "~/Documents/org/templates/bjj-takedown.orgtmpl"))
("bp" "Passes" entry (file+olp "~/Documents/org/bjj/BJJ.org" "Techniques" "Passes")(file "~/Documents/org/templates/bjj-pass.orgtmpl"))
("bf" "FAQ" entry (file+olp "~/Documents/org/bjj/BJJ.org" "FAQ")(file "~/Documents/org/templates/bjj-faq.orgtmpl"))
("h" "Habit" entry (file+olp "~/Documents/org/habits.org" "Habits") (file "~/Documents/org/templates/habit.orgtmpl"))
("f" "Flashcards")
("fq" "Quotes" entry (file+headline "~/Documents/org/flashcards/quotes.org" "Quotes") "* %?\n%u" :prepend t)
("fS" "Stories" entry (file+headline "~/Documents/org/flashcards/stories.org" "Stories") "* Story :drill:\n %t\n %^{The story}\n")
("fe" "Emacs")
("fef" "Emacs facts" entry (file+headline "~/Documents/org/flashcards/emacs.org" "Emacs") "* Fact :drill:\n %t\n %^{The fact}\n")
("feq" "Emacs questions" entry (file+headline "~/Documents/org/flashcards/emacs.org" "Emacs") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fh" "History")
("fhf" "History facts" entry (file+headline "~/Documents/org/flashcards/history.org" "History") "* Fact :drill:\n %t\n %^{The fact}\n")
("fhq" "History questions" entry (file+headline "~/Documents/org/flashcards/history.org" "History") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fm" "Maths")
("fmf" "Math facts" entry (file+headline "~/Documents/org/flashcards/maths.org" "Maths") "* Fact :drill:\n %t\n %^{The fact}\n")
("fmq" "Math questions" entry (file+headline "~/Documents/org/flashcards/maths.org" "Maths") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fc" "Computer Science")
("fcf" "Computer Science facts" entry (file+headline "~/Documents/org/flashcards/computer-science.org" "Computer Science") "* Fact :drill:\n %t\n %^{The fact}\n")
("fcq" "Computer Science questions" entry (file+headline "~/Documents/org/flashcards/computer-science.org" "Computer Science") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fs" "Sports")
("fsf" "Sports facts" entry (file+headline "~/Documents/org/flashcards/sports.org" "Sports") "* Fact :drill:\n %t\n %^{The fact}\n")
("fsq" "Sports questions" entry (file+headline "~/Documents/org/flashcards/sports.org" "Sports") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fn" "Nutrition")
("ft" "Trading")
("ftf" "Trading facts" entry (file+headline "~/Documents/org/flashcards/trading.org" "Trading") "* Fact :drill:\n %t\n %^{The fact}\n")
("ftq" "Trading questions" entry (file+headline "~/Documents/org/flashcards/trading.org" "Trading") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}")
("fl" "Languages")
("fls" "Spanish" entry (file+headline "~/Documents/org/flashcards/languages/spanish.org" "Spanish") "* Question :drill:\n %t\n %^{The question} \n** Answer: \n%^{The answer}"))))
(defadvice org-switch-to-buffer-other-window
(after supress-window-splitting activate)
"Delete the extra window if we're in a capture frame"
(if (equal "org-capture" (frame-parameter nil 'name))
(delete-other-windows)))
(defadvice org-capture-finalize
(after delete-capture-frame activate)
"Advise capture-finalize to close the frame"
(when (and (equal "org-capture" (frame-parameter nil 'name))
(not (eq this-command 'org-capture-refile)))
(delete-frame)))
(defadvice org-capture-refile
(after delete-capture-frame activate)
"Advise org-refile to close the frame"
(delete-frame))
;;;###autoload
(defun my/org-drill ()
"Require, configure and call org-drill."
(interactive)
(require 'org-drill)
(let ((org-drill-scope 'directory))
(find-file "~/Documents/org/roam/index.org")
(org-drill)
(org-save-all-org-buffers)))
;;;###autoload
(defun my/org-drill-buffer ()
"Require, configure and call org-drill."
(interactive)
(require 'org-drill)
(let ((org-drill-scope 'file))
(org-drill)
(org-save-all-org-buffers)))
:init (setq org-drill-scope 'directory)
;;;###autoload
(defun my/org-drill-match ()
"Require, configure and call org-drill."
(interactive)
(require 'org-drill)
(let ((org-drill-scope 'directory)
(org-drill-match (read-string "Please specify a filter (e.g. tag, property etc) for the drill: ")))
(find-file "~/Documents/org/roam/index.org")
(org-drill)
(org-save-all-org-buffers)))
(use-package! org-drill :after org)
(use-package! org-habit
:after org
:config
(setq org-habit-following-days 7
org-habit-preceding-days 35
org-habit-show-habits t)
(defvar my/org-habit-capture-alist '() "An association list that maps capture keys to habit headings")
(defun my/org-habit-check-captured ()
"Check if there is a habit matching that latest captured item and mark it as done."
(message "Checking for habits linked to the captured template ...")
(let* ((key (plist-get org-capture-plist :key))
(habit (cdr (assoc key my/org-habit-capture-alist))))
(if habit
(progn
(message "Found linked habit:%s" habit)
(when (not org-note-abort) (my/org-habit-mark habit)))
(message "No habit found for capture template with key:%s." key))))
(defun my/org-habit-mark (heading)
(save-excursion
(let* ((habits-file "/home/iocanel/Documents/org/habits.org")
(original (current-buffer))
(buf (find-file habits-file)))
(with-current-buffer buf
(goto-char (point-min))
(re-search-forward (concat "TODO " heading ".*:habit:"))
(org-habit-parse-todo)
(org-todo 'done)
(save-buffer t))
(switch-to-buffer original t t))))
(advice-add 'org-drill :after (lambda() (my/org-habit-mark "Org Drill")))
(add-hook 'org-capture-after-finalize-hook 'my/org-habit-check-captured))
(defun my/dired-file-as-plantuml-link-to-clipboard ()
"Create an Org link to the currently selected file in Dired and copy it to the clipboard."
(interactive)
(let* ((file (dired-get-filename))
(name (file-name-base file))
(cleaned-name (replace-regexp-in-string "^[0-9]+\\(\\.\\)[[:blank:]]+" "" name))
(extension (file-name-extension file))
(link (format "[[\"file:%s\" %s]]" file cleaned-name)))
(kill-new link)
(message "Plantuml link to file copied to clipboard: %s" file)))
(defun my/dired-file-as-org-link-to-clipboard ()
"Create an Org link to the currently selected file in Dired and copy it to the clipboard."
(interactive)
(let* ((file (dired-get-filename))
(name (file-name-base file))
(cleaned-name (replace-regexp-in-string "^[0-9]+\\(\\.\\)[[:blank:]]+" "" name))
(extension (file-name-extension file))
(protocol (if (string-match-p "\\(\\.\\(mp4\\|mkv\\|avi\\)\\)$" file) "mpv" "file"))
(link (format "[[%s:%s][%s]]" protocol file cleaned-name)))
(kill-new link)
(message "Org link to file copied to clipboard: %s" file)))
(after! dired
(define-key dired-mode-map (kbd "C-c o l") 'my/dired-file-as-org-link-to-clipboard)
(define-key dired-mode-map (kbd "C-c u l") 'my/dired-file-as-plantuml-link-to-clipboard))
(use-package! org-github-issues
:init
(defvar my/github-repositories nil "The list of watch repositories by org-github-issues")
:commands (org-github-issues-sync-all my/org-github-issues-eww-at-point my/org-github-issues--show-open-workspace-issues)
:config
(setq
gh-user "iocanel"
org-github-issues-user "iocanel"
org-github-issues-org-file "~/Documents/org/github.org"
org-github-issues-tags '("github")
org-github-issues-issue-tags '("issue")
org-github-issues-pull-tags '("pull")
org-github-issues-tag-transformations '((".*" "")) ;; force all labels to empty string so that they can be ommitted.
org-github-issues-auto-schedule "+0d"
org-github-issues-filter-by-assignee t
org-github-issues-headline-prefix t))
(defun my/org-github-issues-eww-at-point ()
"Browse the issue that corresponds to the org entry at point."
(interactive)
(let ((url (my/org-github-issues--url-at-point)))
(when url
(other-window 1)
;(idee/jump-to-non-ide-window)
(split-window-horizontally)
(eww url))))
(defun my/org-github-issues--show-open-project-issues (root)
"Show all the project issues currently assigned to me."
(let* ((project (projectile-ensure-project root))
(project-name (projectile-project-name project)))
(org-ql-search org-github-issues-org-file
`(and (property "GH_URL")
(string-match (regexp-quote ,project-name) (org-entry-get (point) "GH_URL")))
:title (format "Github issues for %s" project-name))
(goto-char (point-min))
(org-agenda-next-line)))
(defun my/org-github-issues--show-open-workspace-issues (workspace)
"Show all the workspace issues currently assigned to me."
(let* ((name (treemacs-project->name workspace))
(projects (treemacs-workspace->projects workspace))
(project-names (mapcar (lambda (p) (treemacs-project->name p)) projects))
(main-project (car project-names)))
(when main-project
(org-ql-search org-github-issues-org-file
`(and (property "GH_URL")
(or (string-match (regexp-quote ,main-project) (org-entry-get (point) "GH_URL"))
(seq-filter (lambda (p) (string-match (regexp-quote p) (org-entry-get (point) "GH_URL"))) project-names)))
:title (format "Github issues for %s" name))
(goto-char (point-min))
(org-agenda-next-line))))
(defun my/org-github-issues--url-at-point ()
"Utility that fetches the url of the issue at point."
(save-excursion
(let ((origin (current-buffer)))
(when (eq major-mode 'org-agenda-mode) (org-agenda-switch-to))
(let* ((p (point))
(url (string-trim (org-entry-get nil "GH_URL"))))
(when (not (equal origin (current-buffer))) (switch-to-buffer origin))
url))))
(use-package! org-jira
:commands (my/org-jira-get-issues my/org-jira-hydra my/org-jira-get-issues my/org-jira-select-board my/org-jira-select-spring)
:custom (org-jira-property-overrides '("CUSTOM_ID" "self"))
:bind (:map evil-normal-state-map ("SPC j" . org-jira-hydra))
:config
(setq jiralib-url "https://issues.redhat.com/"
jiralib-user-login-name "ikanello1@redhat.com"
jira-password nil
jira-token (replace-regexp-in-string "\n\\'" "" (shell-command-to-string "pass show websites/redhat.com/ikanello1@redhat.com/token"))
org-jira-working-dir "~/Documents/org/jira/"
org-jira-projects-list '("ENTSBT" "SB" "QUARKUS"))
(setq jiralib-token `("Authorization" . ,(concat "Bearer " jira-token))))
(defun my/org-jira-get-issues ()
"Sync using org-jira and postprocess."
(interactive)
(org-jira-get-issues (org-jira-get-issue-list org-jira-get-issue-list-callback))
(my/org-jira-postprocess))
(defun my/org-jira-issue-id-at-point ()
"Returns the ID of the current issue."
(save-excursion
(org-previous-visible-heading 1)
(org-element-property :ID (org-element-at-point))))