-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.el
3013 lines (2654 loc) · 117 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 --- My config -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(setq
native-comp-async-report-warnings-errors nil
garbage-collection-messages nil)
(add-to-list 'load-path "/usr/local/share/emacs/site-lisp/")
(progn ;; exec-path, PATH and other env
(defun my/prepend-exec-path (p)
(let ((path (expand-file-name p)))
(when (file-directory-p path)
(setq exec-path (cons path (delete path exec-path))))
(setenv "PATH" (string-join exec-path ":"))))
(my/prepend-exec-path "/opt/local/bin")
(my/prepend-exec-path "/opt/local/sbin")
(my/prepend-exec-path "/usr/local/bin")
(my/prepend-exec-path "/usr/local/sbin")
(my/prepend-exec-path "~/.npm/bin")
(my/prepend-exec-path "~/.npm-packages/bin")
(my/prepend-exec-path "~/.cargo/bin")
(my/prepend-exec-path "~/.rvm/bin")
(my/prepend-exec-path "~/.local/bin")
(my/prepend-exec-path (file-name-concat user-emacs-directory "bin"))
;; handle all dotfiles in .emacs.d
(let ((emacs-dir (expand-file-name user-emacs-directory)))
(setenv "XONSHRC" (concat (file-name-concat emacs-dir "xonsh_rc.xsh")
":~/.xonshrc"))
(setenv "XONSH_CONFIG_DIR" emacs-dir)
;; set here for both xonsh and magit
(setenv "GIT_CONFIG_GLOBAL" (file-name-concat emacs-dir "dotfiles/git/config"))
(setenv "RIPGREP_CONFIG_PATH" (file-name-concat emacs-dir "dotfiles/ripgrep.config"))
(setq treesit-extra-load-path (list (file-name-concat emacs-dir "treesit-langs/dist/")))
))
;; some variables from init-local.el
(defvar my/curl-proxy nil)
(let ((my/-init-local-file (expand-file-name "init-local.el" user-emacs-directory)))
(when (file-exists-p my/-init-local-file)
(load-file my/-init-local-file)))
(progn ;; GC tune {{{
;; Set to large value before start
(setq gc-cons-threshold most-positive-fixnum ; 2^61 bytes
gc-cons-percentage 0.6)
;; ... and restore it
(add-hook 'emacs-startup-hook
(lambda ()
(setq gc-cons-threshold (* 32 (* 1024 1024))
gc-cons-percentage 0.1)))
;; emacs will never free its heap
;; pausing gc will cause huge memory consumption
;; (defun my/gc-pause ()
;; "Pause garbage collection for now."
;; (setq gc-cons-threshold (* 2 (* 1024 (* 1024 1024)))))
;; (defun my/gc-resume ()
;; "Resume garbage collection (and do it once)."
;; (setq gc-cons-threshold (* 64 (* 1024 1024)))
;; (setq garbage-collection-messages nil)
;; (message "Garbage collecting...done (%.3fs)"
;; (my/timeit (garbage-collect)))
;; (setq garbage-collection-messages t)
;; )
;; (add-hook 'focus-out-hook #'my/gc-resume)
;; (add-hook 'evil-insert-state-exit-hook #'my/gc-resume)
;; (add-hook 'evil-insert-state-entry-hook #'my/gc-pause)
;; (add-hook 'minibuffer-setup-hook #'my/gc-pause)
;; (add-hook 'minibuffer-exit-hook #'my/gc-resume)
) ;;; }}}
(progn ;; Package Manager: borg {{{
(add-to-list 'load-path (expand-file-name "lib/borg" user-emacs-directory))
(require 'borg)
(setq borg-maketexi-filename-regexp nil)
(borg-initialize)
) ;; }}}
(progn ;; Package Manager: use-package {{{
(setq use-package-verbose nil
;; always defer. this is important
use-package-always-defer t)
(require 'use-package)
(progn
;; my/env-check :
;; like health check, used for check if os environment meets package requirement.
;; add checks using :my/env-check in package configurations.
(defvar my/env-check-functions nil)
(defun my/env-check ()
(interactive)
(with-current-buffer-window "*my/env-check*" nil nil
(erase-buffer)
(run-hooks 'my/env-check-functions)))
(add-to-list 'use-package-keywords :my/env-check)
(defalias 'use-package-normalize/:my/env-check 'use-package-normalize-forms)
(defun use-package-handler/:my/env-check (name _keyword args rest state)
(use-package-concat
(when args
(let ((fn-name (intern (format "my/env-check-package--%s" name))))
`((defun ,fn-name ()
(insert ,(format "Checking for `%s' (%d conditions)...\n" name (length args)))
(require ',name)
,@(mapcar (lambda (form)
`(unless ,form
(insert (propertize " Failed" 'face 'error) ,(format ": %s\n" form))))
args))
(add-to-list 'my/env-check-functions ',fn-name))))
(use-package-process-keywords name rest state))))
;; for use-package :delight
(use-package delight
:demand t)
) ;; }}}
(progn ;; Profiling, usually disabled {{{
;;(use-package keyfreq
;; :config
;; (keyfreq-mode 1)
;; (keyfreq-autosave-mode 1))
;;(use-package memory-usage)
;; (use-package esup)
) ;; }}}
(progn ;; Some utility helper functions {{{
(defun my/macos-p ()
"Return t if it's in macos."
(string-equal system-type "darwin"))
(defconst my/in-kitty (equal (getenv-internal "TERM" initial-environment) "xterm-kitty"))
(defmacro my/timeit (&rest body)
"Measure and return the time it takes to evaluate BODY."
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(defmacro comment (&rest body)
"Comment out one or more s-expressions."
nil)
(defmacro my/define-advice (symbol args &rest body)
"Like `define-advice', but make sure SYMBOL is defined."
(declare (indent 2) (debug (sexp sexp def-body)))
(when (length> args 2)
(setf (nth 2 args) (intern (concat "my/" (symbol-name (nth 2 args))))))
`(progn
(unless (symbol-function ',symbol)
(display-warning 'my/define-advice (format "Function %s is not defined" ',symbol) :error))
(define-advice ,symbol ,args ,@body)))
(defvar prog-mode-local-only-hook nil
"Custom hook for prog-mode, but local only (not triggered for TRAMP file)")
(defun my/trigger-prog-mode-local-only-hook ()
"Trigger `prog-mode-local-only-hook' on prog-mode, if it's a local buffer."
(unless (and (buffer-file-name)
(file-remote-p (buffer-file-name)))
(run-hooks 'prog-mode-local-only-hook)))
(add-hook 'prog-mode-hook #'my/trigger-prog-mode-local-only-hook)
) ;; }}}
(progn ;; some early settings/hacks
;; for some reason, `tags-file-name' would be set as a global variable sometime
;; which would make CAPF tags function slow (e.g. emacs .el TAGS are loaded for .cc file)
(make-variable-buffer-local 'tags-file-name))
(progn ;; startup
(setq inhibit-startup-echo-area-message t)
(setq my/startup-msg "Welcome back.\n\n")
(defun my/startup-buffer ()
(with-current-buffer (get-buffer-create "*Welcome*")
(insert my/startup-msg
(format "native-comp-available-p: %s\n" (native-comp-available-p))
(format "treesit-available-p: %s\n" (treesit-available-p))
(format "Startup time: %s\n\n" (emacs-init-time))
(string-replace "\n" "" (emacs-version)) "\n\n")
(current-buffer)))
(setq initial-buffer-choice #'my/startup-buffer)
)
(progn ;; pragmata ligatures and icons {{{
(use-package ligature
:config
(ligature-set-ligatures
'prog-mode '("!!" "!=" "!!!" "!==" "&&" "***" "*=" "*/" "++" "+=" "--" "-="
"->" ".." "..." "/*" "//" "/>" "///" "/**" ":::" "::" ":=" "<-"
"<<" "<=" "<=>" "==" "===" "=>" ">=" ">>" "??" "\\\\" "|=" "||"
"[[" "]]"))
:hook (prog-mode . ligature-mode))
;; pragmata major mode icons
(let (delight-args)
(dolist (pair '((dired-mode . "\xf4d3")
(wdired-mode . (:eval (propertize "\xf4d3" 'face 'error)))
(python-mode . "\xe606")
(js-mode . "\xe60c")
(sh-mode . "\xe614")
(c++-mode . "\xe61d")
(c-mode . "\xe61e")
(go-mode . "\xe626")
(tsx-mode . "\xe796")
(lua-mode . "\xe620")
(typescript-mode . "\xe628")
(vimrc-mode . "\xe62b")
(html-mode . "\xe736")
(java-mode . "\xe738")
(ruby-mode . "\xe739")
(markdown-mode . "\xe73e")
(haskell-mode . "\xe777")
(rust-mode . "\xe7a8")
(vterm-mode . "\xe795")
(eat-mode . "\xe795")
(dockerfile-mode . "\xe7b0")))
(push (list (car pair) (cdr pair) :major) delight-args)
(let ((ts-mode (intern (string-replace "-mode" "-ts-mode" (symbol-name (car pair))))))
(when (fboundp ts-mode)
(push (list ts-mode (concat (cdr pair) "+\xe21c") :major) delight-args))))
(delight delight-args))
;; see delight.el
(with-eval-after-load 'cc-mode
(my/define-advice c-update-modeline (:override (&rest _) ignore-for-delight)
nil))
;; default font is set in early-init.el for fast startup
;; https://github.com/fabrizioschiavi/pragmatapro/issues/217
;; "…" (\u2026) has a bug in PragmataPro Liga:
;; it's double-char-width in regular weight, but single-char-width in bold weight.
;; (it's usually double-char-width in other fonts)
;;
;; `truncate-string-ellipsis' returns "…" (\u2026) by default
;; and expects it to always be double-char-width (maybe get this info from regular weight?),
;; so it would make tables unaligned.
(setq truncate-string-ellipsis "...")
;; Actually we would prefer it to be single-char-width. It looks better in shell git status.
;; So we can use the version from PragmataPro Mono Liga.
(setq use-default-font-for-symbols nil) ;; this is required to make the next line work
(set-fontset-font t #x2026 "PragmataPro Mono Liga")
(setq my/cn-font-name (if (my/macos-p) "HYQiHeiY1" "HYQiHeiY1-55W"))
;; 汉仪旗黑Y1(如果需要更扁,还可以选择Y2等)和英文等宽(x2)且等高
(dolist (range '((#x2e80 . #x9fff) ;; https://unicodeplus.com/plane/0
(#xf900 . #xfaff)
(#xfe30 . #xfe4f)
(#xff00 . #xffef)))
;; 它的不同weight是放在不同的字体里的,所以显式地选择55W作为regular
;; 需要加粗时,emacs会自动基于这个字体动态加粗
(set-fontset-font t range (font-spec :family my/cn-font-name)))
;; TODO: also set different chinese font for variable-pitch
;; to do that, we need to define our own fontset
;; https://www.reddit.com/r/emacs/comments/mo0cc8/whats_the_relation_between_setfontsetfont_and/
(unless (my/macos-p)
(set-face-attribute 'variable-pitch nil
:family "Noto Sans"))
(defun my/check-all-fonts-exists ()
(let ((allfonts (font-family-list)))
(dolist (font (list (face-attribute 'default :family)
"PragmataPro Mono Liga"
my/cn-font-name
"Noto Sans"))
(insert (format "Checking for font `%s'...\n" font))
(unless (member font allfonts)
(insert (propertize " Not found\n" 'face 'error))))))
(add-to-list 'my/env-check-functions #'my/check-all-fonts-exists)
) ;; }}}
(progn ;; EVIL & general keybindings {{{
(when window-system
;; https://emacs.stackexchange.com/questions/20240/how-to-distinguish-c-m-from-return
;; to define C-m key
(define-key input-decode-map [?\C-m] [C-m])
;; remove some keybindings that can be accidentally triggered..
;; suspend-frame
(define-key global-map (kbd "C-x C-z") nil)
;; save-buffers-kill-terminal
(define-key global-map (kbd "C-x C-c") nil))
(use-package kkp
:when my/in-kitty
:demand t
:commands (my/kkp-switch-layout)
:config
(defun my/kkp-switch-layout (layout)
(interactive (list (completing-read "Layout: " '(linux-mod macos-mod none) nil t)))
;; my expected layout from left to right: alt, win (super), control
(pcase layout
;; pc layout: control, win (super), alt.
;; aka, switch control and alt
('linux-mod (setq kkp-control-modifier 'meta
kkp-super-modifier 'super
kkp-alt-modifier 'control))
;; macos layout: control, opt (as alt in kitty), cmd.
('macos-mod (setq kkp-control-modifier 'meta
kkp-alt-modifier 'super
kkp-super-modifier 'control))
('none (setq kkp-control-modifier 'control
kkp-super-modifier 'super
kkp-alt-modifier 'meta))))
(my/kkp-switch-layout 'macos-mod)
(setq my/startup-msg
(concat my/startup-msg
"Kitty terminal detected. See :kkp-status.\n"
"Using macos-mod layout. Use :my/kkp-switch-layout to switch.\n"
"\n"))
(global-kkp-mode t))
(use-package evil
:demand t
:init
(setq evil-want-C-w-in-emacs-state t
evil-want-C-i-jump nil
evil-split-window-below t
evil-vsplit-window-right t
evil-want-fine-undo t
evil-search-module 'evil-search
evil-ex-search-vim-style-regexp t
evil-undo-system 'undo-redo
evil-symbol-word-search t
;; required by evil-collection
evil-want-keybinding nil)
(setq evil-emacs-state-tag (propertize " <E> " 'face '((:foreground "red"))))
:config
(evil-mode t)
;; Make :x :q :wq close buffer instead of closing window
(evil-define-command evil-quit (&optional force)
"Kill current buffer."
:repeat nil
(interactive "<!>")
(kill-current-buffer))
(evil-ex-define-cmd "bd[elete]" #'kill-current-buffer)
;; mouse:
;; 1. disable drag to visual mode
(evil-define-key 'motion 'global
[down-mouse-1] nil)
(evil-define-key nil 'global
[down-mouse-1] nil
[drag-mouse-1] nil
;; control-mouse scroll
[C-mouse-4] nil
[C-mouse-5] nil)
;; other general keybindings
(evil-define-key nil 'global
(kbd "C-:") #'execute-extended-command
(kbd "C-S-v") #'yank)
(evil-define-key 'normal 'global
(kbd "C-l") #'evil-ex-nohighlight
(kbd "Q") "@q"
(kbd "U") #'evil-redo))
(progn ;; movement
(defun my/move-buffer-to-window (dir)
"Move current buffer to another window in DIR.
Switch current window to previous buffer (if any)."
(require 'windmove)
(let ((other-win (windmove-find-other-window dir))
(buf (current-buffer))
(pt (point))
(win-start (window-start)))
(when (and other-win
buf
(not (window-minibuffer-p other-win)))
(switch-to-prev-buffer) ;; ignore error
(select-window other-win)
(switch-to-buffer buf)
(set-window-start other-win win-start)
(goto-char pt))))
(defun my/move-buffer-to-window-left () (interactive) (my/move-buffer-to-window 'left))
(defun my/move-buffer-to-window-right () (interactive) (my/move-buffer-to-window 'right))
(defun my/move-buffer-to-window-up () (interactive) (my/move-buffer-to-window 'up))
(defun my/move-buffer-to-window-down () (interactive) (my/move-buffer-to-window 'down))
(evil-define-key 'normal 'global
(kbd "C-w C-h") #'my/move-buffer-to-window-left
(kbd "C-w C-j") #'my/move-buffer-to-window-down
(kbd "C-w C-k") #'my/move-buffer-to-window-up
(kbd "C-w C-l") #'my/move-buffer-to-window-right)
;; only used as fallback when the desktop environment does not have these bindings
(evil-define-key nil 'global
(kbd "s-h") #'evil-window-left
(kbd "s-j") #'evil-window-down
(kbd "s-k") #'evil-window-up
(kbd "s-l") #'evil-window-right))
(use-package evil-collection
:demand t
:after evil
:custom
(evil-collection-key-blacklist
'(
;; evil-collection-pdf binds keys starting with /. who would wants that???
"//" "/!" "/="
"/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"
;; some of my own global keybindings
"C-t" "C-r"
))
(evil-collection-want-unimpaired-p nil)
:config
;; remove keybindings for some modes. let's do them on our own
(mapc (lambda (x) (setq evil-collection-mode-list (delete x evil-collection-mode-list)))
'(vterm eat company corfu wdired))
(evil-collection-init))
(use-package evil-commentary
:after evil
:delight evil-commentary-mode
;; do not use hook prog-mode
;; because some modes (e.g. conf-mode) has comment support but is not prog-mode
:demand t
:config (evil-commentary-mode t))
(use-package evil-surround
:demand t
:after evil
:config (global-evil-surround-mode t))
(use-package vimish-fold)
;; evil-vimish-fold would automatically call vimish-fold-mode
(use-package evil-vimish-fold
:delight evil-vimish-fold-mode
:init (evil-define-key 'normal 'global
;; refresh marks
(kbd "z g") #'vimish-fold-from-marks)
;; do not enable for remote files, because it would block while persisting the folding
:hook (prog-mode-local-only . evil-vimish-fold-mode))
(use-package evil-owl
:demand t
:custom (evil-owl-idle-delay 0.5)
:delight evil-owl-mode
:config
(setq evil-owl-max-string-length 500)
(add-to-list 'display-buffer-alist
'("*evil-owl*"
(display-buffer-in-side-window)
(side . bottom)
(window-height . 0.3)))
;; it's a global mode
(evil-owl-mode))
(use-package evil-snipe
:demand t
:after evil
;; :custom
;; (evil-snipe-scope 'visible)
:delight evil-snipe-local-mode
:config
;; Only uses its "clever-f" feature, not its two-char jump feature; use avy-mode for that
;; global mode
;; (evil-snipe-mode)
(evil-snipe-override-mode))
(use-package avy
:after evil
:custom (avy-background t)
:init (evil-define-key 'normal 'global
(kbd "s") #'avy-goto-char-2))
(use-package evil-visualstar
:demand t
:after evil
:config (global-evil-visualstar-mode))
(use-package expand-region
:commands (er/expand-region er/contract-region)
:init (evil-define-key 'visual 'global
(kbd ".") 'er/expand-region
(kbd "v") 'er/expand-region
(kbd ",") 'er/contract-region))
(when (my/macos-p)
;; (setq mac-command-modifier 'super
;; mac-option-modifier 'meta)
;; Use command as control here, like (my modified) linux
(setq mac-command-modifier 'control
mac-control-modifier 'meta
mac-option-modifier 'super)
(setq mac-pass-command-to-system nil)
;; mimic the linux i3 keybindings
(evil-define-key nil 'global
;; (kbd "s-Q") #'save-buffers-kill-emacs
(kbd "s-f") #'toggle-frame-fullscreen))
(use-package evil-terminal-cursor-changer
:demand t
:unless (display-graphic-p)
:init
(when my/in-kitty
(setq etcc-term-type-override 'kitty))
:config
(etcc-on))
) ;; }}}
(use-package hydra ;;; Hydra keybindings {{{
:commands (my/hydra-copy-filename/body)
:init
(evil-define-key 'normal 'global
(kbd "C-c y") #'my/hydra-copy-filename/body)
:config
(defmacro my/define-hydra-copy-filename (args)
"ARGS: list of (key . form)"
(macroexpand
`(defhydra my/hydra-copy-filename
(:exit t :hint nil :color blue)
,(concat
"
Copy filename as...
-------------------
"
(mapconcat (lambda (arg)
(concat "_" (car arg) "_: %s" (prin1-to-string (cdr arg)) ""))
args "\n"))
,@(mapcar
(lambda (arg) `(,(car arg) (kill-new ,(cdr arg))))
args))))
(my/define-hydra-copy-filename
;; project path
(("y" . (let ((root (project-root (project-current))))
(if root
(file-relative-name (buffer-file-name) root)
(buffer-file-name))))
;; bazel path
("b" . (when-let* ((root (project-root (project-current)))
(rel (file-relative-name (buffer-file-name) root)))
(concat "//" (string-trim-right (or (file-name-directory rel) "") "/+") ":" (file-name-base rel))))
;; full path
("f" . (expand-file-name (buffer-file-name)))
;; short path
("s" . (file-relative-name (buffer-file-name)))))
) ;;; }}}
(progn ;; Some essential utils {{{
(use-package switch-buffer-functions
:demand t)
(use-package add-node-modules-path
:hook (js-mode . add-node-modules-path))
(use-package fringe-scale
:demand t
:unless (my/macos-p)
:init (setq fringe-scale-width my/gui-fringe-size)
:config (fringe-scale-setup))
(use-package which-key
:demand t
:delight which-key-mode
:custom (which-key-ellipsis "..") ;; see `truncate-string-ellipsis'
:config (which-key-mode t))
(use-package help-fns ;; the builtin package
:init
(evil-define-key 'normal 'global
(kbd "C-h F") #'describe-face
(kbd "C-h C-k") #'describe-keymap)
:config
;; This is to prevent showing empty string in completing-read, caused by llama ("##" symbol)
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
;; https://git.sr.ht/~tarsius/llama
;; WTF
(my/define-advice help--symbol-completion-table (:filter-args (args) filter-out-empty-string)
(when-let ((pred (nth 1 args)))
(when (functionp pred)
(let ((new-pred (lambda (x) (and (funcall pred x) (not (string= x ""))))))
(setf (nth 1 args) new-pred))))
args))
(use-package info
:config
(evil-define-key 'normal Info-mode-map
(kbd "C-t") nil))
(use-package term/xterm
:when my/in-kitty
;; getSelection does not seem to work??
:custom (xterm-extra-capabilities '(setSelection))
:demand t
:config (terminal-init-xterm))
) ;; }}}
(progn ;; Theme {{{
(defvar my/monoink nil "MonoInk mode")
(setq my/monoink (equal (getenv "EMACS_MONOINK") "1"))
(defun my/monoink-refresh (&rest _)
(when my/monoink
(call-process "killall" nil nil nil "-USR1" "rabbitink")))
(defun my/monoink-refresh-on-window-config-change (&rest _)
(when my/monoink
(unless (or (minibuffer-window-active-p (minibuffer-window))
(which-key--popup-showing-p)
(get-buffer "*evil-owl*")
(eq last-command 'evil-write)
;; triggered by child frame (e.g. posframe)
(frame-parent (selected-frame)))
(run-with-timer 0 nil #'my/monoink-refresh))))
(add-hook 'window-configuration-change-hook #'my/monoink-refresh-on-window-config-change)
(when my/monoink
(my/define-advice display-color-p (:override (&rest _) monoink-no-color)
nil))
(defun my/patch-term-color-black (_)
;; Fix term-color-black:
;; By default, term-color-black is base02 (see solarized-faces.el),
;; which is, by its definition, a background color (very light in solarized-light).
;; However, shells don't need it as background, but instead they would use it to render text:
;; e.g. in xonsh, this color is used for displaying aborted commands and suggestions,
;; which should be a "comment"-like foreground color, which is base01
(let ((color (face-foreground 'shadow)))
(custom-set-faces
`(term-color-black ((t (:foreground ,color :background ,color)))))))
(add-hook 'enable-theme-functions #'my/patch-term-color-black)
(defun my/load-single-theme (theme)
"Load (and enable) single theme, disable all others."
(interactive (list (completing-read "Theme: " (custom-available-themes) nil t)))
(mapc #'disable-theme custom-enabled-themes)
(load-theme (intern theme) 'no-confirm))
(evil-define-key 'normal 'global (kbd "C-x -") #'my/load-single-theme)
(use-package solarized-theme
:demand t
:custom
(solarized-use-variable-pitch nil)
(solarized-use-more-italic t)
;; (solarized-emphasize-indicators nil) ;; this will remove the flycheck fringe background
)
(if my/monoink
(load-theme 'monoink t)
(load-theme 'solarized-light t))
(setq-default mode-line-format
(delete '(vc-mode vc-mode) mode-line-format))
(custom-set-faces
'(fixed-pitch ((t (:family nil :inherit default))))
'(fixed-pitch-serif ((t :family nil :slant italic :inherit default)))
'(line-number ((t (:height 0.9)))) ;; for pragmata, there's no light weight, let's use a smaller size
'(mode-line ((t (:height 0.9)))) ;; smaller mode-line
'(mode-line-inactive ((t (:height 0.9))))
)
;; Font size management
(defun my/change-font-size ()
"Change font size based on predefined list"
(interactive)
(when-let* ((size-str (completing-read
"Select font size:"
(mapcar #'number-to-string my/gui-font-size-choices)))
(size-val (string-to-number size-str)))
(when (> size-val 0)
(my/gui-font-size-set size-val))))
;; NOTE: there's no way to implement auto-changing function
;; because my external monitor shares the same resolution with my laptop monitor
(evil-define-key nil 'global
(kbd "C-x =") #'my/change-font-size)
;; (add-to-list 'face-font-rescale-alist '(".*CJK.*" . 0.75))
) ;; }}}
(progn ;; workspace management {{{
(setq my/persp-profiles
;; the nil entry should contain all variables below and act as fallback values.
;; this is required because that, when switching profiles, some variables (belonging to some other packages)
;; may not be defined yet, so we would always need default values.
`((nil . ((url-proxy-services . nil)
(pr-review-ghub-username . nil)
(pr-review-ghub-host . nil)))
("pony@home" . ((url-proxy-services . (("http" . "192.168.0.72:9877")
("https" . "192.168.0.72:9877")))
(pr-review-ghub-username . "z1k-pony")
(pr-review-ghub-host . "github.corp.pony.ai/api/v3")))
))
;; simulate i3-like numbered workspace using perspective.el
(use-package perspective
:demand t
:custom
(persp-mode-prefix-key (kbd "C-c C-p"))
(persp-initial-frame-name "0")
(persp-show-modestring nil)
(tab-bar-new-tab-choice 'clone) ;; we create perspective AFTER creating tab, so the new-tab itself should not change window layout
:config
(persp-mode)
(tab-bar-mode)
(defun my/persp-use-profile (profile-name)
(interactive (list (completing-read "Profile (empty to reset): "
(mapcar #'car (cdr my/persp-profiles)))))
(unless persp-mode
(error "Only works when persp-mode is enabled"))
(when (equal (persp-current-name) persp-frame-global-perspective-name)
(error "Cannot apply profile to global perspective"))
(let* ((kvs (alist-get profile-name my/persp-profiles nil nil 'equal))
(default-kvs (alist-get nil my/persp-profiles)))
(when (null kvs)
(setq profile-name nil))
(setq kvs (append kvs `((my/persp-profile-name . ,profile-name))))
(dolist (kv kvs)
;; firstly, set default variable
(set (car kv) (alist-get (car kv) default-kvs nil))
;; secondly, make it perspective-local
(persp-make-variable-persp-local (car kv))
;; thirdly, set the profile's value
(set (car kv) (cdr kv))))
(force-mode-line-update))
(defun my/persp-tab-sort-key (s)
(if (equal s "0")
`(10 . ,s)
(cons (string-to-number s) s)))
;; hook perspective into tab-bar
(progn
(defun my/persp-tab-tabs (&optional frame)
(let* ((curr (persp-curr frame))
(persps (perspectives-hash frame)))
(mapcar (lambda (name)
(let* ((p (gethash name persps))
(local-vars (persp-local-variables p)))
`(,(if (eq curr p) 'current-tab 'tab)
(name . ,(concat name " "
(when-let ((profile (car (alist-get 'my/persp-profile-name local-vars))))
(format "[%s]" profile))))
(persp-name . ,name))))
;; remove GLOBAL
(sort (delete persp-frame-global-perspective-name (hash-table-keys persps))
:key #'my/persp-tab-sort-key))))
(setq tab-bar-tabs-function #'my/persp-tab-tabs)
(defun my/persp-tab-post-select (_prev-tab tab)
(persp-switch (alist-get 'persp-name tab)))
(add-hook 'tab-bar-tab-post-select-functions #'my/persp-tab-post-select)
(defun my/persp-tab-post-open (_)
(persp-new (number-to-string (floor (time-to-seconds (current-time))))))
(add-hook 'tab-bar-tab-post-open-functions #'my/persp-tab-post-open)
(defun my/persp-tab-close (tab is-last-tab)
(unless is-last-tab
(persp-kill (alist-get 'persp-name tab))))
(add-hook 'tab-bar-tab-pre-close-functions #'my/persp-tab-close))
(defun my/cleanup-empty-persps ()
;; kill perspectives that:
;; 1. is not current
;; 2. is not initial
;; 3. only contains single scratch buffer
;; 4. the scratch buffer is not modified
(let ((cur-name (persp-current-name))
(all-names (persp-names))
killed-any)
(dolist (name all-names)
(unless (member name `(,cur-name ,persp-initial-frame-name))
(let ((bufs (persp-buffers (gethash name (perspectives-hash)))))
(when (and (length= bufs 1)
(string-prefix-p "*scratch*" (buffer-name (car bufs)))
(not (buffer-modified-p (car bufs))))
(persp-kill name)
(setq killed-any t)))))
(when killed-any
(force-mode-line-update))))
(add-hook 'persp-switch-hook #'my/cleanup-empty-persps)
;; keybindings
(progn
(defun my/persp-kill-current ()
(interactive)
(persp-kill (persp-current-name))
(force-mode-line-update))
(evil-define-key nil 'global
(kbd "s-q") #'my/persp-kill-current)
(dolist (n (number-sequence 0 9))
(evil-define-key nil 'global
(kbd (format "s-%d" n))
(lambda ()
(interactive)
(persp-switch (format "%s" n))
(force-mode-line-update))))
))
) ;; }}}
(progn ;; minibuffer completion {{{
(use-package recentf
:config
(defun my/recentf-keep-predicate (file)
(and (not (file-remote-p file))
(file-readable-p file)))
(setq recentf-keep '(my/recentf-keep-predicate)))
(use-package orderless
:custom
(completion-styles '(orderless basic))
(completion-category-overrides nil)
(completion-category-defaults nil)
;; (completion-category-overrides '((file (styles basic partial-completion))))
:config
;; https://github.com/minad/vertico/blob/0831da48fe75a173a27eb1ff2837777c80f0a2f4/vertico.el#L296
;; https://github.com/minad/vertico/issues/27#issuecomment-1057924544
(let ((orig-fn (symbol-function 'orderless-highlight-matches)))
(defun my/orderless-reset-function ()
(fset 'orderless-highlight-matches orig-fn))
(add-hook 'minibuffer-setup-hook #'my/orderless-reset-function)))
(use-package vertico
:demand t
:custom
(vertico-sort-function nil)
:config
(vertico-mode)
(define-key vertico-map (kbd "C-j") #'vertico-next) ;; originally C-n
(define-key vertico-map (kbd "C-k") #'vertico-previous)
(define-key vertico-map (kbd "C-n") #'next-history-element) ;; originally M-n
(define-key vertico-map (kbd "C-p") #'previous-history-element)
;; `vertico-exit-input' default is "M-RET". It's used to submit empty string.
;; https://github.com/minad/vertico#submitting-the-empty-string
(define-key vertico-map (kbd "<C-return>") #'vertico-exit-input))
(use-package vertico-directory
:after vertico
;; More convenient directory navigation commands
:bind (:map vertico-map
("RET" . vertico-directory-enter)
("DEL" . vertico-directory-delete-char)
("M-DEL" . vertico-directory-delete-word))
;; Tidy shadowed file names
:hook (rfn-eshadow-update-overlay . vertico-directory-tidy))
;; (use-package marginalia
;; :demand t
;; :config (marginalia-mode))
(use-package consult
:my/env-check
(executable-find "rg")
:custom
(consult-narrow-key "<")
(consult-fd-args '("fd --full-path --color=never --type f"))
;; xref
(xref-show-xrefs-function #'consult-xref)
(xref-show-definitions-function #'consult-xref)
:hook (minibuffer-setup . my/setup-consult-completion-in-minibuffer)
:init
(evil-define-key '(insert emacs normal motion) 'global
(kbd "C-t") #'my/consult-buffer-term-only
(kbd "C-r") #'my/consult-buffer
(kbd "C-S-r") #'my/consult-buffer-all-persp)
(evil-define-key 'normal 'global
(kbd "g s") #'consult-imenu ;; LSP would integrate with imenu to provide file symbols
(kbd "g S") #'consult-imenu-multi
(kbd "C-h i") #'consult-info
(kbd "C-/") #'consult-line
(kbd "C-?") #'consult-ripgrep)
:commands
(my/consult-buffer
my/consult-buffer-all-persp
my/consult-buffer-term-only)
:config
(consult-customize consult-fd :initial "#^^^#") ;; search all files, enter "fast" filter by default
(recentf-mode 1)
(defun my/setup-consult-completion-in-minibuffer ()
(setq-local completion-in-region-function #'consult-completion-in-region))
(setq consult-ripgrep-args (string-replace " --search-zip" "" consult-ripgrep-args))
(defun my/consult-setup-change-dir (cmd)
(let* ((change-dir-fn
(lambda ()
(interactive)
(let ((dir default-directory)
(orig-input (ignore-errors (buffer-substring-no-properties
(1+ (minibuffer-prompt-end)) (point-max)))))
(run-at-time 0 nil
(lambda ()
(setq dir (read-directory-name "Change directory: "))
(setq this-command cmd)
(funcall cmd dir orig-input)))
(minibuffer-quit-recursive-edit))))
(map (make-sparse-keymap))
(map-var (intern (format "my/consult-change-dir-%s-map" cmd))))
(define-key map (kbd "C-d") change-dir-fn)
(set map-var map)
(eval `(consult-customize ,cmd :keymap ,map-var))))
(mapcar #'my/consult-setup-change-dir
'(consult-ripgrep consult-git-grep consult-grep consult-fd consult-find))
(defun my/consult-persp-predicate (buf)
(or (not (boundp persp-mode))
(not persp-mode)
;; does not include global perspective
;; because getting buffers from global perspective is slow and messes the recent buffer list
(persp-is-current-buffer buf)))
;; consult buffers
(setq my/consult--source-term-buffer
`(
:name "Term"
:narrow ?t
:category buffer
:face consult-buffer
:state ,#'consult--buffer-state
:annotate ,(lambda (cand)
(let ((buf (get-buffer cand)))
(concat
(truncate-string-to-width
(or (and (buffer-local-boundp 'eat-terminal buf)
(buffer-local-value 'eat-terminal buf)
(eat-term-title (buffer-local-value 'eat-terminal buf)))
"")
(floor (* 0.2 (window-body-width))) 0 ?\s)
" "
(buffer-local-value 'default-directory buf))))
:items ,(lambda () (consult--buffer-query
:sort 'visibility
:as #'buffer-name
:predicate 'my/consult-persp-predicate
:mode '(vterm-mode eat-mode)))))
(defun my/consult-buffer-annotate (cand)
(let ((buf (get-buffer cand)))
(when (buffer-file-name buf)
(buffer-local-value 'default-directory buf))))
;; similar to consult--source-buffer, excluding terminal, limit to current perspective
(setq my/consult--source-buffer
`(
:name "Buffer"
:narrow ?b
:category buffer
:face consult-buffer
:history buffer-name-history
:state ,#'consult--buffer-state
:default t
:annotate ,#'my/consult-buffer-annotate
:items ,(lambda () (consult--buffer-query
:sort 'visibility
:as #'buffer-name
:predicate 'my/consult-persp-predicate
:exclude (cons (rx bos (or "*vterm" "*eat")) consult-buffer-filter)))))
;; similar to above, but for all perspectives (used to add buffer to current persp)
;; also no preview (preview would add buffer to current persp)
(setq my/consult--source-buffer-all-persp
`(
:name "All Buffer"
:narrow ?*
:category buffer
:face consult-buffer
:history buffer-name-history
:annotate ,#'my/consult-buffer-annotate
:action ,#'consult--buffer-action
:items ,(lambda () (consult--buffer-query
:sort 'visibility
:as #'buffer-name
:exclude (cons (rx bos (or "*vterm" "*eat")) consult-buffer-filter)))))
(defun my/consult-buffer ()
(interactive)
(let ((consult-buffer-sources '(my/consult--source-buffer
consult--source-hidden-buffer
consult--source-recent-file
consult--source-project-buffer-hidden
consult--source-project-recent-file-hidden
consult--source-project-root-hidden)))
(consult-buffer)))