summaryrefslogtreecommitdiff
path: root/emacs-config.org
blob: be4cf182a51081184401e5dc653326e02df44c78 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
#+title: Emacs Configuration
#+property: header-args:elisp :mkdirp yes :results silent :padline no
#+property: header-args:elisp+ :tangle "~/.config/emacs/init.el"

* General
** Settings
Quality of life settings.
#+begin_src elisp
  (setq-default
   help-window-select t
   delete-by-moving-to-trash t
   sentence-end-double-space nil
   confirm-kill-emacs 'yes-or-no-p
   scroll-conservatively 101
   cursor-in-non-selected-windows nil)
#+end_src

** Startup Screen
Disable default startup screen.
#+begin_src elisp
  (setq-default inhibit-startup-screen t)
#+end_src

** Interface
Disable GUI bars.
#+begin_src elisp
  (tool-bar-mode -1)
  ;; (menu-bar-mode -1)
  ;; (scroll-bar-mode -1)
#+end_src

Enable right-click context menu.
#+begin_src elisp
  (context-menu-mode)
#+end_src

Enable vertical minibuffer.
#+begin_src elisp
  (fido-vertical-mode)
#+end_src

Enable smooth scrolling.
#+begin_src elisp
  (pixel-scroll-precision-mode)
#+end_src

** Behaviour
:PROPERTIES:
:ID:       8818a835-08ef-47f8-bcbf-83dd210725a0
:END:
Automatically move cursor point on window split.
#+begin_src elisp
  (advice-add 'split-window-below
              :after (lambda (&rest r) (other-window 1)))
  (advice-add 'split-window-right
              :after (lambda (&rest r) (other-window 1)))
#+end_src

Add advice to copy line at cursor point or region.
#+begin_src elisp
  (defadvice kill-ring-save (before dwim-copy activate compile)
    "Copy the current line at cursor point or region."
    (interactive
     (if mark-active
         (list (region-beginning) (region-end))
       (message "Line copied")
       (list (line-beginning-position)
             (line-beginning-position 2)))))
#+end_src

Add advice to cut line or region at cursor point.
#+begin_src elisp
  (defadvice kill-region (before dwim-cut activate compile)
    "Cut the current line at point or region."
    (interactive
     (if mark-active (list (region-beginning) (region-end))
       (list (line-beginning-position)
             (line-beginning-position 2)))))
#+end_src

Add advice to enable ~query-replace~ to search the whole buffer or region if selected, as the original default behaviour would only begin to replace from the cursor point position.
#+begin_src elisp
  (defun +advice-goto-top-no-region (func &rest args)
    "Move point to top of buffer when no region applying ARGS to FUNC."
    (save-excursion
      (when (not (use-region-p))
        (goto-char (point-min)))
      (apply func args)))

  (advice-add 'query-replace :around #'+advice-goto-top-no-region)
  (advice-add 'query-replace-regexp :around #'+advice-goto-top-no-region)
#+end_src

** Editor
Tab behaviour settings.
#+begin_src elisp
  (setq-default
   tab-width 4
   tab-always-indent 'complete
   backward-delete-char-untabify-method 'hungry
   indent-tabs-mode nil)
#+end_src

Replace selected region when yanking text.
#+begin_src elisp
  (delete-selection-mode)
#+end_src

Auto update buffer on any file changes.
#+begin_src elisp
  (global-auto-revert-mode)
#+end_src

Automatically remove trailing whitespace on file-save.
#+begin_src elisp
  (add-hook 'before-save-hook #'whitespace-cleanup)
#+end_src

Increase default column width.
#+begin_src elisp
  (setq-default fill-column 80)
#+end_src

Set whitespace column length and symbols.
#+begin_src elisp
  (with-eval-after-load 'whitespace
    (setq-default
     whitespace-line-column fill-column
     whitespace-display-mappings '((space-mark ?\s [?·] [?.])
                                   (newline-mark ?\n [?↴ ?\n])
                                   (tab-mark ?\t [?➔ ?\t] [?\\ ?\t]))))
#+end_src

