-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.el
5411 lines (4441 loc) · 192 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
;;; .emacs --- My emacs root config file. -*- lexical-binding: t; outline-minor-mode: t -*-
;;; Commentary:
;; This file is now pointing at a literate program version of
;; my Emacs config in ~/.emacs.d/startup.org
;;
;; Important extra files which are required to be in ~/.emacs.d/
;; - conf/system-vars.el
;; Defines a set of variables specific to the system which this
;; config is being run on (such as which OS is running.)
;; - org-settings/org-agenda-file-list.el
;; Sets the list of files which org should use for agenda on
;; this computer.
;;
;; There's an important and wierd fix which I have to make for OS X.
;; I need to define `x-max-tooltip-size'. I'm still not really sure
;; why, but I think that 50 should be fine.
;;; Code:
;; For debugging what gets compiled at startup
;;(debug-on-entry #'byte-compile)
;; Don't native compile auto loads. There's something wrong with them
;; right now: 17/02/2021.
(setq comp-deferred-compilation-deny-list '("\\(?:[^z-a]*-autoloads\\.el$\\)"))
;; Bootstrap straight
(defvar bootstrap-version)
(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)))
(load bootstrap-file nil 'nomessage))
;; Early in the file to avoid version conflicts
(straight-use-package 'org)
(defalias 'compat-assoc-delete-all #'assoc-delete-all)
(defalias 'compat-executable-find #'executable-find)
(defalias 'compat-dired-get-marked-files #'dired-get-marked-files)
;; Load system specific configuration
(add-to-list 'load-path "~/.emacs.d/conf")
(require 'system-vars)
(setq quiescent-starting-up t)
(when (file-exists-p "~/.emacs.d/conf/system-conf.el")
(require 'system-conf))
(defvar quiescent-toggle-buffer-backwards t
"Whether we should go forwards or backwards when we next toggle buffers.")
(defun quiescent-toggle-buffer ()
"Toggle buffers switching to either the previous buffer or the next buffer."
(interactive)
(progn
(if quiescent-toggle-buffer-backwards
(switch-to-prev-buffer)
(switch-to-next-buffer))
(setq quiescent-toggle-buffer-backwards
(not quiescent-toggle-buffer-backwards))))
(global-set-key (kbd "s-b") #'quiescent-toggle-buffer)
(global-set-key (kbd "s-z") #'isearchb-activate)
(global-set-key (kbd "s-n") #'switch-to-next-buffer)
(global-set-key (kbd "s-p") #'switch-to-prev-buffer)
(when quiescent-exwm-machine
(eval
`(progn
(use-package xelb
:straight t
:demand t)
(use-package exwm
:straight t
:demand t)
(defun quiescent-exwm-setup-input-simulation ()
"Setup my bindings to simulate input in EXWM."
(setq exwm-input-simulation-keys
'(
;; movement
([?\C-b] . [left])
([?\M-b] . [C-left])
([?\C-f] . [right])
([?\M-f] . [C-right])
([?\C-p] . [up])
([?\C-n] . [down])
([?\C-a] . [home])
([?\C-e] . [end])
([?\M-v] . [prior])
([?\C-v] . [next])
([?\C-d] . [delete])
([?\C-k] . [S-end delete])
;; cut/paste.
([?\C-w] . [?\C-x])
([?\M-w] . [?\C-c])
([?\C-y] . [?\C-v])
;; search
([?\C-s] . [?\C-f]))))
(defun quiescent-exwm-setup-global-bindings ()
"Setup global bindings for EXWM mode."
(setq exwm-input-global-keys
`(
;; Bind "s-r" to exit char-mode and fullscreen mode.
([?\s-r] . exwm-reset)
;; Bind "s-w" to switch workspace interactively.
([?\s-w] . exwm-workspace-switch)
;; Bind "s-0" to "s-9" to switch to a workspace by its index.
,@(mapcar (lambda (i)
`(,(kbd (format "s-%d" i)) .
(lambda ()
(interactive)
(exwm-workspace-switch-create ,i))))
(number-sequence 0 9))
;; Bind "s-&" to launch applications ('M-&' also works if the output
;; buffer does not bother you).
([?\s-&] . (lambda (command)
(interactive (list (read-shell-command "$ ")))
(start-process-shell-command command nil command)))
([?\s-*] . (lambda ()
(interactive)
(start-process-shell-command "*Firefox*" nil "firefox --new-tab --url https://www.duckduckgo.com/")))
([?\s-$] . (lambda ()
(interactive)
(start-process-shell-command "*Screenshot*" nil "import /home/edward/screenshot.png")))
;; Bind "s-<f2>" to "slock", a simple X display locker.
([s-f2] . (lambda ()
(interactive)
(start-process "" nil "/usr/bin/slock"))))))
(defun quiescent-exwm-setup-exwm-keys ()
"Setup some keys which I use in EXWM."
(progn
(define-key exwm-mode-map [?\C-q] #'exwm-input-send-next-key)
(define-key exwm-mode-map (kbd "s-c") #'org-capture)
(exwm-input-set-key (kbd "C-c C-j") #'exwm-input-grab-keyboard)
(exwm-input-set-key (kbd "s-b") #'quiescent-toggle-buffer)
(exwm-input-set-key (kbd "s-z") #'isearchb-activate)
(exwm-input-set-key (kbd "s-n") #'switch-to-next-buffer)
(exwm-input-set-key (kbd "s-p") #'switch-to-prev-buffer)))
(defun quiescent-exwm-setup-displays-and-start ()
"Setup settings for multiple displays and fire it up!"
(if quiescent-exwm-multiple-monitors
;; From https://github.com/ch11ng/exwm/wiki
(progn
(require 'exwm-randr)
(when quiescent-home-pc-linux
(setq exwm-randr-workspace-output-plist
'(0 "DisplayPort-1" 1 "DisplayPort-1"
2 "HDMI-A-0" 3 "HDMI-A-0"
4 "HDMI-A-0" 5 "HDMI-A-0"
6 "HDMI-A-0" 7 "HDMI-A-0"))
(add-hook 'exwm-randr-screen-change-hook
(lambda ()
(start-process-shell-command
"xrandr" nil "xrandr --output HDMI-A-0 --left-of DisplayPort-1 --auto")))
(start-process-shell-command
"xrandr" nil "xrandr --output HDMI-A-0 --left-of DisplayPort-1 --auto"))
(when quiescent-work-machine
(setq exwm-randr-workspace-output-plist
'(0 "DP-2" 1 "DP-2"
2 "DP-1" 3 "DP-1"
4 "DP-1" 5 "DP-1"
6 "DP-1" 7 "DP-1"))
(add-hook 'exwm-randr-screen-change-hook
(lambda ()
(start-process-shell-command
"xrandr" nil "xrandr --output DP-1 --left-of DP-2 --auto")))
(start-process-shell-command
"xrandr" nil "xrandr --output DP-1 --left-of DP-2 --auto"))
(exwm-randr-enable))
(exwm-enable)))
(when quiescent-exwm-machine
(require 'exwm-config)
(defun exwm-config-ido ()
"Override the config function for ido to do nothing since I don't use it."
nil)
(exwm-config-default)
(fringe-mode 15)
(quiescent-exwm-setup-input-simulation)
(quiescent-exwm-setup-exwm-keys)
(quiescent-exwm-setup-global-bindings)
(quiescent-exwm-setup-displays-and-start)))))
(setq epa-pinentry-mode 'loopback)
(add-to-list 'load-path "~/.emacs.d/conf")
(require 'system-vars)
(require 'cl-lib)
(setq quiescent-starting-up t)
(setq custom-file "~/.emacs.d/custom.el")
(load custom-file)
;;; * Themes
;;; ** Nano
(use-package mini-frame
:straight t)
(use-package ts
:straight t)
(use-package svg-tag-mode
:straight t)
(straight-use-package
'(nano-emacs :type git :host github :repo "rougier/nano-emacs"))
(require 'nano-layout)
(require 'nano-faces)
(require 'nano-theme)
(require 'nano-theme-dark)
(nano-theme-set-dark)
(call-interactively 'nano-refresh-theme)
(defun quiescent-light-mode ()
"Set the nano theme to light."
(interactive)
(require 'nano-theme-light)
(nano-theme-set-light)
(call-interactively 'nano-refresh-theme))
(defun quiescent-defaults ()
"Setup my defaults the way I like 'em."
(progn
(setq frame-title-format nil)
(when (eq system-type 'darwin)
(setq ns-use-native-fullscreen t
mac-option-key-is-meta t
mac-command-key-is-meta nil
mac-option-modifier 'meta
mac-use-title-bar nil))
(defun copy-from-osx ()
(shell-command-to-string "pbpaste"))
(defun paste-to-osx (text &optional push)
(let ((process-connection-type nil))
(let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
(process-send-string proc text)
(process-send-eof proc))))
(when (and (not (display-graphic-p))
(eq system-type 'darwin))
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx))
(fset 'yes-or-no-p 'y-or-n-p)
(setq-default tab-width 4)
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-language-environment 'utf-8)
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse
uniquify-separator " • "
uniquify-after-kill-buffer-p t
uniquify-ignore-buffers-re "^\\*")
(unless
(or (eq system-type 'windows-nt)
(not (file-exists-p "/bin/bash")))
(setq-default shell-file-name "/bin/bash")
(setq explicit-shell-file-name "/bin/bash"))
(defadvice term-sentinel (around my-advice-term-sentinel (proc msg))
(if (memq (process-status proc) '(signal exit))
(let ((buffer (process-buffer proc)))
ad-do-it
(kill-buffer buffer))
ad-do-it))
(ad-activate 'term-sentinel)
(setq pop-up-windows t)))
(quiescent-defaults)
(require 'nano-modeline)
;;
;;; ** Page Break Lines
(use-package page-break-lines
:straight t
:config (progn
(add-to-list 'page-break-lines-modes 'sql-mode)
(global-page-break-lines-mode 1)))
;;
;;; ** MB Depth
(use-package mb-depth
:straight t)
;;
;;; ** Crosshair
(use-package xhair
:straight t)
;;
;;; ** Visual Bell
;; From Phil, posted on https://pragmaticemacs.wordpress.com/2017/10/15/using-a-visible-bell-in-emacs/
;;
;; Date accessed: 17/02/2023
;;
;; The visible bell is usually fine, but still horrid in certain terminals.
;; We can make a nicer version.
(defun quiescent-invert-nano-header ()
"Invert the nano header line face."
(progn
(invert-face 'nano-face-header-default)
(invert-face 'nano-face-header-strong)))
(defun my-visible-bell ()
"A friendlier visual bell effect."
(quiescent-invert-nano-header)
(run-with-timer 0.1 nil #'quiescent-invert-nano-header))
(define-minor-mode my-visible-bell-mode
"Use `my-visible-bell’ as the `ring-bell-function’."
:global t
(let ((this 'my-visible-bell-mode))
(if my-visible-bell-mode
(progn
(put this 'visible-bell-backup visible-bell)
(put this 'ring-bell-function-backup ring-bell-function)
(setq visible-bell nil
ring-bell-function #'my-visible-bell))
;; Restore the original values when disabling.
(setq visible-bell (get this 'visible-bell-backup)
ring-bell-function (get this 'ring-bell-function-backup)))))
(setq visible-bell t)
(my-visible-bell-mode 1)
;;
;;; ** Compilation mode
(defun endless/colorize-compilation ()
"Colorize from `compilation-filter-start' to `point'."
(let ((inhibit-read-only t))
(ansi-color-apply-on-region
compilation-filter-start (point))))
(defun quiescent-enable-ansi-coloured-compilation ()
"Enabled or disable a hook to colourise compilation buffers."
(interactive)
(if (member #'endless/colorize-compilation compilation-filter-hook)
(remove-hook 'compilation-filter-hook
#'endless/colorize-compilation)
(add-hook 'compilation-filter-hook
#'endless/colorize-compilation)))
;;
;;; * Editing Anything
;;; ** Manipulating Windows and Frames
(defun quiescent-close-help ()
"Close the help window."
(interactive)
(let ((original-window (selected-window)))
(cl-loop
for window being the windows
when (equal "*Help*" (buffer-name (window-buffer window)))
do (select-window window)
(call-interactively #'quit-window))
(select-window original-window)))
(global-set-key (kbd "C-c f") #'make-frame)
(global-set-key (kbd "C-c B") #'browse-url-at-point)
(global-set-key (kbd "s-o") #'other-frame)
(global-set-key (kbd "s-q") #'quiescent-close-help)
(global-set-key (kbd "s-)") #'delete-window)
(global-set-key (kbd "s-!") #'delete-other-windows)
(global-set-key (kbd "s-@") #'split-window-below)
(global-set-key (kbd "s-#") #'split-window-right)
;;
;;; ** Flyspell
(use-package flyspell
:init (progn
(setq ispell-dictionary "british")
(setq ispell-program-name "aspell"))
:config
(progn
(defun quiescent-turn-on-flyspell ()
"Turn on `flyspell'."
(when (null quiescent-starting-up)
(turn-on-flyspell)))
(define-key flyspell-mode-map (kbd "C-.") nil)
(add-hook 'text-mode-hook #'quiescent-turn-on-flyspell)
(add-hook 'bibtex-mode-hook #'quiescent-turn-on-flyspell)
(add-hook 'LaTeX-mode-hook #'quiescent-turn-on-flyspell)
(add-hook 'TeX-mode-hook #'quiescent-turn-on-flyspell)))
;;
;;; ** Unfill Paragraph
;;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph
(defun unfill-paragraph (&optional region)
"Unfill REGION, otherwise paragraph at point.
i.e. the reverse of fill paragraph."
(interactive (progn (barf-if-buffer-read-only) '(t)))
(let ((fill-column (point-max))
;; This would override `fill-column' if it's an integer.
(emacs-lisp-docstring-fill-column t))
(fill-paragraph nil region)))
;; Handy key definition
(define-key global-map "\M-Q" 'unfill-paragraph)
;;
;;; ** Fill/Unfill All
(defmacro quiescent-for-all-paragraphs (&rest body)
"Execute BODY with the point in each paragraph in the current buffer."
`(save-excursion
(goto-char (point-min))
(while (not (eq (point) (point-max)))
(ignore-errors
,@body
(forward-paragraph)))))
(defun quiescent-fill-all-paragraphs ()
"Run `fill-paragraph' on all paragraphs in the current document."
(interactive)
(quiescent-for-all-paragraphs (fill-paragraph)))
(defun quiescent-unfill-all-paragraphs ()
"Run `unfill-paragraph' on all paragraphs in the current document."
(interactive)
(quiescent-for-all-paragraphs (unfill-paragraph)))
;;
;;; ** General Editing Conf
(setq-default indent-tabs-mode nil)
(defun quiescent-disable-indent-tabs-mode ()
"Disable `indent-tabs-mode'."
(setq indent-tabs-mode nil))
(add-hook 'prog-mode-hook #'quiescent-disable-indent-tabs-mode)
(setq tab-width 4)
(column-number-mode 1)
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
;;
;;; ** Using the Other Window
(defmacro quiescent-in-other-buffer (&rest body)
"Execute BODY in the other window and return to the startindg window."
`(progn
(other-window 1)
,@body
(select-window (previous-window))))
(defun quiescent-other-window-line-down ()
"Move point one line down in the other window.
Maintains the point in the current window."
(interactive)
(quiescent-in-other-buffer
(forward-line)))
(defun quiescent-other-window-line-up ()
"Move point one line up in the other window.
Maintains the point in the current window."
(interactive)
(quiescent-in-other-buffer
(forward-line -1)))
(defun quiescent-search-other-window ()
"Search the other window and then pop back to the current one."
(interactive)
(progn
(quiescent-in-other-buffer
(isearch-forward))))
;;
;; ** Searching and Editing Thing at Point
(define-key prog-mode-map (kbd "M-'") #'isearch-forward-symbol-at-point)
;;
;;; ** Swapping windows
(defun quiescent-swap-windows ()
"Swap the two most recently displayed windows."
(interactive)
(let* ((windows (window-list (window-frame)))
(window-1 (car windows))
(buffer-1 (window-buffer window-1))
(window-2 (cadr windows))
(buffer-2 (window-buffer window-2)))
(when window-2
(set-window-buffer window-1 buffer-2)
(set-window-buffer window-2 buffer-1))))
(global-set-key (kbd "s-s") #'quiescent-swap-windows)
;;
;;; ** Treating Buffer Lines as Sets
(defvar *buffer-disjunction-results-buffer-name* "*disjunction-results*"
"The name of the buffer to display disjunction results in.")
(require 'cl-lib)
(defun buffer-disjunction-lines-from-buffer (buffer)
"Produce the lines in BUFFER as a list of strings."
(with-current-buffer buffer
(split-string (buffer-substring (point-min) (point-max))
"\n"
" ")))
(defun buffer-disjunction-buffer-disjunction (this-buffer that-buffer)
"Produce lines which are not in both THIS-BUFFER and THAT-BUFFER.
Results are displayed in a new buffer controlled by
`*buffer-disjunction-results-buffer-name*'."
(interactive "bThis buffer: \nbThat buffer:")
(let* ((these-lines (buffer-disjunction-lines-from-buffer this-buffer))
(those-lines (buffer-disjunction-lines-from-buffer that-buffer))
(results-buffer (get-buffer *buffer-disjunction-results-buffer-name*))
(diff (mapcar (lambda (x) (concat x "\n"))
(cl-set-difference these-lines those-lines))))
(switch-to-buffer (if (not (bufferp results-buffer))
(generate-new-buffer *buffer-disjunction-results-buffer-name*)
results-buffer))
(erase-buffer)
(mapc #'insert diff)))
(defvar *buffer-set-difference-results-buffer-name* "*this-but-not-that-results*"
"The name of the buffer to display this-but-not-that results in.")
(defun buffer-set-difference (this-buffer that-buffer)
"Produce lines which are in THIS-BUFFER but not THAT-BUFFER.
Results are displayed in a new buffer controlled by
`*buffer-set-difference-results-buffer-name*'."
(interactive "bThis buffer: \nbThat buffer:")
(let* ((these-lines (buffer-disjunction-lines-from-buffer this-buffer))
(those-lines (mapcar (lambda (x) (cons x t))
(buffer-disjunction-lines-from-buffer that-buffer)))
(results-buffer (get-buffer *buffer-set-difference-results-buffer-name*))
(diff (mapcar (lambda (x) (concat x "\n"))
(cl-remove-if (lambda (x) (assoc x those-lines))
these-lines))))
(switch-to-buffer (if (not (bufferp results-buffer))
(generate-new-buffer *buffer-set-difference-results-buffer-name*)
results-buffer))
(erase-buffer)
(mapc #'insert diff)))
;;
;;; ** Tiny
(use-package tiny
:straight t
:config (tiny-setup-default))
;;
;;; ** Hippie Expand
(global-set-key [remap dabbrev-expand] 'hippie-expand)
;; TODO: this has a bug where the internal state of hippie expand
;; doesn't know that I fiddled with things.
(defvar quiescent-opening-brackets
(list ?\( ?\[ ?< ?{)
"Brackets that must be closed during hippie expand.")
(defvar quiescent-closing-brackets
(list ?\) ?\] ?> ?})
"Brackets that close an opening bracket during hippie expand.")
(defvar quiescent-start-or-end-of-string
(list ?\" ?\' ?\`)
"Characters that constitute the start of a string.")
(require 'cl-lib)
(defun quiescent-closing-bracket-for (char)
"Produce the closing bracket of CHAR.
Produce nil if CHAR isn't an opening bracket."
(if (not (member char quiescent-opening-brackets))
nil
(nth (cl-position char quiescent-opening-brackets)
quiescent-closing-brackets)))
(defun quiescent-balance-hippie-insertion (s)
"Ensure that S is balanced in brackets and string quote chars.
Ignore any syntax other than strings, just push any open brackets
to a stack and ensure they get closed.
At the first char that double closes, end the string, close the
other brackets and produce the result."
(let ((result)
(brackets)
(quotes))
(let ((final-result (cl-block outer
(dotimes (i (length s) (apply #'string (nreverse result)))
(let ((char (aref s i)))
(cond
((and quotes
(not (member char quiescent-start-or-end-of-string)))
(push char result))
((member char quiescent-start-or-end-of-string)
(if (eq char (car quotes))
(push (pop quotes) result)
(progn
(push char quotes)
(push char result))))
((member char quiescent-opening-brackets)
(progn
(push char brackets)
(push char result)))
((member char quiescent-closing-brackets)
(progn
(if (not (eq char (quiescent-closing-bracket-for (car brackets))))
(progn
(while brackets
(push (quiescent-closing-bracket-for (pop brackets)) result))
(cl-return-from outer (apply #'string (nreverse result))))
(progn
(push char result)
(pop brackets)))))
(t (push char result))))))))
(s-trim-right
(cl-concatenate 'string final-result
(nreverse quotes)
(mapcar #'quiescent-closing-bracket-for brackets))))))
(defun quiescent-ensure-even-braces-after-hippie (f args)
"Run F (`hippie-expand') then ensure brackets match.
Pass ARGS to F."
(let ((start (or he-string-beg (point))))
(funcall f args)
(let* ((end (point))
(inserted (buffer-substring start (point)))
(balanced (quiescent-balance-hippie-insertion inserted)))
(when (not (equal inserted balanced))
(kill-region start (point))
(insert balanced)
(setq this-command 'hippie-expand)))))
(advice-add #'hippie-expand :around
#'quiescent-ensure-even-braces-after-hippie)
(defun quiescent-setup-lisp-string-quotes ()
"Setup the quotes that open and close strings for lisps."
(setq-local quiescent-start-or-end-of-string (list ?\")))
(add-hook 'lisp-mode-hook #'quiescent-setup-lisp-string-quotes)
(add-hook 'emacs-lisp-mode-hook #'quiescent-setup-lisp-string-quotes)
;;
;;; ** Avy
(use-package avy
:straight t
:config (progn
(setq avy-timeout-seconds 0.1)
(global-set-key (kbd "s-x") #'quiescent-avy-super-jump)
(global-set-key (kbd "s-.") #'avy-goto-word-or-subword-1)
(global-set-key (kbd "s-c") #'quiescent-avy-copy-symbol)
(global-set-key (kbd "s-C") #'quiescent-avy-copy-sexp)))
(defun quiescent-avy-super-jump ()
"Jump to a point with avy and then find the definition."
(interactive)
(progn
(avy-goto-word-or-subword-1)
(xref-find-definitions (thing-at-point 'symbol))))
(defun quiescent-avy-copy-symbol ()
"Copy a word subword or symbol using avy."
(interactive)
(save-excursion
(save-window-excursion
(avy-goto-word-or-subword-1)
(call-interactively #'mark-sexp)
(kill-ring-save (region-beginning) (region-end))))
(yank))
(defun quiescent-avy-copy-sexp ()
"Copy an sexp using avy.
Goes backward up list and then copies the sexp."
(interactive)
(save-excursion
(save-window-excursion
(avy-goto-word-or-subword-1)
(backward-up-list)
(call-interactively #'mark-sexp)
(kill-ring-save (region-beginning) (region-end))))
(yank))
(defun quiescent-goto-char (&optional str)
"Perform an isearch-like session with STR.
If candidates are numbered as you go and if you type the number
of a candidate then you can jump to it."
(interactive)
(let ((char (read-string (concat "search: " (or str "")))))
(if (member (aref char 0) '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
(quiescent-jump-to-candidate char str)
(let ((search-string (concat str char)))
(quiescent-highlight-matches search-string)
(call-interactively (quiescent-goto-char search-string))))))
(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)
(defun quiescent-push-mark-to-ring (&optional arg escape-string no-syntax-crossing)
"Double `push-mark' for pushing straight onto the ring.
Ignore ARG, ESCAPE-STRING and NO-SYNTAX-CROSSING, they're there
to make the advice work."
(ignore arg)
(ignore escape-string)
(ignore no-syntax-crossing)
(progn
(push-mark nil t)
(push-mark nil t)))
;;
;;; ** Better Backward Up List
(defun quiescent-backward-up-list ()
"Go `backward-up-list' leaving behind the marker."
(interactive)
(progn
(quiescent-push-mark-to-ring)
(call-interactively #'backward-up-list)))
(global-set-key (kbd "C-M-u") #'quiescent-backward-up-list)
(with-eval-after-load "paredit"
(define-key paredit-mode-map (kbd "C-M-u") #'quiescent-backward-up-list))
(advice-add #'nxml-backward-up-element :before
#'quiescent-push-mark-to-ring)
;;
;;; ** Keep Popping the Ring Until the Point Moved
(defun quiescent-keep-popping-til-moved (pop-function &rest args)
"Keep popping the mark until either the mark ring is empty or the point moved."
(interactive)
(progn
(while (and mark-ring
(= (point) (marker-position (car mark-ring))))
(pop mark-ring))
(apply pop-function args)))
(advice-add #'pop-to-mark-command :around #'quiescent-keep-popping-til-moved)
;;
;;; ** Make the Mark Visible
(use-package visible-mark
:straight t
:config (progn
(global-visible-mark-mode 1)
(setq visible-mark-max 1)
(setq visible-mark-faces `(visible-mark-face1 visible-mark-face2))))
;;
;;; ** Hydra
(use-package hydra
:straight t
:config
(progn
(defhydra hydra-drawing (:color pink
:pre (overwrite-mode)
:post (progn (whitespace-cleanup)
(overwrite-mode -1)))
"Hydra draw"
("f" (hydra-drawing-forward-maintaining-line) "forward")
("b" backward-char "backward")
("p" (hydra-drawing-move-maintaining-column (lambda () (forward-line -1))) "previous line")
("n" (hydra-drawing-move-maintaining-column 'next-line) "next line")
("q" nil "quit" :color blue))
(global-set-key (kbd "C-c C-c d") 'hydra-drawing/body)
(defun hydra-drawing-move-maintaining-column (move-action)
"Execute `MOVE-ACTION' while attempting to mainting column position."
(let* ((column-before)
(column-after))
(progn
(setq column-before (current-column))
(when (= (point) (point-max))
(newline)
(backward-char))
(funcall move-action)
(setq column-after (current-column))
(while (< column-after column-before)
(insert " ")
(setq column-after (1+ column-after))))))
(defun hydra-drawing-forward-maintaining-line ()
"Move forward on character attempting to maintain the current line."
(let* ((line-before (line-number-at-pos)))
(if (= (point) (point-max))
(insert " ")
(progn (forward-char)
(when (/= line-before (line-number-at-pos))
(progn
(backward-char)
(insert " ")))))))
(defhydra hydra-explore-code (:pre (global-auto-highlight-symbol-mode t)
:post (global-auto-highlight-symbol-mode -1)
:color amaranth)
"
Code Explorer
^Move^ ^Jump^ ^Project^
^^^^^^^^---------------------------------------------------------
_f_: forward s-exp _B_: pop mark _s_: project grep
_b_: backward s-exp _m_: mark point _g_: goto file
_n_: forward list _j_: xref jump
_p_: backward list _P_: prev file
_d_: down list _x_: super jump
_u_: up list _i_: imenu
_q_: quit
"
("f" forward-sexp nil)
("b" backward-sexp nil)
("n" forward-list nil)
("p" backward-list nil)
("d" down-list nil)
("u" up-list nil)
("P" (pop-global-mark) nil)
("B" (pop-to-mark-command) nil)
("m" (push-mark) nil)
("j" xref-find-definitions nil)
("s" quiescent-vc-git-grep nil)
("x" quiescent-avy-super-jump nil)
("q" nil nil)
("g" project-find-file nil))))
;;
;;; ** Window Jump
(use-package window-jump
:straight t
:init (progn
(global-set-key (kbd "s-u") #'window-jump-up)
(global-set-key (kbd "s-d") #'window-jump-down)
(global-set-key (kbd "s-l") #'window-jump-left)
(global-set-key (kbd "s-r") #'window-jump-right)))
;;
;;; ** iSearch
(defun quiescent-kill-isearch-match ()
"Kill the current isearch match string and continue searching."
(interactive)
(kill-region isearch-other-end (point))
(isearch-repeat-forward))
(define-key isearch-mode-map [(control k)] 'quiescent-kill-isearch-match)
(defun quiescent-first-search-hit ()
"Activate isearch from the start of the buffer using the existing string if it's present."
(interactive)
(progn
(let ((original-isearch-string (when isearch-mode isearch-string)))
(goto-char (point-min))
(isearch-forward nil t)
(when original-isearch-string (isearch-yank-string original-isearch-string)))))
(defun quiescent-last-search-hit ()
"Activate isearch from the end of the buffer using the existing string if it's present."
(interactive)
(progn
(let ((original-isearch-string (when isearch-mode isearch-string)))
(goto-char (point-max))
(isearch-backward nil t)
(when original-isearch-string (isearch-yank-string original-isearch-string)))))
(define-key isearch-mode-map (kbd "M-<") #'quiescent-first-search-hit)
(define-key isearch-mode-map (kbd "M->") #'quiescent-last-search-hit)
(global-set-key (kbd "M-s M-<") #'quiescent-first-search-hit)
(global-set-key (kbd "M-s M->") #'quiescent-last-search-hit)
;;
;;; ** Crosslink Buffers
(defvar quiescent-cross-link-linked-buffer nil
"The buffer which this buffer is cross linked to.")
(make-variable-buffer-local 'quiescent-cross-link-linked-buffer)
(defun quiescent-cross-link ()
"Cross link symbols in the current buffer to another buffer."
(interactive)
(let ((search-symbol (thing-at-point 'symbol)))
(when (not quiescent-cross-link-linked-buffer)
(call-interactively 'quiescent-cross-link-set-cross-linked-buffer))
(pop-to-buffer quiescent-cross-link-linked-buffer)
(goto-char (point-min))
(search-forward search-symbol)))
(defun quiescent-coss-link-reset ()
"Reset the linked buffer for current buffer."
(interactive)
(setq quiescent-cross-link-linked-buffer nil))
(defun quiescent-cross-link-set-cross-linked-buffer (linked-buffer-name)
"Set the LINKED-BUFFER-NAME of the buffer to link this buffer to."
(interactive "bEnter buffer name to link to: ")
(setq quiescent-cross-link-linked-buffer linked-buffer-name))
;;
;;; ** Multiple Cursors
(use-package multiple-cursors
:straight t
:config
(progn
(global-set-key (kbd "s-SPC") #'mc/edit-lines)
(global-set-key (kbd "C->") #'mc/mark-next-like-this)
(global-set-key (kbd "C-<") #'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") #'mc/mark-all-like-this)
(global-set-key (kbd "s->") #'mc/skip-to-next-like-this)
(global-set-key (kbd "s-<") #'mc/skip-to-previous-like-this)))
;;
;;; ** iEdit
(use-package iedit
:straight t
:config (global-set-key (kbd "C-'") #'iedit-mode))
;;
;;; ** Hi-Scroll-Mode
;; (Based on an XKCD ^_^)
(define-minor-mode hiscroll-mode
"Toggle hiscroll-mode.
Interactively with no argument, this command toggles the mode.
A positive prefix argument enables the mode, any other prefix
argument disables it. From Lisp, argument omitted or nil enables
the mode, `toggle' toggles the state.
When hiscroll-mode is enabled the mouse wheel will move the
current buffer through time (i.e. undo/redo while you scroll.)"
:init-value nil
:lighter " HiS"
:keymap
'(([mouse-5] . hiscroll-undo)
([mouse-4] . hiscroll-redo))
:group 'hiscroll)
(defvar hiscroll-undo-direction 'UNDO
"The direction to go in history of changes for this buffer.")
(defun hiscroll--correct-direction-and-go (desired-direction)