-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.el
1359 lines (1253 loc) · 46.7 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.el --- Initialization file for Emacs -*- lexical-binding:t -*-
;;; Commentary:
;; Emacs Startup File --- initialisation for Emacs
;; C-x C-e to evaluate current expression.
;; M-. to navigate to function source.
;; C-c C-d to navigate to function docs.
;; M-x consult-info-emacs
;;; Code:
;;; PACKAGE MANAGER
(progn ;;; Setup
(defvar elpaca-installer-version 0.7)
(defvar elpaca-directory
(expand-file-name "elpaca/" user-emacs-directory))
(defvar elpaca-builds-directory
(expand-file-name "builds/" elpaca-directory))
(defvar elpaca-repos-directory
(expand-file-name "repos/" elpaca-directory))
(defvar elpaca-order
'(elpaca :repo "https://github.com/progfolio/elpaca.git"
:ref nil :depth 1
:files (:defaults "elpaca-test.el" (:exclude "extensions"))
:build (:not elpaca--activate-package)))
(let* ((repo (expand-file-name "elpaca/" elpaca-repos-directory))
(build (expand-file-name "elpaca/" elpaca-builds-directory))
(order (cdr elpaca-order))
(default-directory repo))
(add-to-list 'load-path (if (file-exists-p build) build repo))
(unless (file-exists-p repo)
(make-directory repo t)
(when (< emacs-major-version 28) (require 'subr-x))
(condition-case-unless-debug err
(if-let
((buffer (pop-to-buffer-same-window "*elpaca-bootstrap*"))
((zerop (apply
#'call-process
`("git" nil ,buffer t "clone"
,@(when-let ((depth (plist-get order :depth)))
(list (format "--depth=%d" depth)
"--no-single-branch"))
,(plist-get order :repo) ,repo))))
((zerop (call-process "git" nil buffer t "checkout"
(or (plist-get order :ref) "--"))))
(emacs (concat invocation-directory invocation-name))
((zerop (call-process
emacs nil buffer nil
"-Q" "-L" "." "--batch"
"--eval"
"(byte-recompile-directory \".\" 0 'force)")))
((require 'elpaca))
((elpaca-generate-autoloads "elpaca" repo)))
(progn (message "%s" (buffer-string)) (kill-buffer buffer))
(error "%s" (with-current-buffer buffer (buffer-string))))
((error) (warn "%s" err) (delete-directory repo 'recursive))))
(unless (require 'elpaca-autoloads nil t)
(require 'elpaca)
(elpaca-generate-autoloads "elpaca" repo)
(load "./elpaca-autoloads")))
(add-hook 'after-init-hook #'elpaca-process-queues)
(elpaca `(,@elpaca-order))
;; Install use-package support
(elpaca elpaca-use-package
;; Enable use-package :ensure support for Elpaca.
(elpaca-use-package-mode))
;; Always load newest byte code
(setq load-prefer-newer t)
;; Lazy
(setq use-package-always-defer t)
;; Profiling - M-x use-package-report
;; (setq use-package-compute-statistics t)
;; Forces Custom to save all customizations in a separate file
(setq custom-file "~/.emacs.d/custom.el"))
;;; PATH - (needs to be called as the first use-package)
(use-package exec-path-from-shell
:ensure t
:demand t
:if (memq window-system '(mac ns x))
:init
(exec-path-from-shell-initialize))
;;; EMACS LISP LIBRARIES
(defmacro comment (&rest _)
"Ignore BODY, yields nil."
nil)
(use-package parseedn
:ensure t) ;; edn parsing
(use-package pcre2el
:ensure t) ;; regex conversion
;;; GENERAL
(use-package emacs
:init
:config
;; General settings
(setq-default
use-short-answers t
confirm-kill-emacs 'y-or-n-p
auto-save-default nil
auto-save-list-file-prefix nil
create-lockfiles nil
history-length 500
make-backup-files nil
backup-inhibited t
enable-recursive-minibuffers t
max-mini-window-height 1
ring-bell-function 'ignore
use-dialog-box nil
vc-follow-symlinks t
large-file-warning-threshold 100000000
recenter-positions '(top middle bottom)
set-mark-command-repeat-pop 't
echo-keystrokes 0.1
set-message-functions '(inhibit-message set-minibuffer-message)
inhibit-message-regexps '(".*recentf.*")
mac-command-modifier 'control
indent-tabs-mode nil
tab-width 2
tab-always-indent 'complete
sentence-end-double-space nil
history-delete-duplicates t
;; You might have to set eshell-hist-ignoredups to nil if for whatever
;; reason the history file gets deleted.
;; eshell-hist-ignoredups 'erase
)
;; force pin entry through emacs
(setenv "GPG_AGENT_INFO" nil)
(setq epg-pinentry-mode 'loopback
epa-file-select-keys nil)
;; Use Utf-8 encoding.
(when (fboundp 'set-charset-priority)
(set-charset-priority 'unicode))
(prefer-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(setq-default locale-coding-system 'utf-8
buffer-file-coding-system 'utf-8)
;; Display help in same window
(add-to-list 'display-buffer-alist
'("*Help*" display-buffer-same-window))
;; Add alias for insert-kbd-macro and call-last-kbd-macro that
;; matches kmacro naming of other commands.
(defalias 'kmacro-insert-macro 'insert-kbd-macro)
(defalias 'kmacro-call-last-macro 'call-last-kbd-macro)
(defun my/init ()
"Open init file (this file)."
(interactive)
(find-file "~/.emacs.d/init.el"))
(defun my/reload-init ()
"Reload init."
(interactive)
(save-buffer)
(load "~/.emacs.d/init.el"))
(defun my/zoom-in ()
"Zoom in all buffers."
(interactive)
(set-face-attribute
'default nil
:height (+ (face-attribute 'default :height) 10))
(when (eq major-mode 'nov-mode)
(my/nov-rerender-without-losing-point))
(when (eq major-mode 'eww-mode)
(eww-reload t)))
(defun my/zoom-out ()
"Zoom out all buffers."
(interactive)
(set-face-attribute
'default nil
:height (- (face-attribute 'default :height) 10))
(when (eq major-mode 'nov-mode)
(my/nov-rerender-without-losing-point))
(when (eq major-mode 'eww-mode)
(eww-reload t)))
(defun my/dedupe-kill (args)
"Prevent sequential duplicate items in kill ring."
(let ((string (car args))
(replace (cdr args))
(last (car-safe kill-ring)))
(when (equal last string)
(setq replace t))
(list string replace)))
(advice-add 'kill-new :filter-args #'my/dedupe-kill)
(defun my/exchange-point-and-mark-no-region ()
"Identical to \\[exchange-point-and-mark] but will not activate the region."
(interactive)
(exchange-point-and-mark)
(deactivate-mark nil))
(defun my/copy-buffer-file-name-as-kill ()
"Copy current buffer file name to kill ring."
(interactive)
(kill-new (buffer-file-name)))
;; Unbind suspend-frame.
;; This would cause the cursor to disappear if you pressed C-x C-z
;; by mistake.
(global-unset-key (kbd "C-x C-z"))
;; Unbind tmm-menubar as I never use it.
(global-unset-key (kbd "M-`"))
;; Unbind scroll down as I never use it.
;; Scroll up is also unbound (C-v is bound to something else).
(global-unset-key (kbd "M-v"))
;; Translations
(define-key key-translation-map ":" ";")
(define-key key-translation-map ";" ":")
(define-key key-translation-map (kbd "RET") (kbd "C-m"))
:bind ((:map global-map
("C-x f" . find-file)
("C-x C-f" . find-file)
("C-z" . undo)
("C-x d" . dired)
("C-x C-d" . dired)
("M-c" . org-capture)
("C-o" . my/other-window)
("C-x o" . my/other-window)
("C-x k" . kill-this-buffer)
("C-x 2" . (lambda () (interactive)
(split-window-sensibly)
(other-window 1)))
("C-x 3" . (lambda () (interactive)
(split-window-sensibly)
(other-window 1)))
("C-x -" . my/zoom-out)
("C-x C--" . my/zoom-out)
("C-x +" . my/zoom-in)
("C-x C-+" . my/zoom-in)
;; C-M-\ is a bit awkward and C-\ (toggle input method)
;; is not something I use.
("C-\\" . indent-region)
([remap exchange-point-and-mark] .
my/exchange-point-and-mark-no-region))
(:map minibuffer-local-map
("C-v" . topiary/yank)
("C-y" . topiary/yank)
("C-w" . topiary/kill)
("C-o" . my/other-window)))
:hook (after-init . (lambda ()
;; Enable visual line mode
(global-visual-line-mode)
;; Enable auto revert
(global-auto-revert-mode))))
(use-package elisp-mode
:config
(defun my/docs-for-elisp-symbol-at-point ()
"Show docs for elisp symbol at point."
(interactive)
(describe-function (symbol-at-point)))
;; Use function name face on use package declarations
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\(use-package\\)\\_>[ ']*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
(2 font-lock-function-name-face nil t))))
:bind (:map emacs-lisp-mode-map
("C-c C-d" . my/docs-for-elisp-symbol-at-point)
:map lisp-interaction-mode-map
("C-c C-d" . my/docs-for-elisp-symbol-at-point)))
(use-package startup
:init
;; Initial scratch message.
(setq initial-scratch-message
";; C-x C-e to evaluate current expression.
;; M-. to navigate to function source.
;; C-c C-d to navigate to function docs.
;; M-x consult-info-emacs to search Emacs/Elisp manuals.
;; M-x shortdoc-display-group to get elisp cheat sheet by category.\n\n")
;; This delays flymake until after initialisation
(setq initial-major-mode nil)
:hook (after-init . (lambda () (with-current-buffer "*scratch*"
(setq initial-major-mode
'lisp-interaction-mode)
(lisp-interaction-mode)))))
(use-package window
:config
(setq split-width-threshold 100)
(defun my/other-window ()
"Switch to another window. If no other window exists create one.
If in minibuffer close."
(interactive)
(if (window-minibuffer-p)
(when (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit))
(progn
(when (one-window-p)
(split-window-sensibly))
(other-window 1))))
(defun my/reset-window-layout (_)
"Reset window layout on width change. Triggers width threshold.
This can be used to make the window layout change based on frame size."
(unless (or (= (window-old-pixel-width) (window-pixel-width))
(one-window-p)
(not (frame-size-changed-p)))
(let* ((_ (other-window 1))
(other-buff (buffer-name)))
(delete-window)
(split-window-sensibly)
(other-window 1)
(switch-to-buffer other-buff)
(other-window 1))))
;; Automatically change window layout on frame resize.
(setq window-size-change-functions '(my/reset-window-layout))
:hook (after-init . (lambda ()
;; Sets the initial frame to fill the screen.
(toggle-frame-fullscreen)
(switch-to-buffer "*Messages*"))))
(use-package Info-mode
:init
(defun my/info-font-setup ()
(face-remap-add-relative 'variable-pitch
:height 1.2))
:hook ((Info-mode . variable-pitch-mode)
(Info-mode . my/info-font-setup)))
(use-package super-save
:ensure t
:defer 1
:init
(setq save-silently t)
(super-save-mode t))
(use-package bookmark
:config
;; Save Bookmarks on any change
(setq bookmark-save-flag 1)
;; Store bookmarks in emacs-sync
(setq bookmark-default-file "~/.emacs.d/emacs-sync/bookmarks"))
(use-package ls-lisp
:demand t
:config
;; Switch to use ls-lisp makes ls platform agnostic
;; (need to test with TRAMP).
(setq ls-lisp-use-insert-directory-program nil))
(use-package dired
:config
;; Directories first
(setq dired-listing-switches "--group-directories-first -alh")
;; Ensures Dired file lists are refreshed when files are
;; created/deleted/renamed.
;; Also hides auto revert message.
(setq dired-auto-revert-buffer t)
;; Bind return to alternate file, so that dired reuses same buffer.
(put 'dired-find-alternate-file 'disabled nil)
;; WDired (writable dired) can be accessed by making the dired
;; buffer writable with the binding C-x C-q. Any change you make to
;; the buffer will remain unchanged until you commit them by typing
;; C-c C-c. To cancel the changes and revert to the original state
;; you can type C-c k.
;; The feature bellow force confirmation in the case of potential
;; overwrites :caused by rename.
(setq wdired-confirm-overwrite t)
:bind (:map dired-mode-map
("RET" . dired-find-alternate-file))
;; Dired hide details by default
:hook ((dired-mode . dired-hide-details-mode)))
(use-package so-long
:defer 1
:config
(global-so-long-mode t))
(use-package kill-buffer-on-q
;; Convenience mode for killing buffer on q
:load-path "~/.emacs.d/elisp")
;;; PRIVACY
(use-package totp
;; further reading
;; https://www.masteringemacs.org/article/keeping-secrets-in-emacs-gnupg-auth-sources
;; https://www.masteringemacs.org/article/securely-generating-totp-tokens-emacs
:defer 1
:load-path "~/.emacs.d/elisp")
(use-package auth-source
:config
(setq auth-sources (quote ("~/.emacs.d/emacs-sync/.authinfo.gpg"))))
;;; VISUAL
(use-package my-theme
;; Note: the theme itself is actually loaded in early init
:config
;;; DYNAMIC THEME
;; Hook for after theme load.
;; Update the theme of these components on theme change.
(defvar after-load-theme-hook nil
"Hook run after a colour theme is loaded using `load-theme'.")
(defadvice load-theme (after run-after-load-theme-hook activate)
"Run `after-load-theme-hook'."
(run-hooks 'after-load-theme-hook))
(defun my/dim-parens ()
"Make parenthesis less prominent by matching comment face."
(font-lock-add-keywords
nil `((,(rx (any "()")) . 'font-lock-dim-face))))
(defun my/fade-characters ()
"Make some characters less prominent."
(font-lock-add-keywords
nil `((,(rx (any "[]{}_&#%~@.,")) . 'font-lock-comment-face))))
(defun my/color-important-words ()
"Make important words more prominent."
(font-lock-add-keywords
nil
'(("\\b[Ee]rrors?\\b\\|\\b[Ff]ailures?\\b\\|\\b[Ff]ail\\b\\|\\bERRORS?\\b\\|\\bFAILURES?\\b\\|\\bFAIL\\b"
. 'error)
("\\b[Ww]arnings?\\b\\|\\bWARNINGS?\\b"
. 'warning)
("\\b[Ss]uccesse?s?\\b\\|\\b[Pp]ass\\b\\|\\bSUCCESSE?S?\\b\\|\\bPASS\\b"
. 'success))))
;; Messages buffer is started before init is run so we can't use a hook
(with-current-buffer "*Messages*"
(my/color-important-words))
;; hooks
(add-hook 'messages-buffer-mode-hook 'my/color-important-words)
(add-hook 'clojure-mode-hook 'my/dim-parens)
(add-hook 'emacs-lisp-mode-hook 'my/dim-parens)
(add-hook 'clojure-mode-hook 'my/fade-characters)
(add-hook 'emacs-lisp-mode-hook 'my/fade-characters)
(add-hook 'eshell-mode-hook 'my/color-important-words)
(add-hook 'inferior-lisp-mode-hook 'my/color-important-words)
(add-hook 'inferior-lisp-mode-hook 'ansi-color-for-comint-mode-filter)
(add-hook 'shell-mode-hook 'my/color-important-words)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-filter)
(add-hook 'compilation-filter-hook 'my/ansi-colorize-buffer))
(use-package my-mode-line
:load-path "~/.emacs.d/elisp"
:hook (after-init . my/mode-line-init))
;;; META NAVIGATION
(use-package recentf
:defer 1
:config
(setq recentf-exclude
'(".*\.gpg"
".*\.gz)"
".emacs.d/emms/history"
".emacs.d/emacs-sync/.*"))
(setq recentf-max-saved-items 10)
(recentf-mode t))
(use-package isearch
:config
(setq search-highlight t)
(setq search-whitespace-regexp ".*?")
(setq isearch-lax-whitespace t)
(setq isearch-regexp-lax-whitespace nil)
(setq isearch-lazy-highlight t)
(setq search-invisible 'open)
(defun my/isearch-thing-at-point ()
(interactive)
(if (string-empty-p isearch-string)
(let ((bounds (topiary/bounds)))
(cond
(bounds
(when (< (car bounds) (point))
(goto-char (car bounds)))
;; We don't want the region to be active when navigating
;; between matches.
(when (region-active-p)
(deactivate-mark t))
(isearch-yank-string
(buffer-substring-no-properties (car bounds) (cdr bounds))))
(t
(setq isearch-error "No thing at point")
(isearch-push-state)
(isearch-update))))
(isearch-update)))
(defun my/goto-match-end ()
(when (and (not isearch-forward)
isearch-other-end
(not isearch-mode-end-hook-quit))
(goto-char isearch-other-end)))
(defun my/isearch-repeat-backward ()
(interactive)
(let ((previous-point (point)))
(isearch-repeat-backward)
(when (not (= (point) isearch-other-end))
(goto-char isearch-other-end))
(when (and (not isearch-error)
isearch-success
(= (point) previous-point))
(my/isearch-repeat-backward))))
;; Make isearch wrap automatically if it doesn't find anything
(defadvice isearch-search (after isearch-no-fail activate)
(unless isearch-success
(ad-disable-advice 'isearch-search 'after 'isearch-no-fail)
(ad-activate 'isearch-search)
(isearch-repeat (if isearch-forward 'forward))
(ad-enable-advice 'isearch-search 'after 'isearch-no-fail)
(ad-activate 'isearch-search)))
:hook (('isearch-mode-end . my/goto-match-end))
:bind
(:map isearch-mode-map
("DEL" . isearch-del-char)
("TAB" . isearch-exit)
("C-i" . isearch-toggle-invisible)
("M-i" . isearch-toggle-invisible)
("C-w" . isearch-del-char)
("C-g" . isearch-cancel)
("C-n" . isearch-repeat-forward)
("C-p" . my/isearch-repeat-backward)
("C-s" . my/isearch-thing-at-point)
("C-r" . isearch-query-replace)
("C-v" . isearch-yank-kill)
("C-y" . isearch-yank-kill)))
(use-package vertico
:ensure t
:init
(vertico-mode))
(use-package vertico-prescient
:ensure t
:after vertico
:demand t
:config
(vertico-prescient-mode t)
(prescient-persist-mode t))
(use-package corfu-prescient
:ensure t
:after corfu
:demand t
:config
(corfu-prescient-mode t))
(use-package project
:init
;; Don't include submodule files in searches etc
(setq project-vc-merge-submodules nil))
(use-package transient
:ensure t)
(use-package magit
:ensure t
:config
(setq magit-diff-highlight-indentation nil)
(setq magit-diff-highlight-trailing nil)
(setq magit-diff-paint-whitespace nil)
(setq magit-revision-insert-related-refs nil)
(setq magit-save-repository-buffers 'dontask)
(setq magit-display-buffer-function 'magit-display-buffer-same-window-except-diff-v1)
(setq magit-diff-refine-hunk 'all)
(setq magit-diff-refine-ignore-whitespace t)
(setq magit-log-margin '(t "%Y-%m-%d %H:%M " magit-log-margin-width t 18))
(setq magit-process-find-password-functions
'(magit-process-password-auth-source))
(setq magit-process-finish-apply-ansi-colors t)
;; Allows q to be used to quit transient buffers
(transient-bind-q-to-quit)
(defun my/get-github-token ()
(funcall
(plist-get
(car (auth-source-search :max 1 :host "github.com"))
:secret)))
(defun my/get-github-username ()
(plist-get
(car (auth-source-search :max 1 :host "github.com"))
:user))
(defun my/magit-spin-off-pull-request ()
"Spin off last commit as a pull request."
(interactive)
(when (y-or-n-p "Spin off pull request?")
(let* ((commit-name (magit-rev-format
"%s"
(or (magit-get-current-branch) "HEAD")))
(branch-name (replace-regexp-in-string
"\\s-+" "-"
(downcase commit-name)))
(from (car (last (magit-region-values 'commit))))
(master-name (car (seq-filter (lambda (n) (or (equal n "master") (equal n "main")))
(magit-list-branch-names)))))
(magit--branch-spinoff branch-name from t)
(with-environment-variables
(("GITHUB_TOKEN" (my/get-github-token)))
(magit-shell-command-topdir
(concat
"git push -u origin " branch-name
";gh pr create --fill --head " branch-name
";git checkout " master-name))))))
(defun my/magit-create-private-github-remote ()
"Create a private repo on github, add remote as origin
and push local commits."
(interactive)
(with-environment-variables
(("GITHUB_TOKEN" (my/get-github-token)))
(magit-shell-command-topdir
(concat
"gh repo create " (my/get-github-username) "/"
(file-name-nondirectory
(directory-file-name default-directory))
" --private --source=. --remote=origin --push"))))
(defun my/current-pr-number ()
(number-to-string (oref (forge-current-pullreq) number)))
(defun my/forge-approve-pull-request ()
"Approve current pull request."
(interactive)
(when-let ((pr-number (my/current-pr-number)))
(with-environment-variables
(("GITHUB_TOKEN" (my/get-github-token)))
(magit-shell-command-topdir
(concat "gh pr review " pr-number " --approve --body '🧞'")))))
(defun my/magit-search-git-log-for-change ()
"Search git log for current symbol or topiary region.
If region spans multiple lines does regex or of each trimmed line.
This effectively returns all changes to that set of lines. Or anything
in the file that matches one of those lines.
Lines containing common patterns that appear throughout the file can
lead to unrelated results. For example (interactive) in this file
would lead to a large number of unrelated results as it's a very
common occurrence.
If this becomes a problem these common lines could be filtered."
(interactive)
(let* ((bounds (topiary/compute-bounds))
(region-str (or
(thing-at-point 'symbol t)
(buffer-substring (car bounds) (cdr bounds))))
(pcre-regex (and region-str
(concat
"("
(mapconcat
(lambda (line)
(concat ".?" (string-trim line) ".?"))
(split-string
(rxt-elisp-to-pcre
(regexp-quote region-str)) "\n")
"|")
")"))))
(if-let ((file (magit-file-relative-name)))
(magit-log-setup-buffer
(list (or magit-buffer-refname
(magit-get-current-branch)
"HEAD"))
(list "--follow" (concat "-G " pcre-regex))
(and file (list file))
magit-log-buffer-file-locked)
(user-error "Buffer isn't visiting a file"))))
:bind (("C-x g" . magit-status))
:hook ((after-save . magit-after-save-refresh-status)))
(use-package forge
:ensure t
;; For generating tokens see: https://github.com/settings/tokens
:after magit
:demand t)
(use-package hl-todo
:ensure t
:config
(setq hl-todo-keyword-faces
'(("TODO" . bold)
("EXPLORE" . bold)))
:hook (prog-mode . hl-todo-mode))
(use-package magit-todos
:ensure t
:after magit
:demand t
:config
(magit-todos-mode)
(setq magit-todos-auto-group-items 15)
(setq magit-todos-group-by '(magit-todos-item-keyword)))
(use-package browse-at-remote
:ensure t
:init
(defun my/git-url-for-region ()
(interactive)
(browse-at-remote-kill)
(message "git url for region yanked!")))
(use-package org
:config
;; Org babel/source blocks
(setq org-src-fontify-natively t
org-src-window-setup 'current-window
org-src-strip-leading-and-trailing-blank-lines t
org-src-preserve-indentation t
org-src-tab-acts-natively t
org-adapt-indentation nil)
(defun my/org-todo-sort ()
"Sort sections by TODO."
(interactive)
(ignore-errors (outline-up-heading 10))
(org-sort-entries nil ?o)
(org-cycle)
(org-cycle))
;; Capture templates.
(setq org-capture-templates
'(("t" "Todo" entry
(file+headline "~/.emacs.d/emacs-sync/org/tasks.org" "Tasks")
"* TODO %?"))))
(use-package consult
:ensure t
:bind
(("C-x b" . my/consult-omni)
("C-x C-b" . my/consult-omni)
("C-x p" . my/consult-omni-project-files-only)
("C-x r b" . consult-bookmark)
("C-M-s" . my/consult-ripgrep)
("M-y" . consult-yank-pop)
("M-g g" . consult-goto-line)
("M-g M-g" . consult-goto-line)
("C-h i" . consult-info))
:init
(defun my/consult-project-with-root (root)
"Return the project for a given project ROOT."
(project--find-in-directory root))
(defun my/consult-project-files (root)
"Compute the project files given the ROOT."
(let* ((project (my/consult-project-with-root root))
(files (project-files project)))
(mapcar (lambda (f) (file-relative-name f root)) files)))
(defun my/consult-file (selected-root)
"Create a view for selecting project files for the project at SELECTED-ROOT."
(let ((candidate (consult--read
(my/consult-project-files selected-root)
:prompt "Project File: "
:sort t
:require-match t
:category 'file
:state (consult--file-preview)
:history 'file-name-history)))
(consult--file-action (concat selected-root candidate))))
(defun my/consult-find-with-concat-root (candidate)
"Find-file concatenating root with CANDIDATE."
(consult--file-action (concat (project-root (project-current)) candidate)))
(defvar my/consult-source-file
`(:name "Project File"
:narrow (?f . "File")
:category file
:face consult-file
:history file-name-history
:enabled ,#'project-current
:action ,#'my/consult-find-with-concat-root
:items
,(lambda ()
(my/consult-project-files (project-root (project-current))))))
(defvar my/consult-source-project
`(:name "Known Project"
:narrow (?p . "Project")
:category file
:face consult-file
:history file-name-history
:annotate ,(lambda (dir)
(format "Project: %s"
(file-name-nondirectory (directory-file-name dir))))
:action ,#'my/consult-file
:items ,#'project-known-project-roots))
(setq my/consult-omni-sources
'(consult--source-buffer
my/consult-source-project
my/consult-source-file
consult--source-recent-file
consult--source-bookmark))
(defun my/consult-omni ()
(interactive)
(consult-buffer my/consult-omni-sources))
(defun my/consult-omni-project-files-only ()
(interactive)
(consult-buffer '(my/consult-source-file)))
(defun consult-info-emacs ()
"Search through Emacs info pages."
(interactive)
(consult-info "emacs" "efaq" "elisp" "cl" ))
(defun my/consult-ripgrep ()
"Search with `rg' for files in DIR with INITIAL input.
See `consult-grep' for details."
(interactive)
(let* ((bounds (topiary/bounds)))
(copy-region-as-kill (car bounds) (cdr bounds))
(call-interactively 'consult-ripgrep)))
:hook (completion-list-mode . consult-preview-at-point-mode)
:config
;; Disable preview, to enable set to 'any
(setq consult-preview-key nil))
(use-package embark-consult
:ensure t
:hook
(embark-collect-mode . consult-preview-at-point-mode))
(use-package embark
:ensure t
:bind
(("C-." . embark-act)
("M-." . embark-dwim)
;; alternative for `describe-bindings'
("C-h b" . embark-bindings)
("C-h C-h" . embark-prefix-help-command))
:init
;; Replace the key help with a completing-read interface
(setq prefix-help-command #'embark-prefix-help-command)
;; More minimalist embark (using completion)
(setq embark-prompter 'embark-completing-read-prompter)
(setq embark-indicators
'(embark-minimal-indicator ; default is embark-mixed-indicator
embark-highlight-indicator
embark-isearch-highlight-indicator))
:config
;; Configure embark-dwim actions
;; Don't want flymake at point as a target (would rather go to source)
(delete 'embark-target-flymake-at-point embark-target-finders))
(use-package wgrep
:ensure t)
;;; TEXT FORMATTING
(use-package hideshow
:config
(setq hs-hide-comments-when-hiding-all nil)
(defun my/display-most-sever-flymake-error (ov)
"Display most sever error in folded code block at top level."
(when (eq 'code (overlay-get ov 'hs))
(let* ((most-sever-error
(car (sort (flymake--overlays :beg (overlay-start ov)
:end (overlay-end ov))
(lambda (a b) (> (overlay-get a 'severity)
(overlay-get b 'severity))))))
(level (and most-sever-error
(overlay-get most-sever-error 'category)))
(marker-string (concat "*" (format "%s" level) "*")))
(if most-sever-error
(let* ((error-face (overlay-get most-sever-error 'face))
(error-face (if (listp error-face)
;; handle eglot diagnostic sometimes
;; being returned as list
(car error-face)
error-face)))
(overlay-put ov 'before-string
(propertize marker-string
'display
(list 'left-fringe
'my/flymake-fringe-indicator
error-face))))
(overlay-put ov 'before-string nil)))))
(setq hs-set-up-overlay 'my/display-most-sever-flymake-error)
(defvar my/previous-flymake-errors nil)
(defun my/toggle-defun-level-hiding ()
"Toggle folded code at top level without losing cursor position."
(interactive)
(save-excursion
;; handle being at the end of a defun
(when (and (char-before ?\))
(member (char-after) (string-to-list "\n ")))
(backward-char 1))
;; Handles being inside a defun
(unless (<= (nth 0 (syntax-ppss)) 0)
(goto-char (car (nth 9 (syntax-ppss)))))
(hs-toggle-hiding)))
(defun my/refresh-folded-code-errors ()
"Refresh folded code that contains errors to make them visible at the top level."
(let ((current-flymake-errors (flymake--overlays)))
(unless (equal current-flymake-errors my/previous-flymake-errors)
(dolist (ov (overlays-in (point-min) (point-max)))
(when (overlay-get ov 'hs)
(my/display-most-sever-flymake-error ov)))
(setq my/previous-flymake-errors current-flymake-errors))))
(defadvice flymake--handle-report (after refresh-folded-errors activate)
(my/refresh-folded-code-errors))
:hook (((emacs-lisp-mode clojure-mode)
. (lambda ()
(hs-minor-mode) (hs-hide-all))))
:bind (:map hs-minor-mode-map
("TAB" . my/toggle-defun-level-hiding)
("<backtab>" . hs-hide-all)))
(use-package subword
:init
(global-subword-mode))
(use-package topiary
:load-path "~/.emacs.d/elisp"
:hook ((text-mode prog-mode comint-mode outline-mode Info-mode eshell-mode magit-blob-mode) . topiary-mode))
(use-package special-mode
:bind (:map special-mode-map
("C-w" . topiary/kill)))
;;; WRITING
(use-package text-scratch
:load-path "~/.emacs.d/elisp"
:demand t)
(use-package abbrev
:init
(setq-default abbrev-mode t)
:config
(defun my/wiki-style-misspellings->abbrev-table ()
"Convert wiki style misspellings to abbrev table entries.
https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/For_machines"
(interactive)
;; remove entries with more than one option
(call-interactively 'mark-whole-buffer)
(flush-lines ",")
;; convert entries to abbrev entries
(while (not (eobp))
(insert "(\"")
(skip-chars-forward "^-")
(insert "\" ")
(delete-char 2)
(insert "\"")
(end-of-line)
(insert "\" nil :count 1)")
(forward-line 1))))
;;; LINTING
(use-package flyspell
:defer 1
:config
(setq ispell-program-name "aspell"
ispell-extra-args '("--sug-mode=ultra" "--lang=en_GB"))
;; Spellchek docs and comments in prog-mode but not strings
(setq flyspell-prog-text-faces (delq 'font-lock-string-face
flyspell-prog-text-faces))
(setq ispell-personal-dictionary "~/.emacs.d/setup/dotfiles/.aspell.en.pws")
:hook
((text-mode . flyspell-mode)
(prog-mode . flyspell-prog-mode)))
(use-package flymake
:init
(define-fringe-bitmap 'my/flymake-fringe-indicator
(vector #b00000000
#b00000000
#b00000000
#b00000000
#b11111111
#b11111111
#b11111111
#b11111111
#b11111111
#b11111111
#b11111111
#b11111111
#b00000000
#b00000000
#b00000000
#b00000000
#b00000000))
(setq flymake-error-bitmap '(my/flymake-fringe-indicator flymake-error))
(setq flymake-warning-bitmap '(my/flymake-fringe-indicator flymake-warning))
(setq flymake-note-bitmap '(my/flymake-fringe-indicator flymake-note))
:config
(custom-set-variables
'(help-at-pt-timer-delay 0.1)
'(help-at-pt-display-when-idle '(flymake-diagnostic)))
:hook
(emacs-lisp-mode . (lambda () (flymake-mode t))))
;;; COMPLETION
(use-package corfu
:ensure t
:custom
(corfu-auto t)
(corfu-preview-current nil)
(setq corfu-bar-width 0)