summaryrefslogtreecommitdiff
path: root/.config/emacs/init.el
blob: d827c23cca73bafbd5cc72830f6beb16b65d515e (about) (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
;;; init.el --- Emacs configuration  -*- lexical-binding: t; -*-

;; Copyright (C) 2023-2069  Thanos Apollo

;; Author: Thanos Apollo <[email protected]>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;; ╭━━━━┳╮╱╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━╮╱╱╱╱╱╭╮╭╮╱╱╱╱╱╱╱╱╭━━━╮
;; ┃╭╮╭╮┃┃╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━╮┃╱╱╱╱╱┃┃┃┃╱╱╱╱╱╱╱╱┃╭━━╯
;; ╰╯┃┃╰┫╰━┳━━┳━╮╭━━┳━━╮┃┃╱┃┣━━┳━━┫┃┃┃╭━━╮╱╱╱╱┃╰━━┳╮╭┳━━┳━━┳━━╮
;; ╱╱┃┃╱┃╭╮┃╭╮┃╭╮┫╭╮┃━━┫┃╰━╯┃╭╮┃╭╮┃┃┃┃┃╭╮┃╭━━╮┃╭━━┫╰╯┃╭╮┃╭━┫━━┫
;; ╱╱┃┃╱┃┃┃┃╭╮┃┃┃┃╰╯┣━━┃┃╭━╮┃╰╯┃╰╯┃╰┫╰┫╰╯┃╰━━╯┃╰━━┫┃┃┃╭╮┃╰━╋━━┃
;; ╱╱╰╯╱╰╯╰┻╯╰┻╯╰┻━━┻━━╯╰╯╱╰┫╭━┻━━┻━┻━┻━━╯╱╱╱╱╰━━━┻┻┻┻╯╰┻━━┻━━╯
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╰╯
;;; Code:

;; Disable package.el
(setq package-enable-at-startup nil
      package-archives nil)

(setf user-full-name "Thanos Apollo"
      user-mail-address "[email protected]")


(defvar is-zeus (string= (system-name) "zeus"))
(defvar is-hermes (string= (system-name) "hermes"))
(defvar is-phone (string= (system-name) "localhost"))

;; Font
(custom-set-faces
 (if is-hermes '(default ((t (:inherit nil :height 120 :family "Jetbrains Mono"))))
   '(default ((t (:inherit nil :height 130 :family "Jetbrains Mono")))))
 '(variable-pitch ((t (:inherit t :height 125 :family "Iosevka Aile"))))
 '(org-modern-symbol ((t (:inherit t :family "Iosevka Aile")))))

;; Autoinsert
(auto-insert-mode 1)

(setq auto-insert-alist '((python-mode . "python.template"))
      auto-insert-directory (locate-user-emacs-file "insert"))

(add-to-list 'completion-styles 'initials t)

(setf tab-always-indent 'complete)

;; xref
(setf xref-show-xrefs-function #'consult-xref
      xref-show-definitions-function #'consult-xref)

;; Set and load custom.el
(setf custom-file (locate-user-emacs-file "custom.el"))
(load custom-file 'noerror)

;; Enable use-package support for imenu
(setf use-package-enable-imenu-support t)

;; Install straight.el
(defvar bootstrap-version)

(let ((bootstrap-file
       (expand-file-name
        "straight/repos/straight.el/bootstrap.el"
        (or (bound-and-true-p straight-base-dir)
            user-emacs-directory)))
      (bootstrap-version 7))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(require 'straight)

(setf straight-use-package-by-default t)

(setf straight-recipe-overrides
      '((transmission :type git :host nil :repo "[email protected]:/var/git/transmission.git")
	(gnosis :type git :host nil :repo "[email protected]:/var/git/gnosis.git")
	(pcmpl-emerge :type git :host nil :repo "[email protected]:/var/git/pcmpl-emerge.git")
	(pcmpl-rc :type git :host nil :repo "[email protected]:/var/git/pcmpl-rc.git")))

(use-package emacs
  :ensure t
  :config

  (setf browse-url-browser-function 'browse-url-generic
	browse-url-generic-program "librewolf"
	backup-directory-alist '((".*" . "~/.Trash"))
	sentence-end-double-space t
	default-input-method "bulgarian-phonetic"
	gc-cons-threshold 5000000
	disabled-command-function nil ;; Enable all commands
	url-privacy-level 'high
	calendar-date-style 'european)
  (if is-zeus
      (display-battery-mode 0)
    (display-battery-mode 1))

  ;; use ssh for tramp, faster for small files
  (setf tramp-default-method "ssh")

  (savehist-mode)
  (save-place-mode 1)
  (recentf-mode 1)
  (electric-pair-mode 1)
  :bind (("M-<backspace>" . 'backward-kill-sexp)
	 ("C-c L" . 'display-line-numbers-mode)
	 ("C-z" . nil)
	 ("C-c r" . 'rename-visited-file)
	 ("C-x 7" . 'window-swap-states)
	 ("C-c t" . 'create-text-scratch))
  :hook ((emacs-lisp-mode . prettify-symbols-mode)
	 (lisp-mode . prettify-symbols-mode)
	 (scheme-mode . prettify-symbols-mode)))

(use-package dired
  :straight nil
  :ensure nil
  :config
  (defun dired-watch-video ()
    "Watch play file with mpv."
    (interactive)
    (call-process-shell-command
     (format "mpv \"%s\"" (dired-get-filename)) nil 0))

  (defun dired-set-wallpaper ()
    "Set NAME as wallpaper using feh."
    (interactive)
    (thanos/wallpaper-set (dired-get-filename)))

  (defun dired-delete-files-except ()
    "Delete all files inside directory except match."
    (interactive)
    (let* ((directory (read-directory-name "Select directory: "))
	   (files (directory-files directory t))
	   (except-match (read-string "Except the ones that have: ")))
      (dolist (file files)
	(unless (or (string= "." (substring file -1))
		    (string= ".." (substring file -2))
		    (string-match except-match file))
	  (dired-delete-file file t)))))

  (defun dired-delete-files-match (&optional directory match)
    "Delete all files inside directory except match."
    (interactive)
    (let* ((directory (or directory (read-directory-name "Select directory: ")))
	   (files (directory-files directory t))
	   (match (or match (read-string "Delete files that match: "))))
      (dolist (file files)
	(when (string-match-p match file)
	  (dired-delete-file file t)))))

  (defun dired-rename-capitalize-file ()
    "Capitalize the base name of the file at point in a Dired buffer."
    (interactive)
    (let* ((file (dired-get-file-for-visit))
	   (new-file (capitalize (file-name-nondirectory file))))
      (if (string-prefix-p "." file)
	  (message "Skipping file starting with '.'")
	(progn
	  (rename-file file (concat (file-name-directory file) new-file))
	  (revert-buffer)
	  (message "Renamed %s to %s" file new-file)))))
  :bind ((:map dired-mode-map
	  ("b" . 'dired-up-directory)
	  ("v" . 'dired-watch-video)
	  ("z" . 'wdired-change-to-wdired-mode)
	  ("C-c w" . 'dired-set-wallpaper)
	  ("C-c d" . 'dired-delete-files-except)
	  ("C-c r" . 'dired-do-query-replace-regexp))))

(defun theme-invisible-dividers (_theme)
  "Make window dividers for THEME invisible."
  (let ((bg (face-background 'default)))
    (custom-set-faces
     `(fringe ((t :background ,bg :foreground ,bg)))
     `(window-divider ((t :background ,bg :foreground ,bg)))
     `(window-divider-first-pixel ((t :background ,bg :foreground ,bg)))
     `(window-divider-last-pixel ((t :background ,bg :foreground ,bg))))))

(add-hook 'enable-theme-functions #'theme-invisible-dividers)

;;;; Theming ;;;;
(setf inhibit-startup-message t
      initial-scratch-message nil)

(blink-cursor-mode -1)
(global-visual-line-mode 0)

(setf visible-bell nil
      display-line-numbers-type 'relative)

(column-number-mode)
(global-display-line-numbers-mode 1)

;; Transparency
(add-to-list 'default-frame-alist '(alpha-background . 85))

;; Theming
(global-visual-line-mode)

(defun thanos/terminal-theming ()
  "Customize theming when laucning Emacs as TUI."
  (unless (display-graphic-p (selected-frame))
    (set-face-background 'default "unspecified-bg" (selected-frame))
    (global-hl-line-mode 0)))

(add-hook 'window-setup-hook 'thanos/terminal-theming)

(when (equal is-phone nil)
  (scroll-bar-mode -1)
  (set-fringe-mode 10))

(tool-bar-mode -1)
(tooltip-mode -1)
(menu-bar-mode -1)

(use-package org
  :ensure t
  :config
  (setf org-directory "~/org/"
	org-agenda-files '("~/org/seminars.org" "~/org/lectures.org")
	org-default-notes-file (expand-file-name "notes.org" org-directory)
	org-ellipsis " "
	org-log-done 'time
	org-hide-emphasis-markers nil ;;change to t to hide emphasis markers
	org-table-convert-region-max-lines 20000
	org-log-done 'time
	org-todo-keywords '((sequence "TODO(t)" "SEMINAR(s)" "LECTURE(l)" "DONE(d)")))

  (setf org-structure-template-alist
	'(("e" . "src emacs-lisp")
	  ("p" . "src python")
	  ("l" . "src lisp")
	  ("b" . "src bash")
	  ("q" . "QUOTE")))

  (add-to-list 'org-latex-classes
               '("article"
		 "\\documentclass[11pt,a4paper]{article}
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{fixltx2e}
\\usepackage{graphicx}
\\usepackage{longtable}
\\usepackage{float}
\\usepackage{wrapfig}
\\usepackage{rotating}
\\usepackage[normalem]{ulem}
\\usepackage{amsmath}
\\usepackage{textcomp}
\\usepackage{marvosym}
\\usepackage{wasysym}
\\usepackage{amssymb}
\\usepackage{hyperref}
\\usepackage{mathpazo}
\\usepackage{color}
\\usepackage{enumerate}
\\definecolor{bg}{rgb}{0.95,0.95,0.95}
\\tolerance=1000
      [NO-DEFAULT-PACKAGES]
      [PACKAGES]
      [EXTRA]
\\linespread{1.1}
\\hypersetup{pdfborder=0 0 0}"
		 ("\\section{%s}" . "\\section*{%s}")
		 ("\\subsection{%s}" . "\\subsection*{%s}")
		 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
		 ("\\paragraph{%s}" . "\\paragraph*{%s}")))
  
  :hook ((org-mode . org-auto-tangle-mode)
	 (org-mode . (lambda () (display-line-numbers-mode -1) (flyspell-mode))))
  :bind (:map org-mode-map (("C-c l" . org-store-link)
			    ("C-c M-t" . org-todo))))

(defun org-insert-book ()
  "Insert org-link from ~/Library for book."
  (interactive)
  (let* ((book-path (read-file-name "Book: " "~/Library/")))
    (org-insert-link nil book-path (file-name-base book-path))))

(use-package org-modern
  :ensure t
  :config
  (setf org-modern-table nil
	org-modern-todo nil
	org-modern-tag nil)
  :hook ((org-mode . org-modern-mode)))

;; Create notes directory for org-roam
(unless (file-exists-p "~/Notes")
  (make-directory "~/Notes"))

(use-package org-roam
  :defer t
  :init
  (define-prefix-command 'thanos/notes-map)
  :config
  (setf org-roam-directory "~/Notes"
	org-roam-dailies-directory "daily/")

  (org-roam-db-autosync-enable)

  (setf org-roam-node-display-template
	(concat "${title:50} "(propertize "${tags:30}" 'face 'org-tag)))

  (setf org-roam-db-node-include-function
	(lambda ()
	  (not (or (member "journal" (org-get-tags))
		   (member "dailies" (org-get-tags))))))
  ;; Templates
  (setf org-roam-capture-templates
	'(("d" "default" plain
	   "%?"
	   :if-new (file+head "%<%Y%m%d%H%M%S>-${slug}.org" "#+title: ${title}\n#+startup: overview\n")
	   :unnarrowed t)
	  ("p" "MUS" plain "* Goals\n\n%?\n\n* Tasks\n\n** TODO Add initial tasks\n\n* Dates\n\n"
	   :if-new (file+head "%<%Y%m%d%H%M%S>-${slug}.org" "#+title: ${title}\n#+filetags: MUS")
	   :unnarrowed t))
	org-roam-dailies-capture-templates
	'(("d" "default" entry
	   "* %?"
	   :target (file+head "%<%Y-%m-%d>.org"
                              "#+title: %<%Y-%m-%d>\n#+filetags: :journal:\n"))
	  ("j" "journal" plain
	   "\n* Daily Notes\n\n* Goals\n+ []\n\n* Extras %?"
	   :target (file+head "%<%Y-%m-%d>.org"
			      "#+title: %<%Y-%m-%d>\n#+filetags: :journal:\n"))))

  (defun org-roam-ref-add-book ()
    "Insert org-link from Library."
    (interactive)
    (let ((book (format "file:%s" (read-file-name "Book: " (if is-zeus "/hdd/Library/" "~/Library/")))))
      book))

  (defun org-roam-sync-notes ()
    "Sync org-oram notes"
    (interactive)
    (let ((git (executable-find "git"))
	  (default-directory org-roam-directory))
      (message "Synching org-roam notes %s" org-roam-directory)
      (unless git
	(error "Git not found, please install `git'"))
      (unless (file-exists-p (expand-file-name ".git" gnosis-dir))
	(message "Creating git repository")
	(vc-create-repo 'Git))
      (shell-command "git pull")
      (shell-command (format "%s %s" git "add ."))
      (shell-command (format "%s %s %s" git "commit -m" (shell-quote-argument "Update org-roam notes")))
      (vc-git-push nil))
    (funcall 'org-roam-db-sync))
  
  :bind (("C-c n" . thanos/notes-map)
	 :map thanos/notes-map
	 ("t" . org-roam-buffer-toggle)
         ("f" . org-roam-node-find)
         ("i" . org-roam-node-insert)
	 ("d" . org-roam-dailies-goto-today)
	 ("D" . org-roam-dailies-goto-date)
	 :map org-mode-map
	 ("C-c C-." . org-roam-tag-add)
	 ("C-c i" . org-id-get-create)))

(use-package org-roam-ui
  :defer t)

(use-package modus-themes
  :straight t
  :config
  (setf modus-themes-italic-constructs nil
	modus-themes-bold-constructs nil
	modus-themes-mixed-fonts nil
	modus-themes-variable-pitch-ui nil
	modus-themes-custom-auto-reload t
	modus-themes-disable-other-themes t
	modus-themes-prompts '(italic)
	modus-themes-completions '((matches . (extrabold))
				   (selection . (semibold italic text-also underline)))
	modus-themes-org-blocks 'tinted-background)
  ;; Palette overrides
  (setf modus-themes-common-palette-overrides
	'((fg-line-number-active cyan-intense)
	  ;; (bg-main "#1d2021") ;;grubox-hard
	  ;; (bg-main "#191919") ;; 1337
	  ;; (bg-main "#1d1f21") ;; tomorrow night
	  ;; (bg-main "#151515") ;; jazz
	  (bg-main "#0C0C0C") ;; random black
	  ;; (bg-main "#171717") ;; badger
	  ;; (overline-heading-1 gold)
	  (fg-heading-1 red-warmer)
	  ;; (bg-heading-1 bg-blue-nuanced)
          (bg-line-number-inactive unspecified)
          (bg-line-number-active unspecified)
	  (bg-paren-match bg-magenta-intense)
          (underline-paren-match fg-main)
	  (underline-err red-intense)
          (underline-warning yellow-faint)
          (underline-note cyan-faint)
	  (string "#86B187")
	  (border-mode-line-active unspecified)
          (border-mode-line-inactive unspecified)
	  (bg-mode-line-active "#433F4f") ;; subtle lavender
	  (bg-mode-line-inactive "#1D1D1D")
	  ;; set fg from badger theme
          (fg-mode-line-active "#F6F3E8")
	  (bg-hl-line bg-dim)
	  (cursor slate)
	  (prose-todo green-intense)
	  (prose-done bg-term-white)
	  (fg-prompt yellow-faint)
	  ,@modus-themes-preset-overrides-intense))
  ;; Headings
  (setf modus-themes-headings
	'((1 . (ultrabold  1.35))
          (2 . (semibold  1.2))
          (agenda-date . (1.3))
          (agenda-structure . (variable-pitch light 1.8))
          (t . (1.15))))
  ;; Load modus
  (load-theme 'modus-vivendi t))

(use-package vertico
  :ensure t
  :config
  (vertico-mode))

(use-package marginalia
  :ensure t
  :config
  (marginalia-mode))

(use-package consult
  :ensure t
  :init (define-prefix-command 'thanos/search)
  :bind (("C-x r d" . 'bookmark-delete)
	 ("C-x r C-r" . 'bookmark-rename)
	 ("C-x r C-j" . 'consult-register)
	 ("C-x r SPC" . 'consult-register-store)
	 ("C-x r b" . 'consult-bookmark)
	 ("C-c m" . 'consult-imenu)
	 ("C-x b" . 'consult-buffer)
	 ("C-x C-b" . 'switch-to-prev-buffer)
         ("M-y" . 'consult-yank-from-kill-ring)
	 ("C-s" . 'thanos/search)
	 :map thanos/search
	 ("f" . 'isearch-forward)
	 ("r" . 'isearch-backward)
	 ("s" . 'consult-line)
         ("i" . 'change-inner)
	 ("C-f" . 'consult-find)
	 ("C-g" . 'consult-grep)
	 ("C-i" . 'consult-info)
	 ("C-l" . 'consult-locate)
	 :map project-prefix-map
	 ("b" . 'consult-project-buffer)))

(use-package which-key
  :ensure t
  :config
  (which-key-mode 1))

(use-package elfeed
  :defer t
  :config
  (setf elfeed-search-filter "@1-week-ago +unread -hackernoon"
	browse-url-browser-function #'browse-url-default-browser
	elfeed-db-directory "~/.config/elfeed")
  ;; Feeds
  (setf elfeed-feeds
	'(("https://hackaday.com/blog/feed/"
	   hackaday linux)
	  ("https://thanosapollo.org/index.xml"
	   thanos)
	  ("http://wikileaks.org/feed"
	   wikileaks)
	  ("https://hackernoon.com/feed"
	   hackernoon)
	  ("https://torrentfreak.com/feed"
	   torrentfreak piracy)
	  ("https://www.science.org/action/showFeed?type=etoc&feed=rss&jc=sciimmunol"
	   science)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.medscape.com%2Findex%2Flist_13470_0&url_selector=a.title&url_pattern=viewarticle%2F.*&content_selector=div.article__main-content&content_cleanup=&title_cleanup=+-+Index&limit=&format=Atom" medscape med)
	  ("http://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fmedfac.mu-sofia.com%2Fen%2Fnews%2F&url_selector=div.news-card&url_pattern=%2F*&content_selector=article.richtext-area&content_cleanup=&title_cleanup=&limit=&format=Atom" musofia med)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.tovima.gr%2Flatest-news%2F&url_selector=a.columns&url_pattern=&content_selector=div.main-content&content_cleanup=div.wrap-facebook%2Cdiv.googlenews&title_cleanup=&limit=&format=Atom" tovima greek news)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.tovima.gr%2Fcategory%2Fscience%2F&url_selector=a.columns&url_pattern=&content_selector=div.main-content&content_cleanup=div.wrap-facebook%2Cdiv.googlenews&title_cleanup=&limit=&format=Atom" tovima greek science news)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.reuters.com%2Fworld%2F&url_selector=a.media-story-card__headline__tFMEu&url_pattern=&content_selector=article.article__container__2MUeZ&content_cleanup=div.info-content__toolbar__3AkHm%2C+div.article-body__row__dFOPA%2C+div.article__read-next__Kjxdw&title_cleanup=&limit=&format=Atom" reuters world news)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.reuters.com%2Fbusiness%2Fhealthcare-pharmaceuticals%2F&url_selector=a.basic-card__title__37xHl&url_pattern=&content_selector=div.article-body__wrapper__3IxHM&content_cleanup=svg.link__new-tab-symbol__3T19s%2C+div.toolbar__container__3kIkw%2C+div.article-body__row__dFOPA+article-body__element__2p5pI&title_cleanup=&limit=&format=Atom" reuters med news)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.reuters.com%2Ftechnology%2Fcybersecurity%2F&url_selector=a.media-story-card__headline__tFMEu%2C+a.media-story-card__heading__eqhp9&url_pattern=&content_selector=article.article__container__2MUeZ&content_cleanup=div.info-content__toolbar__3AkHm%2C+svg.link__new-tab-symbol__3T19s%2C+div.article-body__row__dFOPA%2C+div.read-next-tablet-up__container__3MpHN%2C+div.author-bio__multiple-authors__5YGrG%2C+div.article__read-next__Kjxdw&title_cleanup=&limit=&format=Atom" reuters cybersec news)
	  ("https://annas-blog.org/rss.xml" anna piracy)
	  ("https://planet.emacslife.com/atom.xml" emacs emacslife)
	  ("https://localmonero.co/static/rss/the-monero-standard/feed.xml" monero)
	  ("https://devonzuegel.com/feed" devon)
	  ("https://www.addtoany.com/add_to/feed?linkurl=http%3A%2F%2Fwww.thelancet.com%2Frssfeed%2Flancet_online.xml&type=feed&linkname=The%20Lancet%20Online%20First&linknote=" lancet med)
	  ("https://rss-bridge.thanosapollo.org/?action=display&bridge=CssSelectorBridge&home_page=https%3A%2F%2Fwww.wired.com%2Fmost-recent%2F&url_selector=a.SummaryItemHedLink-civMjp&url_pattern=&content_selector=article.main-content&content_cleanup=div.ActionBarWrapperContent-lasBkU&title_cleanup=&limit=&format=Atom" wired tech news)
	  ("https://www.propublica.org/feeds/propublica/main" probublica news)
	  ("http://tools.cdc.gov/podcasts/feed.asp?feedid=183" cdc med)
	  ("http://planet.lisp.org/rss20.xml" lisp planetlisp)
	  ("https://guix.gnu.org/feeds/blog.atom" guix)
	  ("https://protesilaos.com/master.xml" prot)
	  ;; YouTube
	  ("https://www.youtube.com/feeds/videos.xml?channel_id=UCVls1GmFKf6WlTraIb_IaJg"
	   distrotube yt linux)
	  ("https://www.youtube.com/feeds/videos.xml?channel_id=UCld68syR8Wi-GY_n4CaoJGA"
	   brodie yt linux)
	  ("https://www.youtube.com/feeds/videos.xml?channel_id=UCAiiOTio8Yu69c3XnR7nQBQ"
	   systemcrafters yt linux)
	  ("https://www.youtube.com/feeds/videos.xml?channel_id=UC6QYFutt9cluQ3uSM963_KQ"
	   ninja yt med)
	  ("https://www.youtube.com/feeds/videos.xml?channel_id=UC1yNl2E66ZzKApQdRuTQ4tw"
	   sabine yt physics)
	  ("https://www.aartaka.me.eu.org/"
	   artyom blog lisp)
	  ("https://nyxt-browser.com/feed"
	   nyxt lisp)))

  (defun elfeed-mpv (&optional use-generic-p)
    "Play video link with mpv."
    (interactive "P")
    (let ((entries (elfeed-search-selected)))
      (cl-loop for entry in entries
	       do (elfeed-untag entry 'unread)
	       when (elfeed-entry-link entry)
	       do (start-process-shell-command "elfeed-video" nil (format "mpv \"%s\"" it)))
      (mapc #'elfeed-search-update-entry entries)
      (unless (use-region-p) (forward-line))))
  
  :bind (("C-x f" . elfeed)
	 :map elfeed-search-mode-map
	 ("v" . 'elfeed-mpv)
	 ("U" . 'elfeed-update))
  :hook ((elfeed-searchacw-mode . (lambda () (display-line-numbers-mode 0)))))

;; Python
(use-package python-mode
  :defer t
  :config
  (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)))

(use-package pyenv
  :defer t)

(use-package elpy
  :straight nil
  :ensure nil
  :init
  (elpy-enable))

;; Clojure
(use-package cider
  :defer t)

(use-package clojure-mode
  :defer t)

(use-package rainbow-delimiters
  :defer t
  :hook ((emacs-lisp-mode . rainbow-delimiters-mode)
         (lisp-mode . rainbow-delimiters-mode)
	 (clojure-mode . rainbow-delimiters-mode)
         (scheme-mode . rainbow-delimiters-mode)))

;; (use-package paredit
;;   :ensure t
;;   :hook ((emacs-lisp-mode . paredit-mode)
;; 	 (lisp-mode . paredit-mode)
;; 	 (clojure-mode . paredit-mode)
;; 	 (scheme-mode . paredit-mode)))

(use-package sly
  :init (setf inferior-lisp-program "sbcl")
  :defer t)

(use-package helpful
  :defer t
  :bind (("C-h f" . 'helpful-callable)
	 ("C-h v" . 'helpful-variable)
	 ("C-h k" . 'helpful-key)
	 ("C-h x" . 'helpful-command)
	 ("C-h ." . 'helpful-at-point)
	 ("C-h F" . 'helpful-function)
	 ("C-h C-k" . 'helpful-kill-buffers)
	 ("C-h a" . 'apropos)
	 ("C-h C-m" . 'info-apropos)))

(use-package ox-hugo
  :ensure t
  :config
  (setf org-hugo-section "post"))

(use-package json-mode
  :defer t
  :config
  (add-to-list 'auto-mode-alist '("\\.json'" . json-mode)))

(defun project-magit ()
  "Run magit-status in the current project's root."
  (interactive)
  (magit-status-setup-buffer (project-root (project-current t))))

(use-package magit
  :defer t
  :config
  (setf magit-display-buffer-function #'magit-display-buffer-same-window-except-diff-v1)
  :bind (:map project-prefix-map ("g" . 'project-magit)))

(use-package corfu
  :ensure t
  :config
  (global-corfu-mode)
  (corfu-popupinfo-mode)
  (setf corfu-auto t
	corfu-auto-delay 0.1
	corfu-auto-prefix 2
	corfu-cycle t
	corfu-popupinfo-delay 0.3
	corfu-quit-at-boundary 'separator
	corfu-quit-no-match t
        corfu-preselect 'first
	corfu-preview-current t
	corfu-echo-mode t)
  (setf indent-tabs-mode nil))

(defun insert-brackets (&optional arg)
  "Insert ARG brackets."
  (interactive "P")
  (insert-pair arg ?\[ ?\]))

(global-set-key (kbd "C-x M-[") 'insert-brackets)


(use-package orderless
  :init (add-to-list 'completion-styles 'initials t)
  :ensure t
  :config
  (setf completion-category-overrides '((file (style basic partial-completion)))
	completion-styles '(orderless)
	completion-cycle-threshold 2))

(use-package pdf-tools
  :straight nil
  :ensure nil
  :config
  (add-to-list 'auto-mode-alist '("\\.pdf\\'" . pdf-view-mode))
  :hook
  ((pdf-view-mode . (lambda ()
		      (display-line-numbers-mode 0)
		      ;; Guix package emacs-pdf-tools does not enable
		      ;; minor modes automatically
		      (pdf-tools-enable-minor-modes)))))

(use-package markdown-mode
  :defer t
  :config
  (setq markdown-header-scaling t
        markdown-command "multimarkdown")
  (add-to-list 'auto-mode-alist '("\\.md\\'" . gfm-mode))
  :hook ((markdown-mode . flyspell-mode)))

(use-package org-auto-tangle
  :defer t)

(use-package org-present
  :defer t)

(use-package expand-region
  :defer t)

(use-package change-inner
  :after expand-region
  :bind ((:map thanos/search ("i" . 'change-inner))))

(use-package nov
  :straight nil
  :defer t
  :config
  (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode)))

(use-package eshell-syntax-highlighting
  :defer t)

(use-package emojify
  :ensure t
  :hook (erc-mode . emojify-mode)
  :commands emojify-mode)

(use-package flycheck-package
  :straight t
  :ensure t
  :after flycheck)

(use-package flycheck
  :ensure t
  :config
  (setf flycheck-emacs-lisp-load-path 'inherit)
  (global-flycheck-mode)
  :hook ((emacs-lisp-mode . (lambda () (flycheck-mode) (flycheck-package-setup)))))

;; Shells
(use-package vterm
  :straight nil
  :ensure nil
  :bind (("C-c v" . vterm)
	 :map vterm-mode-map
	 ("M-&" . 'async-shell-command)
	 ("C-c C-y" . 'vterm-copy-mode))
  :hook
  ((vterm-mode . (lambda () (display-line-numbers-mode -1)))))

(use-package shell
  :defer t
  :bind (("C-c V" . shell)
	 :map shell-mode-map
	 ("C-l" . 'comint-clear-buffer))
  :hook ((shell-mode . (lambda () (display-line-numbers-mode -1)))))

(defvar thanos/aliases
  '((g . magit)
    (gl . magit-log)
    (gc . magit-clone)
    (d . dired)
    (o . find-file)
    (oo . find-file-other-window)
    (ll . (lambda () (eshell/ls '-lha)))
    (eshell/clear . eshell/clear-scrollback)))

(defun thanos/set-eshell-aliases (aliases)
  "Set ALIASES as eshell aliases."
  (mapc (lambda (alias)
	  (defalias (car alias) (cdr alias)))
	aliases))

(defun thanos/eshell-clear ()
  "Interactive call for clear-scrollback."
  (interactive)
  (eshell/clear-scrollback))

(use-package eshell
  :config
  (setf eshell-highlight-prompt t)
  (eshell-syntax-highlighting-global-mode 1)
  :bind (("C-c e" . eshell)
	 :map eshell-mode-map
	 ("C-l" . 'thanos/eshell-clear))
  :hook ((eshell-mode . (lambda ()
			  (thanos/set-eshell-aliases thanos/aliases)
			  (display-line-numbers-mode -1)))))

(use-package eat
  :after 'eshell
  :config
  (add-hook 'eshell-load-hook #'eat-eshell-mode)
  (setf eat-term-name "xterm-256color"))

(use-package eshell-git-prompt
  :config
  (defun eshell-git-prompt-multiline ()
  "Eshell Git prompt inspired by spaceship-prompt."
  (let (separator hr dir git git-dirty time sign command)
    (setq separator (with-face " | " 'eshell-git-prompt-multiline-secondary-face))
    (setq hr (with-face (concat "\n" (make-string (/ (window-total-width) 2) ?─) "\n") 'eshell-git-prompt-multiline-secondary-face))
    (setq dir
          (concat
           (with-face "" 'eshell-git-prompt-directory-face)
           (concat  (abbreviate-file-name (eshell/pwd)))))
    (setq git
          (concat (with-face "⎇" 'eshell-git-prompt-exit-success-face)
                  (concat (eshell-git-prompt--branch-name))))
    (setq git-dirty
          (when (eshell-git-prompt--branch-name)
            (if (eshell-git-prompt--collect-status)
                (with-face " ✎" 'eshell-git-prompt-modified-face)
              (with-face " ✔" 'eshell-git-prompt-exit-success-face))))
    (setq time (with-face (format-time-string "%I:%M:%S %p") 'eshell-git-prompt-multiline-secondary-face))
    (setq sign
          (if (= (user-uid) 0)
              (with-face "\n#" 'eshell-git-prompt-multiline-sign-face)
            (with-face "\nλ" 'eshell-git-prompt-multiline-sign-face)))
    (setq command (with-face " " 'eshell-git-prompt-multiline-command-face))

    ;; Build prompt
    (eshell-git-prompt---str-read-only
     (concat hr dir separator git git-dirty separator time sign command))))
  
  (eshell-git-prompt-use-theme 'multiline))

;; Chat
(use-package jabber
  :defer t
  :config
  (defun jabber-buffers-formats (&optional buffer-formats)
    "Return jabber BUFFER-FORMATS without the format specifiers.

By default, returns all jabber related buffers format."
    (let ((buffer-formats (or buffer-formats '(jabber-chat-buffer-format
					       jabber-browse-buffer-format
					       jabber-roster-buffer
					       jabber-groupchat-buffer-format
					       jabber-muc-private-buffer-format))))
      (cl-loop for var in buffer-formats
               for str = (symbol-value var)
               for formatted-str = (when (stringp str)
				     ;; Remove format specifier and
				     ;; the following 2 char, or 1 if
				     ;; there is not second.
                                     (replace-regexp-in-string "%.?" "" str))
               collect formatted-str)))

  (defun jabber-switch-to-buffer ()
    "Prompt the user to select a buffer whose name matches a list of strings."
    (interactive)
    (let* ((string-list (jabber-buffers-formats))
           (buffers-matching-strings
            (cl-loop for buf in (buffer-list)
                     when (cl-loop for str in string-list
                                   thereis (string-match-p str (buffer-name buf)))
                     collect (buffer-name buf))))
      (if buffers-matching-strings
	  (switch-to-buffer
	   (completing-read "Select jabber buffer: " buffers-matching-strings)))
      (error "No jabber buffer found")))
  :bind (:map jabber-global-keymap
	      ("C-b" . 'jabber-switch-to-buffer)))

(use-package erc
  :defer t
  :config
  (unless (expand-file-name "erc" user-emacs-directory)
    (make-directory (expand-file-name "erc" user-emacs-directory)))
  (setf erc-modules
	'(sasl netsplit fill button match track completion readonly
	       networks ring autojoin noncommands irccontrols move-to-prompt stamp
	       menu list log notifications)
	erc-log-channels-directory (expand-file-name "erc" user-emacs-directory))

  (defun thanos/erc-login ()
    "Login to libera.chat"
    (erc-tls :server "irc.libera.chat" :port 6697
	     :nick "thanosapollo"
	     :user "thanosapollo"
	     :password (password-store-get "liberachat/thanos_apollo")))
  :bind (("C-c E" . 'erc-libera)
	 :map erc-mode-map
	 ("C-c RET" . 'erc-cmd-QUERY)))

;; (use-package erc-image
;;   :ensure t
;;   :after erc
;;   :config
;;   (setq erc-image-inline-rescale 300)
;;   (add-to-list 'erc-modules 'image))

(use-package transmission
  :defer t)

(use-package sudo-edit
  :defer t
  :config
  (setf sudo-edit-local-method "sudo"))

(use-package dabbrev
  :defer t
  :config
  (setf dabbrev-ignored-buffer-regexps '("\\.\\(?:pdf\\|jpe?g\\|png\\)\\'")))

(use-package xref
  :defer t
  :config
  (setf xref-show-xrefs-function #'consult-xref
	xref-show-definitions-function #'consult-xref))

;; My packages
(when (or is-zeus is-hermes)
  (use-package yeetube
    :init (define-prefix-command 'thanos/yeetube-map)
    :straight (yeetube :local-repo "~/Dev/emacs-lisp/yeetube")
    :ensure t
    :config
    (setf yeetube-results-limit 20
	  yeetube-mpv-disable-video t
	  yeetube-display-thumbnails t
	  yeetube-play-function #'yeetube-mpv-play)

    (defun yeetube-download-videos-ffmpeg ()
      "Download videos using ffmpeg."
      (interactive)
      (let ((url "")
	    (name "")
	    (download-counter 1)
	    (stored-contents nil))
	;; Read links and names until "q" is entered
	(while (not (string= url "q"))
	  (setf url (read-string "Enter URL (q to quit): "))
	  (unless (string= url "q")
	    (setf name (read-string (format "Custom name (download counter: %d) " download-counter)))
	    (push (cons url name) stored-contents)
	    (setf download-counter (1+ download-counter))))
	;; Process the collected links and names
	(dolist (pair stored-contents)
	  (let ((url (car pair))
		(name (cdr pair)))
	    (async-shell-command
	     (format
	      "ffmpeg -protocol_whitelist file,crypto,data,https,tls,tcp -stats -i '%s' -codec copy '%s.mp4'"
	      url name))))))

    (defun yeetube-download-vimeo-videos ()
      "Download videos from vimeo services."
      (interactive)
      (let ((url "")
	    (name "")
	    (download-counter 1))
	(while (not (string= url "q"))
	  (setf url (read-string "Enter URL (q to quit): "))
	  (unless (string= url "q")
	    (setf name (read-string (format "Custom name (download counter: %d) " download-counter)))
	    (setf download-counter (1+ download-counter))
	    (call-process-shell-command
	     (format
	      "yt-dlp '%s' -o '%s'"
	      (replace-regexp-in-string "\\.json" ".m3u8" url) name)
	     nil 0)))))
    
    :bind (("C-c y" . 'thanos/yeetube-map)
	   :map thanos/yeetube-map
	   ("s" . 'yeetube-search)
	   ("b" . 'yeetube-play-saved-video)
	   ("d" . 'yeetube-download-videos)
	   ("C-d" . 'yeetube-download-vimeo-videos)
	   ("p" . 'yeetube-mpv-toggle-pause)
	   ("v" . 'yeetube-mpv-toggle-video)
	   ("V" . 'yeetube-mpv-toggle-no-video-flag)
	   ("C-p" . 'yeetube-mpv-toggle-video)
	   ("k" . 'yeetube-remove-saved-video)))

  (use-package gnosis
    :straight (gnosis :local-repo "~/Dev/emacs-lisp/gnosis")
    :ensure t
    :init (define-prefix-command 'thanos/gnosis-map)
    :config
    (setf gnosis-vc-auto-push t
	  gnosis-mcq-display-choices nil
	  gnosis-image-width (if is-zeus nil 150)
	  gnosis-image-height (if is-zeus nil 150))
    (gnosis-modeline-mode)
    :bind (("C-r" . thanos/gnosis-map)
	   :map thanos/gnosis-map
	   ("r" . 'gnosis-review)
	   ("a" . 'gnosis-add-note)
	   ("C-d" . 'gnosis-dashboard)
	   ("d" . 'gnosis-add-deck)
	   ("t" . 'gnosis-test-start)))

  ;; Sync gnosis on startup
  (gnosis-vc-pull)

  (use-package pcmpl-tailscale
    :straight (pcmpl-tailscale :local-repo "~/Dev/emacs-lisp/pcmpl-tailscale")
    :ensure nil))

;; Emacs dev

(use-package package-lint
  :defer t)

;; AI tools
(use-package gptel
  :defer t
  :config
  (setf gptel-api-key (password-store-get-field "openai/[email protected]" "api")
	gptel-default-mode 'org-mode
	gptel-directives '((default . "You are a large language model living in Emacs and a helpful assistant. Respond concisely.")
			   (programming . "You are a large language model and a careful programmer. Provide code and only code as output without any additional text, prompt or note.")
			   (epictetus . "You are Epictetus, the stoic philosopher from Nicopolis. Respond concisely as Epictetus.")
			   (med . "You are a medical professor within Emacs. Respond concisely.")
			   (code-review . "You are an expert programmer within Emacs reviewing code. Respond concisely")
			   (writer . "You are an expert writer and FOSS enthusiast. Improve only the article sections provided as a hacker, do not add extra paragraphs.")))

  (setq-default  gptel-model "dolphin-llama3:latest")
  
  (setq gptel-backend (gptel-make-ollama "Ollama"
			:host (if is-zeus "localhost:11434" "zeus:11434")
			:stream t
			:models '("dolphin-llama3:latest" "zephyr:latest" "llama3:latest"
				  "mistral:latest" "mixtral:8x22b" "neural-chat:latest"
				  "dolphin-mixtral:latest" "phi")))
  :bind (("C-c g" . 'gptel-send)
	 :map gptel-mode-map
	 ("C-c h" . 'gptel-menu)))


;; Password-store
(use-package password-store
  :init (define-prefix-command 'thanos/pass)
  :defer t
  :config
  (setf password-store-password-length (+ 20 (random 20)))

  :bind (("C-c p" . 'thanos/pass)
	 :map thanos/pass
	 ("i" . 'password-store-insert)
	 ("e" . 'password-store-edit)
	 ("g" . 'password-store-generate)
	 ("c" . 'password-store-copy)
	 ("s" . 'smtp-get-pass)))

(use-package package-lint
  :defer t)

(use-package consult-mu
  :straight (consult-mu :type git :host github :repo "armindarvish/consult-mu" :files (:defaults "extras/*.el"))
  :after (mu4e consult)
  :bind (:map mu4e-main-mode-map
	      ("M-s" . 'consult-mu)
	      :map mu4e-view-mode-map
	      ("M-s" . 'consult-mu)
	      :map mu4e-headers-mode-map
	      ("M-s" . 'consult-mu)))

(use-package yaml-mode
  :defer t)

;(load-file "~/Dev/guile/guix/etc/copyright.el")

(setq copyright-names-regexp
      (format "%s <%s>" user-full-name user-mail-address))

(when (or is-zeus is-hermes)
  (use-package mu4e
    :straight nil
    :ensure nil  
    :config
    (require 'server)
    (require 'mu4e)

    (setf mu4e-get-mail-command "mbsync -a")

    ;; (when (and is-zeus (server-running-p))
    ;;   (setf mu4e-update-interval (* 10 60)))

    (defun set-mu4e-context (context-name full-name mail-address signature)
      "Return a mu4e context named CONTEXT-NAME with :match-func matching
    folder name CONTEXT-NAME in Maildir. The context's `user-mail-address',
    `user-full-name' and `mu4e-compose-signature'`smtpmail-smpt-server' is set to MAIL-ADDRESS
    FULL-NAME SIGNATURE and SERVER respectively.
    Special folders are set to context specific folders."
      (let ((dir-name (concat "/" context-name)))
	(make-mu4e-context
	 :name context-name
	 ;; we match based on the maildir of the message
	 :match-func
	 `(lambda (msg)
	    (when msg
	      (string-match-p
	       ,(concat "^" dir-name)
	       (mu4e-message-field msg :maildir))))
	 :vars
	 `((user-mail-address    . ,mail-address)
	   (user-full-name       . ,full-name)
	   (mu4e-sent-folder     . ,(concat dir-name "/Sent"))
	   (mu4e-drafts-folder   . ,(concat dir-name "/Drafts"))
	   (mu4e-trash-folder    . ,(concat dir-name "/Trash"))
	   (mu4e-trash-folder    . ,(concat dir-name "/Starred"))
	   (mu4e-refile-folder   . ,(concat dir-name "/Archive"))
	   (mu4e-compose-signature . ,signature)))))

    ;;Fixing duplicate UID errors when using mbsync and mu4e
    (setf mu4e-change-filenames-when-moving t
	  mu4e-maildir-shortcuts
	  '(("/Fastmail/Inbox" . ?i)
	    ("/Drafts" . ?d)
	    ("/Sent" . ?s)
	    ("/Fastmail/Emacs/dev" . ?e)
	    ("/MUSofia/[Gmail]/All Mail" . ?u)
	    ("/Fastmail/Gentoo" . ?g)))

    (setf mu4e-contexts
	  (list
	   (make-mu4e-context
	    :name "Public"
	    :match-func
	    (lambda (msg)
	      (when msg
		(string-prefix-p "/Fastmail" (mu4e-message-field msg :maildir))))
	    :vars '((user-mail-address . "[email protected]")
		    (user-full-name    . "Thanos Apollo")
		    (smtpmail-smtp-server . "smtp.fastmail.com")
		    (smtpmail-smtp-service . 465)
		    (smtpmail-stream-type . ssl)
		    (mu4e-drafts-folder  . "/Drafts")
		    (mu4e-sent-folder  . "/Sent")
		    (mu4e-refile-folder  . "/Archive")
		    (mu4e-trash-folder  . "/Trash")))
	   (make-mu4e-context
	    :name "MUSofia"
	    :match-func
	    (lambda (msg)
	      (when msg
		(string-prefix-p "/MUSofia" (mu4e-message-field msg :maildir))))
	    :vars '((user-mail-address . "[email protected]")
		    (user-full-name    . "Thanos Apollo")
		    (smtpmail-smtp-server . "smtp.gmail.com")
		    (smtpmail-smtp-service . 465)
		    (smtpmail-stream-type . ssl)))))

    (setf message-send-mail-function 'smtpmail-send-it
	  mu4e-compose-signature "\nThanos Apollo\n \nhttps://thanosapollo.org\n
62B7 58D0 F671 9938 BC09 CECA 339F 736C 3A72 0928\n"
	  mu4e-compose-context-policy 'ask)

    (setf mu4e-view-actions
	  (delete-dups
	   (append
	    '(("gapply git patches" . mu4e-action-git-apply-patch)
	      ("mgit am patch" . mu4e-action-git-apply-mbox)
	      ("bb4 am patch" . mu4e-action-git-apply-b4)
	      ("ssetup reword list with b4" . mu4e-action-setup-reword-b4)
	      ("crun checkpatch script" . my-mu4e-action-run-check-patch)
	      ("MCheck if merged" . my-mu4e-action-check-if-merged)))))

    (define-minor-mode mu4e-mode
      "Fix for mu4e mode guix (undefined)."
      :global t
      :group 'mu4e
      :lighter nil
      (message "mu4e mode"))
    
    :bind (("C-x m" . 'mu4e))
    :hook
    ;; Sign messages
    ((message-send . 'mml-secure-message-sign-pgpmime)
     ;; Disable line numbers & autosave
     (mu4e-main-mode . (lambda () (display-line-numbers-mode -1) (auto-save-mode -1))))))

(use-package yasnippet
  :ensure nil
  :config
  (when is-zeus (add-to-list 'yas-snippet-dirs "~/Dev/guile/guix/etc/snippets/yas")))

(use-package debbugs
  :ensure nil)

;; (use-package guix
;;   :straight nil
;;   :config
;;   (defvar guix-home-file "~/dotfiles/guix/home.scm")

;;   (defun guix-home-reconfigure (&optional home-config)
;;     (interactive)
;;     (let ((home-config (or home-config guix-home-file)))
;;       (async-shell-command (format "guix home reconfigure %s" home-config)))))


;;; Random commands ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;

;; Virtual machines
(defvar vm-directory "~/Virtual/")

(defun vm-create-image ()
  "Create qcow2 image."
  (interactive)
  (let ((name (format "%s%s.qcow2" vm-directory (read-string "Name: ")))
	(size (format "%s" (read-string "Size(G): "))))
    (shell-command
     (format "qemu-img create -f qcow2 %s %sG" name size))))


(defun vm-run ()
  "Spawn Virtual Machine."
  (interactive)
  (let ((memory (format "%sG" (read-string "Memory(G): ")))
	(cores (read-string "Cores: "))
	(image (read-file-name "Image: " vm-directory))
	(iso (if (y-or-n-p "Load iso? ")
		 (read-file-name "ISO: ")
	       nil)))
    (async-shell-command
     (format "qemu-system-x86_64 -enable-kvm -m %s -smp %s -hda %s -vga virtio -device virtio-serial-pci -netdev user,id=vmnic,hostfwd=tcp::2222-:22 -device e1000,netdev=vmnic %s"
	     memory cores image (if iso (concat "-cdrom " iso) "")))))

;; Misc Functions ;;
;;;;;;;;;;;;;;;;;;;;

(defun thanos/run-in-background (command)
  "Run COMMAND in the background."
  (let ((command-parts (split-string command "[ ]+")))
    (apply #'call-process `(,(car command-parts) nil 0 nil ,@(cdr command-parts)))))

(defmacro thanos/make-frame (name &rest body)
    "Create temporary frame as NAME.

Create a temporary frame to execute BODY, which will then be deleted."
    `(unwind-protect
	 (with-selected-frame
	     (make-frame '((name . ,name)
			   (fullscreen . 0)
			   (undecorated . t)
			   (minibuffer . only)
			   (width . 70)
			   (height . 15)))
	   ,@body
	   (delete-frame))))

(defun create-text-scratch ()
  "Create a scratch buffer."
  (interactive)
  (switch-to-buffer (get-buffer-create "*Text Scratch*"))
  (org-mode))

(defun create-scratch ()
  "Create scratch buffer."
  (interactive)
  (switch-to-buffer (get-buffer-create "*scratch*"))
  (emacs-lisp-mode))

(defvar-keymap thanos/create-map
  :doc "Create custom buffers"
  "t" #'create-text-scratch
  "e" #'create-scratch)

;; Theming

(defvar wallpapers-dir "~/wallpapers/"
  "Wallpaper directory.")

(defvar wallpaper-current nil
  "Current wallpaper.")

(defun thanos/load-theme (&optional theme)
  "Disable current theme and load a new THEME."
  (interactive)
  (let ((theme (or theme (intern (completing-read "Theme: " (custom-available-themes))))))
    (disable-theme (car custom-enabled-themes))
    (load-theme theme t)))

(defun thanos/wallpaper-set (image)
  "Set IMAGE as wallpaper, using feh."
  (let ((command "swaybg -o '*' -i"))
    ;; Kill previous swaybg
    (call-process-shell-command "kill -15 $(pgrep swaybg | tail -n 1)")
    ;; Set wallpaper
    (call-process-shell-command
     (format "%s %s -m stretch" command image)
     nil 0)))

(defun thanos/wallpaper-random ()
  "Set random wallpaper."
  (interactive)
  (let ((wallpapers (directory-files "~/wallpapers" nil "^[^.].*")))
    (thanos/wallpaper-set (format "%s%s" wallpapers-dir (nth (random (length wallpapers)) wallpapers)))))

(defun thanos/wallpaper-select ()
  "Set wallpaper."
  (interactive)
  (let ((wallpaper (format "%s%s" wallpapers-dir
			   (completing-read "Choose wallpaper: "
					    (directory-files wallpapers-dir nil "^[^.].*")))))
    (thanos/wallpaper-set wallpaper)
    (setf wallpaper-current wallpaper)))

(defun thanos/wallpaper-watcher (_symbol new-value _where _environment)
  "Watch for wallpaper changes."
  (with-temp-buffer (find-file (expand-file-name "wallpaper" user-emacs-directory))
		    (erase-buffer)
		    (setf wallpaper-current new-value)
		    (insert (pp-to-string wallpaper-current))
		    (save-buffer)
		    (kill-buffer)))

(defun thanos/load-wallpaper ()
  "Load saved wallpaper."
  (let ((wallpaper-path (expand-file-name "wallpaper" user-emacs-directory)))
    (if (file-exists-p wallpaper-path)
	(with-temp-buffer
	  (insert-file-contents wallpaper-path)
	  (goto-char (point-min))
	  (let ((contents (read (current-buffer))))
	    (setf wallpaper-current contents)))
      (write-region "nil" nil wallpaper-path))))

(defun thanos/wallpaper-startup (&optional image)
  "Set wallpaper IMAGE on startup."
  (thanos/load-wallpaper)
  (let ((image (or image wallpaper-current)))
    (thanos/wallpaper-set image)))

(add-variable-watcher 'wallpaper-current #'thanos/wallpaper-watcher)

;; Set wallpaper
(thanos/wallpaper-startup)

(defvar-keymap thanos/applications-map
  :doc "Thanos commonly used programs"
  "t" #'thanos/load-theme
  "w" #'thanos/wallpaper-select
  "C-c" thanos/create-map)

(define-key global-map (kbd "C-c a") thanos/applications-map)

(defun thanos/iimage-mode-buffer (arg)
  "Display images if ARG is non-nil, undisplay them otherwise."
  (let ((image-path (cons default-directory iimage-mode-image-search-path))
        (edges (window-inside-pixel-edges (get-buffer-window)))
	file)
    (with-silent-modifications
      (save-excursion
        (dolist (pair iimage-mode-image-regex-alist)
          (goto-char (point-min))
          (while (re-search-forward (car pair) nil t)
            (when (and (setq file (match-string (cdr pair)))
                       (setq file (locate-file file image-path)))
              (if arg
                  (add-text-properties
                   (match-beginning 0) (match-end 0)
                   `(display
                     ,(create-image file nil nil
                                    :max-width 120
				    :max-height 120)
                     keymap ,image-map
                     modification-hooks
                     (iimage-modification-hook)))
                (remove-list-of-text-properties
                 (match-beginning 0) (match-end 0)
                 '(display modification-hooks))))))))))

(define-minor-mode thanos/iimage-mode nil
  :group 'iimage :lighter " iImg"
  (thanos/iimage-mode-buffer thanos/iimage-mode))

;; Password

(defun thanos/pass-autotype (&optional entry)
  "Autotype password ENTRY."
  (let* ((entry (or entry (completing-read "Select entry: " (password-store-list))))
	 (user (password-store-get-field entry "user"))
	 (pass (password-store-get entry)))
    (start-process-shell-command
     "wtype" nil
     (format "sleep 0.3 && wtype %s -P tab %s"
	     (shell-quote-argument (if user user "thanosapollo"))
	     (shell-quote-argument pass)))))

(defun thanos/pass-launcher ()
  "Launch Emacs as a front-end for pass."
  (interactive)
  (thanos/make-frame
   "thanos/pass-launcher"
   (let* ((choice (completing-read "Choose an action: "
				   '("AUTO" "COPY PASS" "EDIT" "GENERATE")))
	  (action (pcase choice
		    ("AUTO" #'thanos/pass-autotype)
		    ("COPY PASS" #'password-store-copy)
		    ("EDIT" #'password-store-edit)
		    ("GENERATE" #'password-store-generate))))
     (funcall action (completing-read "Search: " (password-store-list))))))

(defun smtp-get-pass ()
  "Get password for smtp."
  (interactive)
  (password-store-copy-field "fastmail.com/[email protected]" "smtp"))

(defun thanos/0x0-upload (&optional file)
  "Upload FILE to 0x0.st"
  (interactive)
  (let ((file (or file (dired-get-filename))))
    (async-shell-command (format "curl -F'file=@%s' https://0x0.st" file))))

;;; init.el ends here