-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
4201 lines (3421 loc) · 154 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 --- Nicolai Singh's .emacs -*- lexical-binding: t -*-
;;; Commentary:
;; This contains my personal Emacs settings.
;;; Code:
(defmacro skipped (&rest body)
"Discard BODY."
(declare (indent nil))
nil)
;;;; Startup
(setq original-gc-cons-percentage gc-cons-percentage
higher-gc-cons-percentage 0.6
gc-idle-timer nil)
(defun start-gc-idle-timer ()
"Set a timer that GCs when Emacs idles, like package gcmh."
(when (timerp gc-idle-timer) (cancel-timer gc-idle-timer))
(setq gc-idle-timer (run-with-idle-timer 5 t #'garbage-collect)))
(defun increase-gc-cons-percentage ()
"Set a higher value for gc-cons-percentage to prevent garbage
collection. Use revert-gc-cons-percentage to restore the value."
(setq gc-cons-percentage higher-gc-cons-percentage))
(defun revert-gc-cons-percentage ()
"Restore gc-cons-percentage to its original value."
(setq gc-cons-percentage original-gc-cons-percentage))
(increase-gc-cons-percentage)
(start-gc-idle-timer)
(add-hook 'after-init-hook (lambda ()
(revert-gc-cons-percentage)
(garbage-collect)))
;;;; General settings
(setq auto-hscroll-mode 'current-line
auto-save-interval 50
auto-save-timeout 3
history-delete-duplicates t
scroll-margin 0
tab-width 4
truncate-lines t
user-full-name "Nicolai Singh")
(if (boundp 'use-short-answers)
(setq use-short-answers t)
(fset 'yes-or-no-p 'y-or-n-p))
;; Source code
(setq find-function-C-source-directory "/run/current-system/sw/share/emacs/source/src")
(visit-tags-table (format "/run/current-system/sw/share/emacs/%s/lisp/TAGS" emacs-version))
(setq enable-recursive-minibuffers 1)
(minibuffer-depth-indicate-mode 1)
;; Don't wrap minibuffer entries
(add-hook 'minibuffer-setup-hook (lambda () (setq truncate-lines t)))
;; Tabs and spaces handling
(defmacro define-set-indent-tab-width-fn (width)
"Macro to define a simple function to set `tab-width' to WIDTH."
`(defun ,(intern (format "set-indent-tab-width-%d" width)) ()
(interactive)
,(format "Set `tab-width' to %d." width)
(setq tab-width ,width)))
(define-set-indent-tab-width-fn 2)
(define-set-indent-tab-width-fn 4)
(define-set-indent-tab-width-fn 8)
(add-hook 'shell-mode-hook #'set-indent-tab-width-8)
(add-hook 'emacs-lisp-mode-hook #'set-indent-tab-width-2)
;;; abbrev
(setq abbrev-file-name (expand-file-name "abbrev-defs.el" user-emacs-directory))
;;; bookmark
(setq bookmark-save-flag 1)
;;; custom
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(when (file-exists-p custom-file)
(load custom-file))
;;; facemenu
;; Move facemenu to another binding
(require 'facemenu)
(keymap-global-unset "M-o")
(keymap-global-set "C-c f m" #'facemenu-keymap)
;;; files
(setq auto-save-file-name-transforms `((".*" ,(concat user-emacs-directory "auto-save-files/") t))
backup-directory-alist `(("." . ,(concat user-emacs-directory "backups/")))
confirm-kill-emacs #'yes-or-no-p
large-file-warning-threshold nil
require-final-newline t
revert-without-query '("^.*\\.pdf$"))
;;; grep
(require 'grep)
(defun rgrep-dired (regexp files)
"Run `rgrep' in the current directory to match REGEXP in FILES."
(interactive "srgrep regexp: \nsfilename wildcard (blank for `*'): ")
(let* ((case-fold-search (if current-prefix-arg nil t))
(files (if (equal files "") "*" files)))
(when (eq grep-find-template nil)
(grep-compute-defaults))
(rgrep regexp files (dired-current-directory) nil)))
(defun lgrep-dired (regexp files)
"Run `lgrep' in the current directory to match REGEXP in FILES."
(interactive "slgrep regexp: \nsfilename wildcard (blank for `*'): ")
(let* ((case-fold-search (if current-prefix-arg nil t))
(files (if (equal files "") "*" files)))
(when (eq grep-find-template nil)
(grep-compute-defaults))
(lgrep regexp files (dired-current-directory) nil)))
(eval-after-load 'grep
'(progn
(add-to-list 'grep-find-ignored-directories "node_modules")))
(require 'dired)
(keymap-set dired-mode-map "C-c m g r" #'rgrep-dired)
(keymap-set dired-mode-map "C-c m g l" #'lgrep-dired)
;;; help
(setq help-window-select t)
;;; indent
(setq tab-always-indent 'complete)
;;; minibuffer
(setq completions-format 'one-column)
;;; novice
(setq disabled-command-function nil)
;;; paren
(setq show-paren-delay 0
show-paren-style 'parenthesis
show-paren-when-point-inside-paren nil)
;;; register
(setq register-preview-delay 0)
;;; repeat
(repeat-mode 1)
(with-eval-after-load 'org-roam
(defvar-keymap org-roam-dailies-repeat-map
:repeat t
"." #'org-roam-dailies-goto-next-note
"," #'org-roam-dailies-goto-previous-note))
(with-eval-after-load 'calendar
(defvar-keymap calendar-mode-repeat-map
:repeat t
"]" #'calendar-forward-year
"[" #'calendar-backward-year))
(with-eval-after-load 'howm
(defvar-keymap howm-mode-repeat-map
:repeat t
"n" #'action-lock-goto-next-link
"p" #'action-lock-goto-previous-link
"N" #'howm-next-memo
"P" #'howm-previous-memo
"F" #'howm-first-memo
"L" #'howm-last-memo))
;;; saveplace
(save-place-mode 1)
;;; simple
(setq eval-expression-print-length nil ; don't truncate when evaluating exprs
;; eval-expression-print-level nil
kill-do-not-save-duplicates t
kill-whole-line t
next-line-add-newlines t
save-interprogram-paste-before-kill t
set-mark-command-repeat-pop t)
(defun no-indent-tabs-mode ()
"Turn of `indent-tabs-mode' (i.e. indent using spaces)."
(indent-tabs-mode -1))
(add-hook 'before-save-hook #'delete-trailing-whitespace)
(add-hook 'emacs-lisp-mode-hook #'no-indent-tabs-mode)
(add-hook 'kotlin-mode-hook #'no-indent-tabs-mode)
(add-hook 'scheme-mode-hook #'no-indent-tabs-mode)
(add-hook 'typescript-ts-mode-hook #'no-indent-tabs-mode)
;;; startup
(setq user-mail-address "[email protected]"
initial-scratch-message ""
initial-major-mode 'fundamental-mode)
;;; tab-bar
(setq tab-bar-tab-hints t)
(defun my-select-tab-or-other-window (arg)
(interactive "P")
(cond
((eq 0 arg) (tab-recent))
((numberp arg) (tab-bar-select-tab arg))
(t (other-window 1))))
(keymap-global-set "C-x t T" #'tab-bar-mode)
(keymap-global-set "C-`" #'my-select-tab-or-other-window)
;;; vc
(setq vc-follow-symlinks t)
;;; window
(setq same-window-regexps '("^magit: .*$"
"^magit-status: .*$"))
;;; winner
(winner-mode 1)
;;;; Keybinding prefixes
(define-prefix-command 'my-ctl-c-D-map)
(define-prefix-command 'my-ctl-c-M-map)
(define-prefix-command 'my-ctl-c-b-map)
(define-prefix-command 'my-ctl-c-c-map)
(define-prefix-command 'my-ctl-c-d-map)
(define-prefix-command 'my-ctl-c-e-map)
(define-prefix-command 'my-ctl-c-f-map)
(define-prefix-command 'my-ctl-c-g-map)
(define-prefix-command 'my-ctl-c-h-map)
(define-prefix-command 'my-ctl-c-i-map)
(define-prefix-command 'my-ctl-c-l-map)
(define-prefix-command 'my-ctl-c-m-map)
(define-prefix-command 'my-ctl-c-n-map)
(define-prefix-command 'my-ctl-c-o-map)
(define-prefix-command 'my-ctl-c-p-map)
(define-prefix-command 'my-ctl-c-s-map)
(define-prefix-command 'my-ctl-c-t-map)
(define-prefix-command 'my-ctl-c-v-map)
(define-prefix-command 'my-ctl-c-w-map)
(define-prefix-command 'my-ctl-c-y-map)
(define-prefix-command 'my-ctl-z-map)
(keymap-global-set "C-c D" 'my-ctl-c-D-map)
(keymap-global-set "C-c M" 'my-ctl-c-M-map)
(keymap-global-set "C-c b" 'my-ctl-c-b-map)
(keymap-global-set "C-c c" 'my-ctl-c-c-map)
(keymap-global-set "C-c d" 'my-ctl-c-d-map)
(keymap-global-set "C-c e" 'my-ctl-c-e-map)
(keymap-global-set "C-c f" 'my-ctl-c-f-map)
(keymap-global-set "C-c g" 'my-ctl-c-g-map)
(keymap-global-set "C-c h" 'my-ctl-c-h-map)
(keymap-global-set "C-c i" 'my-ctl-c-i-map)
(keymap-global-set "C-c l" 'my-ctl-c-l-map)
(keymap-global-set "C-c m" 'my-ctl-c-m-map)
(keymap-global-set "C-c n" 'my-ctl-c-n-map)
(keymap-global-set "C-c o" 'my-ctl-c-o-map)
(keymap-global-set "C-c p" 'my-ctl-c-p-map)
(keymap-global-set "C-c s" 'my-ctl-c-s-map)
(keymap-global-set "C-c t" 'my-ctl-c-t-map)
(keymap-global-set "C-c v" 'my-ctl-c-v-map)
(keymap-global-set "C-c w" 'my-ctl-c-w-map)
(keymap-global-set "C-c y" 'my-ctl-c-y-map)
(keymap-global-set "C-z" 'my-ctl-z-map)
;;;; Minor modes
(define-minor-mode window-dedicated-mode
"Minor mode for making windows dedicated."
:global nil
:init-value nil
:lighter " DEDICATED"
(set-window-dedicated-p (get-buffer-window) window-dedicated-mode))
(define-minor-mode reader-mode
"Make a reader-friendly view by removing screen distractions and adding margins."
:init-value nil
:lighter " Reader"
:global nil
:group 'reader
(let ((enabled (if reader-mode t -1)))
(writeroom-mode enabled)
(visual-line-mode enabled)))
(define-generic-mode 'xmodmap-mode
'(?!)
'("add" "clear" "keycode" "keysym" "pointer" "remove")
nil
'("[xX]modmap\\(rc\\)?\\'")
nil
"Simple mode for xmodmap files.
From https://www.emacswiki.org/emacs/XModMapMode")
(define-generic-mode miranda-mode
'("||")
'("where" "if" "otherwise")
nil
nil
nil
"Generic major mode for the Miranda programming language.")
(keymap-global-set "C-c w d" #'window-dedicated-mode)
;;;; Functions
(defun my-sort-init-file ()
"Sort the init file."
(interactive)
(save-excursion
(goto-char (point-min))
;; Packages
(re-search-forward "^;;;; Package loads$")
(forward-page)
(let ((start (point)))
(point-marker)
(end-of-buffer)
(backward-page)
(sort-pages nil start (point)))))
(defun find-init-file ()
"Find my Emacs init file."
(interactive)
(find-file (expand-file-name "init.el" user-emacs-directory)))
(defun find-scratch-buffer ()
"Find the *scratch* buffer."
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*")))
(defun sudo-find-file (file-or-buffer)
"Find local file/directory FILE-OR-BUFFER as sudo."
(let ((sudomethod (if (< emacs-major-version 27) "/sudo::" "/sudoedit::")))
(find-file (concat sudomethod file-or-buffer))))
(defun sudo-find-alternate-file ()
"Alternate find a file/directory as sudo."
(interactive)
(require 'tramp)
(let ((current-buffer (cond
((derived-mode-p 'dired-mode) (dired-current-directory))
((buffer-file-name) (buffer-file-name))
(t (error "Buffer is not visiting a file")))))
(if (tramp-tramp-file-p current-buffer)
;; Use tramp to sudo
(let ((sudomethod "sudo")
(sudouser "root")
(vec (tramp-dissect-file-name tramp-file-name)))
(find-alternate-file (tramp-make-tramp-file-name
sudomethod
sudouser
(tramp-file-name-domain vec)
(tramp-file-name-host vec)
nil ;; PORT
(tramp-file-name-localname vec)
(tramp-make-tramp-hop-name vec))))
;; Emacs 27 added connection method `/sudoedit' for security
;; reasons. Use if available; otherwise, use `/sudo'.
(let ((sudomethod (if (< emacs-major-version 27) "/sudo::" "/sudoedit::")))
(find-alternate-file (concat sudomethod current-buffer))))))
(defun indent-using-tabs-and-fixup ()
(interactive)
(indent-tabs-mode 1)
(tabify (point-min) (point-max)))
(defun indent-using-spaces-and-fixup ()
(interactive)
(indent-tabs-mode -1)
(untabify (point-min) (point-max)))
(defun toggle-line-and-column-numbers ()
"Toggle `line-number-mode' and `column-number-mode'."
(interactive)
(line-number-mode 'toggle)
(column-number-mode 'toggle))
(defun benchmark-this (repetitions)
"Time the execution of the last sexp or the region if active.
Specify a prefix argument to perform the execution REPETITIONS
times."
(interactive "p")
(let ((form (if (use-region-p)
(read (concat "(progn "
(buffer-substring-no-properties
(region-beginning)
(region-end))
")"))
(pp-last-sexp))))
(if (not form)
(error "No form found")
(message "Form: %S\nBenchmark: %S"
form
(eval `(benchmark-run ,repetitions ,form))))))
(defun profiler-toggle ()
(interactive)
(require 'profiler)
(if (profiler-running-p)
(profiler-stop)
(profiler-start 'cpu+mem)))
(defun repeatkey-repeatable-call (f &optional other-bindings)
"Call function F interactively, then allow the last key used to
repeat the call, similar to C-x z z z in `repeat'."
(setq repeatkey-last-command f)
(let ((repeat-char last-command-event))
(call-interactively f)
(set-transient-map
(let ((map (make-sparse-keymap)))
(define-key map (vector repeat-char)
(lambda ()
(interactive)
(repeatkey-repeatable-call repeatkey-last-command)))
(dolist (binding other-bindings)
(define-key map (kbd (car binding)) (cdr binding)))
map))))
(defun toggle-hscroll-mode ()
"Toggle `auto-hscroll-mode' between t and 'current-line."
(interactive)
(let ((value (if (eq auto-hscroll-mode t) 'current-line t)))
(setq auto-hscroll-mode value)
(message "auto-hscroll-mode: %s" value)))
(defun delete-undo-history ()
"Delete the undo history of this buffer."
(interactive)
(buffer-disable-undo)
(buffer-enable-undo))
(defun my-yank-to-other-window ()
"Yank the current word or the region, if active, to the other window."
(interactive)
(let ((text (if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(current-word)))
(separator (cond
((equal current-prefix-arg '(4)) " ")
((equal current-prefix-arg '(16)) "")
(t "\n"))))
(save-excursion
(set-buffer (window-buffer (next-window)))
(insert separator text))))
(defun my-sanitize-string (str)
(replace-regexp-in-string "[^[:alnum:]-]" "_" str))
(defun single-blank-lines-only ()
(interactive)
"Replace consecutive blank lines with single blank lines in the entire buffer."
(replace-regexp-in-region "\n+$" "\n" (point-min) (point-max)))
(defun my-presorted-completion-table (completions)
"Closure for achieving consistent sorting in completing-read."
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (cycle-sort-function . ,#'identity)
(display-sort-function . ,#'identity))
(complete-with-action action completions string pred))))
(keymap-global-set "C-c y o" #'my-yank-to-other-window)
(keymap-global-set "C-c i TAB" #'indent-using-tabs-and-fixup)
(keymap-global-set "C-c i SPC" #'indent-using-spaces-and-fixup)
(keymap-global-set "C-c D ." #'benchmark-this)
(keymap-global-set "C-c f #" #'sudo-find-alternate-file)
(keymap-global-set "C-c f s" #'find-scratch-buffer)
(keymap-global-set "C-c h s" #'toggle-hscroll-mode)
;;;; Post-startup
(defun my-other-keybindings ()
(keymap-global-set "C-c D T" #'cancel-debug-on-entry)
(keymap-global-set "C-c D V" #'cancel-debug-on-variable-change)
(keymap-global-set "C-c D e" #'toggle-debug-on-error)
(keymap-global-set "C-c D p p" #'profiler-toggle)
(keymap-global-set "C-c D p r" #'profiler-report)
(keymap-global-set "C-c D q" #'toggle-debug-on-quit)
(keymap-global-set "C-c D t" #'debug-on-entry)
(keymap-global-set "C-c D v" #'debug-on-variable-change)
(keymap-global-set "C-c d l" #'dictionary-search)
(keymap-global-set "C-c h l" #'hl-line-mode)
(keymap-global-set "C-c l d" #'duplicate-line)
(keymap-global-set "C-c v f" #'visual-line-fill-column-mode)
(keymap-global-set "C-c w '" #'insert-pair)
(keymap-global-set "C-c w <" #'insert-pair)
(keymap-global-set "C-c w [" #'insert-pair)
(keymap-global-set "C-c w \"" #'insert-pair)
(keymap-global-set "C-h C-k" #'describe-keymap)
(keymap-global-set "C-h u f" #'find-library)
(keymap-global-set "C-x B" #'bury-buffer)
(keymap-global-set "C-x C-M-c" #'save-buffers-kill-emacs)
(keymap-global-set "C-x C-m" (key-binding (kbd "M-x")))
(keymap-global-set "C-x D" (lambda () (interactive) (dired "~")))
(keymap-global-set "C-x K" #'kill-current-buffer)
(keymap-global-set "C-x a /" #'unexpand-abbrev)
(keymap-global-set "C-x M-x" #'switch-to-minibuffer)
(keymap-global-set "C-z C-s" #'eshell-toggle)
(keymap-global-set "C-z C-z" #'my-org-capture-inbox)
(keymap-global-set "C-S-z" #'org-capture)
(keymap-global-set "M-SPC" #'cycle-spacing)
(keymap-global-unset "C-h C-c")
(keymap-global-unset "C-h C-f")
(keymap-global-set "C-c j" (lambda ()
(interactive)
(browse-url (concat "https://JIRA.atlassian.net/browse/" (symbol-name (symbol-at-point)))))))
(add-hook 'emacs-startup-hook #'my-other-keybindings)
(defun message-init-time ()
"Report Emacs init time."
(message "Emacs init time: %s with %d GCs" (emacs-init-time) gcs-done))
(add-hook 'emacs-startup-hook #'message-init-time)
;; (add-hook 'after-init-hook #'server-start)
;;;; Package loads
(require 'mode-local)
(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))
;;; 00 straight
(setq straight-host-usernames '((github . "nicolaisingh"))
straight-profiles `((nil . ,(expand-file-name "lisp/straight/versions/default.el"
user-emacs-directory)))
straight-vc-git-default-clone-depth 'full)
(load (expand-file-name "packages.el" user-emacs-directory))
(keymap-global-set "C-h u r" #'straight-get-recipe)
(keymap-global-set "C-h u w" #'straight-visit-package-website)
;;; 01 exec-path-from-shell
(when (memq window-system '(mac ns))
(exec-path-from-shell-initialize))
;;; 02 early-init
(when (< emacs-major-version 27)
;; Manually load early-init.el for older versions of Emacs
(let ((early-init-file (expand-file-name "early-init.el" user-emacs-directory)))
(when (file-exists-p early-init-file)
(load early-init-file))))
;;; adoc-mode / asciidoc
(require 'adoc-mode)
(setq adoc-default-title-type 1
adoc-default-title-sub-type 1)
(defun my-adoc-insert-title-2 ()
"Insert a new title line."
(interactive)
(let ((title-line-p (save-excursion
(beginning-of-line)
(re-search-forward "^=+ " (pos-eol) t))))
(cond
(title-line-p
(forward-line 1)
(open-line 1))
(t (forward-paragraph 1)
(insert "\n")))
(tempo-template-adoc-title-2)))
(defun my-adoc-promote ()
"Increase a header's level."
(interactive)
(save-excursion
(beginning-of-line)
(if (not (re-search-forward "^=+ " (pos-eol) t))
(message "Not a title")
(beginning-of-line)
(insert "="))))
(defun my-adoc-demote ()
"Decrease a header's level."
(interactive)
(save-excursion
(beginning-of-line)
(if (not (re-search-forward "^==+ " (pos-eol) t))
(message "Cannote demote any further")
(beginning-of-line)
(delete-char 1))))
(keymap-unset adoc-mode-map "C-c C-t")
(keymap-set adoc-mode-map "C-c C-b" #'tempo-template-adoc-bold)
(keymap-set adoc-mode-map "C-c C-d" #'my-adoc-demote)
(keymap-set adoc-mode-map "C-c C-p" #'my-adoc-promote)
(keymap-set adoc-mode-map "C-c C-t" #'my-adoc-insert-title-2)
;;; aggressive-indent
(require 'aggressive-indent)
(add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
(add-hook 'clojure-mode-hook #'aggressive-indent-mode)
(add-hook 'clojurescript-mode-hook #'aggressive-indent-mode)
(add-hook 'scheme-mode-hook #'aggressive-indent-mode)
(add-hook 'js2-mode-hook #'aggressive-indent-mode)
(keymap-global-set "C-c i A" #'aggressive-indent-mode)
;; Don't aggressively indent comment lines in the edebug eval list buffer
(add-hook 'edebug-eval-mode-hook
(lambda () (interactive) (aggressive-indent-mode -1)))
;;; alert
(require 'alert)
(setq alert-fade-time 10)
(alert-define-style 'org-alert-email
:title "org-alert email"
:notifier
(lambda (info)
(save-window-excursion
(let ((tag (concat "["
(plist-get info :title)
"] "))
(body (plist-get info :message))
(title-body-length 40)
(message-interactive nil))
(message-mail "[email protected]"
(concat tag (if (length> body title-body-length)
(concat (substring body 0 title-body-length) "...")
body)))
(message-goto-body)
(insert (format "[From %s]\n"
(file-name-base (buffer-name (plist-get info :buffer)))))
(insert body)
(insert (format "\n\n-----\nAlert time: %s"
(format-time-string "%Y-%m-%d %H:%M:%S")))
(message-send-and-exit)))))
(alert-define-style 'legacy-log
:title "Log to *Alerts* buffer (legacy)"
:notifier
(lambda (info)
(let* ((mes (plist-get info :message))
(sev (plist-get info :severity))
(len (length mes)))
(alert-legacy-log-notify mes sev len))))
;;; avy
(require 'avy)
(setq avy-timeout-seconds 0.4
avy-keys '(?a ?o ?e ?u ?i ?d ?h ?t ?n ?s))
(keymap-global-set "C-z C-v" #'avy-goto-char-timer)
;;; browse-kill-ring
(require 'browse-kill-ring)
(keymap-global-set "C-c b k" #'browse-kill-ring)
;;; bs
(skipped
(require 'bs)
(defun bs-must-show-modes (buf)
"Return non-nil for buffers matching specific modes. Used for
the configuration 'files-plus-some-buffers-and-modes."
(let ((major-mode (buffer-local-value 'major-mode buf)))
(memq major-mode '(term-mode
shell-mode
eshell-mode
fundamental-mode))))
(defun bs-dont-show-modes (buf)
"Return non-nil for buffers which should not be shown."
(or (bs-visits-non-file buf)
(let ((major-mode (buffer-local-value 'major-mode buf)))
(memq major-mode '(dired-mode)))))
(defun bs-not-dired-mode-p (buf)
"Return non-nil for dired buffers."
(not (let ((major-mode (buffer-local-value 'major-mode buf)))
(memq major-mode '(dired-mode)))))
;; Show only files, some buffers and modes
(add-to-list 'bs-configurations '("default--files-plus-some-buffers-and-modes"
"^\\(\\*scratch\\*\\|test\\)$"
bs-must-show-modes
"^\\(\\*Ilist\\*\\|\\*Messages\\*\\)$"
bs-dont-show-modes
bs-sort-buffer-interns-are-last))
;; Show only dired buffers
(add-to-list 'bs-configurations '("dired-only"
nil
nil
nil
bs-not-dired-mode-p
nil))
;; Custom size column based on file size, not buffer size
(defun bs--get-filesize-string (_start-buffer _all-buffers)
"Return file size of the current buffer for Buffer Selection Menu."
(let* ((attributes (file-attributes (buffer-name)))
(filesize (if attributes (file-attribute-size attributes) 0))
(a-megabyte (* 1024 1024)))
(if (>= filesize a-megabyte)
(concat (format "%.01f" (/ (float filesize) a-megabyte)) "M")
(concat (format "%.01f" (/ (float filesize) 1024)) "K"))))
(defun bs--sort-by-filesize (b1 b2)
(let* ((b1-attributes (file-attributes (buffer-file-name b1)))
(b2-attributes (file-attributes (buffer-file-name b2)))
(b1-filesize (if b1-attributes (file-attribute-size b1-attributes) 0))
(b2-filesize (if b2-attributes (file-attribute-size b2-attributes) 0)))
(< b1-filesize b2-filesize)))
(defun bs-set-configuration-and-refresh (config-name)
(bs-set-configuration config-name)
(bs--redisplay t)
(bs-message-without-log config-name))
(defun my-bs-config ()
(keymap-set bs-mode-map "0" (lambda ()
(interactive)
(apply #'bs-set-configuration-and-refresh
'("default--files-plus-some-buffers-and-modes"))))
(keymap-set bs-mode-map "1" (lambda ()
(interactive)
(apply #'bs-set-configuration-and-refresh
'("dired-only"))))
(keymap-set bs-mode-map "2" (lambda ()
(interactive)
(apply #'bs-set-configuration-and-refresh
'("all"))))
(keymap-set bs-mode-map "/" #'isearch-forward)
(hl-line-mode)
(set (make-local-variable 'scroll-conservatively) 101))
(add-hook 'bs-mode-hook #'my-bs-config)
(setq bs-default-configuration "default--files-plus-some-buffers-and-modes"
bs-max-window-height 20
bs-minimal-buffer-name-column 20
bs-attributes-list '(("" 1 1 left bs--get-marked-string)
("M" 1 1 left bs--get-modified-string)
("R" 2 2 left bs--get-readonly-string)
("Buffer" bs--get-name-length 10 left bs--get-name)
("" 1 1 left " ")
;; ("Size" 8 8 right bs--get-size-string)
;; ("" 1 1 left " ")
("Size" 8 8 right bs--get-filesize-string)
("" 1 1 left " ")
("Mode" 12 12 right bs--get-mode-name)
("" 2 2 left " ")
("File" 12 12 left bs--get-file-name)
("" 2 2 left " "))
bs-sort-functions '(("by name" bs--sort-by-name "Buffer" region)
;; ("by size" bs--sort-by-size "Size" region)
("by filesize" bs--sort-by-filesize "Size" region)
("by mode" bs--sort-by-mode "Mode" region)
("by filename" bs--sort-by-filename "File" region)
("by nothing" nil nil nil)))
(keymap-global-set "C-x C-b" #'bs-show))
;;; calendar
(require 'calendar)
(require 'year-calendar)
(add-hook 'calendar-today-visible-hook #'calendar-mark-today)
(defun define-my-calendar-mark-org-headings-fn (buffer-name)
(eval
`(defun ,(intern (format "my-calendar-mark-headings-%s" buffer-name)) ()
,(format "Mark all dates that can be found at the end of each heading in %s." buffer-name)
(let ((dates '())
(bufname ,buffer-name))
(save-excursion
(set-buffer bufname)
(goto-char (point-min))
(while (re-search-forward (concat "^\\*+ .*" org-ts-regexp) nil t)
(let ((context (org-element-context)))
(when (eq (car context) 'timestamp)
(let* ((year (plist-get (cadr context) :year-start))
(month (plist-get (cadr context) :month-start))
(day (plist-get (cadr context) :day-start))
(date `(,month ,day ,year)))
(push date dates))))))
(dolist (date dates)
(when (calendar-date-is-visible-p date)
(calendar-mark-visible-date date)))))))
(defun my-calendar-mark-org-headings ()
(interactive)
(let ((bufname (buffer-name)))
(if (memq (intern (concat "my-calendar-mark-headings-" bufname))
calendar-today-visible-hook)
(progn
(remove-hook 'calendar-today-visible-hook (define-my-calendar-mark-org-headings-fn bufname))
(remove-hook 'calendar-today-invisible-hook (define-my-calendar-mark-org-headings-fn bufname))
(message "Will not mark headings for %s" bufname))
(add-hook 'calendar-today-visible-hook (define-my-calendar-mark-org-headings-fn bufname))
(add-hook 'calendar-today-invisible-hook (define-my-calendar-mark-org-headings-fn bufname))
(message "Will mark headings for %s" bufname)
(calendar))))
;;; calibre
(require 'calibre)
(setq calibre-libraries '(("library" . "~/calibre")))
(keymap-global-set "C-c L" #'calibre-library)
;;; cape
(require 'cape)
(keymap-global-set "C-c p &" #'cape-sgml)
(keymap-global-set "C-c p :" #'cape-emoji)
(keymap-global-set "C-c p \\" #'cape-tex)
(keymap-global-set "C-c p a" #'cape-abbrev)
(keymap-global-set "C-c p d" #'cape-dabbrev)
(keymap-global-set "C-c p e" #'cape-elisp-block)
(keymap-global-set "C-c p f" #'cape-file)
(keymap-global-set "C-c p h" #'cape-history)
(keymap-global-set "C-c p k" #'cape-keyword)
(keymap-global-set "C-c p l" #'cape-line)
(keymap-global-set "C-c p r" #'cape-rfc1345)
(keymap-global-set "C-c p s" #'cape-elisp-symbol)
(keymap-global-set "C-c p w" #'cape-dict)
;; (keymap-global-set "C-c p ^" #'cape-tex)
;; (keymap-global-set "C-c p _" #'cape-tex)
;; (keymap-global-set "C-c p p" #'completion-at-point) ; capf
;; (keymap-global-set "C-c p t" #'complete-tag) ; etags
(add-to-list 'completion-at-point-functions #'cape-dabbrev)
(add-to-list 'completion-at-point-functions #'cape-file)
(add-to-list 'completion-at-point-functions #'cape-line)
(add-to-list 'completion-at-point-functions #'cape-elisp-block)
;;; chatgpt-shell
(require 'chatgpt-shell)
(add-to-list 'chatgpt-shell-system-prompts
`("Concise" . ,(string-join '("I need you to reply as concise and direct to the point as possible."
"If including source code, please format them in org-mode."
"Don't wrap responses in markdown.") " ")) t)
(setq chatgpt-shell-openai-key (lambda ()
(auth-source-pick-first-password :host "api.openai.com"))
chatgpt-shell-prompt-query-response-style 'shell
chatgpt-shell-welcome-function nil
;; Set default system prompt
chatgpt-shell-system-prompt (seq-position (map-keys chatgpt-shell-system-prompts) "Concise"))
(defun chatgpt-shell-rephrase-sentences ()
(interactive)
(chatgpt-shell-send-region-with-header
(string-join
'("I need you to improve and rephrase sentences without sounding too formal."
"Give me only 3 improvements for each sentence, nothing else.") " ")))
(defun chatgpt-shell-show-prompt ()
(interactive)
(let ((message-log-max nil))
(if (not chatgpt-shell-system-prompt)
(message "No prompt selected.")
(message "%s" (cdr (nth chatgpt-shell-system-prompt
chatgpt-shell-system-prompts))))))
(defun chatgpt-shell-inline ()
(interactive)
(let ((chatgpt-shell-prompt-query-response-style 'inline))
(call-interactively #'chatgpt-shell-prompt)))
(defun chatgpt-shell-other-buffer ()
(interactive)
(let ((chatgpt-shell-prompt-query-response-style 'other-buffer))
(call-interactively #'chatgpt-shell-prompt)))
(keymap-global-set "C-c l C-SPC" #'chatgpt-shell-send-and-review-region)
(keymap-global-set "C-c l SPC" #'chatgpt-shell-send-region)
(keymap-global-set "C-c l l" #'chatgpt-shell)
(keymap-global-set "C-c l m" #'chatgpt-shell-inline)
(keymap-global-set "C-c l o" #'chatgpt-shell-other-buffer)
(keymap-global-set "C-c l p" #'chatgpt-shell-proofread-region)
(keymap-global-set "C-c l r" #'chatgpt-shell-refactor-code)
(keymap-global-set "C-c l s" #'chatgpt-shell-rephrase-sentences)
(keymap-global-set "C-c l u" #'chatgpt-shell-generate-unit-test)
(keymap-global-set "C-c l x" #'chatgpt-shell-explain-code)
(keymap-set chatgpt-shell-mode-map "C-c C-S-p" #'chatgpt-shell-load-awesome-prompts)
(keymap-set chatgpt-shell-mode-map "C-c C-S-s" #'chatgpt-shell-show-prompt)
;;; chronos
(require 'chronos)
(defun chronos-osascript-notify (c)
"(For Mac OS) Notify expiration of timer C using osascript."
(let ((shell-command-string (concat "osascript -e "
"'"
"display notification "
"\"" (chronos--time-string c) ": " (chronos--message c) "\""
"with title "
"\"Emacs\""
"'")))
(shell-command shell-command-string)))
(defun chronos-alert (c)
"(For Mac OS) Notify expiration of timer C using alert."
(alert (chronos--message c)
:title (concat (chronos--time-string c) ": Timer expired")
:style 'osx-notifier))
(setq chronos-standard-timers '("0:0:30/30-second finished"
"5/5-minute timer finished"
"15/15-minute timer finished"
"30/30-minutes timer finished"
"01:00/1-hour timer finished")
chronos-expiry-functions '(chronos-message-notify
;; For macOS: Use chronos-alert instead of chronos-desktop-notifications-notify
chronos-desktop-notifications-notify))
(defun chronos-load ()
"Load chronos."
(interactive)
(if (get-buffer chronos-buffer-name)
(switch-to-buffer chronos-buffer-name)
(chronos-initialize)))
(keymap-global-set "C-c T" #'chronos-load)
;;; command-log-mode
;; Don't bind automatically after loading module
(setq command-log-mode-key-binding-open-log nil)
(require 'command-log-mode)
(setq command-log-mode-window-font-size 0
command-log-mode-window-size 60
command-log-mode-is-global t
command-log-mode-open-log-turns-on-mode t)
;; The original function definition hard-coded the text scale,
;; ignoring `command-log-mode-window-font-size'.
(defun clm/open-command-log-buffer (&optional arg)
"Open (and create, if non-existant) a buffer used for logging keyboard commands.
If ARG is Non-nil, the existing command log buffer is cleared."
(interactive "P")
(with-current-buffer
(setq clm/command-log-buffer
(get-buffer-create " *command-log*"))
(text-scale-set command-log-mode-window-font-size))
(when arg
(with-current-buffer clm/command-log-buffer
(erase-buffer)))
(let ((new-win (split-window-horizontally
(- 0 command-log-mode-window-size))))