-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1865 lines (1604 loc) · 77.3 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
;;; -*- lexical-binding: t; -*-
(unless (boundp 'package-archives)
(package-initialize))
(require 'cl-lib)
(unless (fboundp 'use-package)
(defmacro use-package (&rest body)
"Very hacky macro to wrap each statement in a"
`(progn
,@(cl-map 'list
(lambda (stmt)
(message "%s" stmt)
`(ignore-errors ,stmt)
)
body
)
)
)
)
;; Main use is to have my key bindings have the highest priority
;; https://github.com/kaushalmodi/.emacs.d/blob/master/elisp/modi-mode.el
(defvar my-mode-map (make-sparse-keymap)
"Keymap for `my-mode'.")
;;;###autoload
(define-minor-mode my-mode
"A minor mode so that my key settings override annoying major modes."
;; If init-value is not set to t, this mode does not get enabled in
;; `fundamental-mode' buffers even after doing \"(global-my-mode 1)\".
;; More info: http://emacs.stackexchange.com/q/16693/115
:init-value t
:lighter " my-mode"
:keymap my-mode-map)
;;;###autoload
(define-globalized-minor-mode global-my-mode my-mode my-mode)
;; https://github.com/jwiegley/use-package/blob/master/bind-key.el
;; The keymaps in `emulation-mode-map-alists' take precedence over
;; `minor-mode-map-alist'
(add-to-list 'emulation-mode-map-alists `((my-mode . ,my-mode-map)))
;; Turn off the minor mode in the minibuffer
(defun turn-off-my-mode ()
"Turn off my-mode."
(my-mode -1))
(defun my/vc-git-editor-command (command)
"command is a git subcommand that requires an editor.
example usage: (my/vc-git-editor-command \"rebase -i HEAD~3\")"
(interactive "P")
(unless server-mode (server-force-delete) (server-mode))
(let ((command (if command command (read-string "command: git "))))
(compile (concat "GIT_EDITOR=\"emacsclient\" bash -c \"git " command "\""))))
(defun my/vc-git-rebase-i (&optional branch)
"if branch isn't supplied from arg, prompt for it"
(interactive)
(let ((revision (if branch branch (read-string "revision: "))))
(my/vc-git-editor-command (concat "rebase -i " revision))))
(defun my/vc-git-rebase-abort ()
(interactive)
(compile "git rebase --abort"))
(defun my/vc-git-rebase-continue ()
(interactive)
(compile "git rebase --continue"))
(defun my/vc-git-fetch ()
(interactive)
(compile "git fetch -v"))
(defun my/vc-get-remote ()
(let* ((remote-names (split-string (shell-command-to-string "git remote")))
(remote-urls
(mapcar (lambda (r)
(cons r
(string-trim
(shell-command-to-string (concat "git remote get-url " r)))))
remote-names))
(annotation-fn (lambda (candidate) (cdr (assoc candidate remote-urls))))
(completion-extra-properties `(:annotation-function ,annotation-fn))
(remote (completing-read "Remote: " remote-urls)))
remote))
(defun my/vc-set-remote-url ()
(interactive)
(let* ((remote (my/vc-get-remote))
(curr-url (string-trim
(shell-command-to-string (concat "git remote get-url " remote))))
(new-url (minibuffer-with-setup-hook (lambda () (insert curr-url))
(read-string (concat "Set url for remote <" remote ">: ")))))
(compile (concat "git remote set-url " remote " " new-url))))
(defun my/vc-set-branch-upstream ()
(interactive)
(let* ((remote (my/vc-get-remote))
(curr-branch (car (vc-git-branches)))
(upstream (minibuffer-with-setup-hook
(lambda () (insert remote "/" curr-branch))
(read-string (concat "set branch <" curr-branch "> upstream to: ")))))
(compile (concat "git branch -u " upstream))))
(defun my/vc-add-remote ())
(defun my/vc-delete-remote ())
(defun my/vc-set-remote-url ())
(defun vc-git-common-ancestor-diff (files &optional rev1 rev2 buffer _async)
"Get a difference report using Git between two revisions of FILES."
(let (process-file-side-effects
(command "diff"))
(vc-git--asciify-coding-system)
(if rev2
;; Diffing against the empty tree.
(unless rev1 (setq rev1 "4b825dc642cb6eb9a060e54bf8d69288fbee4904"))
(setq command "diff-index")
(unless rev1 (setq rev1 "HEAD")))
(if vc-git-diff-switches
(apply #'vc-git-command (or buffer "*vc-diff*")
1 ; bug#21969
files
command
"--exit-code"
(append (vc-switches 'git 'diff)
(list "-p" (concat (or rev1 "HEAD") "..." rev2) "--")))
(vc-git-command (or buffer "*vc-diff*") 1 files
"difftool" "--exit-code" "--no-prompt" "-x"
(concat "diff "
(mapconcat #'identity
(vc-switches nil 'diff) " "))
(concat rev1 "..." rev2) "--"))))
(defun vc-git-diff-advice (orig-fun &rest args)
(if (and (eq this-command #'vc-root-version-diff) (not current-prefix-arg))
(apply #'vc-git-common-ancestor-diff args)
(apply orig-fun args))
)
(advice-add #'vc-git-diff :around #'vc-git-diff-advice)
(defalias 'gt #'>)
(defalias 'gt= #'>=)
(defalias 'lt #'<)
(defalias 'lt= #'<=)
(defun neq (obj1 obj2)
"Convenience for not 'eq'"
(not (eq obj1 obj2)))
(use-package ediff :defer t
:config
(advice-remove 'ediff-quit #'disable-y-or-n-p)
(defun disable-y-or-n-p (orig-fun &rest args)
(cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) t)))
(apply orig-fun args)))
(advice-add 'ediff-quit :around #'disable-y-or-n-p)
(setq ediff-keep-variants nil)
(setq ediff-window-setup-function #'ediff-setup-windows-plain)
(setq ediff-split-window-function #'split-window-horizontally))
(defun vc-ediff-file-at-point ()
(interactive)
(when (eq major-mode 'diff-mode)
(setq my-ediff-prior-window-configuration (current-window-configuration))
(let ((old-revision (first diff-vc-revisions))
(new-revision (second diff-vc-revisions))
(file-to-diff (save-window-excursion
(diff-goto-source)
(buffer-file-name))))
(vc-version-ediff `(,file-to-diff) old-revision new-revision))))
(add-hook 'ediff-quit-hook
(lambda () (when (window-configuration-p my-ediff-prior-window-configuration)
(set-window-configuration my-ediff-prior-window-configuration)))
100)
(when (eq system-type 'gnu/linux)
(setq x-super-keysym 'meta)
(setq x-meta-keysym 'super))
(setq mac-option-modifier 'meta)
(setq mac-command-modifier 'super)
(define-key global-map (kbd "s-/") #'comment-line)
;; terminal stuff, C-/ in case we don't have iterm config
(define-key global-map (kbd "C-/") #'comment-line)
(define-key global-map (kbd "C-_") #'comment-line)
(define-key input-decode-map "\e[1;P9" (kbd "s-/"))
;; mac ligatures
(when (fboundp 'mac-auto-operator-composition-mode)
(mac-auto-operator-composition-mode))
(defun macos-term-select-text-to-clipboard (text)
(unless (eq system-type 'gnu/linux)
(shell-command (concat "echo \"" text "\" | pbcopy" ))))
;; ITERM2 MOUSE SUPPORT
(unless (or window-system (daemonp))
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
(setq interprogram-cut-function #'macos-term-select-text-to-clipboard)) ;; good enough
(winner-mode)
(defun my/set-transparency-in-terminal ()
(interactive)
(unless (string= (face-background 'default) "unspecified-bg")
(setq prev-default-face-bg (face-background 'default)))
(unless (display-graphic-p (selected-frame))
(set-face-background 'default "unspecified-bg" (selected-frame))))
(defun my/unset-transparency-in-terminal ()
(interactive)
(unless (display-graphic-p (selected-frame))
(set-face-background 'default prev-default-face-bg (selected-frame))))
(defun my/set-frame-alpha (&optional arg)
(interactive "sFrame Alpha? ")
(if
(and arg (not (string-empty-p arg)))
(set-frame-parameter nil 'alpha (string-to-number arg))
(set-frame-parameter nil 'alpha 90)))
(defun my/set-frame-alpha-background (&optional arg)
(interactive "sFrame Alpha Background? ")
(if
(and arg (not (string-empty-p arg)))
(set-frame-parameter nil 'alpha-background (string-to-number arg))
(set-frame-parameter nil 'alpha-background 90)))
(set-frame-parameter nil 'alpha-background 85)
(setq my-window-map (make-sparse-keymap))
(define-key my-window-map "u" #'winner-undo)
(define-key my-window-map "r" #'winner-redo)
(define-key my-window-map "<"
(lambda (arg) (interactive "P") (shrink-window-horizontally (if arg arg 1))))
(define-key my-window-map ">"
(lambda (arg) (interactive "P") (enlarge-window-horizontally (if arg arg 1))))
(define-key my-window-map "-"
(lambda (arg) (interactive "P") (shrink-window (if arg arg 1))))
(define-key my-window-map "+"
(lambda (arg) (interactive "P") (enlarge-window (if arg arg 1))))
(define-key my-window-map "v" #'split-window-horizontally)
(define-key my-window-map "s" #'split-window-vertically)
(define-key my-window-map "q" #'delete-window)
(define-key my-window-map "\C-w" #'other-window)
(define-key my-window-map "l" #'windmove-right)
(define-key my-window-map "\C-l" #'windmove-right)
(define-key my-window-map "h" #'windmove-left)
(define-key my-window-map "\C-h" #'windmove-left)
(define-key my-window-map "k" #'windmove-up)
(define-key my-window-map "\C-k" #'windmove-up)
(define-key my-window-map "j" #'windmove-down)
(define-key my-window-map "\C-j" #'windmove-down)
(define-key my-window-map "=" #'balance-windows)
(define-key my-window-map "o" #'maximize-window)
(define-key my-window-map "\C-o" #'delete-other-windows)
(define-key global-map (kbd "\C-w") nil)
(define-key global-map (kbd "\C-w") my-window-map)
(setq gc-cons-threshold most-positive-fixnum)
;; Lower threshold back to 8 MiB (default is 800kB)
(add-hook 'emacs-startup-hook
(lambda () (setq gc-cons-threshold (* 1024 1024 16)))) ;; 16MB
(run-with-idle-timer 2 t #'garbage-collect)
(setq inhibit-startup-screen t)
(use-package menu-bar :defer t :config (menu-bar-mode 0))
(use-package tool-bar :defer t :config (tool-bar-mode 0))
(setq viper-mode t)
(require 'viper)
(require 'rect)
(use-package scroll-bar :defer t)
(viper-mode)
(global-hl-line-mode)
(global-auto-revert-mode)
(setq auto-revert-verbose nil)
(global-visual-line-mode)
(when (fboundp #'global-visual-wrap-prefix-mode)
(global-visual-wrap-prefix-mode)
(setq visual-wrap-extra-indent 4))
(when (fboundp #'kill-ring-deindent-mode)
(kill-ring-deindent-mode))
(add-hook 'prog-mode-hook (lambda () (modify-syntax-entry ?_ "-") (modify-syntax-entry ?_ "_")))
(setq revert-without-query '(".*")) ;; allow reverting without confirm
(setq column-number-mode t)
(setq scroll-margin 8)
(setq visual-bell t)
(setq ring-bell-function 'ignore)
(setq scroll-preserve-screen-position t)
(setq eval-expression-print-level nil)
(setq eval-expression-print-length nil)
(ignore-errors
(make-directory (concat (file-name-directory user-init-file) ".local/"))
(make-directory (concat (file-name-directory user-init-file) ".local/autosave/"))
(make-directory (concat (file-name-directory user-init-file) ".local/backups/")))
(setq auto-save-file-name-transforms
`(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "/Users/jason.z/.emacs.d/.local/cache/autosave/tramp-\\2" t)
(".*" ,(concat (file-name-directory user-init-file) ".local/autosave/\\1") t)))
(setq back-directory-alist `((".*" ,(concat (file-name-directory user-init-file) ".local/backups/"))))
(define-key global-map (kbd "C-S-p") #'yank-from-kill-ring)
(setq auto-save-visited-interval 7)
(auto-save-visited-mode)
(setq backup-directory-alist `(("." . "~/.emacs.d/save-backups")))
(setq backup-by-copying t)
(setq delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t)
;;; ICOMPLETE
(use-package icomplete
:bind (:map icomplete-minibuffer-map
("C-n" . icomplete-forward-completions)
("C-p" . icomplete-backward-completions)
("C-v" . icomplete-vertical-toggle)
("RET" . icomplete-force-complete-and-exit))
:config
(setq icomplete-delay-completions-threshold 0)
(setq icomplete-compute-delay 0)
(setq icomplete-show-matches-on-no-input t)
(setq icomplete-hide-common-prefix nil)
(setq icomplete-prospects-height 10)
(setq icomplete-separator " . ")
(setq icomplete-with-completion-tables t)
(setq icomplete-in-buffer t)
(setq icomplete-max-delay-chars 0)
(setq icomplete-scroll t)
(advice-add 'completion-at-point
:after #'minibuffer-hide-completions)
(defcustom icomplete-vertical-selected-prefix-marker "» "
"Prefix string used to mark the selected completion candidate.
If `icomplete-vertical-render-prefix-marker' is t, the string
setted here is used as a prefix of the currently selected entry in the
list. It can be further customized by the face
`icomplete-vertical-selected-prefix-face'."
:type 'string
:group 'icomplete
:version "31")
(defcustom icomplete-vertical-unselected-prefix-marker " "
"Prefix string used on the unselected completion candidates.
If `icomplete-vertical-render-prefix-marker' is t, the string
setted here is used as a prefix for all unselected entries in the list.
list. It can be further customized by the face
`icomplete-vertical-unselected-prefix-face'."
:type 'string
:group 'icomplete
:version "31")
(defcustom icomplete-vertical-in-buffer-adjust-list t
"Control whether in-buffer completion should align the cursor position.
If this is t and `icomplete-in-buffer' is t, and `icomplete-vertical-mode'
is activated, the in-buffer vertical completions are shown aligned to the
cursor position when the completion started, not on the first column, as
the default behaviour."
:type 'boolean
:group 'icomplete
:version "31")
(defcustom icomplete-vertical-render-prefix-marker t
"Control whether a marker is added as a prefix to each candidate.
If this is t and `icomplete-vertical-mode' is activated, a marker,
controlled by `icomplete-vertical-selected-prefix-marker' is shown
as a prefix to the current under selection candidate, while the
remaining of the candidates will receive the marker controlled
by `icomplete-vertical-unselected-prefix-marker'."
:type 'boolean
:group 'icomplete
:version "31")
(defface icomplete-vertical-selected-prefix-face
'((t :inherit font-lock-keyword-face :weight bold :foreground "cyan"))
"Face used for the prefix set by `icomplete-vertical-selected-prefix-marker'."
:group 'icomplete
:version "31")
(defface icomplete-vertical-unselected-prefix-face
'((t :inherit font-lock-keyword-face :weight normal :foreground "gray"))
"Face used for the prefix set by `icomplete-vertical-unselected-prefix-marker'."
:group 'icomplete
:version "31")
(defun icomplete-vertical--adjust-lines-for-column (lines buffer data)
"Adjust the LINES to align with the column in BUFFER based on DATA."
(if icomplete-vertical-in-buffer-adjust-list
(let ((column
(with-current-buffer buffer
(save-excursion
(goto-char (car data))
(current-column)))))
(dolist (l lines)
(add-text-properties
0 1 `(display ,(concat (make-string column ?\s) (substring l 0 1)))
l))
lines)
lines))
(defun icomplete-vertical--add-marker-to-selected (comp)
"Add markers to the selected/unselected COMP completions."
(if (and icomplete-vertical-render-prefix-marker
(get-text-property 0 'icomplete-selected comp))
(concat (propertize icomplete-vertical-selected-prefix-marker
'face 'icomplete-vertical-selected-prefix-face)
comp)
(concat (propertize icomplete-vertical-unselected-prefix-marker
'face 'icomplete-vertical-unselected-prefix-face)
comp)))
(cl-defun icomplete--render-vertical
(comps md &aux scroll-above scroll-below
(total-space ; number of mini-window lines available
(1- (min
icomplete-prospects-height
(truncate (max-mini-window-lines) 1)))))
;; Welcome to loopapalooza!
;;
;; First, be mindful of `icomplete-scroll' and manual scrolls. If
;; `icomplete--scrolled-completions' and `icomplete--scrolled-past'
;; are:
;;
;; - both nil, there is no manual scroll;
;; - both non-nil, there is a healthy manual scroll that doesn't need
;; to be readjusted (user just moved around the minibuffer, for
;; example);
;; - non-nil and nil, respectively, a refiltering took place and we
;; may need to readjust them to the new filtered `comps'.
(when (and icomplete-scroll
icomplete--scrolled-completions
(null icomplete--scrolled-past))
(cl-loop with preds
for (comp . rest) on comps
when (equal comp (car icomplete--scrolled-completions))
do
(setq icomplete--scrolled-past preds
comps (cons comp rest))
(completion--cache-all-sorted-completions
(icomplete--field-beg)
(icomplete--field-end)
comps)
and return nil
do (push comp preds)
finally (setq icomplete--scrolled-completions nil)))
;; Then, in this pretty ugly loop, collect completions to display
;; above and below the selected one, considering scrolling
;; positions.
(cl-loop with preds = icomplete--scrolled-past
with succs = (cdr comps)
with space-above = (- total-space
1
(cl-loop for (_ . r) on comps
repeat (truncate total-space 2)
while (listp r)
count 1))
repeat total-space
for neighbor = nil
if (and preds (> space-above 0)) do
(push (setq neighbor (pop preds)) scroll-above)
(cl-decf space-above)
else if (consp succs) collect
(setq neighbor (pop succs)) into scroll-below-aux
while neighbor
finally (setq scroll-below scroll-below-aux))
;; Halfway there...
(let* ((selected (propertize (car comps) 'icomplete-selected t))
(chosen (append scroll-above (list selected) scroll-below))
(tuples (icomplete--augment md chosen))
max-prefix-len max-comp-len lines nsections)
(add-face-text-property 0 (length selected)
'icomplete-selected-match 'append selected)
;; Figure out parameters for horizontal spacing
(cl-loop
for (comp prefix) in tuples
maximizing (length prefix) into max-prefix-len-aux
maximizing (length comp) into max-comp-len-aux
finally (setq max-prefix-len max-prefix-len-aux
max-comp-len max-comp-len-aux))
;; Serialize completions and section titles into a list
;; of lines to render
(cl-loop
for (comp prefix suffix section) in tuples
when section
collect (propertize section 'face 'icomplete-section) into lines-aux
and count 1 into nsections-aux
for comp = (icomplete-vertical--add-marker-to-selected comp)
when (get-text-property 0 'icomplete-selected comp)
do (add-face-text-property 0 (length comp)
'icomplete-selected-match 'append comp)
collect (concat prefix
(make-string (max 0 (- max-prefix-len (length prefix))) ? )
(completion-lazy-hilit comp)
(make-string (max 0 (- max-comp-len (length comp))) ? )
suffix)
into lines-aux
finally (setq lines lines-aux
nsections nsections-aux))
;; Kick out some lines from the beginning due to extra sections.
;; This hopes to keep the selected entry more or less in the
;; middle of the dropdown-like widget when `icomplete-scroll' is
;; t. Funky, but at least I didn't use `cl-loop'
(setq lines
(nthcdr
(cond ((<= (length lines) total-space) 0)
((> (length scroll-above) (length scroll-below)) nsections)
(t (min (ceiling nsections 2) (length scroll-above))))
lines))
;; HACK TO BE ABLE TO USE M-<tab> TO USE NORMAL MINIBUFFER COMPLETION
(my-mode)
(when icomplete--in-region-buffer
(setq lines (icomplete-vertical--adjust-lines-for-column
lines icomplete--in-region-buffer completion-in-region--data)))
;; At long last, render final string return value. This may still
;; kick out lines at the end.
(concat " \n"
(cl-loop for l in lines repeat total-space concat l concat "\n")))))
(fido-vertical-mode)
(use-package savehist
:init
(savehist-mode))
(if (gt= emacs-major-version 30)
(setq completions-sort 'historical)
(setq completions-sort nil) ;; no sorting makes completion faster
)
(define-key minibuffer-local-completion-map "\t" #'icomplete-force-complete)
(define-key minibuffer-local-completion-map (kbd "C-<return>") #'viper-exit-minibuffer)
(define-key global-map (kbd "C-z") #'viper-mode) ;; C-z to suspend frame is annoying with viper
(setq completion-styles '(partial-completion basic) completion-category-overrides nil completion-category-defaults nil)
;; need this hook otherwise i think fido setup or something overrides the completion which is annoying
(defun my-icomplete-styles () (setq-local completion-styles '(partial-completion basic)))
(add-hook 'icomplete-minibuffer-setup-hook 'my-icomplete-styles)
(defvar my-icomplete-prev-command nil)
(defun my-icomplete-save ()
"save the prvious icomplete session"
(setq my-icomplete-prev-command this-command)
(add-hook 'post-command-hook #'my-icomplete-exit-save-input nil 'local))
(defvar my-icomplete-prev-input "")
(defun my-icomplete-exit-save-input ()
(setq my-icomplete-prev-input (minibuffer-contents-no-properties)))
(add-hook 'icomplete-minibuffer-setup-hook 'my-icomplete-save)
(defun my-icomplete-repeat ()
(interactive)
(when (and (not (equal my-icomplete-prev-command #'my-icomplete-repeat))
(commandp my-icomplete-prev-command))
(minibuffer-with-setup-hook
(lambda () (insert my-icomplete-prev-input))
(call-interactively my-icomplete-prev-command))))
(viper-map! :leader "'" #'my-icomplete-repeat)
;; insert * at the beginning so we don't have to match exactly at the beginning
;; but only in the icomplete minibuffer so we don't clash with viper minibuffer and stuff
;; NOTE: command category can slow down M-x
(defun icomplete-partial-completion-setup ()
(unless (or
(eq (icomplete--category) 'file)
)
(insert "*")))
(add-hook 'icomplete-minibuffer-setup-hook #'icomplete-partial-completion-setup)
(defvar icomplete-current-command nil)
;; insert wild card to sorta emulate orderless
(defun icomplete-partial-completion-insert-wildcard ()
(interactive)
(if (memq icomplete-current-command
(list 'viper-ex
'eww))
(insert " ")
(insert "*")))
(define-key icomplete-minibuffer-map " " #'icomplete-partial-completion-insert-wildcard)
;; this allows us to still insert spaces
(define-key viper-insert-basic-map (kbd "M-SPC SPC") (lambda () (interactive) (insert " ")))
(defun my/minibuffer-setup-hook ()
(setq icomplete-current-command this-command)
;; (setq-local icomplete-show-matches-on-no-input nil)
)
(add-hook 'minibuffer-setup-hook #'my/minibuffer-setup-hook 100)
(advice-add #'viper-search :after
(lambda (string &rest args)
(hi-lock-face-buffer string)))
;; keep highlighting after isearch
(setq lazy-highlight-cleanup nil)
(defun my/ioccur-minibuf-after-edit (beg end len)
(setq my/ioccur-string (buffer-substring-no-properties (1+ (length my/ioccur-prompt-string)) (point-max)))
(when (gt (length (string-replace ".*" "" my/ioccur-string)) 2)
(ignore-errors (occur-1 my/ioccur-string
my/ioccur-nlines-arg
(list my/occur-buffer)))))
(setq my/ioccur-prompt-string "Find: ")
(setq my/ioccur-string "")
(defun my/ioccur (arg)
"Run a pseudo interactive grep, which will incrementally update the xref buffer based on minibuffer input.
With a prefix-arg run normally and specfiy a directory"
(interactive "P")
(setq my/ioccur-string "")
(setq my/occur-buffer (current-buffer))
(setq my/ioccur-nlines-arg (when arg (prefix-numeric-value arg)))
(minibuffer-with-setup-hook
(lambda ()
(local-set-key (kbd "SPC") (lambda () (interactive) (insert ".*")))
(add-hook 'after-change-functions #'my/ioccur-minibuf-after-edit nil 'local))
(occur-1 (read-regexp my/ioccur-prompt-string)
my/ioccur-nlines-arg
(list my/occur-buffer))))
(viper-map! :mode 'occur-mode
:n "i" #'occur-edit-mode)
(viper-map! :leader
"ss" #'my/ioccur
;; not sure why but we need to rescan the imenu for our igrep xref buffer
"si" (lambda () (interactive)
(imenu--menubar-select imenu--rescan-item)
(call-interactively 'imenu)))
(defun ripgrep ()
(interactive)
(call-interactively 'grep))
(defun rripgrep ()
(interactive)
(call-interactively 'rgrep))
(defun rripgrep-multiline ()
(interactive)
(call-interactively 'rgrep))
(defun rgrep-multiline ()
(interactive)
(grep-apply-setting 'grep-command "grep -Pazo --color=auto -nH --null -e ")
(call-interactively 'rgrep))
(defun grep-options-advice ()
"A convenient way for us to put different options depending on the grep command being run.
See notes:emacs-notes-and-tips for more details."
(cond ((or (eq this-command 'ripgrep) (eq this-command 'rripgrep))
(progn
(grep-apply-setting 'grep-command "rg -nS --no-heading ") ;; for normal single file grep
(grep-apply-setting 'grep-find-template "find <D> <X> -type f <F> -exec rg <C> -nS --no-heading -H <R> /dev/null {} +"))) ;; for rgrep; uses grep-find-template
((eq this-command 'rripgrep-multiline)
(progn
(grep-apply-setting 'grep-find-template "find <D> <X> -type f <F> -exec rg <C> -nSU --no-heading -H <R> /dev/null {} +")))
((eq this-command 'rgrep-multiline)
(progn
(grep-apply-setting 'grep-find-template "find -H <D> <X> -type f <F> -exec grep -zo <C> -nH --null -e <R> \\{\\} +")))
(t (progn ;; defaults in case I want to change them later to do something different, otherwise don't really need this last case
(grep-apply-setting 'grep-find-template "find -H <D> <X> -type f <F> -exec grep <C> -nH --null -e <R> \\{\\} +")
(grep-apply-setting 'grep-command "grep --color=auto -nH --null -e ")))
)
)
(advice-add #'grep-compute-defaults :before #'grep-options-advice)
(require 'dabbrev)
;; #'dabbrev-completion resets the global variables first so we do the same
(advice-add #'dabbrev-capf :before #'dabbrev--reset-global-variables)
(add-hook 'completion-at-point-functions #'dabbrev-capf 100)
(use-package xref
:config
(setq xref-search-program
(cond ((executable-find "rg") 'ripgrep)
((executable-find "ugrep") 'ugrep)
(t 'grep)))
(setq xref-show-xrefs-function #'xref-show-definitions-completing-read)
(setq xref-show-definitions-function #'xref-show-definitions-completing-read)
(defun search-advice (orig-fun regexp)
(let ((xref-show-xrefs-function #'xref--show-xref-buffer))
(minibuffer-with-setup-hook
(lambda ()
;; for some reason this doesn't apply in xref find apropos but that's honestly ok
;; cause it uses a space separated list of words anyways
(local-set-key (kbd "M-SPC") (lambda () (interactive) (insert " ")))
(local-set-key (kbd "SPC") (lambda () (interactive) (insert ".*"))))
(funcall orig-fun regexp))))
(advice-add 'project-find-regexp :around #'search-advice)
(advice-add 'xref-find-apropos :around #'search-advice)
(advice-add 'previous-history-element :after #'end-of-line) ;; usually we want to go to end of line
(setq my/xref-vi-state-modify-map
(make-composed-keymap
nil
(make-composed-keymap
(list my/viper-vi-basic-motion-keymap
my/viper-vi-motion-g-keymap
my/viper-vi-motion-leader-keymap)
xref--xref-buffer-mode-map)))
(viper-modify-major-mode 'xref--xref-buffer-mode 'vi-state my/xref-vi-state-modify-map))
(setq enable-recursive-minibuffers t)
(setq icomplete-in-buffer t)
(setq minibuffer-visible-completions t)
(when (fboundp 'global-completion-preview-mode) (global-completion-preview-mode))
;; for default completion behavior in the *completions* buffer, should we decide to use it
(define-key completion-in-region-mode-map (kbd "C-n") #'minibuffer-next-completion)
(define-key completion-in-region-mode-map (kbd "C-p") #'minibuffer-previous-completion)
(when (boundp 'minibuffer-visible-completions-map)
(define-key minibuffer-visible-completions-map (kbd "C-n") #'minibuffer-next-completion)
(define-key minibuffer-visible-completions-map (kbd "C-p") #'minibuffer-previous-completion))
(setq completions-format 'one-column)
(setq completions-max-height 20)
(setq completions-header-format nil)
(define-error 'no-completions "completing-read-in-region: No completions")
(defun completing-read-in-region (start end collection &optional predicate)
"Prompt for completion of region in the minibuffer if non-unique.
Use as a value for `completion-in-region-function'."
(let* ((initial (buffer-substring-no-properties start end))
(limit (car (completion-boundaries initial collection predicate "")))
(all (completion-all-completions initial collection predicate (length initial)))
;; when the completion candidate list a single one, for some reason completing-read
;; will delete a bunch of lines.
;; to couteract this, we basically undo an atomic change and set the completion variable
(completion (cond
((atom all) nil)
((and (consp all) (atom (cdr all)))
(concat (substring initial 0 limit) (car all)))
(t
(setq completion
(catch 'done
(atomic-change-group
(let ((completion
(completing-read "Completion: " collection predicate nil initial)))
(throw 'done completion)))))))))
(cond (completion (completion--replace start end completion) t)
(t (signal 'no-completions nil)))))
;;(setq completion-in-region-function #'completing-read-in-region)
(setq completion-in-region-function #'completion--in-region) ;; this is default
;; similar to doom's +corfu/move-to-minibuffer
(define-key my-mode-map (kbd "M-<tab>")
(lambda ()
(interactive)
(unwind-protect
(let ((completion-in-region-function
(if completion-in-region--data #'completing-read-in-region #'completion--in-region)))
(call-interactively #'complete-symbol))
)))
(setq tab-always-indent 'complete)
(defun indent-current-line ()
"Indent the line. Stolen from the middle section of `indent-for-tab-command'."
(or (not (eq (indent--funcall-widened indent-line-function) 'noindent))
(indent--default-inside-comment)
(when (or (lt= (current-column) (current-indentation))
(not (eq tab-always-indent 'complete)))
(indent--funcall-widened (default-value 'indent-line-function)))))
;; want backtab to undo our indents basically but not do completion
(defun indent-for-backtab-command ()
"Want backtab to reindent the line if we've overintended with tab.
Otheriwse, shift backwards by tab-width."
(interactive)
(if-let ((cmd (ignore-errors
(lookup-key (symbol-value (intern (concat (symbol-name major-mode) "-map")))
[backtab]))))
(call-interactively cmd)
(let ((text-before-pt (buffer-substring-no-properties (line-beginning-position) (point)))
;; get the would be indented column then undo the changes and restore point
(indented-column (catch 'done (save-excursion
(atomic-change-group
(indent-current-line)
(throw 'done (current-column)))))))
;; only deindent if we're equal to or before the would-be indented column
;; otherwise, go back to the intended indented position
(if (lt= (current-column) indented-column)
(indent-rigidly (if (region-active-p) (region-beginning) (line-beginning-position))
(if (region-active-p) (region-end) (line-end-position))
(- tab-width))
(indent-current-line)))))
(defun indent-for-tab-check-empty-before-point (orig-fun &rest args)
"Around advice for `indent-for-tab-command'.
Insert tabs DWIM style if the current position is already indented and
we're at the beginning of the text on the current line.
ORIG-FUN is `indent-for-tab-command' and ARGS is prefix-arg for that."
(let ((text-before-pt (buffer-substring-no-properties (line-beginning-position) (point)))
;; get the would be indented column then undo the changes and restore point
(indented-column (catch 'done (save-excursion
(atomic-change-group
(indent-current-line)
(throw 'done (current-column)))))))
;; only indent if we're equal to or past the would-be indented column
(if (and (gt= (current-column) indented-column)
(string-blank-p text-before-pt))
(insert-tab current-prefix-arg)
(apply orig-fun args))))
(advice-add 'indent-for-tab-command :around #'indent-for-tab-check-empty-before-point)
(define-key viper-insert-basic-map [backtab] #'indent-for-backtab-command)
(use-package vc :config
(setq-default vc-handled-backends '(SVN Git Hg))
(setq vc-git-diff-switches '("--histogram" "--diff-algorithm=histogram"))
)
;; fallback to hunk based diff
(setq diff-font-lock-syntax 'hunk-also)
(defun diff-kill-hunk-a (og-fn &rest args)
(let* ((stage-buf (get-buffer-create "*vc-stage-diff*"))
(current-vc-revisions diff-vc-revisions)
(beg-of-file-and-junk (save-excursion (diff-beginning-of-file-and-junk) (point)))
(hunk-file-b (car (diff-hunk-file-names)))
(hunk-file-a (cadr (diff-hunk-file-names)))
(end-of-file-and-junk (save-excursion
(diff-beginning-of-file-and-junk)
(re-search-forward (concat "--- " hunk-file-a))
(re-search-forward (concat "+++ " hunk-file-b))
(point)))
(file-and-junk (buffer-substring beg-of-file-and-junk (1+ end-of-file-and-junk)))
(hunk-header-re (rx-to-string
`(: bol "@@ -" (group (1+ digit)) "," (1+ digit)
" +" (group (1+ digit)) "," (1+ digit) " @@")))
(hunk-header (save-excursion
(re-search-backward hunk-header-re)
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(hunk-header-start (progn
(string-match hunk-header-re hunk-header)
(string-to-number
(substring hunk-header (match-beginning 1) (match-end 1)))))
(hunk-bounds (diff-bounds-of-hunk))
(file-bounds (ignore-errors (diff-bounds-of-file)))
;; If the current hunk is the only one for its file, kill the
;; file header too.
(hunk-and-file-killed (save-excursion
(and file-bounds
(progn (goto-char (car file-bounds))
(= (progn (diff-hunk-next) (point))
(car hunk-bounds)))
(progn (goto-char (cadr hunk-bounds))
;; bzr puts a newline after the last hunk.
(while (looking-at "^\n")
(forward-char 1))
(= (point) (cadr file-bounds))))))
)
;;(message "killed hunk header start %s %s" hunk-header-start hunk-header)
;; kill-ring-deindent-mode can mess with whitespace in killed hunks
;; which can cause patches to not apply
(if (and (boundp 'kill-ring-deindent-mode) kill-ring-deindent-mode)
(progn
(kill-ring-deindent-mode -1)
(apply og-fn args)
(kill-ring-deindent-mode))
(apply og-fn args))
(with-current-buffer stage-buf
(display-buffer stage-buf)
(diff-mode)
(ignore-errors
(setq-local diff-vc-backend (car (vc-deduce-fileset t)))
(setq-local diff-vc-revisions current-vc-revisions))
(diff-mode)
;; if the file-headers already exist don't do anything
(let* ((found-file (condition-case nil
(save-excursion
(goto-char (point-min))
(re-search-forward (concat "--- " hunk-file-a))
(re-search-forward (concat "+++ " hunk-file-b))
t)
(error nil))))
(message "found-file %s %s" found-file file-and-junk)
(unless (or found-file hunk-and-file-killed)
(goto-char (point-max))
(insert file-and-junk))
(goto-char (point-min))
(message "wee %s %s %s" (point) hunk-file-a hunk-file-b )
(condition-case nil
(progn
(re-search-forward (concat "--- " hunk-file-a))
(re-search-forward (concat "+++ " hunk-file-b))
(message "wee %s %s %s" (point) hunk-file-a hunk-file-b )
(forward-line)
(beginning-of-line))
(error (message "WTF %s %s" hunk-file-a hunk-file-b ))
)
;; we should be in the right file in the stage buffer now
(ignore-errors
(let ((start-of-next-file
(save-excursion
(condition-case nil
(progn
(re-search-forward diff-file-header-re)
(diff-beginning-of-file-and-junk)
(point)
)
;; no next file
(error (point-max))
)
)
))
;; insert the hunk at the correct position
;; using hunk headers eg: @@ -0,0 +1,3 @@
(while
(progn
(message "before diff hunk next")
(diff-hunk-next)
;;(message "after diff hunk next")
;; don't go past the expected file
(if (> (point) start-of-next-file)
(progn
(message "after diff hunk next2a")
(goto-char start-of-next-file) nil)
;;(message "after diff hunk next2")
(let* ((stage-hunk-header (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(stage-hunk-header-start
(progn
;;(message "after diff hunk next2b")
(string-match hunk-header-re stage-hunk-header)
(string-to-number
(substring stage-hunk-header (match-beginning 1) (match-end 1)))
)
))
(message "stage hunk header start %s %s %s" stage-hunk-header-start
hunk-header-start
(> hunk-header-start stage-hunk-header-start))
(> hunk-header-start stage-hunk-header-start)
)
)
)
))
)
;;(ignore-errors (diff-hunk-next))
(message "yanking after %s"
(buffer-substring-no-properties (line-beginning-position)
(line-end-position)))
(when (looking-at hunk-header-re)
(message "goto prev line %s"
(buffer-substring-no-properties (line-beginning-position)
(line-end-position)))