-
Notifications
You must be signed in to change notification settings - Fork 24
/
init.el
1141 lines (1008 loc) · 41.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
(when (< emacs-major-version 26)
(error "Your version of emacs is old and must be upgraded before you can use these packages! Version >= 26 is required."))
(setq warning-minimum-level :emergency)
(setq frame-resize-pixelwise t
x-frame-normalize-before-maximize t)
;; set coding system so emacs doesn't choke on melpa file listings
(set-language-environment 'utf-8)
(setq locale-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(unless (eq system-type 'windows-nt)
(set-selection-coding-system 'utf-8))
(prefer-coding-system 'utf-8)
(setq buffer-file-coding-system 'utf-8)
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
;; set things that need to be set before packages load
(setq outline-minor-mode-prefix "\C-c\C-o")
(add-hook 'outline-minor-mode-hook
(lambda () (local-set-key "\C-c\C-o"
outline-mode-prefix-map)))
(setq save-abbrevs 'silently)
(setq max-specpdl-size 10000
max-lisp-eval-depth 5000)
;; load the package manager
(require 'package)
(when (< emacs-major-version 27)
(package-initialize))
(require 'cl-lib)
;; Use y/n instead of yes/no
(fset 'yes-or-no-p 'y-or-n-p)
;; Add additional package sources
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; Fix gnu package archive verification in Emacs 26.2 by disabling broken TLS 1.3 support
;; per https://www.reddit.com/r/emacs/comments/cdei4p/failed_to_download_gnu_archive_bad_request/
(if (and (= emacs-major-version 26) (= emacs-minor-version 2))
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3"))
;; Assume yes to the package installation prompts if EMACS_AUTOINSTALL_PACKAGES=yes
(if (string-equal (getenv "EMACS_AUTOINSTALL_PACKAGES") "yes")
(defadvice package-install-selected-packages (around auto-confirm compile activate)
(letf (((symbol-function 'yes-or-no-p) (lambda (&rest args) t))
((symbol-function 'y-or-n-p) (lambda (&rest args) t)))
ad-do-it)))
;; A list of the packages we always want
(setq package-selected-packages
'(;; gnu packages
windresize
diff-hl
adaptive-wrap
pdf-tools
yasnippet
yasnippet-snippets
multiple-cursors
visual-regexp
command-log-mode
better-defaults
minions
ace-window
ctable
yaml-mode
deferred
epc
s
rotate
howdoi
multi-term
with-editor
anzu
counsel
flx-ido
smex
hydra
ivy-hydra
wgrep
which-key
outline-magic
smooth-scroll
unfill
company
undo-fu
company-math
company-auctex
web-mode
markdown-mode
polymode
poly-markdown
poly-org
poly-noweb
eval-in-repl
exec-path-from-shell
dumb-jump
flycheck
htmlize
dictionary
untitled-new-buffer))
;; hide compilation buffer when complete
;; from http://emacs.stackexchange.com/questions/62/hide-compilation-window
(add-hook 'compilation-finish-functions
(lambda (buf str)
(run-at-time
"2 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(run-at-time
"2 sec" nil 'delete-windows-on
(get-buffer-create "*Compile-Log*"))))
;; install packages if needed
(unless (cl-every 'package-installed-p package-selected-packages)
(message "Missing packages detected, please wait...")
(package-refresh-contents)
(package-install-selected-packages))
(when (< emacs-major-version 28)
(package-initialize))
;; add custom lisp directory to path
(unless
(file-exists-p (concat user-emacs-directory "lisp"))
(make-directory (concat user-emacs-directory "lisp")))
;; add custom lisp directory to path
(let ((default-directory (concat user-emacs-directory "lisp/")))
(setq load-path
(append
(let ((load-path (copy-sequence load-path))) ;; Shadow
(append
(copy-sequence (normal-top-level-add-to-load-path '(".")))
(normal-top-level-add-subdirs-to-load-path)))
load-path)))
;; on OSX Emacs needs help setting up the system paths
(when (memq window-system '(mac ns))
(require 'exec-path-from-shell)
;; From https://github.com/aculich/.emacs.d/blob/master/init.el
;; Import additional environment variables beyond just $PATH
(dolist (var '("PYTHONPATH" ; Python modules
"INFOPATH" ; Info directories
"JAVA_OPTS" ; Options for java processes
"SBT_OPTS" ; Options for SBT
"RUST_SRC_PATH" ; Rust sources, for racer
"CARGO_HOME" ; Cargo home, for racer
"EMAIL" ; My personal email
"GPG_TTY"
"GPG_AGENT_INFO"
"SSH_AUTH_SOCK"
"SSH_AGENT_PID"
))
(add-to-list 'exec-path-from-shell-variables var))
(exec-path-from-shell-initialize))
;; Add to the list of the packages we want
(when (executable-find "R")
(add-to-list 'package-selected-packages 'ess)
(add-to-list 'package-selected-packages 'poly-R))
(when (executable-find "python")
(add-to-list 'package-selected-packages 'poetry)
(add-to-list 'package-selected-packages 'conda))
(when (executable-find "pdflatex")
(add-to-list 'package-selected-packages 'auctex)
(add-to-list 'package-selected-packages 'ivy-bibtex))
(when (executable-find "git")
(add-to-list 'package-selected-packages 'git-commit)
(add-to-list 'package-selected-packages 'magit))
(when (executable-find "julia")
(add-to-list 'package-selected-packages 'julia-mode)
(add-to-list 'package-selected-packages 'julia-repl))
(when (or (executable-find "ghc")
(executable-find "stack"))
(add-to-list 'package-selected-packages 'haskell-mode)
(add-to-list 'package-selected-packages 'company-ghci))
(when (executable-find "jupyter")
(add-to-list 'package-selected-packages 'ein))
(when (executable-find "pandoc")
(add-to-list 'package-selected-packages 'pandoc-mode)
(add-to-list 'package-selected-packages 'ox-pandoc))
(when (executable-find "scala")
(add-to-list 'package-selected-packages 'scala-mode)
(add-to-list 'package-selected-packages 'sbt-mode))
;; install packages if needed
(unless (cl-every 'package-installed-p package-selected-packages)
(message "Missing packages detected, please wait...")
(package-refresh-contents)
(package-install-selected-packages))
;; ;; clean up the mode line
(setq minions-mode-line-lighter "☰")
(minions-mode 1)
;; No, we do not need the splash screen
(setq inhibit-startup-screen t)
(require 'better-defaults)
;; better defaults are well, better... but we don't always agree
(with-eval-after-load "menu-bar"
(menu-bar-mode 1))
(with-eval-after-load "scroll-bar"
(scroll-bar-mode 1))
(setq select-active-regions 'only)
;; scrolling behavior
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ; one line at a time
(setq mouse-wheel-progressive-speed nil) ; don't accelerate scrolling
(setq mouse-wheel-follow-mouse 't) ; scroll window under mouse
(setq scroll-preserve-screen-position t)
(setq scroll-conservatively 100000)
(setq scroll-error-top-bottom t)
(setq scroll-preserve-screen-position t)
;; scroll without moving point
(require 'smooth-scroll)
(global-set-key [(control down)] 'scroll-up-1)
(global-set-key [(control up)] 'scroll-down-1)
(global-set-key [(control left)] 'scroll-right-1)
(global-set-key [(control right)] 'scroll-left-1)
;; Use y/n instead of yes/no
(fset 'yes-or-no-p 'y-or-n-p)
(transient-mark-mode 1) ; makes the region visible
(line-number-mode 1) ; makes the line number show up
(column-number-mode 1) ; makes the column number show up
;; make home and end behave
(global-set-key (kbd "<home>") 'move-beginning-of-line)
(global-set-key (kbd "<end>") 'move-end-of-line)
;; enable toggling paragraph un-fill
(define-key global-map "\M-Q" 'unfill-paragraph)
;;; line wrapping
;; neck beards be damned, we don't need to hard wrap. The editor can soft wrap for us.
(remove-hook 'text-mode-hook 'turn-on-auto-fill)
;; (add-hook 'visual-line-mode-hook 'adaptive-wrap-prefix-mode)
;;
(setq-default truncate-lines t)
(global-visual-line-mode 1)
(add-hook 'prog-mode-hook
(lambda()
(setq truncate-lines t)
(outline-minor-mode t)))
;; indicate visual-line-mode wrap
(setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))
(setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))
;; but be gentle
(when (fboundp 'set-fringe-bitmap-face)
(defface visual-line-wrap-face
'((t (:foreground "gray")))
"Face for visual line indicators.")
(set-fringe-bitmap-face 'left-curly-arrow 'visual-line-wrap-face)
(set-fringe-bitmap-face 'right-curly-arrow 'visual-line-wrap-face))
;; don't require two spaces for sentence end.
(setq sentence-end-double-space nil)
;; The beeping can be annoying--turn it off
(setq visible-bell t
ring-bell-function #'ignore)
;; save place -- move to the place I was last time I visited this file
(save-place-mode t)
;; regular cursor
;(setq-default cursor-type '(bar . 5))
(setq-default blink-cursor-blinks 0)
(add-hook 'after-init-hook
(lambda()
(setq blink-cursor-blinks 0)))
;; easy navigation in read-only buffers
(setq view-read-only t)
(with-eval-after-load "view-mode"
(define-key view-mode-map (kbd "s") 'isearch-forward-regexp))
;; set up read-only buffers
(add-hook 'read-only-mode-hook
(lambda()
(cond
((and (not buffer-read-only)
(not (eq (get major-mode 'mode-class) 'special)))
(hl-line-mode -1)
(setq-local blink-cursor-blinks 0)
(setq-local cursor-type '(bar . 3))
(company-mode t))
((and buffer-read-only
(not (eq (get major-mode 'mode-class) 'special)))
(hl-line-mode t)
(setq-local blink-cursor-blinks 1)
(setq-local cursor-type 'hollow)
(company-mode -1)))))
;; show parentheses
(show-paren-mode 1)
(setq show-paren-delay 0)
;; Use CUA mode to make life easier. We do _not__ use standard copy/paste etc. (see below).
(cua-mode t)
(cua-selection-mode t) ;; cua goodness without copy/paste etc.
;; load windows-style keys using windows key instead of control.
(require 'win-win)
;; ;; Make control-z undo
(require 'undo-fu)
(global-unset-key (kbd "C-z"))
(global-set-key (kbd "C-z") 'undo-fu-only-undo)
(global-set-key (kbd "C-S-z") 'undo-fu-only-redo)
(global-set-key (kbd "C-u") 'undo-fu-only-undo)
(global-set-key (kbd "C-S-u") 'undo-fu-only-redo)
;; Make right-click do something close to what people expect
(require 'mouse3)
(global-set-key (kbd "<mouse-3>") 'mouse3-popup-menu)
;; (global-set-key (kbd "C-f") 'isearch-forward)
;; (global-set-key (kbd "C-s") 'save-buffer)
;; (global-set-key (kbd "C-o") 'counsel-find-file)
(define-key cua-global-keymap (kbd "<C-S-SPC>") nil)
(define-key cua-global-keymap (kbd "<C-return>") nil)
(setq cua-rectangle-mark-key (kbd "<C-S-SPC>"))
(define-key cua-global-keymap (kbd "<C-S-SPC>") 'cua-rectangle-mark-mode)
;; zoom in/out like we do everywhere else.
(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
(global-set-key (kbd "<C-mouse-5>") 'text-scale-decrease)
(global-set-key (kbd "<C-mouse-4>") 'text-scale-increase)
;; page up/down
(global-set-key (kbd "<C-prior>") 'beginning-of-buffer)
(global-set-key (kbd "<C-next>") 'end-of-buffer)
;; allow multiple cursors, as in Sublime and VScode
(require 'multiple-cursors)
(defhydra multiple-cursors-hydra (:hint nil)
"
^Up^ ^Down^ ^Other^
----------------------------------------------
[_p_] Next [_n_] Next [_l_] Edit lines
[_P_] Skip [_N_] Skip [_a_] Mark all
[_M-p_] Unmark [_M-n_] Unmark [_r_] Mark by regexp
^ ^ ^ ^ [_q_] Quit
"
("l" mc/edit-lines :exit t)
("a" mc/mark-all-like-this :exit t)
("n" mc/mark-next-like-this)
("N" mc/skip-to-next-like-this)
("M-n" mc/unmark-next-like-this)
("p" mc/mark-previous-like-this)
("P" mc/skip-to-previous-like-this)
("M-p" mc/unmark-previous-like-this)
("r" mc/mark-all-in-region-regexp :exit t)
("q" nil))
(global-set-key (kbd "C-c m") #'multiple-cursors-hydra/body)
;; Undo/redo window changes
(winner-mode 1)
;; windmove
(global-set-key (kbd "C-x <S-left>") 'windmove-left)
(global-set-key (kbd "C-x <S-right>") 'windmove-right)
(global-set-key (kbd "C-x <S-up>") 'windmove-up)
(global-set-key (kbd "C-x <S-down>") 'windmove-down)
;; Store and recall window layouts (views!)
(global-set-key (kbd "C-c v") 'ivy-push-view)
(global-set-key (kbd "C-c V") 'ivy-switch-view)
;; use ace-window for navigating windows
(global-set-key (kbd "C-x O") 'ace-window)
(with-eval-after-load "ace-window"
(setq aw-dispatch-always t)
(set-face-attribute 'aw-leading-char-face nil :height 2.5))
;; rotate buffers and window arrangements
(global-set-key (kbd "C-c b") 'rotate-window)
(global-set-key (kbd "C-c a") 'rotate-layout)
;; modified from https://github.com/aculich/.emacs.d/blob/master/init.el
(setq frame-title-format
'(:eval (if (buffer-file-name)
(abbreviate-file-name (buffer-file-name)) "%b"))
;; Size new windows proportionally wrt other windows
;;window-combination-resize t
)
;; enable on-the-fly spell checking
(setq flyspell-use-meta-tab nil)
(add-hook 'text-mode-hook
(lambda ()
(flyspell-mode 1)))
;; prevent flyspell from finding misspellings in code
(add-hook 'prog-mode-hook
(lambda ()
;; `ispell-comments-and-strings'
(flyspell-prog-mode)))
;; ispell should not check code blocks in org mode
(add-to-list 'ispell-skip-region-alist '(":\\(PROPERTIES\\|LOGBOOK\\):" . ":END:"))
(add-to-list 'ispell-skip-region-alist '("#\\+BEGIN_SRC" . "#\\+END_SRC"))
(add-to-list 'ispell-skip-region-alist '("#\\+begin_src" . "#\\+end_src"))
(add-to-list 'ispell-skip-region-alist '("^#\\+begin_example " . "#\\+end_example$"))
(add-to-list 'ispell-skip-region-alist '("^#\\+BEGIN_EXAMPLE " . "#\\+END_EXAMPLE$"))
;; Dictionaries
(global-set-key (kbd "C-c d") 'dictionary-search)
(global-set-key (kbd "C-c D") 'dictionary-match-words)
(when (eq system-type 'gnu/linux)
(setq mygtklp (executable-find "gtklp"))
(when mygtklp
(setq lpr-command "gtklp")
(setq ps-lpr-command "gtklp")))
;; use ivy instead of ido
(ido-mode nil)
(ivy-mode 1)
(counsel-mode 1)
(require 'ivy-hydra)
;; make sure we wrap in the minibuffer
(setq ivy-truncate-lines nil)
;; more obvious separator for yank-pop
(setq counsel-yank-pop-separator "
-%<-%<-%<-%<-%<-%<-%<-%<-%<-%<-%<-%<
")
(setq counsel-find-file-ignore-regexp "\\`\\.")
(setq ivy-use-virtual-buffers t)
(setq ivy-count-format "(%d/%d) ")
(setq ivy-use-selectable-prompt t)
;; (setq ivy-display-style nil)
;; Ivy-based interface to describe keybindings
(global-set-key (kbd "C-h b") 'counsel-descbinds)
;; isearch
(setq enable-recursive-minibuffers t
isearch-allow-scroll t)
(require 'hl-line)
(require 'anzu)
(global-anzu-mode +1)
(global-set-key (kbd "C-s") 'isearch-forward)
(global-set-key (kbd "C-S-s") 'isearch-forward-regexp)
(defun my-turn-on-hl-line ()
(setq old-hl-line-mode-value hl-line-mode)
(hl-line-mode 1))
(defun my-toggle-hl-line ()
(unless old-hl-line-mode-value (hl-line-mode -1)))
(add-hook 'isearch-mode-hook 'my-turn-on-hl-line)
(add-hook 'isearch-mode-end-hook 'my-toggle-hl-line)
;; from https://emacs.stackexchange.com/questions/10307/how-to-center-the-current-line-vertically-during-isearch
(defadvice isearch-update (before my-isearch-reposite activate)
(sit-for 0)
(recenter))
(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)
(define-key isearch-mode-map (kbd "C-n") 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-p") 'isearch-repeat-backward)
(define-key isearch-mode-map (kbd "C-p") 'isearch-repeat-backward)
(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)
;; visual query replace
(global-set-key (kbd "C-r") 'vr/query-replace)
(global-set-key (kbd "C-S-r") 'vr/replace)
;; default file searcher if we don't find something better
(global-set-key (kbd "C-c f") 'find-grep-dired)
(global-set-key (kbd "C-c f") 'find-grep-dired)
;; use better searching tool if available
(cond
((executable-find "rg") ; search with ripgrep if we have it
(global-set-key (kbd "C-c f") 'counsel-rg)
(global-set-key (kbd "C-c s") 'counsel-rg))
((executable-find "ag") ; otherwise search with ag if we have it
(global-set-key (kbd "C-c f") 'counsel-ag)
(global-set-key (kbd "C-c s") 'counsel-ag))
((executable-find "pt") ; otherwise search with pt if we have it
(global-set-key (kbd "C-c f") 'counsel-pt)
(global-set-key (kbd "C-c f") 'counsel-pt)))
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "M-y") 'counsel-yank-pop)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "C-o") 'counsel-find-file)
;; search for files to open with "C-O=
(when (memq window-system '(mac ns)) ; use mdfind on Mac. TODO: what about windows?
(setq locate-command "mdfind")
(setq counsel-locate-cmd 'counsel-locate-cmd-mdfind))
;; default file-finding in case we don't have something better
(global-set-key (kbd "C-x C-S-F") 'find-name-dired)
(global-set-key (kbd "C-c l") 'find-name-dired)
;; use locate if we have it.
(when (executable-find "locate")
(global-set-key (kbd "C-c l") 'counsel-locate)
;;(global-set-key (kbd "C-x C-S-F") 'counsel-locate) ;; FIXME -- need better key
)
(global-set-key (kbd "C-x C-r") 'counsel-recentf)
(global-set-key (kbd "<f1> f") 'counsel-describe-function)
(global-set-key (kbd "<f1> v") 'counsel-describe-variable)
(global-set-key (kbd "<f1> l") 'counsel-load-library)
(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
(global-set-key (kbd "<f2> u") 'counsel-unicode-char)
;; Ivy-based interface to shell and system tools
(global-set-key (kbd "C-c g") 'counsel-git)
(global-set-key (kbd "C-c j") 'counsel-git-grep)
(global-set-key (kbd "C-c k") 'counsel-ag)
;; Ivy-resume and other commands
(global-set-key (kbd "C-c i") 'ivy-resume)
;; Make Ivy more like ido
(define-key ivy-minibuffer-map (kbd "RET") 'ivy-alt-done)
(define-key ivy-minibuffer-map (kbd "C-d") 'ivy-done)
(define-key ivy-minibuffer-map (kbd "C-b") 'ivy-immediate-done)
(define-key ivy-minibuffer-map (kbd "C-f") 'ivy-immediate-done)
(defun my-toggle-truncate-lines ()
"Toggle truncate lines in quietly."
(interactive)
(let ((inhibit-message t))
(toggle-truncate-lines)))
(define-key ivy-minibuffer-map (kbd "C-l") 'my-toggle-truncate-lines)
(define-key swiper-map (kbd "C-l") 'my-toggle-truncate-lines)
;; show recently opened files
(with-eval-after-load "recentf"
(setq recentf-max-menu-items 50)
(add-to-list 'recentf-exclude "/\\.git/.*\\'")
(add-to-list 'recentf-exclude "/elpa/.*\\'")
(add-to-list 'recentf-exclude "/tramp.*\\'")
(add-to-list 'recentf-exclude "/sudo.*\\'"))
(recentf-mode 1)
;; better occur mode
(add-hook 'occur-mode-hook
(lambda()
(toggle-truncate-lines t)
(setq-local cursor-type 'box)
(setq-local blink-cursor-blinks 1)
(company-mode -1)
(hl-line-mode t)
(next-error-follow-minor-mode t)))
;; Jump easy to definition
(setq dumb-jump-selector 'ivy
dumb-jump-aggressive nil
dumb-jump-default-project "./")
(require 'company)
(setq company-idle-delay nil
company-minimum-prefix-length 2
company-global-modes '(not term-mode))
;; use C-n and C-p to cycle through completions
(define-key company-active-map (kbd "C-n") 'company-select-next)
(define-key company-active-map (kbd "<tab>") 'company-complete-common)
(define-key company-active-map (kbd "C-p") 'company-select-previous)
(define-key company-active-map (kbd "<backtab>") 'company-select-previous)
(require 'company-capf)
(require 'company-files)
(require 'company-math)
(delete-dups (push 'company-math-symbols-unicode company-backends))
(delete-dups (push 'company-capf company-backends))
(delete-dups (push 'company-files company-backends))
;; completion key bindings
(setq tab-always-indent 'complete)
(define-key company-mode-map [remap indent-for-tab-command] #'company-indent-or-complete-common)
(define-key company-mode-map (kbd "C-M-i") 'company-complete)
(define-key company-mode-map (kbd "C-M-S-i") 'counsel-company)
(add-hook 'after-init-hook 'global-company-mode)
(which-key-mode t)
;;; Configure outline minor modes
;; Less crazy key bindings for outline-minor-mode
(setq outline-minor-mode-prefix "\C-c\C-o")
;; load outline-magic along with outline-minor-mode
(add-hook 'outline-minor-mode-hook
(lambda ()
(require 'outline-magic)
(define-key outline-minor-mode-map "\C-c\C-o\t" 'outline-cycle)
(define-key outline-minor-mode-map (kbd "<backtab>") 'outline-cycle)))
(setq command-log-mode-auto-show t)
(global-set-key (kbd "C-x cl") 'global-command-log-mode)
;; require the main file containing common functions
(require 'eval-in-repl)
(setq comint-process-echoes t
eir-repl-placement 'below)
;; truncate lines in comint buffers
(add-hook 'comint-mode-hook
(lambda()
(setq truncate-lines 1)))
;; Scroll down for input and output
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
;;; ESS (Emacs Speaks Statistics)
(with-eval-after-load "ess"
(require 'ess-site)
(require 'ess-mode)
;; standard control-enter evaluation
(define-key ess-mode-map (kbd "<C-return>") 'ess-eval-region-or-function-or-paragraph-and-step)
(define-key ess-mode-map (kbd "<C-S-return>") 'ess-eval-buffer)
(define-key ess-mode-map [remap ess-indent-or-complete] #'company-indent-or-complete-common)
;; Set ESS options
(setq
ess-auto-width 'window
ess-use-auto-complete nil
ess-use-flymake nil
ess-use-company 't
;; ess-r-package-auto-set-evaluation-env nil
inferior-ess-same-window nil
ess-indent-with-fancy-comments nil ; don't indent comments
ess-eval-visibly t ; enable echoing input
ess-eval-empty t ; don't skip non-code lines.
ess-ask-for-ess-directory nil ; start R in the working directory by default
ess-ask-for-ess-directory nil ; start R in the working directory by default
ess-R-font-lock-keywords ; font-lock, but not too much
(quote
((ess-R-fl-keyword:modifiers)
(ess-R-fl-keyword:fun-defs . t)
(ess-R-fl-keyword:keywords . t)
(ess-R-fl-keyword:assign-ops . t)
(ess-R-fl-keyword:constants . 1)
(ess-fl-keyword:fun-calls . t)
(ess-fl-keyword:numbers)
(ess-fl-keyword:operators . t)
(ess-fl-keyword:delimiters)
(ess-fl-keyword:=)
(ess-R-fl-keyword:F&T)))))
(defalias 'python 'run-python)
(with-eval-after-load "python"
;; conda (https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html) environment support
(require 'conda)
;; poetry (https://poetry.eustace.io/) environment support
(require 'poetry)
;; try to get indent/completion working nicely
;; readline support is wonky at the moment
(setq python-shell-completion-native-enable nil)
;; simple evaluation with C-ret
(require 'eval-in-repl-python)
;;(setq eir-use-python-shell-send-string nil)
(define-key python-mode-map (kbd "C-c C-c") 'eir-eval-in-python)
(define-key python-mode-map (kbd "<C-return>") 'eir-eval-in-python)
(define-key python-mode-map (kbd "C-c C-b") 'python-shell-send-buffer)
(define-key python-mode-map (kbd "<C-S-return>") 'python-shell-send-buffer)
;; use ipython if we can
(when (executable-find "ipython")
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "--simple-prompt -i"))
;; make outline work
(add-hook 'python-mode-hook
(lambda()
;;(setq-local outline-regexp "[#]+")
(outline-minor-mode t))))
(when (executable-find "julia")
;(require 'julia-mode)
;(require 'julia-repl)
(add-hook 'julia-mode-hook 'julia-repl-mode))
(with-eval-after-load "elisp-mode"
(require 'company-elisp)
;; ielm
(require 'eval-in-repl-ielm)
;; For .el files
(define-key emacs-lisp-mode-map (kbd "C-c C-c") 'eir-eval-in-ielm)
(define-key emacs-lisp-mode-map (kbd "<C-return>") 'eir-eval-in-ielm)
(define-key emacs-lisp-mode-map (kbd "C-c C-b") 'eval-buffer)
(define-key emacs-lisp-mode-map (kbd "<C-S-return>") 'eval-buffer)
;; For *scratch*
(define-key lisp-interaction-mode-map "\C-c\C-c" 'eir-eval-in-ielm)
(define-key lisp-interaction-mode-map (kbd "<C-return>") 'eir-eval-in-ielm)
(define-key lisp-interaction-mode-map (kbd "C-c C-b") 'eval-buffer)
(define-key lisp-interaction-mode-map (kbd "<C-S-return>") 'eval-buffer)
;; For M-x info
(define-key Info-mode-map (kbd "C-c C-c") 'eir-eval-in-ielm)
;; Set up completions
(add-hook 'emacs-lisp-mode-hook
(lambda()
(require 'company-elisp))))
(with-eval-after-load "haskell-mode"
(defalias 'haskell 'haskell-interactive-bring))
;; Use markdown-mode for files with .markdown or .md extensions
(setq
markdown-enable-math t
markdown-fontify-code-blocks-natively t)
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
(when (executable-find "pandoc")
(add-hook 'markdown-mode-hook 'pandoc-mode))
(add-to-list 'auto-mode-alist `("\\.html?\\'" . web-mode))
;;; AucTeX config
;; Modified from https://emacs.stackexchange.com/questions/33198/how-to-get-auctex-to-automatically-generate-atex-engineluatex-file-variable-d/33204
(with-eval-after-load "tex-site"
(defun iqss-prompt-tex-engine ()
(when (eq major-mode 'latex-mode)
;; Check if we are looking at a new or shared file that doesn't specify a TeX engine.
(when (and (not buffer-read-only)
(not (member 'TeX-engine (mapcar 'car file-local-variables-alist))))
(save-excursion
(add-file-local-variable
'TeX-engine
(intern (completing-read "TeX engine not set, how should this document be typeset?: "
(mapcar 'car (TeX-engine-alist)) nil nil nil nil "default"))))
(TeX-normal-mode t)
(blink-cursor-start))))
(add-hook
'find-file-hook
(lambda() (run-at-time "0.5 sec" nil 'iqss-prompt-tex-engine)))
(with-eval-after-load "Latex"
;; Highlight beamer alert
(setq font-latex-user-keyword-classes
'(("beamer-alert" (("alert" "{")
("alerta" "{")
("alertb" "{")
("alertc" "{")
("alertd" "{")
("alerte" "{"))
font-latex-bold-face command)))
;; Easy compile key
(define-key LaTeX-mode-map (kbd "<C-return>") 'TeX-command-run-all)
(defun my-tex-quit ()
(interactive)
"Kill any running tex jobs, and cancel other operations."
(ignore-errors
(let ((inhibit-message t))
(TeX-kill-job)))
(keyboard-quit))
(define-key LaTeX-mode-map (kbd "C-g")
'my-tex-quit)
;; Allow paragraph filling in tables
(setq LaTeX-indent-environment-list
(delq (assoc "table" LaTeX-indent-environment-list)
LaTeX-indent-environment-list))
(setq LaTeX-indent-environment-list
(delq (assoc "table*" LaTeX-indent-environment-list)
LaTeX-indent-environment-list))
;; Misc. latex settings
(setq TeX-parse-self t
TeX-auto-save t)
;; (setq TeX-master 'dwim)
(setq TeX-save-query nil)
(setq-default TeX-master 'dwim)
;; Add beamer frames to outline list
(setq TeX-outline-extra
'((".*\\\\begin{frame}\n\\|.*\\\\begin{frame}\\[.*\\]\\|.*\\\\begin{frame}.*{.*}\\|.*[ ]*\\\\frametitle\\b" 3)))
;; reftex settings
(setq reftex-enable-partial-scans t)
(setq reftex-save-parse-info t)
(setq reftex-use-multiple-selection-buffers t)
(setq reftex-plug-into-AUCTeX t)
(add-hook 'TeX-mode-hook
(lambda ()
(turn-on-reftex)
(TeX-PDF-mode t)
(LaTeX-math-mode)
(TeX-source-correlate-mode t)
(imenu-add-to-menubar "Index")
(outline-minor-mode)
(require 'company-math)
(company-auctex-init)))
;; Use pdf-tools to open PDF files
(when (eq system-type 'gnu/linux)
(if (string-equal (getenv "EMACS_AUTOINSTALL_PACKAGES") "yes")
(pdf-tools-install t)
(pdf-tools-install))
(setq TeX-view-program-selection '((output-pdf "PDF Tools")))
TeX-source-correlate-start-server t
;; Update PDF buffers after successful LaTeX runs
(add-hook 'TeX-after-compilation-finished-functions
#'TeX-revert-document-buffer))
;; Count words in latex
;; see http://app.uio.no/ifi/texcount/faq.html#emacs
;; TeXcount setup for TeXcount version 2.3 and later
;;
(when (executable-find "texcount")
(defun texcount ()
(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)))
(defalias 'tex-count-words 'texcount "Count the number of words in the buffer."))
(define-key LaTeX-mode-map "\C-cw" 'tex-count-words)
(add-to-list 'TeX-command-list
(list "TeX-count-words" "tex-count-words" 'TeX-run-function nil t)))
(with-eval-after-load "reftex"
(add-to-list 'reftex-section-levels '("frametitle" . 2))
(setq reftex-toc-split-windows-horizontally t)
(add-hook 'reftex-toc-mode-hook (lambda() (company-mode -1))))
(with-eval-after-load "bibtex"
(add-hook 'bibtex-mode-hook
(lambda ()
(define-key bibtex-mode-map "\M-q" 'bibtex-fill-entry)))))
(setq ivy-bibtex-default-action 'ivy-bibtex-insert-citation)
(global-set-key (kbd "C-c r") 'ivy-bibtex)
(with-eval-after-load "org"
(setq org-replace-disputed-keys t
org-support-shift-select t)
(setf (alist-get ':eval org-babel-default-header-args) "never-export"
(alist-get ':exports org-babel-default-header-args) "both")
;; (setq org-startup-indented t)
;; increase imenu depth to include third level headings
(setq org-imenu-depth 3)
;; Set sensible mode for editing dot files
(add-to-list 'org-src-lang-modes '("dot" . graphviz-dot))
;; Update images from babel code blocks automatically
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
;; configure org-mode when opening first org-mode file
;; Load additional export formats
(require 'ox-ascii)
(require 'ox-md)
(require 'ox-html)
(require 'ox-latex)
(require 'ox-odt)
(when (executable-find "pandoc")
(require 'ox-pandoc))
(require 'org-capture)
(require 'org-protocol)
;; Enable common programming language support in org-mode
(require 'ob-shell)
(require 'ob-emacs-lisp)
(require 'ob-org)
(require 'ob-R)
(when (executable-find "python") (require 'ob-python))
(when (executable-find "matlab") (require 'ob-matlab))
(when (executable-find "octave") (require 'ob-octave))
(when (executable-find "perl") (require 'ob-perl))
(when (executable-find "dot") (require 'ob-dot))
(when (executable-find "ghci") (require 'ob-haskell))
(when (executable-find "ditaa") (require 'ob-ditaa))
;; Fontify code blocks in org-mode
(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
(setq org-confirm-babel-evaluate nil))
;;; polymode
;; (require 'polymode)
;; (require 'poly-R)
;; (with-eval-after-load "markdown"
;; (require 'poly-markdown))
;; (with-eval-after-load "org"
;; (require 'poly-org))
(when (executable-find "mu")
(autoload 'mu4e "mu4e" "Read your mail." t)
(with-eval-after-load "mu4e"
(require 'mu4e)
(require 'mu4e-headers)
(setq mu4e-headers-include-related t
mu4e-headers-show-threads nil
mu4e-headers-skip-duplicates t
;; don't keep message buffers around
message-kill-buffer-on-exit t
;; enable notifications
mu4e-enable-mode-line t
mu4e-headers-fields '(
(:human-date . 12)
(:flags . 6)
;; (:mailing-list . 10)
(:from-or-to . 22)
(:subject)))
;; ;; use org for composing rich text emails
;; (require 'org-mu4e)
;; (setq org-mu4e-convert-to-html t)
;; (define-key mu4e-headers-mode-map (kbd "C-c c") 'org-mu4e-store-and-capture)
;; (define-key mu4e-view-mode-map (kbd "C-c c") 'org-mu4e-store-and-capture)
;;
;; rerender html
(require 'mu4e-contrib)
(setq mu4e-html2text-command 'mu4e-shr2text)
(add-hook 'mu4e-view-mode-hook 'visual-line-mode)))
;;; Dired configuration
(add-hook 'dired-mode-hook
(lambda()
(diff-hl-dired-mode)
(diff-hl-margin-mode)))
;; show details by default
(setq diredp-hide-details-initially-flag nil)
;; set dired listing options
(if (eq system-type 'gnu/linux)
(setq dired-listing-switches "-alDhp"))
;; make sure dired buffers end in a slash so we can identify them easily
(defun ensure-buffer-name-ends-in-slash ()
"change buffer name to end with slash"
(let ((name (buffer-name)))
(if (not (string-match "/$" name))
(rename-buffer (concat name "/") t))))
(add-hook 'dired-mode-hook 'ensure-buffer-name-ends-in-slash)
(add-hook 'dired-mode-hook
(lambda()
(setq truncate-lines 1)))
;; open files in external programs
;; (from http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html
;; consider replacing with https://github.com/thamer/runner
(defun xah-open-in-external-app (&optional file)
"Open the current file or dired marked files in external app.
The app is chosen from your OS's preference."
(interactive)
(let (doIt
(myFileList
(cond
((string-equal major-mode "dired-mode")
(dired-get-marked-files))
((not file) (list (buffer-file-name)))
(file (list file)))))
(setq doIt (if (<= (length myFileList) 5)
t
(y-or-n-p "Open more than 5 files? ")))
(when doIt
(cond
((string-equal system-type "windows-nt")
(mapc
(lambda (fPath)
(w32-shell-execute "open" (replace-regexp-in-string "/" "\\" fPath t t)))
myFileList))
((string-equal system-type "darwin")
(mapc
(lambda (fPath)
(shell-command (format "open \"%s\"" fPath)))
myFileList))
((string-equal system-type "gnu/linux")
(mapc
(lambda (fPath)
(let ((process-connection-type nil))
(start-process "" nil "xdg-open" fPath))) myFileList))))))
;; use zip/unzip to compress/uncompress zip archives
(with-eval-after-load "dired-aux"
(add-to-list 'dired-compress-file-suffixes
'("\\.zip\\'" "" "unzip"))
;; open files from dired with "E"
(define-key dired-mode-map (kbd "E") 'xah-open-in-external-app))
(with-eval-after-load "git-commit"
(require 'magit))
;; term
(with-eval-after-load "term"
(define-key term-mode-map (kbd "C-j") 'term-char-mode)
(define-key term-raw-map (kbd "C-j") 'term-line-mode)
(require 'with-editor)
(when (executable-find "git") (require 'git-commit))
(shell-command-with-editor-mode t)
(add-hook 'term-mode-hook
(lambda ()
(toggle-truncate-lines 1))))