Enable pairing brackets and add additional bracket pairs.
#+begin_src elisp
  (electric-pair-mode)
  (with-eval-after-load "elec-pair"
    (cl-pushnew '(?\{ . ?\}) electric-pair-pairs))
#+end_src

** Theme
Enable theme.
#+begin_src elisp
  (load-theme 'modus-vivendi)
#+end_src

** Tab-bar
Open scratch window on new tab.
#+begin_src elisp
  (with-eval-after-load 'tab-bar
    (setq-default tab-bar-new-tab-choice #'get-scratch-buffer-create))
#+end_src

** Modeline
Enable column number in modeline.
#+begin_src elisp
  (column-number-mode)
#+end_src

** Completion
Enable completions previews.
#+begin_src elisp
  (add-hook 'prog-mode-hook
            #'completion-preview-mode)
#+end_src

Set completion keybindings.
#+begin_src elisp
  (with-eval-after-load 'completion-preview
    (let ((map completion-preview-active-mode-map))
      (keymap-set map "M-n" #'completion-preview-next-candidate)
      (keymap-set map "M-p" #'completion-preview-prev-candidate)))
#+end_src

** Which-key
Enable which-key that displays a pop-up window of posisble keybindings sequences.
#+begin_src elisp
  (which-key-mode)
#+end_src

** Winner-mode
Enable winner-mode.
#+begin_src elisp
  (winner-mode)
#+end_src

Set winner mode keybindings.
#+begin_src elisp
  (with-eval-after-load 'winner
    (keymap-set winner-mode-map "C-<" #'winner-undo)
    (keymap-set winner-mode-map "C->" #'winner-redo))
#+end_src

** History
*** Minibuffer History
Enable saving minibuffer history.
#+begin_src elisp :noweb yes
  (savehist-mode)
  (with-eval-after-load 'savehist
    <<savehist-settings>>)
#+end_src

Automatically save history on exit.
#+begin_example elisp :tangle no :noweb-ref savehist-settings
  (add-hook 'kill-emacs-hook #'savehist-save)
#+end_example

Ensure savehist file is loaded.
#+begin_src elisp :tangle no :noweb-ref savehist-settings
  (unless savehist-loaded
    (load-file savehist-file))
#+end_src

Add addtional variable lists to save.
#+begin_src elisp :tangle no :noweb-ref savehist-settings
  (dolist (var '(command-history
                 register-alist
                 mark-ring
                 kill-ring
                 search-ring
                 regexp-search-ring))
    (cl-pushnew var savehist-additional-variables))
#+end_src

*** Recent Files
Enable saving history of recent opened files.
#+begin_src elisp :noweb yes
  (with-eval-after-load 'recentf
    <<recentf-settings>>)
  (recentf-mode)
#+end_src

Recentf settings.
#+begin_src elisp :tangle no :noweb-ref recentf-settings
  (setq-default
   recentf-max-saved-items 50
   recentf-auto-cleanup 'never)
#+end_src

Save recent files and cleanup file list on exit.
#+begin_src elisp :tangle no :noweb-ref recentf-settings
  (add-hook 'kill-emacs-hook #'recentf-cleanup)
  (add-hook 'kill-emacs-hook #'recentf-save-list)
#+end_src

Disallow duplicates in history.
#+begin_src elisp :tangle no :noweb-ref recentf-settings
  (setq-default history-delete-duplicates t)
#+end_src

*** Cursor Point Position
Enable saving cursor point positions.
#+begin_src elisp :noweb yes
  (save-place-mode)
  (with-eval-after-load 'saveplace
    <<saveplace-settings>>)
#+end_src

Save place settings.
#+begin_src elisp :tangle no :noweb-ref saveplace-settings
  (setq-default
   save-place-abbreviate-file-names t
   save-place-limit 800)
#+end_src

*** Bookmarks
Don't display bookmark icon in fringe.
#+begin_src elisp
  (with-eval-after-load 'bookmark
    (setq-default bookmark-fringe-mark nil))
#+end_src

Disable auto-bookmarking org-mode files.
#+begin_src elisp
  (with-eval-after-load 'org
    (setq-default org-bookmark-names-plist nil))
#+end_src

** Spell Checking
Set dictionary.
#+begin_src elisp
  (setq-default ispell-dictionary "british")
#+end_src

Enable spell checking.
#+begin_src elisp
  (add-hook 'text-mode-hook #'flyspell-mode)
  (add-hook 'prog-mode-hook #'flyspell-prog-mode)
#+end_src

Add add word to dictionary function.
#+begin_src elisp
  (defun +flyspell-add-word-to-dictionary ()
    "Save word at point to personal dictionary."
    (interactive)
    (when-let ((loc (point))
               (word (flyspell-get-word)))
      (when (yes-or-no-p
             (format "Add '%s' to dictionary?" (car word)))
        (flyspell-do-correct
         'save nil (car word) loc (cadr word) (caddr word) loc)
        (save-buffer)
        (revert-buffer-quick)
        (message "Added '%s' to personal dictionary" (car word)))))
#+end_src

Set custom spell checking keybindings.
#+begin_src elisp
  (setq-default flyspell-mode-map (make-sparse-keymap))
  (with-eval-after-load 'flyspell
    (keymap-set flyspell-mode-map "C-x c c" #'flyspell-auto-correct-word)
    (keymap-set flyspell-mode-map "C-x c u" #'flyspell-auto-correct-previous-word)
    (keymap-set flyspell-mode-map "C-x c l" #'flyspell-check-previous-highlighted-word)
    (keymap-set flyspell-mode-map "C-x c n" #'flyspell-goto-next-error)
    (keymap-set flyspell-mode-map "C-x c b" #'flyspell-buffer)
    (keymap-set flyspell-mode-map "C-x c d" #'ispell-change-dictionary)
    (keymap-set flyspell-mode-map "C-x c i" #'+flyspell-add-word-to-dictionary))
#+end_src

** Dired
#+begin_src elisp :noweb yes
  (with-eval-after-load 'dired
    <<dired-settings>>)
#+end_src

Dired settings.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (setq-default
   dired-guess-shell-alist-user '(("\\.pdf$" "zathura"))
   dired-listing-switched "-AFhlv --group-directories-first")
#+end_src

Hide dot-files when using ~dired-omit-mode~.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (setq-default dired-omit-files "\\`[.].*\\'")
#+end_src

Auto enable omit mode on Dired startup.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (add-hook 'dired-mode-hook (lambda ()
                               (dired-omit-mode)
                               (toggle-truncate-lines 1)))
#+end_src

Add shred function.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (if (executable-find "shred")
      (defun +dired-shred-file ()
        "Shred marked files or a file at point in Dired."
        (interactive)
        (mapc (lambda (file) "Run the shred shell command on FILE."
                (if (file-regular-p file)
                    (when (yes-or-no-p (format "Shred %s?" file))
                      (shell-command (format "shred -u \"%s\"" file)))
                  (error "Aborting shred procedure; \"%s\" is not a file!" file)))
              (dired-get-marked-files)))
    (error "Shred command not found!"))
#+end_src

Add additional dired keybindings.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (keymap-set dired-mode-map "b" #'dired-up-directory)
  (keymap-set dired-mode-map "z" #'+dired-shred-file)
#+end_src

Allow changing file permissions when in a writable dired buffer.
#+begin_src elisp :tangle no :noweb-ref dired-settings
  (with-eval-after-load 'wdired
    (setq-default wdired-allow-to-change-permissions t))
#+end_src

** Search & Replace
Case-sensitive search by default and show match count in minibuffer.
#+begin_src elisp
  (with-eval-after-load 'isearch
    (setq-default
     case-fold-search nil
     isearch-lazy-count t))
#+end_src

** Org
#+begin_src elisp :noweb yes
  (with-eval-after-load 'org
    <<org-settings>>
    <<org-todo-tag-settings>>
    <<org-babel-settings>>
    <<org-export-settings>>)
#+end_src

#+begin_src elisp :noweb yes
  (with-eval-after-load 'org-keys
    <<org-speed-command-settings>>)
#+end_src

#+begin_src elisp :noweb yes
  (with-eval-after-load 'org-agenda
    <<org-agenda-settings>>)
#+end_src

*** Org Settings
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-settings
:END:

Org directory location.
#+begin_src elisp
  (require 'xdg)
  (setq-default org-directory
                (expand-file-name "org" (xdg-user-dir "DOCUMENTS")))
#+end_src

Hide emphasis markers and set ellipse symbol.
#+begin_src elisp
  (setq-default org-ellipsis "⤵"
                org-hide-emphasis-markers t)
#+end_src

Enable checkbox dependency checking.
#+begin_src elisp
  (setq-default org-enforce-todo-dependencies t
                org-enforce-todo-checkbox-dependencies t
                org-checkbox-hierarchical-statistics nil)
#+end_src

Set org column format.
#+begin_src elisp
  (setq-default org-columns-default-format
                "%70ITEM %TODO %10CLOCKSUM %16DEADLINE")
#+end_src

Enable notes for various actions.
#+begin_src elisp
  (setq-default org-log-done 'time
                org-log-reschedule 'note
                org-log-redeadline 'note
                org-log-into-drawer t
                org-log-note-clock-out t)
#+end_src

Prioritise attribute tags for image width.
#+begin_src elisp
  (setq-default org-image-actual-width nil)
#+end_src

Add advice to prompt for confirmation before commenting or cutting a subtree.
#+begin_src elisp
  (advice-add 'org-cut-subtree
              :before-while (lambda (&rest _)
                              "Prompts for confirmation before cutting subtree."
                              (let ((heading (org-entry-get nil "ITEM")))
                                (y-or-n-p (format "Cut \"%s\" subtree?" heading)))))

  (advice-add 'org-toggle-comment
              :before-while (lambda (&rest _)
                              "Prompt for confirmation before commenting a subtree."
                              (let ((heading (org-entry-get nil "ITEM")))
                                (y-or-n-p (format "Toggle comment on \"%s\" subtree?" heading)))))
#+end_src

Use custom IDs for links.
#+begin_src elisp
  (with-eval-after-load 'org-id
    (setq-default org-id-link-to-org-use-id
                  'create-if-interactive-and-no-custom-id))
#+end_src

*** Org To-dos & Tags
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-todo-tag-settings
:END:
Set to-do key words and selection type.
#+begin_src elisp
  (setq-default
   org-use-fast-todo-selection 'expert
   org-todo-keywords '((sequence "IDLE(i)" "TODO(t)" "LIVE(l)" "WAIT(w@/!)"
                                 "|" "AXED(a@)" "DONE(d)")))
#+end_src

Style keywords.
#+begin_src elisp
  (set-face-attribute 'org-todo nil :weight 'bold :slant 'italic)
  (setq-default
   org-todo-keyword-faces '(("IDLE"  . "grey")
                            ("TODO"  . "RoyalBlue1")
                            ("LIVE"  . "goldenrod2")
                            ("WAIT"  . "linen")
                            ("DONE"  . "SeaGreen3")
                            ("AXED"  . "OrangeRed2")))
#+end_src

Add no-export tag.
#+begin_src elisp
  (with-eval-after-load 'ox
    (cl-pushnew "NOEXPORT" org-export-exclude-tags))
#+end_src

Set tag list.
#+begin_src elisp
  (setq-default
   org-tag-alist '(;; Built-in Actions
                   ("ARCHIVE") ("NOEXPORT") ("ignore")

                   ;; General definitions
                   ("blog") ("crypt") ("emacs") ("mail") ("note") ("web")

                   ;; Code
                   (:startgroup)
                   ("code")
                   (:grouptags)
                   ("bash") ("c") ("cpp") ("css") ("elisp") ("html")
                   (:endgroup)

                   ;; Text
                   (:startgroup)
                   ("text")
                   (:grouptags)
                   ("article") ("book") ("man") ("pdf") ("txt") ("doc")
                   (:endgroup)

                   ;; Audio
                   (:startgroup)
                   ("audio")
                   (:grouptags)
                   ("audbook") ("music") ("podcast")
                   (:endgroup)

                   ;; Video
                   (:startgroup)
                   ("video")
                   (:grouptags)
                   ("movie") ("yt")
                   (:endgroup)))
#+end_src

*** Org Speed Command Keys
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-speed-command-settings
:END:

Enable org speed commands.
#+begin_src elisp
  (setq-default org-use-speed-commands t)
#+end_src

Alternative comment key =;= added to be consistent with ~org-toggle-comment~ (=C-c ;=).
#+begin_src elisp
  (dolist (key '(("P" . org-set-property)
                 ("$" . org-archive-subtree-default-with-confirmation)
                 ("q" . org-set-tags-command)
                 ("]" . org-shiftright)
                 ("[" . org-shiftleft)
                 (";" . org-toggle-comment)
                 ("z" . org-add-note)
                 ("y" . org-copy-subtree)))
    (cl-pushnew key org-speed-commands))
#+end_src

Add ='= shortcut key to edit source blocks, which is consistent with ~org-edit-special~ (=C-c '=).
#+begin_src elisp
  (cl-pushnew '("'" . org-edit-src-code) org-babel-key-bindings)
#+end_src

*** Org Babel
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-babel-settings
:END:

Add new structure template shortcuts when inserting a block.
#+begin_src elisp
  (cl-pushnew '("se" . "src elisp") org-structure-template-alist)
#+end_src

Add advice to automatically open an edit buffer when inserting a structure template.
#+begin_src elisp
  (defadvice org-insert-structure-template
      (after edit-src activate compile)
    "Auto enter an edit buffer when inserted an Org block."
    (call-interactively 'org-edit-special))
#+end_src

Enable supported org babel languages.
#+begin_src elisp
  (dolist (lang '(shell C latex plantuml))
    (cl-pushnew `(,lang . t) org-babel-load-languages))
#+end_src

Redisplay images when executing an org block.
#+begin_src elisp
  (with-eval-after-load 'ob-core
    (add-hook 'org-babel-after-execute-hook
              (lambda () "Redisplay Org inline images."
                (when org-inline-image-overlays
                  (org-redisplay-inline-images)))))
#+end_src

*** Org Export
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-export-settings
:END:

Use SVG LaTeX previews.
#+begin_src elisp
  (setq-default org-preview-latex-default-process 'dvisvgm)
  (with-eval-after-load 'ox-html
    (setq-default org-html-with-latex 'dvisvgm))
#+end_src


Set [[https://ctan.org/pkg/hyperref][hyperref]] LaTeX settings.
#+begin_src elisp
  (with-eval-after-load 'ox-latex
    (setq-default org-latex-hyperref-template
   "\\hypersetup{
     pdfauthor={%a},
     pdftitle={%t},
     pdfkeywords={%k},
     pdfsubject={%d},
     pdfcreator={%c},
     pdflang={%L},
     linktoc=all,
     colorlinks=true,
     urlcolor=blue,
     citecolor=blue,
     linkcolor=blue
    }
    ")
    )
#+end_src

Add =doc= LaTeX class.
#+begin_src elisp
  (with-eval-after-load 'ox-latex
    (cl-pushnew
     '("doc"
       "
  % Document Type
  \\documentclass[11pt]{article}

  % Geometry
  \\usepackage[a4paper,margin=1in]{geometry}

  % Paraskip
  \\usepackage{parskip}
  \\setlength{\\parindent}{0em}
  \\setlength{\\parskip}{1em}

  % Line height
  \\renewcommand{\\baselinestretch}{1.2}

  % Fancyhdr (header/footer)
  \\usepackage{fancyhdr}
  \\pagestyle{fancy}
  \\fancyhead[LH,LH]{\\scriptsize\\leftmark}
  \\fancyhead[RH,RH]{\\scriptsize\\rightmark}
  \\fancyfoot[CF]{\\thepage}
  \\fancyfootoffset{\\pagewidth}
  \\renewcommand{\\headrulewidth}{0.5pt} % top line thickness
  \\renewcommand{\\footrulewidth}{0.5pt} % bottom line thickness
  \\renewcommand{\\headruleskip}{10pt}   % top line padding
  \\renewcommand{\\footruleskip}{10pt}    % bottom line padding
  "
       ("\\section{%s}" . "\\section*{%s}")
       ("\\subsection{%s}" . "\\subsection*{%s}")
       ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
       ("\\paragraph{%s}" . "\\paragraph*{%s}")
       ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
     org-latex-classes)
    )
#+end_src

*** Org Habit
#+begin_src elisp
  (with-eval-after-load "org-habit"
    (setq-default
     org-habit-today-glyph ?T
     org-habit-completed-glyph ?+))
#+end_src

*** Org Agenda
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-agenda-settings
:END:

Match encrypted org files for agenda.
#+begin_src elisp
  (setq-default org-agenda-file-regexp "^[^.#].+\\.org\\(?:\\.gpg\\)?$")
#+end_src

Set agenda directory location and add files to agenda.
#+begin_src elisp
  (let ((dir (expand-file-name "agenda/" org-directory)))
    (if (file-exists-p dir)
        (setq-default org-agenda-files (directory-files-recursively
                                        dir org-agenda-file-regexp))
      (warn "Org-agenda directory '%s' not found" dir)))
#+end_src

Set agenda header formats.
#+begin_src elisp
  (setq-default
   org-agenda-prefix-format '((agenda . "%?2i%-5c%?t%s\s")
                              (todo . "%?2i%-5c")
                              (tags . "⬜ %?2i%-5c")
                              (search . "%?2i%-5c"))
   org-agenda-deadline-leaders '("!" "In %2d d.:" "[!%2dx]")
   org-agenda-scheduled-leaders '("+" "[+%2dx]")
   org-agenda-format-date "%F %A")
#+end_src

Set bulk actions and marker.
#+begin_src elisp
  (setq-default
   org-agenda-bulk-custom-functions '((?k org-agenda-kill))
   org-agenda-bulk-mark-char "☑️")
#+end_src

Set priority faces.
#+begin_src elisp
  (setq-default org-priority-faces
                '((?A . (:foreground "red" :slant italic))
                  (?B . (:foreground "cadetblue" :slant italic))
                  (?C . (:foreground "goldenrod" :slant italic))))
#+end_src

Set face for to-dos in agenda view.
#+begin_src elisp
  (set-face-attribute
   'org-agenda-dimmed-todo-face nil
   :weight 'bold
   :inherit nil
   :background 'unspecified)
#+end_src

Set sorting strategy.
#+begin_src elisp
  (setq-default
   org-agenda-sorting-strategy
   '((agenda
      deadline-up
      time-up
      habit-down
      priority-up
      category-up)
     (todo
      todo-state-down
      deadline-up
      scheduled-up
      priority-down
      category-keep)
     (tags
      todo-state-down
      priority-down
      category-keep)
     (search
      category-keep)))
#+end_src

Set time grid.
#+begin_src elisp
  (setq-default
   org-agenda-time-grid
   '((daily today require-timed)
     (0000 0100 0200 0300 0400 0500 0600 0700 0800 0900 1000 1100
      1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300)
     "⏰" "——————————————————————————————"))
#+end_src

**** Org Agenda Commands
Command to view to personal planner.
#+begin_src elisp
  (cl-pushnew '("=" "Day Planner: Personal"
                ((agenda "" ((org-agenda-overriding-header "PERSONAL AGENDA:")
                             (org-agenda-span 'day)
                             (org-agenda-include-deadlines nil)
                             (org-agenda-show-all-dates nil)))

                 (agenda "" ((org-agenda-overriding-header "\nPERSONAL DEADLINES:")
                             (org-agenda-span 'day)
                             (org-agenda-format-date "%F %A")
                             (org-agenda-entry-types '(:deadline))
                             (org-agenda-skip-deadline-if-done t)))

                 (tags-todo "-goal/TODO" ((org-agenda-overriding-header "\nPERSONAL TASKS:"))))

                ((org-habit-show-habits nil)
                 (org-agenda-include-diary nil)
                 (org-agenda-use-time-grid nil)
                 (org-agenda-block-separator nil)
                 (org-agenda-category-filter-preset '("-Work"))))
              org-agenda-custom-commands)
#+end_src

Command to view work planner.
#+begin_src elisp
  (cl-pushnew '("-" "Day Planner: Work"
                ((agenda "" ((org-agenda-overriding-header "WORK AGENDA:")
                             (org-agenda-start-day "monday")
                             (org-agenda-span 5)
                             (org-agenda-include-deadlines nil)
                             (org-agenda-show-all-dates nil)
                             (org-habit-show-habits nil)))

                 (agenda "" ((org-agenda-overriding-header "\nWORK DEADLINES:")
                             (org-agenda-span 'day)
                             (org-agenda-time-grid nil)
                             (org-agenda-format-date "%F %A")
                             (org-agenda-entry-types '(:deadline))
                             (org-agenda-skip-deadline-if-done t)))

                 (alltodo "" ((org-agenda-overriding-header "\nWORK TASKS:"))))

                ((org-agenda-block-separator nil)
                 (org-agenda-category-filter-preset '("+Work"))))
              org-agenda-custom-commands)
#+end_src

Command to view all personal tasks.
#+begin_src elisp
  (cl-pushnew '("l" "All Personal Tasks"
                tags-todo "-goal-milestone"
                ((org-agenda-overriding-header "PERSONAL TASKS:")
                 (org-agenda-category-filter-preset '("-Work"))))
              org-agenda-custom-commands)
#+end_src

Command to view all work tasks.
#+begin_src elisp
  (cl-pushnew '("k" "All Work Related Tasks"
                todo ""
                ((org-agenda-overriding-header "WORK TASKS:")
                 (org-agenda-category-filter-preset '("+Work"))))
              org-agenda-custom-commands)
#+end_src

Command to organise idle tasks.
#+begin_src elisp
  (cl-pushnew '("o" "Organise Tasks"
                todo "IDLE"
                ((org-agenda-overriding-header "TASKS TO ORGANISE:")))
              org-agenda-custom-commands)
#+end_src

Command to view tasks that require archiving.
#+begin_src elisp
  (cl-pushnew '("$" "Tasks to Archive"
                tags "CLOSED<=\"<-3m>\"+CATEGORY={Task}/DONE|AXED"
                ((org-agenda-show-inherited-tags nil)
                 (org-agenda-todo-list-sublevels nil)
                 (org-agenda-search-view-max-outline-level 1)
                 (org-agenda-overriding-header "PERSONAL TASKS TO ARCHIVE")))
              org-agenda-custom-commands)
#+end_src

Command to view habits.
#+begin_src elisp
  (cl-pushnew '("h" "Habits"
                tags-todo "STYLE=\"habit\""
                ((org-agenda-overriding-header "HABITS:")))
              org-agenda-custom-commands)
#+end_src

Command to view projects.
#+begin_src elisp
  (cl-pushnew
   '("p" "Projects"
     ((tags-todo "+proj|+goal|+milestone"
                 ((org-agenda-overriding-header "PROJECTS:")))
      (tags-todo "-proj-goal-milestone"
                 ((org-agenda-overriding-header "\nPROJECT TASKS:"))))
     ((org-agenda-prefix-format "⬜ %?2i%-5c%?t%?s%(+org-agenda-breadcrumb)")
      (org-agenda-compact-blocks t)
      (org-agenda-category-filter-preset +project-categories)
      (org-agenda-sorting-strategy
       '(category-up deadline-up time-up priority-down effort-up))))
   org-agenda-custom-commands)
#+end_src

Command to view clock report.
#+begin_src elisp
  (cl-pushnew '("c" "Clock Review"
                agenda ""
                ((org-agenda-span 'day)
                 (org-agenda-prefix-format " %-5c | %t | %s | ")
                 (org-agenda-archives-mode t)
                 (org-agenda-use-time-grid nil)
                 (org-agenda-start-with-log-mode 'clockcheck)
                 (org-agenda-start-with-clockreport-mode t)
                 (org-agenda-clock-consistency-checks
                  '(:min-duration 0
                    :max-gap 5
                    :inherit 'warning
                    :default-face ((:background 'unspecified)
                                   (:family "monospace"))))))
              org-agenda-custom-commands)
#+end_src

Command to review the week.
#+begin_src elisp
  (cl-pushnew '("r" "Review: Week"
                agenda ""
                ((org-agenda-overriding-header
                  (format "WEEK %s REVIEW"
                          (format-time-string "%U" (current-time))))
                 (org-habit-show-habits nil)
                 (org-agenda-span 'week)
                 (org-agenda-include-diary nil)
                 (org-agenda-show-all-dates t)
                 (org-agenda-clockreport-mode t)))
              org-agenda-custom-commands)
#+end_src

Command to review the month.
#+begin_src elisp
  (cl-pushnew '("R" "Review: Month"
                agenda ""
                ((org-agenda-overriding-header
                  (format "%s REVIEW"
                          (upcase (format-time-string "%B" (current-time)))))
                 (org-habit-show-habits nil)
                 (org-agenda-span 'month)
                 (org-agenda-include-diary nil)
                 (org-agenda-show-all-dates t)
                 (org-agenda-start-with-clockreport-mode t)))
              org-agenda-custom-commands)
#+end_src

Command to review the weekend.
#+begin_src elisp
  (cl-pushnew '("w" "Review: Weekend"
                agenda ""
                ((org-agenda-overriding-header "WEEKEND REVIEW")
                 (org-agenda-span 2)
                 (org-agenda-start-day "saturday")))
              org-agenda-custom-commands)
#+end_src
*** Org Clock-table
Define a custom clocktable formatter function for ~+org-clocktable-format~.
#+begin_src elisp
  (with-eval-after-load 'org-clock
    (defun +org-clocktable-format (&rest args)
      "A cleaner Org clocktable format."
      (apply #'org-clocktable-write-default args)
      (save-excursion
        ;; Rename headline
        (search-forward "Headline")
        (backward-kill-sexp)
        (insert "Tasks")
        (org-table-align)

        ;; Capitalise row
        (beginning-of-line)
        (setq this-command-keys-shift-translated t)
        (call-interactively 'end-of-line)
        (call-interactively 'capitalize-dwim)
        (deactivate-mark)

        ;; Remove cookies
        (org-mark-element)
        (while (re-search-forward
                "\\[[0-9]+\\(?:/[0-9]\\|%\\)\\] " nil t)
          (replace-match ""))
        (org-table-align)
        (exchange-point-and-mark t)

        ;; Move total time row to the end of the table
        (kill-whole-line)
        (condition-case nil
            (while t
              (org-table-move-row-down))
          (error nil))
        (org-table-insert-hline t))))
#+end_src

Set default clocktable parameters.
#+begin_src elisp :tangle no :noweb-ref org-agenda-settings
  (setq-default
   org-agenda-clockreport-parameter-plist
   '(:link t :fileskip0 t :hidefiles t :formatter +org-clocktable-format))
#+end_src

*** Org Capture
#+begin_src elisp :noweb yes
  (with-eval-after-load 'org-capture
    <<org-capture-quick-keys>>
    <<org-capture-personal-keys>>)
#+end_src

**** Capture Templates
Task template.
#+begin_src elisp :tangle no :noweb-ref capture-task-template
  "* IDLE %^{Task} %^G
  :PROPERTIES:
  :CAPTURED: %U
  :END:"
#+end_src

Event template.
#+begin_src elisp :tangle no :noweb-ref capture-event-template
  "* %^{Event}
  SCHEDULED: %^T
  :PROPERTIES:
  :CAPTURED: %U
  :END:"
#+end_src

Note template.
#+begin_src elisp :tangle no :noweb-ref capture-note-template
  "* %^{Note} %^G
  :PROPERTIES:
  :CAPTURED: %U
  :END:"
#+end_src

Meeting template.
#+begin_src elisp :tangle no :noweb-ref capture-meeting-template
  "* TODO Meeting with: *%\\1* at *%\\2*                                       :meeting:
  SCHEDULED: %^T
  :PROPERTIES:
  :CAPTURED: %U
  :NAME: %^{Name}
  :LOCATION: %^{Location}
  :END:"
#+end_src

Goal setting template.
#+begin_src elisp :tangle no :noweb-ref capture-goal-template
  "* [/] %^{Goal Title}                                                           :goal:
  DEADLINE: %^{When do you expect this goal to be completed?}t
  :PROPERTIES:
  :CAPTURED: %U
  :END:

  :SMART:
  :SPECIFIC: %^{What SPECIFICALLY is the goal?}
  :MEASURABLE: %^{What RESULTS will determine the completion of the goal?}
  :ACTIONABLE: %^{What ACTIONS will you take to accomplish this goal?}
  :RELEVANT: %^{Why is it IMPORTANT to accomplish this goal?}
  :TIMELY: Due by =DEADLINE=
  :END:"
#+end_src

Daily review template.
#+begin_src elisp :tangle no :noweb-ref capture-weekly-review-template
  ":PROPERTIES:
   :CAPTURED: %U
   :END:
   %?
   ,,#+BEGIN: clocktable :scope agenda-with-archives :block %t
   ,,#+END:"
#+end_src

Weekly review template.
#+begin_src elisp :tangle no :noweb-ref capture-weekly-review-template
  ":PROPERTIES:
   :CAPTURED: %U
   :END:
   %?
   ,,#+BEGIN: clocktable :scope agenda-with-archives :block %<%Y>-W%<%V>
   ,,#+END:"
#+end_src

Monthly review template.
#+begin_src elisp :tangle no :noweb-ref capture-monthly-review-template
  ":PROPERTIES:
   :CAPTURED: %U
   :END:
   %?
   ,,#+BEGIN: clocktable :scope agenda-with-archives :block %<%Y-%m>
   ,,#+END:"
#+end_src

**** Capture Template Quick Keys
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-capture-quick-keys :noweb yes :noweb-prefix no
:END:

Capture a quick task.
#+begin_src elisp :noweb yes
  (cl-pushnew
   '("c" "Quick Task (personal)" entry
     (file "agenda/tasks.org")
     <<capture-task-template>>
     :prepend t
     :empty-lines-after 1
     :immediate-finish t)
   org-capture-templates)
#+end_src

Capture event.
#+begin_src elisp
  (cl-pushnew
   '("v" "Event" entry
     (file "agenda/events.org")
     <<capture-event>>
     :prepend t
     :empty-lines-after 1
     :immediate-finish t)
   org-capture-templates)
#+end_src

Captured to last clocked file.
#+begin_src elisp
  (cl-pushnew
   '("l" "Clocked File" item (clock) nil)
   org-capture-templates)
#+end_src

**** Capture Template Personal Keys
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref org-capture-personal-keys :noweb yes :noweb-prefix no
:END:

Personal capture templates.
#+begin_src elisp
  (cl-pushnew '("p" "personal...") org-capture-templates)

  (cl-pushnew
   '("pt" "Task" entry
     (file "agenda/tasks.org")
     <<capture-task-template>>
     :prepend t
     :empty-lines-after 1
     :immediate-finish t)
   org-capture-templates)

  (cl-pushnew
   '("pT" "Detailed Task" entry
     (file "agenda/tasks.org")
     <<capture-task-template>>
     :prepend t
     :empty-lines-after 1)
   org-capture-templates)

  (cl-pushnew
   '("pp" "Plan Today" checkitem
     (file+olp+datetree "agenda/planner.org")
     nil
     :empty-lines-after 1
     :jump-to-captured t)
   org-capture-templates)

  (cl-pushnew
   '("pn" "Note" entry (file "notes/notes.org")
     <<capture-note-template>>
     :empty-lines-after 1)
   org-capture-templates)

  (cl-pushnew
   '("pe" "Meeting" entry
     (file "agenda/tasks.org")
     <<capture-meeting-template>>
     :prepend t
     :empty-lines-after 1)
   org-capture-templates)
#+end_src

Review captures.
#+begin_src elisp
  (cl-pushnew '("pr" "Review") org-capture-templates)

  (cl-pushnew
   '("prd" "Daily Review" plain
     (file+olp+datetree "agenda/planner.org" "Reviews" "Daily")
     <<capture-daily-review-template>>
     :time-prompt t
     :empty-lines-after 1)
   org-capture-templates)

  (cl-pushnew
   '("prw" "Weekly Review" plain
     (file+olp+datetree "agenda/planner.org" "Reviews" "Weekly")
     <<capture-weekly-review-template>>
     :time-prompt t
     :tree-type week
     :jump-to-captured t
     :empty-lines-after 1)
   org-capture-templates)

  (cl-pushnew
   '("prm" "Monthly Review" plain
     (file+olp+datetree "agenda/planner.org" "Reviews" "Monthly")
     <<capture-monthly-review-template>>
     :time-prompt t
     :tree-type month
     :jump-to-captured t
     :empty-lines-after 1)
   org-capture-templates)
#+end_src

*** Org Encryption
Enable org-crypt module.
#+begin_src elisp
  (with-eval-after-load 'org
    (cl-pushnew 'org-crypt org-modules))
#+end_src

Use symmetrical keys for encryption/decryption.
#+begin_src elisp
  (with-eval-after-load 'org-crypt
    (setq-default org-crypt-key nil))
#+end_src

Redirect pinentry queries to Emacs to query passphrases through the minibuffer.
#+begin_src elisp
  (with-eval-after-load 'epg-config
    (setq-default epg-pinentry-mode 'loopback))
#+end_src

Encrypt file on save.
#+begin_src elisp
  (with-eval-after-load 'org-crypt
    (setq-default org-crypt-disable-auto-save 'encrypt)
    (org-crypt-use-before-save-magic))
#+end_src

*** Org Refile
Display filename on refile target and enable instant completion.
#+begin_src elisp
  (with-eval-after-load 'org-refile
    (setq-default
     org-refile-use-outline-path 'file
     org-outline-path-complete-in-steps nil))
#+end_src

Increase refile depth for current buffer and agenda files.
#+begin_src elisp
  (with-eval-after-load 'org-refile
    (cl-pushnew '(nil :maxlevel . 9) org-refile-targets)
    (cl-pushnew '(org-agenda-files :maxlevel . 9) org-refile-targets))
#+end_src

** Error Checking
Enable flymake for programming modes.
#+begin_src elisp
  (add-hook 'prog-mode-hook #'flymake-mode)
#+end_src

Set flymake keybindings.
#+begin_src elisp
  (with-eval-after-load 'flymake
    (keymap-set flymake-mode-map "M-# #" #'flymake-show-buffer-diagnostics)
    (keymap-set flymake-mode-map "M-# M-#" #'flymake-show-project-diagnostics)
    (keymap-set flymake-mode-map "M-# M-]" #'flymake-goto-next-error)
    (keymap-set flymake-mode-map "M-# M-[" #'flymake-goto-prev-error))
#+end_src

** Compilation
Set make command and scroll output.
#+begin_src elisp
  (with-eval-after-load 'compile
    (setq-default compile-command "make"
                  compilation-scroll-output t))
#+end_src

Truncate lines in compilation buffer.
#+begin_src elisp
  (with-eval-after-load 'compile
    (add-hook 'compilation-mode-hook
              (lambda () (toggle-truncate-lines 1))))
#+end_src

Auto-close compilation buffer when there are no errors or warnings.
#+begin_src elisp
  (with-eval-after-load 'compile
    (add-hook 'compilation-finish-functions
              (lambda (buffer string)
                "Bury a compilation buffer if succeeded without warnings "
                (require 'winner)
                (when (and
                       (buffer-live-p buffer)
                       (string-match "compilation" (buffer-name buffer))
                       (string-match "finished" string)
                       (eq 0 compilation-num-warnings-found)
                       (eq 0 compilation-num-errors-found))
                  (run-with-timer 1 nil (lambda ()
                                          (winner-undo)
                                          (message "Compilation successful")))))))
#+end_src

** Shell
Make shell prompt and output read-only.
#+begin_src elisp
  (with-eval-after-load 'shell
    (setq-default comint-prompt-read-only t)
    (defadvice comint-output-filter (after output-readonly activate)
      "Set last process output read-only."
      (add-text-properties
       comint-last-output-start (line-end-position 0)
       '(read-only t rear-nonsticky (inhibit-line-move-field-capture)))))
#+end_src

** Eshell
Set the eshell welcome message.
#+begin_src elisp
  (with-eval-after-load 'em-banner
    (setq-default eshell-banner-message ""))
#+end_src

Set the prompt for eshell.
#+begin_src elisp :noweb yes
  (with-eval-after-load 'em-prompt
    <<eshell-prompt>>)
#+end_src

*** Eshell Prompt
:PROPERTIES:
:header-args:elisp: :tangle no :noweb-ref eshell-prompt :results none
:ID:       531f8b73-c91a-449d-a13b-2bddc8fe13c7
:END:

Prompt faces.
#+begin_src elisp
  (defface +eshell-user
    '((t :foreground "DodgerBlue" :weight bold))
    "The face used to highlight $USER."
    :group 'eshell-prompt)

  (defface +eshell-host
    '((t :foreground "indianred" :weight bold))
    "The face used to highlight $HOSTNAME."
    :group 'eshell-prompt)

  (defface +eshell-dir
    '((t :foreground "goldenrod" :weight bold))
    "The face used to highlight $PWD."
    :group 'eshell-prompt)

  (defface +eshell-branch
    '((t :foreground "MediumSeaGreen" :weight bold))
    "The face used to highlight the Git branch."
    :group 'eshell-prompt)

  (set-face-attribute 'eshell-prompt nil :inherit 'default)
#+end_src

Prompt functions.
#+begin_src elisp
  (require 'vc-git)
  (defun +eshell-git-branch ()
    "Get the Git branch for the current working directory."
    (vc-git--symbolic-ref (eshell/pwd)))

  (defun +eshell-dir (&optional limit)
    "Get full directory path or restrict to current folder if LIMIT."
    (if limit
        (file-name-nondirectory (abbreviate-file-name (eshell/pwd)))
      (abbreviate-file-name (eshell/pwd))))

  (defun +eshell-prompt-layout (user dir host branch)
    "Return a prompt layout using USER, DIR, HOST, & BRANCH."
    (propertize
     (concat
      "[" user "@" host " in " dir (when branch (concat " on " branch)) "] ")))

  (defun +eshell-prompt-function ()
    "Custom eshell prompt function."
    (let ((branch (+eshell-git-branch)))
      (+eshell-prompt-layout
       (propertize (eshell-user-name) 'face '+eshell-user)
       (propertize (+eshell-dir t) 'face '+eshell-dir)
       (propertize (system-name) 'face '+eshell-host)
       (when branch (propertize branch 'face '+eshell-branch)))))
#+end_src

Prompt settings.
#+begin_src elisp
  (setq-default
   eshell-highlight-prompt nil
   eshell-prompt-function #'+eshell-prompt-function)
#+end_src

** Diff-mode
Rebind diff-mode's ~diff-goto-source~ keybinding (due to conflict with =M-o=).
Add version control next action shortcut in diff-mode buffer.
#+begin_src elisp
  (with-eval-after-load 'diff-mode
    (keymap-set diff-mode-map "M-j" #'diff-goto-source)
    (keymap-set diff-mode-map "v" #'vc-next-action))
#+end_src

* Global Keybindings
Unlock previously unusable keybinding.
#+begin_src elisp
  (define-key input-decode-map [?\C-\[] (kbd "<C-[>"))
#+end_src

To prevent other packages overriding any custom global keybinding, all custom global keybinding are stored in a global minor mode.
#+begin_src elisp
  (define-minor-mode +global-keys
    "Minor to store my custom global keybindings."
    :global t
    :lighter " +GKEYS"
    :keymap (make-sparse-keymap))

  (+global-keys)
#+end_src

File actions.
#+begin_src elisp
  (keymap-set +global-keys-map "C-x x R" #'rename-visited-file)
  (keymap-set +global-keys-map "C-x x D" #'delete-file)
  (keymap-set +global-keys-map "C-x M-f" #'recentf)
#+end_src

Window actions.
#+begin_src elisp
  (keymap-set +global-keys-map "M-o" #'other-window)
  (keymap-set +global-keys-map "M-O" #'window-swap-states)
  (keymap-set +global-keys-map "M-V" #'scroll-other-window-down)
  (keymap-set +global-keys-map "C-S-V" #'scroll-other-window)
  (keymap-set +global-keys-map "C-M-<" #'beginning-of-buffer-other-window)
  (keymap-set +global-keys-map "C-M->" #'end-of-buffer-other-window)
#+end_src

Buffer actions.
#+begin_src elisp
  (keymap-set +global-keys-map "C-M--" #'kill-this-buffer)
  (keymap-set +global-keys-map "C-M-0" #'kill-buffer-and-window)
  (keymap-set +global-keys-map "C-M-]" #'next-buffer)
  (keymap-set +global-keys-map "C-x M-b" #'bs-show)
  (keymap-set +global-keys-map "C-x M-i" #'ibuffer)
  (keymap-set +global-keys-map "M-ESC" #'previous-buffer) ; C-M-[ translates to M-ESC
  (keymap-set +global-keys-map "M-_" #'bury-buffer)
  (keymap-set +global-keys-map "M-*" #'unbury-buffer)
#+end_src

Set meta-key quick actions to mirror =C-x DIGIT= bindings, therefore reducing the need for additional keypresses.
These bindings override their corresponding numerical argument, however these can be can be alternatively called with =C-u DIGIT= or =C-DIGIT=.
#+begin_src elisp
  (keymap-set +global-keys-map "M-0" #'delete-window)
  (keymap-set +global-keys-map "M-1" #'delete-other-windows)
  (keymap-set +global-keys-map "M-2" #'split-window-below)
  (keymap-set +global-keys-map "M-3" #'split-window-right)
  (keymap-set +global-keys-map "M-4" #'ctl-x-4-prefix)
  (keymap-set +global-keys-map "M-5" #'ctl-x-5-prefix)
  (keymap-set +global-keys-map "M-6" (keymap-global-lookup "C-x 6"))
  (keymap-set +global-keys-map "M-7" (keymap-global-lookup "C-x 7"))
  (keymap-set +global-keys-map "M-8" (keymap-global-lookup "C-x 8"))
  (keymap-set +global-keys-map "M-9" (keymap-global-lookup "C-x 9"))
#+end_src

Tab actions.
#+begin_src elisp
  (keymap-set +global-keys-map "C-x C-<tab>" #'tab-new)
  (keymap-set +global-keys-map "C-x C-<backspace>" #'tab-close)
  (keymap-set +global-keys-map "C-x t l" #'tab-list)
  (keymap-set +global-keys-map "C-x t <tab>" #'toggle-frame-tab-bar)
#+end_src

Improve region text manipulation.
#+begin_src elisp
  (keymap-set +global-keys-map "M-c" #'capitalize-dwim)
  (keymap-set +global-keys-map "M-l" #'downcase-dwim)
  (keymap-set +global-keys-map "M-u" #'upcase-dwim)
  (keymap-set +global-keys-map "M-U" #'upcase-char)
#+end_src

Help actions.
#+begin_src elisp
  (keymap-set +global-keys-map "C-h M" #'describe-keymap)
  (keymap-set +global-keys-map "C-h j" #'describe-char)
  (keymap-set +global-keys-map "C-h y" (lambda ()
                                         "Open the Directory node for info on major topics."
                                         (interactive)
                                         (require 'info)
                                         (Info-find-node "dir" "top")))
#+end_src

Display modes.
#+begin_src elisp
  (keymap-set +global-keys-map "C-x x c" #'display-fill-column-indicator-mode)
  (keymap-set +global-keys-map "C-x x l" #'display-line-numbers-mode)
  (keymap-set +global-keys-map "C-x x h" #'hl-line-mode)
  (keymap-set +global-keys-map "C-x x o" #'overwrite-mode)
  (keymap-set +global-keys-map "C-x x s" #'prettify-symbols-mode)
  (keymap-set +global-keys-map "C-x x v" #'visual-line-mode)
  (keymap-set +global-keys-map "C-x x SPC" #'whitespace-mode)
#+end_src

Org functions.
#+begin_src elisp
  (keymap-set +global-keys-map "C-c a" #'org-agenda)
  (keymap-set +global-keys-map "C-c c" #'org-capture)
  (keymap-set +global-keys-map "C-c l" #'org-store-link)
  (keymap-set +global-keys-map "C-c C-x ," #'org-timer-pause-or-continue)
  (keymap-set +global-keys-map "C-c C-x ." #'org-timer)
  (keymap-set +global-keys-map "C-c C-x 0" #'org-timer-start)
  (keymap-set +global-keys-map "C-c C-x ;" #'org-timer-set-timer)
  (keymap-set +global-keys-map "C-c C-x _" #'org-timer-stop)
#+end_src

Toggle theme.
#+begin_src elisp
  (keymap-set +global-keys-map "C-c t" #'modus-themes-toggle)
#+end_src

* EXTENSIONS
#+begin_src elisp
  (add-to-list 'load-path "~/shared/modally/")
  (require 'modally)
  (modally-mode)
#+end_src