-
Notifications
You must be signed in to change notification settings - Fork 10
/
.emacs.el
2548 lines (2356 loc) · 93.5 KB
/
.emacs.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
;;; initfile --- Summary:
;;; Commentary:
;; Emacs 25.1 and newer tested
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Configuration/Customization:
;; Defines global variables that are later used to customize and set
;; up packages.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specify the ycmd server command and path to the ycmd directory *inside* the
;; cloned ycmd directory. I use a wrapper to add the path to the ycmd clang
;; directories.
(if (file-exists-p "~/.PythonYcmd.sh")
(defvar my:ycmd-server-command
'("~/.PythonYcmd.sh" "/home/nils/Research/ycmd/ycmd"))
(defvar my:ycmd-server-command '("python" "/home/nils/Research/ycmd/ycmd"))
)
(defvar my:ycmd-extra-conf-whitelist '("~/.ycm_extra_conf.py"))
(defvar my:ycmd-global-config "~/.ycm_extra_conf.py")
;; In order to get python code completion with ycmd+jedi you must specify
;; the path to the python executable you're using.
(defvar my:ycmd-python-binary-path "/usr/bin/python")
;; Enable ycmd-eldoc support. Eldoc can cause delays when working with
;; template-heavy C++ code.
(defvar my:use-ycmd-eldoc nil)
;; Choose ycmd or lsp for C/C++ completion. lsp or ycmd
(defvar my:cxx-completer "lsp")
;; When t use smart-hungry-delete
;; (https://github.com/hrehfeld/emacs-smart-hungry-delete).
;; When nil use hungry-delete
(defvar my:use-smart-hungry-delete nil)
;; Set to t if you want to use ycmd-goto in C/C++/Rust mode
(defvar my:use-ycmd-goto nil)
;; Set to t if you want to use lsp-find-definition in C/C++/Rust mode
(defvar my:use-lsp-goto t)
;; lsp-mode performs better with plists (see LSP documentation)
(defvar lsp-use-plists t)
;; Specify the jupyter executable name, and the start dir of the server
(defvar my:jupyter_location (executable-find "jupyter"))
(defvar my:jupyter_start_dir "/home/nils")
;; In order to get EIN to work with byte-compiling we must explicitly
;; load some of the EIN files. Which files need to be loaded seems to change
;; over time, so to make maintenance easier we provide a variable here that
;; can be updated.
(defvar my:ein-explicit-load-files '(ein ein-notebook ein-jupyter))
;; In order to get lsp-mode to work properly when byte-compiling the init file
;; we must explicitly load its files. This list is produced from all the .el
;; files at:
;; https://github.com/emacs-lsp/lsp-mode
(defvar my:lsp-explicit-load-files
'(lsp-completion lsp-diagnostics lsp-headerline lsp-lens lsp-mode
lsp-modeline lsp-protocol lsp))
;; Compilation command for C/C++
(defvar my:compile-command "clang++ -Wall -Wextra -std=c++17 ")
(defun my:compile-command-hook()
"Try to intelligently set the compilation command locally in the buffer.
If a compile_commands.json file exists in the projectile directory then
the build directory is extracted from the first file in the JSON file and
set as the build directory for CMake. By default 10 cores are used for
compilation."
(require 'json)
(when (and (projectile-project-root)
(file-exists-p
(format "%s/compile_commands.json"
(projectile-project-root)))
(file-exists-p
(format "%s/CMakeLists.txt"
(projectile-project-root))))
(let ((my:project-build-dir
(gethash "directory"
(first (let ((json-array-type 'list)
(json-object-type 'hash-table))
(json-read-file
(format "%s/compile_commands.json"
(projectile-project-root))
))))))
(set (make-local-variable 'compile-command)
(format "cmake --build %s -j50 --target"
my:project-build-dir)
))))
;; Which theme to use.
;; - spacemacs-dark
;; - sourcerer
;; - doom-* (the doom themes https://github.com/hlissner/emacs-doom-themes)
(defvar my:use-theme 'doom-one)
;; Set my:use-dvorak-bindings to t if you use a Dvorak keyboard layout
(defvar my:use-dvorak-bindings t)
;; Set to t in order to enable using hydra with only x as the activation
;; key. The commands are:
;; - xb starts switch-buffer
;; - xf starts find-file or counsel-find-file
;; - xg starts Magit status
;; - xh starts hydra-dispatch
;; - xn starts hydra-move
;; - xx does save-buffer
;;
;; I've found that this leads to instability and causes Emacs to crash
;; fairly frequently, at least when run in daemon mode.
;;
;; A hydra-dispatch can be accessed using 'C-c h'
(defvar my:use-no-modifier-hydra nil)
;; Set my:use-evil-mode to t if you want to use Evil mode
;;
;; Note: Currently there is a warning about evil-want-integration not
;; being set to nil during compilation that I haven't figured out
;; how to fix yet.
(defvar my:use-evil-mode nil)
;; Set my:use-prescient to t if you want to use prescient for sorting
;;
;; https://github.com/raxod502/prescient.el
(defvar my:use-prescient t)
;; Set my:byte-compile-init to t if you want to compile the init file.
;; This will improve startup time by ~2-3 times, but makes getting certain
;; packages to load correctly more difficult. Most of the packages work
;; correctly with a byte-compiled init file.
(defvar my:byte-compile-init t)
;; Force Emacs to try to start a server. On macOS checking if a server is
;; started doesn't always work correctly so this is a workaround for that.
(defvar my:force-server-start nil)
;; Specify the search backend. Must be either:
;; - ivy https://github.com/abo-abo/swiper
;; - selectrum https://github.com/raxod502/selectrum
(defvar my:search-backend "selectrum")
;; A list of modes for which to disable whitespace mode
(defvar my:ws-disable-modes '(magit-mode help-mode Buffer-menu-mode))
;; Modes in which to disable auto-deleting of trailing whitespace
(defvar my:ws-butler-global-exempt-modes
'(markdown-mode ein:notebook-multilang-mode))
;; TEX is installed in a different location on macOS
(when (string-equal system-type "darwin")
(setenv "PATH" (concat (getenv "PATH") ":/Library/TeX/texbin/"))
(setq exec-path (append exec-path '("/Library/TeX/texbin/")))
(setenv "PATH" (concat (getenv "PATH") ":/usr/local/bin"))
(setenv "PATH" (concat (getenv "PATH") ":/opt/homebrew/bin")))
;; Set font size. Font size is set to my:font-size/10
(defvar my:font-size 90)
;; I find it much easier to use underline rather than actual
;; highlighting to read.
(defun my:set-custom-faces()
"Set custom faces after the theme is loaded."
(custom-set-faces
'(ivy-current-match
((t (:background nil :underline t))))
'(company-tooltip
((t (:background unspecified))))
'(company-tooltip-selection
((t (:background unspecified :underline t))))
'(highlight
((t (:background "nil" :foreground "nil" :underline t))))
)
;; Customize powerline colors. I like purple pink-ish
(custom-set-faces
'(powerline-active1
((t (:background "black" :foreground "#c678dd" :bold t :underline t))))
'(powerline-active2
((t (:background "black" :foreground "#c678dd" :bold t :underline t))))
)
(set-face-attribute 'powerline-inactive1 nil :background
(face-attribute 'powerline-active1 :background))
(set-face-attribute 'powerline-inactive2 nil :background
(face-attribute 'powerline-active2 :background))
(set-face-attribute 'powerline-inactive1 nil :foreground
(face-attribute 'powerline-active1 :foreground))
(set-face-attribute 'powerline-inactive2 nil :foreground
(face-attribute 'powerline-active2 :foreground))
;; Custom face for avy
(custom-set-faces
'(avy-lead-face
((t (:foreground "black" :background "#c678dd" :bold t :underline t)))))
;; Make the background pure black because I find that easier to read
(custom-set-faces
'(default
((t (:background "black")))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set packages to install
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq package-archives '(("melpa-stable" . "https://stable.melpa.org/packages/")
("melpa" . "https://melpa.org/packages/")
("gnu" . "http://elpa.gnu.org/packages/")))
;; Disable package initialize after us. We either initialize it
;; anyway in case of interpreted .emacs, or we don't want slow
;; initizlization in case of byte-compiled .emacs.elc.
(setq package-enable-at-startup nil)
;; Disable magically opening remote files during init
(defvar file-name-handler-alist-old file-name-handler-alist)
(setq file-name-handler-alist nil)
;; Ask package.el to not add (package-initialize) to .emacs.
(setq package--init-file-ensured t)
;; set use-package-verbose to t for interpreted .emacs,
;; and to nil for byte-compiled .emacs.elc
(eval-and-compile
(setq use-package-verbose (not (bound-and-true-p byte-compile-current-file))))
;; Add the macro generated list of package.el loadpaths to load-path.
(mapc #'(lambda (add) (add-to-list 'load-path add))
(eval-when-compile
(require 'package)
(package-initialize)
;; Install use-package if not installed yet.
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; (require 'use-package)
(let ((package-user-dir-real (file-truename package-user-dir)))
;; The reverse is necessary, because outside we mapc
;; add-to-list element-by-element, which reverses.
(nreverse
(apply #'nconc
;; Only keep package.el provided loadpaths.
(mapcar #'(lambda (path)
(if (string-prefix-p package-user-dir-real path)
(list path)
nil))
load-path))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; By default Emacs triggers garbage collection at ~0.8MB which makes
;; startup really slow. Since most systems have at least 64MB of memory,
;; we increase it during initialization.
(setq gc-cons-threshold 128000000)
(add-hook 'after-init-hook
#'(lambda ()
;; restore after startup
(setq gc-cons-threshold 8000000
file-name-handler-alist file-name-handler-alist-old
)))
;; Extra plugins and config files are stored here
(if (not (file-directory-p "~/.emacs.d/plugins/"))
(make-directory "~/.emacs.d/plugins/"))
(add-to-list 'load-path (expand-file-name "~/.emacs.d/plugins"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Start emacs server if not already running
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when (or my:force-server-start
(and (fboundp 'server-running-p) (not (server-running-p))))
(server-start))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; General Tweaks
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Only allow encrypted auth sources
(if (file-exists-p "~/.authinfo.gpg")
(setq auth-sources '((:source "~/.authinfo.gpg"))))
;; turn on highlight matching brackets when cursor is on one
(show-paren-mode t)
;; Overwrite region selected
(delete-selection-mode t)
;; Show column numbers by default
(setq column-number-mode t)
;; Use CUA to delete selections
(setq cua-mode t)
(setq cua-enable-cua-keys nil)
;; Prevent emacs from creating a bckup file filename~
(setq make-backup-files nil)
;; Settings for searching
(setq-default case-fold-search t ;case insensitive searches by default
search-highlight t) ;hilit matches when searching
;; Highlight the line we are currently on
(global-hl-line-mode t)
;; Disable the toolbar at the top since it's useless
(if (functionp 'tool-bar-mode) (tool-bar-mode -1))
;; Auto-wrap at 80 characters
(setq-default fill-column 80)
(add-hook 'text-mode-hook (lambda () (auto-fill-mode 1)))
;; Only break in comments in programming mode.
(add-hook 'c-mode-common-hook
(lambda ()
(auto-fill-mode 1)
(set (make-local-variable 'fill-nobreak-predicate)
(lambda ()
(not (eq (get-text-property (point) 'face)
'font-lock-comment-face))))))
;; Disable Emacs help on translation, e.g. C-x C-h actually
;; can do things now
(define-key key-translation-map [?\C-h] [?\C-?])
;; Global Keyboard Shortcuts
;; Set help to C-?
(global-set-key (kbd "C-?") 'help-command)
;; Set mark paragraph to M-?
(global-set-key (kbd "M-?") 'mark-paragraph)
;; Set backspace to C-h
(global-set-key (kbd "C-h") 'delete-backward-char)
;; Set backspace word to M-h
(global-set-key (kbd "M-h") 'backward-kill-word)
;; Use meta+tab word completion
(global-set-key (kbd "M-TAB") 'dabbrev-expand)
;; Easy undo key
(global-set-key (kbd "C-/") 'undo)
;; Comment or uncomment the region
(global-set-key (kbd "C-c ;") 'comment-or-uncomment-region)
;; Indent after a newline, if required by syntax of language
(global-set-key (kbd "C-m") 'newline-and-indent)
;; Load the compile ocmmand
(global-set-key (kbd "C-c C-c") 'compile)
;; We don't want to type yes and no all the time so, do y and n
(defalias 'yes-or-no-p 'y-or-n-p)
;; Disable the horrid auto-save
(setq auto-save-default nil)
;; Disable the menu bar since we don't use it, especially not in the
;; terminal
(when (and (not (eq system-type 'darwin)) (fboundp 'menu-bar-mode))
(menu-bar-mode -1))
;; Don't ring the bell
(setq ring-bell-function 'ignore)
;; Non-nil means draw block cursor as wide as the glyph under it.
;; For example, if a block cursor is over a tab, it will be drawn as
;; wide as that tab on the display.
(setq x-stretch-cursor t)
;; Dont ask to follow symlink in git
(setq vc-follow-symlinks t)
;; Check (on save) whether the file edited contains a shebang, if yes,
;; make it executable from
;; http://mbork.pl/2015-01-10_A_few_random_Emacs_tips
(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
;; Highlight some keywords in prog-mode
(add-hook 'prog-mode-hook
(lambda ()
;; Highlighting in cmake-mode this way interferes with
;; cmake-font-lock, which is something I don't yet understand.
(when (not (derived-mode-p 'cmake-mode))
(font-lock-add-keywords
nil
'(("\\<\\(FIXME\\|TODO\\|BUG\\|DONE\\)"
1 font-lock-warning-face t))))))
;; Setup use-package
(eval-when-compile
(require 'use-package))
(use-package bind-key
:ensure t)
;; so we can (require 'use-package) even in compiled emacs to e.g. read docs
(use-package use-package
:commands use-package-autoload-keymap)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; esup: Emacs StartUp Profiler
;; - Profile the load time of the Emacs init file
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package esup
:ensure t
:init
(setq esup-child-max-depth 0)
;; Use a hook so the message doesn't get clobbered by other messages.
(add-hook
'emacs-startup-hook
(lambda ()
(message "Emacs ready in %s with %d garbage collections."
(format "%.2f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Enable terminal emacs to copy and paste from system clipboard
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Note: this uses C-c before C-w, M-w, and M-y
;; From: https://stackoverflow.com/questions/64360/how-to-copy-text-from-emacs-to-another-application-on-linux
(defun my-copy-to-xclipboard(arg)
"Copy the selection ARG to the X11 clipboard."
(interactive "P")
(cond
((not (use-region-p))
(message "Nothing to yank to X-clipboard"))
((and (not (display-graphic-p))
(/= 0 (shell-command-on-region
(region-beginning) (region-end) "xsel -i -b")))
(message "Error: Is program `xsel' installed?"))
(t
(when (display-graphic-p)
(call-interactively 'clipboard-kill-ring-save))
(message "Yanked region to X-clipboard")
(when arg
(kill-region (region-beginning) (region-end)))
(deactivate-mark))))
(defun my-cut-to-xclipboard()
"Cut the selection to the X11 clipboard."
(interactive)
(my-copy-to-xclipboard t))
(defun my-paste-from-xclipboard()
"Paste the selection from the X11 clipboard."
(interactive)
(if (display-graphic-p)
(clipboard-yank)
(insert (shell-command-to-string "xsel -o -b"))))
(global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
(global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
;; Use C-c M-y instead of C-c C-y so it works in Python mode too.
(global-set-key (kbd "C-c M-y") 'my-paste-from-xclipboard)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to set SSH_AUTH_SOCK env variable to socket
;; created by systemd.
;;
;; Wrap in a function so we can control when and where to call.
;;
;; Can use systemd service at ;; ~/.config/systemd/user/ssh-agent.service
;;
;; [Unit]
;; Description=SSH key agent
;;
;; [Service]
;; Type=forking
;; Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
;; ExecStart=/usr/bin/ssh-agent -a $SSH_AUTH_SOCK
;;
;; [Install]
;; WantedBy=default.target
;;
;; Run:
;; systemctl --user enable ssh-agent
;; systemctl --user start ssh-agent
;;
;; Make sure ~/.ssh/config has:
;; AddKeysToAgent yes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun my:set_ssh_auth_sock()
"If the file XDG_RUNTIME_DIR/ssh-agent.socket exists, use as SSH socket"
(when (file-exists-p (format "%s/ssh-agent.socket"
(getenv "XDG_RUNTIME_DIR")))
(setenv "SSH_AUTH_SOCK"
(format "%s/ssh-agent.socket" (getenv "XDG_RUNTIME_DIR")))))
;; Set env variable.
(my:set_ssh_auth_sock)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; async - library for async/thread processing
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package async
:ensure t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; s is used by origami, etc and sometimes during Emacs
;; upgrades disappears so we try to install it on its own.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package s
:ensure t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Automatically compile and save ~/.emacs.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when my:byte-compile-init
(defun byte-compile-init-files (file)
"Automatically compile FILE."
(interactive)
(save-restriction
;; Suppress the warning when you setq an undefined variable.
(if (>= emacs-major-version 23)
(setq byte-compile-warnings '(not free-vars obsolete))
(setq byte-compile-warnings
'(unresolved
callargs
redefine
obsolete
noruntime
cl-warnings
interactive-only)))
(byte-compile-file (expand-file-name file))))
;; Add a post-save hook that checks if ~/.emacs.el exists and if the file
;; name of the current buffer is ~/.emacs.el or the symbolically linked
;; file.
(add-hook
'after-save-hook
(function
(lambda ()
(when (and (string= (file-truename "~/.emacs.el")
(file-truename (buffer-file-name)))
(file-exists-p "~/.emacs.el"))
(byte-compile-init-files "~/.emacs.el")))))
;; Byte-compile again to ~/.emacs.elc if it is outdated. We use file-truename
;; to follow symbolic links so that ~/.emacs.el can be symbolically linked to
;; the location where the .emacs.el is stored.
(when (file-newer-than-file-p
(file-truename "~/.emacs.el")
(file-truename "~/.emacs.elc"))
(byte-compile-init-files "~/.emacs.el")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auto-package-update
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Auto update packages once a week
(use-package auto-package-update
:ensure t
:commands (auto-package-update-maybe)
:config
(setq auto-package-update-delete-old-versions t)
(setq auto-package-update-hide-results t)
(auto-package-update-maybe)
(add-hook 'auto-package-update-before-hook
(lambda () (message "I will update packages now")))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; diminish - Hide the minor modes in the mode line for more room
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package diminish
:ensure t
:init
(eval-when-compile
;; Silence missing function warnings
(declare-function diminish "diminish.el"))
:config
(diminish 'abbrev-mode)
(diminish 'eldoc-mode)
(diminish 'auto-revert-mode)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ws-butler-mode
;;
;; Remove trailing white space upon saving
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package ws-butler
:ensure t
:diminish ws-butler-mode
:config
(setq ws-butler-global-exempt-modes my:ws-butler-global-exempt-modes)
(ws-butler-global-mode 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; all-the-icons
;;
;; Used by company-box and some themes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package all-the-icons
:ensure t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Select search backend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar my:use-ivy nil)
(defvar my:use-selectrum nil)
(if (string-match "ivy" my:search-backend)
(setq my:use-ivy t)
(if (string-match "selectrum" my:search-backend)
(setq my:use-selectrum t)
(warn "my:search-backend must be to 'ivy' or 'selectrum'")
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; hydra config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package hydra
:ensure t
:config
;; Define a hydra for movement
(defhydra hydra-move ()
"move"
("n" next-line)
("p" previous-line)
("f" forward-char)
("b" backward-char)
("a" beginning-of-line)
("e" move-end-of-line)
("u" forward-word)
("o" backward-word)
("M-f" forward-word)
("M-b" backward-word)
("h" backward-delete-char)
("d" delete-forward-char)
("M-h" backward-kill-word)
("M-d" kill-word)
("s" save-buffer)
("/" undo-tree-undo)
("x" goto-line)
("m" newline-and-indent)
("t" transpose-chars)
("c" avy-goto-word-1)
(";" (call-interactively 'commend-or-uncomment-region))
;; Selection
("r" (if (region-active-p)
(deactivate-mark)
(rectangle-mark-mode t)))
("M-w" (when (or (bound-and-true-p rectangle-mark-mode)
(region-active-p))
(call-interactively 'kill-ring-save)
))
("w" (when (or (bound-and-true-p rectangle-mark-mode)
(region-active-p))
(call-interactively 'kill-region)
))
("SPC" (call-interactively 'set-mark-command))
("y" (call-interactively 'yank))
("k" (if (bound-and-true-p rectangle-mark-mode)
(call-interactively 'kill-rectangle)
(call-interactively 'kill-line)))
;; Scrolling
("v" scroll-up-command)
("M-v" scroll-down-command)
("l" recenter-top-bottom)
("g" nil "cancel" :color blue)
)
(defhydra hydra-dispatch
(:color blue :hint nil)
("f" (call-interactively #'hydra-flyspell-correct/body) "Flyspell")
("m" (call-interactively #'hydra-move/body) "Move")
("o" (call-interactively #'hydra-origami/body) "Origami")
("p" (call-interactively #'hydra-projectile/body) "Projectile")
("s" (call-interactively #'hydra-string-inflection/body)
"String inflection")
("g" nil "cancel")
)
;; We define a global x-hydra-timer because we need to be able to
;; cancel the timer in order to enter projectile from 'xp'
(defvar x-hydra-timer)
;; We save the buffer-undo-list because we want to be able to
;; remove inserting the 'x' character from the undo history in the
;; case where we entered hydra
(defvar x-hydra-buffer-undo-list)
(defun x-hydra-pre ()
(when (not buffer-read-only)
(setq x-hydra-buffer-undo-list buffer-undo-list)
(undo-boundary)
(insert "x")
)
(setq x-hydra-timer (timer-create))
(timer-set-time x-hydra-timer (timer-relative-time (current-time) 0.5))
(timer-set-function x-hydra-timer 'hydra-keyboard-quit)
(timer-activate x-hydra-timer))
(defhydra x-hydra (:body-pre x-hydra-pre
:color blue
:hint nil)
;; Some Emacs functions like find-file must receive one argument,
;; but we want to call it interactively so we need to use
;; (call-interactively 'find-file)
("b" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'switch-to-buffer)))
("f" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'find-file)))
("g" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'magit-status)))
("h" (progn (cancel-timer x-hydra-timer)
(when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively #'hydra-dispatch/body)))
("n" (progn (cancel-timer x-hydra-timer)
(when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively #'hydra-move/body)))
("x" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(save-buffer)))
("3" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'split-window-right)))
("2" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'split-window-below)))
("1" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'delete-other-windows)))
("0" (progn (when (not buffer-read-only)
(undo)
(setq buffer-undo-list x-hydra-buffer-undo-list))
(call-interactively 'delete-window)))
)
(when my:use-no-modifier-hydra
(global-set-key "x" #'x-hydra/body))
;; Dispatch to hydra
(global-set-key (kbd "C-c h") #'hydra-dispatch/body)
)
(use-package use-package-hydra
:ensure t
:after hydra)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Ivy config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when my:use-ivy
(use-package ivy
:ensure t
:diminish ivy-mode
:commands (ivy-mode)
:config
(when my:byte-compile-init
(require 'ivy))
(ivy-mode t)
(setq ivy-use-virtual-buffers t)
(setq enable-recursive-minibuffers t)
(setq ivy-wrap t)
(global-set-key (kbd "C-c C-r") 'ivy-resume)
;; Show #/total when scrolling buffers
(setq ivy-count-format "%d/%d ")
)
;; Using prescient for sorting results with ivy:
;; https://github.com/raxod502/prescient.el
(when my:use-prescient
(use-package ivy-prescient
:ensure t
:after (counsel)
:config
(ivy-prescient-mode t)
(prescient-persist-mode t)
)
)
(use-package swiper
:ensure t
)
(use-package counsel
:ensure t
:bind (("M-x" . counsel-M-x)
("C-x C-f" . counsel-find-file)
("<f1> f" . counsel-describe-function)
("<f1> v" . counsel-describe-variable)
("<f1> l" . counsel-find-library)
("C-c i" . counsel-info-lookup-symbol)
("C-c u" . counsel-unicode-char)
("C-s" . buffer-dependent-swiper)
("C-r" . buffer-dependent-swiper)
("C-c g" . counsel-git-grep)
("C-c j" . counsel-git)
("C-c k" . counsel-ag)
("C-c r" . counsel-rg)
("C-x l" . counsel-locate)
:map minibuffer-local-map
("C-r" . counsel-minibuffer-add)
)
:config
(if (executable-find "rg")
;; use ripgrep instead of grep because it's way faster
(setq counsel-grep-base-command
"rg -i -M 120 --no-heading --line-number --color never %s %s"
counsel-rg-base-command
"rg -i -M 120 --no-heading --line-number --color never %s .")
(warn "\nWARNING: Could not find the ripgrep executable. It "
"is recommended you install ripgrep."))
;; Switch whether we use swiper or counsel-grep depending on the major mode.
;; This is because for certain themes font highlighting is very expensive
;; in some modes (e.g. C++ mode)
(defun buffer-dependent-swiper (&optional initial-input)
(interactive)
(if (or (not buffer-file-name)
(ignore-errors
(file-remote-p (buffer-file-name)))
(if (or (eq major-mode 'org-mode)
(eq major-mode 'c++-mode))
(<= (buffer-size) 50000)
;; The value 300000 is the default number of characters
;; before falling back to counsel-grep from swiper.
(<= (buffer-size) 300000)))
(swiper initial-input)
(progn
(when (file-writable-p buffer-file-name)
(save-buffer))
(counsel-grep initial-input))))
)
(use-package counsel-projectile
:ensure t
:after (:all counsel projectile)
:bind (("C-x M-f" . counsel-projectile-find-file-dwim))
:init
(eval-when-compile
;; Silence missing function warnings
(declare-function counsel-projectile-mode "counsel-projectile.el"))
:config
(counsel-projectile-mode))
;; Use universal ctags to build the tags database for the project.
;; When you first want to build a TAGS database run 'touch TAGS'
;; in the root directory of your project.
(use-package counsel-etags
:ensure t
:init
(eval-when-compile
;; Silence missing function warnings
(declare-function counsel-etags-virtual-update-tags "counsel-etags.el")
(declare-function counsel-etags-guess-program "counsel-etags.el")
(declare-function counsel-etags-locate-tags-file "counsel-etags.el"))
:bind (
("M-." . counsel-etags-find-tag-at-point)
("M-t" . counsel-etags-grep-symbol-at-point))
:config
;; Ignore files above 800kb
(setq counsel-etags-max-file-size 800)
;; Ignore build directories for tagging
(add-to-list 'counsel-etags-ignore-directories '"build*")
(add-to-list 'counsel-etags-ignore-directories '".vscode")
(add-to-list 'counsel-etags-ignore-filenames '".clang-format")
;; Don't ask before rereading the TAGS files if they have changed
(setq tags-revert-without-query t)
;; Don't warn when TAGS files are large
(setq large-file-warning-threshold nil)
;; How many seconds to wait before rerunning tags for auto-update
(setq counsel-etags-update-interval 180)
;; Set up auto-update
(add-hook
'prog-mode-hook
(lambda () (add-hook 'after-save-hook
(lambda ()
(counsel-etags-virtual-update-tags)))))
;; The function provided by counsel-etags is broken (at least on Linux)
;; and doesn't correctly exclude directories, leading to an excessive
;; amount of incorrect tags. The issue seems to be that the trailing '/'
;; in e.g. '*dirname/*' causes 'find' to not correctly exclude all files
;; in that directory, only files in sub-directories of the dir set to be
;; ignore.
(defun my-scan-dir (src-dir &optional force)
"Create tags file from SRC-DIR. \
If FORCE is t, the commmand is executed without \
checking the timer."
(let* ((find-pg (or
counsel-etags-find-program
(counsel-etags-guess-program "find")))
(ctags-pg (or
counsel-etags-tags-program
(format "%s -e -L" (counsel-etags-guess-program
"ctags"))))
(default-directory src-dir)
;; run find&ctags to create TAGS
(cmd (format
"%s . \\( %s \\) -prune -o -type f -not -size +%sk %s | %s -"
find-pg
(mapconcat
(lambda (p)
(format "-iwholename \"*%s*\"" p))
counsel-etags-ignore-directories " -or ")
counsel-etags-max-file-size
(mapconcat (lambda (n)
(format "-not -name \"%s\"" n))
counsel-etags-ignore-filenames " ")
ctags-pg))
(tags-file (concat (file-name-as-directory src-dir) "TAGS"))
(doit (or force (not (file-exists-p tags-file)))))
;; always update cli options
(when doit
(message "%s at %s" cmd default-directory)
(async-shell-command cmd)
(visit-tags-table tags-file t))))
(setq counsel-etags-update-tags-backend
(lambda ()
(interactive)
(let* ((tags-file (counsel-etags-locate-tags-file)))
(when tags-file
(my-scan-dir (file-name-directory tags-file) t)
(run-hook-with-args
'counsel-etags-after-update-tags-hook tags-file)
(unless counsel-etags-quiet-when-updating-tags
(message "%s is updated!" tags-file))))))
)
(use-package flyspell-correct-ivy
:ensure t
:after (:all flyspell ivy))
(use-package lsp-ivy
:ensure t
:diminish
:after (:all lsp-mode ivy))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Selectrum config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when my:use-selectrum
(use-package selectrum
:ensure t
:config
(selectrum-mode t)
(when my:use-prescient
(use-package selectrum-prescient
:ensure t
:config
(selectrum-prescient-mode t)
(prescient-persist-mode t)))
)
(use-package ctrlf
:ensure t
:bind (("C-s" . ctrlf-forward-fuzzy-regexp)
("C-r" . ctrlf-backward-fuzzy-regexp)
:map ctrlf-mode-map
("C-s" . ctrlf-forward-fuzzy-regexp)
("C-r" . ctrlf-backward-fuzzy-regexp)
)
:config
(ctrlf-mode t)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set up GNU Global Tags (ggtags)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when (executable-find "global")
(use-package ggtags
:ensure t
:diminish ggtags-mode
:defer t
:commands (ggtags-mode)
:init
;; More complicated hook logic so we don't interfere with LSP
;; or ycmd-goto
(when (and (not (and (string-equal my:cxx-completer "lsp")
my:use-lsp-goto))
(not (and (string-equal my:cxx-completer "ycmd")
my:use-ycmd-goto)))
(add-hook 'c-mode-common-hook
(lambda ()
(when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
(ggtags-mode t)))))
:config
;; Don't try to update GTAGS on each save;
;; makes the system sluggish for huge projects.
(setq ggtags-update-on-save t)
;; Don't auto-highlight tag at point.. makes the system really sluggish!
(setq ggtags-highlight-tag nil)
;; Enabling nearness requires global 6.5+
(setq ggtags-sort-by-nearness t)
(setq ggtags-navigation-mode-lighter nil)
(setq ggtags-mode-line-project-name nil)
(setq ggtags-oversize-limit (* 30 1024 1024)) ; 30 MB
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set up projectile
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package projectile
:ensure t
:diminish projectile-mode
:hydra
(hydra-projectile (:color teal :hint nil)
("a" projectile-ag "ag")
("b" projectile-switch-to-buffer "Switch buffer")
("c" projectile-invalidate-cache "Invalidate cache")
("d" projectile-find-dir "Find dir")
("sf" projectile-find-file "Find file")