-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1751 lines (1451 loc) · 61.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
;; init.el --- Feng Li's .emacs configurations -*- lexical-binding: t; -*-
;;; Commentary:
;; Copyright: Feng Li <http://feng.li/>
;;
;; Download: https://github.com/feng-li/.emacs.d/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Load all required packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (setq url-proxy-services
;; '(("no_proxy" . "^\\(localhost\\|10\\..*\\|192\\.168\\..*\\)")
;; ("http" . "127.0.0.1:7890")
;; ("https" . "127.0.0.1:7890")))
;; Add path for auto saved files
;;; Code:
(defvar my-base-save-list (concat (getenv "HOME") "/.config/emacs/"))
(defvar my-auto-save-list (concat my-base-save-list (system-name))) ;; host-specified
(unless (file-directory-p my-auto-save-list) (make-directory my-auto-save-list t))
(setq package-archives
'(
;; ("melpa" . "https://melpa.org/packages/")
;; ("elpa" . "https://elpa.gnu.org/packages/")
("elpa-cn" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
("melpa-cn" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
))
;; Local server socket dir. Some server does not allow to use the default
;; (setq server-use-tcp t)
;; Add personal load path recursively in front of the default load path if it exists.
(defvar my-site-lisp (concat user-emacs-directory "site-lisp"))
(if (file-directory-p my-site-lisp)
(let ((default-directory my-site-lisp))
(setq load-path
(append
(let ((load-path (copy-sequence load-path))) ;; Shadow
(append
(copy-sequence (normal-top-level-add-to-load-path '(".")))
(normal-top-level-add-subdirs-to-load-path)))
load-path))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; custom-set-variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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.
'(blink-cursor-mode t)
'(case-fold-search t)
'(column-number-mode t)
'(custom-safe-themes
'("81c3de64d684e23455236abde277cda4b66509ef2c28f66e059aa925b8b12534" default))
'(doc-view-continuous t)
'(global-font-lock-mode t nil (font-lock))
'(highlight-doxygen-commend-start-regexp
"\\(/\\*\\(!\\|\\*[^*]\\)\\|#\\('\\)\\|##\\('\\)\\|//\\(!\\|'\\|/[^/\12]\\)\\)")
'(hl-paren-background-colors '("light gray" "steel blue" "lime green" "orange1"))
'(indicate-empty-lines nil)
'(neo-window-width 40)
'(org-support-shift-select t)
'(package-selected-packages
'(wgrep pdf-tools proxy-mode gptel jinx envrc imenu-list lsp-latex lean-mode treesit-auto writegood-mode multiple-cursors pinyinlib company counsel swiper ivy ht flycheck-languagetool lsp-metals notmuch poly-R visual-fill-column keytar gnu-elpa-keyring-update use-package scala-mode lexic pandoc-mode synosaurus yaml-mode mw-thesaurus unfill powerthesaurus julia-mode neotree format-all adaptive-wrap highlight-doxygen electric-operator elpy markdown-mode dracula-theme yasnippet-snippets flycheck-julia math-symbol-lists polymode company-auctex highlight-symbol popup iedit yasnippet magit ess dash auctex with-editor magit-popup))
'(safe-local-variable-values '((TeX-engine . pdflatex)))
'(save-place-mode t)
'(scroll-bar-mode nil)
'(scroll-conservatively 1)
'(send-mail-function 'mailclient-send-it)
'(show-paren-mode t nil (paren))
'(tool-bar-mode nil)
'(warning-suppress-types '(((tar link)) (comp) (comp) (undo discard-info))))
;; Automatically install emacs packages
(unless (file-directory-p package-user-dir)
(package-refresh-contents)
(package-install-selected-packages)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Basic Preferences
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Personal information
(setq frame-title-format "%b")
(setq user-full-name "Feng Li")
(setq user-mail-address "m@feng.li")
(setq gc-cons-threshold (* 1000 1024 1024)) ; 1000 MB
;; Auto-save list file prefix
(setq auto-save-list-file-prefix (concat my-auto-save-list "/.saves-"))
;; Term
(setenv "TERM" "xterm-256color")
(setenv "COLORTERM" "trueclor") ;; Ensure True Color in Systemd
(add-to-list 'term-file-aliases '("dumb" . "xterm-256color"))
;; Stop displaying strange symbols in place of the desired colored output
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
;; Allow shift-arrow keys and control-arrow keys under different tty
;; Set export TERM="xterm" in .bashrc and
;; term "xterm" in .screenrc.
;; (defadvice terminal-init-xterm (after select-shift-up activate)
;; (define-key input-decode-map "\e[1;2A" [S-up]))
;; Theme
(use-package dracula-theme
:ensure t
:config
(load-theme 'dracula t)
;; Modify background
(unless (display-graphic-p)
(set-face-background 'default "#1d1f21" nil)
)
)
;; (defun on-after-init ()
;; (unless (display-graphic-p (selected-frame))
;; (set-face-background 'default "#1d1f21" (selected-frame))))
;; ;; (set-face-background 'default "#1d1f21" (selected-frame))))
;; (add-hook 'window-setup-hook #'on-after-init)
;; (setq dracula-use-24-bit-colors-on-256-colors-terms t)
;; The scratch settings
(setq initial-scratch-message nil) ;; Disable scratch information
(setq inhibit-startup-message t) ;;stop start up message
;; (setq fundamental-mode 'text-mode)
(setq initial-major-mode 'text-mode) ;; text mode in scratch
(setq major-mode 'text-mode)
;; Suspend and resume hook
(add-hook 'suspend-hook
(function (lambda ()
(or (y-or-n-p
"Really suspend emacs? ")
(error "Suspend canceled")))))
(add-hook 'suspend-resume-hook
(function (lambda () (message "Emacs resumed!"))))
;; TAB settings
(setq-default indent-tabs-mode nil)
;; SAVE MY PINK FINGER
;; Internal swap Ctrl and Alt key. Or use Gnome Tweak "Left Alt as Ctrl, Left Ctrl as Win, Left Win as Alt"
;; (setq x-ctrl-keysym 'alt)
;; (setq x-ctrl-keysym 'meta)
;; (setq x-alt-keysym 'ctrl)
;; (setq x-meta-keysym 'ctrl)
;; Switch to previous buffer with Shift-Tab
(defun switch-to-previous-buffer ()
(interactive)
(switch-to-buffer (other-buffer)))
(global-set-key (kbd "<backtab>") 'switch-to-previous-buffer)
;; Use y-n for short
(fset 'yes-or-no-p 'y-or-n-p)
;; Redraw-display when focused
(global-set-key (kbd "<f5>") 'redraw-display)
;; Set Fonts
(add-to-list 'default-frame-alist '(font . "M PLUS Code Latin 50-11")) ;
;; Menu bar
(menu-bar-mode t)
;; Tooltip mode
(tooltip-mode nil)
;; Global auto revert mode
;; (global-auto-revert-mode t)
;; Better vertical bar
(set-display-table-slot standard-display-table 'vertical-border ?│)
(set-face-background 'vertical-border (face-background 'mode-line))
(set-face-foreground 'vertical-border (face-background 'mode-line))
;; Remove weird ESC ESC key
;; (if (display-graphic-p)
;; (progn
;; ;; Do nothing with window system
;; )
;; (global-unset-key [?\e ?\e ?\e])
;; (global-set-key [?\e ?\e escape] 'keyboard-escape-quit)
;; )
(add-hook 'before-save-hook #'delete-trailing-whitespace)
;; Let Alt key be the meta key
(setq x-alt-keysym 'meta)
;; Mirror "C-x -> C-", and "M-x -> M-,"
(define-key global-map (kbd "C-,") ctl-x-map)
(define-key global-map (kbd "M-,") 'execute-extended-command)
;; Paste behavior
(define-key global-map (kbd "C-S-v") 'yank) ; Shift-Ctr-v to paste, same as terminal behavior
(define-key global-map (kbd "<mouse-3>") 'yank) ; Right click to paste
;; Global visual line mode with better indentation
;; (global-visual-line-mode t)
(setq-default fill-column 120)
(global-set-key (kbd "M-p") 'fill-paragraph) ;; mirror key for M-q
(dolist (hook '(message-mode-hook
prog-mode-hook
org-mode-hook
mail-mode-hook
text-mode-hook))
(add-hook hook #'(lambda ()
(display-line-numbers-mode t)
(setq word-wrap-by-category t) ; Better CJK wrap support
)))
(use-package visual-fill-column
:ensure t
:custom
(visual-fill-column-extra-text-width nil)
(visual-fill-column-width (+ fill-column +6)) ;; fill-column+6
:config
(dolist (hook '(message-mode-hook
;; prog-mode-hook
org-mode-hook
mail-mode-hook
text-mode-hook
reftex-toc-mode-hook))
(add-hook hook #'(lambda ()
(visual-line-mode t)
(visual-fill-column-mode)
)))
)
(use-package adaptive-wrap
:ensure t
:config
(setq-default adaptive-wrap-extra-indent 0)
(add-hook 'visual-line-mode-hook #'adaptive-wrap-prefix-mode)
)
;; shift selection
(setq shift-select-mode t)
;; allow mouse to select
;; (xterm-mouse-mode t)
;; make typing override text selection
(delete-selection-mode 1) ;
;; Cursor is bar: Not clear under console
(setq-default cursor-type 'box)
(setq-default visible-cursor t)
;;set visible-bell
(setq visible-bell t)
;; set big kill ring
(setq kill-ring-max 150)
(global-set-key (kbd "C-k") 'kill-whole-line)
;; auto-fill mode
(dolist (hook '(;;prog-mode-hook
;; text-mode-hook
;; LaTeX-mode-hook
;; markdown-mode-hook
message-mode-hook
org-mode-hook
mail-mode-hook
;; ess-mode-hook
))
(add-hook hook #'(lambda () (auto-fill-mode 1))))
;; Unfilling a region joins all the lines in a paragraph into a single line for each
;; paragraphs in that region. It is the contrary of fill-region.
(use-package unfill
:ensure t
:config
(define-key global-map (kbd "C-M-q") 'unfill-paragraph)
)
;; https://www.reddit.com/r/emacs/comments/kwl0mc/lspdescribethingatpoint_config_improvement/
(add-to-list 'display-buffer-alist
#'((lambda (buffer _) (with-current-buffer buffer
(seq-some (lambda (mode)
(derived-mode-p mode))
'(help-mode))))
(display-buffer-reuse-window display-buffer-below-selected)
(reusable-frames . visible)
(window-height . 0.4)))
;; Security
(setq enable-dir-local-variables t) ;; Trust .dir-locals.el
(add-hook 'text-mode-hook
;; allow file-local variables in Emacs
(lambda () (setq-local enable-local-variables :all)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Global key bindings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; copy with other applications
(setq select-enable-clipboard t)
(global-set-key (kbd "<mouse-2>") 'clipboard-yank)
;; Keybind to browse the kill ring
(global-set-key (kbd "C-c y")
#'(lambda ()
(interactive)
(popup-menu 'yank-menu)))
(setq ring-bell-function (lambda () t))
;; Disable capitalize-key, conflict with tmux prefix ESC.
;; (global-unset-key (kbd "M-c"))
;; Kill the current buffer, without confirmation.
;; Kill buffer without conformation
(global-set-key (kbd "C-x k") 'kill-current-buffer)
;; Bind undo with the common keyboard
(global-set-key (kbd "C-z") 'undo)
;; Unset suspend-frame key
(global-unset-key (kbd "C-x C-z"))
;; Personal global key settings
(global-set-key (kbd "<home>") 'beginning-of-buffer)
(global-set-key (kbd "<end>") 'end-of-buffer)
(global-set-key (kbd "<select>") 'end-of-buffer)
(global-set-key (kbd "<f9> n") 'new-frame)
(global-set-key (kbd "<f9> g") 'rgrep)
(global-set-key (kbd "<f9> f") 'find-name-dired)
(global-set-key (kbd "<f9> q") 'fill-region-as-paragraph)
(global-set-key (kbd "<f9> TAB") 'indent-relative)
(global-set-key (kbd "<f2>") 'next-multiframe-window) ;; Circulate among windows
(global-set-key (kbd "C-x o") 'next-window-any-frame) ;; Circulate among windows
;; Follow mode (dual pages display)
(global-set-key (kbd "C-<f2>") 'follow-delete-other-windows-and-split)
;; Control-tab to switch among buffers
;; (global-set-key (kbd "<backtab>") 'next-buffer)
;; Unset 'exchange-point-and-mark' key which may cause conflicts with Fcitx
(global-unset-key (kbd "C-x C-x"))
;; Key bind to increase and decrease text size
(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
;; (global-set-key (kbd "M-SPC") 'set-mark-command) ;It was C-SPC
;; Insert current time, Linux only?
(global-set-key (kbd "C-c t") 'my-insert-time)
(defun my-insert-time ()
(interactive)
(insert (format-time-string "%a %b %d %H:%M:%S %Z %Y")))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;; Desktop and history
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Disable backup files (*~)
(setq make-backup-files nil)
;; Desktop save mode
(defvar my-desktop-path my-auto-save-list)
(setq desktop-path (list my-desktop-path))
(setq desktop-dirname my-desktop-path)
(desktop-save-mode 1)
(setq desktop-load-locked-desktop t)
(setq desktop-restore-frames nil)
;; save mini buffer history
(savehist-mode 1)
(setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring))
(setq savehist-file (concat my-desktop-path "/savehist-file.el"))
;; Add a hook when emacs is closed to we reset the desktop modification time (in this way
;; the user does not get a warning message about desktop modifications)
(add-hook 'kill-emacs-hook
(lambda ()
;; Reset desktop modification time so the user is not bothered
(setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))))
(setq desktop-buffers-not-to-save
(concat "\\("
"^\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
"\\|\\.emacs.*\\|\\*Help*\\|\\.gz$"
"\\)$"))
(setq desktop-globals-to-save
(quote
(desktop-missing-file-warning
search-ring
regexp-search-ring
register-alist
file-name-history)))
(add-to-list 'desktop-modes-not-to-save '(dired-mode Info-mode fundamental-mode))
;; save-place-mode
(setq save-place-file (concat my-auto-save-list "/save-place-file.el"))
;; bookmarks
;; every time bookmark is changed, automatically save it
(setq bookmark-save-flag 1)
(setq bookmark-default-file (concat my-auto-save-list "/bookmarks"))
(global-set-key (kbd "<f9> b") 'bookmark-bmenu-list)
(defun bookmark-current-file ()
(interactive)
(bookmark-set (buffer-file-name) nil))
(global-set-key (kbd "<f9> m") 'bookmark-current-file)
;;Session (keep sections with different machines)
;; (use-package session"
;; '(progn
;; (setq session-use-package nil)
;; (add-hook 'after-init-hook 'session-initialize)
;; ;; Save sessions with customization
;; (setq session-save-file (concat my-auto-save-list "/session-save-file.el"))
;; ))
;; Eshell
(setq eshell-directory-name (concat my-auto-save-list "/eshell"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Hide and Show code blocks
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(dolist (hook '(after-text-mode-hook c-mode-hook org-mode-hook python-mode-hook mail-mode-hook ess-mode-hook))
(add-hook hook #'(lambda () (hs-minor-mode))))
(global-set-key (kbd "M-+") 'hs-toggle-hiding)
(global-set-key (kbd "M-*") 'hs-show-all)
;; Electric operators
(use-package electric-operator
:ensure t
:config
(electric-pair-mode t)
(dolist (hook
'(python-mode-hook
python-ts-mode
c-mode-hook
c++-mode-hook
LaTeX-mode-hook
ess-r-mode-hook))
(add-hook hook #'(lambda () (electric-operator-mode 1))))
;; Customize rules
(electric-operator-add-rules-for-mode 'python-mode (cons "*" nil))
(electric-operator-add-rules-for-mode 'python-mode (cons "/" nil))
(electric-operator-add-rules-for-mode 'python-mode (cons "?" nil))
;; Add support for various treesitter based modes
(apply #'electric-operator-add-rules-for-mode 'inferior-python-mode (electric-operator-get-rules-for-mode 'python-mode))
(apply #'electric-operator-add-rules-for-mode 'python-ts-mode (electric-operator-get-rules-for-mode 'python-mode))
(setq electric-operator-R-named-argument-style "spaced")
)
;; Goto matched parenthesis
(global-set-key (kbd "?") 'goto-match-paren) ;;
(defun goto-match-paren (arg)
(interactive "p")
(cond ((looking-at "[\[\(\{]") (forward-sexp))
((looking-back "[\]\)\}]" 1) (backward-sexp))
;; now, try to succeed from inside of a bracket
((looking-at "[\]\)\}]") (forward-char) (backward-sexp))
((looking-back "[\[\(\{]" 1) (backward-char) (forward-sexp))
(t (self-insert-command (or arg 1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Matlab, Octave, yaml mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Let m-file connected with octave mode.
(setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))
(use-package yaml-mode
:ensure t
:defer t
:config
(add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; General IDE settings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package counsel
:ensure t
:custom
(counsel-rg-base-command
'("rg" "--max-columns" "1600" "--with-filename" "--no-heading" "--line-number" "--color" "never" "%s"))
:config
(ivy-mode)
(setq ivy-use-virtual-buffers t)
(setq enable-recursive-minibuffers t)
;; enable this if you want `swiper' to use it
;; (setq search-default-mode #'char-fold-to-regexp)
(global-set-key "\C-s" 'swiper)
(global-set-key (kbd "<f6>") 'ivy-resume)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "M-,") 'counsel-M-x) ; mirror of M-x
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "C-c G") 'counsel-git-grep)
(global-set-key (kbd "C-c g") 'counsel-ag) ;; find patten within git repository
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)
(define-key ivy-minibuffer-map (kbd "C-c C-v") 'ivy-occur)
;; Use C-j for immediate termination with the current value, and RET for continuing
;; completion for that directory. This is the ido behaviour.
(define-key ivy-minibuffer-map (kbd "C-j") #'ivy-immediate-done)
(define-key ivy-minibuffer-map (kbd "RET") #'ivy-alt-done)
;; Ignore hidden files.
(setq counsel-find-file-ignore-regexp
(concat
;; File names beginning with # or .
"\\(?:\\`[#.]\\)"
;; File names ending with # or ~
"\\|\\(?:\\`.+?[#~]\\'\\)"))
)
;; let `ivy-read' support Chinese pinyin, toggle with `
(use-package pinyinlib
:ensure t
:config
(defvar-local my-pinyin-search-prefix "`")
(defun re-builder-pinyin (str)
(or (pinyin-to-utf8 str)
(ivy--regex-plus str)
(ivy--regex-ignore-order)
))
(setq ivy-re-builders-alist
'(
(t . re-builder-pinyin)
))
(defun my-pinyinlib-build-regexp-string (str)
(progn
(cond ((equal str ".*")
".*")
(t
(pinyinlib-build-regexp-string str t))))
)
(defun my-pinyin-regexp-helper (str)
(cond ((equal str " ")
".*")
((equal str "")
nil)
(t
str)))
(defun pinyin-to-utf8 (str)
(cond ((equal 0 (length str))
nil)
((equal (substring str 0 1) my-pinyin-search-prefix)
(mapconcat 'my-pinyinlib-build-regexp-string
(remove nil (mapcar 'my-pinyin-regexp-helper
(split-string (replace-regexp-in-string my-pinyin-search-prefix "" str) "")))
""))
nil))
)
;; iedit mode
(use-package iedit
:ensure t
:config
(global-set-key (kbd "C-c i") 'iedit-mode)
)
(use-package multiple-cursors
:ensure t
:config
(global-set-key (kbd "C-c a") 'mc/mark-all-like-this)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
)
;; Ibuffer mode
(use-package ibuffer
:ensure t
:config
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "<f9> i") 'ibuffer)
(setq dired-omit-mode t)
(add-hook 'dired-mode-hook
(lambda ()
(setq dired-omit-files "^\\.[a-z|A-Z]+\\|^\\.?#\\|^\\.$")
(setq dired-omit-extensions
'(".pyc" "CVS/" "~" ".o" ".bin" ".bak" ".obj" ".map" ".a"
".ln" ".blg" ".bbl" ".elc" ".lof" ".glo" ".idx" ".lot"
".dvi" ".fmt" ".tfm" ".class" ".fas" ".lib" ".x86f"
".sparcf" ".lo" ".la" ".toc" ".aux" ".cp" ".fn" ".ky"
".pg" ".tp" ".vr" ".cps" ".fns" ".kys" ".pgs" ".tps"
".vrs" ".idx" ".lof" ".lot" ".glo" ".blg" ".cp" ".cps"
".fn" ".fns" ".ky" ".kys" ".pg" ".pgs" ".tp" ".tps"
".vr" ".vrs" ".Rc" ))
(setq dired-listing-switches "-hla")
(define-key dired-mode-map (kbd "<return>")
'dired-find-alternate-file) ; was dired-advertised-find-file
(define-key dired-mode-map (kbd "<delete>") 'dired-do-delete)
(define-key dired-mode-map (kbd "<f9> DEL")
(lambda () (interactive) (find-alternate-file "..")))
(dired-omit-mode 1)
(local-set-key (kbd "<f9> h") 'dired-omit-mode)))
(put 'dired-find-alternate-file 'disabled nil)
(setq ibuffer-saved-filter-groups
(quote
(("default"
("Scripts" (or
(mode . ess-mode)
(mode . prog-mode)
(mode . shell-script-mode)
(mode . sh-mode)
(mode . python-mode)
(mode . makefile-mode)
(mode . snippet-mode)
(mode . emacs-lisp-mode)))
("Files" (or
(mode . LaTeX-mode)
(mode . latex-mode)
(mode . markdown-mode)
(mode . bibtex-mode)
(mode . org-mode)))
("Config" (or
(mode . yaml-mode)
(mode . conf-unix-mode)))
("Proc" (or
(mode . inferior-ess-mode)))
("Help" (or
(mode . help-mode)
(mode . lexic-mode)
(mode . ess-help-mode)
(mode . Info-mode)))
("Messages" (or
(name . "^\\*scratch\\*$")
(name . "^\\*Bookmark List\\*$")
(name . "^\\*Messages\\*$")
(name . "^\\*Completions\\*$")
(mode . fundamental-mode)))
))))
(add-hook 'ibuffer-mode-hook
(lambda ()
(ibuffer-switch-to-saved-filter-groups "default"))))
;; Ido mode
(use-package ido
:ensure t
:config
(ido-mode t)
(setq ido-use-virtual-buffers nil)
(setq ido-enable-flex-matching nil)
(setq ido-ignore-extensions t)
(setq ido-save-directory-list-file (concat my-auto-save-list "/ido-save-directory-list-file.el"))
(setq ido-ignore-files ; this also works with directories with c-x c-f
'("\\.Rc$" "\\.dvi$" "\\.pdf$" "\\.ps$" "\\.out$" "\\.fls$" "\\.spl$"
"\\.fff$" "\\.ttt$" "\\.log$" "\\.ods$" "\\.eps$" "\\#$" "\\.png$" "\\~$"
"\\.RData$" "\\.nav$" "\\.snm$" "\\`\\.\\./" "\\`\\./" "\\.synctex.gz$"
"\\.fdb_latexmk$" "\\.tar.gz$" "\\.zip$" "\\.o$" "\\.tar$" "\\.Rproj$"
"\\.Rcheck$" "\\.doc$" "\\.docx$" "\\.Rhistory$" "auto/" "__pycache__/"
"\\.bcf$" "\\.run.xml$" "_region_.tex$" "\\.xdv$" "\\.DS_Store$"
"\\.cfg$" "\\.bak$" "\\.gitignore" "\\.tmp$"))
(setq ido-ignore-directories ; only works with ido-dired
'("\\`auto/" "\\.prv/" "\\`CVS/" "\\`.git/" "\\`.ropeproject/" "\\`\\.\\./"
"\\`\\./" "\\`_bookdown_files/" "__pycache__/"))
(setq ido-ignore-buffers
'("\\` " "^\\*ESS\\*" "^\\*Messages\\*" "^\\*Help\\*" "^\\*Buffer"
"*scratch*" "^\\*Ibuffer*" "^\\*ESS-errors*" "^\\*Warnings*" "*TeX Help*"
"*Pymacs*" "*Flymake log*" "\\.log$" "^\\*.*Completions\\*$" "^\\*Ediff"
"^\\*tramp" "^\\*cvs-" "_region_" "^TAGS$" "^\\*Ido"
"^\\*.*dictem buffer\\*$" "^\\*inferior-lisp*" "^\\*Compile-Log\\*"
"*output*" "\.\*output*" "^\\*EGLOT*" "^\\*Async-native-compile-log*"
"^\\*lsp-log*" "^\\*grammarly-ls::stderr*" "^\\*grammarly-ls*"))
(defun ido-kill-emacs-hook () (ignore-errors (ido-save-history)))
)
;; ElDoc mode
(add-hook 'emacs-lisp-mode-hook #'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook #'turn-on-eldoc-mode)
(add-hook 'ielm-mode-hook #'turn-on-eldoc-mode)
;; Comint for input history and scrolling
(use-package comint
:ensure nil
:config
(define-key comint-mode-map (kbd "C-<up>")'comint-previous-matching-input-from-input)
(define-key comint-mode-map (kbd "C-<down>") 'comint-next-matching-input-from-input)
(define-key comint-mode-map (kbd "C-k") 'comint-kill-input)
;; Comint history length
(setq comint-input-ring-size 5000)
(setq comint-read-input-ring t)
;; Comint scroll output
(setq comint-scroll-to-bottom-on-output 'others)
(setq comint-scroll-show-maximum-output t)
(setq comint-scroll-to-bottom-on-input 'this)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-move-point-for-output t)
;; Clear buffer output
;; Emacs 25's new default function was bound to (C-C, C-O)
(define-key comint-mode-map (kbd "C-l") 'comint-clear-buffer)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Auto completion settings (company mode, yasnippet)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package yasnippet
:ensure t
:config
;; Make yasnippet treat LaTeX-mode as latex-mode
(add-hook 'LaTeX-mode-hook
(lambda ()
(yas-activate-extra-mode 'latex-mode)))
(yas-global-mode 1)
;; (setq yas-snippet-dirs
;; '(;; personal snippets
;; "~/.emacs.d/snippets"
;; ;; snippet collection
;; ;; "~/.emacs.d/site-lisp/yasnippet-snippets/snippets"
;; ))
)
(use-package company
:ensure t
:config
(add-hook 'after-init-hook #'global-company-mode)
(setq company-minimum-prefix-length 2)
(setq company-idle-delay
(lambda () (if (company-in-string-or-comment) nil 0.5)))
(setq company-files-exclusions '(".git/" ".DS_Store" "auto/"))
;; Preserve initial cases
(setq company-dabbrev-downcase nil)
(setq company-dabbrev-ignore-case nil)
(setq company-dabbrev-other-buffers t)
;; Add yasnippet support for all company backends
(add-to-list 'company-backends '(company-capf :with company-yasnippet))
(defun my-text-mode-hook ()
(setq-local company-backends
'((company-yasnippet
company-dabbrev
company-ispell :separate)
company-files)))
(add-hook 'text-mode-hook #'my-text-mode-hook)
;; https://github.com/abo-abo/oremacs/issues/38#issuecomment-948472184
(setq company-show-quick-access t)
(defun hz-company-complete-number ()
"Convert the company-quick-access-keys to the candidates' row NUMBER visible on the
tooltip, and then feed it to `company-complete-number' to quickly select and insert
company candidates. If the currently entered character is belongs to
company-quick-access-keys and a part of the candidate simultaneously, append it to the
currently entered string to construct new company-prefix."
(interactive)
(let* ((k (this-command-keys))
(re (concat "^" company-prefix k)))
(if (cl-find-if (lambda (s) (string-match re s))
company-candidates)
(self-insert-command 1)
(company-complete-tooltip-row
(cond
((equal k "1") 1)
((equal k "2") 2)
((equal k "3") 3)
((equal k "4") 4)
((equal k "5") 5)
((equal k "6") 6)
((equal k "7") 7)
((equal k "8") 8)
((equal k "9") 9)
((equal k "0") 10)
)
))))
(let ((c-a-map company-active-map)
(c-s-map company-search-map))
(mapc (lambda (x)
(define-key c-a-map (format "%s" x) #'hz-company-complete-number))
'(1 2 3 4 5 6 7 8 9 0))
(mapc (lambda (x)
(define-key c-s-map (format "%s" x) #'hz-company-complete-number))
'(1 2 3 4 5 6 7 8 9 0)))
)
;; Font lock
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t) ;; Highlight parentheses
(use-package highlight-parentheses
:ensure t
:disabled t
:config
(define-globalized-minor-mode global-highlight-parentheses-mode
highlight-parentheses-mode (lambda () (highlight-parentheses-mode t)))
(global-highlight-parentheses-mode t)) ;; Highlight symbols
;; parentheses mode
(show-paren-mode t)
;; (setq show-paren-style 'expression) ;; highlight whole block
;; Commenting
(global-set-key (kbd "M-3") 'comment-or-uncomment-region)
(global-set-key (kbd "M-0") 'comment-or-uncomment-region) ;; mirror
;; Add extra info path
(use-package info-look
:ensure t
:config
(add-to-list
'Info-default-directory-list (concat user-emacs-directory "info")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mail
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'auto-mode-alist '("neomutt" . message-mode))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Version Control and Diff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ediff
(setq ediff-ignore-similar-regions t)
(setq vc-handled-backends ()) ;; Disable vc-git and use magit
(vc-mode -1)
(with-eval-after-load 'info
(info-initialize)
(add-to-list 'Info-directory-list
(concat user-emacs-directory "/magit/Documentation/"))
)
(setq vc-follow-symlinks nil)
(use-package magit
:ensure t
:defer nil
:config
;; Save transient file with customization
(setq transient-history-file (concat my-auto-save-list "/transient-history-file.el"))
;; magit with-editor support
(define-key (current-global-map)
[remap async-shell-command] 'with-editor-async-shell-command)
(define-key (current-global-map)
[remap shell-command] 'with-editor-shell-command)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Spelling checking & dictionaries
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Spelling Check
(use-package ispell
:ensure t
:config
;; (setq ispell-dictionary "american")
;; Hunspell 1.7 is broken with emacs 26.1
(defun ispell-get-coding-system () 'utf-8)
(setq ispell-use-framepop-p t)
;; Prefer hunspell if exits
(if (locate-file "hunspell" exec-path)
(progn
(setq ispell-program-name "hunspell")
(setq ispell-really-hunspell t)
(setenv "DICPATH" (concat user-emacs-directory "dicts/hunspell"))
(setq ispell-local-dictionary "en_US")
(setq ispell-local-dictionary-alist
'(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil
("-d" "en_US,en_GB") nil UTF-8)))
)
)
(global-set-key (kbd "<f9> 4") 'ispell-word))
;; jinx directly calling the widely-used API of the Enchant library.
(use-package jinx
:hook (emacs-startup . global-jinx-mode)
:bind (("M-4" . jinx-correct)
("C-M-$" . jinx-languages))
:config
(add-to-list 'jinx-exclude-regexps '(t "\\cc")) ;; Disable Chinese check
)
;; Auto correct spelling mistakes
(global-set-key (kbd "<f9> c") 'flyspell-auto-correct-word)
(with-eval-after-load 'comint
(define-key comint-mode-map (kbd "C-d") nil)
)
;; FlyCheck
;; Enable nice rendering of diagnostics like compile errors.
;; Remove flymake mode and using flycheck mode
(remove-hook 'prog-mode-hook #'flymake-mode)
(remove-hook 'text-mode-hook #'flymake-mode)
(remove-hook 'LaTeX-mode-hook #'flymake-mode)
(use-package flycheck
:ensure t
:custom
(flycheck-checker-error-threshold 800)
(flycheck-check-syntax-automatically (quote (idle-change mode-enabled))) ; save
(flycheck-idle-change-delay 3) ;; Set delay based on what suits you the best
(global-flycheck-mode t)
(flycheck-add-next-checker 'python-flake8 'python-pylint)
(flycheck-flake8rc '(".flake8" "setup.cfg" "tox.ini"
"~/.config/flake8/setup.cfg"
"~/.config/flake8/tox.ini"))
:config
;; Checkers for Python
(setq flycheck-python-flake8-executable "flake8")
(setq flycheck-python-pylint-executable "pylint")
;; make python-pylint run after python-flake8
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Language and writing https://languagetool.org/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LanguageTool https://languagetool.org/download/
(use-package flycheck-languagetool
:custom
(flycheck-languagetool-active-modes
'(latex-mode LaTeX-mode org-mode markdown-mode gfm-mode))
:config
;; (setq flycheck-languagetool-active-modes '(LaTeX-mode org-mode markdown-mode))
(setq flycheck-languagetool-url "http://localhost:8081")
(flycheck-languagetool-setup)
)
;; (use-package flycheck-grammarly
;; :defer nil
;; :custom
;; (flycheck-grammarly-active-modes
;; '(LaTeX-mode org-mode markdown-mode gfm-mode))
;; :config
;; (setq flycheck-grammarly-check-time 0.8)
;; (flycheck-grammarly-setup)
;; )
(use-package synosaurus
:config
(dolist (hook '(text-mode-hook latex-mode-hook LaTeX-mode-hook prog-mode-hook org-mode-hook markdown-mode-hook))
(add-hook hook (lambda () (synosaurus-mode))))
(setq synosaurus-choose-method 'popup) ; popup, ido
;; (setq synosaurus-backend 'Wordnet) ; apt install wordnet
(define-key synosaurus-mode-map (kbd "<f9> s") 'synosaurus-choose-and-replace)
)
;; Fly spell performance
(setq flyspell-issue-welcome-flag nil)
(setq flyspell-issue-message-flag nil)
;; Fly spell mode for major mode
(add-hook 'prog-mode-hook #'flyspell-prog-mode)
(dolist (hook '(text-mode-hook LaTeX-mode-hook markdown-mode-hook))
(add-hook hook (lambda () (flyspell-mode 1))))
(add-hook 'LaTeX-mode-hook (function (lambda () (setq ispell-parser 'tex))))