-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinit.el
6712 lines (5753 loc) · 210 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 --- Xu Chunyang's Emacs Configuration -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2021, 2024 Xu Chunyang
;; Author: Xu Chunyang
;; URL: https://github.com/xuchunyang/emacs.d
;;; Code:
;;; Startup
;; Custom
(setq custom-file "~/.emacs.d/custom.el")
(setq message-log-max 10000)
;; Emacs should have them, see Bug#33576
(defun version> (v1 v2) (version< v2 v1))
(defun version>= (v1 v2) (version<= v2 v1))
;;; Package Manager
(require 'package)
(setq package-user-dir (concat "~/.emacs.d/elpa-" emacs-version))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
;;; `use-package'
(unless (fboundp 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(use-package use-package
:config
(setq use-package-enable-imenu-support t)
(setq use-package-verbose 'errors)
(setq use-package-expand-minimally t)
(defmacro chunyang-use-package-keywords-add (keyword)
"Add new keyword as placeholder."
`(progn
(add-to-list 'use-package-keywords ,keyword 'append)
(defun ,(intern (format "use-package-normalize/%s" keyword)) (&rest _))
(defun ,(intern (format "use-package-handler/%s" keyword)) (&rest _))))
(chunyang-use-package-keywords-add :about)
(chunyang-use-package-keywords-add :homepage)
(chunyang-use-package-keywords-add :info)
(chunyang-use-package-keywords-add :notes))
(use-package diminish :ensure t)
;;; Extra `load-path'
(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))
;;; Require helper libraries
(require 'subr-x)
(require 'seq)
(require 'rx)
(use-package dash
:ensure t
:config
;; Will also make `it', `acc' into variable face everywhere
(dash-enable-font-lock))
;;; Initialization
;; Disable the site default settings. See (info "(emacs) Init File")
(setq inhibit-default-init t)
;; Don't show "For information about GNU Emacs and the GNU system, type C-h
;; C-a." after startup
(setq inhibit-startup-echo-area-message "xcy")
;; Use `fundamental-mode' to reduce startup time
(setq initial-major-mode 'lisp-interaction-mode)
(use-package chunyang-scratch
:preface
(defun chunyang-scratch-save ()
(ignore-errors
(with-current-buffer "*scratch*"
(write-region nil nil "~/.emacs.d/var/scratch"))))
(defun chunyang-scratch-restore ()
(let ((f "~/.emacs.d/var/scratch"))
(when (file-exists-p f)
(with-current-buffer "*scratch*"
(insert-file-contents f)
(set-buffer-modified-p nil)))))
:init
(add-hook 'kill-emacs-hook #'chunyang-scratch-save)
(add-hook 'after-init-hook #'chunyang-scratch-restore)
;; This is not a real package so don't load it
:defer t)
;; Load personal secrets
(load (expand-file-name user-login-name user-emacs-directory) 'no-error)
;; Make ~/.emacs.d clean
(use-package no-littering
:ensure t)
;; See also `bidi-paragraph-direction'; setting that non-nil might
;; speed up redisplay.
(setq bidi-paragraph-direction 'left-to-right)
;; (set-language-environment "English")
;; (setenv "LANG" "en_US.UTF-8")
;;; macOS
(defconst *is-mac* (eq system-type 'darwin))
(defconst *is-mac-port* (boundp 'mac-carbon-version-string))
(use-package ns-win
:if *is-mac*
:no-require t
:defer t
:init
;; Anne Pro 2
(setq mac-command-modifier 'meta
mac-option-modifier 'meta)
;; 自带键盘
(setq mac-command-modifier 'meta
mac-option-modifier 'control)
:preface
(defun chunyang-toggle-keyboard ()
(interactive)
(pcase mac-option-modifier
('meta
(message "anne pro 2 => builtin")
(setq mac-option-modifier 'control))
('control
(message "builtin => anne pro 2")
(setq mac-option-modifier 'meta)))))
(use-package mac-win
:disabled
:if *is-mac-port*
:config
(mac-auto-ascii-mode)
;; This is annoying
;; (key-binding (kbd "<C-tab>"))
;; => mac-next-tab-or-toggle-tab-bar
(unbind-key "<C-tab>"))
;; [[https://emacs-china.org/t/topic/5507][Mac 下给 Emacs 设置 PATH 和 exec-path - Emacs-general - Emacs China]]
;; NOTE: When PATH is changed, run the following command
;; $ sh -c 'printf "%s" "$PATH"' > .path
(when *is-mac*
(condition-case err
(let ((path (with-temp-buffer
(insert-file-contents-literally "~/.path")
(buffer-string))))
(setenv "PATH" path)
(setq exec-path (append (parse-colon-path path) (list exec-directory))))
(error (warn "%s" (error-message-string err)))))
(use-package chunyang-mac
:if *is-mac*
:commands (chunyang-mac-Terminal-send-region
chunyang-mac-Terminal-cd
chunyang-mac-iTerm-send-region
chunyang-mac-iTerm-send-string
chunyang-mac-iTerm-cd
chunyang-mac-Finder-reveal
chunyang-mac-edit-file-tags
chunyang-mac-search-tags
helm-chunyang-mac-tags
chunyang-chrome-refresh
chunyang-chrome-url
chunyang-mac-app-running-p
chunyang-mac-switch-to-app
chunyang-mac-switch-input-source
chunyang-mac-toggle-theme
chunyang-chrome-screenshot
chunyang-chrome-switch-tab))
(use-package chrome-cli
:if *is-mac*
:commands chrome-cli-switch-to-tab)
(use-package iterm2
:if *is-mac*
:commands (iterm2-send-region
iterm2-cd))
(use-package finda
:if *is-mac*
:homepage https://keminglabs.com/finda/
:about Open apps, files, Emacs buffers, Chrome tabs/history etc
:defer 2
:config
(require 'server)
(or (server-running-p) (server-mode)))
(use-package omnifocus
:commands omnifocus-capture)
(use-package helm-osx-app
:about "Launch macOS apps"
:load-path "~/src/helm-osx-app"
:commands helm-osx-app)
(use-package helm-chrome-control
:about "Control Chrome tabs"
:load-path "~/src/helm-chrome-control"
:commands helm-chrome-control)
;;; User Interface
(when (bound-and-true-p tool-bar-mode)
(tool-bar-mode -1))
(when (bound-and-true-p scroll-bar-mode)
(scroll-bar-mode -1))
(unless (memq window-system '(ns mac))
(menu-bar-mode -1))
(setq inhibit-startup-screen t)
(setq ring-bell-function #'ignore)
(fset 'yes-or-no-p #'y-or-n-p)
(setq echo-keystrokes 0.6) ; 默认 1 秒,更快地显示未完成地按键
(defun chunyang-frame-transparency-adjust ()
"Adjust current frame's transparency using <up> and <down>."
(declare (interactive-only "Use `set-frame-parameter' instead."))
(interactive)
;; If `alpha' is not a number in [0, 100], reset to 100
(pcase (frame-parameter nil 'alpha)
((and (pred numberp) n (guard (<= 0 n 100))))
(_ (setf (frame-parameter nil 'alpha) 100)))
(while (pcase (read-key (format "%2d%% Press <up> and <down> to adjust"
(frame-parameter nil 'alpha)))
((or (and 'up (let new-alpha (1+ (frame-parameter nil 'alpha))))
(and 'down (let new-alpha (1- (frame-parameter nil 'alpha)))))
(when (<= 0 new-alpha 100)
(setf (frame-parameter nil 'alpha) new-alpha))
t))))
;; (column-number-mode)
(size-indication-mode)
(defconst chunyang-mode-line-format
'(" %+ "
mode-line-buffer-identification
" "
mode-line-position
" "
mode-line-modes
mode-line-misc-info)
"A simple and clean mode-line.")
;; (substring-no-properties (format-mode-line chunyang-mode-line-format))
;; ;; =>
;; " * init.el 3% of 153k (189,0) (Emacs-Lisp) "
(setq-default mode-line-format chunyang-mode-line-format)
;; To restore
;; (setq-default mode-line-format (eval (car (get 'mode-line-format 'standard-value))))
(defun chunyang-toggle-mode-or-header-line ()
"Toggle between mode and header line for displaying the status line."
(interactive)
(if (default-value 'header-line-format)
(setq-default header-line-format nil
mode-line-format chunyang-mode-line-format)
(setq-default mode-line-format nil
header-line-format chunyang-mode-line-format)))
(use-package spaceline
:disabled
:ensure t
:config
(require 'spaceline-config)
(spaceline-spacemacs-theme))
;; Fonts
(when (memq window-system '(mac ns))
(set-face-attribute 'default nil :font "Source Code Pro-14")
(setq face-font-rescale-alist `(("STkaiti" . ,(/ 16.0 14))))
(set-fontset-font t 'han (font-spec :family "STkaiti"))
(set-fontset-font t 'cjk-misc (font-spec :family "STkaiti")))
;; (when (eq window-system 'x)
;; (set-face-attribute 'default nil :font "Source Code Pro-11"))
(use-package unicode-fonts
:disabled
:ensure t
:config (unicode-fonts-setup))
(defun chunyang-font-size-adjust ()
"Adjust font size."
(interactive)
(cl-flet ((get-size () (font-get (face-attribute 'default :font) :size))
(set-size (size)
(set-face-attribute
'default nil
:font
(format "%s-%d"
(font-get (face-attribute 'default :font) :family)
size))
t))
(while (pcase (read-key
(format
"use <up> and <down> to adjust (current %d), C-g to quit"
(get-size)))
('up (set-size (1+ (get-size))))
('down (set-size (1- (get-size))))
(?0 (set-size 13))))))
;; Theme
(use-package spacemacs-theme
:ensure t
:no-require t ; Silence byte-compiling warnings
:defer t
:init (setq spacemacs-theme-comment-bg nil
spacemacs-theme-org-height nil))
(use-package tomorrow-theme
:ensure color-theme-sanityinc-tomorrow
:defer t
:init
(load-theme 'sanityinc-tomorrow-eighties t))
(use-package tangotango-theme
:ensure t
:no-require t
:defer t)
;; Don't translate quote in `message' and `format-message'
(setq text-quoting-style 'grave)
;; Remove the default italics for C-x 8 RET, see the comment of
;; `mule--ucs-names-annotation'.
(custom-set-faces '(completions-annotations ((t))))
(use-package frame-tabs
:about Show buffer tabs in side window
:ensure t
:defer t
:preface
(defun chunyang-frame-tabs-filter (buffer _frame)
(let ((name (buffer-name buffer)))
(unless (or (eq (aref name 0) ?\s)
(string-match-p "\\`\\*helm" name))
name)))
:config
(setq frame-tabs-filter-function #'chunyang-frame-tabs-filter))
;;; Histroy
(setq history-length 100)
(setq history-delete-duplicates t)
(use-package savehist ; Minibuffer history
:config
(setq savehist-additional-variables '(extended-command-history
Info-history-list
ivy-views))
(savehist-mode))
(use-package recentf ; Recent files
:preface
(defun chunyang-recentf-save-list-for-alfred ()
"Save the recent list as JSON file for Alfred Script Filter.
See URL `https://www.alfredapp.com/help/workflows/inputs/script-filter/json/'."
(require 'json)
(when (and recentf-mode recentf-list)
(write-region
(json-encode-list
`((items
,@(mapcar
(lambda (f)
(let ((abbrev (abbreviate-file-name f))
(onlyname (file-name-nondirectory f)))
`((uid . ,abbrev)
(type . "file")
(title . ,onlyname)
(subtitle . ,abbrev)
(arg . ,abbrev)
(autocomplete . ,onlyname)
(icon . ((type . "fileicon")
(path . ,abbrev))))))
recentf-list))))
nil "~/.recentf-alfred.json" nil 'silent)))
:config
(setq recentf-max-saved-items 512
recentf-exclude '("/\\.git/.*\\'" ; Git contents
"/\\.emacs\\.d/elpa" ; ELPA
"/\\.emacs\\.d/etc/"
"/\\.emacs\\.d/var/"
"-autoloads\\.el\\'"
"\\.elc\\'"
"/TAGS\\'"))
(recentf-mode)
;; (add-hook 'kill-emacs-hook #'chunyang-recentf-save-list-for-alfred 'append)
)
(use-package bookmark
:defer t
:config
;; Save bookmark when Emacs is killed (this is the default)
(setq bookmark-save-flag t))
(use-package saveplace ; Save point position in files
:config (save-place-mode))
(use-package desktop
:disabled
:config (desktop-save-mode))
;;; Minibuffer
(setq enable-recursive-minibuffers t)
(minibuffer-depth-indicate-mode)
;; Give useful pormpt during M-! (`shell-command') etc
(use-package prompt-watcher
:preface
(defun prompt-watcher ()
(let ((prompt-fn
(lambda (prompt)
(let ((inhibit-read-only t)
(props (text-properties-at (point-min))))
(erase-buffer)
(insert prompt)
(set-text-properties (point-min) (point-max) props)))))
(pcase (list this-command (minibuffer-prompt) (and current-prefix-arg t))
('(shell-command-on-region "Shell command on region: " t)
(funcall prompt-fn "Shell command on region and replace: "))
('(shell-command "Shell command: " t)
(funcall prompt-fn "Shell command and insert output: "))
('(eshell-command "Emacs shell command: " t)
(funcall prompt-fn "Emacs shell command and insert output: "))
('(async-shell-command "Async shell command: " t)
(funcall prompt-fn "Async shell command and insert output: "))
(`(pp-eval-expression "Eval: " ,_)
(funcall prompt-fn "Pp Eval: ")))))
(define-minor-mode prompt-watcher-mode
"Watch the minibuffer prompt and customize if asking."
:global t
(if prompt-watcher-mode
(add-hook 'minibuffer-setup-hook #'prompt-watcher)
(remove-hook 'minibuffer-setup-hook #'prompt-watcher)))
:init (prompt-watcher-mode)
;; This is not a real package so don't load it
:defer t)
;; Doesn't work for helm commands
;; (read-string "Year (default 2019): " nil nil "2019")
(use-package minibuf-eldef ; Only show defaults in prompts when applicable
:defer 1.2 ; To save 0.136sec
:init
;; Must be set before minibuf-eldef is loaded
(setq minibuffer-eldef-shorten-default t)
:config
(minibuffer-electric-default-mode))
(use-package chunyang-edit-minibuffer
:bind (:map minibuffer-local-map ("C-c '" . chunyang-edit-minibuffer)))
(use-package which-key
:disabled
:ensure t
:diminish which-key-mode
:config (which-key-mode))
;;; Ivy
;; counsel -> swiper -> ivy
(use-package counsel
:disabled
;; TODO Toggle between normal Emacs completing-read & Ivy
;; :disabled
:ensure t
:diminish ivy-mode
:config
(setq ivy-do-completion-in-region nil)
(setq ivy-count-format "")
(ivy-mode)
(setq ivy-use-virtual-buffers t) ; recentf in `ivy-switch-buffer'
(setq counsel-find-file-at-point t)
;; TODO: Replace global-set-key with bind-key
(global-set-key (kbd "M-i") 'swiper)
(global-set-key (kbd "C-o") 'counsel-imenu)
(global-set-key (kbd "M-I") 'counsel-ag)
(global-set-key (kbd "C-z") 'ivy-resume)
(global-set-key (kbd "M-l") 'ivy-switch-buffer)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "M-y") 'counsel-yank-pop)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "C-h f") 'counsel-describe-function)
(global-set-key (kbd "C-h v") 'counsel-describe-variable)
(global-set-key (kbd "C-c f l") 'counsel-find-library)
(global-set-key (kbd "C-h S") 'counsel-info-lookup-symbol)
(global-set-key (kbd "C-c g") 'counsel-git)
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history))
(use-package prescient
:disabled
:ensure t
:homepage https://github.com/raxod502/prescient.el
:about Simple but effective sorting and filtering
:config (prescient-persist-mode))
(use-package ivy-prescient
:ensure t
:after ivy
:config (ivy-prescient-mode))
(use-package company-prescient
:disabled
:ensure t
:after company
:config (company-prescient-mode))
;;; selectrum
(use-package selectrum
:disabled
:homepage https://github.com/raxod502/selectrum
:load-path "~/src/selectrum"
:config (selectrum-mode))
(use-package selectrum-prescient
:after selectrum
:homepage https://github.com/raxod502/prescient.el
:load-path "~/src/prescient.el"
:config (selectrum-prescient-mode))
;;; Vertico
(use-package vertico
:ensure t
:init
(vertico-mode)
;; Different scroll margin
;; (setq vertico-scroll-margin 0)
;; Show more candidates
;; (setq vertico-count 12)
;; Grow and shrink the Vertico minibuffer
;; (setq vertico-resize t)
;; Optionally enable cycling for `vertico-next' and `vertico-previous'.
;; (setq vertico-cycle t)
)
;; Example configuration for Consult
(use-package consult
:ensure t
;; Replace bindings. Lazily loaded due by `use-package'.
:bind (;; C-c bindings in `mode-specific-map'
;; ("C-c M-x" . consult-mode-command)
;; ("C-c h" . consult-history)
;; ("C-c k" . consult-kmacro)
;; ("C-c m" . consult-man)
;; ("C-c i" . consult-info)
;; ([remap Info-search] . consult-info)
;; C-x bindings in `ctl-x-map'
;; ("C-x M-:" . consult-complex-command) ;; orig. repeat-complex-command
("M-l" . consult-buffer) ;; orig. switch-to-buffer
;; ("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
;; ("C-x 5 b" . consult-buffer-other-frame) ;; orig. switch-to-buffer-other-frame
;; ("C-x t b" . consult-buffer-other-tab) ;; orig. switch-to-buffer-other-tab
;; ("C-x r b" . consult-bookmark) ;; orig. bookmark-jump
;; ("C-x p b" . consult-project-buffer) ;; orig. project-switch-to-buffer
;; Custom M-# bindings for fast register access
;; ("M-#" . consult-register-load)
;; ("M-'" . consult-register-store) ;; orig. abbrev-prefix-mark (unrelated)
;; ("C-M-#" . consult-register)
;; Other custom bindings
("M-y" . consult-yank-pop) ;; orig. yank-pop
;; M-g bindings in `goto-map'
;; ("M-g e" . consult-compile-error)
;; ("M-g f" . consult-flymake) ;; Alternative: consult-flycheck
;; ("M-g g" . consult-goto-line) ;; orig. goto-line
;; ("M-g M-g" . consult-goto-line) ;; orig. goto-line
;; ("M-g o" . consult-outline) ;; Alternative: consult-org-heading
;; ("M-g m" . consult-mark)
;; ("M-g k" . consult-global-mark)
("C-o" . consult-imenu)
("M-g I" . consult-imenu-multi)
;; M-s bindings in `search-map'
;; ("M-s d" . consult-find) ;; Alternative: consult-fd
;; ("M-s c" . consult-locate)
;; ("M-s g" . consult-grep)
;; ("M-s G" . consult-git-grep)
("M-I" . consult-ripgrep)
("M-i" . consult-line)
;; ("M-s L" . consult-line-multi)
;; ("M-s k" . consult-keep-lines)
;; ("M-s u" . consult-focus-lines)
;; Isearch integration
;; ("M-s e" . consult-isearch-history)
;; :map isearch-mode-map
;; ("M-e" . consult-isearch-history) ;; orig. isearch-edit-string
;; ("M-s e" . consult-isearch-history) ;; orig. isearch-edit-string
;; ("M-s l" . consult-line) ;; needed by consult-line to detect isearch
;; ("M-s L" . consult-line-multi) ;; needed by consult-line to detect isearch
;; Minibuffer history
:map minibuffer-local-map
("M-s" . consult-history) ;; orig. next-matching-history-element
("M-r" . consult-history) ;; orig. previous-matching-history-element
)
;; Enable automatic preview at point in the *Completions* buffer. This is
;; relevant when you use the default completion UI.
:hook (completion-list-mode . consult-preview-at-point-mode)
;; The :init configuration is always executed (Not lazy)
:init
;; Optionally configure the register formatting. This improves the register
;; preview for `consult-register', `consult-register-load',
;; `consult-register-store' and the Emacs built-ins.
(setq register-preview-delay 0.5
register-preview-function #'consult-register-format)
;; Optionally tweak the register preview window.
;; This adds thin lines, sorting and hides the mode line of the window.
(advice-add #'register-preview :override #'consult-register-window)
;; Use Consult to select xref locations with preview
(setq xref-show-xrefs-function #'consult-xref
xref-show-definitions-function #'consult-xref)
;; Configure other variables and modes in the :config section,
;; after lazily loading the package.
:config
;; Optionally configure preview. The default value
;; is 'any, such that any key triggers the preview.
;; (setq consult-preview-key 'any)
;; (setq consult-preview-key "M-.")
;; (setq consult-preview-key '("S-<down>" "S-<up>"))
;; For some commands and buffer sources it is useful to configure the
;; :preview-key on a per-command basis using the `consult-customize' macro.
(consult-customize
consult-theme :preview-key '(:debounce 0.2 any)
consult-ripgrep consult-git-grep consult-grep
consult-bookmark consult-recent-file consult-xref
consult--source-bookmark consult--source-file-register
consult--source-recent-file consult--source-project-recent-file
;; :preview-key "M-."
:preview-key '(:debounce 0.4 any))
;; Optionally configure the narrowing key.
;; Both < and C-+ work reasonably well.
(setq consult-narrow-key "<") ;; "C-+"
;; Optionally make narrowing help available in the minibuffer.
;; You may want to use `embark-prefix-help-command' or which-key instead.
;; (define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help)
;; By default `consult-project-function' uses `project-root' from project.el.
;; Optionally configure a different project root function.
;;;; 1. project.el (the default)
;; (setq consult-project-function #'consult--default-project--function)
;;;; 2. vc.el (vc-root-dir)
;; (setq consult-project-function (lambda (_) (vc-root-dir)))
;;;; 3. locate-dominating-file
;; (setq consult-project-function (lambda (_) (locate-dominating-file "." ".git")))
;;;; 4. projectile.el (projectile-project-root)
;; (autoload 'projectile-project-root "projectile")
;; (setq consult-project-function (lambda (_) (projectile-project-root)))
;;;; 5. No project support
;; (setq consult-project-function nil)
)
(use-package emacs
:init
(setq read-extended-command-predicate
#'command-completion-default-include-p))
(use-package orderless
:ensure t
:init
;; Configure a custom style dispatcher (see the Consult wiki)
;; (setq orderless-style-dispatchers '(+orderless-consult-dispatch orderless-affix-dispatch)
;; orderless-component-separator #'orderless-escapable-split-on-space)
(setq completion-styles '(orderless basic)
completion-category-defaults nil
completion-category-overrides '((file (styles partial-completion)))))
(use-package corfu
:ensure t
;; Optional customizations
:custom
(corfu-cycle t) ;; Enable cycling for `corfu-next/previous'
(corfu-auto t) ;; Enable auto completion
;; (corfu-separator ?\s) ;; Orderless field separator
;; (corfu-quit-at-boundary nil) ;; Never quit at completion boundary
;; (corfu-quit-no-match nil) ;; Never quit, even if there is no match
;; (corfu-preview-current nil) ;; Disable current candidate preview
;; (corfu-preselect 'prompt) ;; Preselect the prompt
;; (corfu-on-exact-match nil) ;; Configure handling of exact matches
;; (corfu-scroll-margin 5) ;; Use scroll margin
;; Enable Corfu only for certain modes.
;; :hook ((prog-mode . corfu-mode)
;; (shell-mode . corfu-mode)
;; (eshell-mode . corfu-mode))
;; Recommended: Enable Corfu globally. This is recommended since Dabbrev can
;; be used globally (M-/). See also the customization variable
;; `global-corfu-modes' to exclude certain modes.
:init
(global-corfu-mode))
;; A few more useful configurations...
(use-package emacs
:init
;; TAB cycle if there are only few candidates
(setq completion-cycle-threshold 3)
;; Emacs 28: Hide commands in M-x which do not apply to the current mode.
;; Corfu commands are hidden, since they are not supposed to be used via M-x.
;; (setq read-extended-command-predicate
;; #'command-completion-default-include-p)
;; Enable indentation+completion using the TAB key.
;; `completion-at-point' is often bound to M-TAB.
(setq tab-always-indent 'complete))
;;; Helm
;; Emacs 27
;; (setq completion-styles '(flex))
;; (fido-mode)
(use-package helm
:disabled
:ensure t
:defer t
:init
;; Disable Helm's C-x c bindings
(setq helm-command-prefix-key nil)
:preface
(defun chunyang-helm-project-search (directory)
(interactive (list (if current-prefix-arg
default-directory
(or (locate-dominating-file "." ".git")
default-directory))))
(let ((default-directory directory))
(helm-grep-ag (expand-file-name default-directory) nil)))
:bind (("M-l" . helm-mini)
("M-i" . helm-occur)
("M-I" . chunyang-helm-project-search)
("C-o" . helm-imenu)
("M-x" . helm-M-x)
("C-x C-f" . helm-find-files)
("C-x C-d" . helm-browse-project)
("M-y" . helm-show-kill-ring)
("C-z" . helm-resume)
("C-h a" . helm-apropos)
("C-c f l" . helm-locate-library))
:diminish helm-mode
:config
;; https://emacs-china.org/t/helm-mode-eshell-tab/15006
(setq helm-mode-handle-completion-in-region nil)
;; Check `helm-completion-style'
;; (setq completion-styles
;; '(basic
;; partial-completion
;; emacs22
;; helm-flex))
(setq helm-completion-style 'flex)
(helm-mode)
;; Persist recent used of filtered candidates
(helm-adaptive-mode)
(setq helm-window-prefer-horizontal-split 'decide)
(define-minor-mode chunyang-helm-window-hack-mode
"Hack helm window display."
:global t
(let ((action '("\\`\\*helm"
(display-buffer-in-side-window)
(window-height . 0.4))))
(if chunyang-helm-window-hack-mode
(progn
(add-to-list 'display-buffer-alist action)
(setq helm-display-function #'display-buffer))
(setq display-buffer-alist
(delete action display-buffer-alist))
(let ((standard-value (eval (car (get 'helm-display-function 'standard-value)))))
(setq helm-display-function standard-value)))))
;; (chunyang-helm-window-hack-mode)
(setq helm-grep-ag-command
"rg --color=always --smart-case --no-heading --line-number %s %s %s")
(with-eval-after-load 'helm-unicode
(define-advice helm-unicode-insert-char (:override (_candidate) candidates)
(insert
(mapconcat
(lambda (x) (substring x -1))
(helm-marked-candidates)
" ")))))
(use-package helm-ring
:defer t
:config
(defun chunyang-helm-kill-ring-diff (_str)
(let ((marked (helm-marked-candidates)))
(unless (= (length marked) 2)
(user-error "Need exact 2 marked candidates, got %d" (length marked)))
(apply #'chunyang-ediff-strings-wordwise marked )))
(add-to-list 'helm-kill-ring-actions
(cons "Ediff" #'chunyang-helm-kill-ring-diff)
t))
(use-package wgrep-helm
:ensure t
:defer t)
(use-package helm-ls-git
:ensure t
:config
(setq helm-ls-git-default-sources
'(helm-source-ls-git-buffers
helm-source-ls-git))
:defer t)
(use-package helm-fd
:about find files using fd <https://github.com/sharkdp/fd> with helm
:commands helm-fd)
(use-package helm-descbinds
:ensure t
;; XXX how to lazy loading?
:config
(setq helm-descbinds-window-style 'split-window)
(helm-descbinds-mode))
;;; Buffers, Windows and Frames
(use-package uniquify ; Make buffer names unique (turn on by default)
:defer t
:config (setq uniquify-buffer-name-style 'forward))
(use-package autorevert ; Auto-revert buffers of changed files
:diminish auto-revert-mode
:config (global-auto-revert-mode))
(use-package simple
:no-require t
:config
(define-advice goto-line (:before (&rest _) preview-line-number)
"Preview line number when prompting for line number.
Idea from URL `https://www.reddit.com/r/emacs/comments/as83e2/weekly_tipstricketc_thread/egu2sve'."
(interactive
(lambda (spec)
(if (and (boundp 'display-line-numbers) ; `display-line-numbers' was added in Emacs 26.1
(not display-line-numbers))
(unwind-protect
(progn (display-line-numbers-mode)
(advice-eval-interactive-spec spec))
(display-line-numbers-mode -1))
(advice-eval-interactive-spec spec))))))
(use-package chunyang-simple
:preface
;; 我已经用了用了 M-RET,结果 Org/Eww/Gnus 也用
;; 一个处理按键冲突的简单解决方法
(defun chunyang-escape (key)
"Execute KEY as it is in `global-map'."
;; NOTE M-RET not working in `markdown-mode'
;; (interactive "kExecute Key in global-map: ")
(interactive (list (vector (read-key "Execute Key in global-map: "))))
(call-interactively (lookup-key (current-global-map) key))
(message nil))
(defun chunyang-dispatch-key (key)
(interactive "kDispatch Key: ")
(require 'seq)
(let* ((commands (seq-filter #'commandp
(mapcar (lambda (map) (lookup-key map key))
(current-active-maps))))
(collection (mapcar #'symbol-name commands))
(command (intern (completing-read
(concat (key-description key) " ")
collection nil t nil nil (cadr collection)))))
(command-execute command)))
:bind (("C-x 3" . chunyang-split-window-right)
("C-x 2" . chunyang-split-window-below)
("C-h t" . chunyang-switch-scratch)
;; C-\ runs `toggle-input-method' by default
;; ("C-\\" . chunyang-dispatch-key)
:map lisp-interaction-mode-map
("C-c C-l" . chunyang-scratch-clear)
:map messages-buffer-mode-map
("C-c C-l" . chunyang-clear-messages-buffer))
:commands (chunyang-window-click-swap
chunyang-cycle-filename-format
chunyang-display-number-as-char
chunyang-delete-region
chunyang-center-current-line
chunyang-count-top-level-expression))
(use-package chunyang-misc
:commands (chunyang-open-another-emacs
chunyang-starify-region
chunyang-timer
chunyang-random-word
chunyang-format-as-binary
;; Clipboard
chunyang-insert-image-from-clipboard
;; QR Code
chunyang-scan-qr-code-from-screen
chunyang-qrdecode
chunyang-qrencode
;; 迅雷
chunyang-decode-thunder-link
chunyang-encode-thunder-link
;; https://www.quotes.net
chunyang-random-quote
;; HTML -> Markdown
chunyang-yank-html
;; Eval
chunyang-eval-in-other-emacs
chunyang-fake-module-reload
;; Insert datetime
chunyang-helm-time
chunyang-indent-align-cycle
;; Randome project name
chunyang-glitch-name
;; Emacs -Q with straight.el
chunyang-straight-emacs-Q-command
chunyang-straight-git-version))
(use-package chunyang-buffers
:commands chunyang-kill-buffer-query-function
:init (add-hook 'kill-buffer-query-functions #'chunyang-kill-buffer-query-function)
:preface
(defun chunyang-kill-all-buffers (&optional except-current-buffer)
"Kill all buffers.
With prefix argument, don't kill the current buffer."
(interactive "P")
(let ((buffers-to-kill
(seq-filter
(lambda (buffer)
(let ((name (buffer-name buffer)))
;; Ignore uninteresting buffers
(and (not (string-prefix-p " " name))
(not (member name '("*scratch*" "*Messages*")))
(not (and except-current-buffer (eq buffer (current-buffer)))))))
(buffer-list))))
(mapc #'kill-buffer buffers-to-kill)
(delete-other-windows)))
(defun chunyang-kill-dir-buffers (dir)
"Kill all buffers whose `default-directory' is under DIR."
(interactive (list default-directory))
(setq dir (expand-file-name dir))
(let ((count 0))
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(and (string-prefix-p dir (expand-file-name default-directory))
(kill-buffer buffer)
(cl-incf count))))
(message "Killed %d buffer%s in %s"
count
(if (> count 1) "s" "")
(abbreviate-file-name dir))))
(defun chunyang-kill-invisible-buffers ()