-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.el
1377 lines (1152 loc) · 43.5 KB
/
init.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; MyEmacsConfig --- a minimal cross platform config
;;
;; -*- lexical-binding: t -*-
;;
;;; Commentary:
;; It is meant to provide a decent working environment
;; that's fairly easy to manage.
;; A lot of this is based on my own personal config and
;; the excellent resource of Emacs ONBOARD
;; URL: https://github.com/monkeyjunglejuice/emacs.onboard
;; In general, I try to use as many of the builtin features available
;; and tend to only use external package when it's much more convenient.
;;; Code:
(require 'server)
(defvar init-script-initial-clients nil
"Connected clients when init script was run.")
(setq init-script-initial-clients server-clients)
(unless init-script-initial-clients
(server-start))
;; **********************
;;; * Package Management *
;; **********************
;; Ensure that Emacs can verify SSL/TLS certificates
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
;; Straight
(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
(concat
"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))
(setq package-enable-at-startup nil)
(setq straight-use-package-by-default t)
;; Package archives
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(require 'use-package)
(setq use-package-always-ensure t)
;; Natively compile packages at first use or immediately after installation?
(setq package-native-compile t)
;; File modes ;;
;; No config needed - just needed for the file types.
(use-package csv-mode)
(use-package yaml-mode)
(use-package toml-mode)
(use-package json-mode)
(use-package markdown-mode)
;; ********************
;;; * Usage Statistics *
;; ********************
;; File: elisp.info, Node: Garbage Collection, Up: GNU Emacs Internals
;; Set a high value of 1 GB to prevent frequent garbage collections
;; during initialization.
(setq gc-cons-threshold #x40000000) ; default threshold is 800 KB
;; Prevent longer GC pauses and experience less mini-interruptions.
;; When idle for 15 sec, run the GC no matter what.
;; This hack was stolen from <https://akrl.sdf.org/>
(defmacro my-time (&rest body)
"Measure and return the time it takes evaluating BODY."
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(defvar my-gc-timer nil
"Timer object for garbage collection monitoring.
The timer can be canceled with `my-cancel-gc-timer'.")
(defun my-start-gc-timer ()
"Start the garbage collection timer."
(interactive)
(setq my-gc-timer
(run-with-idle-timer 15 t
(lambda ()
(my-time (garbage-collect))))))
(defun my-cancel-gc-timer ()
"Cancel the garbage collection timer."
(interactive)
(when (timerp my-gc-timer)
(cancel-timer my-gc-timer)
(setq my-gc-timer nil)))
;; Start the GC Timer
(my-start-gc-timer)
;; Show a message when garbage collection happens? Useful while tuning the GC
(setq garbage-collection-messages nil)
;; Diagnostics
(add-hook 'emacs-startup-hook
(lambda ()
(message "Emacs started in %s with %d garbage collections."
(format "%.3f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done)))
;; WakaTime
;; Requires wakatime-cli
(load (concat user-emacs-directory ".waka.el"))
(use-package wakatime-mode
:straight nil)
(global-wakatime-mode)
;; Straight
(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))
(setq package-enable-at-startup nil)
(setq straight-use-package-by-default t)
(require 'use-package)
(setq use-package-always-ensure t)
;; Natively compile packages at first use or immediately after installation?
(setq package-native-compile t)
;; Magit ;;
(use-package magit
:commands (magit-status magit-get-current-branch)
:custom
(magit-display-buffer-function #'magit-display-buffer-same-window-except-diff-v1))
(setenv "GIT_ASKPASS" "git-gui--askpass")
(global-set-key (kbd "C-c g g") #'magit)
;; Forge ;;
(use-package forge
:after magit)
;; Git Time Machine ;;
(use-package git-timemachine)
(setq git-timemachine-abbreviation-length 6)
(global-set-key (kbd "C-c g t") #'git-timemachine)
;; File modes ;;
;; No config needed - just needed for the file types.
(use-package csv-mode)
(use-package yaml-mode)
(use-package toml-mode)
(use-package json-mode)
(use-package markdown-mode)
;; **********
;;; * System *
;; **********
;; Prevent stale elisp bytecode from shadowing more up-to-date source files
(setq load-prefer-newer t)
;; Increase warning threshold
(setq large-file-warning-threshold (* 64 1000000))
;; Set undo limit to 64 MB
(setq undo-outer-limit (* 64 1000000))
(global-set-key (kbd "M-r") #'undo-redo)
(global-set-key (kbd "M-u") #'undo-only)
;; Increase the amount of data which Emacs reads from subprocesses
(setq read-process-output-max (* 1024 1024)) ; 1 MB
;; Sets the default directory
(setq default-directory "~/")
;; Info
(defvar info-custom-dir (expand-file-name "~/.emacs.d/info/")
"Location of custom info directory.")
(add-to-list 'Info-directory-list info-custom-dir)
(when (eq system-type 'windows-nt)
(add-to-list 'Info-directory-list
(expand-file-name "~/scoop/apps/emacs/current/share/info")))
(defun info-custom-manuals (manual)
(info (concat info-custom-dir manual ".info")))
(defun info-custom-python ()
"Launch python info file."
(interactive)
(info-custom-manuals "python"))
(global-set-key (kbd "C-c h p") #'info-custom-python)
;; Sets the auth source (requires gpg!)
(setq auth-sources '("~/.authinfo.gpg")
epa-gpg-program "gpg" ;; Ensures GPG program is GPG
epa-pinentry-mode 'loopback) ;; Needed if GPG requires a password for decrypting keys
;; Saves session
;; Works with daemons but not remotely running Emacs sessions.
(desktop-save-mode 1)
(add-hook 'server-after-make-frame-hook #'desktop-read)
(setq desktop-load-locked-desktop 'check-pid)
;; ******
;;; * UI *
;; ******
;; Help
(setq help-at-pt-timer-delay 0
help-at-pt-display-when-idle "always")
;; Tooltips
(setq tooltip-delay 0
tooltip-short-delay 0
use-system-tooltips nil)
;; Fireplace - a mission critical package;;
(use-package fireplace)
;; Dashboard ;;
(use-package page-break-lines)
(use-package dashboard
:config
(require 'page-break-lines))
;; Which-Key ;;
(use-package which-key
:init (which-key-mode)
:diminish which-key-mode
:config
(setq which-key-idle-delay 0.1))
;; Frame
(add-to-list 'default-frame-alist '(fullscreen . maximized))
(setq server-after-make-frame-hook #'dashboard-open)
;; Basic
(setq inhibit-startup-message t
visible-bell t
confirm-kill-emacs 'yes-or-no-p
global-tab-line-mode t
truncate-lines t
x-stretch-cursor t
use-dialog-box nil)
(scroll-bar-mode -1) ; Disable visible scrollbar
(tool-bar-mode -1) ; Disable the toolbar
(tooltip-mode -1) ; Disable tooltips
(set-fringe-mode 0) ; Removes fringes
(menu-bar-mode -1) ; Disable the menu bar
(global-visual-line-mode t) ; Enables visual lines
(blink-cursor-mode t) ; Blinks the cursor
(global-hl-line-mode t) ; Hightlights current line
;; Modeline
(setq mode-line-compact t ; Compresses repeating spaces to a single space
display-time-24hr-format t
display-battery-mode t)
(display-time-mode 1) ; Displays the time.
(display-battery-mode 1) ; Displays the battery.
(size-indication-mode 1) ; Show the buffer size in the modeline
(column-number-mode 1) ; Show column number along with line number in modeline
;; Tabs
(setq tab-bar-position t
tab-bar-show 1
tab-bar-close-button-show nil
tab-bar-format '(tab-bar-format-history
tab-bar-format-tabs
tab-bar-separator)
tab-bar-new-tab-choice #'scratch-buffer
tab-bar-new-tab-to 'rightmost)
;; Programming UI
(add-hook 'prog-mode-hook #'whitespace-mode)
(setq display-raw-bytes-as-hex t)
;; Line numbers
(column-number-mode)
(global-display-line-numbers-mode t)
(setq display-line-numbers-type t)
;; Use spaces not tabs - this is the hill I will die on.
(setq-default c-basic-offset 4
tab-width 4
indent-tabs-mode nil
tab-always-indent t)
;; Electric indents reidents text lines on-the-fly.
;; I do not like this.
(electric-indent-mode 0)
;; Theme
(defvar me/dark-theme 'modus-vivendi "Default dark theme.")
(defvar me/light-theme 'modus-operandi "Default light theme.")
(defun load-theme-light ()
"Load the light theme which is set by me/light-theme."
(interactive)
(load-theme me/light-theme t))
(defun load-theme-dark ()
"Load the dark theme which is set by me/dark-theme."
(interactive)
(load-theme me/dark-theme t))
(load-theme me/dark-theme t)
;; Font - only use Cascadia on windows.
(if (eq system-type 'windows-nt)
(add-to-list 'default-frame-alist
'(font . "Cascadia Mono-14"))
(set-face-attribute 'default nil
:font "FreeMono-13"
:width 'expanded
:weight 'medium))
;; Highlighting changes
(setq highlight-changes-mode t)
(global-set-key (kbd "C-c c c") #'highlight-changes-mode)
(global-set-key (kbd "C-c c h") #'highlight-changes-remove-highlight)
(global-set-key (kbd "C-c c v") #'highlight-changes-visible-mode)
(global-set-key (kbd "C-c c n") #'highlight-changes-next-change)
(global-set-key (kbd "C-c c p") #'highlight-changes-previous-change)
(global-set-key (kbd "C-c c r") #'highlight-changes-rotate-faces)
(global-set-key (kbd "C-c c f") #'highlight-compare-with-file)
(global-set-key (kbd "C-c c b") #'highlight-compare-buffers)
;; *****************
;;; * Basic Editing *
;; *****************
(save-place-mode 1)
(setq save-interprogram-paste-before-kill t)
(setq yank-pop-change-selection t)
(global-set-key (kbd "C-x j") #'join-line)
(global-set-key (kbd "C-c r") #'replace-string)
(global-set-key (kbd "C-c q") #'query-replace)
(global-set-key (kbd "C-/") #'undo)
;; In general, I prefer to go forward to the beginning of a word
;; not the end, but I've provided a shortcut for both a bound
;; forward to the beginning of the word to M-f
(global-set-key (kbd "C-M-f") #'forward-word)
(global-set-key (kbd "M-f") #'forward-to-word)
;; Handy for scrolling through buffers.
;; <SPC> :: scroll forward one windowful.
;; S-<SPC> :: scroll backward one windowful.
;; s :: start incremental search.
;; q :: Quit and go back to buffer & position before view-mode.
;; e :: Quit and stay in the same buffer & position.
(global-set-key (kbd "C-c v") #'view-mode)
;; Format whitespace visualisation
(setq whitespace-style '(face tabs spaces trailing lines space-before-tab
newline indentation empty space-after-tab
space-mark tab-mark missing-newline-at-eof))
;; Count the words
(global-set-key (kbd "C-c b c") #'count-words)
;; ********************
;;; * Advanced Editing *
;; ********************
;; Completion
(require 'icomplete)
(setq icomplete-mode t
icomplete-in-buffer t
icomplete-compute-delay 0.01
icomplete-delay-completions-threshold 10000
icomplete-show-matches-on-no-input t
icomplete-hide-common-prefix nil
read-buffer-completion-ignore-case t
completion-ignore-case t
completion-auto-help nil
completion-styles '(basic partial-completion initials substring)) ;; Flex is too aggressive.
;; Used to use fido-vertical but that has difficulty when not selecting an item
;; from the completion list. That is, when rather than selecting "fo" if "foo"
;; is present "foo" will always be selected.
;; Hence I switched to icomplete.
;; The vertical completion takes up more screen real estate so I ignored that.
;; For some reason icomplete doesn't always load after initialisation
(icomplete-mode)
;; Use the default `completion--in-region' function.
(setq completion-in-region-function
(lambda (&rest args)
(apply #'completion--in-region args)))
;; Enables advanced editing modes
(put 'set-goal-column 'disabled nil) ;; Enables setting a goal column
(put 'narrow-to-region 'disabled nil) ;; Enables narrowing
(put 'upcase-region 'disabled nil) ;; Enables up case a regoin
(put 'downcase-region 'disabled nil) ;; Enables down case a regoin
;; Multiple cursors ;;
(use-package multiple-cursors
:straight nil
:config
(global-set-key (kbd "C->") #'mc/mark-next-like-this)
(global-set-key (kbd "C-<") #'mc/mark-previous-like-this)
(global-set-key (kbd "C-x M-.") #'mark-pop)
(global-set-key (kbd "C-x C-.") #'mc/edit-lines)
(define-key mc/keymap (kbd "<return>") nil))
;; Expand region
(use-package expand-region
:bind ("C-=" . er/expand-region))
;; iSpell ;;
(global-set-key (kbd "C-c d s") 'ispell)
(global-set-key (kbd "C-c d w") 'ispell-word)
(global-set-key (kbd "C-c d c") 'ispell-comments-and-strings)
;; Multi-file operations
(global-set-key (kbd "C-c f c") #'fileloop-continue)
;; *************************
;;; * Programming Languages *
;; *************************
;;;; The Grand Unified Debugger
(global-set-key (kbd "C-x C-a i") #'gud-goto-info)
(global-set-key (kbd "C-x C-a t") #'gud-tooltip-mode)
;;;; Python
(defun python-imenu-use-flat-index
()
(setq imenu-create-index-function
#'python-imenu-create-flat-index))
(add-hook 'python-mode-hook
#'python-imenu-use-flat-index)
(global-set-key (kbd "C-c g d") #'pdb)
(setq gud-pdb-command-name "poetry run python -m pdb")
(setq python-shell-interpreter "ipython")
;;;; Perl
(add-to-list 'major-mode-remap-alist '(perl-mode . cperl-mode))
(setq cperl-invalid-face nil)
(setq cperl-electric-keywords t) ;; expands for keywords such as
;; foreach, while, etc...
;; Electric parentheses
(setq cperl-electric-parens t)
;; Help on idle after 1s
(setq cperl-lazy-help-time 1)
;;;; Latex
(global-prettify-symbols-mode)
(use-package tex
;; In general I prefer the AUCTeX modes over their builtin counter parts
;; That is, LaTeX-mode over latex-mode etc.
:straight nil
:ensure auctex
:config
(add-hook 'LaTeX-mode-hook #'TeX-fold-mode)
(add-hook 'LaTeX-mode-hook
(lambda ()
(setq flymake-compiler "pdflatex")
(setq flymake-args '("-interaction=nonstopmode" "%f"))))
(add-to-list 'auto-mode-alist '("\\.TeX$" . LaTeX-mode))
(setq TeX-auto-save t
TeX-parse-self t
TeX-process-asynchronous t
TeX-check-TeX nil
TeX-electric-sub-and-superscript t
font-latex-fontify-sectioning 'color ;; Disable variable font for sections
TeX-engine 'default))
;; Useful (AUC)TeX commands
;;
;; C-c C-s :: Insert section.
;; C-c C-e :: Insert environment.
;; C-c C-m :: Insert macro.
;; C-c C-a :: Compile all.
;; C-c C-c :: Compile.
;; C-c C-b :: Compile buffer.
;; C-c C-z :: Compile section.
;; C-c C-r :: Compile region.
;; C-c C-f C-b :: Bold.
;; C-c C-f C-i :: Italics.
;; C-c C-f C-e :: Emphasized.
;; C-c C-o C-f :: Fold mode.
;; C-c ~ :: Math mode.
;; C-x * e :: calc-embedded.
;; C-c C-p C-d :: Preview document.
;; C-c C-p C-p :: Preview at point.
;;;; Emacs Speaks Statistics
(use-package ess
:config (require 'ess-r-mode))
;; (well also Python but that's not important right now)
(setq ess-ask-for-ess-directory t) ;; I actually like this feature!
(setq ess-local-process-name "R")
(setq ess-eval-visibly-p 'nowait) ;; No waiting whilst ESS is evaluating
(define-key ess-r-mode-map (kbd "M-?") nil) ;; unbinds M-?
;; **************
;;; * Navigation *
;; **************
;; Outline
;; Mainly used text based files.
(with-eval-after-load "outline"
(define-key outline-minor-mode-map (kbd "C-c o")
(lookup-key outline-minor-mode-map (kbd "C-c @"))))
(add-hook 'text-mode-hook #'outline-minor-mode)
;; Hide-show
;; Mainly used programming based files.
(define-key hs-minor-mode-map (kbd "C-c o")
(lookup-key hs-minor-mode-map (kbd "C-c @")))
(setq hs-isearch-open t) ;; Unhides comments and code when using isearch
(add-hook 'prog-mode-hook #'hs-minor-mode)
;; Semantic mode
;; Language aware editing commands for:
;; C, C++, HTML,Java, Javascript, Make, Python, Scheme, SRecode, and Texinfo
(require 'cedet)
(setq semantic-default-submodes
'(;; Perform semantic actions during idle time
global-semantic-idle-scheduler-mode
;; Use a database of parsed tags
global-semanticdb-minor-mode
;; Decorate buffers with additional semantic information
global-semantic-decoration-mode
;; Highlight the name of the function you're currently in
global-semantic-highlight-func-mode
;; show the name of the function at the top in a sticky
global-semantic-stickyfunc-mode
;; Generate a summary of the current tag when idle
global-semantic-idle-summary-mode
;; Show a breadcrumb of location during idle time
global-semantic-idle-breadcrumbs-mode
;; Switch to recently changed tags with `semantic-mrub-switch-tags',
;; or `C-x B'
global-semantic-mru-bookmark-mode))
(add-hook 'emacs-lisp-mode-hook #'semantic-mode)
(add-hook 'python-mode-hook #'semantic-mode)
(add-hook 'html-mode-hook #'semantic-mode)
;; Search
(setq isearch-repeat-on-direction-change t
isearch-wrap-pause 'no)
(global-set-key (kbd "C-s") #'isearch-forward)
(global-set-key (kbd "C-r") #'isearch-backward)
;; imenu
(defun me/imenu-function ()
"Function imenu."
(interactive)
(let ((unread-command-events (listify-key-sequence "Function\n") ))
(call-interactively 'imenu)))
(defun me/imenu-variable ()
"Variable imenu."
(interactive)
(let ((unread-command-events (listify-key-sequence "Variable\n") ))
(call-interactively 'imenu)))
(defun me/imenu-class ()
"Class imenu."
(interactive)
(let ((unread-command-events (listify-key-sequence "Class\n") ))
(call-interactively 'imenu)))
(defun me/imenu-method ()
"Method imenu."
(interactive)
(let ((unread-command-events (listify-key-sequence "Method\n") ))
(call-interactively 'imenu)))
(add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "M-g f") #'me/imenu-function)))
(add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "M-g v") #'me/imenu-variable)))
(add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "M-g c") #'me/imenu-class)))
(add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "M-g m") #'me/imenu-method)))
;; File navigation
(global-set-key (kbd "C-c f ,") 'find-file-other-window)
(global-set-key (kbd "C-c f v") 'view-file)
(recentf-mode 1)
(global-set-key (kbd "C-c f r") 'recentf-open-files)
;; Projects
(defun edit-projects ()
"Edit the list of projects."
(interactive)
(find-file project-list-file))
(global-set-key (kbd "C-x p a") #'edit-projects)
(global-set-key (kbd "C-x p s") 'project-search)
;; Buffers
(global-set-key (kbd "C-x C-b") #'ibuffer)
(global-set-key (kbd "C-c b k") #'kill-buffer-and-window)
(global-set-key (kbd "C-c b ,") #'switch-to-buffer-other-window)
(global-set-key (kbd "C-c b v") #'view-buffer-other-window)
(global-set-key (kbd "M-[") #'previous-buffer)
(global-set-key (kbd "M-]") #'next-buffer)
(global-set-key (kbd "C-c b a") #'append-to-buffer)
(defun kill-this-buffer-reliably () (interactive) (kill-buffer (current-buffer)))
(global-set-key (kbd "C-x k") #'kill-this-buffer-reliably)
;; Scratch buffer
(setq initial-major-mode #'org-mode)
(setq initial-scratch-message "")
(global-set-key (kbd "C-c b s") #'scratch-buffer)
(global-auto-revert-mode 1)
(setq midnight-mode t)
(midnight-delay-set 'midnight-delay "02:00am")
;; *********
;;; * Dired *
;; *********
;; Loads useful functions.
;; Came for dired-do-find-marked-files
;; Stayed for the file omition, virtual-dired buffers and dired-x-find-file
(with-eval-after-load 'dired
(setq dired-x-hands-off-my-keys nil)
(require 'dired-x)
(dired-x-bind-find-file)
(add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1))))
(setq dired-listing-switches "-alh")
(setq dired-kill-when-opening-new-dired-buffer t)
(defun dired-sort-criteria (criteria)
"Sort-dired by different CRITERIA by Robert Gloeckner."
(interactive
(list
(or (completing-read "criteria [name]: "
'("size(S)" "extension(X)" "creation-time(ct)"
"access-time(ut)" "time(t)" "name()"))
"")))
(string-match ".*(\\(.*\\))" criteria)
(dired-sort-other
(concat dired-listing-switches
(match-string 1 criteria))))
(setq global-auto-revert-non-file-buffers t)
(global-set-key (kbd "C-c d d") 'dired-default-directory-on-left)
(global-set-key (kbd "C-c d p") 'dired-at-point)
(global-set-key (kbd "C-c d f") 'find-dired)
(global-set-key (kbd "C-c d n") 'find-name-dired)
(global-set-key (kbd "C-c d w") 'wdired-change-to-wdired-mode)
(global-set-key (kbd "C-c d ,") 'dired-other-window)
;; ***************
;;; * Development *
;; ***************
;; Python
(require 'flymake)
(add-hook 'prog-mode-hook #'flymake-mode)
(setq flymake-start-on-flymake-mode t)
(setq python-flymake-command '("ruff" "check" "--output-format"
"concise" "--quiet"
"--exit-zero" "--select" "ALL"
"--ignore" "D407"
"--stdin-filename=stdin" "-"))
(define-key flymake-mode-map (kbd "M-n") 'flymake-goto-next-error)
(define-key flymake-mode-map (kbd "M-p") 'flymake-goto-prev-error)
(define-key flymake-mode-map (kbd "C-c l b") 'flymake-show-buffer-diagnostics)
(define-key flymake-mode-map (kbd "C-c l p") 'flymake-show-project-diagnostics)
;; *********
;;; * Tools *
;; *********
;; Shell/Eshell
(require 'em-banner)
(setq eshell-banner-message "")
(require 'em-dirs)
(setq eshell-list-files-after-cd t)
(global-set-key (kbd "C-c e") #'eshell)
(global-set-key (kbd "C-c s") #'shell)
;; Start an LLM-chat shell
(defvar lchat-model "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
"Default model for the lchat shell.")
(defun lchat ()
"Start an LLM chat shell."
(interactive)
(let ((eshell-buffer-name "lchat"))
(if (get-buffer eshell-buffer-name)
(switch-to-buffer eshell-buffer-name)
(progn
(eshell)
(insert (format "lchat -m %s" lchat-model))
(eshell-send-input)))))
(global-set-key (kbd "C-c m l") #'lchat)
;; Visit init file
(defun my-visit-user-init-file ()
"Visit the init file."
(interactive)
(find-file user-init-file))
(global-set-key (kbd "C-c f i") #'my-visit-user-init-file)
;; Calculator
(defun calc-or-quick-calc (arg)
"Run calc or quick calc if ARG."
(interactive "P")
(if arg
(call-interactively #'quick-calc)
(call-interactively #'calc)))
(global-set-key (kbd "C-x c") 'calc-or-quick-calc)
;; Farenheit to Celcius calculator
(defun farenheit-to-celcius (temp)
"Convert TEMP from Farenheit to Celcius."
(/ (- temp 32) 1.8))
(defun farenheit-to-celcius-at-point ()
"Get the Farenheit temperature at point as Celcius."
(interactive)
(let* ((temp-F (number-at-point))
(temp-C (farenheit-to-celcius temp-F))
(bounds (bounds-of-thing-at-point 'number)))
(when bounds
(message (format "%fF = %fC" temp-F temp-C))
(if buffer-read-only
(kill-new (format "%f" temp-C))
(progn
(delete-region (car bounds) (cdr bounds))
(insert (format "%f" temp-C)))))))
;; Doc view
(require 'doc-view)
(setq doc-view-resolution 200)
(add-hook 'doc-view-mode-hook (lambda () (display-line-numbers-mode -1)))
(add-hook 'doc-view-mode-hook #'doc-view-fit-page-to-window)
(defun doc-view-other-frame-scroll-up ()
"Equivalent of switching to other frame, pressing SPC and then switching back."
(interactive)
(other-frame 1)
(doc-view-scroll-up-or-next-page)
(other-frame 1))
(defun doc-view-other-frame-scroll-down ()
"Equivalent of switching to other frame, pressing p and then switching back."
(interactive)
(other-frame 1)
(doc-view-scroll-down-or-previous-page)
(other-frame 1))
(global-set-key (kbd "C-c p n") #'doc-view-other-frame-scroll-up)
(global-set-key (kbd "C-c p p") #'doc-view-other-frame-scroll-down)
;; Browser
(setq shr-width 70
shr-cookie-policy nil
eww-retrieve-command nil
eww-browse-url-new-window-is-tab nil
browse-url-browser-function #'eww)
(defun eww-search-wiki ()
"Search Wikipedia."
(interactive)
(let ((eww-search-prefix
"https://en.wikipedia.org/wiki/Special:Search?go=Go&search="))
(call-interactively #'eww)))
(defun eww-search-scholar ()
"Search Google Scholar."
(interactive)
(let ((eww-search-prefix "https://scholar.google.co.uk/scholar?q="))
(call-interactively #'eww)))
(defun eww-search-pypi ()
"Search PyPi."
(interactive)
(let ((eww-search-prefix "https://pypi.org/search/?q="))
(call-interactively #'eww)))
(global-set-key (kbd "C-c u w") #'eww-search-wiki)
(global-set-key (kbd "C-c u s") #'eww-search-scholar)
(global-set-key (kbd "C-c u p") #'eww-search-pypi)
(global-set-key (kbd "C-c u u") #'eww)
;; Newsticker (RSS)
(global-set-key (kbd "C-c m n") #'newsticker-show-news)
(setq newsticker-frontend #'newsticker-treeview
newsticker-hide-old-items-in-newsticker-buffer t)
(setq newsticker-url-list
'(("npj imaging" "https://www.nature.com/npjimaging.rss" nil 3600)
("npj ml" "https://www.nature.com/natmachintell.rss" nil 3600)
("arxiv medphys" "https://rss.arxiv.org/rss//physics.med-ph" nil 3600)
("mr in med" "https://onlinelibrary.wiley.com/action/showFeed?jc=15222594&type=etoc&feed=rss" nil 3600)))
;; IRC
(require 'erc)
(load "~/.irc-auth")
(setq erc-autojoin-channels-alist
'(("Libera.Chat"
"#emacs"
"#python"
"#fortran"
"##forth"
"#nhs-dev"
"#math"
"#machinelearning")))
(setq erc-modules '(netsplit
fill
button
match
track
completion
readonly
networks
ring
autojoin
noncommands
irccontrols
move-to-prompt
stamp
menu
list
sasl))
(setq erc-nick "TactfulCitrus"
erc-port "6667"
erc-server "irc.libera.chat"
erc-try-new-nick-p nil
erc-warn-about-blank-lines t
erc-sasl-user erc-nick
erc-sasl-password libera-chat-pass)
(defun irc () (interactive) (erc))
(global-set-key (kbd "C-c m i") #'irc)
;; Music
(use-package emms
:ensure t
:config
(require 'emms-setup)
(require 'emms-player-mplayer)
(require 'emms-player-mpv)
(emms-all)
(if (eq system-type 'windows-nt)
(setq emms-player-list '(emms-player-mplayer))
(setq emms-player-list '(emms-player-mpv)
emms-info-functions '(emms-info-native)
emms-player-mpv-parameters '("--no-video"))))
(defvar emms-content-classic
"http://relax.stream.publicradio.org/relax.mp3"
"URL for a classical music radio station.")
(defun emms-play-classic ()
"Play the ClassicFM radio."
(interactive)
(emms-play-url emms-content-classic))
(defvar emms-content-lofi
"https://lofi.stream.laut.fm/lofi"
"URL for a lofi music radio station.")
(defun emms-play-lofi ()
"Play a LoFi radio station."
(interactive)
(emms-play-url emms-content-lofi))
(defvar emms-content-bosa
"https://centova5.transmissaodigital.com:20104/live"
"URL for a bosa nova radio station.")
(defun emms-play-bosa ()
"Play a Bosa Nova radio station."
(interactive)
(emms-play-url emms-content-bosa))
(defvar emms-content-smooth
"http://playerservices.streamtheworld.com/pls/977_SMOOJAZZ.pls"
"URL for a Smooth Jazz radio station.")
(defun emms-play-smooth ()
"Play a smooth jazz radio station."
(interactive)
(emms-play-url emms-content-smooth))
(defvar emms-content-tradjazz
"https://icecast.walmradio.com:8443/classic"
"URL for a traditional Jazz radio station.")
(defun emms-play-tradjazz ()
"Play a Traditional Jazz radio station."
(interactive)
(emms-play-url emms-content-tradjazz))
(defvar emms-content-jazz
"https://icecast.walmradio.com:8443/jazz"
"URL for a Jazz radio station.")
(defun emms-play-jazz ()
"Play a Jazz radio station."
(interactive)
(emms-play-url emms-content-jazz))
(defvar emms-content-news
"https://media-ssl.musicradio.com/LBCUKMP3"
"URL for a news radio station.")
(defun emms-play-news ()
"Play a news radio station."
(interactive)
(emms-play-url emms-content-news))
(defvar emms-content-funk
"http://jazz-wr06.ice.infomaniak.ch/jazz-wr06-128.mp3"
"URL for a funk radio station.")
(defun emms-play-funk ()
"Play a funk radio station."
(interactive)
(emms-play-url emms-content-funk))
(global-set-key (kbd "C-c m r b") #'emms-play-bosa)
(global-set-key (kbd "C-c m r c") #'emms-play-classic)
(global-set-key (kbd "C-c m r f") #'emms-play-funk)
(global-set-key (kbd "C-c m r j") #'emms-play-jazz)
(global-set-key (kbd "C-c m r l") #'emms-play-lofi)
(global-set-key (kbd "C-c m r n") #'emms-play-news)
(global-set-key (kbd "C-c m r s") #'emms-play-smooth)
(global-set-key (kbd "C-c m r t") #'emms-play-tradjazz)
(global-set-key (kbd "C-c m s") #'emms-stop)
;; ********
;;; * Mail *
;; ********
;; Notmuch ;;
(use-package notmuch
:straight nil)
(setq-default notmuch-search-oldest-first nil)
(global-set-key (kbd "C-c m m") #'notmuch)
;; Sometimes notmuch renders html really badly for the current colour scheme.
;; To remedy this, it's handy to open up the current email in a side buffer
;; and use =eww= to render the raw html.
(add-to-list 'display-buffer-alist
'("html" . (display-buffer-same-window)))
(defun notmuch-view-html-in-eww-other-window ()
"Display the current mail buffer in another window."
(interactive)
(other-window-prefix)
(notmuch-show-view-raw-message)
(shr-render-buffer (current-buffer)))
(global-set-key (kbd "C-c m v") #'notmuch-view-html-in-eww-other-window)
;; Sending Mail ;;
(use-package smtpmail)