-
Notifications
You must be signed in to change notification settings - Fork 2
/
.emacs
1679 lines (1612 loc) · 65.8 KB
/
.emacs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; .emacs --- initialization file.
;;; Commentary:
;;; _____ _ __ _ _ _
;;; | | \ \_/ / /`_ \ \ / | |\ | | |\ |
;;; |_| /_/ \ \_\_/ \_\/ |_| \| |_| \|
;;;
;;; [ @author TxGVNN ]
;;; Code:
(when (version< emacs-version "29")
(error "Requires GNU Emacs 29 or newer, but you're running %s" emacs-version))
(setq gc-cons-threshold most-positive-fixnum) ;; enable gcmh
(setq read-process-output-max (* 1024 1024)) ;; 1mb
;; doom-emacs:docs/faq.org#unset-file-name-handler-alist-temporarily
(defvar doom--file-name-handler-alist file-name-handler-alist)
(setq file-name-handler-alist nil)
(add-hook 'emacs-startup-hook
(lambda ()
(setq file-name-handler-alist doom--file-name-handler-alist)))
(defvar emacs-config-version "20240927.1551")
(defvar hidden-minor-modes '(whitespace-mode))
(require 'package)
(setq package-archives
'(("me" . "https://txgvnn.github.io/giaelpa/")
("melpa" . "https://melpa.org/packages/")
("gnu" . "https://elpa.gnu.org/packages/")
("nongnu" . "https://elpa.nongnu.org/nongnu/"))
package-archive-priorities '(("me" . 9)))
;; BOOTSTRAP `use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(use-package gcmh
:ensure t
:init (gcmh-mode)
:config
(setq gcmh-idle-delay 'auto)
(add-to-list 'hidden-minor-modes 'gcmh-mode))
;;; COMPLETION SYSTEM: vertico, orderless, marginalia, consult, embark
(use-package vertico
:ensure t
:init (vertico-mode)
:config
(setq vertico-cycle t)
(delete ".git/" completion-ignored-extensions)
(add-hook 'minibuffer-setup-hook #'vertico-repeat-save)
(advice-add #'vertico--format-candidate :around
(lambda (orig cand prefix suffix index _start)
(setq cand (funcall orig cand prefix suffix index _start))
(concat
(if (= vertico--index index)
(propertize "»" 'face 'vertico-current) " ") cand)))
:bind ("C-x C-r" . vertico-repeat)
(:map vertico-map
("<prior>" . vertico-scroll-down)
("<next>" . vertico-scroll-up)))
(use-package vertico-directory
:after vertico
:ensure nil
:bind (:map vertico-map ("M-DEL" . vertico-directory-delete-word))
:hook (rfn-eshadow-update-overlay . vertico-directory-tidy))
(use-package marginalia
:ensure t :defer t
:hook (after-init . marginalia-mode))
(use-package orderless
:ensure t :defer t
:custom
(completion-styles '(orderless basic))
(completion-category-defaults nil)
(completion-category-overrides
'((command (styles . (orderless+initialism)))
(symbol (styles . (orderless+initialism)))
(variable (styles . (orderless+initialism)))
(file (styles . (basic partial-completion)))
(minibuffer (styles . (orderless-initialism)))))
:config
(orderless-define-completion-style orderless+initialism
(orderless-matching-styles '(orderless-initialism
orderless-literal
orderless-regexp)))
(add-hook 'shell-mode-hook
(lambda () (setq-local completion-styles '(substring orderless)))))
(use-package consult
:ensure t :defer t
:bind
("M-g g" . consult-goto-line)
("M-g M-g" . consult-goto-line)
("M-g i" . consult-imenu)
("M-g o" . consult-outline)
("M-g m" . consult-mark)
("M-g k" . consult-global-mark)
("M-g e" . consult-flymake)
("M-s m" . consult-man)
("M-s w" . consult-line)
("M-s g" . consult-grep)
("M-s r" . consult-ripgrep)
("M-s F" . consult-find)
("M-s u" . consult-focus-lines)
("C-x / k" . consult-keep-lines)
("M-X" . consult-mode-command)
("C-x B" . consult-buffer)
(:map minibuffer-local-map ("M-r" . consult-history))
:init
;; @FIXME: Disable `consult-completion-in-region' buggy in (e)shell-mode (tramp), minibuffer (compile command)
;; (setq completion-in-region-function
;; (lambda (&rest args)
;; (apply (if (and (fboundp 'vertico-mode) vertico-mode)
;; #'consult-completion-in-region
;; #'completion--in-region) args)))
(advice-add #'multi-occur :override #'consult-multi-occur)
(advice-add #'register-preview :override #'consult-register-window)
(setq xref-show-definitions-function #'consult-xref
xref-show-xrefs-function #'consult-xref)
:config
(setq register-preview-delay 0
register-preview-function #'consult-register-format
consult-preview-key "C-l"
consult-project-function nil)
(setf (alist-get 'slime-repl-mode consult-mode-histories)
'slime-repl-input-history))
(use-package embark
:ensure t :defer t
:bind ("C-c /" . embark-act)
(:map minibuffer-local-map ("M-o" . embark-act))
(:map embark-general-map ("/" . embark-chroot))
(:map embark-region-map ("M-&" . async-shell-from-region))
(:map embark-file-map
("s" . embark-run-eat)
("S" . embark-run-shell)
("t" . embark-run-term)
("v" . magit-status-setup-buffer)
("M-+" . make-directory-and-go)
("x" . consult-file-externally))
:config
(setq embark-indicators '(embark-minimal-indicator))
;; as chroot
(defun embark-chroot (dir &optional prefix)
"Run CMD in directory DIR."
(interactive "DIn directory:\nP")
(let ((default-directory (replace-regexp-in-string "[^/]*$" "" dir))
(embark-quit-after-action t)
(action (embark--prompt
(mapcar #'funcall embark-indicators)
(embark--action-keymap 'file nil) `(,(list :type 'file :target `,dir)))))
(command-execute action)))
;; project become
(defun embark-become-project (&optional _target)
(interactive "s") ; prompt for _target and ignore it
(embark--quit-and-run
(lambda ()
(let ((use-dialog-box nil)
(this-command 'project-switch-project))
(command-execute this-command)))))
(define-key embark-general-map (kbd "p") #'embark-become-project)
;; perspective
(defun embark-persp-to-buffer (&optional _target)
(interactive "s") ; prompt for _target and ignore it
(embark--quit-and-run
(lambda () (command-execute #'persp-switch-to-buffer))))
(define-key embark-buffer-map (kbd "P") #'embark-persp-to-buffer)
;; region
(add-to-list 'embark-target-injection-hooks
'(async-shell-from-region embark--allow-edit))
;; terminal
(defun embark-run-term(&optional dir)
"Create or visit a ansi-term buffer."
(interactive "D")
(let ((default-directory (if dir (file-name-directory dir) default-directory)))
(crux-visit-term-buffer)))
(defun embark-run-shell(&optional dir)
"Create or visit a shell buffer."
(interactive "D")
(let ((default-directory (if dir (file-name-directory dir) default-directory)))
(crux-visit-shell-buffer)))
(defun embark-run-eat(&optional dir)
"Create or visit a eat buffer."
(interactive "D")
(let ((default-directory (if dir (file-name-directory dir) default-directory)))
(crux-visit-eat-buffer)))
(defun make-directory-and-go(dir)
(interactive "D")
(make-directory dir)
(find-file dir)))
(use-package embark-consult
:ensure t :defer t
:init
(with-eval-after-load 'consult
(with-eval-after-load 'embark
(require 'embark-consult))))
;;; VERSION CONTROL: git-gutter, magit, git-link
(use-package git-gutter
:ensure t :defer t
:init
(setq git-gutter:lighter ""
git-gutter:disabled-modes '("fundamental-mode"))
(defun git-gutter-mode-turn-on-custom ()
"Enable git-gutter except TRAMP."
(when (not (file-remote-p default-directory))
(git-gutter-mode +1)))
:hook
(prog-mode . git-gutter-mode-turn-on-custom)
(magit-post-refresh . git-gutter:update-all-windows)
:bind
("C-x v p" . git-gutter:previous-hunk)
("C-x v n" . git-gutter:next-hunk)
("C-x v s" . git-gutter:stage-hunk)
("C-x v r" . git-gutter:revert-hunk))
(use-package magit
:ensure t :defer t
:custom
(magit-delete-by-moving-to-trash nil)
:config
(add-hook 'magit-process-find-password-functions
'magit-process-password-auth-source)
(defun magit-find-file-at-path (project rev path)
(let ((default-directory project)
(line (string-to-number (car (last (split-string path "#L"))))))
(with-current-buffer
(magit-find-file rev (car (split-string path "#")))
(goto-char (point-min))
(forward-line (1- line)))))
(defun magit-link-at-point ()
(interactive)
(let* ((link (magit-with-toplevel
(list (abbreviate-file-name default-directory)
(magit-rev-parse "--short" "HEAD")
(magit-file-relative-name))))
(magit-link (format "(magit-find-file-at-path \"%s\" \"%s\" \"%s#L%s\")"
(car link) (cadr link) (caddr link) (line-number-at-pos))))
(kill-new magit-link)
(message magit-link)))
:bind
("C-x v f" . magit-find-file))
(use-package git-link
:ensure t :defer t
:config (setq git-link-use-commit t))
(use-package magit-todos
:ensure t :defer t
:init
(with-eval-after-load 'magit
(let ((inhibit-message t))
(magit-todos-mode)))
:custom ;; j-T in magit-status buffer
(magit-todos-branch-list nil)
(magit-todos-update nil))
;;; SEARCHING: ripgrep, anzu, engine-mode
(use-package isearch :defer t
:init
(global-set-key (kbd "M-s s") 'isearch-forward-regexp)
(global-set-key (kbd "M-s %") 'query-replace-regexp)
(define-key isearch-mode-map (kbd "M-s %") 'isearch-query-replace-regexp))
(use-package anzu
:ensure t :defer t
:hook (after-init . global-anzu-mode)
:config
(setq anzu-mode-lighter ""
anzu-replace-threshold 100)
(global-set-key [remap query-replace] 'anzu-query-replace)
(global-set-key [remap query-replace-regexp] 'anzu-query-replace-regexp)
(define-key isearch-mode-map [remap isearch-query-replace] #'anzu-isearch-query-replace)
(define-key isearch-mode-map [remap isearch-query-replace-regexp] #'anzu-isearch-query-replace-regexp))
(use-package isearch-mb
:ensure t :after anzu
:init (isearch-mb-mode)
:config
(add-to-list 'isearch-mb--after-exit #'anzu-isearch-query-replace)
(add-to-list 'isearch-mb--with-buffer #'isearch-yank-word)
(define-key isearch-mb-minibuffer-map (kbd "C-w") #'isearch-yank-word)
(define-key isearch-mb-minibuffer-map (kbd "M-%") 'anzu-isearch-query-replace)
(define-key isearch-mb-minibuffer-map (kbd "M-s %") 'isearch-query-replace-regexp))
(use-package rg :ensure t :defer t)
(use-package engine-mode
:ensure t :defer t
:config
(setq engine/browser-function 'eww-browse-url)
(defengine nixhub "https://www.nixhub.io/search?q=%s")
(defengine debian-package "https://packages.debian.org/search?searchon=names&keywords=%s")
(defengine vagrant-box
"https://app.vagrantup.com/boxes/search?provider=libvirt&q=%s&utf8=%%E2%%9C%%93")
(defengine alpine-apk-file
"https://pkgs.alpinelinux.org/contents?file=%s&path=&name=&branch=edge&arch=x86_64")
(defengine ubuntu-package
"https://packages.ubuntu.com/search?keywords=%s&searchon=names&suite=all§ion=all"))
;;; WORKSPACE: project, perspective, envrc
(use-package project :defer t
:ensure t
:custom
(project-vc-extra-root-markers '(".pc"))
(project-switch-use-entire-map t)
(project-compilation-buffer-name-function 'project-prefixed-buffer-name)
:bind
(:map project-prefix-map
("j" . project-jump-persp)
("s" . project-eat)
("S" . project-shell)
("M-x" . project-execute-extended-command)
("v" . magit-project-status))
:config
(advice-add #'project-find-file :override #'project-find-file-cd)
(defun project-find-file-cd (&optional include-all)
"Project-find-file set default-directory is project-root"
(interactive)
(let* ((pr (project-current t))
(default-directory (project-root pr))
(dirs (list default-directory)))
(project-find-file-in (thing-at-point 'filename) dirs pr include-all)))
(defun project-prefixed-buffer-name-full (mode)
(concat "*" (downcase mode) ":" default-directory "*"))
(setq project-compilation-buffer-name-function 'project-prefixed-buffer-name-full)
(defun project-eat ()
"Project eat with history"
(interactive)
(let* ((default-directory (project-root (project-current t)))
(project-shell-name (funcall project-compilation-buffer-name-function "eat"))
(shell-buffer (get-buffer project-shell-name)))
(if current-prefix-arg
(eat-hist (generate-new-buffer-name project-shell-name)
project-shell-name)
(if (get-buffer-process shell-buffer)
(pop-to-buffer-same-window shell-buffer)
(eat-hist project-shell-name project-shell-name)))))
(defun project-consult-grep (&optional initial)
"Using consult-grep(INITIAL) in project."
(interactive)
(consult-grep (project-root (project-current t)) initial))
(define-key project-prefix-map (kbd "g") #'project-consult-grep)
(define-key project-prefix-map (kbd "G") #'project-find-regexp)
(defun project-consult-ripgrep (&optional initial)
"Using consult-ripgrep(INITIAL) in project."
(interactive)
(consult-ripgrep (project-root (project-current t)) initial))
(define-key project-prefix-map (kbd "r") #'project-consult-ripgrep)
(define-key project-prefix-map (kbd "R") #'project-query-replace-regexp)
;; embark
(defun embark-on-project()
(interactive)
(require 'embark nil t)
(embark-chroot (project-root (project-current t))))
(define-key project-prefix-map (kbd "/") #'embark-on-project)
(defun project-jump-persp ()
"Just jump to persp of project."
(interactive)
(let ((dir (project-root (project-current t))))
(persp-switch dir))))
(use-package project-tasks
:ensure t :defer t
:custom
(project-tasks-separator ":")
(project-tasks-files '(".*task.*\.org$"))
:init
(with-eval-after-load 'embark
(define-key embark-file-map (kbd "P") #'project-tasks-in-dir))
:bind (:map project-prefix-map ("P" . project-tasks))
:config
(defun project-tasks-goto-task (task)
"Go to SRC of TASK in project."
(interactive "sTask: ")
(let ((file (car (split-string task project-tasks-separator)))
(task-name (mapconcat #'identity (cdr (split-string task project-tasks-separator))
project-tasks-separator)))
(if (string-empty-p task-name) ;; call on current buffer without separator. Ex: #+call: func()
(setq task-name file)
(find-file file))
(org-babel-goto-named-src-block task-name)))
(with-eval-after-load 'marginalia
(add-to-list 'marginalia-prompt-categories '("select task" . project-task)))
(with-eval-after-load 'embark
(defvar-keymap embark-project-task-actions
:doc "Keymap for actions for project-task (when mentioned by name)."
:parent embark-general-map
"j" #'project-tasks-goto-task)
(add-to-list 'embark-keymap-alist '(project-task . embark-project-task-actions))))
(use-package envrc
:ensure t :defer t
:config
(defun my/ensure-current-project (fn &rest args) ;; purcell/envrc#59
(let ((default-directory (project-root (project-current t))))
(with-temp-buffer
(envrc-mode 1)
(apply fn args))))
(advice-add 'project-compile :around #'my/ensure-current-project)
(setq envrc-none-lighter nil
envrc-on-lighter '(:propertize " env" face envrc-mode-line-on-face)
envrc-error-lighter '(:propertize " env" face envrc-mode-line-error-face))
:hook (after-init . envrc-global-mode))
(use-package perspective
:ensure t
:init
(setq persp-mode-prefix-key (kbd "C-z")
persp-initial-frame-name "0")
(persp-mode)
:bind
("C-x b" . persp-switch-to-buffer*)
("C-x x" . persp-switch-last)
("<f5>" . persp-switch-last)
(:map perspective-map ("z" . perspective-map))
:config
;; buffer
(with-eval-after-load 'marginalia
(add-to-list 'marginalia-command-categories '(persp-switch-to-buffer* . buffer)))
;; hack local var when switch
(add-hook 'persp-switch-hook #'hack-dir-local-variables-non-file-buffer)
;; persp-ibuffer
(add-hook 'ibuffer-hook
(lambda ()
(persp-ibuffer-set-filter-groups)
(unless (eq ibuffer-sorting-mode 'alphabetic)
(ibuffer-do-sort-by-alphabetic))))
(with-eval-after-load 'ibuffer
(require 'ibuf-ext)
(advice-add #'ibuffer-visit-buffer :override #'ibuffer-visit-buffer-persp)
(defun ibuffer-visit-buffer-persp (&optional single)
"Override 'ibuffer-visit-buffer with support perspective."
(interactive "P")
(let ((buffer (ibuffer-current-buffer t)))
(if (bound-and-true-p persp-mode)
(unless (persp-is-current-buffer buffer)
(let ((other-persp (persp-buffer-in-other-p buffer)))
(persp-switch (cdr other-persp)))))
(switch-to-buffer buffer)
(when single (delete-other-windows)))))
(with-eval-after-load 'project
(defun project-switch-project (dir)
"Override 'project-switch-project with support perspective."
(interactive (list (project-prompt-project-dir)))
(let ((command (if (symbolp project-switch-commands)
project-switch-commands
(project--switch-project-command)))
(default-directory dir))
(persp-switch dir)
(let ((project-current-directory-override dir))
(call-interactively command)))))
;; find-file
(advice-add #'find-file :override #'find-file-persp)
(defun find-file-persp (filename &optional wildcards)
"Override 'find-file(FILENAME WILDCARDS)."
(interactive
(find-file-read-args "Find file: "
(confirm-nonexistent-file-or-buffer)))
(if-let* ((bound-and-true-p persp-mode)
(pr (ignore-errors
(project-current nil (file-name-directory filename))))
(dir (project-root pr)))
(persp-switch dir))
(let ((value (find-file-noselect filename nil nil wildcards)))
(if (listp value)
(mapcar 'pop-to-buffer-same-window (nreverse value))
(pop-to-buffer-same-window value))))
;; compile
(with-eval-after-load 'compile
(defvar persp-compile-history (make-hash-table :test 'equal))
(defun persp--get-command-history (persp)
(or (gethash persp persp-compile-history)
(puthash persp (make-ring 16) persp-compile-history)))
(advice-add #'compilation-read-command :override #'compilation-read-command-persp)
(defun compilation-read-command-persp (command &optional prompt)
"Override compilation-read-command (COMMAND)."
(let* ((persp-name (if (bound-and-true-p persp-mode)
(persp-name (persp-curr)) "0"))
(history
(ring-elements (persp--get-command-history persp-name)))
(command (or (car history) command))
(input (read-shell-command
(format "%s `%s' [%s]: " (or prompt "Compile")
(pretty--abbreviate-directory default-directory) command) nil
'history command)))
(ring-remove+insert+extend (persp--get-command-history persp-name)
(if (string-empty-p input) command input))))
(defun detached-compile-custom (command &optional comint)
"Override detached-compile(COMMAND COMINT) to use `compilation-read-command-persp'."
(interactive
(list
(let ((command (eval compile-command t)))
(if (or compilation-read-command current-prefix-arg)
(compilation-read-command-persp command "Detached compile")
command))
(consp current-prefix-arg)))
(let* ((detached-enabled t)
(detached-session-origin (or detached-session-origin 'compile))
(detached-session-action (or detached-session-action
detached-compile-session-action))
(detached-session-mode (or detached-session-mode 'attached)))
(compile command comint)))
(advice-add #'detached-compile :override #'detached-compile-custom))
(with-eval-after-load 'savehist
(add-to-list 'savehist-additional-variables 'persp-compile-history)))
;; project-temp-root
(defvar project-temp-root "~/")
(defun project-temp-M-x (&optional prefix)
"With PREFIX we will set `project-temp-root'."
(interactive "P")
(if prefix (setq project-temp-root (read-directory-name "Select dir: ")))
(unless (fboundp 'embark-chroot) (require 'embark))
(embark-chroot project-temp-root))
(global-set-key (kbd "C-x P") #'project-temp-M-x)
;;; DISPLAY ENHANCE
(use-package smartparens
:ensure t :defer t
:config (require 'smartparens-config)
(add-hook 'multiple-cursors-mode-enabled-hook (lambda()(turn-off-smartparens-mode)))
(add-hook 'multiple-cursors-mode-disabled-hook (lambda()(turn-on-smartparens-mode)))
(add-to-list 'hidden-minor-modes 'smartparens-mode)
:bind (:map smartparens-mode-map
("C-M-f" . 'sp-forward-sexp)
("C-M-b" . 'sp-backward-sexp))
:hook ((markdown-mode prog-mode) . smartparens-mode))
(use-package rainbow-mode
:ensure t :defer t
:hook (prog-mode . rainbow-mode)
:config (add-to-list 'hidden-minor-modes 'rainbow-mode))
(use-package rainbow-delimiters
:ensure t :defer t
:hook (prog-mode . rainbow-delimiters-mode))
(use-package volatile-highlights
:ensure t
:hook (after-init . volatile-highlights-mode)
:config (add-to-list 'hidden-minor-modes 'volatile-highlights-mode))
(use-package symbol-overlay
:ensure t :defer t
:bind ("M-s H" . symbol-overlay-put)
:hook (prog-mode . symbol-overlay-mode)
:custom (symbol-overlay-priority 100)
:config
(set-face-attribute 'symbol-overlay-default-face nil :inherit 'bold :underline t)
(add-to-list 'hidden-minor-modes 'symbol-overlay-mode))
(use-package hl-todo
:ensure t :defer t
:hook (prog-mode . hl-todo-mode))
(use-package beacon
:ensure t :defer t
:hook (after-init . beacon-mode)
:config (add-to-list 'hidden-minor-modes 'beacon-mode))
;;; COMPLETION CODE: corfu, yasnippet, eglot, dumb-jump, pcmpl-args
(use-package corfu
:ensure t :defer t
:custom
(completion-cycle-threshold 3)
(corfu-auto nil)
(corfu-cycle t)
(corfu-preselect 'prompt)
(corfu-bar-width 0)
(corfu-right-margin-width 0)
:hook
((shell-mode eshell-mode comint-mode) . corfu-echo-mode)
((prog-mode text-mode) . corfu-mode)
:bind
(:map corfu-map
("M-m" . corfu-move-to-minibuffer)
("TAB" . corfu-complete-common-or-next) ;; Use TAB for cycling, default is `corfu-complete'.
([tab] . corfu-complete-common-or-next)
("S-TAB" . corfu-previous)
([backtab] . corfu-previous))
:config
(unless (display-graphic-p)
(use-package corfu-terminal
;; FIXME: codeberg.org/akib/emacs-corfu-terminal#18
:ensure t :defer t
:init (add-hook 'corfu-mode-hook #'corfu-terminal-mode)))
(defvar-local corfu-common-old nil)
(defun corfu-complete-common-or-next ()
"Complete common prefix or go to next candidate (@minad/corfu#170)."
(interactive)
(if (= corfu--total 1)
(if (not (thing-at-point 'filename))
(progn
(corfu--goto 1)
(corfu-insert))))
(let* ((input (car corfu--input))
(str (if (thing-at-point 'filename) (file-name-nondirectory input) input))
(pt (length str))
(common (try-completion str corfu--candidates)))
(if (and (> pt 0)
(stringp common)
(not (string= str common)))
(insert (substring common pt))
(if (equal common corfu-common-old)
(corfu-next)))
(setq-local corfu-common-old common)))
(put 'corfu-complete-common-or-next 'completion-predicate #'ignore)
(defun corfu-enable-in-minibuffer ()
"Enable Corfu in the minibuffer if `completion-at-point' is bound."
(when (where-is-internal #'completion-at-point (list (current-local-map)))
(setq-local corfu-auto nil)
(corfu-mode 1)))
(add-hook 'minibuffer-setup-hook #'corfu-enable-in-minibuffer)
(defun corfu-move-to-minibuffer ()
"Move completion to minibuffer instead of corfu."
(interactive)
(let ((completion-extra-properties corfu--extra)
completion-cycle-threshold completion-cycling)
(apply #'consult-completion-in-region completion-in-region--data))))
(use-package corfu-history
:after corfu
:init (corfu-history-mode)
:config
(with-eval-after-load 'savehist
(add-to-list 'savehist-additional-variables 'corfu-history)))
(use-package cape
:ensure t :defer t
:bind (("C-c p p" . completion-at-point) ;; capf
("C-c p t" . complete-tag) ;; etags
("C-c p d" . cape-dabbrev) ;; or dabbrev-completion
("C-c p h" . cape-history)
("C-c p f" . cape-file)
("C-c p k" . cape-keyword)
("C-c p s" . cape-symbol)
("C-c p a" . cape-abbrev)
("C-c p i" . cape-ispell)
("C-c p l" . cape-line)
("C-c p w" . cape-dict))
:config
(defun cape-backends-add-to-corfu-mode ()
(add-to-list 'completion-at-point-functions #'cape-file :append)
(add-to-list 'completion-at-point-functions #'cape-dabbrev :append))
:hook (corfu-mode . cape-backends-add-to-corfu-mode))
(use-package yasnippet
:ensure t :defer t
:hook (after-init . yas-global-mode)
:config
(setq yas-lighter " ¥")
(define-key yas-minor-mode-map [(tab)] nil)
(define-key yas-minor-mode-map (kbd "TAB") nil))
(use-package yasnippet-snippets
:ensure t :defer t
:config (add-to-list 'yas-snippet-dirs "~/.gxt/emacs/snippets"))
(use-package consult-yasnippet
:ensure t :defer t
:init (global-set-key (kbd "M-]") #'completion-customize)
(defun completion-customize(&optional prefix)
"Complete and Yasnippet(PREFIX)."
(interactive "P")
(if prefix
(consult-yasnippet nil)
(call-interactively 'completion-at-point))))
(use-package dumb-jump
:ensure t :defer t
:init
(add-hook 'eglot-managed-mode-hook (lambda () (add-hook 'xref-backend-functions 'dumb-jump-xref-activate t t)))
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
(use-package eglot :defer t
:commands eglot-ensure
:config
(setq eglot-disable-on-tramp t)
(defun eglot-ensure-remote (orig &rest args)
"Ensure eglot on remote."
(unless (and eglot-disable-on-tramp (file-remote-p default-directory))
(apply orig args)))
(advice-add 'eglot-ensure :around #'eglot-ensure-remote)
:custom
(eglot-report-progress nil)
(eglot-sync-connect nil)
:after (project flymake))
(use-package pcmpl-args :ensure t :defer 1)
;;; TOOLS
(use-package avy
:ensure t :defer t
:config
(setq avy-all-windows nil
avy-background t)
:bind
("M-g a" . avy-goto-char)
("M-g l" . avy-goto-line))
(use-package crux
:ensure t :defer t
:bind
("C-^" . crux-top-join-line)
("C-a" . crux-move-beginning-of-line)
("C-o" . crux-smart-open-line-above)
("C-c c" . crux-create-scratch-buffer)
("C-c d" . crux-duplicate-current-line-or-region)
("C-c M-d" . crux-duplicate-and-comment-current-line-or-region)
("C-c D" . crux-delete-file-and-buffer)
("C-c r" . crux-rename-buffer-and-file)
("C-c s" . crux-visit-eat-buffer)
("C-c t" . crux-visit-term-buffer)
("C-c S" . crux-visit-shell-buffer)
("C-h RET" . crux-find-user-init-file)
("C-x / e" . crux-open-with)
("C-x 7" . crux-swap-windows))
(use-package expreg
:ensure t :defer t
:init (define-key esc-map "@" 'expreg-expand))
(use-package move-text
:ensure t :defer t
:bind
("M-g <up>" . move-text-up)
("M-g <down>" . move-text-down))
(use-package ace-window
:ensure t :defer t
:bind ("C-x o" . ace-window)
:config
(setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
aw-scope (quote frame)))
(if (version< emacs-version "28.1")
(use-package undo-tree
:ensure t
:init (add-hook 'after-init-hook #'global-undo-tree-mode)
:config
(setq undo-tree-mode-lighter ""
undo-limit 800000 ; 800kb
undo-strong-limit 12000000 ; 12mb
undo-outer-limit 128000000 ; 128mb
undo-tree-history-directory-alist
`((".*" . ,temporary-file-directory))))
(use-package vundo
:ensure t :defer t
:init (global-set-key (kbd "C-x u") #'vundo)
:config (define-key vundo-mode-map (kbd "q") #'vundo-confirm)))
(use-package pinentry
:ensure t :defer t
:config
(require 'server)
(setq pinentry--socket-dir server-socket-dir)
:hook (after-init . pinentry-start))
(use-package multiple-cursors
:ensure t :defer t
:bind
("C-c e a" . mc/mark-all-like-this)
("C-c e n" . mc/mark-next-like-this)
("C-c e p" . mc/mark-previous-like-this)
("C-c e l" . mc/edit-lines)
("C-c e r" . mc/mark-all-in-region))
(use-package helpful
:ensure t :defer t
:init
(global-set-key [remap describe-command] 'helpful-command)
(global-set-key [remap describe-function] 'helpful-callable)
(global-set-key [remap describe-key] 'helpful-key)
(global-set-key [remap describe-macro] 'helpful-macro)
(global-set-key [remap describe-variable] 'helpful-variable)
(global-set-key [remap describe-symbol] 'helpful-symbol))
(use-package eev
:ensure t :defer 1
:config (require 'eev-load)
(define-abbrev-table 'global-abbrev-table
'(("eekcopy" " (eek \"C-x o C-p C-e C-SPC C-a M-w C-n C-x O C-e RET C-a C-y\") ;; copy output")
("datetime" "$(date +%Y%m%dT%H%M%S)")))
(defun eepitch-get-buffer-name-line()
(if (not (eq eepitch-buffer-name ""))
(format "ξ:%s "eepitch-buffer-name) ""))
(add-to-list 'mode-line-misc-info
'(:eval (propertize (eepitch-get-buffer-name-line) 'face 'warning)))
(defun eepitch-set-local-buffer-name(&rest _)
"Set `eepitch-buffer-name' to local buffer name."
(setq-local eepitch-code-tmp eepitch-code)
(setq-default eepitch-code '(error "eepitch not set up"))
(setq-local eepitch-code eepitch-code-tmp)
(setq-local eepitch-buffer-name-tmp eepitch-buffer-name)
(setq-default eepitch-buffer-name "")
(setq-local eepitch-buffer-name eepitch-buffer-name-tmp))
(defun eepitch-set-local-buffer-name-follow(&rest _)
"Set `eepitch-buffer-name' to local buffer name."
(setq-local eepitch-buffer-name (default-value 'eepitch-buffer-name)))
(advice-add #'eepitch :after #'eepitch-set-local-buffer-name)
(advice-add #'eepitch-buffer-create :after #'eepitch-set-local-buffer-name-follow)
(advice-add #'eepitch-this-line :after #'eepitch-set-local-buffer-name)
(defun eepitch-this-line-or-setup (&optional prefix)
"Setup eepitch-buffer-name if PREFIX or eval this line."
(interactive "P")
(if (not prefix)
(eepitch-this-line)
(setq-local eepitch-buffer-name (read-buffer-to-switch "Buffer: "))
(unless (get-buffer eepitch-buffer-name)
(shell eepitch-buffer-name))))
(global-set-key (kbd "<f8>") #'eepitch-this-line-or-setup))
(use-package so-long
:ensure t :defer t
:hook (after-init . global-so-long-mode))
(use-package detached
:ensure t
:custom
(detached-init-allow-list '(compile org))
(detached-terminal-data-command system-type)
:init
(defun shell-dtach (&optional buffer sockfile)
"Start dtach in shell(BUFFER).
Why not use detached, because detached doesnt run with -A"
(interactive (list nil
(and current-prefix-arg
(read-file-name "Sock file: "))))
(let* ((explicit-shell-file-name (if (executable-find "dtach")
"dtach" nil))
(file-name (or sockfile
(format "/tmp/%s.dtach"
(replace-regexp-in-string
"/" "~" default-directory))))
(explicit-dtach-args `("-A" ,file-name "-z"
"/bin/bash" "--noediting" "-login")))
(if buffer
(shell buffer)
(if sockfile
(shell-hist (format "*dtach:%s*" sockfile))
(shell-hist (format "*dtach:%s*" default-directory))))))
:config
(defun project-detached-compile ()
"Run `detached-compile' in the project root."
(declare (interactive-only compile))
(interactive)
(let ((default-directory (project-root (project-current t)))
(compilation-buffer-name-function
(or project-compilation-buffer-name-function
compilation-buffer-name-function)))
(call-interactively #'detached-compile)))
:hook (after-init . detached-init)
:bind
(([remap async-shell-command] . detached-shell-command)
("C-x M" . detached-compile)
("C-x D" . detached-list-sessions)
:map project-prefix-map
("C" . project-detached-compile)))
(use-package 0x0 :ensure t :defer t)
(use-package dpaste :ensure t :defer t)
(use-package gist
:ensure t :defer t
:config
(defun gist-ask-for-description-maybe ()
"Override to return the current file name."
(file-name-nondirectory (or (buffer-file-name) (buffer-name)))))
(use-package devdocs
:ensure t :defer t
:bind ("M-s d" . #'devdocs-lookup))
;;; CHECKER: flymake(C-h .)
(use-package flymake
:custom (flymake-mode-line-lighter "ƒ")
:config
(define-key flymake-mode-map (kbd "C-c ! l") 'flymake-show-diagnostics-buffer)
(define-key flymake-mode-map (kbd "M-g n") #'flymake-goto-next-error)
(define-key flymake-mode-map (kbd "M-g p") #'flymake-goto-prev-error)
(remove-hook 'flymake-diagnostic-functions 'flymake-proc-legacy-flymake)
:hook (prog-mode . flymake-mode))
;;; DIRED
(use-package dired :defer t
:custom
(dired-listing-switches "-alht")
:bind
(:map dired-mode-map ("E" . dired-ediff-files))
:config
(defun dired-auto-update-name (&optional suffix)
"Auto update name with SUFFIX.ext."
(interactive "p")
(let ((filename (file-name-nondirectory (dired-get-file-for-visit)))
(timestamp (format-time-string "%Y%m%dT%H%M%S")))
(rename-file filename (concat filename "_" timestamp) t)
(revert-buffer))))
(use-package diredfl
:ensure t :defer t
:init (add-hook 'dired-mode-hook 'diredfl-mode))
;;; TERM: shell, term, xclip
(setenv "PAGER" "cat")
(defun interactive-cd (dir)
"Prompt for a DIR and cd to it."
(interactive "Dcd ")
(let ((inhibit-read-only t))
(insert (concat "cd " dir)))
(pcase major-mode
('shell-mode (comint-send-input))
('eshell-mode (eshell-send-input))
('term-mode (term-send-input))))
(use-package with-editor
:ensure t :defer t
:init
(add-hook 'shell-mode-hook #'with-editor-export-editor)
(add-hook 'eshell-mode-hook #'with-editor-export-editor)
(add-hook 'term-exec-hook #'with-editor-export-editor)
(add-hook 'vterm-mode-hook #'with-editor-export-editor))
(use-package comint :defer t
:custom
(comint-input-ignoredups t)
(comint-input-ring-size 1024))
(use-package shell
:bind (:map shell-mode-map ("C-c d" . interactive-cd))
:config
(defun shell--save-history (&rest _)
"Save `shell' history."
(let ((inhibit-message t))
(comint-write-input-ring)))
(advice-add #'comint-add-to-input-history :after #'shell--save-history)
(defun shell-hist(buffer-name &optional histfile)
"Create a shell BUFFER-NAME and set `comint-input-ring-file-name' is HISTFILE."
(let* ((shell-directory-name (locate-user-emacs-file "shell"))
(histfile (or histfile buffer-name))
(comint-history-file (expand-file-name
(format "%s/%s.history" shell-directory-name
(replace-regexp-in-string
"/" "~"
(format "%s.%s" (abbreviate-file-name default-directory) histfile)))))
(comint-input-ring-file-name comint-history-file)
(comint-input-ring (make-ring 1))
(buff (get-buffer-create buffer-name)))
;; HACK: make shell-mode doesnt set comint-input-ring-file-name
(ring-insert comint-input-ring "uname")
(with-current-buffer (shell buff)
(setq-local comint-input-ring-file-name comint-history-file)
(comint-read-input-ring t)
(set-process-sentinel (get-buffer-process (current-buffer))
#'shell-write-history-on-exit))
(pop-to-buffer buff display-comint-buffer-action)
buff)))
(use-package term :defer t
:hook
(term-mode . (lambda()
(let (term-escape-char) (term-set-escape-char ?\C-x))))
:bind
(:map term-mode-map
("C-c d" . interactive-cd))
(:map term-raw-map
("M-x" . execute-extended-command)
("C-c C-y" . term-paste)
("C-c d" . interactive-cd)))
(use-package eat
:ensure t :defer t
:custom (eat-line-input-ring-size 1024)
:init
(defun eat--line--save-history (&rest _)
"Save `eat-line' history."
(let ((inhibit-message t))
(eat--line-write-input-ring)))
(advice-add #'eat-line-send-input :after #'eat--line--save-history)
(defun eat-hist(&optional buffer-name histfile)
"Create a eat BUFFER-NAME (eat-line-mode) and set `eat--line-input-ring-file-name' is HISTFILE."
(interactive (list "*eat*" nil))
(let* ((eat-buffer-name buffer-name)
(shell-directory-name (locate-user-emacs-file "shell"))
(histfile (or histfile buffer-name))
(history-file (expand-file-name
(format "%s/%s.history" shell-directory-name
(replace-regexp-in-string
"/" "~" (format "%s.%s" (abbreviate-file-name default-directory) histfile))))))
(with-current-buffer (eat)
(eat-line-mode)
(setq-local eat--line-input-ring-file-name history-file)
(ignore-errors
(eat-line-load-input-history-from-file eat--line-input-ring-file-name "bash")))
(pop-to-buffer buffer-name display-comint-buffer-action)))
:config
(define-key eat-line-mode-map [xterm-paste] #'xterm-paste)
(defun eat-kill-process-confirm (orig-fun &rest args)
(if (y-or-n-p "Kill process? ")
(apply orig-fun args)))
(advice-add 'eat-kill-process :around #'eat-kill-process-confirm)
(defvar-local eat--line-input-ring-file-name nil
"Name of the file to which the buffer's `eat--line-input-ring' is saved.")
(defun eat--line-write-input-ring ()
"Clone from `comint-write-input-ring'."
(cond ((or (null eat--line-input-ring-file-name)
(equal eat--line-input-ring-file-name "")
(null eat--line-input-ring) (ring-empty-p eat--line-input-ring))
nil)
((not (file-writable-p eat--line-input-ring-file-name))
(message "Cannot write history file %s" eat--line-input-ring-file-name))
(t
(let* ((history-buf (get-buffer-create " *Temp Input History*"))
(ring eat--line-input-ring)
(file eat--line-input-ring-file-name)
(index (ring-length ring)))
(with-current-buffer history-buf
(erase-buffer)
(while (> index 0)
(setq index (1- index))
(insert (ring-ref ring index) "\n"))
(write-region (buffer-string) nil file nil 'no-message)
(kill-buffer nil)))))))
(use-package xclip ;; -- don't use xsel
:ensure t :defer t
:init
(add-hook 'tty-setup-hook
(lambda()(require 'xclip nil t)
(ignore-errors (xclip-mode)))))
;; BUILTIN
(use-package tramp :defer t
:custom
(tramp-default-method "ssh")
(tramp-histfile-override nil)
(tramp-allow-unsafe-temporary-files t))
(use-package ediff