-
Notifications
You must be signed in to change notification settings - Fork 4
/
README.org
2651 lines (2281 loc) · 74.3 KB
/
README.org
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
#+TITLE: Emacs
#+AUTHOR: Robin Schroer
#+CATEGORY: Emacs
#+FILETAGS: side_project yak
#+STARTUP: overview
#+PROPERTY: header-args :results silent
* Use lexical binding in this file
This org-mode file gets built into an elisp file, and we want this comment at
the top for performance reasons.
#+BEGIN_SRC emacs-lisp :tangle yes
;; -*- lexical-binding: t; -*-
#+END_SRC
* Who Am I?
#+begin_src emacs-lisp :tangle yes
(setq user-full-name "Robin Schroer"
user-login-name "sulami"
user-mail-address "[email protected]"
sulami/source-directory "~/src")
#+end_src
* Better defaults
** Discard the custom file
~custom.el~ is hidden state, all config is declarative.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq custom-file (make-temp-file ""))
#+END_SRC
** Hide Backups
This way we lose everything backups if the whole machine crashes, but
we don't accidentally leave backups around.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq backup-directory-alist '(("." . "~/emacs-backup/"))
auto-save-file-name-transforms '((".*" "~/emacs-auto-save/" t))
create-lockfiles nil)
#+END_SRC
** Revert changed files
This reloads changed files which are open in Emacs but not edited.
#+begin_src emacs-lisp :tangle yes
(setq revert-without-query '(".*"))
#+end_src
** No trash
#+BEGIN_SRC emacs-lisp :tangle yes
(setq delete-by-moving-to-trash nil)
#+END_SRC
** Remember recent files
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package recentf
:straight nil
:custom
(recentf-max-saved-items 1024)
:hook
(after-init . recentf-mode))
#+END_SRC
** Be quiet on startup
#+BEGIN_SRC emacs-lisp :tangle yes
(setq inhibit-splash-screen t
inhibit-startup-screen t
inhibit-startup-message t)
#+END_SRC
** Don't tell me about keybindings
#+begin_src emacs-lisp :tangle yes
(setq suggest-key-bindings nil)
#+end_src
** Make Tramp great again
These are just fixes to make TRAMP work and be reasonably fast. Mostly
sourced from the internet, I don't pretend to know how this actually
works.
#+begin_src emacs-lisp :tangle yes
(setq tramp-default-method "ssh"
tramp-ssh-controlmaster-options
"-o ControlMaster=auto -o ControlPath='tramp.%%C'")
;; Various speedups
;; from https://www.gnu.org/software/emacs/manual/html_node/tramp/Frequently-Asked-Questions.html
(setq remote-file-name-inhibit-cache 3600
tramp-completion-reread-directory-timeout nil
vc-ignore-dir-regexp (format "%s\\|%s"
vc-ignore-dir-regexp
tramp-file-name-regexp)
tramp-verbose 0)
;; Disable the history file on remote hosts
(setq tramp-histfile-override t)
;; Save backup files locally
;; from https://stackoverflow.com/a/47021266
(add-to-list 'backup-directory-alist
(cons tramp-file-name-regexp "/tmp/emacs-backup/"))
#+end_src
** Remember where we were
#+begin_src emacs-lisp :tangle yes
(require 'saveplace)
(save-place-mode 1)
#+end_src
** Always follow symbolic links
#+begin_src emacs-lisp :tangle yes
(setq vc-follow-symlinks t)
#+end_src
** Don't ring the bell
#+begin_src emacs-lisp :tangle yes
(setq ring-bell-function 'ignore)
#+end_src
** Don't blink the cursor
I find this mostly distracting.
#+begin_src emacs-lisp :tangle yes
(blink-cursor-mode -1)
#+end_src
** Start the scratch buffer empty
#+BEGIN_SRC emacs-lisp :tangle yes
(setq initial-scratch-message "")
#+END_SRC
** Enable winner-mode
This allows me to use ~winner-undo~ if I accidentally delete a window.
#+begin_src emacs-lisp :tangle yes
(add-hook 'after-init-hook 'winner-mode)
#+end_src
** Default to Elisp
#+begin_src emacs-lisp :tangle yes
(setq initial-major-mode 'emacs-lisp-mode)
#+end_src
** Switch to the help window
#+begin_src emacs-lisp :tangle yes
(setq help-window-select t)
#+end_src
** Spaces > tabs
#+BEGIN_SRC emacs-lisp :tangle yes
(setq-default indent-tabs-mode nil)
#+END_SRC
** Tabs are 4 spaces
#+begin_src emacs-lisp :tangle yes
(setq-default tab-width 4)
#+end_src
** Sentences end with a single space
#+begin_src emacs-lisp :tangle yes
(setq sentence-end-double-space nil)
#+end_src
** Show trailing whitespace
It's disabled by default, and then gets enabled for all file-based
buffer modes, so not for REPLS and shells.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq-default show-trailing-whitespace nil)
(defun sulami/show-trailing-whitespace ()
"Just sets `show-trailing-whitespace'."
(setq show-trailing-whitespace t))
(add-hook 'prog-mode-hook 'sulami/show-trailing-whitespace)
(add-hook 'text-mode-hook 'sulami/show-trailing-whitespace)
#+END_SRC
** Show empty lines
This shows vim-style tildes on the left fringe.
#+begin_src emacs-lisp :tangle yes
(when (display-graphic-p)
(setq-default indicate-empty-lines t)
(define-fringe-bitmap 'tilde [0 0 0 113 219 142 0 0] nil nil 'center)
(setcdr (assq 'empty-line fringe-indicator-alist) 'tilde))
#+end_src
** Enable so-long-mode
This disables expensive modes when a buffer has very long lines to
prevent performance issues.
#+begin_src emacs-lisp :tangle yes
(if (version<= "27.1" emacs-version)
(global-so-long-mode 1))
#+end_src
** Highlight matching parentheses
I prefer this over using rainbow parentheses, which make it difficult
to see what's actually happening.
#+BEGIN_SRC emacs-lisp :tangle yes
(show-paren-mode 1)
#+END_SRC
** Scrolling
These settings were lifted off the internet™ and make scrolling with pointing
devices feel more reasonable.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq mouse-wheel-progressive-speed nil
mouse-wheel-scroll-amount '(1 ((shift) . 1) ((control) . nil)))
#+END_SRC
** No line wrapping
At least as a default, much nicer when resizing windows.
#+BEGIN_SRC emacs-lisp :tangle yes
(set-default 'truncate-lines t)
(setq line-move-visual nil)
#+END_SRC
** UTF-8
#+BEGIN_SRC emacs-lisp :tangle yes
(setq-default buffer-file-coding-system 'utf-8)
(setenv "LANG" "en_US.UTF-8")
(setenv "LC_ALL" "en_US.UTF-8")
(prefer-coding-system 'utf-8)
#+END_SRC
** Spelling
Use ~aspell~ with British English.
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package flyspell
:straight nil
:custom
(ispell-program-name "aspell")
(ispell-extra-args (quote ("--sug-mode=ultra" "--lang=en_GB-ise")))
(flyspell-sort-corrections nil)
(flyspell-issue-message-flag nil)
:hook
(prog-mode . flyspell-prog-mode))
#+END_SRC
** Enable erase buffer
#+begin_src emacs-lisp :tangle yes
(put 'erase-buffer 'disabled nil)
#+end_src
** Y/N for yes or no questions
#+BEGIN_SRC emacs-lisp :tangle yes
(fset 'yes-or-no-p 'y-or-n-p)
#+END_SRC
** Ask before exiting
#+BEGIN_SRC emacs-lisp :tangle yes
(setq confirm-kill-emacs 'yes-or-no-p)
#+END_SRC
** Frame title
Set the frame title to the current project name. This is useful if I
have several frames/Emacsen open and want to switch between them.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq frame-title-format
(list :eval '(let ((p-name (projectile-project-name)))
(if (string-equal p-name "-")
"Emacs"
(concat "Emacs - " p-name)))))
#+END_SRC
** Disable all the GUI
#+BEGIN_SRC emacs-lisp :tangle yes
(if (and (fboundp 'tool-bar-mode)
tool-bar-mode)
(tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tooltip-mode) (tooltip-mode -1))
#+END_SRC
** Setup SSH via GPG-Agent
This assumes GPG-Agent is already running. Otherwise start it with
~gpg-agent --daemon~.
#+begin_src emacs-lisp :tangle yes
(setenv "SSH_AUTH_SOCK" (string-trim (shell-command-to-string "gpgconf --list-dirs agent-ssh-socket")))
#+end_src
** Enable recursive minibuffers
For example to yank while entering into the minibuffer.
#+begin_src emacs-lisp :tangle yes
(setq enable-recursive-minibuffers t)
#+end_src
* macOS
Everything in here relates to macOS in some way.
** Swap the modifier keys
The MacPorts build I'm using swaps the modifiers from what I'm used to, so I'm
swapping them back.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq mac-command-modifier 'super
mac-option-modifier 'meta)
#+END_SRC
** Fix paste
Especially Alfred likes to paste with ~⌘-v~, so that needs to work.
#+BEGIN_SRC emacs-lisp :tangle yes
(define-key global-map (kbd "s-v") 'yank)
#+END_SRC
** Maximise with ⌘-Return
#+BEGIN_SRC emacs-lisp :tangle yes
(define-key global-map (kbd "<s-return>") 'toggle-frame-maximized)
#+END_SRC
** Mac font panel
#+BEGIN_SRC emacs-lisp :tangle yes
(define-key global-map (kbd "s-f") #'mac-font-panel-mode)
#+END_SRC
** Fix frame focus
The MacPorts Emacs version I'm using has the peculiar behaviour that
requires ~menu-bar-mode~ to be enabled in order to focus the current
frame when switching workspaces.
#+begin_src emacs-lisp :tangle yes
;; Use `mac-font-panel-mode' as a proxy to find out if this is the
;; MacPorts version.
(when (fboundp 'mac-font-panel-mode)
(menu-bar-mode 1))
#+end_src
** Get the macOS theme
#+begin_src emacs-lisp :tangle yes
(defun sulami/macos-dark-theme-p ()
"Return non-nil if on macOS and currently in dark theme."
(when (fboundp 'mac-application-state)
(equal "NSAppearanceNameDarkAqua"
(plist-get (mac-application-state) :appearance))))
#+end_src
* Package management
** use-package
Default =straight= to install anything ~use-package~ defines.
#+BEGIN_SRC emacs-lisp :tangle yes
(setq straight-use-package-by-default t)
#+END_SRC
** el-patch
Allows for patching functions in packages.
#+begin_src emacs-lisp :tangle yes
(use-package el-patch)
#+end_src
** Dash
List library that comes in handy.
#+begin_src emacs-lisp :tangle yes
(use-package dash)
#+end_src
** Updating all packages
#+begin_src emacs-lisp :tangle yes
(defun sulami/update-packages ()
"Prunes and updates packages, revalidates patches."
(straight-prune-build-directory)
(straight-pull-all)
(el-patch-validate-all)
(straight-freeze-versions)
(byte-recompile-directory "~/.emacs.d/straight/build" nil 'force))
#+end_src
* Appearance
** Font
Set the font to Fira Code and enable ligatures.
#+BEGIN_SRC emacs-lisp :tangle yes
(let ((font "PragmataPro Mono 14"))
(set-face-attribute 'default nil :font font)
(set-frame-font font nil t))
;; (when (boundp 'mac-auto-operator-composition-mode)
;; (mac-auto-operator-composition-mode))
#+END_SRC
** Theme
I use =doom-themes=, mostly =doom-solarized-light= & =doom-gruvbox=.
There are some fixes to prevent themes from clashing, and I also
disable most backgrounds as I find them distracting.
#+BEGIN_SRC emacs-lisp :tangle yes
;; I like to live dangerously
(setq custom-safe-themes t)
(defconst sulami/light-theme 'doom-solarized-light)
(defconst sulami/dark-theme 'doom-gruvbox)
(defun sulami/disable-all-themes ()
"Disables all custom themes."
(interactive)
(mapc #'disable-theme custom-enabled-themes))
(defun sulami/before-load-theme-advice (theme &optional no-confirm no-enable)
"Disable all themes before loading a new one.
Prevents mixing of themes, where one theme doesn't override all faces
of another theme."
(sulami/disable-all-themes))
(advice-add 'load-theme
:before
#'sulami/before-load-theme-advice)
(defun sulami/set-face-straight-underline (face)
"Remove FACE's :underline style, if it's set"
(when (display-graphic-p)
(let ((old-attr (face-attribute face :underline)))
(when (eq 'cons (type-of old-attr))
(set-face-attribute face nil :underline (nth 3 old-attr))))))
(defun sulami/after-load-theme-advice (theme &optional no-confirm no-enable)
"Unsets backgrounds for some org-mode faces.
Also changes squiggly underlines to straight ones."
(require 'flycheck)
(set-face-background 'outline-1 nil)
(set-face-background 'org-block nil)
(set-face-background 'org-block-begin-line nil)
(set-face-background 'org-block-end-line nil)
(set-face-background 'org-quote nil)
(cl-loop for face in '(flyspell-incorrect
flyspell-duplicate
flycheck-error
flycheck-warning
flycheck-info)
do (sulami/set-face-straight-underline face)))
(advice-add 'load-theme
:after
#'sulami/after-load-theme-advice)
(use-package doom-themes
:after (dash)
:init
(setq doom-themes-enable-bold t
doom-themes-enable-italic t)
:config
(doom-themes-org-config)
;; Set the default colourscheme according to the time of day
:hook
(after-init . (lambda ()
(when (display-graphic-p)
(let* ((hour-of-day (read (format-time-string "%H")))
(theme (if (or (not (sulami/macos-dark-theme-p))
(<= 8 hour-of-day 17))
sulami/light-theme
sulami/dark-theme)))
(load-theme theme t)
(sulami/after-load-theme-advice theme))))))
(use-package mac-auto-theme
:straight nil
:no-require t
:if (fboundp 'mac-application-state)
:after (doom-themes)
:hook
(mac-effective-appearance-change . (lambda ()
(when (display-graphic-p)
(load-theme
(if (sulami/macos-dark-theme-p)
sulami/dark-theme
sulami/light-theme)
t)))))
#+END_SRC
** Modeline
I use =doom-modeline=, without any icons, and patched to be regular
height.
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package doom-modeline
:hook (after-init . doom-modeline-mode)
:custom
(doom-modeline-icon nil)
(doom-modeline-height 10)
(doom-modeline-buffer-file-name-style 'relative-to-project)
(doom-modeline-buffer-encoding nil)
(doom-modeline-persp-name nil)
(doom-modeline-vcs-max-length 36)
:config/el-patch
(defun doom-modeline--font-height ()
"Calculate the actual char height of the mode-line."
(let ((height (face-attribute 'mode-line :height)))
;; WORKAROUND: Fix tall issue of 27 on Linux
;; @see https://github.com/seagle0128/doom-modeline/issues/271
(round
(* (if (and (>= emacs-major-version 27)
(not (eq system-type 'darwin)))
1.0
(if doom-modeline-icon
(el-patch-swap 1.68 1.0)
(el-patch-swap 1.25 1.0)))
(cond ((integerp height) (/ height 10))
((floatp height) (* height (frame-char-height)))
(t (frame-char-height))))))))
#+END_SRC
* Custom functions
** Config
*** Open this file
#+BEGIN_SRC emacs-lisp :tangle yes
(defun sulami/open-emacs-config ()
"Opens the config file for our favourite OS."
(interactive)
(find-file sulami/emacs-config-file))
#+END_SRC
*** Reload this file
#+BEGIN_SRC emacs-lisp :tangle yes
(defun sulami/reload-emacs-config ()
"Loads the config file for our favourite OS."
(interactive)
(org-babel-load-file sulami/emacs-config-file))
#+END_SRC
** Buffers
*** Rename buffer file
#+BEGIN_SRC emacs-lisp :tangle yes
(defun sulami/rename-file-and-buffer ()
"Rename the current buffer and file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(message "Buffer is not visiting a file!")
(let ((new-name (read-file-name "New name: " filename)))
(cond
((vc-backend filename) (vc-rename-file filename new-name))
(t
(rename-file filename new-name t)
(set-visited-file-name new-name t t)))))))
#+END_SRC
*** Switch to buffer shortcuts
#+BEGIN_SRC emacs-lisp :tangle yes
(defun sulami/open-scratch-buffer ()
"Opens the scratch buffer."
(interactive)
(switch-to-buffer "*scratch*"))
(defun sulami/open-message-buffer ()
"Opens the message buffer."
(interactive)
(switch-to-buffer "*Messages*"))
(defun sulami/open-minibuffer ()
"Focusses the minibuffer, if active."
(interactive)
(when (active-minibuffer-window)
(select-window (minibuffer-window))))
#+END_SRC
*** Buffer line count
#+BEGIN_SRC emacs-lisp :tangle yes
(defun sulami/buffer-line-count ()
"Get the number of lines in the active buffer."
(count-lines 1 (point-max)))
#+END_SRC
*** Delete buffer file
#+begin_src emacs-lisp :tangle yes
(defun sulami/delete-file-and-buffer ()
"Deletes a buffer and the file it's visiting."
(interactive)
(when-let* ((file-name (buffer-file-name))
(really (yes-or-no-p (format "Delete %s? "
file-name))))
(delete-file file-name)
(kill-buffer)))
#+end_src
*** Copy buffer
#+begin_src emacs-lisp :tangle yes
(defun sulami/copy-buffer ()
"Copies the entire buffer to the kill-ring."
(interactive)
(copy-region-as-kill 1 (point-max)))
#+end_src
*** Open the source directory
#+begin_src emacs-lisp :tangle yes
(defun sulami/open-source-dir ()
(interactive)
(find-file sulami/source-directory))
#+end_src
*** Toggle a terminal
#+begin_src emacs-lisp :tangle yes
(defun sulami/toggle-term ()
"Opens global vterm, or switches to last buffer."
(interactive)
(let ((buf-name "*vterm*"))
(cond
((eq major-mode 'vterm-mode)
(evil-switch-to-windows-last-buffer))
((get-buffer buf-name)
(switch-to-buffer buf-name))
((vterm buf-name)))))
#+end_src
** Windows
*** Maximise a window
#+begin_src emacs-lisp :tangle yes
(defun sulami/toggle-maximise-window ()
"Toggles maximising the current window.
From: https://gist.github.com/mads-hartmann/3402786"
(interactive)
(if (and (= 1 (length (window-list)))
(assoc ?_ register-alist))
(jump-to-register ?_)
(progn
(window-configuration-to-register ?_)
(delete-other-windows))))
#+end_src
** Run a shell command on a region
#+begin_src emacs-lisp :tangle yes
(defun sulami/shell-command-on-region (beg end cmd)
(interactive "r\nsCommand: ")
(shell-command-on-region beg end cmd t t))
#+end_src
** Sort words
#+begin_src emacs-lisp :tangle yes
(defun sulami/sort-words (beg end)
"Sorts words in region."
(interactive "r")
(sort-regexp-fields nil "\\w+" "\\&" beg end))
#+end_src
** Kill matching lines
#+begin_src emacs-lisp :tangle yes
(defun sulami/kill-matching-lines (regexp &optional rstart rend interactive)
"Kill lines containing matches for REGEXP.
See `flush-lines' or `keep-lines' for behavior of this command.
If the buffer is read-only, Emacs will beep and refrain from deleting
the line, but put the line in the kill ring anyway. This means that
you can use this command to copy text from a read-only buffer.
\(If the variable `kill-read-only-ok' is non-nil, then this won't
even beep.)"
(interactive
(keep-lines-read-args "Kill lines containing match for regexp"))
(let ((buffer-file-name nil)) ;; HACK for `clone-buffer'
(with-current-buffer (clone-buffer nil nil)
(let ((inhibit-read-only t))
(keep-lines regexp rstart rend interactive)
(kill-region (or rstart (line-beginning-position))
(or rend (point-max))))
(kill-buffer)))
(unless (and buffer-read-only kill-read-only-ok)
;; Delete lines or make the "Buffer is read-only" error.
(flush-lines regexp rstart rend interactive)))
#+end_src
** Toggle narrowing
#+begin_src emacs-lisp :tangle yes
(defun sulami/toggle-narrow ()
"Toggles `narrow-to-defun' or `org-narrow-to-subtree'."
(interactive)
(if (buffer-narrowed-p)
(widen)
(if (eq major-mode 'org-mode)
(org-narrow-to-subtree)
(narrow-to-defun))))
#+end_src
** Toggle line numbers
This one is faster than ~linum-mode~.
#+begin_src emacs-lisp :tangle yes
(defun sulami/toggle-line-numbers ()
"Toggles buffer line number display."
(interactive)
(setq display-line-numbers (not display-line-numbers)))
#+end_src
** Find the font face used
This one is quite useful for debugging syntax highlighting. It's
adapted from [[https://stackoverflow.com/questions/1242352/get-font-face-under-cursor-in-emacs][here]].
#+begin_src emacs-lisp :tangle yes
(defun sulami/what-face (pos)
(interactive "d")
(let ((face (or (get-char-property pos 'read-face-name)
(get-char-property pos 'face))))
(if face
(message "Face: %s" face)
(message "No face at %d" pos))))
#+end_src
** Create a random UUID
I need random UUIDs all the time. This generates one and places it in
the clipboard, ready for pasting. Heavily dependent on macOS.
#+begin_src emacs-lisp :tangle yes
(defun sulami/random-uuid ()
(interactive)
(let ((uuid (s-trim (shell-command-to-string "uuidgen | tr '[:upper:]' '[:lower:]'"))))
(kill-new uuid)
(message "Generated UUID: %s" uuid)))
#+end_src
** Copy the path & line at point
This is useful to run Rspec tests.
#+begin_src emacs-lisp :tangle yes
(defun sulami/rspec-path ()
(interactive)
(let ((fp (f-relative buffer-file-name (projectile-project-root)))
(ln (1+ (line-number-at-pos))))
(-> (format "%s:%d" fp ln)
(message)
(kill-new))))
#+end_src
** Fill/unfill paragraph
Fills the current paragraph. If used again, "unfills" it, for pasting
in places that doesn't like hard line breaks, such as GitHub. "Taken
from [[https://gist.github.com/heikkil/a3edf506046c84f6f508edbaf005810a][here]].
#+begin_src emacs-lisp :tangle yes
(defun sulami/fill-or-unfill ()
"Like `fill-paragraph', but unfill if used twice."
(interactive)
(let ((fill-column
(if (eq last-command #'sulami/fill-or-unfill)
(progn (setq this-command nil)
(point-max))
fill-column)))
(if (eq major-mode 'org-mode)
(call-interactively #' org-fill-paragraph)
(call-interactively #'fill-paragraph))))
#+end_src
* General
General allows me to use fancy prefix keybindings.
I'm using a spacemacs-inspired system of a global leader key and a local leader
key for major modes. Bindings are setup in the respective ~use-package~
declarations.
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package general
:config
(general-auto-unbind-keys)
(general-evil-setup)
(defconst leader-key "SPC")
(general-create-definer leader-def
:prefix leader-key
:keymaps 'override
:states '(normal visual))
(defconst local-leader-key ",")
(general-create-definer local-leader-def
:prefix local-leader-key
:keymaps 'override
:states '(normal visual))
(leader-def
"" '(nil :wk "my lieutenant general prefix")
;; Prefixes
"a" '(:ignore t :wk "app")
"b" '(:ignore t :wk "buffer")
"d" '(:ignore t :wk "dired")
"f" '(:ignore t :wk "file")
"f e" '(:ignore t :wk "emacs")
"g" '(:ignore t :wk "git")
"h" '(:ignore t :wk "help")
"j" '(:ignore t :wk "jump")
"k" '(:ignore t :wk "lisp")
"m" '(:ignore t :wk "mail")
"o" '(:ignore t :wk "org")
"p" '(:ignore t :wk "project/perspective")
"s" '(:ignore t :wk "search/spell")
"t" '(:ignore t :wk "toggle")
"w" '(:ignore t :wk "window")
;; General keybinds
"\\" 'indent-region
"|" 'sulami/shell-command-on-region
"a c" 'calc
"a s" 'shell
"b e" 'erase-buffer
"b d" 'kill-this-buffer
"b D" 'kill-buffer-and-window
"b m" 'sulami/open-message-buffer
"b ." 'sulami/open-minibuffer
"b r" 'sulami/rename-file-and-buffer
"b s" 'sulami/open-scratch-buffer
"b y" 'sulami/copy-buffer
"d d" #'dired
"d s" #'sulami/open-source-dir
"f f" 'find-file
"f e e" 'sulami/open-emacs-config
"f e r" 'sulami/reload-emacs-config
"f D" 'sulami/delete-file-and-buffer
"f R" 'sulami/rename-file-and-buffer
"h e" 'info-display-manual
"h g" 'general-describe-keybindings
"h l" 'view-lossage
"h m" 'woman
"h v" 'describe-variable
"t a" 'auto-fill-mode
"t l" 'toggle-truncate-lines
"t r" 'refill-mode
"t s" 'flyspell-mode
"t n" 'sulami/toggle-line-numbers
"t N" 'sulami/toggle-narrow
"t w" 'whitespace-mode
"w =" 'balance-windows
"w f" 'make-frame
"w m" 'sulami/toggle-maximise-window
"w u" 'winner-undo)
(general-define-key
"s-m" #'suspend-frame
"s-t" #'sulami/toggle-term
"s-u" #'universal-argument
"s-=" (lambda () (interactive) (text-scale-increase 0.5))
"s--" (lambda () (interactive) (text-scale-decrease 0.5))
"s-0" (lambda () (interactive) (text-scale-increase 0))
"M-q" #'sulami/fill-or-unfill)
(general-nmap "g r" #'xref-find-references)
;; Dired
(general-define-key
:keymaps 'dired-mode-map
"<return>" 'dired-find-alternate-file))
#+END_SRC
* Evil
This provides vim-style modal editing. There is quite a bit of
boilerplate to make it work with the various components, but I really
can't stand the default Emacs keybindings.
#+begin_src emacs-lisp :tangle yes
(use-package evil
:init
(setq evil-want-C-u-scroll t
evil-want-C-i-jump t
evil-want-Y-yank-to-eol t
evil-want-keybinding nil
evil-ex-visual-char-range t
evil-move-beyond-eol t
evil-disable-insert-state-bindings t)
:custom
(evil-undo-system 'undo-fu)
:config
;; This conflicts with the local leader
(unbind-key "," evil-motion-state-map)
(defun sulami/evil-shift-left-visual ()
"`evil-shift-left`, but keeps the selection."
(interactive)
(call-interactively 'evil-shift-left)
(evil-normal-state)
(evil-visual-restore))
(defun sulami/evil-shift-right-visual ()
"`evil-shift-right`, but keeps the selection."
(interactive)
(call-interactively 'evil-shift-right)
(evil-normal-state)
(evil-visual-restore))
:general
(leader-def
"TAB" #'evil-switch-to-windows-last-buffer
"<tab>" #'evil-switch-to-windows-last-buffer
"w d" #'evil-window-delete
"w h" #'evil-window-move-far-left
"w j" #'evil-window-move-very-bottom
"w k" #'evil-window-move-very-top
"w l" #'evil-window-move-far-right
"w /" #'evil-window-vsplit
"w -" #'evil-window-split)
(general-imap
"C-w" #'evil-delete-backward-word
"C-k" #'evil-insert-digraph)
(general-vmap
">" #'sulami/evil-shift-right-visual
"<" #'sulami/evil-shift-left-visual)
:hook (after-init . evil-mode))
#+end_src
** evil-collection
This adds evil-keybindings for /a lot/ of popular modes.
I have to disable some because they clash with my own.
#+begin_src emacs-lisp :tangle yes
(use-package evil-collection
:after (evil)
:config
(setq evil-collection-mode-list
(->> evil-collection-mode-list
(delete 'company)
(delete 'gnus)
(delete 'lispy)))
(evil-collection-init))
#+end_src
** evil-org
Evil-keybindings for org/agenda.
#+begin_src emacs-lisp :tangle yes
(use-package evil-org
:after (org)
:config
(require 'evil-org-agenda)
:hook ((org-mode . evil-org-mode)
(org-agenda-mode . evil-org-agenda-set-keys)))
#+end_src
** evil-commentary
=vim-commentary= but for evil.
#+begin_src emacs-lisp :tangle yes
(use-package evil-commentary
:hook (evil-mode . evil-commentary-mode))
#+end_src
** evil-surround
=vim-surround= but for evil.
#+begin_src emacs-lisp :tangle yes
(use-package evil-surround
:hook (evil-mode . global-evil-surround-mode))
#+end_src
** evil-numbers
#+begin_src emacs-lisp :tangle yes
(use-package evil-numbers
:defer t
:general
(general-nvmap
"C-a" 'evil-numbers/inc-at-pt
"C-z" 'evil-numbers/dec-at-pt))
#+end_src
* Undo-fu
This just provides linear undo/redo. As an added bonus, it also does
"undo in region".
#+begin_src emacs-lisp :tangle yes
(use-package undo-fu
:defer t
:custom
(undo-fu-allow-undo-in-region t)
(undo-fu-ignore-keyboard-quit t))
#+end_src
* Which key
This shows all available keybindings when I hit a key. Sometimes
useful.
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package which-key
:hook (after-init . which-key-mode))
#+END_SRC
* Vertico
Vertico, the successor to Selectrum, is a great narrowing and
selection tool, intended to replace Ivy & Helm.
#+begin_src emacs-lisp :tangle yes
(use-package vertico
:custom
(completion-in-region-function #'consult-completion-in-region)
:general
(leader-def
"b b" #'consult-buffer
"p b" #'consult-project-buffer)
:hook
(after-init . vertico-mode))
#+end_src