forked from Nephyrin/NephScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs
3275 lines (2858 loc) · 122 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
;; -*- mode: Emacs-Lisp; -*-
;;
;; Misc
;;
;; TODO Make ~/.emacs a stub that sets up auto-compile and native comp and then loads ~/.emacs.d/init.el
;; That'll make sure the functions defined here are compiled and so on.
;; Don't load outdated .elc files, it's basically never what was intended.
(setq load-prefer-newer t)
(require 'comp)
(setq native-comp-speed 3)
(setq native-comp-always-compile t)
;; Set to block native compilation. Also need to nuke the ~/.emacs.d/eln-cache folder.
;;(add-to-list 'native-comp-bootstrap-deny-list ".*")
;;(add-to-list 'native-comp-deferred-compilation-deny-list ".*")
;; Global libraries macros in here (and also )
(add-to-list 'load-path "~/.emacs.d/dash") ; dependency of ht
(add-to-list 'load-path "~/.emacs.d/emacs-ht")
(require 'ht)
(require 'cl) ;; Used so xe and friends can run some crap
(setq redisplay-dont-pause t)
(setq inhibit-eval-during-redisplay nil)
(setq fast-but-imprecise-scrolling t)
(setq jit-lock-chunk-size 100)
(setq jit-lock-defer-time 0)
(setq jit-lock-stealth-load nil)
(setq jit-lock-stealth-nice 0.01)
(setq jit-lock-stealth-time 0.2)
(add-to-list 'load-path "~/.emacs.d/neph-autoloads")
;; Disable silly "type Y-E-S" prompts
(fset 'yes-or-no-p 'y-or-n-p)
;; Always answer yes to: File %s is %s on disk. Make buffer %s, too?
;; (There's no variable to control this)
(defun neph-y-or-n-p (orig-func prompt &rest args)
(if (string-match "^File .* is .* on disk. Make buffer .*, too\\? $"
prompt)
t
(apply orig-func prompt args)))
(advice-add 'y-or-n-p :around #'neph-y-or-n-p)
; This just makes things slower. Maybe useful on spinning disks?
(setq cache-long-line-scans nil)
(setq cache-long-scans nil)
; Clear suspend-frame binding to use C-z as a prefix
(global-unset-key (kbd "C-z"))
(put 'upcase-region 'disabled nil)
; Fix x clipboard
(setq x-select-enable-primary nil)
(setq x-select-enable-clipboard t)
(setq mouse-drag-copy-region nil)
(when (boundp 'x-cut-buffer-or-selection-value)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value))
;(global-set-key (kbd "C-{") 'clipboard-yank)
;(global-set-key (kbd "C-}") 'clipboard-kill-ring-save)
;(global-set-key (kbd "C-M-}") 'clipboard-kill-region)
;(global-set-key "\C-w" 'clipboard-kill-region)
;(global-set-key "\M-w" 'clipboard-kill-ring-save)
;(global-set-key "\C-y" 'clipboard-yank)
(setq yank-pop-change-selection t)
(setq save-interprogram-paste-before-kill t)
(setq inhibit-startup-message t)
(setq-default indent-tabs-mode nil)
(setq js-indent-level 2)
(setq tab-width 2)
(global-auto-revert-mode t)
(setq backup-directory-alist
`((".*" . , "~/.emacscache/autosave")))
(setq auto-save-file-name-transforms
`((".*" , "~/.emacscache/autosave" t)))
(setq backup-directory-alist `(("." . "~/.emacscache/backup")))
(setq delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t)
(setq vc-follow-symlinks t)
(require 'uniquify)
(setq uniquify-buffer-name-style (quote post-forward))
; (global-ede-mode t)
; Hide toolbar, hide menu in console mode
(menu-bar-mode -1)
; OS X builds can lack these, check
(when (functionp 'scroll-bar-mode) (scroll-bar-mode -1))
(when (functionp 'tool-bar-mode) (tool-bar-mode -1))
(setq split-width-threshold 170)
(setq split-height-threshold 1000)
(require 'speedbar)
(speedbar-change-initial-expansion-list "buffers")
(global-set-key [f8] 'speedbar-get-focus)
(global-set-key (kbd "C-c C-f") 'find-dired)
; Trailing spaces and whitespace
(require 'whitespace)
(global-whitespace-mode)
; Options list of whitespace to mess with, 'face' option uses faces per type
; instead of replacement chars
(setq whitespace-style (quote (face trailing tabs)))
;;
;; Libraries. These are all dumped on the path before loading things
;;
(add-to-list 'load-path "~/.emacs.d/bui.el")
(add-to-list 'load-path "~/.emacs.d/compat.el")
(add-to-list 'load-path "~/.emacs.d/emacs-spinner")
(add-to-list 'load-path "~/.emacs.d/s.el")
(add-to-list 'load-path "~/.emacs.d/f.el")
;;
;; Electric mode tweaks
;;
;; Custom inhibits on top of the normal behavior, since some choices are pretty bad by default.
(setq electric-pair-inhibit-predicate
(lambda (c)
(let ((whitespace-forward (or (looking-at "[ \n\t\"]") (looking-at "$")))
(whitespace-backward (or (eq (point) 2) (looking-back "[ \n\t]." 2)))
(is-quote (char-equal c ?\")))
;; Inhibit quotes unless there is whitespace on either side of the point.
;;
;; Inhibit non-quotes unless there is whitespace ahead fo the point (because `foo(` should work, but `foo"' is
;; less sensical for auto-pairing)
(if (or (not whitespace-forward)
(and (not whitespace-backward) is-quote))
;; Inhibit
t
;; Otherwise chain to normal inhibit behavior
(electric-pair-conservative-inhibit c)))))
;;
;; Mark & Mark Ring
;;
(global-set-key (kbd "C-x p") 'pop-to-mark-command)
(setq set-mark-command-repeat-pop t)
;;
;; Snippets
;;
; Recompile all .elc. The 0 tells us to compile files that have no .elc
; already. Yes it should be 0, not t. Append t as third arg to force.
; (byte-recompile-directory "~/.emacs.d/" 0)
; or command line:
; emacs -batch -f batch-byte-compile *.el
;; (progn
;; (setq kill-ring nil)
;; (setq buffer-undo-tree nil)
;; (garbage-collect))
;;
;; Desktop saving
;;
;; Autosave desktop as emacs-server-desktop for the server, otherwise leave
;; disabled unless asked for
(require 'desktop)
(setq desktop-path '("~/.emacs.d/"))
(setq desktop-dirname "~/.emacs.d/")
(setq desktop-base-file-name "emacs-desktop")
(setq desktop-base-lock-name "emacs-desktop.lock")
(setq desktop-restore-eager 0)
(setq desktop-save t)
(add-to-list 'desktop-globals-to-save 'register-alist)
(when (or server-mode (daemonp))
(setq desktop-base-file-name "emacs-server-desktop")
(setq desktop-base-lock-name "emacs-server-desktop.lock")
(desktop-save-mode 1))
;;
;; Xterm color
;;
(add-to-list 'load-path "~/.emacs.d/xterm-color")
(require 'xterm-color)
(add-to-list 'load-path "~/.emacs.d/eterm-256color")
;(require 'eterm-256color) FIXME debug-init
;(add-hook 'term-mode-hook #'eterm-256color-mode)
;;
;; Protobuf mode
;;
;; Shipped with protobuf, so load if present
(if (require 'protobuf-mode nil t)
(add-to-list 'auto-mode-alist '("\.proto$" . protobuf-mode))
;; Basically functions
(message "NEPH -- No protobuf-mode available, using c-mode for .proto")
(add-to-list 'auto-mode-alist '("\.proto$" . c-mode)))
;;
;; Markdown mode
;;
(add-to-list 'load-path "~/.emacs.d/markdown-mode")
(autoload 'markdown-mode "markdown-mode"
"Major mode for editing Markdown files" t)
(add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;;
;; Evil
;;
(add-to-list 'load-path "~/.emacs.d/evil")
(require 'neph-evil-autoload)
(global-set-key (kbd "C-z C-M-SPC") 'evil-mode)
;;
;; Highlight Symbol
;;
(add-to-list 'load-path "~/.emacs.d/highlight-symbol")
(require 'highlight-symbol)
;; This hack fixes highlight-symbol-mode perf, but breaks the explicit commands
;; See https://github.com/nschum/highlight-symbol.el/issues/26
;(defun highlight-symbol-add-symbol-with-face (symbol face)
; (save-excursion
; (goto-char (point-min))
; (while (re-search-forward symbol nil t)
; (let ((ov (make-overlay (match-beginning 0)
; (match-end 0))))
; (overlay-put ov 'highlight-symbol t)
; (overlay-put ov 'face face)))))
;
;(defun highlight-symbol-remove-symbol (_symbol)
; (dolist (ov (overlays-in (point-min) (point-max)))
; (when (overlay-get ov 'highlight-symbol)
; (delete-overlay ov))))
(global-set-key (kbd "C-z H") 'highlight-symbol)
(global-set-key (kbd "C-z C-H") 'highlight-symbol-remove-all)
(setq highlight-symbol-idle-delay 0.3)
;;
;; Rust mode
;;
(add-to-list 'load-path "~/.emacs.d/rust-mode")
(add-to-list 'load-path "~/.emacs.d/rustic")
(require 'rustic)
;;
;; Lua mode
;;
(add-to-list 'load-path "~/.emacs.d/lua-mode")
(autoload 'lua-mode "lua-mode"
"Major mode for editing Lua files" t)
(add-to-list 'auto-mode-alist '("\\.lua\\'" . lua-mode))
;; Defaults to 3. What in the goddamn.
(setq lua-indent-level 2)
;;
;; Command helpers
;;
(defun neph-buffer-command (cmd &optional name callback buffer-init-func)
"Runs a command into a new buffer, noting when it finishes, with a callback"
(interactive "MCommand: \n")
(let* ((envcmd (concat "env -u NEPH_COLOR_TERM " cmd))
(name (if name name "neph-buffer-command"))
(buf (generate-new-buffer (concat name ": " cmd))))
(switch-to-buffer buf nil t)
(when buffer-init-func (with-current-buffer buf
(apply buffer-init-func nil)))
(insert (concat "<command: " cmd ">"))
(newline)
(let ((proc (start-process-shell-command
(concat name "-proc")
buf envcmd)))
(set-process-sentinel
proc
`(lambda (process signal)
(when (eq (process-status process) 'exit)
(message (concat ,name " finished"))
(with-current-buffer (process-buffer process)
(rename-buffer (concat (buffer-name) " <command finished>"))
(newline)
(insert "<command finished>")
(when ,callback
(apply ,callback (list process)))
(goto-char (point-min)))))))))
(defun neph-p4inter (args)
"Runs the p4inter command with args"
(interactive "Mp4inter: \n")
(neph-buffer-command
(concat "p4inter " args) "neph-p4inter"
(lambda (process)
(delete-trailing-whitespace)
(highlight-regexp "^Change" 'git-commit-note))))
;; FIXME We force-wrap env around it in neph-buffer-command
(defun neph-evmk (args)
"Runs the p4inter command with args"
(interactive "Mevmk: \n")
(neph-buffer-command
(concat "evmk " args)
"neph-evmk"
;; callback
nil
;; buffer-init-func
(lambda ()
(font-lock-mode t)
(compilation-minor-mode t))))
;;
;; Htmlize
;;
(add-to-list 'load-path "~/.emacs.d/htmlize")
(autoload 'htmlize-buffer "htmlize" "htmlize" t)
;; Hacky thing to htmlize a region and send it straight to browser
;;(defun neph-html-region ()
;; (interactive)
;; (let* ((regionp (region-active-p))
;; (beg (and regionp (region-beginning)))
;; (end (and regionp (region-end)))
;; (buf (current-buffer))
;; ;; poor man's with-temp-killring (requires let*)
;; (kill-ring (list "temp kill ring"))
;; (kill-ring-yank-pointer kill-ring))
;; (with-temp-buffer
;; ;;(switch-to-buffer (current-buffer) nil t)
;; (rename-buffer "*Neph HTMLIZE Temp Buffer*" t)
;; (font-lock-mode -1) ;; We want to keep the face properties from the source buffer always
;; (insert-buffer-substring-as-yank buf beg end)
;; (with-current-buffer (htmlize-buffer)
;; (write-file "~/.emacs.d/htmlize-temp.htm"))))
;; ;;(kill-buffer)))
;; ;; This is the way the help actually suggests you prevent it from opening this buffer.
;; (let ((display-buffer-alist (cons '("\\*Async Shell Command\\*" (display-buffer-no-window))
;; display-buffer-alist)))
;; (async-shell-command "chromium ~/.emacs.d/htmlize-temp.htm")))
(defun neph-html-region ()
(interactive)
(require 'htmlize)
(with-current-buffer
(htmlize-region (point) (mark))
(write-file "~/.emacs.d/htmlize-temp.htm"))
;;(kill-buffer)))
(start-process-shell-command "neph-html-region" nil "chromium ~/.emacs.d/htmlize-temp.htm"))
(defun neph-html-copy ()
"Copy the selected region to the clipboard as html. Requires awk and xclip be available."
(interactive)
(require 'htmlize)
;; Detect some modes that clash with htmlize, set mode to inline-css for maximal CnP compatibility
(let ((ghl (and (boundp 'global-hl-line-mode) global-hl-line-mode))
(htmlize-output-type 'inline-css)
(htmlize-pre-style 't))
;; Disable incompatible modes, run htmlize, re-enable
(when ghl (global-hl-line-mode -1))
(with-current-buffer
(htmlize-region (point) (mark))
(write-file "~/.emacs.d/htmlize-temp.htm"))
(when ghl (global-hl-line-mode 1)))
;; Awful awk script to skip all the doctype/html/body/head document tags and just select the 'pre'
;; tag, then stuff it onto the clipboard
(start-process-shell-command "neph-html-copy" nil
(concat "awk -i inplace '/^ *<pre/ { inpre=1; };"
" /^ *<\\/pre/ { inpre=0; print };"
" inpre { print };'"
" ~/.emacs.d/htmlize-temp.htm && "
"xclip -quiet -i -selection clipboard -target text/html"
" ~/.emacs.d/htmlize-temp.htm")))
;(global-set-key (kbd "C-z M-w") 'neph-html-copy)
;;
;; htmlfontify
;;
;; Sometimes htmlize fails on some buffers, sometimes htmlfontify does :-/ :-/
;; :height 96 should be 10pt, ends up at 9pt, increase this a little because idk
(with-eval-after-load "htmlfontify"
(setq hfy-font-zoom 1.09))
;; Like neph-html-region, uses htmlfontify to fontify things, pops up in a browser
;; WIP STILL DOESNT WORK WITH ansi-term
(defun WIP-neph-hfy-html-region ()
(interactive)
;; hfy breaks on buffers that have non-font-lock propertization on text in emacs 25, just capture current text into a
;; non-font-lock buffer.
(let* ((regionp (region-active-p))
(beg (and regionp (region-beginning)))
(end (and regionp (region-end)))
(buf (current-buffer))
;; poor man's with-temp-killring (requires let*)
(kill-ring (list "temp kill ring"))
(kill-ring-yank-pointer kill-ring))
;;(hfy-optimizations (list 'skip-refontification)))
;; (flet ((hfy-force-fontification () (message "Prevented hfy-force-fontification")) ;; See above comment
;; (hfy-fontified-p () (message "Lying about fontification") t))
(with-temp-buffer
;;(switch-to-buffer (current-buffer) nil t)
;;(font-lock-mode t) ;; We want to keep the face properties from the source buffer always
(let ((tempbuf (current-buffer)))
(flet ((hfy-buffer () (message "Intercepted hfy-buffer") tempbuf))
;; (copy-to-buffer (buffer start end)
;; (message "Intercepted copy-to-buffer")
;; (let ((thisbuf (current-buffer)))
;; (with-current-buffer (get-buffer buffer)
;; (insert-buffer-substring thisbuf start end))
;; (with-current-buffer (get-buffer "tmp.tmp")
;; (insert-buffer-substring thisbuf start end)))))
(switch-to-buffer buf)
;;(rename-buffer "*Neph HTMLIZE Temp Buffer*" t)
;;(insert-buffer-substring-as-yank buf beg end)
(hfy-fontify-buffer)
(switch-to-buffer tempbuf)
(write-file "~/.emacs.d/htmlize-temp.htm")))))
;; This is the way the help actually suggests you prevent it from opening this buffer.
(let ((display-buffer-alist (cons '("\\*Async Shell Command\\*" (display-buffer-no-window))
display-buffer-alist)))
(async-shell-command "chromium ~/.emacs.d/htmlize-temp.htm")))
;;
;; Multi-term
;;
;(load-file "~/.emacs.d/multi-term.el")
;(setq multi-term-program "/bin/bash")
;
;(global-set-key (kbd "C-x t") 'multi-term-dedicated-open)
;; Term key overrides
(defun term-send-raw-C-z ()
"Send a raw Control-z value to term."
(interactive)
(term-send-raw-string (kbd "C-z")))
(with-eval-after-load 'term
(define-key term-raw-map (kbd "C-y") 'term-paste)
;; Allow C-z to escape
(define-key term-raw-map (kbd "C-z") nil)
;; But make C-z C-z send a real C-z
(define-key term-raw-map (kbd "C-z C-z") 'term-send-raw-C-z))
;;
;; Emacs Interactive Notebook (jupyter)
;;
;; FIXME This should autoload, but the janky-AF http proxy disabling needs to be looked at
;; (something inherits it during the load process and I can't stop it)
(add-to-list 'load-path "~/neph/emacs.d/auto-complete")
(add-to-list 'load-path "~/neph/emacs.d/js2-mode")
(add-to-list 'load-path "~/neph/emacs.d/polymode")
(add-to-list 'load-path "~/neph/emacs.d/skewer-mode")
(add-to-list 'load-path "~/neph/emacs.d/emacs-web-server")
(add-to-list 'load-path "~/neph/emacs.d/emacs-websocket")
(add-to-list 'load-path "~/neph/emacs.d/emacs-ipython-notebook/lisp")
(defun neph-load-ein ()
"Janky function to load ein late."
(interactive)
(setenv "HTTP_PROXY" nil)
(setenv "HTTPS_PROXY" nil)
(setenv "http_proxy" nil)
(setenv "https_proxy" nil)
(require 'ein-notebook)
(define-key ein:notebook-mode-map (kbd "C-c <C-return>") 'ein:worksheet-execute-autoexec-cells)
(define-key ein:notebook-mode-map (kbd "C-c <C-S-return>") 'neph-ein-restart-and-autoexec))
(defun neph-ein-restart-and-autoexec ()
"Restart the current notebook's kernel and then execute all autoexec cells."
(interactive)
;; This inlines ein:kernel-restart-session since it doesn't take a callback.
(ein:aif ein:%notebook%
(let ((kernel (ein:$notebook-kernel it)))
(ein:kernel-delete-session
kernel
(lambda (kernel)
(ein:events-trigger (ein:$kernel-events kernel) 'status_restarting.Kernel)
(ein:kernel-retrieve-session
kernel 0
(lambda (kernel)
(ein:events-trigger (ein:$kernel-events kernel)
'status_restarted.Kernel)
(ein:notebook-execute-autoexec-cells ein:%notebook%))))))
(message "Not in notebook buffer")))
;;
;; ECB
;;
;(add-to-list 'load-path "~/.emacs.d/ecb")
;(require 'ecb)
;(setq ecb-show-sources-in-directories-buffer 'always)
;(setq ecb-layout-name "left7")
;(setq ecb-tip-of-the-day nil)
;(setq ecb-windows-width 0.1)
;
;;; Quiet startup warning
;(setq ecb-options-version "2.40")
;
;(global-set-key (kbd "C-z q") 'ecb-activate)
;(global-set-key (kbd "C-z Q") 'ecb-deactivate)
;;
;; Color identifiers mode
;;
(add-to-list 'load-path "~/.emacs.d/color-identifiers-mode")
(require 'color-identifiers-mode)
;;
;; Helm
;;
(add-to-list 'load-path "~/.emacs.d/emacs-async") ; helm dep
(add-to-list 'load-path "~/.emacs.d/helm")
(require 'helm-autoloads)
;;(require 'helm)
;;(require 'helm-config)
;;(require 'helm-files)
(helm-mode 1)
;; Since 215005e25718 helm's default score func is just crazy broken
;; and puts really-fuzzy matches above extremely-direct matches
(setq helm-fuzzy-default-score-fn 'helm-fuzzy-helm-style-score)
;;Default: (setq helm-fuzzy-default-score-fn 'helm-fuzzy-flex-style-score)
(global-set-key (kbd "C-z F") (lambda () (interactive) (helm-find-1 (read-directory-name "Run find in directory: " nil "" t))))
(global-set-key (kbd "M-x") 'helm-M-x)
(global-set-key (kbd "C-z b") 'helm-mini)
(global-set-key (kbd "C-z C-b") 'helm-bookmarks)
(global-set-key (kbd "C-z C-o") 'helm-occur)
(global-set-key (kbd "C-z C-S-o") 'occur)
(global-set-key (kbd "C-M-y") 'helm-show-kill-ring)
(global-set-key (kbd "C-z <C-tab>") 'helm-imenu)
;; Blows up helm on emacs 25 right now
;; (setq helm-follow-mode-persistent nil)
(define-key isearch-mode-map (kbd "C-o") 'helm-occur-from-isearch)
(define-key isearch-mode-map (kbd "C-S-o") 'isearch-occur)
(when (executable-find "ack-grep")
(setq helm-grep-default-command "ack-grep -Hn --no-group --no-color --smart-case --type-set IGNORED:ext:P,map --noIGNORED %p %f"
helm-grep-default-recurse-command "ack-grep -H --no-group --no-color --smart-case --type-set IGNORED:ext:P,map --noIGNORED %p %f"))
;; helm-grep ripgrep ;; -color=always --colors 'match:fg:black' --colors 'match:bg:yellow'
(setq helm-grep-ag-command "rg --smart-case --no-heading --line-number %s %s %s")
(setq helm-grep-ag-pipe-cmd-switches '())
(require 'grep)
(setq grep-find-ignored-files (append grep-find-ignored-files
'( ;; Binaries
"*.pdb" "*.map" "*.P" "*.dylib" "*.lib" "*.a" "*.dSYM" "*.app" "*.framework" "*.dll"
"*.so.0" "*.so" "*.o" "*.exe" "*.dbg" "*.sys" "*.h.gch"
;; Python compiled thing
"*.pyd" "*.pyc"
;; Archives
"*.zip" "*.rar" "*.7z" "*.xz" "*.bz2" "*.gz" "*.tar" "*.dmg" "*.deb" "*.rpm" "*.iso"
"*.msi"
;; Source engine cruft
"*.vtf" "*.vvd" "*.vcd" "*.phy" "*.mdl" "*.dmx" "*.bsp" "*.vpk" "*.vtx"
"*.fbx" "*.vmt" "*.vmf" "*.dds" "*.smd" "*.nav" "*.vcs" "*.pcf" "*.dem"
"*.lmp"
"soundcache/*.manifest"
"reslists/*.txt"
"reslists_xbox/*.lst"
"*.xsiaddon"
;; Misc
"*.ma" "*.mll" ; Maya
"*.cache"
"*.svn-base"
"*.sdf" ; Visual studio database thing
"*.al" ; Perl cruft
"*.ppm"
"*.vcproj" "*.vcxproj"
;; PS3 compiled file... thing
"*.prx" "*.sprx"
;; Misc Media
"*.raw" "*.ani" "*.bik" "*.dat" "*.ttf" "*.pdf" "*.max"
;; Images
"*.tga" "*.jpg" "*.jpeg" "*.png" "*.bmp" "*.psd" "*.cbr" "*.icns" "*.ico" "*.gif"
;; Sound
"*.wav" "*.ogg" "*.mp3"
;; Video
"*.h264" "*.mkv" "*.avi" "*.mp4" "*.mov" "*.webm"
;; Oneoffs
"ip-country-region-city-latitude-longitude-isp.csv"
"engine_symbols.txt"
"dedicated_symbols.txt"
"staging_latest_good.txt")))
(when (functionp 'remove-duplicates)
(remove-duplicates grep-find-ignored-files :test 'string=))
;; Use ncdu to look at not-ignored files in a directory in this list:
;; (concat "ncdu " (mapconcat (lambda (x) (concat "--exclude '" x "'")) grep-find-ignored-files " "))
;;
;; FZF
;;
;; FIXME Ignore stuff like .ccls-cache by customizing process-environment with defadvice:
;; (let ((process-environment
;; (cons (concat "FZF_DEFAULT_COMMAND=git ls-files")
;; process-environment))
(add-to-list 'load-path "~/.emacs.d/fzf")
(require 'fzf)
(global-set-key (kbd "C-z C-S-f") 'fzf)
(setq fzf/args "--no-hscroll --print-query -x --no-unicode")
(setq fzf/window-height 50)
;;
;; Helm Swoop
;;
(add-to-list 'load-path "~/.emacs.d/helm-swoop")
(require 'helm-swoop)
(global-set-key (kbd "C-z M-s") 'helm-swoop)
(global-set-key (kbd "C-z M-S") 'helm-multi-swoop-all)
;;
;; Helm AG and Helm RG and RG they're all different
;;
(add-to-list 'load-path "~/.emacs.d/helm-ag")
(add-to-list 'load-path "~/.emacs.d/helm-rg")
(add-to-list 'load-path "~/.emacs.d/wgrep") ;; For rg.el
(add-to-list 'load-path "~/.emacs.d/rg.el")
(require 'helm-ag)
(require 'helm-rg)
(require 'rg)
;; Define a minor mode to lock rg bounce buffers into read-only and provide some quick access keys
;;
;; Pressing the default bind (C-c C-e) will turn off this mode and unlock helm-rg--bounce's editing mode, which is
;; useful, but not by default when I just want a persistent buffer to visit search results.
(defun neph-rg-bounce-navigation-mode-handler ()
"Default hook for neph-rg-bounce-navigation-mode."
(if neph-rg-bounce-navigation-mode
(progn
(message "Neph: Visit Mode")
(read-only-mode 1))
(message "Neph: Edit Mode")
(read-only-mode -1)))
(define-minor-mode neph-rg-bounce-navigation-mode
"Mode that puts helm-rg bounce buffers into read-only navigation rather than editing."
:keymap '())
(add-hook 'neph-rg-bounce-navigation-mode-hook 'neph-rg-bounce-navigation-mode-handler)
(define-key helm-rg--bounce-mode-map (kbd "C-c C-e") #'neph-rg-bounce-navigation-mode)
(define-key neph-rg-bounce-navigation-mode-map (kbd "g") #'helm-rg--bounce-refresh)
(define-key neph-rg-bounce-navigation-mode-map (kbd "r") #'helm-rg--bounce-refresh-current-file)
(define-key neph-rg-bounce-navigation-mode-map (kbd "d") #'helm-rg--bounce-dump)
(define-key neph-rg-bounce-navigation-mode-map (kbd "D") #'helm-rg--bounce-dump-current-file)
(define-key neph-rg-bounce-navigation-mode-map (kbd "RET")
;; This function always calls 'alternate-method', so let bind that to normal method for the "normal visit" keybind.
(lambda () (interactive)
(let ((helm-rg-display-buffer-alternate-method
helm-rg-display-buffer-normal-method))
(helm-rg--visit-current-file-for-bounce))))
(define-key neph-rg-bounce-navigation-mode-map (kbd "C-o") #'helm-rg--visit-current-file-for-bounce)
(define-key neph-rg-bounce-navigation-mode-map (kbd "e") #'helm-rg--expand-match-context)
(define-key neph-rg-bounce-navigation-mode-map (kbd "E") #'helm-rg--spread-match-context)
(define-key neph-rg-bounce-navigation-mode-map (kbd "q") #'kill-this-buffer)
;; Defaults on
(add-hook 'helm-rg--bounce-mode-hook 'neph-rg-bounce-navigation-mode)
(setq helm-ag-insert-at-point t)
;; (setq helm-ag-always-set-extra-option t)
(defun helm-ff-helm-do-ag ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action '(lambda (basedir)
(let ((parent (file-name-directory (directory-file-name basedir)))
(default-directory nil))
(helm-do-ag nil (list parent)))))))
;; FIXME not needed?
;; (put 'helm-ff-helm-do-ag 'helm-only nil)
(define-key helm-find-files-map (kbd "M-g") 'helm-ff-run-grep-ag)
(add-to-list 'helm-sources-using-default-as-input helm-source-do-ag)
(add-to-list 'helm-sources-using-default-as-input 'helm-ag-source)
;; Helm's auto-affinity thing seems to massively slow it down when the system is
;; under heavy load, even if that load is in low priority compilation cgroups.
;;
;; A common query with all files in cache goes from 20s -> 2s for me with this,
;; similar to running the query on an idle system. It sounds like this affinity
;; thing is trying to work around poor OS-level behavior to begin with, but with
;; it disabled the Right Thing™ seems to happen on my systems.
(setq helm-ag-base-command (concat helm-ag-base-command " --noaffinity"))
;; Keys to walk a visible helm-ag buffer
(defun neph-helm-ag-next (arg)
(interactive "P")
(let* ((direction (if arg -1 1))
(agbuf (or (get-buffer "*helm ag results*") (get-buffer "*hgrep*")))
(agwin (get-buffer-window agbuf)))
(flet ((notdone () (if (and (looking-at "$") (looking-back "^"))
(progn (message "End of results") nil)
t))
(move () (next-logical-line direction) (beginning-of-line)))
(when agbuf
(if agwin
(progn (select-window agwin)
(move)
(when (notdone)
(helm-ag-mode-jump-other-window)))
(switch-to-buffer agbuf)
(move)
(when (notdone)
(helm-ag-mode-jump)))))))
(defun neph-helm-ag-prev (arg)
(interactive "P")
(neph-helm-ag-next (if arg nil 1)))
(defun neph-helm-ag-update ()
(interactive)
(let ((agbuf (get-buffer "*helm ag results*")))
(when agbuf
(with-current-buffer agbuf
(helm-ag--update-save-results)))))
(global-set-key (kbd "C-M-z C-M-n") 'neph-helm-ag-next)
(global-set-key (kbd "C-M-z C-M-p") 'neph-helm-ag-prev)
(global-set-key (kbd "C-M-z C-M-g") 'neph-helm-ag-update)
;; RG version (needs helm-projectile-ag fix)
;(setq helm-ag-base-command "rg --vimgrep --no-heading")
;; Older fix:
;;(setq helm-ag-base-command "rg --color=never --with-filename --no-heading")
;;(defun helm-ag--construct-ignore-option (pattern)
;; (concat "-g !" pattern))
;; Most keybinds in projectile below
;;
;; multiple-cursors
;;
(add-to-list 'load-path "~/.emacs.d/multiple-cursors")
(require 'multiple-cursors)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-.") 'mc/unmark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-,") 'mc/unmark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
;;
;; phi-search
;;
(add-to-list 'load-path "~/.emacs.d/phi-search")
(autoload 'phi-search "phi-search" "Phi Search." t)
(global-set-key (kbd "C-S-s") 'phi-search)
(global-set-key (kbd "C-S-r") 'phi-search-backward)
;; See also
;;phi-search-additional-keybinds
;;phi-replace-additional-keybinds
;; -- NOT keymaps tho, see doc
(defun kill-phisearch-match ()
"Kill the current isearch match string and continue searching."
(interactive)
(when phi-search--selection
;; In phisearch, we're in the minibuffer by default, and there are N
;; search-overlays of which we are centered on index
;; phi-search--selection, if any.
(phi-search--with-target-buffer
(let ((ov (nth phi-search--selection phi-search--overlays)))
(kill-region (overlay-end ov) (overlay-start ov)))))
(phi-search-complete))
(with-eval-after-load "phisearch"
(define-key phi-search-default-map (kbd "C-.") 'kill-phisearch-match))
;;
;; Swiper
;;
(defun neph-swiper-current-word ()
"Start swiper with the current word"
(interactive)
(let ((current-word (save-excursion
(neph-mark-current-word)
(buffer-substring (mark) (point)))))
(swiper current-word)))
(add-to-list 'load-path "~/.emacs.d/swiper")
(autoload 'swiper "swiper" "Swiper popup thing" t)
(global-set-key (kbd "C-z s") 'swiper)
(defun isearch-to-swiper ()
"Drop into swiper with current isearch"
(interactive)
(isearch-exit)
(swiper isearch-string))
(define-key isearch-mode-map (kbd "C-z s") 'isearch-to-swiper)
;;
;; Company mode
;;
(add-to-list 'load-path "~/.emacs.d/company-mode")
(add-to-list 'load-path "~/.emacs.d/company-quickhelp")
(add-to-list 'load-path "~/.emacs.d/pos-tip")
(require 'neph-company-autoload)
(defun neph-company-setup ()
(interactive)
(company-mode t)
(company-quickhelp-mode t)
;;(semantic-mode t)
(local-set-key (kbd "<C-tab>") 'company-complete))
;; Turn on in these modes
(add-hook 'c-mode-common-hook 'neph-company-setup)
(add-hook 'python-mode-hook 'neph-company-setup)
(add-hook 'lisp-mode-hook 'neph-company-setup)
(add-hook 'emacs-lisp-mode-hook 'neph-company-setup)
;;
;; YouCompleteMe (deprecated for LSP, remove?)
;;
;; Deps
;;(add-to-list 'load-path "~/.emacs.d/emacs-deferred")
;;(add-to-list 'load-path "~/.emacs.d/emacs-request")
;;(add-to-list 'load-path "~/.emacs.d/emacs-ycmd")
;;(require 'neph-ycmd-autoload)
;;
;;(with-eval-after-load "company-ycmd" (company-ycmd-setup))
;;(with-eval-after-load "ycmd"
;; (setq ycmd-server-command '("python" "/usr/share/ycmd/ycmd")))
;;
;;(defun neph-ycm-setup ()
;; (interactive)
;; (require 'company-ycmd)
;; (ycmd-mode 1))
;;
;;(add-hook 'python-mode-hook 'neph-ycm-setup)
;;
;; Yasnippet
;;
(add-to-list 'load-path "~/.emacs.d/yasnippet")
(require 'yasnippet)
;;
;; Flycheck
;;
(add-to-list 'load-path "~/.emacs.d/flycheck")
(autoload 'flycheck-mode "flycheck" "flycheck-mode" t)
;;
;; C++ Helper mode(s) : Company/lsp and associated helper libraries
;;
;; cquery
(add-to-list 'load-path "~/.emacs.d/epl")
(add-to-list 'load-path "~/.emacs.d/pkg-info")
(add-to-list 'load-path "~/.emacs.d/hydra")
(add-to-list 'load-path "~/.emacs.d/ace-window")
(add-to-list 'load-path "~/.emacs.d/pfuture")
(add-to-list 'load-path "~/.emacs.d/avy")
(add-to-list 'load-path "~/.emacs.d/yaml.el")
(add-to-list 'load-path "~/.emacs.d/lsp-mode")
(add-to-list 'load-path "~/.emacs.d/lsp-docker")
(add-to-list 'load-path "~/.emacs.d/lsp-mode/clients")
(add-to-list 'load-path "~/.emacs.d/treemacs/src/elisp")
(add-to-list 'load-path "~/.emacs.d/treemacs/src/extra")
(add-to-list 'load-path "~/.emacs.d/emacs-ccls")
(add-to-list 'load-path "~/.emacs.d/dap-mode")
(add-to-list 'load-path "~/.emacs.d/posframe")
(add-to-list 'load-path "~/.emacs.d/lsp-ui")
(add-to-list 'load-path "~/.emacs.d/lsp-pyright")
(add-to-list 'load-path "~/.emacs.d/lsp-treemacs")
(add-to-list 'load-path "~/.emacs.d/helm-lsp")
(require 'lsp-treemacs)
(require 'treemacs)
(require 'treemacs-mouse-interface)
(require 'treemacs-hydras)
(require 'pkg-info)
(require 'lsp)
(require 'company)
(require 'company-quickhelp)
;;(require 'treemacs-projectile)
(require 'lsp-ui)
(require 'lsp-ui-flycheck)
(require 'lsp-headerline)
(require 'lsp-modeline)
(require 'lsp-diagnostics)
(require 'lsp-pyright)
(require 'dap-mode)
(require 'dap-cpptools)
(require 'dap-ui)
(require 'dap-mouse)
(require 'dap-hydra)
(require 'ccls)
(require 'helm-lsp)
(setq company-quickhelp-color-background "black")
(setq ccls-executable "/usr/bin/ccls")
;; Use helm-lsp-workspace-symbol to replace xref-find-apropos (recommended by helm-lsp readme)
(define-key lsp-mode-map [remap xref-find-apropos] #'helm-lsp-workspace-symbol)
(lsp-treemacs-sync-mode 1)
(setq lsp-ui-doc-show-with-cursor t)
(setq lsp-lens-enable nil)
;; FIXME?
;;(with-eval-after-load 'lsp-mode
;; (add-hook 'lsp-after-open-hook (lambda () (lsp-ui-flycheck-enable 1))))
(setq lsp-ui-peek-always-show t)
(setq ccls-sem-highlight-method 'font-lock)
(ccls-use-default-rainbow-sem-highlight)
;;(setq ccls-sem-highlight-method nil)
;; We'll set these from the theme. Uncomment for random themes.
(setq ccls-args
(list
(concat "--init=" (json-encode
(ht ;; No: 60G memory usage lol ("index" (ht ("multiVersion" 1)))
;; Clang args that trip things up, and include /usr/lib/glib-2.0 in compiles
("clang" (ht ("extraArgs" [-ferror-limit=0 -I/usr/lib/glib-2.0/include/])
("excludeArgs" ["-frounding-math" "-march=pentium4"]))))))
;; Extra logging
"-log-file=/tmp/ccls.log"
"-v=1"))
(defun neph-clear-text-properties ()
"Reset all text properties in the buffer."
(interactive)
(with-silent-modifications (set-text-properties (buffer-end 0) (buffer-end 1) nil)))
(defun neph-lsp-reset ()
"Reconnects to LSP, fixing annoying CCLS highlighting bug."
(interactive)
(lsp-disconnect)
(neph-clear-text-properties)
(lsp))
(defun neph-lsp-reformat-definition ()
"Reformat the definition under the cursor according to how LSP parsed it."
(interactive)
;; Request the "hover" of the thing under cursor, which is the expanded definition of it.
(let* ((hoverContents (-some->> (lsp--text-document-position-params)
(lsp--make-request "textDocument/hover")
(lsp--send-request)
(gethash "contents")))
(hoverText (-some->> (seq-subseq hoverContents -1)
(lsp-seq-first)
(gethash "value")))