-
Notifications
You must be signed in to change notification settings - Fork 2
/
.emacs
2405 lines (1929 loc) · 85.7 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 config file
;; ====== TODO ======
;;
;; - Highlight 'single quoted' text and "double quoted" text differently in
;; Python.
;; - Export all face changes to the theme.
;; - Fix TeXcount.
;; - Fix bug with isearch when the search is not found and you type delete.
;; - Fix inconsistency between C-x # and C-x C-c in emacsclient.
;; - Make C-SPC unset the mark when selection is active.
;; - Find a way to let the cursor move off screen.
;; - Make SyntaxErrors in Python show the relevant character
;; - Figure out escape codes entered in files ([<8;32;21m)
;; Thanks to http://homepages.inf.ed.ac.uk/s0243221/emacs/ for many of these
;; ===== auto-compile ====
;; Should automatically recompile .el files
;; This should stay at the top of this file.
;; ;; -*- no-byte-compile: t -*-
;; (add-to-list 'load-path "~/Documents/auto-compile")
;; (require 'auto-compile)
;; (auto-compile-on-load-mode 1)
;; (auto-compile-on-save-mode 1)
;; Don't show the splash screen on startup. Aside from me not needing it
;; anymore, this fixes a bug with emacsclient.
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)
;; straight.el initialization
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 6))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(setq inhibit-splash-screen t)
;; ========== Add a directory to the emacs load-path for extensions =========
(add-to-list 'load-path "~/.emacs.d/lisp/")
;; ==== Set the font as Menlo =====
;; https://github.com/hbin/top-programming-fonts
(set-frame-font "Menlo:pixelsize=18")
(add-to-list 'default-frame-alist
(cons 'font "Menlo:pixelsize=18")
(cons 'width 123)
(cons 'height 72))
;; ==== use-package ====
;; ;; Bootstrap use-package. From
;; ;; https://swsnr.de/posts/my-emacs-configuration-with-use-package
;;
;; (require 'package)
;; (setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
;;
;; (package-initialize)
;;
;; (unless (package-installed-p 'use-package)
;; (package-refresh-contents)
;; (package-install 'use-package))
;; Alternate use-package init, from git (https://jwiegley.github.io/use-package/installation/)
(add-to-list 'load-path "~/Documents/use-package")
(require 'use-package)
;; Keep the package repository certs up-to-date (otherwise it might fail to
;; connect if they expire)
;; (use-package gnu-elpa-keyring-update
;; :config (gnu-elpa-keyring-update))
(with-eval-after-load 'info
(info-initialize)
(add-to-list 'Info-directory-list
"~/Documents/use-package/"))
;; Make packages always auto-install
(setq use-package-always-ensure t)
;; It doesn't seem to work for some reason, so we use :ensure t below.
;; Install various packages
;; ==== lsp stuff ====
(use-package eglot)
;; Enable eglot mode if it is supported
(defun eglot-ensure ()
(when (eglot-current-server)
(eglot-ensure)))
(add-hook 'prog-mode-hook 'eglot-ensure)
;; (add-to-list 'eglot-server-programs '((python-mode) "jedi-language-server"))
(add-to-list 'eglot-server-programs
`(rst-mode . (,(executable-find "python3") "-m" "esbonio")))
(add-hook 'rst-mode-hook 'eglot-ensure)
;; ==== treesit-auto ====
;; (requires emacs 29)
(if (not (version< emacs-version "29"))
(use-package treesit-auto
:custom
(treesit-auto-install t)
:config
(treesit-auto-add-to-auto-mode-alist 'all)
(global-treesit-auto-mode)))
;; ==== prescient ====
(use-package prescient
:ensure t
:config
(prescient-persist-mode +1))
;; (use-package ido-completing-read+
;; :ensure t)
;; ==== company ====
(defun my-company-toggle-or-cycle ()
"Start company completion or cycle through candidates."
(interactive)
(if company-candidates
(company-select-next)
(company-manual-begin)))
(use-package company
:ensure t
:defer t
:init
(add-hook 'after-init-hook 'global-company-mode)
:config
;; Disable automatic completion
(setq company-idle-delay nil)
(global-set-key (kbd "C-<tab>") 'my-company-toggle-or-cycle))
;; ==== which-key ====
(use-package which-key
:config
(which-key-mode))
;; ==== command-log-mode ====
;; Provides a nicer way to show keyboard output for demos
(use-package command-log-mode)
(setq clm/log-command-exceptions* '(nil))
;; ==== flycheck ====
(use-package flycheck
:bind
(("M-n" . flycheck-next-error)
("M-p" . flycheck-previous-error))
:custom
(flycheck-disabled-checkers '(python-flake8 python-pylint)))
;; ===== flycheck-pyflakes ======
;; (use-package flycheck-pyflakes)
;; copied from flycheck-pyflakes, but adding support for python-ts-mode
(flycheck-define-checker python-pyflakes
"A Python syntax and style checker using the pyflakes utility.
To override the path to the pyflakes executable, set
`flycheck-python-pyflakes-executable'.
See URL `http://pypi.python.org/pypi/pyflakes'."
:command ("pyflakes" source-inplace)
:error-patterns
((error line-start (file-name) ":" line ":" (message) line-end))
:modes (python-mode python-ts-mode))
(add-to-list 'flycheck-checkers 'python-pyflakes)
;; ==== Undo-tree ====
;; Git repo at http://www.dr-qubit.org/git/undo-tree.git
;; Installed by use-package
;; (add-to-list 'load-path "~/Documents/undo-tree")
;; (require 'undo-tree)
;; C-S-/ has to pass through this escape code with iTerm2.
(use-package undo-tree
:bind
("C-[ [ a b" . undo-tree-redo)
:custom
((global-undo-tree-mode t)
(undo-tree-auto-save-history t)
(undo-tree-history-directory-alist (quote ((".*" . "~/.emacs.d/undo-tree/"))))))
;; ;; Compress saved undo files
;; (defadvice undo-tree-make-history-save-file-name
;; (after undo-tree activate)
;; (setq concat ad-return-value ".gz"))
;; ==== multiple-cursors ====
;; Installed by use-package
;; (add-to-list 'load-path "~/Documents/multiple-cursors.el")
;; (require 'multiple-cursors)
;; f5 and f6 are bound to C-< and C-> in iTerm 2, respectively
(use-package multiple-cursors
:bind
(([f6] . mc/mark-next-like-this)
([f5] . mc/mark-previous-like-this)))
;; ==== aggressive-indent-mode ====
;;
;; Installed by use-package
;;
;; (add-to-list 'load-path "~/Documents/aggressive-indent-mode")
;; (require 'aggressive-indent)
(use-package aggressive-indent
:config
(global-aggressive-indent-mode 1)
:custom
(aggressive-indent-excluded-modes '(markdown-mode python-mode python-ts-mode makefile-mode
diff-mode)))
;; ==== MediaWiki Mode ====
;; (add-to-list 'load-path "~/Documents/mediawiki-el") ;; The bzr clone
;;
;; ;; (require 'mediawiki)
;;
;; Commented out because it doesn't seem to be working. But I don't need it
;; for now.
(use-package mediawiki
:mode ("\\.mediawiki\\'" . mediawiki-mode))
;; === Anzu ====
;; Show the total number of search results
;; (add-to-list 'load-path "~/Documents/emacs-anzu")
;; (require 'anzu)
(use-package anzu)
;; :config
;; (global-anzu-mode +1))
;; ==== bug-hunter ====
(use-package bug-hunter)
;; ==== cython-mode ====
(use-package cython-mode
:mode
("\\.pyx\\'"
"\\.pxd\\'"
"\\.pxi\\'"))
(use-package flycheck-cython)
;; ==== ido-sort-mtime ====
(use-package ido-vertical-mode
:config
(ido-vertical-mode 1))
(use-package ido-sort-mtime
:config
(ido-sort-mtime-mode 1))
;; ==== auctex ====
(use-package tex
:ensure auctex
:defer t)
;; ==== magit ====
;; (use-package magit)
;; ==== git-gutter ====
(use-package git-gutter)
;; ;; ==== unmodified-buffer
;;
;; (use-package unmodified-buffer
;; :straight (:host github :repo "arthurcgusmao/unmodified-buffer")
;; :hook (after-init . unmodified-buffer-mode))
;; ;; ==== filetree ====
;;
;; (use-package filetree)
;; ==== cask-mode =====
;; We don't use cask any more but cask-mode can still be useful for editing
;; the Cask files
(use-package cask-mode)
;; ===== Smart comment =====
;; https://emacs.stackexchange.com/questions/16792/easiest-way-to-check-if-current-line-is-empty-ignoring-whitespace
(defun current-line-empty-p ()
(save-excursion
(beginning-of-line)
(looking-at "[[:space:]]*$")))
(defun smart-comment-end (arg)
"Same as smart-comment-line but comments if the line is empty"
(interactive "*P")
(if (current-line-empty-p) (comment-dwim arg) (smart-comment-line arg)))
(use-package smart-comment
:custom
(smart-comment-end-action 'smart-comment-end)
:bind
("M-;" . smart-comment))
;; ==== sml-modeline ====
;; Puts a progress bar on the mode line
(use-package sml-modeline
:custom
(sml-modeline-len 17)
(sml-modeline-mode t)
:custom-face
(sml-modeline-end-face ((t (:background "black" :foreground "white"))))
(sml-modeline-vis-face ((t (:inherit yascroll:thumb-text-area)))))
;; ==== dockerfile-mode ====
(use-package dockerfile-mode)
;; ===== Turn on flyspell-mode ====
(use-package flyspell-lazy
:config
(flyspell-lazy-mode 1)
:custom
(flyspell-lazy-changes-threshold 10)
(flyspell-lazy-idle-seconds 0.1)
(flyspell-lazy-less-feedback t)
(flyspell-lazy-mode t)
(flyspell-lazy-size-threshold 5)
(flyspell-lazy-use-flyspell-word nil)
(flyspell-lazy-window-idle-seconds 1))
;; ==== cmake-font-lock ====
;; Enables syntax highlighting for cmake files
(use-package cmake-font-lock)
;; ;; ==== latex-extra ====
;;
;; ;; https://github.com/Malabarba/latex-extra
;;
;; ;; Enables content folding (hit TAB on section headers) and some other
;; ;; features as well.
;; (use-package latex-extra
;; :hook (LaTeX-mode . latex-extra-mode))
;; ==== avy ====
;; Replacement for ace-jump-mode. Type C-x SPC then some characters to
;; navigate around
(use-package avy
:bind
("C-x SPC" . avy-goto-char)
("C-'" . avy-goto-char-timer))
;; ==== ace-window ====
;; Like avy but for switching windows
(use-package ace-window
:bind
("M-o" . ace-window)
)
;; ==== Markdown mode =====
(add-to-list 'load-path "~/Documents/markdown-mode") ;; The git clone
(defun markdown-fill-paragraph-plain-text (&optional arg)
"Run `fill-paragraph' as if we were in `text-mode'.
ARG is the same as with `fill-paragraph'."
(interactive "P")
(unwind-protect
(progn (text-mode)
(fill-paragraph arg))
(markdown-mode)))
(autoload 'markdown-mode "markdown-mode.el" "Major mode for editing Markdown
files" t)
(autoload 'gfm-mode "markdown-mode.el" "Major mode for editing GitHub flavored
Markdown" t)
(use-package markdown-mode
:mode
("\\.md" . gfm-mode)
("\\.markdown" . gfm-mode)
("PULLREQ_EDITMSG" . gfm-mode)
("COMMIT_EDITMSG" . gfm-mode)
("TAG_EDITMSG" . gfm-mode)
:bind
(:map markdown-mode-map
;; ("M-q" . markdown-fill-paragraph-plain-text)
("M-p" . flycheck-previous-error)
("M-n" . flycheck-next-error)))
;; Enable spell checking in Markdown code blocks
;; This is required to support spell checking inside of MyST directive blocks.
(defun markdown-flyspell-check-word-myst-p ()
"Return t if `flyspell' should check word just before point.
Used for `flyspell-generic-check-word-predicate'. Based on
`markdown-flyspell-check-word-p', but allows spelling inside of code blocks"
(save-excursion
(goto-char (1- (point)))
(not (or
;; (markdown-code-block-at-point-p)
(markdown-inline-code-at-point-p)
;; (markdown-in-comment-p)
(let ((faces (get-text-property (point) 'face)))
(if (listp faces)
(or (memq 'markdown-reference-face faces)
(memq 'markdown-markup-face faces)
(memq 'markdown-plain-url-face faces)
(memq 'markdown-inline-code-face faces)
(memq 'markdown-url-face faces))
(memq faces '(markdown-reference-face
markdown-markup-face
markdown-plain-url-face
markdown-inline-code-face
markdown-url-face))))))))
(advice-add 'markdown-flyspell-check-word-p :override #'markdown-flyspell-check-word-myst-p)
(setq flyspell-generic-check-word-predicate
#'markdown-flyspell-check-word-myst-p)
;; ==== YAML Mode ====
(use-package yaml-mode)
;; (add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
;; ==== TOML Mode ====
(use-package toml-mode)
;; ===== Coffeescript ====
(use-package coffee-mode)
;; ===== sass ======
(use-package sass-mode
:mode "\\.sass\\'")
;; ===== Rust =====
(use-package rust-mode)
;; ===== auto-complete-mode ====
(use-package auto-complete
:config
(ac-config-default)
(ac-flyspell-workaround)
(ac-linum-workaround)
(setq ac-use-fuzzy nil)
(setq ac-ignore-case nil)
(setq ac-use-menu-map t)
(setq ac-auto-start nil)
(substitute-key-definition 'ac-next 'next-line ac-menu-map)
(substitute-key-definition 'ac-previous 'previous-line ac-menu-map)
(substitute-key-definition 'ac-isearch 'isearch-forward ac-menu-map)
(global-auto-complete-mode +1)
:bind
(:map
ac-menu-map
;; ("C-n" . ac-next)
;; ("\C-p" . ac-previous)
("<backtab>" . ac-previous)
("C-c s" . ac-isearch)
:map
ac-completing-map
("\r" . nil)
("<up>" . nil)
("<down>" . nil)
("C-<tab>" . ac-expand))
;; :map
;; ac-mode-map ("M-TAB" . auto-complete)
:hook
(latex-mode . auto-complete-mode)
(LaTeX-mode . auto-complete-mode)
(prog-mode . auto-complete-mode)
(text-mode . auto-complete-mode))
;; ==== Jedi ====
;; Python completion using Jedi and auto-complete-mode
(use-package jedi
:config
(setq jedi:use-shortcuts t)
;; (setq jedi:complete-on-dot t)
(add-to-list 'ac-sources 'ac-source-jedi-direct)
;; Doesn't work yet. See https://github.com/tkf/emacs-jedi/issues/53.
(setq jedi:install-imenu nil)
(setq jedi:imenu-create-index-function 'jedi:create-flat-imenu-index)
(setq jedi:server-args
'("--log-level" "DEBUG"
"--log-traceback"))
;; Disable Jedi function tooltips. Can use
;;
;; (setq jedi:tooltip-method nil)
;;
;; instead to make it show up in the minibuffer (the default is a popup). We
;; disable it because the tooltips are annoying, and the minibuffer stuff
;; overwrites more useful things like flycheck.
(setq jedi:get-in-function-call-delay 100000)
:custom
(jedi:server-command
`("~/Documents/emacs-jedi/env/bin/python" ,(expand-file-name "~/Documents/emacs-jedi/jediepcserver.py")))
;; C-M-i is currently bound to flyspell-auto-correct-word
;; :bind
;; (:map
;; python-mode-map
;; ;; M-TAB
;; ("C-M-i" . jedi:complete))
:hook
(python-mode . jedi:setup))
;; ==== copilot ====
(use-package copilot
:straight (:host github :repo "zerolfx/copilot.el" :files ("dist" "*.el"))
:custom (copilot-node-executable "~/anaconda/envs/emacs/bin/node" "Set node executable.")
:ensure t)
(add-hook 'prog-mode-hook 'copilot-mode)
(add-hook 'yaml-mode-hook 'copilot-mode)
(with-eval-after-load 'auto-complete
;; disable inline preview
(setq ac-disable-inline t)
;; show menu if have only one candidate
(setq ac-candidate-menu-min 0))
;; Based on function from https://robert.kra.hn/posts/2023-02-22-copilot-emacs-setup/
(defun copilot-complete-or-accept ()
"Command that either triggers a completion or accepts one if one
is available. Enables copilot-mode if it isn't already."
(interactive)
(unless (bound-and-true-p copilot-mode)
(copilot-mode 1))
(if (copilot--overlay-visible)
(progn
(copilot-accept-completion))
(copilot-complete)))
(defun my-copilot-disable-predicates ()
"Disable Copilot display when the cursor is in a string or comment."
(or (nth 3 (syntax-ppss)) ;; check if cursor is in a string
(nth 4 (syntax-ppss)))) ;; check if cursor is in a comment
;; Don't show copilot completions in strings or comments unless manually
;; triggered with C-Enter.
(add-hook 'copilot-disable-predicates #'my-copilot-disable-predicates)
;; Note: To complete with Jedi, use C-TAB
;; (define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
;; (define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)
(define-key copilot-mode-map [C-down] #'copilot-next-completion)
(define-key copilot-mode-map [C-up] #'copilot-previous-completion)
(define-key copilot-mode-map [C-right] #'copilot-accept-completion-by-word)
(define-key copilot-mode-map [C-M-right] #'copilot-accept-completion-by-line)
(define-key global-map (kbd "C-<return>") #'copilot-complete-or-accept)
;; Alternate binding since C-Enter doesn't work on Linux
(define-key global-map (kbd "<backtab>") #'copilot-complete-or-accept)
;; Undefine backtab for Python-mode when python mode is loaded
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map [backtab] nil)))
(add-hook 'python-ts-mode-hook
(lambda ()
(define-key python-mode-map [backtab] nil)))
;; ;; ==== chatgpt arcana ====
;;
;; ;; To set the API key, add
;; ;;
;; ;; machine chat.openai.com login YOUR_CHATGPT_EMAIL_HERE password YOUR_API_KEY_HERE
;; ;;
;; ;; to ~/.authinfo
;;
;; (use-package request)
;; (use-package chatgpt-arcana
;; :straight (:host github :repo "CarlQLange/ChatGPT-Arcana.el" :files
;; ("*.el"))
;; :init (setq chatgpt-arcana-api-key (auth-source-pick-first-password
;; :host "chat.openai.com")))
;; ==== ellama ====
(use-package ellama
:init
;; setup key bindings
(setopt ellama-keymap-prefix "C-c e")
(setq openai-key (auth-source-pick-first-password
:host "chat.openai.com"))
(require 'llm-openai)
(setq ellama-provider
(make-llm-openai
:key openai-key
:chat-model "gpt-4-turbo-preview")
llm-warn-on-nonfree nil))
;; ==== popwin ====
;; Make annoying popup windows go away better
(use-package popwin
:config
(popwin-mode 1))
;; ==== Visual regexp ====
(use-package visual-regexp
:bind
(("C-c r" . vr/replace)
("C-c q" . vr/select-query-replace)
;; if you use multiple-cursors, this is for you:
("C-c m" . vr/mc-mark)))
(use-package visual-regexp-steroids
:bind
;; use visual-regexp-steroids's isearch instead of the built-in regexp
;; isearch
;; Commented out because it makes C-x C-s not work inside of isearch. See
;; https://github.com/benma/visual-regexp.el/issues/56. You can still use
;; C-M-s and C-M-r to get the regexp isearch.
;; (("C-r" . vr/isearch-backward)
;; ("C-s" . vr/isearch-forward))
(("C-M-r" . vr/isearch-backward)
("C-M-s" . vr/isearch-forward))
:config
;; Make vr--isearch always case insensitive
(defadvice vr--isearch (around add-case-insensitive (forward string &optional bound noerror count) activate)
(setq string (concat "(?i)" string))
ad-do-it))
;; ===== expand-region =====
(use-package expand-region
:bind
("M-=" . er/expand-region))
;; ;; ==== didyoumean.el ====
;;
;; (use-package didyoumean)
;; ==== auto-package-update ====
(use-package auto-package-update
:config
(setq auto-package-update-delete-old-versions t)
;; (setq auto-package-update-hide-results t)
(auto-package-update-maybe))
;; Also update straight packages
(add-hook 'auto-package-update-after-hook
'straight-pull-all)
;; ====== visible-mark =====
(use-package visible-mark)
;; ==== highlight-symbol ====
(use-package highlight-symbol
:bind
("M-N" . highlight-symbol-next)
("M-P" . highlight-symbol-prev)
:custom
(highlight-symbol-idle-delay 0)
:hook
(prog-mode . highlight-symbol-mode))
;; ===== highlight-numbers ======
(use-package highlight-numbers
:hook
(prog-mode . highlight-numbers-mode))
;; ==== dumb jump ====
(use-package ivy)
(use-package dumb-jump
:hook
(xref-backend-functions . 'dumb-jump-xref-activate)
;; :bind (
;; ;; ("M-g o" . dumb-jump-go-other-window)
;; ("M-." . dumb-jump-go)
;; ("M-," . dumb-jump-back)
;; ;; ("M-g i" . dumb-jump-go-prompt)
;; ;; ("M-g x" . dumb-jump-go-prefer-external)
;; ;; ("M-g z" . dumb-jump-go-prefer-external-other-window)
;; )
:config (setq dumb-jump-selector 'ivy) ;; (setq dumb-jump-selector 'helm)
)
(use-package jsonian)
;; ==== Auto dim =====
;; (use-package auto-dim-other-buffers
;; :hook
;; (after-init . (lambda ()
;; (when (fboundp 'auto-dim-other-buffers-mode)
;; (auto-dim-other-buffers-mode t)))))
;; ;; Enable this when https://github.com/gonewest818/dimmer.el/pull/16 is merged.
;; (use-package dimmer
;; :defer 1
;; :config
;; ;; (setq dimmer-fraction 0.50)
;; (dimmer-mode t))
;; Highlight non-ascii chars
;; https://www.emacswiki.org/emacs/ShowWhiteSpace#HighlightChars
;; (require 'highlight-chars)
;;
;; (defface nonascii-face
;; '((t (:background "color-160")))
;; "Face for highlighting nonascii characters"
;; :group 'text-mode)
;;
;; (defvar highlight-nonascii-p nil
;; "Non-nil means font-lock mode highlights nonascii characters.")
;;
;; (defun toggle-highlight-nonascii (&optional msgp)
;; "Toggle highlighting of non-ASCII characters.
;; Uses face `nonascii-face'."
;; (interactive "p")
;; (setq highlight-nonascii-p (not highlight-nonascii-p))
;; (if highlight-nonascii-p
;; (add-hook 'font-lock-mode-hook 'highlight-nonascii)
;; (remove-hook 'font-lock-mode-hook 'highlight-nonascii)
;; (dont-highlight-nonascii))
;; (font-lock-mode) (font-lock-mode)
;; (when msgp (message "non-ASCII highlighting is now %s"
;; (if highlight-nonascii-p "ON" "OFF"))))
;;
;; (defun highlight-nonascii ()
;; "Highlight whitespace characters."
;; (when (boundp 'nobreak-char-display) (setq nobreak-char-display nil))
;; (font-lock-add-keywords nil
;; `(("[:nonascii:]" (0 'nonascii-face
;; ,hc-font-lock-override))) 'APPEND))
;;
;; (defun dont-highlight-nonascii ()
;; "Do not highlight nonascii characters."
;; (when (fboundp 'font-lock-remove-keywords)
;; (font-lock-remove-keywords nil `(("[:nonascii:]" (0 'nonascii-face ,hc-font-lock-override))))
;; ))
;;
;; (add-hook 'change-major-mode-hook
;; (lambda ()
;; (add-hook 'font-lock-mode-hook
;; 'highlight-nonascii)))
;;
;; (add-hook 'after-change-major-mode-hook
;; (lambda ()
;; (when (eq major-mode 'THE-MODE)
;; (remove-hook 'font-lock-mode-hook
;; 'highlight-nonascii)
;; (dont-highlight-nonascii)))
;; 'APPEND)
;; Use better indentation for C files
(setq c-default-style "cc-mode"
c-basic-offset 4)
;;
;; TeXcount setup for TeXcount version 2.3
;;
(defun texcount-setup ()
(defun latex-word-count ()
(interactive)
(let*
( (this-file (buffer-file-name))
(enc-str (symbol-name buffer-file-coding-system))
(enc-opt
(cond
((string-match "utf-8" enc-str) "-utf8")
((string-match "latin" enc-str) "-latin1")
("-encoding=guess")
) )
(word-count
(with-output-to-string
(with-current-buffer standard-output
(call-process "texcount" nil t nil "-0" enc-opt this-file)
) ) ) )
(message word-count)
) )
(define-key 'latex-mode-map "\C-c w" 'latex-word-count)
)
(add-hook 'latex-mode-hook 'latex-setup t)
;; Commands to interact with the clipboard
(defun osx-copy ()
(interactive)
(let ((deactivate-mark t))
(call-process-region (point) (mark) "pbcopy")))
(defun osx-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "pbpaste" nil t nil))
(defun linux-copy (beg end)
(interactive "r")
(call-process-region beg end "xclip" nil nil nil "-selection" "c"))
(defun linux-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "xsel" nil t nil "-b"))
(cond
((string-equal system-type "darwin") ; Mac OS X
(define-key global-map (kbd "C-x C-w") 'osx-copy)
(define-key global-map (kbd "C-x C-y") 'osx-paste))
((string-equal system-type "gnu/linux") ; linux
(define-key global-map (kbd "C-x C-w") 'linux-copy)
(define-key global-map (kbd "C-x C-y") 'linux-paste)))
;; Better zapping
;; Load zap-up-to-char from the misc package that comes with emacs
(autoload 'zap-up-to-char "misc"
"Kill up to, but not including ARGth occurrence of CHAR.
\(fn arg char)"
'interactive)
;; Make M-z zap-up-to-char (doesn't include char)
(global-set-key "\M-z" 'zap-up-to-char)
;; Make M-Z zap in reverse
(defun reverse-zap-up-to-char (char)
"Like zap-up-to-char with argument -1"
(interactive "cZap back to char: ")
(zap-up-to-char -1 char))
(global-set-key "\M-Z" 'reverse-zap-up-to-char)
;; ==== Make DEL delete four spaces at the beginning of a line ====
;; (defun remove-indentation-spaces ()
;; "remove TAB-WIDTH spaces from the beginning of this line"
;; (interactive)
;; (if (save-excursion (re-search-backward "[^ \t]" (line-beginning-position) t))
;; (delete-backward-char 1)
;; (indent-rigidly (line-beginning-position) (line-end-position) (- tab-width))))
;;
;; (global-set-key (kbd "DEL") 'remove-indentation-spaces)
;; ==== Automatically update table of contents in rst-mode ====
(add-hook 'rst-adjust-hook 'rst-toc-update)
;; Disabled abbrevs in rst-mode that add things to words like "contents"
;; whenever they are typed.
(add-hook 'rst-mode-hook (lambda () (clear-abbrev-table rst-mode-abbrev-table)))
;; Prevent emacs from asking about saving abbrevs every time it closes. Any
;; abbrevs that should be saved persistantly should be added to this file.
(setq save-abbrevs nil)
;; ==== Useful tools for removing duplicate lines ====
(defun remove-duplicate-lines-region (start end)
"Find duplicate lines in region START to END keeping first occurrence."
(interactive "*r")
(save-excursion
(let ((end (copy-marker end)))
(while
(progn
(goto-char start)
(re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
(replace-match "\\1\n\\2")))))
(defun remove-duplicate-lines-buffer ()
"Delete duplicate lines in buffer and keep first occurrence."
(interactive "*")
(remove-duplicate-lines-region (point-min) (point-max)))
;; ==== Fix Shift-Up to do selection ====
;; See http://lists.gnu.org/archive/html/help-gnu-emacs/2011-05/msg00211.html
(define-key input-decode-map "\e[1;2A" [S-up])
(defadvice terminal-init-xterm (after select-shift-up activate)
(define-key input-decode-map "\e[1;2A" [S-up]))
;; ==== Put autosave and backup files in ~/.emacs.d ====
;; Thanks to
;; http://stackoverflow.com/questions/2020941/emacs-newbie-how-can-i-hide-the-buffer-files-that-emacs-creates
;;
;; Put autosave files (ie #foo#) and backup files (ie foo~) in ~/.emacs.d/.
(defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
(defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
(setq backup-directory-alist (list (cons ".*" backup-dir)))
(setq auto-save-list-file-prefix autosave-dir)
(setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))
;; create the autosave dir if necessary, since emacs won't.
(make-directory "~/.emacs.d/autosave/" t)
(make-directory "~/.emacs.d/backup/" t)
;; ==== Save command histories ====
;; See
;; http://stackoverflow.com/questions/1229142/how-can-i-save-my-mini-buffer-history-in-emacs
(setq savehist-additional-variables
'(kill-ring search-ring regexp-search-ring my-fido-command-completions-alist))
(savehist-mode 1)
(setq history-length 1000) ; default is 100
;; Prevent savehist from spamming messages about the history file being locked
(defun steal-history-file-lock ()
"Steals the lock on `~/.emacs.d/history` file if present, after a short delay."
(let* ((history-file (expand-file-name "history" user-emacs-directory))
(lock-file (concat history-file ".lock"))
(wait-time 0.5)) ; Delay in seconds before attempting to steal the lock
(when (file-exists-p lock-file)
(run-with-timer wait-time nil
(lambda ()
(when (file-exists-p lock-file)
(delete-file lock-file)))))))
(add-hook 'savehist-save-hook 'steal-history-file-lock)
;; ==== Set F7 to delete whole line ====
;; iTerm2 doesn't send C-S-Backspace to emacs, so set it up as a keyboard
;; shortcut to F7 in the iTerm2 prefs. See
;; http://code.google.com/p/iterm2/issues/detail?id=1702.
;; TODO: kill-whole-line kills a newline too, which I don't like. So write a
;; custom routine.
(defun kill-total-line ()
(interactive)
(let ((kill-whole-line t))