-
Notifications
You must be signed in to change notification settings - Fork 1
/
.emacs
1860 lines (1660 loc) · 69.6 KB
/
.emacs
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
;;; -*- lexical-binding: t -*-
;; NOTE: this file is accompanied by .emacs.d/early-init.el, many vars are set there
;;;; Installation of packages required by this config:
;; It's unfortunately a bit involved, but not too much.
;;
;; 1. In `.emacs' remove `:defer t', so all packages required by use-package would be
;; searched immediately. You'll restore them later
;; 2. Insert at the top of .emacs:
;; (require 'use-package-ensure)
;; (setq use-package-always-ensure t)
;; then start emacs.
;; Allows in a case of an ∞ loop send with «killall -SIGUSR1 emacs» to break it
(setq debug-on-event 'sigusr1)
(define-key special-event-map [sigusr2]
(lambda () (interactive)
(save-some-buffers t)
(kill-emacs)))
(setq
compile-command "ninja -C build"
;; * `interactive-only' warns about replace-regexp, and I tried rewriting this
;; function in terms of others — the simple loop they documented is not what I get.
;; * `docstring' works around poorly designed behavior of single quotes
;; https://emacs.stackexchange.com/questions/73047/emacs-29-docstring-single-quote-escaping-rules-compiler-level-event#comment135034_73048
byte-compile-warnings '(not interactive-only docstrings))
;; Turn off the bars
(tool-bar-mode -1)
(menu-bar-mode -1)
(toggle-indicate-empty-lines) ;; visually indicate where a file ends
(bind-key "C-x C-c" nil) ;; I never use it, but do accidentally press
(bind-key "C-x s" 'save-buffer) ;; I accidentally press it instead of C-x C-s
(bind-key "<f1>" nil) ; am not using it in Emacs, but do globally for recording
(add-to-list 'load-path "~/.emacs.d/lisp")
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "Ubuntu Mono" :foundry "unknown" :slant normal :weight normal :height 95 :width normal))))
;; make some evil-goggle colors to look fancier than the default boring gray
'(evil-goggles-change-face ((t (:background "#ffcccc"))))
'(evil-goggles-delete-face ((t (:background "#ffcccc"))))
'(evil-goggles-paste-face ((t (:background "#bbffbb"))))
'(evil-goggles-undo-redo-add-face ((t (:background "#bbffbb"))))
'(evil-goggles-undo-redo-change-face ((t (:background "#bbffbb"))))
'(evil-goggles-undo-redo-remove-face ((t (:background "#bbffbb"))))
'(evil-goggles-yank-face ((t (:background "#ffff55"))))
'(evil-goggles-indent-face ((t (:background "light blue"))))
'(fixed-pitch ((t (:inherit default)))) ; isn't inherited by default
'(font-lock-comment-face ((t (:foreground "dim gray" :slant italic :weight bold))))
'(font-lock-doc-face ((t (:foreground "blue"))))
'(font-lock-doc-string-face ((t (:foreground "medium blue" :slant italic))))
'(font-lock-function-name-face ((t (:foreground "magenta"))))
'(font-lock-keyword-face ((t (:foreground "blue"))))
'(font-lock-preprocessor-face ((t (:inherit font-lock-builtin-face :underline t))))
'(font-lock-type-face ((t (:foreground "ForestGreen"))))
'(fringe ((t (:background "grey95" :foreground "blue"))))
'(line-number ((t (:inherit (shadow default)))))
'(mode-line ((t (:background "light yellow" :foreground "dim gray"))))
'(mode-line-inactive ((t (:background "dim gray" :foreground "white"))))
'(region ((t (:background "gray")))))
;; >> Dynamic font calculation based on current screen size
(defun my-best-font-size (screen-height-mm)
"Calculates comfortable font size for the screen that Emacs is on"
(cond
((<= screen-height-mm 250) 105)
(t 95)))
(defvar my-last-screen-height (make-hash-table :test 'equal))
(defun my-set-best-font-size ()
"Sets font size based on the screen height. Differently sized screens may
have same resolution, so we're interested in height millimeters."
(let ((curr-screen-height (nth 2 (assoc 'mm-size (frame-monitor-attributes))))
(last-screen-height (gethash (selected-frame) my-last-screen-height)))
(when (and curr-screen-height ; terminal would have it equal nil
(/= (or last-screen-height 0) curr-screen-height))
(set-face-attribute 'default (selected-frame) :height (my-best-font-size curr-screen-height))
(puthash (selected-frame) curr-screen-height my-last-screen-height))))
;; << Dynamic font calculation based on current screen size
(defalias 'ar 'align-regexp)
(defalias 'ss 'server-start)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(company-dabbrev-downcase nil)
'(custom-safe-themes
'("e9460a84d876da407d9e6accf9ceba453e2f86f8b86076f37c08ad155de8223c" "a1289424bbc0e9f9877aa2c9a03c7dfd2835ea51d8781a0bf9e2415101f70a7e" "54a63c60d03a025672ad021381a8bf96788c045908593d535fadb3695fd852c6" default))
'(inhibit-startup-screen t)
'(package-selected-packages
'(yasnippet-snippets evil-surround text-mode sp-sublimetext-like smartparens-config emvil autorevert avy evil-goggles undo-fu xr aggressive-fill-paragraph lsp-mode symbol-overlay evil evil-magit magit racer ## smex async go-mode winum company-ngram flycheck-rust php-mode htmlize csharp-mode meson-mode surround ess minizinc-mode rainbow-delimiters atom-dark-theme highlight-numbers color-identifiers-mode company-anaconda anaconda-mode markdown-mode yasnippet smartparens slime pretty-symbols paredit lua-mode idomenu highlight-parentheses helm-company emms ctypes company-c-headers cmake-mode autopair))
'(semantic-imenu-bucketize-file nil)
'(semantic-imenu-bucketize-type-members nil)
'(semantic-imenu-buckets-to-submenu nil)
'(send-mail-function 'smtpmail-send-it)
'(smtpmail-smtp-server "smtp.yandex.com")
'(smtpmail-smtp-service 25))
(defun add-list-to-list (dst src)
"Similar to `add-to-list', but accepts a list as 2nd argument"
(set dst
(append (eval dst) src)))
(defun my-tramp-root-mode-line-indicator ()
"Add a red 'root' indicator to the mode line when editing as tramp-root."
(when (and buffer-file-name (string-match "^/sudo:" buffer-file-name))
(let ((root-indicator '((:eval (propertize " root " 'face '(:foreground "red"))))))
(setq mode-line-format
(append (list (nth 0 mode-line-format) (nth 1 mode-line-format))
root-indicator
(nthcdr 2 mode-line-format))))))
(add-hook 'find-file-hook 'my-tramp-root-mode-line-indicator)
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(use-package package
:defer t
:config
(push '("melpa" . "https://melpa.org/packages/")
package-archives)
)
(use-package flycheck
:defer t
:init
(setq flycheck-check-syntax-automatically '(save)) ;; I only want it on save
:custom-face
(flycheck-error-list-warning ((t (:inherit warning :foreground "blue"))))
(flycheck-fringe-warning ((t (:inherit warning :foreground "blue"))))
(flycheck-warning ((t (:underline (:color "blue" :style wave)))))
:config
(defun myactions-flycheck-mode-hook ()
(when (and (bound-and-true-p flycheck-mode) ;; flycheck hooks is called upon disabling it
(bound-and-true-p haskell-mode))
(flycheck-haskell-setup)
))
(add-hook 'flycheck-mode-hook 'myactions-flycheck-mode-hook)
(add-to-list 'flycheck-clang-warnings "-Wno-missing-braces")
(add-to-list 'flycheck-clang-args "-frelaxed-template-template-args")
)
(use-package minibuffer
:defer t
:init
;; we want partial-completion to be the default one
(setq completion-styles '(partial-completion basic emacs22)))
(use-package company
:defer nil ;; :bind implies `defer t', override it
:bind ("s-/" . company-complete)
:init
(setq company-minimum-prefix-length 3
company-idle-delay 0.2 ;; delay before completition
)
:bind (:map company-active-map
("C-n" . nil)
("C-p" . nil)
("M-n" . 'company-select-next)
("M-p" . 'company-select-previous)
(:map company-search-map
("C-n" . nil)
("C-p" . nil)
("M-n" . 'company-select-next)
("M-p" . 'company-select-previous)))
:config
(global-company-mode 1)
(add-list-to-list 'company-dabbrev-code-modes
'(c++-mode c-mode php-mode))
)
(use-package company-box
:hook (company-mode . company-box-mode))
(use-package yasnippet
:defer 2 ;; lazy-load after 2 seconds of being idle
:config
(use-package yasnippet-snippets)
(yas-global-mode)
)
(use-package autorevert
:init
(setq
;; Do not break markers in a buffer upon reverting a buffer. Details:
;; https://github.com/magit/magit/issues/4442
revert-buffer-insert-file-contents-function 'revert-buffer-insert-file-contents-delicately
;; For some undocumented reason Emacs uses both inotify and polls on
;; files. That's stupid, just a waste of resources, sure let's avoid that.
auto-revert-avoid-polling t)
:config
(global-auto-revert-mode 1) ;; automatically revert any buffeers, whose files changed on disk
(defun my-on-focus-hook ()
(when (frame-focus-state)
(auto-revert-buffers)
(my-set-best-font-size)))
(add-function :after after-focus-change-function #'my-on-focus-hook)
)
(add-list-to-list 'auto-mode-alist
'(("\\.m$" . octave-mode)
("\\.service\\'" . conf-mode)
("\\.rules\\'" . conf-mode)
("\\PKGBUILD\\'" . sh-mode)
("\\.glade$\\'" . xml-mode)
("\\.mzn\\'" . minizinc-mode)
("[Dd]ockerfile[^.]*$" . dockerfile-mode)
("\\tsx\\'" . tsx-ts-mode)
))
(defun sort-lines-nocase (beg end)
(defvar sort-fold-case nil)
(let ((sort-fold-case t))
(sort-lines nil beg end)))
(defun replace-char-after (character-number replacement)
"Replaces char in the buffer after the `character-number' with `replacement'"
(save-excursion
(goto-char character-number)
(delete-char 1)
(insert-char replacement)))
(defun replace-delimiters (old-closing-char new-opening-char new-closing-char opening-point end-point)
"Replaces delimiters between `opening-point' and the
`end-point'. Note, that the `opening-point' should point to the
opening symbol, thus the function seeks only the closing"
(cl-block replace-delimiters
(let ((closing-point opening-point))
(setq closing-point (+ 1 opening-point))
(while (< closing-point end-point)
(if (eq (char-after closing-point) ?\n) ;;no closing delimiter
(progn
(print "Err: no closing delimiter")
(cl-return-from replace-delimiters nil))
(when (eq (char-after closing-point) old-closing-char)
(progn
(replace-char-after opening-point new-opening-char);;opening delimiter
(replace-char-after closing-point new-closing-char);;closing delimiter
(cl-return-from replace-delimiters (+ 1 closing-point)))))
(setq closing-point (+ closing-point 1))))))
(defun swap-<-and-quote-includes (beg end)
"Swaps in the text between `beg' and `end' the matching «<» and
«>» character to the \" quote, and vice versa. Mainly used
before sorting to swap the order of these characters, next
after the sort to restore the text."
(cl-block swap-<-and-quote-includes
(let ((curr-point beg))
(while (< curr-point end)
(setq curr-point (+ curr-point 1))
;;first check «"»
(if (eq (char-after curr-point) ?\")
(progn
(setq curr-point (replace-delimiters ?\" ?< ?> curr-point end))
(if (eq curr-point nil)
(cl-return-from swap-<-and-quote-includes t)))
;;else if «<»
(if (eq (char-after curr-point) ?<)
(progn
(setq curr-point (replace-delimiters ?\> ?\" ?\" curr-point end))
(if (eq curr-point nil)
(cl-return-from swap-<-and-quote-includes t)))))))))
(defun sort-paragraphs-with-keyword (string)
"Sorts statements with the `string', case insensitive"
(save-excursion
(let (beg end)
(goto-char (point-min))
(while (and (not (looking-at string));;look for includes, if no then
(eq (forward-line 1) 0) ;;go one line down (if not EOF).
))
(setq beg (point))
(while (and (looking-at string)
(eq (forward-line 1) 0)));;to not hang cuz of EOF
(setq end (point))
(sort-lines-nocase beg end)
)))
(defun c-sort-includes ()
"Sorts #include statements"
(interactive)
(save-excursion
(let (beg end orig-content sorted-content)
(goto-char (point-min))
(while (and (not (looking-at "#include "));;look for includes, if no then
(eq (forward-line 1) 0) ;;go one line down (if not EOF).
))
(setq beg (point))
(while (and (looking-at "#include ")
(eq (forward-line 1) 0)));;to not hang cuz of EOF
(setq end (point))
(setq orig-content (buffer-substring-no-properties beg end))
(setq sorted-content (with-temp-buffer
(insert orig-content)
(swap-<-and-quote-includes (point-min) (point-max)) ;;swap characters < and > in includes
(sort-lines-nocase (point-min) (point-max)) ;;sort
(swap-<-and-quote-includes (point-min) (point-max)) ;;swap the characters back
(buffer-string)))
(when (not (string= orig-content sorted-content))
(kill-region beg end)
(insert sorted-content))
)))
(defun csharp-sort-usings ()
"Sorts using statements by length(for csharp))"
(interactive)
(save-excursion
(let (beg end)
(goto-char (point-min))
(while (and (not (looking-at "using "));;look for usings, if no then
(eq (forward-line 1) 0) ;;go one line down (if not EOF).
))
(setq beg (point))
(while (and (looking-at "using ")
(eq (forward-line 1) 0)));;to not hang cuz of EOF
(setq end (point))
(sort-lines-nocase beg end))))
;;Ponification syntax table for c=like lamguages
(defun init-prettify-table-c-like ()
(setq prettify-symbols-alist (list
'("!=" . ?≠)
'(">=" . ?≥)
'("<=" . ?≤)
;;'("null" . ?∅)
;;'("NULL" . ?∅)
;;'("nullptr" . ?∅)
;; '("int" . ?ℤ)
'("..." . ?…)
;; '("float" . ?ℚ);;rational numbers
)))
(defun myactionsfor-c-mode-common-hook ()
(fix-c-style-indentation)
(turn-on-auto-fill) ;;auto fill mode for c modes.
(when (derived-mode-p 'c-mode 'c++-mode 'java-mode 'csharp-mode 'lua-mode)
(init-prettify-table-c-like)
(prettify-symbols-mode 1)
(if (derived-mode-p 'c-mode)
(setq flycheck-clang-language-standard "c11")
(setq flycheck-clang-language-standard "c++17"))
))
(use-package cc-mode
:defer t
:hook (c-mode-common . myactionsfor-c-mode-common-hook)
)
(defun exec-cmd-foreach-backward (regex cmd begin end)
"Executes a command for every match of group #1. Searches
backward, so you can mutate text forward"
(save-excursion
(goto-char end)
(while (re-search-backward regex begin t)
(goto-char (match-beginning 1))
(funcall cmd)
)))
(defun expand-c-args-in-region ()
(interactive)
(let ((re (if (member major-mode '(purescript-mode haskell-mode))
"\\(\\), " ; the style where comma goes first
",\\( \\)")))
(exec-cmd-foreach-backward re 'newline-and-indent
(region-beginning) (region-end))))
(use-package csharp-mode
:defer t
:config
(defun myhook-csharp-mode ()
(flycheck-mode -1) ;; for some reason it lags with C#
(c-set-offset 'innamespace '+)
(make-local-variable 'before-save-hook)
(add-hook 'before-save-hook 'csharp-sort-usings))
:hook (csharp-mode . myhook-csharp-mode)
)
(use-package term
:defer t
:config
(yas-minor-mode -1) ;; useless in term-mode, and causes troubles with <tab>
;; (global-set-key (kbd "<RET>")
;; (lambda ()
;; (comint-truncate-buffer)
;; (term-send-input)
;; ))
)
(use-package avy
:config
(setq avy-case-fold-search nil) ;; make searches case sensitive. Well, at least
;; upcase ones, that as good as avy allows.
)
(use-package lsp-mode
:defer t
:init
(setq lsp-enable-indentation nil ;; I prefer default indentation functional
lsp-enable-on-type-formatting nil ;; don't reformat my code
;; disable "path in project + in class hierarchy" header. Not useful to me.
lsp-headerline-breadcrumb-enable nil
;; don't show signature/docs in the minibuffer. For me it's almost never useful; at
;; the same time, I find annoying that it overrides flycheck messages.
lsp-eldoc-enable-hover nil
;; For these purposes I use symbol-overlay mode instead. Not that am against having
;; the two at the same time, but due to some bug in either clangd or lsp-mode, on
;; rare occasions I get the wrong symbol highlighted. So let's just disable that.
lsp-enable-symbol-highlighting nil
lsp-pylsp-plugins-pydocstyle-enabled nil
lsp-lens-enable nil ;; more doc annotations, disable them
lsp-completion-default-behaviour :insert ;; in foo|buzz complete to foobar|buzz \wout eating buzz
;; even if given python project has typing utterly broken, mypy still gives
;; immensely useful syntax checking that is lacking otherwise with pyls.
lsp-pylsp-plugins-mypy-live-mode nil
lsp-pylsp-plugins-mypy-enabled t
)
(defun evil-lsp-find-definition (_string _position)
(condition-case nil
(lsp-find-definition)
('error nil)
(:success t)))
:config
(defun myactionsfor-lsp-mode-hook ()
(set (make-local-variable 'company-backends)
;; lsp-mode provides company-capf. company-lsp they say not supported, Idk
;; why. Perhaps because last commit was at 2019, so it may be unmaintainted
'(company-capf company-etags company-dabbrev))
)
(add-hook 'lsp-mode-hook 'myactionsfor-lsp-mode-hook)
;; even if given python project has typing utterly broken, mypy still gives
;; immensely useful syntax checking that is lacking otherwise with pyls.
(lsp-register-custom-settings '(("pyls.plugins.pyls_mypy.enabled" t t)
("pyls.plugins.pyls_mypy.live_mode" nil t)))
;; I am forgetting the actual name to apply a "fix available" of clangd, because
;; it's a bit non-intuitive. So let's alias it to somethig clearer
(defalias 'lsp-apply-fix 'lsp-execute-code-action)
)
(use-package emvil ;; my Evil config, in a separate file
:ensure nil)
(use-package ido
:init
(setq-default ido-case-fold t) ;; case insensistivity
(setq ido-enable-flex-matching t) ;; fuzzy match
:config
(ido-mode)
)
(use-package smex
:bind ("M-x" . smex)
:config
(smex-initialize)
)
;; visible whitespace config
;; (setq whitespace-style (list 'face 'tabs 'spaces 'space-before-tab 'empty 'space-mark 'tab-mark))
;; (global-whitespace-mode 1) commented out due to problems with markdown
;; scrolling related changes
(setq mouse-wheel-scroll-amount '(3 ((shift) . 1)) ;; three line at a time
mouse-wheel-progressive-speed nil ;; don't accelerate scroll
mouse-wheel-follow-mouse t ;; scroll window under mouse
scroll-step 2) ;; keyboard scroll two lines at a time
;;A few c-like indentation fixes follows
(setq-default indent-tabs-mode nil)
(setq c-default-stile "stroustrup"
c-basic-offset 4)
(defun fix-c-style-indentation ()
"Fixes indentation for c-like langs"
(c-set-offset 'case-label '+)
(c-set-offset 'innamespace 0);;don't indent namespaces
(c-set-offset 'func-decl-cont 0)
(c-set-offset 'cpp-macro 0 nil)
(c-set-offset 'substatement-open 0) ;; brackets the same level as the statement
(c-set-offset 'statement-case-open 0) ;; last `{' after `switch() { case foo: {` on the same level
(c-set-offset 'brace-list-intro c-basic-offset) ;; enums
(c-set-offset 'inextern-lang 0) ;; extern "C" { … }
(c-set-offset 'inlambda '+) ;; extern "C" { … }
)
(use-package lisp-mode
:defer t
:init
;; the inferior-lisp-program is actually defined by `inf-lisp' but should work
(setq inferior-lisp-program "/usr/bin/sbcl")
:config
(add-hook 'lisp-mode-hook (lambda ()
(turn-on-auto-fill)
))
)
;;Make C-a key to work as the home key in a most code editors
(defun smarter-move-beginning-of-line (arg)
"Move point back to indentation of beginning of line.
Move point to the first non-whitespace character on this line.
If point is already there, move to the beginning of the line.
Effectively toggle between the first non-whitespace character and
the beginning of the line.
If ARG is not nil or 1, move forward ARG - 1 lines first. If
point reaches the beginning or end of the buffer, stop there."
(interactive "^p")
(setq arg (or arg 1))
; Move lines first
(when (/= arg 1)
(let ((line-move-visual nil))
(forward-line (1- arg))))
(let ((orig-point (point)))
(back-to-indentation)
(when (= orig-point (point))
(move-beginning-of-line 1))))
; remap C-a to `smarter-move-beginning-of-line'
(global-set-key [remap move-beginning-of-line]
'smarter-move-beginning-of-line)
(defun clipboard-yank-fixed ()
"A version of clipboard-yank that pastes \"instead\" of selection if one
exists"
(interactive)
(when (use-region-p)
(kill-region (region-beginning) (region-end)))
(let ((select-enable-clipboard t))
(clipboard-yank)))
(bind-key "C-y" 'clipboard-yank-fixed)
(defun clipboard-copy-fixed ()
(interactive)
(let ((select-enable-clipboard t))
(clipboard-kill-ring-save (region-beginning) (region-end))))
(bind-key "M-w" 'clipboard-copy-fixed)
(defun copy-text-to-clipboard (text)
(with-temp-buffer
(insert text)
(clipboard-kill-region (point-min) (point-max))))
(defun copy-obj-to-clipboard (obj)
(copy-text-to-clipboard (prin1-to-string obj)))
(defalias 'obj-to-string #'prin1-to-string)
(defun what-face (pos)
(interactive "d")
(let ((face (or (get-char-property (point) 'read-face-name)
(get-char-property (point) 'face))))
(if face (message "Face: %s" face) (message "No face at %d" pos))))
(defun align-regexp-with-spaces (orig-func BEG END REGEXP &optional GROUP SPACING REPEAT)
"Make align-regexp always align with spaces rather than tabs"
(let ((indent-tabs-mode nil))
(funcall orig-func BEG END REGEXP GROUP SPACING REPEAT)))
(advice-add 'align-regexp :around #'align-regexp-with-spaces)
(defun kmacro-call-macro-no-ding (orig-func arg &optional no-repeat end-macro macro)
"Make mistyped search while recording a macro never break the replay"
(let ((isearch-wrap-pause 'no-ding))
(funcall orig-func arg no-repeat end-macro macro)))
(advice-add 'kmacro-call-macro :around #'kmacro-call-macro-no-ding)
(defun evil-exchange-point-and-mark (orig-func &optional arg)
"Move caret one char to the right before passing control
over. Needed in my case because I use `exchange-point-and-mark'
to select last pasted text, and I usually go to normal mode
before doing that, whic by itself makes caret move one char left"
(when (and (memq evil-state '(visual normal))
(< (point) (point-max)))
(forward-char))
(funcall orig-func arg))
(advice-add 'exchange-point-and-mark :around #'evil-exchange-point-and-mark)
;;I am using only eng and ru layout, and sometimes TeX. So it would be better to use layout toggling
;;only with this two layouts, and don't mess it with TeX or whatever some day come in my mind to try.
(defun input-switch-eng-ru ()
(interactive)
"Swithes an input methods between russian and english.
Works just as the standard «toggle-input-method»(and in fact
calls it), but before checks if an input method one of russian or
english. If it isn't, set the lang to english."
(if (not current-input-method)
(set-input-method "russian-computer")
(deactivate-input-method)))
(bind-key "C-\\" 'input-switch-eng-ru)
(global-set-key (kbd "s-\\") (lambda () (interactive);;sets input method to «TeX»
(set-input-method "TeX")))
(define-key isearch-mode-map "\C-g" nil) ;; I like to interrupt search immediately
(defun improved-newline-and-indent()
"Calls `newline-and-indent' always with an exception of being
called insede of '{}' braces. In this case it throws the braces
in a few lines, and puts the cursor at the middle line"
(interactive "*")
(if (and (eq (char-before) ?{);;if inside "{|}"
(eq (char-after) ?}))
(progn
(when (bound-and-true-p smartparens-mode)
;; smartparens tries to do the same thing as me, but fails at that,
;; because it only works when I just typed the pair. Otherwise it does
;; nothing.
;;
;; So I tried detecting the situation, but it gets worse: sometimes the
;; sp-last-inserted-pair is set, other times it isn't. All in all, the
;; only way to work around this problem seems to be to restart the
;; mode.
(smartparens-mode 0)
(smartparens-mode 1))
(indent-according-to-mode) ;; indent the line
(newline 2) ;; 2 newlines
(indent-according-to-mode) ;; indent the line
(forward-line -1)
(indent-according-to-mode) ;; indent the line
)
(newline-and-indent)))
(bind-key "C-w" 'clipboard-kill-region)
(bind-key "s-y" 'yank)
(use-package idomenu
:bind ("s-i" . idomenu))
(bind-key "<RET>" 'improved-newline-and-indent)
(global-set-key (kbd "<f11>") (lambda () (interactive) (ff-find-other-file nil t))) ;switch between a corresponding c/c++ header and a file
(bind-key "<C-mouse-4>" 'text-scale-decrease);set in wheel font decrease
(bind-key "<C-mouse-5>" 'text-scale-increase);set in wheel font increase
(use-package highlight-numbers
:init
(defun enable-highlight-numbers-mode ()
;; the mode breaks rendering in the following modes. The upstream fix would
;; simple (or not): the
;; `((,regexp . 'highlight-numbers-number))
;; line has to be replaced with
;; `((,regexp . (0 highlight-numbers-number prepend)))
;; or something similar. Unfortunately it makes font-lock fail with "unknown
;; variable highlight-numbers-number" even though it is not not a variable but a
;; facename and the syntax is correct per `font-lock-keywords' Help. That's as
;; much of a motivation I had to debug that.
(unless (derived-mode-p 'eww-mode 'diff-mode)
(highlight-numbers-mode t)))
(define-globalized-minor-mode global-highlight-numbers-mode
highlight-numbers-mode enable-highlight-numbers-mode)
:config
(set-face-attribute 'highlight-numbers-number nil :weight 'bold :foreground "blue" :background "light gray")
(global-highlight-numbers-mode)
)
(column-number-mode)
(delete-selection-mode)
;; setup and enable flyspell mode
(setq-default flyspell-issue-message-flag nil)
;; (dolist (hook '(c-mode-hook))
;; (add-hook hook (lambda ()
;; (ispell-change-dictionary "english")
;; (flyspell-mode 1)
;; )));;enable for c
;; (dolist (hook '(c++-mode-hook))
;; (add-hook hook (lambda ()
;; (ispell-change-dictionary "english")
;; (flyspell-mode 1)
;; )));;enable for c++
(defun is-in-comment ()
"tests if point is in comment"
(nth 4 (syntax-ppss)))
(defun current-line-string ()
"returns current line as a string"
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
(use-package smartparens-config
:ensure nil
:init
(setq-default sp-autoskip-closing-pair t) ;; skip only when pair is active
(setq sp-show-pair-from-inside t
sp-escape-quotes-after-insert nil ;; https://github.com/Fuco1/smartparens/issues/783#issuecomment-324598759
sp-python-insert-colon-in-function-definitions t)
:config
(use-package sp-sublimetext-like :ensure nil) ;; sublime-like behavior of smartparens
(smartparens-global-mode 1)
(show-smartparens-global-mode 1)
;; I use smartparens which can highlight matching pairs, so I don't need
;; emacs's default blink parenthesis functionality
(setq blink-paren-function nil)
(defun maybe-add-semicolon-paren (_id action _context)
"A helper function that inserts semicolon after closing
parentheses when appropriate. Mainly useful in C, C++, and other
languages with similar syntax"
;; here, caret supposed to be in between parens, i.e. (|)
(when (and (eq action 'insert)
(looking-at ")\\s-*$")
(not (is-in-comment))
(not (string-match-p
(regexp-opt '("if" "else" "switch" "for" "while" "do" "define") 'words)
(current-line-string))))
(save-excursion
(forward-char) ;; skip closing brace
(insert ";"))))
(defun maybe-add-semicolon-paren-rust (_id action _context)
"A helper function that inserts semicolon after closing
parentheses when appropriate, for Rust lang"
;; here, caret supposed to be in between parens, i.e. (|)
(when (and (eq action 'insert)
(looking-at ")\\s-*$")
(not (is-in-comment))
(not (string-match-p
(regexp-opt '("fn" "if" "for" "while") 'words)
(current-line-string))))
(save-excursion
(forward-char) ;; skip closing brace
(insert ";"))))
(defun maybe-add-semicolon-bracket (_id action _context)
"A helper function that inserts semicolon after closing
parentheses when appropriate. Mainly useful in C, C++, and other
languages with similar syntax"
;; here, caret supposed to be in between braces, i.e. {|}
(when (and (eq action 'insert)
(looking-at "}\\s-*$")
(not (is-in-comment))
(string-match-p "\\breturn\\b" (current-line-string)))
(save-excursion
(forward-char) ;; skip closing brace
(insert ";"))))
(defun maybe-complete-lambda (_id action _context)
"Completes C++ lambda, given a pair of square brackets"
(when (eq action 'insert)
(let ((curr-line (current-line-string))
;; try detecting "auto foo = []"
(lambda-assign-regex "=\\s-*\\[\\]$")
;; try detecting "func([])" and "func(arg1, [])"
(lambda-inline-regex "[(,]\\s-*\\[\\]"))
(when (or (string-match-p lambda-assign-regex curr-line)
(string-match-p lambda-inline-regex curr-line))
(save-excursion
;; here, caret supposed to be in between brackets, i.e. [|]
(forward-char) ;; skip closing brace
(insert "() {}")
(when (eolp)
(insert ";"))
)))))
(sp-with-modes '(c-mode c++-mode java-mode csharp-mode vala-mode js-mode)
(sp-local-pair "(" nil :post-handlers '(:add maybe-add-semicolon-paren))
(sp-local-pair "{" nil :post-handlers '(:add maybe-add-semicolon-bracket)))
(sp-local-pair 'c++-mode "[" nil :post-handlers '(:add maybe-complete-lambda))
(sp-local-pair 'rust-mode "(" nil :post-handlers '(:add maybe-add-semicolon-paren-rust))
(defun sp-emacs-style-backtick (_ _ _)
"Text-mode is used for editing the commit messages. Emacs has style where
a backtick ends with a singular quote, so let's check if current dir is
part of Emacs repo, in which case replace the pair that SP inserted."
(when (string-match-p "/emacs/" default-directory)
(save-excursion
(delete-char 1)
(insert "'"))))
(sp-local-pair 'text-mode "`" nil :post-handlers '(:add sp-emacs-style-backtick))
)
;; mode to highlight outside parentheses
(use-package highlight-parentheses
:init
(setq highlight-parentheses-colors nil
highlight-parentheses-background-colors '("light green" "yellow" "orange" "pink"))
:config
(global-highlight-parentheses-mode)
)
(defun large-files-throttling ()
"Disables certain features if file is too large"
(when (> (buffer-size) (* 1024 1024 10))
(highlight-parentheses-mode -1)))
(add-hook 'find-file-hook #'large-files-throttling)
(defun myfunc-before-save-hook ()
(unless (derived-mode-p 'diff-mode)
(delete-trailing-whitespace))
;; (when (derived-mode-p 'c-mode 'c++-mode)
;; (c-sort-includes))
)
(add-hook 'before-save-hook 'myfunc-before-save-hook)
(use-package rust-mode
:defer t
:config
(cl-assert (boundp 'company-backends)) ;; I always use company-mode
(defun myfunc-rust-mode-hook ()
;; note: if racer-mode breaks in some way (e.g. no more completions), do:
;; 1. `rustup component add rust-src` 2. `cargo +nightly install racer --force'
(racer-mode)
(set (make-local-variable 'company-backends)
'(company-capf company-etags company-dabbrev))
)
(add-hook 'rust-mode-hook 'myfunc-rust-mode-hook)
)
(use-package symbol-overlay
:bind ("s-`" . symbol-overlay-put)
:defer nil ;; :bind implies `defer t', override it
:init
(setq symbol-overlay-ignore-functions nil ;; don't ignore keywords in various languages
symbol-overlay-map (make-sparse-keymap)) ;; disable special cmds on overlays
(defun enable-symbol-overlay-mode ()
(unless (or (minibufferp)
(derived-mode-p 'magit-mode)
(derived-mode-p 'xref--xref-buffer-mode))
(symbol-overlay-mode t)))
(define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
symbol-overlay-mode ;; name of the minor mode
enable-symbol-overlay-mode)
:config
(global-symbol-overlay-mode)
)
(defun make-«»-pairs ()
;; TODO: I can make it globally by using (standard-syntax-table) but for some
;; reason pairing it makes it think that ( is a pair to ». Looks like a bug to
;; me. Gotta ask on the mailing list.
(modify-syntax-entry ?« "(»")
(modify-syntax-entry ?» ")«"))
(defun common-hook-for-text-modes ()
(make-«»-pairs)
(setq case-fold-search t) ;; ignore case in search
(set (make-local-variable 'dabbrev-upcase-means-case-search) nil) ;; ignore case
(set (make-local-variable 'company-minimum-prefix-length) 2)
(flyspell-mode))
(use-package text-mode
:defer t
:ensure nil
:hook (text-mode . common-hook-for-text-modes)
)
(defun myfunc-gud-gdb-mode ()
(add-hook 'comint-output-filter-functions 'comint-truncate-buffer)
(company-mode 0)
(local-set-key (kbd "C-d") 'delete-char) ;; gdb rebinds the key
)
(add-hook 'gud-mode-hook 'myfunc-gud-gdb-mode)
(add-hook 'gdb-mode-hook (lambda () (add-hook 'comint-output-filter-functions 'comint-truncate-buffer)))
(put 'erase-buffer 'disabled nil)
(defun latin-to-gothic (φp1 φp2 φreverse-direction-p)
"Replace English alphabets to Unicode gothic characters.
For example, A ⇒ 𝔄, a ⇒ 𝔞.
When called interactively, work on current line or text selection.
If any `universal-argument' is called first, reverse direction.
When called in elisp, the φp1 and φp2 are region begin/end positions to work on.
URL `http://ergoemacs.org/misc/thou_shalt_use_emacs_lisp.html'
Version 2015-04-12"
(interactive
(if (use-region-p)
(progn
(list (region-beginning) (region-end) current-prefix-arg ))
(list (line-beginning-position) (line-end-position) current-prefix-arg )))
(let (
(ξlatin-to-gothic [ ["A" "𝔄"] ["B" "𝔅"] ["C" "ℭ"] ["D" "𝔇"] ["E" "𝔈"] ["F" "𝔉"] ["G" "𝔊"] ["H" "ℌ"] ["I" "ℑ"] ["J" "𝔍"] ["K" "𝔎"] ["L" "𝔏"] ["M" "𝔐"] ["N" "𝔑"] ["O" "𝔒"] ["P" "𝔓"] ["Q" "𝔔"] ["R" "ℜ"] ["S" "𝔖"] ["T" "𝔗"] ["U" "𝔘"] ["V" "𝔙"] ["W" "𝔚"] ["X" "𝔛"] ["Y" "𝔜"] ["Z" "ℨ"] ["a" "𝔞"] ["b" "𝔟"] ["c" "𝔠"] ["d" "𝔡"] ["e" "𝔢"] ["f" "𝔣"] ["g" "𝔤"] ["h" "𝔥"] ["i" "𝔦"] ["j" "𝔧"] ["k" "𝔨"] ["l" "𝔩"] ["m" "𝔪"] ["n" "𝔫"] ["o" "𝔬"] ["p" "𝔭"] ["q" "𝔮"] ["r" "𝔯"] ["s" "𝔰"] ["t" "𝔱"] ["u" "𝔲"] ["v" "𝔳"] ["w" "𝔴"] ["x" "𝔵"] ["y" "𝔶"] ["z" "𝔷"] ])
ξuseMap
)
(if φreverse-direction-p
(progn (setq ξuseMap
(mapcar
(lambda (ξx)
(vector (aref ξx 1) (aref ξx 0)))
ξlatin-to-gothic)))
(progn (setq ξuseMap ξlatin-to-gothic)))
(save-excursion
(save-restriction
(narrow-to-region φp1 φp2)
(let ( (case-fold-search nil))
(mapc
(lambda (ξx)
(goto-char (point-min))
(while (search-forward (elt ξx 0) nil t)
(replace-match (elt ξx 1) 'FIXEDCASE 'LITERAL)))
ξuseMap))))))
(defun text-to-html ()
"In active region quotes a spec. chars like «<» and «&», and
inserts «<br>» tag for line breaks"
(interactive)
(let ((start (region-beginning))
(end (region-end)))
(use-package sgml-mode)
(sgml-quote start end)
(replace-regexp "^\\(.*\\)$" "\\1<br>" nil start end)))
(use-package haskell-mode
:defer t
:config
(defun haskell-sort-n-align-imports ()
"Sorts and aligns Haskell imports"
(interactive)
(save-excursion
(goto-char (point-min))
(while (and (not (looking-at "import "));;look for usings, if no then
(eq (forward-line 1) 0) ;;go one line down (if not EOF).
))
(haskell-sort-imports)))
(defun myhook-haskell-mode ()
(haskell-indent-mode)
(make-local-variable 'before-save-hook)
(add-hook 'before-save-hook 'haskell-sort-n-align-imports))
(add-hook 'haskell-mode-hook 'myhook-haskell-mode)
:hook (haskell-mode . myhook-haskell-mode)
)
(use-package shell
:defer t
:config
(defun myfunc-shell-mode ()
(flycheck-mode 1)
)
:hook (shell-mode . myfunc-shell-mode)
)
(use-package ispell
:defer t
:init
;; It's unclear if the default aspell supports multiple langs at once, but Emacs
;; with aspel backend doesn't. Let's use hunspell instead.
(setq ispell-program-name "hunspell")
(setq flyspell-prog-text-faces '(font-lock-comment-face font-lock-doc-face))
:config
(ispell-set-spellchecker-params) ;; ispell initialization, a mandatory call
(ispell-hunspell-add-multi-dic "en_US,ru_RU")
(ispell-change-dictionary "en_US,ru_RU" t) ;; with t set dict globally
)
(use-package markdown-mode
:defer t
:init
(setq-default markdown-enable-math t) ;; enable latex delimiters
(setq markdown-command "pandoc")
(defun my-markdown-hook ()
(common-hook-for-text-modes)
(setq-local evil-shift-width 2))
:custom-face
(markdown-inline-code-face ((t (:inherit markdown-code-face :background "light blue"))))
:config
(add-hook 'markdown-mode-hook 'my-markdown-hook))
(defun just-one-space-region ()
"Replaces every space in active region between words to one
space and removes newlines (useful e.g. to join arguments in a
function declaration), for inactive region calls
`just-one-space'"
(interactive)
(if mark-active
(progn (replace-regexp "\\s-+" " " nil (region-beginning) (region-end))
(replace-regexp "
" "" nil (region-beginning) (region-end)))
(just-one-space)))
(bind-key "M-<SPC>" 'just-one-space-region)
;; Force gdb-mi to not dedicate any windows
(advice-add 'gdb-display-buffer
:around (lambda (orig-fun &rest r)
(let ((window (apply orig-fun r)))
(set-window-dedicated-p window nil)
window)))
(advice-add 'gdb-set-window-buffer
:around (lambda (orig-fun name &optional ignore-dedicated window)
(funcall orig-fun name ignore-dedicated window)
(set-window-dedicated-p window nil)))