-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
executable file
·1600 lines (1386 loc) · 57.1 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
;;; PRE-INIT
;;; To ensure setup is quick
(setq gc-cons-threshold 500000000
load-prefer-newer t
package-enable-at-startup nil)
(defun to/report-startup-time () ""
(message "*** Emacs loaded in %s with %d garbage collections."
(format "%.2f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done))
(add-hook 'emacs-startup-hook 'to/report-startup-time)
;; Silence compiler warnings as they can be pretty disruptive
(setq comp-async-report-warnings-errors nil)
;;; Init bootstrap
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
(setq straight-use-package-by-default t)
;;; In order to still use `package-list-packages' to find new toys.
(require 'use-package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("melpa-stable" . "https://stable.melpa.org/packages/")
("elpa" . "https://elpa.gnu.org/packages/")))
;;; Use utf-8 everywhere it makes sense.
(define-coding-system-alias 'cp65001 'utf-8)
(prefer-coding-system 'utf-8) ;; fixes some packages containing non-iso-latin characets
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(eval-when-compile
(require 'cl))
(defmacro to/pushn (list &rest vals)
(let ((forms (mapcar (lambda (var) `(push ,var ,list)) vals)))
`(progn ,@forms)))
(defmacro to/set-safe (name value) `(when (boundp ',name) (setq ,name
,value)))
(defmacro to/disable (name) `(when (fboundp ',name) (,name -1)))
(defmacro measure-time (msg &rest body)
"Measure the time it takes to evaluate BODY."
`(let ((time (current-time))) ,@body (message "%s %.06f" ,msg (float-time (time-since time)))))
;; generic setq
(setq experiments-dir (expand-file-name "experiments" user-emacs-directory)
packages-dir (expand-file-name "packages" user-emacs-directory)
custom-file (expand-file-name "custom.el" user-emacs-directory))
(defun to/append-to-list (list-var elements)
"Append ELEMENTS to the end of LIST-VAR.
The return value is the new value of LIST-VAR."
(unless (consp elements)
(error
"ELEMENTS must be a list"))
(let ((list (symbol-value list-var)))
(if list (setcdr (last list) elements)
(set list-var elements)))
(symbol-value list-var))
(defun to/do-list-dir (thedir)
"Iterates over all files in THEDIR and loads them"
(if (file-accessible-directory-p thedir)
(progn (dolist (file (directory-files thedir t "\.el$" nil))
(load (file-name-sans-extension file) t t)))))
(defun risky-local-variable-p (sym &optional _ignored) "Noop" nil)
(use-package emacs
:demand t
:init
(global-so-long-mode 1)
(to/pushn load-path packages-dir experiments-dir)
(push '(fullscreen . maximized) default-frame-alist)
(set-fringe-mode 20)
(setq-default custom-safe-themes t
transient-mark-mode t
tab-width 4)
(setq completion-cycle-threshold 3)
;; Emacs 28: Hide commands in M-x which do not apply to the current mode.
;; Corfu commands are hidden, since they are not supposed to be used via M-x.
(setq read-extended-command-predicate
#'command-completion-default-include-p)
;; Enable indentation+completion using the TAB key.
;; `completion-at-point' is often bound to M-TAB.
(setq tab-always-indent 'complete)
(setq
read-process-output-max (* 1024 1024)
bidi-paragraph-direction 'left-to-right
bidi-inhibit-bpa t
apropos-do-all t
auto-save-file-name-transforms `((".*" ,temporary-file-directory t))
backup-directory-alist `((".*" . ,temporary-file-directory))
bookmark-default-file (concat user-emacs-directory "bookmarks.em")
bookmark-save-flag 1
compilation-scroll-output 'first-error
confirm-kill-emacs 'y-or-n-p
cursor-type t
delete-selection-mode t
echo-keystrokes 0.1
ediff-diff-options "-w"
ediff-split-window-function 'split-window-horizontally
ediff-window-setup-function 'ediff-setup-windows-plain
fill-column 80
frame-title-format '((:eval (if (buffer-file-name) (abbreviate-file-name (buffer-file-name)) "%b")))
history-length 1000
indent-tabs-mode nil
inhibit-splash-screen t
inhibit-startup-message t
large-file-warning-threshold 100000000
locale-coding-system 'utf-8
mouse-yank-at-point t
require-final-newline t
ring-bell-function 'ignore
save-interprogram-paste-before-kill t
scroll-conservatively 100000
scroll-margin 0
scroll-preserve-screen-position 0
user-full-name "Tom Solberg"
user-mail-address "[email protected]"
vc-make-backup-files t
x-select-enable-clipboard t
x-select-enable-primary t)
(when window-system (set-frame-font "Cascadia Code 14"))
(fset 'yes-or-no-p 'y-or-n-p)
(mapc 'frame-set-background-mode (frame-list))
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(setenv "SSH_ASKPASS" "git-gui --askpass")
(transient-mark-mode 1)
(make-variable-buffer-local 'transient-mark-mode)
(put 'transient-mark-mode 'permanent-local t)
(to/disable tool-bar-mode)
(to/disable scroll-bar-mode)
(to/disable blink-cursor-mode)
(to/disable menu-bar-mode)
(to/disable horizontal-scroll-bar-mode)
(load-file (expand-file-name (if (eq system-type 'windows-nt)
"windows-setup.el"
"unix-setup.el")
user-emacs-directory))
(add-hook 'before-save-hook 'delete-trailing-whitespace)
)
;; Requires early setup
(use-package no-littering)
(use-package benchmark-init
:demand t
:config
;; To disable collection of benchmark data after init is done.
(add-hook 'after-init-hook 'benchmark-init/deactivate))
(use-package comint
:straight (:type built-in)
:init
(defun to/preoutput-turn-buffer-read-only (text)
(propertize text 'read-only t))
:custom
(comint-prompt-readonly t)
(comint-buffer-maximum-size 20000 "Increase comint buffer size.")
(comint-prompt-read-only t "Make the prompt read only.")
:config
(add-hook 'comint-preoutput-filter-functions
'to/preoutput-turn-buffer-read-only))
(use-package desktop
:disabled
:hook (after-init-hook . desktop-read)
:init (setq desktop-restore-eager 8
desktop-auto-save-timeout 120)
(desktop-save-mode t))
(use-package exec-path-from-shell
:when (memq window-system '(mac ns x))
:init (setq exec-path-from-shell-arguments nil)
:config (exec-path-from-shell-initialize))
(use-package savehist
:init (savehist-mode +1)
:custom
(savehist-additional-variables '(search-ring regexp-search-ring))
(savehist-autosave-interval 60))
(use-package recentf
:bind (("<f6>" . counsel-recentf))
:custom
(recentf-max-saved-items 2000)
:init
(recentf-mode +1)
:config
(to/append-to-list 'recentf-exclude '("/elpa/"
"company-statistics-cache.el"
"bookmarks.em" ".mc-lists.el"
"custom.el"
no-littering-etc-directory
no-littering-var-directory)))
(use-package saveplace
:init (setq save-place-file (concat user-emacs-directory "places"))
:config (save-place-mode))
(use-package uniquify
:straight (:type built-in)
:config (setq uniquify-buffer-name-style 'forward))
;;; Visuals
;; (use-package all-the-icons)
(use-package doom-modeline
:init
(doom-modeline-mode 1)
(set-face-attribute 'mode-line nil :underline nil)
:config
(set-face-attribute 'mode-line nil :underline nil)
(when (not (memq window-system '(w32))) (nerd-icons-install-fonts t)))
;; (use-package emojify
;; :hook (after-init . global-emojify-mode)
;; :config
;; (defun to/set-emoji-font (frame)
;; "Adjust the font settings of FRAME so Emacs can display emoji properly."
;; (if (eq system-type 'darwin)
;; (set-fontset-font t 'symbol (font-spec :family "Apple Color Emoji") frame 'prepend)
;; (set-fontset-font t 'symbol (font-spec :family "Symbola") frame 'prepend)))
;; (to/set-emoji-font nil)
;; (add-hook 'after-make-frame-functions 'to/set-emoji-font))
;; (use-package font-lock
;; :straight (:type built-in)
;; :custom
;; (font-lock-multiline t)
;; :hook (((emacs-lisp-mode lua-mode) . font-lock-mode)))
(use-package global-display-line-numbers-mode
:straight (:type built-in)
:commands global-display-line-numbers-mode
:custom
(display-line-numbers-major-tick 50)
(display-line-numbers-minor-tick 10)
:init
(global-display-line-numbers-mode 1)
(dolist (mode '(org-mode-hook
term-mode-hook
shell-mode-hook
treemacs-mode-hook
eshell-mode-hook))
(add-hook mode (lambda () (display-line-numbers-mode 0)))))
(use-package hl-line
:demand t
:init (global-hl-line-mode +1)
:config
(add-hook 'org-mode-hook (lambda () (setq-local global-hl-line-mode nil))))
(use-package paren
:commands show-paren-mode
:init
(show-paren-mode +1))
(use-package rainbow-delimiters)
(use-package rainbow-mode :config (add-hook 'prog-mode-hook #'rainbow-mode))
(use-package rotate)
(use-package smooth-scrolling)
(use-package solarized-theme
:demand t
:custom (frame-background-mode 'light)
:config
(load-theme 'solarized-light t)
(enable-theme 'solarized-light))
(defun dw/org-mode-visual-fill ()
(setq visual-fill-column-width 110
visual-fill-column-center-text t)
(visual-fill-column-mode 1))
(use-package visual-fill-column
:defer t
:hook (org-mode . dw/org-mode-visual-fill))
(use-package treemacs
:defer t
:config
(progn
(setq treemacs-collapse-dirs (if treemacs-python-executable 3 0)
treemacs-deferred-git-apply-delay 0.5
treemacs-directory-name-transformer #'identity
treemacs-display-in-side-window t
treemacs-eldoc-display t
treemacs-file-event-delay 5000
treemacs-file-extension-regex treemacs-last-period-regex-value
treemacs-file-follow-delay 0.2
treemacs-file-name-transformer #'identity
treemacs-follow-after-init t
treemacs-expand-after-init t
treemacs-git-command-pipe ""
treemacs-goto-tag-strategy 'refetch-index
treemacs-indentation 2
treemacs-indentation-string " "
treemacs-is-never-other-window nil
treemacs-max-git-entries 5000
treemacs-missing-project-action 'ask
treemacs-move-forward-on-expand nil
treemacs-no-png-images nil
treemacs-no-delete-other-windows t
treemacs-project-follow-cleanup nil
treemacs-persist-file (expand-file-name ".cache/treemacs-persist" user-emacs-directory)
treemacs-position 'left
treemacs-read-string-input 'from-child-frame
treemacs-recenter-distance 0.1
treemacs-recenter-after-file-follow nil
treemacs-recenter-after-tag-follow nil
treemacs-recenter-after-project-jump 'always
treemacs-recenter-after-project-expand 'on-distance
treemacs-litter-directories '("/node_modules" "/.venv" "/.cask")
treemacs-show-cursor nil
treemacs-show-hidden-files t
treemacs-silent-filewatch nil
treemacs-silent-refresh nil
treemacs-sorting 'alphabetic-asc
treemacs-select-when-already-in-treemacs 'move-back
treemacs-space-between-root-nodes t
treemacs-tag-follow-cleanup t
treemacs-tag-follow-delay 1.5
treemacs-text-scale nil
treemacs-user-mode-line-format nil
treemacs-user-header-line-format nil
treemacs-wide-toggle-width 70
treemacs-width 35
treemacs-width-increment 1
treemacs-width-is-initially-locked t
treemacs-workspace-switch-cleanup nil)
;; The default width and height of the icons is 22 pixels. If you are
;; using a Hi-DPI display, uncomment this to double the icon size.
(treemacs-resize-icons 44)
(treemacs-follow-mode t)
(treemacs-filewatch-mode t)
(treemacs-fringe-indicator-mode 'always)
(pcase (cons (not (null (executable-find "git")))
(not (null treemacs-python-executable)))
(`(t . t)
(treemacs-git-mode 'deferred))
(`(t . _)
(treemacs-git-mode 'simple)))
(treemacs-hide-gitignored-files-mode nil))
:bind
(:map global-map
("M-0" . treemacs-select-window)
("C-x t 1" . treemacs-delete-other-windows)
("C-x t t" . treemacs)
("C-x t B" . treemacs-bookmark)
("C-x t C-t" . treemacs-find-file)
("C-x t M-t" . treemacs-find-tag)))
(use-package treemacs-projectile
:after (treemacs projectile))
(use-package treemacs-icons-dired
:hook (dired-mode . treemacs-icons-dired-enable-once))
(use-package treemacs-magit
:after (treemacs magit))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; General utility
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package alert
:commands alert
:config
(setq alert-default-style 'notifications))
;;; General purpose code tools
(use-package dap-mode
:commands dap-mode
;; Uncomment the config below if you want all UI panes to be hidden by default!
;; :custom
;; (lsp-enable-dap-auto-configure nil)
;; :config
;; (dap-ui-mode 1)
:commands dap-debug
:init
(use-package treemacs)
:config
;; (require 'dap-node)
;; (dap-node-setup)
(require 'dap-go)
(dap-go-setup)
(require 'dap-hydra)
(require 'dap-gdb-lldb)
(dap-gdb-lldb-setup)
(dap-register-debug-template "Rust::GDB Run Configuration"
(list :type "gdb"
:request "launch"
:name "GDB::Run"
:gdbpath "rust-gdb"
:target nil
:cwd nil))
:bind (:map lsp-mode-map
("<f5>" . dap-debug)
("M-<f5>" . dap-hydra)))
(use-package dumb-jump)
(defun ts/lsp-mode-setup-completion ()
(setf (alist-get 'styles (alist-get 'lsp-capf completion-category-defaults))
'(orderless))) ;; Configure orderless
(use-package lsp-mode
:custom
(lsp-eslint-download-url "https://github.com/emacs-lsp/lsp-server-binaries/blob/master/dbaeumer.vscode-eslint-2.2.2.vsix?raw=true")
:hook (((rust-mode go-mode) . lsp-mode)
((rust-mode) . lsp-lens-mode)
(lsp-completion-mode . ts/lsp-mode-setup-completion)
(lsp-mode . corfu-mode)
(lsp-mode . (lambda ()
(setq flycheck-local-checkers
'((lsp .
((next-checkers
. (typos)))))))))
:commands (lsp ls-deferred)
:custom
(lsp-completion-provider :none)
:init
(use-package lsp-treemacs :after lsp)
(use-package lsp-ivy :after lsp)
(use-package lsp-ui
:commands lsp-ui-mode
:bind-keymap ("C-c C-l" . lsp-command-map)
:config
(setq lsp-ui-sideline-enable t
lsp-ui-flycheck-enable t
lsp-ui-flycheck-list-position 'right
lsp-ui-flycheck-live-reporting t)
(setq lsp-go-env (make-hash-table :test 'equal))
(puthash "GOPROXY" "https://proxy.golang.org,direct" lsp-go-env))
:config
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]target\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]cargo\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]node_modules\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]\\.venv\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]third-party\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]bazel-bin\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]bazel-out\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]bazel-src\\'")
(add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]bazel-testlogs\\'")
(setq lsp-disabled-clients '(rls)
lsp-headerline-breadcrumb-enable nil
lsp-headerline-breadcrumb-icons-enable nil
lsp-prefer-flymake nil
lsp-idle-delay 0.500
lsp-ui-doc-use-childframe t)
(lsp-enable-which-key-integration t))
(use-package delight)
(defun bury-compile-buffer-if-successful (buffer string)
"Bury a compilation
buffer if succeeded without warnings "
(if (and (string-match "compilation"
(buffer-name
buffer))
(string-match "finished" string)
(not (with-current-buffer buffer (search-forward "warning" nil t))))
(run-with-timer 1 nil (lambda (buf)
(bury-buffer buf)
(switch-to-prev-buffer (get-buffer-window buf) 'kill)) buffer)))
(add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful)
(use-package editorconfig)
;;
;; General input modes
;;
(global-subword-mode +1)
(use-package elec-pair
:commands elec-pair-mode
:hook (prog-mode . electric-pair-mode)
:demand t
:config (electric-pair-mode +1))
(use-package multiple-cursors
:bind (("M-ö" . mc/edit-lines)
("C-M-ö" . mc/edit-ends-of-lines)
("M-Ö" . mc/edit-beginnings-of-lines)
("M-ä" . mc/mark-all-dwim)
("C-<" . mc/mark-previous-like-this)
("C->" . mc/mark-next-like-this)
("C-S-ä" . mc/mark-more-like-this-extended)
("M-å" . mc/mark-all-in-region))
:config
(unsupported-cmd isearch-forward-use-region ".")
(unsupported-cmd isearch-backward-use-region "."))
(use-package which-key
:init (which-key-mode 1)
:config (setq which-key-idle-delay 0.3))
(use-package cascadia-code
:straight (:type built-in)
:hook (prog-mode . cascadia-code-mode))
(use-package projectile
:diminish t
:config (projectile-mode)
:defer 10
:custom ((projectile-completion-system 'ivy))
:bind-keymap ("C-c p" . projectile-command-map)
:init
(use-package ibuffer-projectile
:hook (ibuffer . (lambda ()
(ibuffer-projectile-set-filter-groups)
(unless (eq ibuffer-sorting-mode 'alphabetic)
(ibuffer-do-sort-by-alphabetic)))))
;; (use-package counsel-projectile
;; :after projectile
;; :config (counsel-projectile-mode))
:config
(setq projectile-switch-project-action 'magit-status
projectile-globally-ignored-directories (append '("*__pycache__*" "*.egg-info") projectile-globally-ignored-directories)
projectile-globally-ignored-file-suffixes (append '(".pyc") projectile-globally-ignored-file-suffixes)
projectile-indexing-method 'alien
projectile-enable-caching 't
projectile-git-command "fd . -0"
projectile-git-submodule-command "git submodule --quiet foreach 'echo $path'")
(projectile-mode +1)
(projectile-global-mode 1))
(defun untabify-buffer ()
(interactive)
(untabify 1 (point-max))
(if (not (eq major-mode 'mew-draft-mode))
;; delete-trailing-whitespace does not work in mew-draft-mode.
(delete-trailing-whitespace)))
(defun rename-current-buffer-file ()
"Renames current buffer and file it is
visiting."
(interactive)
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not (and filename
(file-exists-p filename)))
(error
"Buffer '%s' is not visiting a file!"
name)
(let ((new-name (read-file-name "New name: " filename)))
(if (get-buffer new-name)
(error
"A buffer named '%s' already exists!"
new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory
new-name)))))))
(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer.
Including indent-buffer, which should not be called automatically on save."
(interactive)
(untabify-buffer)
(delete-trailing-whitespace))
(defun transpose-params ()
"Presumes that params are in the form (p, p, p) or
{p, p, p} or [p, p, p]"
(interactive)
(let* ((end-of-first (cond ((looking-at ", ")
(point))
((and
(looking-back ",")
(looking-at " "))
(- (point) 1))
((looking-back ", ")
(- (point) 2))
(t
(error
"Place point between params to transpose."))))
(start-of-first (save-excursion (goto-char end-of-first)
(move-backward-out-of-param)
(point)))
(start-of-last (+ end-of-first 2))
(end-of-last (save-excursion (goto-char start-of-last)
(move-forward-out-of-param)
(point))))
(transpose-regions start-of-first end-of-first start-of-last end-of-last)))
(defun isearch-forward-use-region ()
(interactive)
(when (region-active-p)
(add-to-history 'search-ring (region-as-string))
(deactivate-mark))
(call-interactively 'isearch-forward))
(defun isearch-backward-use-region ()
(interactive)
(when (region-active-p)
(add-to-history 'search-ring (region-as-string))
(deactivate-mark))
(call-interactively 'isearch-backward))
(defun check-expansion ()
(save-excursion (if (looking-at "\\_>") t (backward-char 1)
(if (looking-at "\\.") t (backward-char 1)
(if (looking-at "->") t nil)))))
(defun to/kill-other-buffers
(&optional
kill-special)
"Kill buffers that do not belong to a `projectile' project.
With prefix argument (`C-u'), also kill the special buffers."
(interactive "P")
(let ((bufs (buffer-list (selected-frame))))
(dolist (buf bufs)
(with-current-buffer buf (let ((buf-name (buffer-name buf)))
(when (or (null (projectile-project-p))
(and kill-special
(string-match "^\*" buf-name)))
;; Preserve buffers with names starting with *scratch or
;; *Messages
(unless (string-match "^\\*\\(\\scratch\\|Messages\\)" buf-name)
(message "Killing buffer %s" buf-name)
(kill-buffer buf))))))))
(defun to/close-older-buffers ()
;; Run in interactive
(interactive)
;; let bufs -> reverse buffer list (oldest first)
;; numbufs -> number of buffs in buffer-list
(let ((bufs (reverse (buffer-list (selected-frame))))
(numbufs (length (buffer-list (selected-frame)))))
;; for each buffer
(dolist (buf bufs)
;; if there's more than 10 buffers in list
(if (> numbufs 10)
;; let current buffer be buf
(with-current-buffer buf (progn
;; extract the buffer-name for easier use
(setq buf-name (buffer-name buf))
;; if buffer-name begins with star
(if (string-match "\*" buf-name)
;; reduce remaining buffers by 1
(setq numbufs (- numbufs 1))
;; else
(progn
;; if modified, prompt before killing
(if (buffer-modified-p buf)
(if (y-or-n-p (concat "Kill unsaved
buffer " buf-name
"?"))
;; if yes
(progn
;; kill and reduce ounter
(kill-buffer buf)
(setq numbufs (- numbufs 1))))
;; else kill directly and reduce counter
(progn (kill-buffer)
(setq numbufs (- numbufs 1))))))))))))
(defun create-scratch-buffer nil
"create a scratch buffer"
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*"))
(lisp-interaction-mode))
(defun write-region-delete-and-open(start end filename)
"function takes current
region, and writes it to specified file"
(interactive "r\nFFilename: ")
(write-region start end filename t)
(kill-region start end))
(defun random-uuid ()
"Insert a UUID. This uses a simple hashing of variable data.
Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d
Note: this code uses https://en.wikipedia.org/wiki/Md5"
(interactive)
(let ((myStr (md5 (format "%s%s%s%s%s%s%s%s%s%s" (user-uid)
(emacs-pid)
(system-name)
(user-full-name)
(current-time)
(emacs-uptime)
(garbage-collect)
(buffer-string)
(random)
(recent-keys)))))
(insert (format "%s-%s-4%s-%s%s-%s" (substring myStr 0 8)
(substring myStr 8 12)
(substring myStr 13 16)
(format "%x" (+ 8 (random 4)))
(substring myStr 17 20)
(substring myStr 20 32)))))
(defun* yank-arg () "Yanks the arg at point, abiding by the syntax-table of the
current mode"
(interactive)
(let (bounds start end)
(setq bounds (bounds-of-thing-at-point 'symbol))
(if bounds (progn
(setq start (car bounds))
(setq end (cdr bounds)))
(cond ((eq (char-syntax (char-after)) ?\()
(progn
(setq start (point))
(forward-sexp)
(setq end (point))))
((eq (char-syntax (char-before)) ?\()
(progn (backward-char)
(setq start (point))
(forward-sexp)
(setq end (point))))
((eq (char-syntax (char-before)) ?\))
(progn
(setq end (point))
(backward-sexp)
(setq start (point))))
(t (progn (message "No arg at point to yank!")
(return-from yank-arg)))))
(goto-char end)
(cond ((eq (char-syntax (char-after)) ?\() ; Followed by a sexp
(progn (forward-sexp)
(setq end (point))))
((looking-at "[ ,]") ; Whitespace or comma - zap until next symbol
(while (looking-at "[ ,]")
(forward-char))
(setq end (point))))
(goto-char start)
(while (looking-back "[[:space:],\n]")
(backward-char))
(setq start (point))
(kill-region start end)))
(defun toggle-maximize-buffer ()
"Maximize buffer"
(interactive)
(if (= 1 (length (window-list)))
(jump-to-register '_)
(progn (window-configuration-to-register '_)
(delete-other-windows))))
(defun move-forward-out-of-param ()
(while (not (looking-at ")\\|, \\| ?}\\| ?\\]"))
(cond ((point-is-in-string-p)
(move-point-forward-out-of-string))
((looking-at "(\\|{\\|\\[")
(forward-list))
(t (forward-char)))))
(defun move-backward-out-of-param ()
(while (not (looking-back "(\\|, \\|{ ?\\|\\[ ?"))
(cond ((point-is-in-string-p)
(move-point-backward-out-of-string))
((looking-back ")\\|}\\|\\]")
(backward-list))
(t (backward-char)))))
(defun current-quotes-char ()
(nth 3 (syntax-ppss)))
(defalias 'point-is-in-string-p 'current-quotes-char)
(defun to/regen-rusty-tags-projectile ()
"generate tags files for all rust
projects below the dominating .projectile file"
(interactive)
(start-process "rusty-tags" ;; process-name
"*rusty-tags*" ;; buffer-name
"rusty-tags" ;; executable name
;; rest = args
"-o" "-s" (expand-file-name (locate-dominating-file
default-directory
".projectile"))
;; generate for emacs
"emacs"))
(defun wsl-copy-region-to-clipboard (start end)
"Copy region to Windows clipboard."
(interactive "r")
(call-process-region start end "clip.exe" nil 0))
(define-key global-map (kbd "M-o") 'wsl-copy-region-to-clipboard)
(defun toggle-camelcase-underscores ()
"Toggle between camelcase and underscore
notation for the symbol at point."
(interactive)
(save-excursion (let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (car bounds))
(end (cdr bounds))
(currently-using-underscores-p (progn (goto-char start)
(re-search-forward "_" end t))))
(if currently-using-underscores-p (progn (upcase-initials-region start end)
(replace-string "_" "" nil start end)
(downcase-region start (1+ start)))
(replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
(downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
;; C, C++, Ardunio
(add-to-list 'auto-mode-alist '("\\.as$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.h$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.hpp$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.cpp$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.inl$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.c$" . c-mode))
(add-to-list 'auto-mode-alist '("\.ino$" . arduino-mode))
;; (add-to-list 'auto-mode-alist '("\.rs$" . rust-mode))
;; WEB
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.hbs" . web-mode))
(add-to-list 'auto-mode-alist '("\\.svelte" . web-mode))
(add-to-list 'auto-mode-alist '("/mutt" . mail-mode))
;; OTHER MODES
(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;; (add-to-list 'auto-mode-alist '("\\.el\\'" . emacs-lisp-mode))
(add-to-list 'auto-mode-alist '("\\.js$" . js-mode))
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'auto-mode-alist '("\\.launch\\'" . xml-mode))
;; Work
(add-to-list 'auto-mode-alist '("\\.build\\'" . nxml-mode))
(add-to-list 'auto-mode-alist '("meson.build\\'" . meson-mode))
(defvar auto-minor-mode-alist ()
"Alist of filename patterns vs correpsonding minor mode functions, see `auto-mode-alist'
All elements of this alist are checked, meaning you can enable multiple minor modes for the same regexp.")
(defun enable-minor-mode-based-on-extension ()
"Check file name against `auto-minor-mode-alist' to enable minor modes
the checking happens for all pairs in auto-minor-mode-alist"
(when buffer-file-name
(let ((name (file-name-sans-versions buffer-file-name))
(remote-id (file-remote-p buffer-file-name))
(case-fold-search auto-mode-case-fold)
(alist auto-minor-mode-alist))
;; Remove remote file name identification.
(when (and (stringp remote-id)
(string-match-p (regexp-quote remote-id) name))
(setq name (substring name (match-end 0))))
(while (and alist (caar alist) (cdar alist))
(if (string-match-p (caar alist) name)
(funcall (cdar alist) 1))
(setq alist (cdr alist))))))
(add-hook 'find-file-hook #'enable-minor-mode-based-on-extension)
(add-to-list 'auto-minor-mode-alist '("\\.svelte\\'" . lsp-mode))
;;
;; magit
;;
(use-package magit
:defer t
:bind (("C-x g" . magit-status))
:bind (:map magit-status-mode-map ( "q" . magit-quit-session))
:init
(use-package magit-filenotify
:if (not (memq window-system '(w32)))
:hook (magit-status-mode . magit-filenotify-mode))
;; (use-package magit-todos
;; :hook (magit-status-mode . magit-todos-mode))
:config
(setq magit-todos-nice nil)
(custom-set-faces '(magit-diff-added ((t (:background "black" :foreground "green3"))))
'(magit-diff-removed ((t (:background "black" :foreground "red3"))))))
(defadvice magit-status (around magit-fullscreen activate)
(window-configuration-to-register :magit-fullscreen)
ad-do-it
(delete-other-windows))
(defun magit-quit-session ()
"Restores the previous window configuration and kills the magit buffer"
(interactive)
(kill-buffer)
(jump-to-register :magit-fullscreen))
;; flycheck
;; flycheck-clang-tidy
;; flycheck-golangci-lint
;; flycheck-inline
;; flycheck-irony
;; flycheck-pos-tip
;; flycheck-rust
;; flymake-gjshint
;; flymake-rust
;; flymake-sass
(defun to/adjust-flycheck-automatic-syntax-eagerness ()
"Adjust how often we check for errors based on if there are any.
This lets us fix any errors as quickly as possible, but in a
clean buffer we're an order of magnitude laxer about checking."
(setq flycheck-idle-change-delay (if flycheck-current-errors 0.5 30.0)))
(defun flycheck-handle-idle-change ()
"Handle an expired idle time since the last change.
This is an overwritten version of the original
flycheck-handle-idle-change, which removes the forced deferred.
Timers should only trigger inbetween commands in a single
threaded system and the forced deferred makes errors never show
up before you execute another command."
(flycheck-clear-idle-change-timer)
(flycheck-buffer-automatically 'idle-change))
(use-package flycheck
:hook ((cc-mode python-mode rust-mode go-mode) . flycheck-mode)
:diminish t
:commands flycheck-mode
:custom
(flycheck-check-syntax-automatically '(save idle-change mode-enabled))
(flycheck-disabled-checkers '(python-pylint))
:init
(defvar-local flycheck-local-checkers nil)
(defun +flycheck-checker-get(fn checker property)
(or (alist-get property (alist-get checker flycheck-local-checkers))
(funcall fn checker property)))
(advice-add 'flycheck-checker-get :around '+flycheck-checker-get)
(use-package flycheck-clang-tidy
:hook (cc-mode . flycheck-clang-tidy-setup)
:after flycheck)
(use-package flycheck-pos-tip
:hook (flycheck-mode . flycheck-pos-tip-mode)
:config (custom-set-variables '(flycheck-display-errors-function