-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
config.el
1495 lines (1342 loc) · 62.3 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
;;; lang/org/config.el -*- lexical-binding: t; -*-
(defvar +org-babel-native-async-langs '(python)
"Languages that will use `ob-comint' instead of `ob-async' for `:async'.")
(defvar +org-babel-mode-alist
'((c . C)
(cpp . C)
(C++ . C)
(D . C)
(elisp . emacs-lisp)
(sh . shell)
(bash . shell)
(matlab . octave)
(rust . rustic-babel)
(amm . ammonite))
"An alist mapping languages to babel libraries. This is necessary for babel
libraries (ob-*.el) that don't match the name of the language.
For example, (fish . shell) will cause #+begin_src fish blocks to load
ob-shell.el when executed.")
(defvar +org-babel-load-functions ()
"A list of functions executed to load the current executing src block. They
take one argument (the language specified in the src block, as a string). Stops
at the first function to return non-nil.")
(defvar +org-capture-todo-file "todo.org"
"Default target for todo entries.
Is relative to `org-directory', unless it is absolute. Is used in Doom's default
`org-capture-templates'.")
(defvar +org-capture-changelog-file "changelog.org"
"Default target for changelog entries.
Is relative to `org-directory' unless it is absolute. Is used in Doom's default
`org-capture-templates'.")
(defvar +org-capture-notes-file "notes.org"
"Default target for storing notes.
Used as a fall back file for org-capture.el, for templates that do not specify a
target file.
Is relative to `org-directory', unless it is absolute. Is used in Doom's default
`org-capture-templates'.")
(defvar +org-capture-journal-file "journal.org"
"Default target for storing timestamped journal entries.
Is relative to `org-directory', unless it is absolute. Is used in Doom's default
`org-capture-templates'.")
(defvar +org-capture-projects-file "projects.org"
"Default, centralized target for org-capture templates.")
(defvar +org-habit-graph-padding 2
"The padding added to the end of the consistency graph")
(defvar +org-habit-min-width 30
"Hides the consistency graph if the `org-habit-graph-column' is less than this value")
(defvar +org-habit-graph-window-ratio 0.3
"The ratio of the consistency graphs relative to the window width")
(defvar +org-startup-with-animated-gifs nil
"If non-nil, and the cursor is over a gif inline-image preview, animate it!")
;;
;;; `org-load' hooks
(defun +org-init-org-directory-h ()
(unless org-directory
(setq-default org-directory "~/org"))
(unless org-id-locations-file
(setq org-id-locations-file (expand-file-name ".orgids" org-directory))))
(defun +org-init-agenda-h ()
(unless org-agenda-files
(setq-default org-agenda-files (list org-directory)))
(setq-default
;; Different colors for different priority levels
org-agenda-deadline-faces
'((1.001 . error)
(1.0 . org-warning)
(0.5 . org-upcoming-deadline)
(0.0 . org-upcoming-distant-deadline))
;; Don't monopolize the whole frame just for the agenda
org-agenda-window-setup 'current-window
org-agenda-skip-unavailable-files t
;; Shift the agenda to show the previous 3 days and the next 7 days for
;; better context on your week. The past is less important than the future.
org-agenda-span 10
org-agenda-start-on-weekday nil
org-agenda-start-day "-3d"
;; Optimize `org-agenda' by inhibiting extra work while opening agenda
;; buffers in the background. They'll be "restarted" if the user switches to
;; them anyway (see `+org-exclude-agenda-buffers-from-workspace-h')
org-agenda-inhibit-startup t))
(defun +org-init-appearance-h ()
"Configures the UI for `org-mode'."
(setq org-indirect-buffer-display 'current-window
org-enforce-todo-dependencies t
org-entities-user
'(("flat" "\\flat" nil "" "" "266D" "♭")
("sharp" "\\sharp" nil "" "" "266F" "♯"))
org-fontify-done-headline t
org-fontify-quote-and-verse-blocks t
org-fontify-whole-heading-line t
org-hide-leading-stars t
org-image-actual-width nil
org-imenu-depth 6
org-priority-faces
'((?A . error)
(?B . warning)
(?C . success))
org-startup-indented t
org-tags-column 0
org-use-sub-superscripts '{}
;; `showeverything' is org's default, but it doesn't respect
;; `org-hide-block-startup' (#+startup: hideblocks), archive trees,
;; hidden drawers, or VISIBILITY properties. `nil' is equivalent, but
;; respects these settings.
org-startup-folded nil)
(setq org-refile-targets
'((nil :maxlevel . 3)
(org-agenda-files :maxlevel . 3))
;; Without this, completers like ivy/helm are only given the first level of
;; each outline candidates. i.e. all the candidates under the "Tasks" heading
;; are just "Tasks/". This is unhelpful. We want the full path to each refile
;; target! e.g. FILE/Tasks/heading/subheading
org-refile-use-outline-path 'file
org-outline-path-complete-in-steps nil)
(plist-put org-format-latex-options :scale 1.5) ; larger previews
;; HACK Face specs fed directly to `org-todo-keyword-faces' don't respect
;; underlying faces like the `org-todo' face does, so we define our own
;; intermediary faces that extend from org-todo.
(with-no-warnings
(custom-declare-face '+org-todo-active '((t (:inherit (bold font-lock-constant-face org-todo)))) "")
(custom-declare-face '+org-todo-project '((t (:inherit (bold font-lock-doc-face org-todo)))) "")
(custom-declare-face '+org-todo-onhold '((t (:inherit (bold warning org-todo)))) "")
(custom-declare-face '+org-todo-cancel '((t (:inherit (bold error org-todo)))) ""))
(setq org-todo-keywords
'((sequence
"TODO(t)" ; A task that needs doing & is ready to do
"PROJ(p)" ; A project, which usually contains other tasks
"LOOP(r)" ; A recurring task
"STRT(s)" ; A task that is in progress
"WAIT(w)" ; Something external is holding up this task
"HOLD(h)" ; This task is paused/on hold because of me
"IDEA(i)" ; An unconfirmed and unapproved task or notion
"|"
"DONE(d)" ; Task successfully completed
"KILL(k)") ; Task was cancelled, aborted, or is no longer applicable
(sequence
"[ ](T)" ; A task that needs doing
"[-](S)" ; Task is in progress
"[?](W)" ; Task is being held up or paused
"|"
"[X](D)") ; Task was completed
(sequence
"|"
"OKAY(o)"
"YES(y)"
"NO(n)"))
org-todo-keyword-faces
'(("[-]" . +org-todo-active)
("STRT" . +org-todo-active)
("[?]" . +org-todo-onhold)
("WAIT" . +org-todo-onhold)
("HOLD" . +org-todo-onhold)
("PROJ" . +org-todo-project)
("NO" . +org-todo-cancel)
("KILL" . +org-todo-cancel)))
(set-ligatures! 'org-mode
:name "#+NAME:"
:name "#+name:"
:src_block "#+BEGIN_SRC"
:src_block "#+begin_src"
:src_block_end "#+END_SRC"
:src_block_end "#+end_src"
:quote "#+BEGIN_QUOTE"
:quote "#+begin_quote"
:quote_end "#+END_QUOTE"
:quote_end "#+end_quote"))
(defun +org-init-babel-h ()
(setq org-src-preserve-indentation t ; use native major-mode indentation
org-src-tab-acts-natively t ; we do this ourselves
;; You don't need my permission (just be careful, mkay?)
org-confirm-babel-evaluate nil
org-link-elisp-confirm-function nil
;; Show src buffer in popup, and don't monopolize the frame
org-src-window-setup 'other-window
;; Our :lang common-lisp module uses sly, so...
org-babel-lisp-eval-fn #'sly-eval)
;; I prefer C-c C-c over C-c ' (more consistent)
(define-key org-src-mode-map (kbd "C-c C-c") #'org-edit-src-exit)
;; Don't process babel results asynchronously when exporting org, as they
;; won't likely complete in time, and will instead output an ob-async hash
;; instead of the wanted evaluation results.
(after! ob
(add-to-list 'org-babel-default-lob-header-args '(:sync)))
(defadvice! +org-babel-disable-async-maybe-a (fn &optional orig-fn arg info params)
"Use ob-comint where supported, disable async altogether where it isn't.
We have access to two async backends: ob-comint or ob-async, which have
different requirements. This advice tries to pick the best option between them,
falling back to synchronous execution otherwise. Without this advice, they die
with an error; terrible UX!
Note: ob-comint support will only kick in for languages listed in
`+org-babel-native-async-langs'.
Also adds support for a `:sync' parameter to override `:async'."
:around #'ob-async-org-babel-execute-src-block
(if (null orig-fn)
(funcall fn orig-fn arg info params)
(let* ((info (or info (org-babel-get-src-block-info)))
(params (org-babel-merge-params (nth 2 info) params)))
(if (or (assq :sync params)
(not (assq :async params))
(member (car info) ob-async-no-async-languages-alist)
;; ob-comint requires a :session, ob-async does not, so fall
;; back to ob-async if no :session is provided.
(unless (member (alist-get :session params) '("none" nil))
(unless (memq (let* ((lang (nth 0 info))
(lang (cond ((symbolp lang) lang)
((stringp lang) (intern lang)))))
(or (alist-get lang +org-babel-mode-alist)
lang))
+org-babel-native-async-langs)
(message "Org babel: %s :session is incompatible with :async. Executing synchronously!"
(car info))
(sleep-for 0.2))
t))
(funcall orig-fn arg info params)
(funcall fn orig-fn arg info params)))))
;; HACK Fix #6061. Seems `org-babel-do-in-edit-buffer' has the side effect of
;; deleting side windows. Should be reported upstream! This advice
;; suppresses this behavior wherever it is known to be used.
(defadvice! +org-fix-window-excursions-a (fn &rest args)
"Suppress changes to the window config anywhere
`org-babel-do-in-edit-buffer' is used."
:around #'evil-org-open-below
:around #'evil-org-open-above
:around #'org-indent-region
:around #'org-indent-line
(save-window-excursion (apply fn args)))
(defadvice! +org-fix-newline-and-indent-in-src-blocks-a (&optional indent _arg _interactive)
"Mimic `newline-and-indent' in src blocks w/ lang-appropriate indentation."
:after #'org-return
(when (and indent
org-src-tab-acts-natively
(org-in-src-block-p t))
(save-window-excursion
(org-babel-do-in-edit-buffer
(call-interactively #'indent-for-tab-command)))))
(defadvice! +org-inhibit-mode-hooks-a (fn datum name &optional initialize &rest args)
"Prevent potentially expensive mode hooks in `org-babel-do-in-edit-buffer' ops."
:around #'org-src--edit-element
(apply fn datum name
(if (and (eq org-src-window-setup 'switch-invisibly)
(functionp initialize))
;; org-babel-do-in-edit-buffer is used to execute quick, one-off
;; logic in the context of another major mode, but initializing a
;; major mode with expensive hooks can be terribly expensive.
;; Since Doom adds its most expensive hooks to
;; MAJOR-MODE-local-vars-hook, we can savely inhibit those.
(lambda ()
(let ((doom-inhibit-local-var-hooks t))
(funcall initialize)))
initialize)
args))
;; Refresh inline images after executing src blocks (useful for plantuml or
;; ipython, where the result could be an image)
(add-hook! 'org-babel-after-execute-hook
(defun +org-redisplay-inline-images-in-babel-result-h ()
(unless (or
;; ...but not while Emacs is exporting an org buffer (where
;; `org-display-inline-images' can be awfully slow).
(bound-and-true-p org-export-current-backend)
;; ...and not while tangling org buffers (which happens in a temp
;; buffer where `buffer-file-name' is nil).
(string-match-p "^ \\*temp" (buffer-name)))
(save-excursion
(when-let ((beg (org-babel-where-is-src-block-result))
(end (progn (goto-char beg) (forward-line) (org-babel-result-end))))
(org-display-inline-images nil nil (min beg end) (max beg end)))))))
(after! python
(unless org-babel-python-command
(setq org-babel-python-command
(string-trim
(concat python-shell-interpreter " "
(if (string-match-p "\\<i?python[23]?$" python-shell-interpreter)
(replace-regexp-in-string
"\\(^\\| \\)-i\\( \\|$\\)" " " python-shell-interpreter-args)
python-shell-interpreter-args))))))
(after! ob-ditaa
;; TODO Should be fixed upstream
(let ((default-directory (org-find-library-dir "org-contribdir")))
(setq org-ditaa-jar-path (expand-file-name "scripts/ditaa.jar")
org-ditaa-eps-jar-path (expand-file-name "scripts/DitaaEps.jar")))))
(defun +org-init-babel-lazy-loader-h ()
"Load babel libraries lazily when babel blocks are executed."
(defun +org--babel-lazy-load (lang &optional async)
(cl-check-type lang (or symbol null))
(unless (cdr (assq lang org-babel-load-languages))
(when async
;; ob-async has its own agenda for lazy loading packages (in the child
;; process), so we only need to make sure it's loaded.
(require 'ob-async nil t))
(prog1 (or (run-hook-with-args-until-success '+org-babel-load-functions lang)
(require (intern (format "ob-%s" lang)) nil t)
(require lang nil t))
(add-to-list 'org-babel-load-languages (cons lang t)))))
(defadvice! +org--export-lazy-load-library-h (&optional element)
"Lazy load a babel package when a block is executed during exporting."
:before #'org-babel-exp-src-block
(+org--babel-lazy-load-library-a (org-babel-get-src-block-info nil element)))
(defadvice! +org--src-lazy-load-library-a (lang)
"Lazy load a babel package to ensure syntax highlighting."
:before #'org-src--get-lang-mode
(or (cdr (assoc lang org-src-lang-modes))
(+org--babel-lazy-load lang)))
;; This also works for tangling
(defadvice! +org--babel-lazy-load-library-a (info)
"Load babel libraries lazily when babel blocks are executed."
:after-while #'org-babel-confirm-evaluate
(let* ((lang (nth 0 info))
(lang (cond ((symbolp lang) lang)
((stringp lang) (intern lang))))
(lang (or (cdr (assq lang +org-babel-mode-alist))
lang)))
(+org--babel-lazy-load
lang (and (not (assq :sync (nth 2 info)))
(assq :async (nth 2 info))))
t))
(advice-add #'org-babel-do-load-languages :override #'ignore))
(defun +org-init-capture-defaults-h ()
"Sets up some reasonable defaults, as well as two `org-capture' workflows that
I like:
1. The traditional way: invoking `org-capture' directly, via SPC X, or through
the :cap ex command.
2. Through a org-capture popup frame that is invoked from outside Emacs (the
~/.emacs.d/bin/org-capture script). This can be invoked from qutebrowser,
vimperator, dmenu or a global keybinding."
(setq org-default-notes-file
(expand-file-name +org-capture-notes-file org-directory)
+org-capture-journal-file
(expand-file-name +org-capture-journal-file org-directory)
org-capture-templates
'(("t" "Personal todo" entry
(file+headline +org-capture-todo-file "Inbox")
"* [ ] %?\n%i\n%a" :prepend t)
("n" "Personal notes" entry
(file+headline +org-capture-notes-file "Inbox")
"* %u %?\n%i\n%a" :prepend t)
("j" "Journal" entry
(file+olp+datetree +org-capture-journal-file)
"* %U %?\n%i\n%a" :prepend t)
;; Will use {project-root}/{todo,notes,changelog}.org, unless a
;; {todo,notes,changelog}.org file is found in a parent directory.
;; Uses the basename from `+org-capture-todo-file',
;; `+org-capture-changelog-file' and `+org-capture-notes-file'.
("p" "Templates for projects")
("pt" "Project-local todo" entry ; {project-root}/todo.org
(file+headline +org-capture-project-todo-file "Inbox")
"* TODO %?\n%i\n%a" :prepend t)
("pn" "Project-local notes" entry ; {project-root}/notes.org
(file+headline +org-capture-project-notes-file "Inbox")
"* %U %?\n%i\n%a" :prepend t)
("pc" "Project-local changelog" entry ; {project-root}/changelog.org
(file+headline +org-capture-project-changelog-file "Unreleased")
"* %U %?\n%i\n%a" :prepend t)
;; Will use {org-directory}/{+org-capture-projects-file} and store
;; these under {ProjectName}/{Tasks,Notes,Changelog} headings. They
;; support `:parents' to specify what headings to put them under, e.g.
;; :parents ("Projects")
("o" "Centralized templates for projects")
("ot" "Project todo" entry
(function +org-capture-central-project-todo-file)
"* TODO %?\n %i\n %a"
:heading "Tasks"
:prepend nil)
("on" "Project notes" entry
(function +org-capture-central-project-notes-file)
"* %U %?\n %i\n %a"
:heading "Notes"
:prepend t)
("oc" "Project changelog" entry
(function +org-capture-central-project-changelog-file)
"* %U %?\n %i\n %a"
:heading "Changelog"
:prepend t)))
;; Kill capture buffers by default (unless they've been visited)
(after! org-capture
(org-capture-put :kill-buffer t))
;; Fix #462: when refiling from org-capture, Emacs prompts to kill the
;; underlying, modified buffer. This fixes that.
(add-hook 'org-after-refile-insert-hook #'save-buffer)
;; HACK Doom doesn't support `customize'. Best not to advertise it as an
;; option in `org-capture's menu.
(defadvice! +org--remove-customize-option-a (fn table title &optional prompt specials)
:around #'org-mks
(funcall fn table title prompt
(remove '("C" "Customize org-capture-templates")
specials)))
(defadvice! +org--capture-expand-variable-file-a (file)
"If a variable is used for a file path in `org-capture-template', it is used
as is, and expanded relative to `default-directory'. This changes it to be
relative to `org-directory', unless it is an absolute path."
:filter-args #'org-capture-expand-file
(if (and (symbolp file) (boundp file))
(expand-file-name (symbol-value file) org-directory)
file))
(add-hook! 'org-capture-mode-hook
(defun +org-show-target-in-capture-header-h ()
(setq header-line-format
(format "%s%s%s"
(propertize (abbreviate-file-name (buffer-file-name (buffer-base-buffer)))
'face 'font-lock-string-face)
org-eldoc-breadcrumb-separator
header-line-format)))))
(defun +org-init-capture-frame-h ()
(add-hook 'org-capture-after-finalize-hook #'+org-capture-cleanup-frame-h)
(defadvice! +org-capture-refile-cleanup-frame-a (&rest _)
:after #'org-capture-refile
(+org-capture-cleanup-frame-h))
(when (modulep! :ui doom-dashboard)
(add-hook '+doom-dashboard-inhibit-functions #'+org-capture-frame-p)))
(defun +org-init-attachments-h ()
"Sets up org's attachment system."
(setq org-attach-store-link-p 'attached ; store link after attaching files
org-attach-use-inheritance t) ; inherit properties from parent nodes
;; Autoload all these commands that org-attach doesn't autoload itself
(use-package! org-attach
:commands (org-attach-delete-one
org-attach-delete-all
org-attach-new
org-attach-open
org-attach-open-in-emacs
org-attach-reveal-in-emacs
org-attach-url
org-attach-set-directory
org-attach-sync)
:config
(unless org-attach-id-dir
;; Centralized attachments directory by default
(setq-default org-attach-id-dir (expand-file-name ".attach/" org-directory)))
(after! projectile
(add-to-list 'projectile-globally-ignored-directories org-attach-id-dir)))
;; Add inline image previews for attachment links
(org-link-set-parameters "attachment" :image-data-fun #'+org-inline-image-data-fn))
(defun +org-init-custom-links-h ()
;; Modify default file: links to colorize broken file links red
(org-link-set-parameters
"file" :face (lambda (path)
(if (or
;; file uris is not a valid path on windows
;; ref https://lists.gnu.org/archive/html/bug-gnu-emacs/2024-05/threads.html#00729
;; emacs <= 29 crashes for (file-exists-p "file://whatever")
(if (featurep :system 'windows) (string-prefix-p "//" path))
(file-remote-p path)
;; filter out network shares on windows (slow)
(if (featurep :system 'windows) (string-prefix-p "\\\\" path))
(file-exists-p path))
'org-link
'(warning org-link))))
;; Additional custom links for convenience
(pushnew! org-link-abbrev-alist
'("github" . "https://github.com/%s")
'("youtube" . "https://youtube.com/watch?v=%s")
'("google" . "https://google.com/search?q=")
'("gimages" . "https://google.com/images?q=%s")
'("gmap" . "https://maps.google.com/maps?q=%s")
'("kagi" . "https://kagi.com/search?q=%s")
'("duckduckgo" . "https://duckduckgo.com/?q=%s")
'("wikipedia" . "https://en.wikipedia.org/wiki/%s")
'("wolfram" . "https://wolframalpha.com/input/?i=%s")
'("doom-repo" . "https://github.com/doomemacs/doomemacs/%s")
`("emacsdir" . ,(doom-path doom-emacs-dir "%s"))
`("doomdir" . ,(doom-path doom-user-dir "%s")))
(+org-define-basic-link "org" 'org-directory)
(+org-define-basic-link "doom" 'doom-emacs-dir)
(+org-define-basic-link "doom-docs" 'doom-docs-dir)
(+org-define-basic-link "doom-modules" 'doom-modules-dir)
;; Add "lookup" links for packages and keystrings; useful for Emacs
;; documentation -- especially Doom's!
(letf! ((defun -call-interactively (fn)
(lambda (path _prefixarg)
(funcall
fn (or (intern-soft path)
(user-error "Can't find documentation for %S" path))))))
(org-link-set-parameters
"kbd"
:follow (lambda (ev)
(interactive "e")
(minibuffer-message "%s" (+org-link-doom--help-echo-from-textprop
nil (current-buffer) (posn-point (event-start ev)))))
:help-echo #'+org-link-doom--help-echo-from-textprop
:face 'help-key-binding)
(org-link-set-parameters
"var"
:follow (-call-interactively #'helpful-variable)
:activate-func #'+org-link--var-link-activate-fn
:face '(font-lock-variable-name-face underline))
(org-link-set-parameters
"fn"
:follow (-call-interactively #'helpful-callable)
:activate-func #'+org-link--fn-link-activate-fn
:face '(font-lock-function-name-face underline))
(org-link-set-parameters
"face"
:follow (-call-interactively #'describe-face)
:activate-func #'+org-link--face-link-activate-fn
:face '(font-lock-type-face underline))
(org-link-set-parameters
"cmd"
:follow (-call-interactively #'describe-command)
:activate-func #'+org-link--command-link-activate-fn
:face 'help-key-binding
:help-echo #'+org-link-doom--help-echo-from-textprop)
(org-link-set-parameters
"doom-package"
:follow #'+org-link--doom-package-link-follow-fn
:activate-func #'+org-link--doom-package-link-activate-fn
:help-echo #'+org-link-doom--help-echo-from-textprop)
(org-link-set-parameters
"doom-module"
:follow #'+org-link--doom-module-link-follow-fn
:activate-func #'+org-link--doom-module-link-activate-fn
:help-echo #'+org-link-doom--help-echo-from-textprop)
(org-link-set-parameters
"doom-executable"
:activate-func #'+org-link--doom-executable-link-activate-fn
:help-echo #'+org-link-doom--help-echo-from-textprop
:face 'org-verbatim)
(org-link-set-parameters
"doom-ref"
:follow (lambda (link)
(let ((link (+org-link-read-desc-at-point link))
(url "https://github.com")
(doom-repo "doomemacs/doomemacs"))
(save-match-data
(browse-url
(cond ((string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(format "%s/%s/issues/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link)
(format "%s/%s/commit/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((user-error "Invalid doom-ref link: %S" link)))))))
:face (lambda (link)
(let ((link (+org-link-read-desc-at-point link)))
(if (or (string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link))
'org-link
'error))))
(org-link-set-parameters
"doom-user"
:follow (lambda (link)
(browse-url
(format "https://github.com/%s"
(string-remove-prefix
"@" (+org-link-read-desc-at-point link)))))
:face (lambda (_)
;; Avoid confusion with function `org-priority'
'org-priority))
(org-link-set-parameters
"doom-changelog"
:follow (lambda (link)
(find-file (doom-path doom-docs-dir "changelog.org"))
(org-match-sparse-tree nil link))))
;; TODO PR this upstream
(defadvice! +org--follow-search-string-a (fn link &optional arg)
"Support ::SEARCH syntax for id: links."
:around #'org-id-open
:around #'org-roam-id-open
(save-match-data
(cl-destructuring-bind (id &optional search)
(split-string link "::")
(prog1 (funcall fn id arg)
(cond ((null search))
((string-match-p "\\`[0-9]+\\'" search)
;; Move N lines after the ID (in case it's a heading), instead
;; of the start of the buffer.
(forward-line (string-to-number option)))
((string-match "^/\\([^/]+\\)/$" search)
(let ((match (match-string 1 search)))
(save-excursion (org-link-search search))
;; `org-link-search' only reveals matches. Moving the point
;; to the first match after point is a sensible change.
(when (re-search-forward match)
(goto-char (match-beginning 0)))))
((org-link-search search)))))))
;; Add "lookup" links for packages and keystrings; useful for Emacs
;; documentation -- especially Doom's!
;; Allow inline image previews of http(s)? urls or data uris.
;; `+org-http-image-data-fn' will respect `org-display-remote-inline-images'.
(setq org-display-remote-inline-images 'download) ; TRAMP urls
(org-link-set-parameters "http" :image-data-fun #'+org-http-image-data-fn)
(org-link-set-parameters "https" :image-data-fun #'+org-http-image-data-fn)
(org-link-set-parameters "img" :image-data-fun #'+org-inline-image-data-fn)
;; Add support for youtube links + previews
(require 'org-yt nil t)
(defadvice! +org-dont-preview-if-disabled-a (&rest _)
"Make `org-yt' respect `org-display-remote-inline-images'."
:before-while #'org-yt-image-data-fun
(not (eq org-display-remote-inline-images 'skip))))
(defun +org-init-export-h ()
"TODO"
(setq org-export-with-smart-quotes t
org-html-validation-link nil
org-latex-prefer-user-labels t)
(when (modulep! :lang markdown)
(add-to-list 'org-export-backends 'md))
(use-package! ox-hugo
:when (modulep! +hugo)
:after ox)
(use-package! ox-pandoc
:when (modulep! +pandoc)
:when (executable-find "pandoc")
:after ox
:init
(add-to-list 'org-export-backends 'pandoc)
(setq org-pandoc-options
'((standalone . t)
(mathjax . t)
(variable . "revealjs-url=https://revealjs.com"))))
(defadvice! +org--dont-trigger-save-hooks-a (fn &rest args)
"Exporting and tangling trigger save hooks; inadvertantly triggering
mutating hooks on exported output, like formatters."
:around '(org-export-to-file org-babel-tangle)
(let (before-save-hook after-save-hook)
(apply fn args)))
(defadvice! +org--fix-async-export-a (fn &rest args)
:around '(org-export-to-file org-export-as)
(let ((old-async-init-file org-export-async-init-file)
(org-export-async-init-file (make-temp-file "doom-org-async-export")))
(doom-file-write
org-export-async-init-file
`((setq org-export-async-debug ,(or org-export-async-debug debug-on-error)
load-path ',load-path)
(unwind-protect
(let ((init-file ,old-async-init-file))
(if init-file
(load init-file nil t)
(load ,early-init-file nil t)
(require 'doom-start)))
(delete-file load-file-name))))
(apply fn args))))
(defun +org-init-habit-h ()
(add-hook! 'org-agenda-mode-hook
(defun +org-habit-resize-graph-h ()
"Right align and resize the consistency graphs based on
`+org-habit-graph-window-ratio'"
(when (featurep 'org-habit)
(let* ((total-days (float (+ org-habit-preceding-days org-habit-following-days)))
(preceding-days-ratio (/ org-habit-preceding-days total-days))
(graph-width (floor (* (window-width) +org-habit-graph-window-ratio)))
(preceding-days (floor (* graph-width preceding-days-ratio)))
(following-days (- graph-width preceding-days))
(graph-column (- (window-width) (+ preceding-days following-days)))
(graph-column-adjusted (if (> graph-column +org-habit-min-width)
(- graph-column +org-habit-graph-padding)
nil)))
(setq-local org-habit-preceding-days preceding-days)
(setq-local org-habit-following-days following-days)
(setq-local org-habit-graph-column graph-column-adjusted))))))
(defun +org-init-hacks-h ()
"Getting org to behave."
;; Open file links in current window, rather than new ones
(setf (alist-get 'file org-link-frame-setup) #'find-file)
;; Open directory links in dired
(add-to-list 'org-file-apps '(directory . emacs))
(add-to-list 'org-file-apps '(remote . emacs))
;; Open help:* links with helpful-* instead of describe-*
(advice-add #'org-link--open-help :around #'doom-use-helpful-a)
;; Unlike the stock showNlevels options, these will also show the parents of
;; the target level, recursively.
(pushnew! org-startup-options
'("show2levels*" org-startup-folded show2levels*)
'("show3levels*" org-startup-folded show3levels*)
'("show4levels*" org-startup-folded show4levels*)
'("show5levels*" org-startup-folded show5levels*))
;; TODO Upstream this.
(defadvice! +org--recursive-org-persist-mkdir-a (fn &rest args)
"`org-persist-write:index' does not recursively create
`org-persist-directory', which causes an error if it's a parent doesn't exist."
:before #'org-persist-write:index
(make-directory org-persist-directory t))
(defadvice! +org--more-startup-folded-options-a ()
"Adds support for 'showNlevels*' startup options.
Unlike showNlevels, this will also unfold parent trees."
:before-until #'org-cycle-set-startup-visibility
(when-let (n (pcase org-startup-folded
(`show2levels* 2)
(`show3levels* 3)
(`show4levels* 4)
(`show5levels* 5)))
(org-fold-show-all '(headings))
(save-excursion
(goto-char (point-max))
(save-restriction
(narrow-to-region (point-min) (or (re-search-forward org-outline-regexp-bol nil t) (point-max)))
(org-fold-hide-drawer-all))
(goto-char (point-max))
(let ((regexp (if (and (wholenump n) (> n 0))
(format "^\\*\\{%d,%d\\} " (1- n) n)
"^\\*+ "))
(last (point)))
(while (re-search-backward regexp nil t)
(when (or (not (wholenump n))
(= (org-current-level) n))
(org-fold-core-region (line-end-position) last t 'outline))
(setq last (line-end-position 0)))))
t))
;; Some uses of `org-fix-tags-on-the-fly' occur without a check on
;; `org-auto-align-tags', such as in `org-self-insert-command' and
;; `org-delete-backward-char'.
;; TODO Should be reported/PR'ed upstream
(defadvice! +org--respect-org-auto-align-tags-a (&rest _)
:before-while #'org-fix-tags-on-the-fly
org-auto-align-tags)
(defadvice! +org--recenter-after-follow-link-a (&rest _args)
"Recenter after following a link, but only internal or file links."
:after '(org-footnote-action
org-follow-timestamp-link
org-link-open-as-file
org-link-search)
(when (get-buffer-window)
(recenter)))
(defadvice! +org--strip-properties-from-outline-a (fn &rest args)
"Fix variable height faces in eldoc breadcrumbs."
:around #'org-format-outline-path
(let ((org-level-faces
(cl-loop for face in org-level-faces
collect `(:foreground ,(face-foreground face nil t)
:weight bold))))
(apply fn args)))
(defun +org--restart-mode-h ()
"Restart `org-mode', but only once."
(quiet! (org-mode-restart))
(delq! (current-buffer) org-agenda-new-buffers)
(remove-hook 'doom-switch-buffer-hook #'+org--restart-mode-h
'local)
(run-hooks 'find-file-hook))
(add-hook! 'org-agenda-finalize-hook
(defun +org-exclude-agenda-buffers-from-workspace-h ()
"Don't associate temporary agenda buffers with current workspace."
(when (and org-agenda-new-buffers
(bound-and-true-p persp-mode)
(not org-agenda-sticky))
(let (persp-autokill-buffer-on-remove)
(persp-remove-buffer org-agenda-new-buffers
(get-current-persp)
nil))))
(defun +org-defer-mode-in-agenda-buffers-h ()
"`org-agenda' opens temporary, incomplete org-mode buffers.
I've disabled a lot of org-mode's startup processes for these invisible buffers
to speed them up (in `+org--exclude-agenda-buffers-from-recentf-a'). However, if
the user tries to visit one of these buffers they'll see a gimped, half-broken
org buffer. To avoid that, restart `org-mode' when they're switched to so they
can grow up to be fully-fledged org-mode buffers."
(dolist (buffer org-agenda-new-buffers)
(when (buffer-live-p buffer) ; Ensure buffer is not killed
(with-current-buffer buffer
(add-hook 'doom-switch-buffer-hook #'+org--restart-mode-h
nil 'local))))))
(defadvice! +org--restart-mode-before-indirect-buffer-a (base-buffer &rest _)
"Restart `org-mode' in buffers in which the mode has been deferred (see
`+org-defer-mode-in-agenda-buffers-h') before they become the base buffer for an
indirect buffer. This ensures that the buffer is fully functional not only when
the *user* visits it, but also when some code interacts with it via an indirect
buffer as done, e.g., by `org-capture'."
:before #'make-indirect-buffer
(with-current-buffer base-buffer
(when (memq #'+org--restart-mode-h doom-switch-buffer-hook)
(+org--restart-mode-h))))
(defvar recentf-exclude)
(defadvice! +org--optimize-backgrounded-agenda-buffers-a (fn file)
"Prevent temporarily opened agenda buffers from polluting recentf."
:around #'org-get-agenda-file-buffer
(let ((recentf-exclude (list (lambda (_file) t)))
(doom-inhibit-large-file-detection t)
org-startup-indented
org-startup-folded
vc-handled-backends
org-mode-hook
find-file-hook)
(funcall fn file)))
;; HACK With https://code.orgmode.org/bzg/org-mode/commit/48da60f4, inline
;; image previews broke for users with imagemagick support built in. This
;; reverses the problem, but should be removed once it is addressed
;; upstream (if ever).
(defadvice! +org--fix-inline-images-for-imagemagick-users-a (fn &rest args)
:around #'org-display-inline-images
(letf! (defun create-image (file-or-data &optional type data-p &rest props)
(let ((type (if (plist-get props :width) type)))
(apply create-image file-or-data type data-p props)))
(apply fn args)))
(defadvice! +org--fix-inconsistent-uuidgen-case-a (uuid)
"Ensure uuidgen always produces lowercase output regardless of system."
:filter-return #'org-id-new
(if (eq org-id-method 'uuid)
(downcase uuid)
uuid)))
(defun +org-init-keybinds-h ()
"Sets up org-mode and evil keybindings. Tries to fix the idiosyncrasies
between the two."
(add-hook 'doom-escape-hook #'+org-remove-occur-highlights-h)
;; C-a & C-e act like `doom/backward-to-bol-or-indent' and
;; `doom/forward-to-last-non-comment-or-eol', but with more org awareness.
(setq org-special-ctrl-a/e t)
(setq org-M-RET-may-split-line nil
;; insert new headings after current subtree rather than inside it
org-insert-heading-respect-content t)
(add-hook! 'org-tab-first-hook
#'+org-yas-expand-maybe-h
#'+org-indent-maybe-h)
(add-hook 'doom-delete-backward-functions
#'+org-delete-backward-char-and-realign-table-maybe-h)
(map! :map org-mode-map
;; Recently, a [tab] keybind in `outline-mode-cycle-map' has begun
;; overriding org's [tab] keybind in GUI Emacs. This is needed to undo
;; that, and should probably be PRed to org.
[tab] #'org-cycle
"C-c C-S-l" #'+org/remove-link
"C-c C-i" #'org-toggle-inline-images
;; textmate-esque newline insertion
"S-RET" #'+org/shift-return
"C-RET" #'+org/insert-item-below
"C-S-RET" #'+org/insert-item-above
"C-M-RET" #'org-insert-subheading
[C-return] #'+org/insert-item-below
[C-S-return] #'+org/insert-item-above
[C-M-return] #'org-insert-subheading
(:when (featurep :system 'macos)
[s-return] #'+org/insert-item-below
[s-S-return] #'+org/insert-item-above
[s-M-return] #'org-insert-subheading)
;; Org-aware C-a/C-e
[remap doom/backward-to-bol-or-indent] #'org-beginning-of-line
[remap doom/forward-to-last-non-comment-or-eol] #'org-end-of-line
:localleader
"#" #'org-update-statistics-cookies
"'" #'org-edit-special
"*" #'org-ctrl-c-star
"+" #'org-ctrl-c-minus
"," #'org-switchb
"." #'org-goto
"@" #'org-cite-insert
(:when (modulep! :completion ivy)
"." #'counsel-org-goto
"/" #'counsel-org-goto-all)
(:when (modulep! :completion helm)
"." #'helm-org-in-buffer-headings
"/" #'helm-org-agenda-files-headings)
(:when (modulep! :completion vertico)
"." #'consult-org-heading
"/" #'consult-org-agenda)
"A" #'org-archive-subtree-default
"e" #'org-export-dispatch
"f" #'org-footnote-action
"h" #'org-toggle-heading
"i" #'org-toggle-item
"I" #'org-id-get-create
"k" #'org-babel-remove-result
"K" #'+org/remove-result-blocks
"n" #'org-store-link
"o" #'org-set-property
"q" #'org-set-tags-command
"t" #'org-todo
"T" #'org-todo-list
"x" #'org-toggle-checkbox
(:prefix ("a" . "attachments")
"a" #'org-attach
"d" #'org-attach-delete-one
"D" #'org-attach-delete-all
"f" #'+org/find-file-in-attachments
"l" #'+org/attach-file-and-insert-link
"n" #'org-attach-new
"o" #'org-attach-open
"O" #'org-attach-open-in-emacs
"r" #'org-attach-reveal
"R" #'org-attach-reveal-in-emacs
"u" #'org-attach-url
"s" #'org-attach-set-directory
"S" #'org-attach-sync
(:when (modulep! +dragndrop)
"c" #'org-download-screenshot
"p" #'org-download-clipboard
"P" #'org-download-yank))
(:prefix ("b" . "tables")
"-" #'org-table-insert-hline
"a" #'org-table-align
"b" #'org-table-blank-field
"c" #'org-table-create-or-convert-from-region
"e" #'org-table-edit-field
"f" #'org-table-edit-formulas
"h" #'org-table-field-info
"s" #'org-table-sort-lines
"r" #'org-table-recalculate
"R" #'org-table-recalculate-buffer-tables
(:prefix ("d" . "delete")
"c" #'org-table-delete-column
"r" #'org-table-kill-row)
(:prefix ("i" . "insert")
"c" #'org-table-insert-column