-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1840 lines (1629 loc) · 68.4 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
;;; init --- Matt Lee's init file -*- lexical-binding: t -*-
;;; Commentary:
;; Lots of tweaks to my Emacs.
;;; Code:
(defvar matt-init-start-time (current-time))
(defvar matt-init-stop-time)
(when (getenv "emacs_perf")
(defun matt-require-perf-wrapper (orig feature &rest args)
(let* ((require-start-time (current-time))
(loaded (apply orig feature args)))
(message "%06dμs | %s"
(* 1000 1000 (float-time (time-subtract (current-time) require-start-time)))
feature)
loaded))
(advice-add 'require :around 'matt-require-perf-wrapper))
;; gc tweaks - see http://bling.github.io/blog/2016/01/18/why-are-you-changing-gc-cons-threshold/
(setq garbage-collection-messages t)
;; time a gc collection
;; (let ((before gc-elapsed)) (garbage-collect) (- gc-elapsed before))
(defun matt-gc-inhibit ()
"Increase the gc threshold to prevent garbage collection."
(setq gc-cons-threshold (* 512 1024 1024)))
;; (add-hook 'minibuffer-setup-hook #'matt-gc-inhibit)
;; (remove-hook 'minibuffer-setup-hook #'matt-gc-inhibit)
(defun matt-gc-uninhibit ()
"Return the gc threshold to a normal level."
(setq gc-cons-threshold (* 8 1024 1024)))
;; (add-hook 'minibuffer-exit-hook #'matt-gc-uninhibit)
;; (remove-hook 'minibuffer-exit-hook #'matt-gc-uninhibit)
(defvar matt-gc-on-blur-timer nil)
(defun matt-gc-on-blur ()
"Run gc when Emacs window loses focus AKA blurs.
Focus change event is debounced so we don't gc on focus."
(if (frame-focus-state)
(when matt-gc-on-blur-timer
(cancel-timer matt-gc-on-blur-timer))
(setq matt-gc-on-blur-timer (run-with-timer 5 nil #'garbage-collect))))
(add-function :after after-focus-change-function
#'matt-gc-on-blur)
;; (remove-function after-focus-change-function #'matt-gc-on-blur)
(matt-gc-inhibit) ;; speedup startup
(require 'package)
(require 'seq)
(require 'cl-lib)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;; (add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/"))
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(eval-when-compile
(require 'use-package))
(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))
(add-to-list 'load-path (expand-file-name "themes" user-emacs-directory)) ;; HACK: ensure matt-make-theme is on the load-path
(defun matt-recompile-packages ()
"Recompile all packages in `package-user-dir'."
(interactive)
(byte-recompile-directory package-user-dir nil 'force))
(use-package exec-path-from-shell
:ensure t
:config
(exec-path-from-shell-initialize))
(defun dissoc (key alist)
"Delete elements of ALIST where KEY is equal to the element's car."
(delq (assoc key alist) alist))
;; prevent ctrl mouse scroll changing the font size
(setq mouse-wheel-scroll-amount (dissoc '(control) mouse-wheel-scroll-amount))
(setq mouse-wheel-scroll-amount (dissoc '(control meta) mouse-wheel-scroll-amount))
(mouse-wheel-mode 1)
;; horizontal mouse scroll
(setq mouse-wheel-tilt-scroll t)
(setq mouse-wheel-flip-direction t) ;; osx style
;; hide tool bar & menu bar & tab bar
(tool-bar-mode -1)
(menu-bar-mode -1)
(tab-bar-mode -1)
;; scroll at same rate as the cursor - this stops the scroll "jumping" when you move off the top/bottom
(setq scroll-conservatively 10)
;; display keystrokes almost immediately
(setq echo-keystrokes 0.3)
(setq ring-bell-function 'ignore)
;; keys
(defvar matt-keymap (make-sparse-keymap))
(global-set-key (kbd "M-m") matt-keymap)
(defun matt-define-key (key def)
"Define key sequence KEY as DEF in personal keymap."
(define-key matt-keymap (kbd key) def))
(defun matt-describe-keymap ()
"Describe `matt-keymap'."
(interactive)
(describe-keymap matt-keymap))
(matt-define-key "?" 'matt-describe-keymap)
;; themes
(setq custom-theme-load-path
(cons (expand-file-name "themes/" user-emacs-directory) custom-theme-load-path))
(defun matt-disable-current-theme ()
"Disable the current theme, assumes only one theme is enabled."
(interactive)
(disable-theme (car custom-enabled-themes)))
(defun matt-disable-all-themes ()
"Disable all loaded themes."
(interactive)
(when (not (eql nil custom-enabled-themes))
(matt-disable-current-theme)
(matt-disable-all-themes)))
(matt-define-key "t ESC" 'matt-disable-all-themes)
(defun matt-load-theme (theme)
"Load THEME after disabling all loaded themes."
(interactive
(list
(intern
(completing-read "Load custom theme: "
(mapcar 'symbol-name (custom-available-themes))))))
(matt-disable-all-themes)
(load-theme theme t))
(matt-define-key "t l" 'matt-load-theme)
(defun matt-reload-theme ()
"Reload the current theme."
(interactive)
(matt-load-theme (car custom-enabled-themes)))
(matt-define-key "t r" 'matt-reload-theme)
(defvar matt-default-theme 'xorbit)
(defvar matt-secondary-theme 'gruuvem)
(defun matt-load-default-theme ()
"Load the default theme, specified by `matt-default-theme'."
(interactive)
(matt-load-theme matt-default-theme))
(matt-define-key "t d" 'matt-load-default-theme)
(defun matt-toggle-theme ()
"Toggle between default and secondary themes."
(interactive)
(matt-load-theme
(if (eq matt-secondary-theme (car custom-enabled-themes))
matt-default-theme
matt-secondary-theme)))
(matt-define-key "t t" 'matt-toggle-theme)
;; font
(defun matt-font-size (&optional sz)
"Set the default font size to SZ."
(interactive "NFont size: ")
(if (numberp sz)
(set-face-attribute 'default nil :height (truncate (* sz 10)))
(round (/ (face-attribute 'default :height) 10.0))))
(matt-define-key "f f" 'matt-font-size)
(defun matt-font-face-wide ()
"Change the default font family to a wide font."
(interactive)
(set-face-attribute 'default nil :width 'normal :weight 'medium :slant 'normal :family "Inconsolata"))
(matt-define-key "f w" 'matt-font-face-wide)
(defun matt-font-face-narrow ()
"Change the default font family to a narrow font."
(interactive)
(set-face-attribute 'default nil :width 'normal :weight 'medium :slant 'normal :family "Victor Mono"))
(matt-define-key "f n" 'matt-font-face-narrow)
;; (set-face-attribute 'default nil :family "Victor Mono") ;; https://rubjo.github.io/victor-mono/
;; (set-face-attribute 'default nil :family "Iosevka") ;; https://be5invis.github.io/Iosevka/
;; (set-face-attribute 'default nil :family "Inconsolata") ;; apt: fonts-inconsolata
;; (set-face-attribute 'default nil :family "M+ 1mn") ;; apt: fonts-mplus
;; (set-face-attribute 'default nil :family "M+ 1m")
;; (set-face-attribute 'default nil :family "VL Gothic") ;; apt: fonts-vlgothic
;; (set-face-attribute 'default nil :family "Nimbus Mono L")
;; (set-face-attribute 'default nil :family "Liberation Mono") ;; apt: fonts-liberation
;; (set-face-attribute 'default nil :family "DejaVu Sans Mono")
;; (set-face-attribute 'default nil :family "Droid Sans Mono")
;; (set-face-attribute 'default nil :family "Mononoki") ;; apt: fonts-mononoki
;; (set-face-attribute 'default nil :family "Space Mono")
;; (set-face-attribute 'default nil :family "Courier 10 Pitch")
;; (set-face-attribute 'default nil :family "Ubuntu Mono")
(defvar matt-font-size-default 14)
(defun matt-font-size-increase ()
"Increase the font size by 1 point."
(interactive)
(matt-font-size (1+ (matt-font-size)))
(message "Font size: %s" (matt-font-size)))
(global-set-key (kbd "C-=") 'matt-font-size-increase)
(defun matt-font-size-decrease ()
"Decrease the font size by 1 point."
(interactive)
(matt-font-size (1- (matt-font-size)))
(message "Font size: %s" (matt-font-size)))
(global-set-key (kbd "C--") 'matt-font-size-decrease)
(defun matt-font-size-default ()
"Set the font to the default size, specified by `matt-font-size-default'."
(interactive)
(matt-font-size matt-font-size-default)
(message "Font size: %s" (matt-font-size)))
(matt-define-key "f d" 'matt-font-size-default)
(matt-font-size-default)
(matt-font-face-narrow)
;; cursor - bar instead of a block
(set-default 'cursor-type '(bar . 2))
;; window title - include file's full path
(setq frame-title-format
'("" invocation-name
(:eval (when (and (boundp 'server-name)
(not (equal server-name "server")))
(concat " - " server-name)))
" - "
(:eval (if (buffer-file-name)
(replace-regexp-in-string "%" "%%" (abbreviate-file-name (buffer-file-name)))
"%b"))))
(defun matt-find-files (filenames)
"Useful as eshell alias, FILENAMES can be multiple args or globs."
(mapc 'find-file
(if (consp (car filenames)) (car filenames) filenames)))
(defvar matt-mode-line-format-default)
(when (not (boundp 'matt-mode-line-format-default)) ;; make safe for re-evaluation
(setq matt-mode-line-format-default mode-line-format))
(defvar matt-mode-line-format-minimal
'("%e "
mode-line-modified
" "
mode-line-buffer-identification
mode-line-frame-identification
mode-name
;; centre
(:propertize " " display ((space :align-to (- right 10)))) ;; (length "[00:00:00]") => 10
;; right
mode-line-misc-info))
(defun matt-toggle-minimal-mode-line ()
"Toggle between the normal mode line and a minimal version."
(interactive)
(setq-default mode-line-format
(if (eq mode-line-format matt-mode-line-format-minimal)
matt-mode-line-format-default
matt-mode-line-format-minimal)))
(matt-toggle-minimal-mode-line)
(defun matt-number-modes-cycle ()
"Cycle through the column and line number modes."
(interactive)
(cond ((and line-number-mode column-number-mode)
(line-number-mode -1)
(column-number-mode -1))
(line-number-mode
(line-number-mode -1)
(column-number-mode 1))
(column-number-mode
(line-number-mode 1)
(column-number-mode 1))
(:else
(line-number-mode 1)
(column-number-mode -1))))
(defun matt-wrap-mode ()
"Interactively adjust the fill column for wrapping."
(interactive)
(let ((map (make-sparse-keymap)))
(define-key map [left] (lambda () (interactive) (setq fill-column (1- fill-column))))
(define-key map [right] (lambda () (interactive) (setq fill-column (1+ fill-column))))
(define-key map "?" (lambda () (interactive) (message "Current fill column: %s" fill-column)))
(define-key map "v" 'display-fill-column-indicator-mode)
(define-key map (kbd "M-q") 'prog-fill-reindent-defun)
(set-transient-map map t nil "Adjust wrapping %k")))
(column-number-mode -1)
(line-number-mode -1)
(size-indication-mode -1)
(setq mode-line-percent-position nil)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; (remove-hook 'before-save-hook 'delete-trailing-whitespace)
(setq kill-whole-line t)
(setq mouse-yank-at-point t) ;; middle click paste at point (not mouse pointer)
(setq require-final-newline nil)
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(setq-default fill-column 115)
(setq-default display-fill-column-indicator-character ?\u2506)
(global-auto-revert-mode t)
(fset 'yes-or-no-p 'y-or-n-p)
(delete-selection-mode t) ;; delete the selection with a keypress
(save-place-mode 1)
(savehist-mode)
(setq history-delete-duplicates t)
(setq set-mark-command-repeat-pop t)
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
;; remove newlines with cycling whitespace
(setq cycle-spacing-actions '((just-one-space -) (delete-all-space -) restore))
;; (setq enable-recursive-minibuffers nil)
(setq read-process-output-max (* 1024 1024))
;; window splitting - always horizontal
(setq split-height-threshold 0)
(setq split-width-threshold nil)
;; default to no wrap
(setq-default truncate-lines t)
;; store all backup and autosave files in the tmp dir
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
;; enable stuff
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(put 'scroll-left 'disabled nil)
(put 'narrow-to-page 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(global-set-key (kbd "C-d") 'backward-delete-char)
(global-set-key (kbd "<M-backspace>") 'backward-kill-word)
(global-set-key (kbd "<C-backspace>") 'backward-kill-word)
(global-set-key (kbd "M-DEL") 'kill-word)
(global-set-key (kbd "<M-delete>") 'kill-word)
(global-set-key (kbd "M-SPC") 'cycle-spacing)
(matt-define-key "b b" 'bury-buffer)
(matt-define-key "a r" 'align-regexp)
(matt-define-key "s r" 'replace-string)
(matt-define-key "s l" 'sort-lines)
(matt-define-key "w b" 'balance-windows)
(matt-define-key "w l" 'toggle-truncate-lines) ;; mnemonic "wrap lines"
(matt-define-key "w w" 'toggle-word-wrap)
(matt-define-key "w v" 'display-fill-column-indicator-mode) ;; mnemonic "wrap view"
(matt-define-key "w f" 'auto-fill-mode) ;; mnemonic "wrap fill"
(matt-define-key "w c" 'set-fill-column) ;; mnemonic "wrap column"
(matt-define-key "w m" 'matt-wrap-mode)
(matt-define-key "l n" 'display-line-numbers-mode)
(defvar matt-sound-command
(cond ((executable-find "ffplay") (list "ffplay" "-nodisp" "-autoexit"))
((executable-find "paplay") (list "paplay"))
((executable-find "afplay") (list "afplay"))
(t nil)))
(defvar matt-sound-default-file
(cond ((file-exists-p "/usr/share/sounds/sound-icons/trumpet-12.wav") "/usr/share/sounds/sound-icons/trumpet-12.wav") ;; linux
((file-exists-p "/System/Library/Sounds/Funk.aiff") "/System/Library/Sounds/Funk.aiff") ;; osx
(t nil))
"The default sound file to be played by `matt-sound-play'.")
(defun matt-sound-play (&optional sound)
"Play SOUND, an audio file, by shelling out to `matt-sound-command'."
(let ((sound (or sound matt-sound-default-file)))
(when (and sound
(file-exists-p (expand-file-name sound))
(stringp (car matt-sound-command))
(executable-find (car matt-sound-command)))
(apply 'start-process "matt-sound-command" nil
(seq-concatenate 'list matt-sound-command (list (expand-file-name sound)))))))
;; (matt-sound-play "/System/Library/Sounds/Basso.aiff")
;; (matt-sound-play "/System/Library/Sounds/Blow.aiff")
;; (matt-sound-play "/System/Library/Sounds/Bottle.aiff")
;; (matt-sound-play "/System/Library/Sounds/Frog.aiff")
;; (matt-sound-play "/System/Library/Sounds/Funk.aiff")
;; (matt-sound-play "/System/Library/Sounds/Glass.aiff")
;; (matt-sound-play "/System/Library/Sounds/Hero.aiff")
;; (matt-sound-play "/System/Library/Sounds/Morse.aiff")
;; (matt-sound-play "/System/Library/Sounds/Ping.aiff")
;; (matt-sound-play "/System/Library/Sounds/Pop.aiff")
;; (matt-sound-play "/System/Library/Sounds/Purr.aiff")
;; (matt-sound-play "/System/Library/Sounds/Sosumi.aiff")
;; (matt-sound-play "/System/Library/Sounds/Submarine.aiff")
;; (matt-sound-play "/System/Library/Sounds/Tink.aiff")
;; (matt-sound-play "/usr/share/sounds/sound-icons/trumpet-12.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/canary-long.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/cembalo-2.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/cembalo-6.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/cembalo-12.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/gummy-cat-2.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/piano-3.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/prompt.wav")
;; (matt-sound-play "/usr/share/sounds/sound-icons/xylofon.wav")
;; (matt-sound-play "/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga")
;; (matt-sound-play "/usr/share/sounds/freedesktop/stereo/complete.oga")
;; (matt-sound-play "/usr/share/sounds/freedesktop/stereo/message.oga")
;; (matt-sound-play "/usr/share/sounds/gnome/default/alerts/glass.ogg")
;; (matt-sound-play "/usr/share/sounds/gnome/default/alerts/drip.ogg")
(defun matt-beep ()
"Play the default audio file. Beep!"
(interactive)
(matt-sound-play))
(use-package dabbrev
:config
(setq dabbrev-ignored-buffer-regexps (list (rx bos " "))))
(use-package recentf
:config
(setq recentf-save-file (expand-file-name "recentf" user-emacs-directory))
(setq recentf-max-saved-items 500)
(setq recentf-max-menu-items 15)
(setq recentf-auto-cleanup 'never) ;; disable - can cause problems with remote files
(recentf-mode 1))
(use-package dired
:config
(setq dired-listing-switches "-al --group-directories-first"))
(use-package comint
:init
(setq comint-input-ignoredups t))
(use-package isearch
:bind (:map isearch-mode-map
("C-." . 'isearch-forward-symbol-at-point)))
(use-package buffer-naming
:config
(buffer-naming-load)
(buffer-naming-set-fn 'project-el-buffer-naming-fn))
(use-package ffap
:bind ("C-x C-f" . find-file-at-point))
(defvar matt-scratch-file-locations
(list "dev/scratch*" "dev/*/scratch*" "dev/*/*/scratch*"
"src/scratch*" "src/*/scratch*" "src/*/*/scratch*"))
(use-package project
:config
(defun matt-project-find-scratch ()
(interactive)
(let* ((pr (project-current t))
(dir (project-root pr))
(scratch-files (seq-remove #'file-directory-p
(mapcan (lambda (pattern)
(file-expand-wildcards (concat dir pattern) t))
matt-scratch-file-locations)))
(current-file-name (buffer-file-name))
(current-extension (when current-file-name (file-name-extension current-file-name)))
(scratch-file (if (= (length scratch-files) 1)
(car scratch-files)
(car (seq-filter (lambda (file) (equal (file-name-extension file) current-extension))
scratch-files)))))
(cond
((and scratch-files
(or (null scratch-file)
(equal current-file-name scratch-file)))
(find-file (completing-read "Open scratch file: " scratch-files)))
((and scratch-file (file-exists-p scratch-file))
(find-file scratch-file))
(t
(message "No scratch file found")))))
(defun matt-test-file-p (file)
;; (rx bol (* any) "/test/" (* any) "_test.clj" (optional (any "sc")) eol)
(string-match-p "^.*/test/.*_test\\.clj[sc]?$" file))
(defun matt-guess-test-file (src-file)
(thread-last src-file
(replace-regexp-in-string "/src/" "/test/")
(replace-regexp-in-string "\\(\\.clj[sc]?\\)" "_test\\1")))
(defun matt-try-guess-test-file (file-name)
(when (and file-name (not (matt-test-file-p file-name)))
(matt-guess-test-file file-name)))
(defun matt-guess-src-file (test-file)
(thread-last test-file
(replace-regexp-in-string "/test/" "/src/")
(replace-regexp-in-string "_test\\(\\.clj[sc]?\\)" "\\1")))
(defun matt-guess-other-file (file)
(when file
(let ((other-file (if (matt-test-file-p file)
(matt-guess-src-file file)
(matt-guess-test-file file))))
(when (and other-file
(not (equal file other-file))
(file-exists-p other-file))
other-file))))
(defun matt-toggle-between-src-and-test ()
(interactive)
(let* ((file-name (buffer-file-name))
(other-file (matt-guess-other-file file-name))
(test-file-name (matt-try-guess-test-file file-name)))
(cond (other-file
(find-file other-file))
((and test-file-name
(yes-or-no-p (format "Test file %s doesn't exist, create it?" test-file-name)))
(find-file test-file-name))
(t
(message "No matching src or test file found")))))
(defun matt-guess-related-file (file)
(let* ((sibling-files (seq-remove (lambda (f) (or (file-directory-p f)
(file-equal-p f file)
(string-prefix-p ".#" f))) ;; lock files
(directory-files (file-name-parent-directory file))))
(ext-files (seq-filter (lambda (f) (equal (url-file-extension f)
(url-file-extension file)))
sibling-files))
;; NOTE: could improve this prefix calculation, for now this works with numeric prefixes
(prefix-fn (lambda (f) (when (string-match (rx (group (1+ num))) f) (match-string 0 f))))
(file-prefix (funcall prefix-fn (file-name-nondirectory file)))
(prefix-files (when file-prefix
(seq-filter (lambda (f) (equal file-prefix (funcall prefix-fn f)))
ext-files))))
(cond ((eq 1 (length sibling-files)) (car sibling-files))
((eq 1 (length ext-files)) (car ext-files))
((eq 1 (length prefix-files)) (car prefix-files)))))
(defun matt-toggle-related-file ()
(interactive)
(let* ((file-name (buffer-file-name))
(related-file (matt-guess-related-file file-name)))
(if related-file
(find-file related-file)
(message "No related file found"))))
:bind (:map matt-keymap
("M-p" . project-switch-project)
("p b" . project-switch-to-buffer)
("p f" . project-find-file)
("p t" . matt-toggle-between-src-and-test)
("p r" . matt-toggle-related-file)
("p s" . matt-project-find-scratch)))
(use-package ibuffer
:init
(setq ibuffer-default-sorting-mode 'filename/process) ;; groups file buffers together
(setq ibuffer-show-empty-filter-groups nil)
(setq ibuffer-formats
'((mark modified read-only " "
(name 52 52 :left :elide) " "
filename-and-process)
(mark modified read-only " "
(name 25 25 :left :elide) " "
(size 9 -1 :right) " "
(mode 16 16 :left :elide) " "
filename-and-process)))
(use-package ibuffer-vc
:ensure t
:config
(defun matt-ibuffer-vc-ignore-some-buffers (f buf)
(when (or (buffer-file-name buf)
(eq 'cider-repl-mode (with-current-buffer buf major-mode))
(and (get-buffer-process buf)
(string-match-p "^nrepl" (process-name (get-buffer-process buf)))))
(funcall f buf)))
(advice-add 'ibuffer-vc-root :around 'matt-ibuffer-vc-ignore-some-buffers)
:hook (ibuffer . ibuffer-vc-set-filter-groups-by-vc-root))
:bind ("C-x C-b" . (lambda () (interactive) (matt-clean-buffers) (ibuffer))))
(use-package vundo
:ensure t
:bind ("M-_" . vundo))
(use-package eshell
:init
(add-hook 'eshell-first-time-mode-hook ;; can't use `:bind' because `eshell-mode-map' is buffer local
(lambda ()
(define-key eshell-hist-mode-map [up] 'previous-line)
(define-key eshell-hist-mode-map [down] 'next-line)
(define-key eshell-hist-mode-map [C-up] 'eshell-previous-matching-input-from-input)
(define-key eshell-hist-mode-map [C-down] 'eshell-next-matching-input-from-input)
(define-key eshell-hist-mode-map [M-up] 'eshell-previous-input)
(define-key eshell-hist-mode-map [M-down] 'eshell-next-input)
(define-key eshell-mode-map [home] 'eshell-bol)
(kill-local-variable 'mode-line-format))) ;; ensure matt-toggle-minimal-mode-line works
(defun matt-eshell-current-directory ()
(interactive)
(let ((path (file-name-directory (or (buffer-file-name) default-directory))))
(eshell)
(cd path)
(eshell-reset)))
(defun matt-eshell-other-buffers-directory ()
(interactive)
(when-let* ((filename (buffer-file-name (other-buffer)))
(path (file-name-directory filename)))
(cd path)
(eshell-reset)))
(defun matt-eshell-dwim-directory ()
(interactive)
(if (equal major-mode 'eshell-mode)
(matt-eshell-other-buffers-directory)
(matt-eshell-current-directory)))
:config
;; FIX: currently breaks the input ring
;; (setq eshell-hist-ignoredups 'erase)
(setq eshell-history-size (* 1024 1024))
:bind (:map matt-keymap
("o e" . eshell)
("o d" . matt-eshell-dwim-directory)))
(use-package tramp
:defer t
:init
(autoload 'matt-file-reopen-as-root "tramp" nil t)
:config
;; open as root - from emacs prelude
(defun matt-file-owner-uid (filename)
"Return the UID of the FILENAME as an integer. See `file-attributes'."
(nth 2 (file-attributes filename 'integer)))
(defun matt-file-owned-by-user-p (filename)
"Return t if file FILENAME is owned by the currently logged in user."
(equal (matt-file-owner-uid filename)
(user-uid)))
(defun matt-find-alternate-file-as-root (filename)
"Wraps `find-alternate-file' with opening a file as root."
(find-alternate-file (concat "/sudo:root@localhost:" filename)))
(defun matt-file-reopen-as-root ()
"Find file as root if necessary."
(interactive)
(unless (or (tramp-tramp-file-p buffer-file-name)
(equal major-mode 'dired-mode)
(not (file-exists-p (file-name-directory buffer-file-name)))
(file-writable-p buffer-file-name)
(matt-file-owned-by-user-p buffer-file-name))
(matt-find-alternate-file-as-root buffer-file-name))))
(use-package bookmark
:bind (:map matt-keymap
("b m" . bookmark-set)
("b j" . bookmark-jump)
("b l" . bookmark-bmenu-list)))
(use-package display-line-numbers
:hook (prog-mode . display-line-numbers-mode)
:config
(setq display-line-numbers-width-start t)
(setq display-line-numbers-widen t))
(use-package abbrev-mode
:hook (sql-mode
sql-interactive-mode
clojure-mode
org-mode))
(use-package smartparens
:vc (:url "https://github.com/Fuco1/smartparens")
:init
(smartparens-global-mode 1)
(show-smartparens-global-mode 1)
;; (add-to-list 'sp-ignore-modes-list 'org-mode)
:config
(use-package smartparens-config) ;; fixes various issues, including single quote issue in lisps
(sp-use-smartparens-bindings)
(setq sp-highlight-pair-overlay nil))
(use-package vertico
:ensure t
:init
(vertico-mode))
(use-package orderless
:ensure t
:init
(setq orderless-match-faces [match])
(setq completion-styles '(orderless basic)) ;; reversing this order breaks cider's completion
(setq completion-category-defaults nil)
(setq completion-category-overrides '((file (styles partial-completion))))
(setq orderless-matching-styles '(orderless-literal orderless-initialism)))
(use-package corfu
:ensure t
:config
(defun matt-corfu-on ()
"Enable Corfu in the minibuffer if `completion-at-point' is bound."
(when (where-is-internal #'completion-at-point (list (current-local-map)))
(corfu-mode 1)))
:custom
(corfu-cycle t)
(corfu-auto t)
(corfu-auto-prefix 2)
:hook (minibuffer-setup . matt-corfu-on)
:init
(global-corfu-mode))
(use-package cape
:ensure t
:init
(add-hook 'completion-at-point-functions #'cape-dabbrev)
(add-hook 'completion-at-point-functions #'cape-file))
(use-package consult
:ensure t
:bind (("M-y" . consult-yank-pop)
("C-x b" . consult-buffer)
("C-x f" . consult-recent-file)
("S-<down>" . consult-buffer)
(:map matt-keymap
("g g" . consult-ripgrep)
("M-b" . consult-bookmark)
("c l" . consult-line)
("c m" . consult-mark)
("c g" . consult-global-mark)
("c p" . consult-project-buffer)
("c k" . consult-locate))))
(use-package embark
:ensure t
:bind (("C-." . embark-act)
;; "M-." . embark-dwim
("C-h B" . embark-bindings)
)
:init
(setq prefix-help-command #'embark-prefix-help-command))
(use-package embark-consult
:ensure t)
(use-package rainbow-mode
:ensure t
:init
(setq rainbow-html-colors nil)
(setq rainbow-x-colors nil)
:hook (emacs-lisp-mode
css-mode))
(use-package grep
:config
(setq grep-find-ignored-directories
(append grep-find-ignored-directories '("target" "out" "node_modules" "build" "dist" "bower")))
(add-hook 'grep-mode-hook (lambda () (toggle-truncate-lines 1))))
(use-package idle-highlight-mode
:ensure t
:hook (prog-mode))
(use-package multiple-cursors
:ensure t
:bind (:map matt-keymap
("<down>" . mc/mark-more-like-this-extended)
("m r" . mc/edit-lines) ;; mnemonic "multiple-cursors rectangle"
("r r" . mc/mark-all-symbols-like-this-in-defun))) ;; mnemonic "refactor rename"
(use-package move-text
:ensure t
:bind (("C-S-<up>" . move-text-up)
("C-S-<down>" . move-text-down)))
(use-package string-inflection
:ensure t
:config
(defun string-inflection-snake-kebab-function (str)
"foo_bar => foo-bar"
(if (string-inflection-kebab-case-p str)
(string-inflection-underscore-function str)
(string-inflection-kebab-case-function str)))
(defun string-inflection-snake-kebab ()
"foo_bar => foo-bar"
(interactive)
(string-inflection--single-or-region #'string-inflection-snake-kebab-function))
:bind (:map matt-keymap
("-" . string-inflection-snake-kebab)
("M--" . string-inflection-all-cycle)))
(use-package csv-mode
:ensure t
:defer t)
(use-package magit
:ensure t
:init
(setq magit-status-buffer-switch-function 'switch-to-buffer)
:bind (("C-x g" . magit)
(:map matt-keymap
("g l" . magit-log-buffer-file)
("g b" . magit-blame-addition)
("g c" . (lambda () (interactive) (find-file (concat (magit-gitdir) "config"))))
("g f" . magit-find-file)
("g d f" . magit-diff-buffer-file)))) ;; mnemonic "git diff file"
(use-package diff-hl
:ensure t
:init
(global-diff-hl-mode 1)
:bind ("C-c g" . diff-hl-command-map))
(use-package ediff
:defer t
:config
(setq ediff-window-setup-function 'ediff-setup-windows-plain) ;; don't start another frame
(setq ediff-split-window-function 'split-window-horizontally))
(use-package calendar
:init
(add-hook 'calendar-today-visible-hook 'calendar-mark-today)
(setq calendar-week-start-day 1)
:bind (:map matt-keymap
("o c" . calendar)))
(use-package org
:defer t
:init
(defface org-doing-face '((t (:inherit org-todo))) "org mode face for DOING items")
(defface org-postponed-face '((t (:inherit org-done))) "org mode face for POSTPONED items")
(defface org-query-face '((t (:inherit org-todo))) "org mode face for ??? (query) items")
(setq org-fontify-done-headline nil)
(setq org-todo-keywords '((sequence "TODO" "DOING" "DONE")
(sequence "BLOCKED")
(sequence "POSTPONED")
(sequence "???" "DONE")))
(setq org-todo-keyword-faces
'(("TODO" org-todo)
("DONE" org-done)
("DOING" org-doing-face)
("POSTPONED" org-postponed-face)
("BLOCKED" org-postponed-face)
("???" org-query-face)))
(setq org-adapt-indentation t) ;; fixes indentation of drawers (e.g. logbook/clock)
(setq org-startup-folded t)
(setq org-cycle-hide-drawer-startup nil)
:config
(use-package org-clock)
(setq org-duration-format 'h:mm) ;; display days as hours
(defun matt-org-insert-timestamp ()
(interactive)
(org-insert-time-stamp (org-current-time 0) 'with-hm 'inactive))
(defun matt-org-clock-in ()
"A stripped down version of `org-clock-in' that just inserts
the current time in the correct position & format."
(interactive)
(save-excursion
(org-clock-find-position nil)
(insert-before-markers "\n")
(backward-char 1)
(org-indent-line)
(insert org-clock-string " ")
(matt-org-insert-timestamp)))
(defun matt-org-clock-out ()
"A stripped down version of `org-clock-out' that just inserts
the current time in the correct position & format."
(interactive)
(save-excursion
(org-clock-find-position nil)
;; TODO: verify that we haven't already clocked out
(goto-char (line-end-position))
(insert "--")
(matt-org-insert-timestamp)
(org-clock-update-time-maybe)))
(defun matt-org-clock-report ()
(interactive)
(save-excursion
(org-clock-report t))
;; scroll to the clock report at the top of the page
(scroll-down))
(defun matt-org-clock-fortnight ()
(interactive)
(goto-char (line-beginning-position))
(org-insert-heading nil nil :top)
(insert "F?")
(org-insert-subheading nil)
(insert "Forecast")
(let* (;; TODO: find the previous Monday, if not today
(t1 (decode-time (current-time)))
(d1 (encode-time (make-decoded-time :second 0
:minute 0
:hour 0
:day (decoded-time-day t1)
:month (decoded-time-month t1)
:year (decoded-time-year t1)
:dst (decoded-time-dst t1)
:zone (decoded-time-zone t1)))))
(mapc (lambda (i)
(org-clock-find-position nil)
(insert-before-markers "\n")
(backward-char 1)
(org-indent-line)
(insert org-clock-string " ")
(org-insert-time-stamp (time-add d1 (+ (* i 24 60 60)
(* 8 60 60)))
'with-hm 'inactive)
(insert "--")
(org-insert-time-stamp (time-add d1 (+ (* i 24 60 60)
(* 16 60 60)))
'with-hm 'inactive)
(org-clock-update-time-maybe))
;; MTWTF MTWT
'(0 1 2 3 4 7 8 9 10)))
(org-insert-heading nil)
(insert "W?")
(matt-org-clock-in))
:bind ((:map org-mode-map
("C-<up>" . org-backward-element)
("C-<down>" . org-forward-element)
("C-S-<up>" . org-metaup)
("C-S-<down>" . org-metadown)
("S-<up>" . nil)
("S-<down>" . nil)
("S-<left>" . nil)
("S-<right>" . nil)
("M-<left>" . nil)
("M-<right>" . nil)
("C-<left>" . nil)
("C-<right>" . nil)
("C-c <left>" . org-shiftleft)
("C-c <right>" . org-shiftright)
("C-c <up>" . org-shiftup)
("C-c <down>" . org-shiftdown)
("M-." . org-open-at-point))
(:map matt-keymap
("c i" . matt-org-clock-in)
("c o" . matt-org-clock-out)
("c r" . matt-org-clock-report)
("c f" . matt-org-clock-fortnight)
("l l" . org-store-link)
("l t" . org-toggle-link-display))))
(use-package org-bullets
:ensure t
:init (setq org-bullets-bullet-list '("●" "○"))
:hook (org-mode . org-bullets-mode))
(use-package rainbow-delimiters
:ensure t
:hook ((emacs-lisp-mode
lisp-mode
clojure-mode
cider-repl-mode
scheme-mode)
. rainbow-delimiters-mode-enable))
(use-package elisp-mode
:bind (:map emacs-lisp-mode-map
("C-c C-k" . eval-buffer)))
(use-package elisp-slime-nav
:ensure t
:hook (emacs-lisp-mode . elisp-slime-nav-mode))
(use-package markdown-mode
:ensure t
:defer t
:bind (:map markdown-mode-map
("M-<left>" . nil)
("M-<right>" . nil)))
(use-package js
:init
(setq js-indent-level tab-width)
:mode (("\\.json\\'" . js-mode)
("\\.cfn\\'" . js-mode))) ;; cloud formation
(use-package lua-mode
:ensure t
:defer t)
(use-package css-mode
:ensure t
:config
(setq css-indent-offset tab-width)
:defer t)
(use-package cc-mode
:init
(add-hook 'java-mode-hook (lambda () (c-set-offset 'arglist-intro '+)))
:defer t)
(use-package web-mode
:ensure t
:mode (("\\.jsx\\'" . web-mode)
("\\.mustache\\'" . web-mode))