forked from karthink/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
3662 lines (3299 loc) · 140 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
;; -*- lexical-binding: t -*-
;; #+options: prop:t
;; #+begin_quote
;; my dot emacs grows
;;
;; one day i look inside it
;;
;; singularity
;; #+end_quote
;; * PACKAGE MANAGEMENT
;; ** STRAIGHT!
(defvar bootstrap-version)
;; cache directory
(defvar user-repos-directory "~/.local/share/git/"
"Location where cloned repos are stored.")
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(unless (file-directory-p user-repos-directory)
(make-directory user-repos-directory t))
(setq straight-base-dir user-repos-directory)
(setq straight-check-for-modifications '(find-when-checking
check-on-save)
straight-vc-git-default-clone-depth 2)
(load bootstrap-file nil 'nomessage))
(setf (alist-get 'stable straight-profiles)
(expand-file-name "stable.el" user-emacs-directory))
;;; ** PACKAGE.EL
;;; "Activate" packages, /i.e./ autoload commands, set paths, info-nodes and so
;;; on. Set load paths for ELPA packages. This is unnecessary in Emacs 27 with an
;;; early-init.el, but I haven't checked.
;;; (package-initialize)
;;; (package-activate-all)
;;; (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
;
;;; Package repositories that are no longer used or included by default:
;;; #+begin_src emacs-lisp
;;; (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))
;;; (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
;;; #+end_src
;
;;; This is a workaround for a bug that's probably been fixed by now!
;;; #+begin_src emacs-lisp
;;; (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
;;; #+end_src
;; ** USE PACKAGE
;; =use-package= is a neat wrapper over =with-eval-after-load= and =require=, a
;; judicious combination of which helps with lazy loading code. It does a lot
;; more besides, like simplify code to add hooks, bind keys and generate
;; autoloads.
;;
;; The one thing it's not is a package manager!
;;; (unless (package-installed-p 'use-package)
;;; (package-refresh-contents)
;;; (package-install 'use-package))
(straight-use-package 'use-package)
(eval-when-compile
(eval-after-load 'advice
`(setq ad-redefinition-action 'accept))
(require 'use-package)
(setq use-package-verbose nil
use-package-compute-statistics nil
;use-package-ignore-unknown-keywords t
use-package-minimum-reported-time 0.01)
)
;;; (require 'bind-key)
;;; (use-package package
;;; :hook (package-menu-mode . hl-line-mode))
;; * PATHS
;; I avoid defining too many custom helpers, =dir-concat= is an exception. Emacs
;; 28 provides =file-name-concat=, but I'm on 27.2 some of the time.
(use-package emacs
:config
(defun dir-concat (dir file)
"join path DIR with filename FILE correctly"
(concat (file-name-as-directory dir) file))
;; Set directory
(setq default-directory
(cond ((equal (system-name) "surface")
"/cygdrive/c/Users/karth/OneDrive/Documents/")
((equal system-type 'nt)
"/cygdrive/c/Users/karth/OneDrive/Documents/")
(t "~/")))
;; Adds ~/.emacs.d to the load-path
(push (dir-concat user-emacs-directory "plugins/") load-path)
(push (dir-concat user-emacs-directory "lisp/") load-path)
(defvar user-cache-directory "~/.cache/emacs/"
"Location where files created by emacs are placed."))
;; "plugins/" contains downloaded packages or plugins I've written. "lisp/" is
;; configuration and glue code.
;; * CORE
;; Optimizations to make Emacs more responsive. These are mostly copied from
;; Doom Emacs.
(require 'setup-core)
;; Trying out [[https://gitlab.com/koral/gcmh][gcmh]] on an experimental basis.
(condition-case-unless-debug nil
(use-package gcmh
:defer 2
:straight t
;; :hook (after-init . gcmh-mode)
:config
(setq gcmh-idle-delay 'auto ; default is 15s
gcmh-high-cons-threshold (* 32 1024 1024)
gcmh-verbose nil)
(gcmh-mode 1))
(error (setq gc-cons-threshold (* 16 1024 1024))))
;; setup-core is the first of many concerns to be shunted into its own file.
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-core.org"
;; ----------------------------------------------------------------
;; * DAEMON
;;;################################################################
;; Hack: When starting a server, silently load all the "heavy" libraries and
;; goodies I use. There are more elegant approaches, such as incremental
;; deferring, but this is good enough. A regular (non-daemon) Emacs session
;; still launches in ~0.3 seconds.
(when (daemonp)
(add-hook
'after-init-hook
(defun my/load-packages-eagerly ()
(run-at-time 1 nil
(lambda ()
(let ((after-init-time (current-time)))
(when (featurep 'straight) (straight-check-all))
(dolist (lib '("org" "ob" "ox" "ol" "org-roam"
"org-capture" "org-agenda" "org-fragtog"
"org-gcal" "latex" "reftex" "cdlatex"
"consult" "helpful" "elisp-mode"
"notmuch" "elfeed" "simple"
"expand-region" "embrace"
"ace-window" "avy" "yasnippet"
"magit" "modus-themes" "diff-hl"
"dired" "ibuffer" "pdf-tools"
"emacs-wm"))
(with-demoted-errors "Error: %S" (load-library lib)))
(when (featurep 'pdf-tools) (pdf-tools-install t))
(let ((elapsed (float-time (time-subtract (current-time)
after-init-time))))
(message "[Pre-loaded packages in %.3fs]" elapsed))))))))
;;;################################################################
;; * PERSONAL INFO
;;;################################################################
;; Store personal information in a GPG encrypted file. This keeps the config
;; user agnostic. Starting Emacs requires both this file and my GPG keys to be
;; present. If you want to use this configuration wholesale (a terrible idea),
;; you'll need to set the my-* variables appropriately.
(let* ((personal-file (concat user-emacs-directory "lisp/personal.el.gpg"))
(personal-file-bc (concat personal-file ".elc")))
(unless (file-exists-p personal-file-bc)
(epa-file-enable)
(byte-compile-file personal-file)))
(with-demoted-errors "Error (personal info): %S" (load-library "personal.el.gpg"))
(setq user-full-name my-full-name)
(setq user-mail-address my-email-address)
(use-package org
:straight `(org
:fork (:host nil
:repo "https://git.tecosaur.net/tec/org-mode.git"
:branch "dev"
:remote "tecosaur")
:files (:defaults "etc")
:build t
:pre-build
(with-temp-file "org-version.el"
(require 'lisp-mnt)
(let ((version
(with-temp-buffer
(insert-file-contents "lisp/org.el")
(lm-header "version")))
(git-version
(string-trim
(with-temp-buffer
(call-process "git" nil t nil "rev-parse" "--short" "HEAD")
(buffer-string)))))
(insert
(format "(defun org-release () \"The release version of Org.\" %S)\n" version)
(format "(defun org-git-version () \"The truncate git commit hash of Org mode.\" %S)\n" git-version)
"(provide 'org-version)\n")))
:pin nil))
;;;################################################################
;; * MODELINE
;;;################################################################
(load (expand-file-name "lisp/setup-modeline" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-modeline.org" :minlevel 2
;; ----------------------------------------------------------------
;;;################################################################
;; * MINIBUFFER
;;;################################################################
(use-package minibuffer
:config
(load (expand-file-name "lisp/setup-minibuffer" user-emacs-directory)))
;; ---------------------------
;; #+INCLUDE: "./lisp/setup-minibuffer.org" :minlevel 2
;; ---------------------------
;;;################################################################
;; * UI
;;;################################################################
;; Miscellaneous UI preferences.
(load (expand-file-name "lisp/setup-ui" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-ui.org"
;; ----------------------------------------------------------------
;; * AUTOLOADS
;; A relic of a past era when I would generate autoloads manually.
;; #+begin_src emacs-lisp
;; (require 'setup-autoloads nil t)
;; (require 'plugin-autoloads nil t)
;; #+end_src
;;;################################################################
;; * CUSTOM FILE
;;;################################################################
;; Don't populate the init file with custom-set-variables, create and use a
;; separate file instead.
(use-package cus-edit
:config
;; Get custom-set-variables out of init.el
(defvar my/custom-file (dir-concat user-emacs-directory "custom.el"))
(setq custom-file my/custom-file)
(defun my/cus-edit ()
(let ((file my/custom-file))
(unless (file-exists-p file)
(make-empty-file file))
(load-file file)))
:hook (after-init . my/cus-edit))
;;;################################################################
;; * KEYBIND SETUP
;;;################################################################
;; The first of several packages that I no longer use but are too entangled with
;; everything else to remove safely. So it stays in.
;;
;; These are mostly leader based keybindings that make sense to use with
;; evil-mode... which I don't use.
(load (expand-file-name "lisp/setup-keybinds" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-keybinds.org" :minlevel 2
;; ----------------------------------------------------------------
;;;################################################################
;; * SAVE AND BACKUP
;;;################################################################
;; Put backups elsewhere:
(setq auto-save-interval 2400)
(setq auto-save-timeout 300)
(setq auto-save-list-file-prefix
(dir-concat user-cache-directory "auto-save-list/.saves-"))
(setq backup-directory-alist
`(("." . ,(dir-concat user-cache-directory "backup")))
backup-by-copying t ; Use copies
version-control t ; Use version numbers on backups
delete-old-versions t ; Automatically delete excess backups
kept-new-versions 10 ; Newest versions to keep
kept-old-versions 5 ; Old versions to keep
)
;;;################################################################
;; * MISCELLANEOUS PREFERENCES
;;;################################################################
;; For lazy typists
(setq use-short-answers t)
;; Move the mouse away if the cursor gets close
;; (mouse-avoidance-mode 'animate)
;; highlight the current line, as in Matlab
;; (global-hl-line-mode)
;; Confirm when killing Emacs
(setq confirm-kill-emacs (lambda (prompt)
(y-or-n-p-with-timeout prompt 2 nil)))
(use-package uniquify
:init (setq uniquify-buffer-name-style 'forward))
(use-package paren
:defer 2
:config
(show-paren-mode 1)
(setq show-paren-delay 0.1
show-paren-highlight-openparen t
show-paren-when-point-inside-paren t))
;; Underline looks a bit better when drawn lower
(setq x-underline-at-descent-line t)
;; FULLSCREEN
(global-set-key [f11] 'toggle-frame-fullscreen)
;; Frame title
(setq frame-title-format
'(""
(:eval
(if (and (boundp 'org-roam-directory)
(string-match-p org-roam-directory (or buffer-file-name "")))
(replace-regexp-in-string
".*/[0-9]*-?" "roam:"
(subst-char-in-string ?_ ? buffer-file-name))
"%b"))))
;; Byte-compile elisp files immediately after saving them if .elc exists:
(defun auto-byte-recompile ()
"If the current buffer is in `emacs-lisp-mode' and there
already exists an `.elc' file corresponding to the current
buffer file, then recompile the file."
(interactive)
(when (and (eq major-mode 'emacs-lisp-mode)
(not ;; (string= user-init-file (buffer-file-name))
(string-match-p "init\\.el$" (buffer-file-name)))
(file-exists-p (byte-compile-dest-file buffer-file-name)))
(byte-recompile-file buffer-file-name)))
(add-hook 'after-save-hook 'auto-byte-recompile)
(add-hook 'kill-emacs-hook (lambda () (byte-recompile-file user-init-file)))
(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
(global-prettify-symbols-mode 1)
;;;######################################################################
;; * INTERFACING WITH THE OS
;;;######################################################################
(if IS-WINDOWS
(setq shell-file-name "C:/cygwin/cygwin.bat"))
(use-package auth-source-pass
:init (auth-source-pass-enable))
;; Consult clipboard before primary selection
;; http://www.gnu.org/software/emacs/manual/
;; html_node/emacs/Clipboard.html
(use-package select
:config
(setq select-enable-clipboard t))
;; Howard Abrams' piper lets you mix Shell and Emacs commands through "pipes"
;; using Emacs buffers. A neat idea but I never use it.
(use-package piper
:disabled
:bind ("C-x |" . piper)
:config
(defun +piper-start (&optional arg)
"Start piper. With prefix ARG, start piper on current buffer"
(interactive "P")
(if arg (piper) (piper-user-interface))))
;; For easy sharing of files/text.
(use-package 0x0
:straight t
:commands (0x0-upload 0x0-dwim)
:bind ("C-x U" . 0x0-dwim))
;; Emacs is slow sometimes, I try to find out why.
(use-package explain-pause-mode
:straight (explain-pause-mode
:host github
:repo "lastquestion/explain-pause-mode")
:commands explain-pause-mode
:config
(setq explain-pause-alert-style 'silent))
;;;----------------------------------------------------------------
;; ** SHELL AND ESHELL PREFERENCES
;;;----------------------------------------------------------------
;; Settings for shell, eshell, comint and vterm
(load (expand-file-name "lisp/setup-shells" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-shells.org" :minlevel 2
;; ----------------------------------------------------------------
;;;######################################################################
;; * LINE NUMBERS
;;;######################################################################
(line-number-mode 1)
(defvar my/addons-enabled-modes (list 'prog-mode-hook
'conf-unix-mode-hook
'conf-windows-mode-hook
'conf-javaprop-mode-hook
'tex-mode-hook
'text-mode-hook
'message-mode-hook)
"List of modes where special features (like line numbers)
should be enabled.")
;; (dolist (mode-hook my/addons-enabled-modes)
;; (add-hook mode-hook (lambda () "Turn on line numbers for major-mode"
;; (interactive)
;; (display-line-numbers-mode))))
(setq display-line-numbers-width-start t
display-line-numbers-type 'relative)
;;;################################################################
;; * EDITING
;;;######################################################################
(use-package visual-fill-column
:straight t
:commands visual-fill-column-mode
:hook ((eww-after-render . visual-fill-column-mode)
(eww-after-render . visual-line-mode))
:config
(setq-default visual-fill-column-center-text t
visual-fill-column-width 94))
(use-package so-long
:hook (after-init . global-so-long-mode))
(use-package iedit
:straight t
:bind (("C-M-;" . iedit-mode)
("M-n" . my/iedit-1-down)
("M-p" . my/iedit-1-up))
:config
(defun my/iedit-1-down (arg)
(interactive "p")
(let ((current-prefix-arg '(1)))
(call-interactively #'iedit-mode)
(iedit-expand-down-to-occurrence)))
(defun my/iedit-1-up (arg)
(interactive "p")
(let ((current-prefix-arg '(1)))
(call-interactively #'iedit-mode)
(iedit-expand-up-to-occurrence))))
(use-package replace
:defer
:bind (:map occur-mode-map
("C-x C-q" . occur-edit-mode))
:general
(:keymaps 'occur-mode-map
:states '(normal motion)
"gc" 'next-error-follow-minor-mode
:states 'motion
"f" 'next-error-follow-minor-mode))
(require 'better-editing nil t)
(use-package emacs
:config
(setq set-mark-command-repeat-pop t)
(global-set-key (kbd "M-r") ctl-x-r-map)
(setq undo-limit (* 80 1024 1024))
:bind
(("M-z" . zap-to-char-save)
("<C-M-backspace>" . backward-kill-sexp)))
(use-package view
:general
(:keymaps 'view-mode-map
:states '(normal motion visual)
"M-SPC" 'space-menu))
(use-package easy-kill
:straight t
:bind (([remap kill-ring-save] . #'easy-kill)
([remap mark-sexp] . #'easy-mark)
:map easy-kill-base-map
("," . easy-kill-expand))
:config
(add-to-list 'easy-kill-alist '(40 sentence " "))
(add-to-list 'easy-kill-alist '(62 page "\n"))
(add-to-list 'easy-kill-alist '(104 paragraph "\n")))
(use-package goto-chg
:straight t
:bind (("M-g ;" . goto-last-change)
("M-i" . goto-last-change)
("M-g M-;" . goto-last-change)))
(use-package quail
:commands my/cdlatex-input-tex
:config
(defun my/cdlatex-input-tex ()
(interactive)
(require 'cdlatex nil t)
(let ((cim current-input-method))
(unless (equal cim "TeX")
(activate-input-method "TeX"))
(cl-letf (((symbol-function 'texmathp)
(lambda () t))
((symbol-function 'insert)
(lambda (symbol)
(setq unread-input-method-events
(nconc (quail-input-string-to-events symbol)
(list 0))))))
(cdlatex-math-symbol))
(unless (equal cim "TeX")
(run-at-time 0 nil (lambda () (activate-input-method cim)))))))
;; Visual indicator when recording macros
(use-package kmacro
:defer
:config
(defsubst my/mode-line-macro-recording ()
"Display macro being recorded."
(when (or defining-kbd-macro executing-kbd-macro)
(let ((sep (propertize " " 'face 'highlight ))
(vsep (propertize " " 'face '(:inherit variable-pitch))))
;; "●"
(propertize (concat sep "MACRO" vsep
(number-to-string kmacro-counter) vsep
"▶" sep)
'face 'highlight))))
(setq-default mode-line-format
(cl-pushnew '(:eval (my/mode-line-macro-recording))
(default-value 'mode-line-format)
:test 'equal)))
;; * MANAGE STATE
;; ** RECENTF
;; Keep track of recently opened files. Also feeds into the list of recent
;; directories used by consult-dir.
(use-package recentf
;; :defer 2
:config
(setq recentf-save-file (dir-concat user-cache-directory "recentf")
recentf-max-saved-items 200
recentf-auto-cleanup 300)
(recentf-mode 1))
;; ** SAVEHIST
;; Save history across various prompts
(use-package savehist
:defer 2
:hook (after-init . savehist-mode)
:config
(setq savehist-file (dir-concat user-cache-directory "savehist"))
(setq history-length 1000)
(setq history-delete-duplicates t)
(setq savehist-save-minibuffer-history t))
;; ** DESKTOP
;; Save and resume Emacs sessions.
(use-package desktop
:defer
;; :hook (kill-emacs . desktop-save-in-desktop-dir)
:config
;; (when (daemonp)
;; (defun my/restore-desktop (frame)
;; "Restores desktop and cancels hook after first frame opens."
;; (with-selected-frame frame
;; (desktop-save-mode 1)
;; (desktop-read)
;; (remove-hook 'after-make-frame-functions 'my/restore-desktop)))
;; (add-hook 'after-make-frame-functions 'my/restore-desktop))
(setq desktop-auto-save-timeout 300
desktop-path `(,(dir-concat user-cache-directory "desktop"))
desktop-dirname (dir-concat user-cache-directory "desktop")
desktop-base-file-name "desktop"
desktop-restore-forces-onscreen nil
desktop-globals-to-clear nil
desktop-load-locked-desktop t
desktop-missing-file-warning nil
desktop-restore-eager 20
desktop-restore-frames t
desktop-save 'ask-if-new))
;;;################################################################
;; ** UNDO HISTORY
;; The =undo-fu-session= package saves and restores the undo states of buffers
;; across Emacs sessions.
(use-package undo-fu-session
:straight t
:hook ((prog-mode conf-mode text-mode tex-mode) . undo-fu-session-mode)
:config
(setq undo-fu-session-directory
(dir-concat user-cache-directory "undo-fu-session/")))
;; * BUFFER MANAGEMENT
(load (expand-file-name "lisp/better-buffers" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/better-buffers.org" :minlevel 2
;; ----------------------------------------------------------------
;; ** IBUFFER
(load (expand-file-name "lisp/setup-ibuffer" user-emacs-directory))
;; ----------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-ibuffer.org" :minlevel 3
;; ----------------------------------------------------------------
;;;################################################################
;; * WINDOW MANAGEMENT
;;;################################################################
;;;----------------------------------------------------------------
;; ** +SHACKLE+
;;;----------------------------------------------------------------
;; Wasamasa's Shackle package simplifies Emacs' rather arcane display-buffer
;; rules so you don't have to tear your hair out understanding how to configure
;; it. Unfortunately I did, see [[*SETUP-WINDOWS][setup-windows]].
(use-package shackle
:disabled t
:init (shackle-mode))
;;;----------------------------------------------------------------
;; ** SETUP-WINDOWS
;;;----------------------------------------------------------------
;; Setup-windows defines window rules for displaying various kinds of buffers.
(use-package setup-windows
:demand t
:hook ((help-mode . visual-line-mode)
(Custom-mode . visual-line-mode)
(helpful-mode . visual-line-mode))
;; :bind (;; ("C-x +" . balance-windows-area)
;; ("<f8>" . +make-frame-floating-with-current-buffer)
;; ("C-M-`" . window-toggle-side-windows))
:bind
("<f9>" . +make-frame-floating-with-current-buffer)
;; "C-M-`" 'window-toggle-side-windows
:general
(:keymaps 'space-menu-window-map
:wk-full-keys nil
"w" '(window-toggle-side-windows :wk "toggle side windows")))
(use-package window
:bind (("H-+" . balance-windows-area)
;; ("C-x +" . balance-windows-area)
("C-x q" . my/kill-buffer-and-window))
:config
(defun my/kill-buffer-and-window ()
"Kill buffer.
Also kill this window, tab or frame if necessary."
(interactive)
(cl-letf ((symbol-function 'delete-window)
(symbol-function 'my/delete-window-or-delete-frame))
(kill-buffer-and-window))))
;; setup-windows:
;; ---------------------------------------------------------------
;; #+INCLUDE: "./lisp/setup-windows.org" :minlevel 2
;; ---------------------------------------------------------------
;;;----------------------------------------------------------------
;; ** POPUPS
;;;----------------------------------------------------------------
;; Designate buffers to popup status and toggle or cycle through them
(use-package popper
:load-path "plugins/popper/"
:after (setup-windows setup-project)
:commands popper-mode
:bind (("C-`" . popper-toggle-latest)
("C-M-`" . popper-cycle)
("H-`" . popper-toggle-latest)
("H-M-`" . popper-cycle)
("H-6" . popper-toggle-type)
("H-M-k" . popper-kill-latest-popup)
("M-`" . my/switch-to-other-buffer)
("s-n" . my/next-buffer)
("s-p" . my/previous-buffer))
:init
;; (setq popper-group-function
;; (defun my/popper-group-by-heuristic ()
;; "Group popups according to heuristic rules suitable for
;; my usage."
;; (let ((dd (abbreviate-file-name default-directory)))
;; (cond
;; ((string-match-p "\\(?:~/\\.config/\\|~/dotfiles/\\)" dd)
;; 'config)
;; ((or (string-match-p "local/share/git" dd)
;; (string-match-p "plugins/" dd))
;; 'projects)
;; ((string-match-p "\\(?:KarthikBa\\|research/\\)" dd)
;; 'research)
;; ((string-match-p "karthinks" dd) 'website)
;; ((locate-dominating-file dd "research") 'documents)
;; ((locate-dominating-file dd "init.el") 'emacs)
;; (t (popper-group-by-project))))))
(setq popper-group-function nil)
(setq ;; popper-mode-line nil
popper-reference-buffers
(append my/help-modes-list
my/man-modes-list
my/repl-modes-list
my/repl-names-list
my/occur-grep-modes-list
;; my/man-modes-list
'(Custom-mode
(compilation-mode . hide)
messages-buffer-mode)
'(("^\\*Warnings\\*$" . hide)
("^\\*Compile-Log\\*$" . hide)
"^\\*Matlab Help.*\\*$"
;; "^\\*Messages\\*$"
"^\\*Backtrace\\*"
"^\\*evil-registers\\*"
"^\\*Apropos"
"^Calc:"
"^\\*eldoc\\*"
"^\\*TeX errors\\*"
"^\\*ielm\\*"
"^\\*TeX Help\\*"
"^\\*ChatGPT\\*$"
"\\*Shell Command Output\\*"
("\\*Async Shell Command\\*" . hide)
"\\*Completions\\*"
;; "\\*scratch.*\\*$"
"[Oo]utput\\*")))
(advice-add 'popper-cycle :after
(defun my/popper-cycle-repeated (&rest _)
"Continue to cycle popups with the grave key."
(set-transient-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "`") #'popper-cycle)
map))))
(setq popper-display-function
(defun my/popper-select-below (buffer &optional _alist)
(funcall (if (> (frame-width) 170)
;; #'display-buffer-in-direction
#'popper-select-popup-at-bottom
#'display-buffer-at-bottom)
buffer
`((window-height . ,popper-window-height)
(direction . below)
(body-function . ,#'select-window)))))
(use-package embark
:defer
:bind (:map embark-buffer-map
("_" . embark-popper-toggle))
:config
(defun embark-popper-toggle (buf)
"Toggle popup status."
(popper-toggle-type buf)))
(use-package popper-echo
:defer 3
:config
(defun popper-message-shorten (name)
(cond
((string-match "^\\*[hH]elpful.*?: \\(.*\\)\\*$" name)
(concat (match-string 1 name)
"(H)"))
((string-match "^\\*Help:?\\(.*\\)\\*$" name)
(concat (match-string 1 name)
"(H)"))
((string-match "^\\*eshell:? ?\\(.*\\)\\*$" name)
(concat (match-string 1 name)
(if (string-empty-p (match-string 1 name)) "shell(E)" "(E)")))
((string-match "^\\*\\(.*?\\)\\(?:Output\\|Command\\)\\*$" name)
(concat (match-string 1 name)
"(O)"))
((string-match "^\\*\\(.*?\\)[ -][Ll]og\\*$" name)
(concat (match-string 1 name)
"(L)"))
((string-match "^\\*[Cc]ompil\\(?:e\\|ation\\)\\(.*\\)\\*$" name)
(concat (match-string 1 name)
"(C)"))
(t name)))
(setq popper-echo-transform-function #'popper-message-shorten)
(setq popper-echo-dispatch-keys '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)
popper-echo-dispatch-actions t)
(advice-add 'popper-echo :around
(defun my/popper-echo-no-which-key (orig-fn)
(let ((which-key-show-transient-maps nil))
(funcall orig-fn))))
(popper-echo-mode +1))
:config
(setq popper-display-control 'user)
(popper-mode 1)
(defun my/switch-to-other-buffer (&optional _arg)
(interactive)
(switch-to-buffer (other-buffer)))
(defun my/next-buffer (&optional arg)
"Switch to the next non-popup buffer."
(interactive "P")
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win
(my/next-buffer)
(setq prefix-arg current-prefix-arg))
(dotimes (or (abs (prefix-numeric-value arg)) 1)
(my/switch-buffer-1 #'next-buffer))))
(defun my/previous-buffer (&optional arg)
"Switch to the previous non-popup buffer."
(interactive "P")
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win
(my/previous-buffer)
(setq prefix-arg current-prefix-arg))
(dotimes (or (abs (prefix-numeric-value arg)) 1)
(my/switch-buffer-1 #'previous-buffer))))
(defun my/switch-buffer-1 (switch)
"Switch to the next user buffer in cyclic order.
User buffers are those not starting with *."
(funcall switch)
(let ((i 0))
(while (and (< i 50)
(member (buffer-local-value 'popper-popup-status (current-buffer))
'(popup user-popup)))
(setq i (1+ i)) (funcall switch))))
(define-key global-map (kbd "C-x C-p") #'my/previous-buffer)
(define-key global-map (kbd "C-x C-n") #'my/next-buffer)
(define-key global-map (kbd "C-x n g") #'set-goal-column)
(defvar my/buffer-cycle-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "n") #'my/next-buffer)
(define-key map (kbd "p") #'my/previous-buffer)
(define-key map (kbd "b")
(defun my/switch-buffer () (interactive)
"Switch to consult-buffer"
(run-at-time 0 nil
(lambda (&optional arg)
(interactive "P")
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win (consult-buffer))
(consult-buffer)))
current-prefix-arg)))
map))
(map-keymap
(lambda (_ cmd) (put cmd 'repeat-map 'my/buffer-cycle-map))
my/buffer-cycle-map)
:general
(:states 'motion
"C-w ^" '(popper-raise-popup :wk "raise popup")
"C-w _" '(popper-lower-to-popup :wk "lower to popup"))
(:keymaps 'space-menu-window-map
"^" '(my/popup-raise-popup :wk "raise popup")
"_" '(my/popup-lower-to-popup :wk "lower to popup")))
;;----------------------------------------------------------------
;; ** WINUM
;;----------------------------------------------------------------
;; Add window numbers and use them to switch windows
(use-package winum
:straight t
:init
(defun my/winum-select (num)
(lambda (&optional arg) (interactive "P")
(if arg
(winum-select-window-by-number (- 0 num))
(if (equal num (winum-get-number))
(winum-select-window-by-number (winum-get-number (get-mru-window t)))
(winum-select-window-by-number num)))))
(setq winum-keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-M-0") 'winum-select-window-0-or-10)
(dolist (num '(1 2 3 4 5 6 7 8 9) nil)
(define-key map (kbd (concat "C-M-" (int-to-string num)))
(my/winum-select num)))
map))
;; If evil-mode is enabled further mode-line customization is needed before
;; enabling winum:
(unless (bound-and-true-p evil-mode)
(winum-mode 1)))
;;----------------------------------------------------------------
;; ** +WINNER+
;;----------------------------------------------------------------
;; Winner mode is disabled in favor of =tab-bar-history-mode=, which does the
;; same but with a separate window configuration history for each tab. This is
;; usually what I want.
(use-package winner
:disabled
:commands winner-undo
:bind (("C-c <left>" . winner-undo)
("C-x C-/" . winner-undo)
("H-/" . winner-undo)
("s-u" . winner-undo))
:general
(:keymaps 'space-menu-window-map
:wk-full-keys nil
"u" 'winner-undo
"r" 'winner-redo)
:config
(winner-mode +1))
;;----------------------------------------------------------------
;; ** Ace-window
;;----------------------------------------------------------------
(use-package ace-window
:straight t
:bind
(("C-x o" . ace-window)
("H-o" . ace-window)
("M-o" . other-window)
("M-O" . my/other-window-prev))
:general
(:keymaps 'space-menu-map
"`" 'ace-window)
;; :custom-face
;; (aw-leading-char-face ((t (:height 2.5 :weight normal))))
:config
(setq aw-dispatch-always t
aw-scope 'global
aw-background nil
aw-keys '(?q ?w ?e ?r ?t ?y ?u ?i ?p))
(setq aw-dispatch-alist
'((?k aw-delete-window "Delete Window")
(?m aw-swap-window "Swap Windows")
(?M aw-move-window "Move Window")
(?c aw-copy-window "Copy Window")
(?j aw-switch-buffer-in-window "Select Buffer")
(?\t aw-flip-window)
(?b aw-switch-buffer-other-window "Switch Buffer Other Window")
(?c aw-split-window-fair "Split Fair Window")
(?s aw-split-window-vert "Split Vert Window")
(?v aw-split-window-horz "Split Horz Window")
(?o delete-other-windows "Delete Other Windows")
(?? aw-show-dispatch-help)))
(defun my/other-window-prev (&optional arg all-frames)
(interactive "p")
(other-window (if arg (- arg) -1) all-frames)))
(use-package emacs
:config
(defun my/enlarge-window-horizontally (&optional repeat)
"Enlarge window horizontally by 8% of the frame width."
(interactive "p")
(enlarge-window-horizontally (* (or repeat 1)
(floor (frame-width) 20))))
(defun my/shrink-window-horizontally (&optional repeat)
"Enlarge window horizontally by 8% of the frame width."
(interactive "p")
(shrink-window-horizontally (* (or repeat 1)
(floor (frame-width) 20))))
(defun my/shrink-window (&optional repeat)
"Enlarge window horizontally by 8% of the frame height."
(interactive "p")
(shrink-window (* (or repeat 1)
(floor (frame-height) 20))))
(defun my/enlarge-window (&optional repeat)
"Enlarge window horizontally by 8% of the frame height."
(interactive "p")
(enlarge-window (* (or repeat 1)
(floor (frame-height) 20))))
:bind
(("<C-S-right>" . my/enlarge-window-horizontally)
("<C-S-left>" . my/shrink-window-horizontally)
("<C-S-up>" . my/enlarge-window)
("<C-S-down>" . my/shrink-window)))
;;----------------------------------------------------------------
;; ** Windmove
;;----------------------------------------------------------------