aboutsummaryrefslogtreecommitdiff
path: root/shoshimacs.org
blob: fe1b1f00cd84adb4cbb1483ad79a01d70106080d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
#+AUTHOR: Grant Shoshin Shangreaux
#+EMAIL: shoshin@cicadas.surf
#+DESCRIPTION: Html version of an Emacs literate org configuration
#+STARTUP: overview
#+PROPERTY: header-args:emacs-lisp :lexical t
#+OPTIONS: broken-links:mark

* A New Start

To welcome in Emacs 28 I intend to re-aquaint myself with the application
and its ecosystem. I've been perusing the packages available through the
default ELPA and non-gnu ELPA repos and trying to put together the various
things that I've grown accustomed to.

However, with a beginner's mind, I've been trying to avoid going down the
same old idiosyncratic paths. Courting a bit of discomfort in order to learn
what newcomers might experience coming to Emacs in this current version.

** Overview

This document is a journal, manual, and a program at once. I'm no expert at
writing a document like this. If you happen to be reading it, the journal
nature may be confusing. Over time, the journal will be incorporated into the
bits that are a manual, solidified knowledge gained through the experience.

The program bits will be tangled into [[./shoshimacs.el]]. As a program, it
requires a certain structure from top to bottom. Here, the snippets may be
scattered around. I'll attempt to have a system to keep them organized, but
this is all an experiment.

The following code block is the "table of contents" that determines what
is "tangled" into the resulting elisp file:

#+begin_src emacs-lisp :tangle yes :noweb no-export
  ;;; shoshimacs.el --- Beginner's Mind Config  -*- lexical-binding:t -*-

  <<preamble>>

  <<defvars>>

  ;;; Package Management
  <<package-management>>

  ;;; Major Keybinding
  <<keybinding>>

  ;;; Completion
  <<completion>>

  ;;; Editing
  <<editing>>

  ;;; Programming
  <<programming>>

  ;;; Projects
  <<projects>>

  ;; Applications
  <<applications>>

  ;;; External Services
  <<external-services>>

  ;;; User Interface
  <<user-interface>>
#+end_src

** Preamble
:PROPERTIES:
:header-args:emacs-lisp+: :noweb-ref preamble :noweb-sep "\n\n" :results silent
:END:

This section covers initialization that works best before anything else is
configured. For example, it is much easier to manage the "custom variables"
if they are not automatically tacked onto your initialization file. You can
set and load a separate file to keep it clean:

#+begin_src emacs-lisp
  (let ((my-custom-file (expand-file-name
			 "shoshimacs-custom.el" user-emacs-directory)))
    (unless (file-exists-p my-custom-file)
      (make-empty-file my-custom-file))
    (setq custom-file my-custom-file)
    (load custom-file))
#+end_src

*** shortcuts to this configuration document

I use a special variable to hold the path to this org file in its
project directory (under version control), then two functions to
quickly jump to it and reload it.

#+begin_src emacs-lisp
  (defvar *my-config* "~/projects/shoshimacs/shoshimacs.org"
    "Path to my main configuration file.")

  (defun my-configuration ()
    "Opens my configuration file in buffer."
    (interactive)
    (find-file *my-config*))

  (defun my-reload-config ()
    "Tangles and reloads a literate config with `org-babel-load-file'"
    (interactive)
    (org-babel-load-file *my-config*))
#+end_src

*** using a hostname to tweak the config for different machines

I use this config on several machines. Using their individual hostnames,
I can add conditional configuration for each of them as needed. As an
example, I use a smaller font size on an older low-resolution laptop.

Turns out Emacs has the function ~system-name~ to do this.
Unclear why I couldn't find it at the time I wrote the following function.
I'm leaving it as a historical example of foolish Elisp hackery. I couldn't
quickly figure out how to do it via Emacs itself, so I just shelled out
to the ~hostname~ command (which is probably not going to work on every
system)

#+begin_src emacs-lisp :tangle no
  (defun my-hostname ()
    "Helper function to determine on which host Emacs is starting."
    (string-trim (with-temp-buffer (shell-command "hostname" t) (buffer-string))))
#+end_src

* Package Management
:PROPERTIES:
:header-args:emacs-lisp+: :noweb-ref package-management :noweb-sep "\n\n" :results silent
:END:

I've been using [[https://github.com/radian-software/straight.el#start-of-content][straight.el]]
as my package manager since 2019 when I moved away from Spacemacs as my
main configuration for day-to-day work. While I definitely recommend it
as a flexible yet minimal package manager, it is targeted more to
experienced Emacs users. In my opinion, the main benefit of something
like straight is having all of the packages' source code cloned into
local repos on your machine. This makes it easier to fix bugs and make
contributions to the packages you're using.

This configuration will stick to packages available through the built-in
~package.el~ system. As of Emacs 28, this is everything in the ELPA and
non-gnu ELPA package repositories. 

** ELPA and Non-GNU ELPA

ELPA packages have their copyright assigned to the FSF, which is a requirement
for any code to be included into Emacs itself. ELPA packages are thus the
most likely to be merged into Emacs as a new feature. Some, like EMMS, are
likely to continue as "add-on" optional features only some users may choose.

Non-gnu ELPA is relatively new, and does not require copyright assignment
to the FSF. Packages are added to both repositories through the emacs-devel
mailing list and the maintainers there. It intends to extend the packages
available to the base Emacs installation while providing a bridge to inclusion
in ELPA or Emacs proper at some time in the future.

*** Add non-gnu ELPA to Emacs < 28

Emacs 28 is the first version to include non-gnu ELPA by default. Some
distributions may not yet have it as an available package.

#+name: add-nongnu-elpa
#+begin_src emacs-lisp
  (when (< emacs-major-version 28)
    (add-to-list 'package-archives '("nongnu" . "https://elpa.nongnu.org/nongnu/")))
#+end_src

** Installing Packages

~package.el~ provides the [[help:package-install][package-install]] command which can be used interactively
or from Lisp code like this configuration. If a package is already installed,
it won't try to install it again. When you install a package this way, Emacs will
add its name to [[help:package-selected-packages][package-selected-packages]]. Packages will also not be upgraded
when running a configuration that calls ~package-install~.

~M-x list-packages~ provides an interface to browse, install and upgrade packages
as well. Often, I will try something out by installing it through this UI, and
then add it to my config with ~package-install~ if I intend to keep it around.

I'll initialize the package functionality and refresh the contents to look for
updates, and ensure any additional archives are fetched. this may have a startup
impact, but i'm not concerned about that.

#+begin_src emacs-lisp
  (package-initialize)
  (package-refresh-contents)  ;; this will make internet requests on start up
#+end_src

** Packages not in the default repos

Any elisp package that is in Emacs's [[help:load-path][load-path]] can be ~require~'d and used.
~(add-to-list 'load-path (expand-file-name "some-package/" user-emacs-directory))~
is an example of putting the directory ~some-package/~ into the load path.

[[*minitest-emacs][The installation of minitest-emacs is an example in this config.]]

*** COMMENT xah-fly-keys

muscle memory has bound me to xah-fly-keys, and so i've decided to just
manually install it by cloning the repo and adding it to the

#+begin_src shell :var dir=(expand-file-name "xah-fly-keys" user-emacs-directory) :tangle no
  git clone https://github.com/xahlee/xah-fly-keys $dir
#+end_src

#+begin_src emacs-lisp
  (add-to-list 'load-path (expand-file-name "xah-fly-keys/" user-emacs-directory))
  (require 'xah-fly-keys)
  (xah-fly-keys-set-layout "qwerty")
  (xah-fly-keys t)
#+end_src

however, it is now in non-gnu elpa! so i don't need this anymore.

** Emacs 28 native compilation

[[info:elisp#Native Compilation][elisp#Native Compilation]]

This is a new feature in Emacs 28 that will compile all of the Elisp as native
machine code, rather than byte-code, which can result in major performance boosts.
Compilation will happen in the background and is logged to the
=*Async-native-compile-log*= buffer if you are curious. Mostly you shouldn't
have to worry about it, though you may see some compilation warnings at times.

#+begin_src emacs-lisp
  (when (and (functionp #'native-comp-available-p) (native-comp-available-p))
    (setq native-comp-always-compile t
	  package-native-compile t))
#+end_src

* Keybinding
:PROPERTIES:
:header-args:emacs-lisp+: :noweb-ref keybinding :noweb-sep "\n\n" :results silent
:END:

Keybindings are the key to playing Emacs like an instrument. no matter
what you choose, keep in mind that you can always bind keys to your
most commonly used commands to make things convienient.

I highly recommend creating a personal key map bound to a "leader key".
You initiate it with the leader, and then bind following key sequences
to commands you use. creating your own will make it easier to remember
and keep organized.

** xah-fly-keys

This is what I adopted to combat RSI. my muscle memory is tied into it
tightly right now. you may have other opinions about keybindings

#+begin_src emacs-lisp
  ;; these need to be set before requiring the package
  (setq xah-fly-use-control-key nil
	xah-fly-use-meta-key nil)
  (package-install 'xah-fly-keys)
  (require 'xah-fly-keys)
  (xah-fly-keys-set-layout "qwerty")
  (xah-fly-keys t)
#+end_src

i'm setting it up early in the config so that its keymaps are available
to modify / integrate with other packages.

*** adding some custom commands to xah maps
**** SPC 1 delete-other-windows

i want this often, feels as if i'm constantly wanting that other window
to go away.

#+begin_src emacs-lisp
  (define-key 'xah-fly-leader-key-map (kbd "1") #'delete-other-windows)
#+end_src

** with-map-defkey

This is an experiment in learning to write Elisp macros, all with the
result of being able to have a mini "dsl" to define keybindings in my
personal (or any given) keymap. The only thing being a macro really
does in this case is to avoid evaluating the ~pairs~ arguments. A tiny
bit of ergonomics essentially avoiding some parens and quotes ๐Ÿ˜‚

#+begin_src emacs-lisp
  (defmacro with-map-defkey (keymap leader &rest pairs)
    "Define a new KEYMAP with prefix key LEADER, and list of bindings in it."
    (declare (indent 2))
    `(progn
       (defvar ,keymap (make-sparse-keymap))
       (define-prefix-command (quote ,keymap))
       (global-set-key (kbd ,leader) ,keymap)
       (mapc (lambda (pair)
	       (define-key ,keymap
		 (kbd (if (numberp (car pair)) (number-to-string (car pair))
			(symbol-name (car pair))))
		 (cadr pair)))
	     (quote ,(seq-partition pairs 2)))))

  (with-map-defkey my-key-map "M-m"
    1 delete-other-windows
    a apropos
    b consult-buffer
    c my-configuration
    d embark-act
    e eshell
    f find-file
    g magit
    h info
    i consult-imenu
    j describe-function
    k describe-variable
    n tab-next
    p project-prefix-map
    s consult-git-grep
    t consult-theme
    w which-key-mode
    <f1> my-reload-config)
#+end_src

** COMMENT bind-map

there is a package available in nongnu elpa that offers a macro that
sort of generalizes the leader key idea. it also hooks into the evil package
a bit to allow definition of bindings over different "states" that your
editor might be in, like command or insert mode in xah-fly-keys. its fairly
geared toward evil in that way, so i'm not sure how it will interact, but let us
find out. the other option would be to make a simple macro of our own to hook
into leader key maps.

#+begin_src emacs-lisp
  (package-install 'bind-map)

  (bind-map my-keymap
    :keys ("M-m"))

  (bind-map my-org-map
    :keys ("M-m m")
    :major-modes (org-mode)
    :bindings ("," #'org-insert-structure-template))

  (bind-map my-elisp-map
    :keys ("M-m m")
    :major-modes (emacs-lisp-mode
		  lisp-interaction-mode)
    :bindings ("e" #'edebug-instrument-function))

  (bind-map my-ruby-map
    :keys ("M-m m")
    :major-modes (ruby-mode)
    :bindings ("d" #'ruby-beginning-of-defun))

  (bind-map-set-keys my-keymap
    "b" #'consult-buffer
    "c" #'my-config
    "g" #'magit
    "j" #'consult-grep
    "k" #'consult-imenu
    "n" #'tab-next
    "t" #'consult-theme)
#+end_src

* Completion
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref completion :noweb-sep "\n\n" :results silent
:END:

Completion is a huge part of my experience using Emacs. I have been on
an evolving journey of from the basic type of terminal tab completion 
to spaceship level UI implemented as almost a sub-application in Emacs.

This configuration is aiming at using a new crop of completion enhancements
that tie into Emacs's native completion API. This is a more modular approach
that allows a sort of composition of extensions to completion behavior and
its appearance in the user interface.

** Two kinds of completion

I want to point out that there are two distinct but similar features
both grouped under the concept of "completion". The first is *Minibuffer*
completion. Any time you use the minibuffer to enter commands or arguments,
there is a completion system available to help you enter text there.
The second is *Buffer* completion, offering candidates for text you are
typing in any buffer. Code completion provided by a language server
is one example. In vanilla Emacs, you get [[info:emacs#Symbol Completion][Symbol Completion]]
for free, since Emacs itself is a running Lisp process with knowledge of
all the defined symbols in the system.

I've been confused by this in the past, because the features are so similar.
However, completing text in an arbitrary buffer really depends on context,
and it is much more complex than completing commands and arguments that are
appropriate to a specific situation.

** Emacs completion styles

Emacs has a quite sophisticated way of selecting candidates for completion.
You can read about them here: [[info:emacs#Completion Styles][emacs#Completion Styles]]

I've grown used to the =flex= style of completion where typing
=pr/s/sho.o= at the find file prompt expands to
=projects/shoshimacs/shoshin-config.org=. There are other alternatives
and you can even write your own. The ~completion-styles~ is a list of
all the styles you'd like to use. It starts at the front, and if no matches
are found, moves to the next style of completion. In this config, I just
added =flex= to the front of the default completion styles.

#+begin_src emacs-lisp
  (setq completion-styles '(flex basic partial-completion emacs22)
	completion-cycle-threshold 3
	tab-always-indent 'complete)
#+end_src

~completion-cycle-threshold~ defines when you want to just cycle through
alternatives on each <TAB> (or whatever key you use) rather than presenting
options. Setting it to 3 means if my options are "FOO, FOP, FOR" or less,
hitting complete will change FOO->FOP, FOP->FOR, FOR->FOO.

~tab-always-indent~ changes the behavior of the TAB key:

#+begin_quote
  If โ€˜completeโ€™, TAB first tries to indent the current line, and if the line
  was already indented, then try to complete the thing at point.
#+end_quote

** [[info:consult#Top][consult]] - Consulting [[info:elisp#Minibuffer Completion][completing-read]]

consult offers enhanced completion similar to ivy and helm, but with the
built in completing read functionality of the minibuffer.

#+begin_src emacs-lisp
  (package-install 'consult)
#+end_src

main entry point would be ~consult-buffer~. however, there are many consult
commands that can enhance any completing read function.

*** "Virtual Buffers"

it introduces this concept of "Virtual Buffers", but i'm not certain what
it means. consult "supports ... narrowing to the virtual buffer types".

perhaps a Virtual Buffer is a "grouping" of actual Emacs buffers or "things"
that can be materialized in a buffer. For example, I can ~consult-buffer~
and press ~m SPC~ to narrow the "buffer list" to any bookmarks.

*** consult keybindings

#+begin_src emacs-lisp
  (global-set-key (kbd "C-x b") #'consult-buffer)
  (define-key xah-fly-leader-key-map (kbd "f") #'consult-buffer)
  (define-key xah-fly-command-map (kbd "n") #'consult-line)
#+end_src

*** consult-themes

i had a bit of a mess with it at first, because i'd implemented my own
solution to a quirk of theme loading. enabling themes is additive,
and can cause unexpected results. so i added [[info:elisp#Advising Functions][advice]]
to ~load-theme~ to automatically disable the old one before enabling
the new.

it seems like ~consult-theme~ does this as well. additionally, as
it will preview the theme as you are narrowing the selection. i did not
expect this behavior and it got all kinds of wonky. the manual has a
nice example of delaying the theme-switch-preview since it is slow.
this way you can scroll / narrow your list of themes without the colors
changing with every keypress.

#+begin_src emacs-lisp
  (with-eval-after-load 'consult
    (consult-customize consult-theme :preview-key '(:debounce 0.5 any))
    (setq consult-themes my-chosen-themes))
#+end_src

I set a special list of ~my-chosen-themes~ but sometimes I want to turn it
off. Its nice to have a filtered list most times, but sometimes I just want
all of them in the ~consult-themes~ list.

#+begin_src emacs-lisp
  (defun my-show-all-themes ()
    (interactive)
    (setq consult-themes nil))
#+end_src

*** TODO consult-project-buffer

how do project buffers get filtered? i'm seeing buffers assigned to a project
that in my mind, shouldn't be.

looks like it interfaces with ~project-switch-to-buffer~ which has its own
logic about which project a buffer belongs to. some of the mistakes i was seeing
earlier were simply due to starting a repl in a particular directory.

it appears that "special" buffers may get assigned to a particular project as
well. for example the EWW buffer is part of a project, but it is unclear as
to why. appears likely to have to do with the behavior of the ~default-directory~
variable which is buffer-local.

i may want to figure out ways to mark "special" buffers as having a non-project
default-directory set so they don't show up, or just filter them out if it
becomes annoying. i'm accustomed to ~perspectives~ provided by a MELPA package
that hooked into ~projectile~'s project definitions. it would keep a list of
perspective-local buffers where the perspective was tied to a project.

** embark

#+begin_src emacs-lisp
  (package-install 'embark)
  (package-install 'embark-consult)
  (global-set-key (kbd "C-;") #'embark-act)
  (setq prefix-help-command #'embark-prefix-help-command)
#+end_src

** marginalia

#+begin_src emacs-lisp
  (package-install 'marginalia)
  (marginalia-mode)
#+end_src

** vertico

#+begin_src emacs-lisp
  (package-install 'vertico)
  (setq minibuffer-prompt-properties
	'(read-only t cursor-intangible t face minibuffer-prompt))
  (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
  (setq read-extended-command-predicate
	#'command-completion-default-include-p)
  (setq enable-recursive-minibuffers t)
  (vertico-mode)
#+end_src

*** vertico-directory

i'd like to emulate the behavior in ~find-file~ that i'm used to from Ivy.
basically, when i press DEL it should act normally until i hit a directory
boundary, then it should jump up a dir with the following press.

this is implemented with the ~vertico-directory~ extension.

#+begin_src emacs-lisp
  (require 'vertico-directory)
  (define-key vertico-map (kbd "RET") #'vertico-directory-enter)
  (define-key vertico-map (kbd "DEL") #'vertico-directory-delete-char)
  (define-key vertico-map (kbd "M-DEL") #'vertico-directory-delete-word)
  (define-key vertico-map (kbd "M-j") #'vertico-quick-insert)
#+end_src

** corfu

#+begin_src emacs-lisp
  (package-install 'corfu)
  (setq corfu-auto t
	corfu-cycle t
	corfu-quit-no-match t)
  (global-corfu-mode t)
#+end_src

*** corfu-terminal enables in terminal interface

#+begin_src emacs-lisp
  (package-install 'corfu-terminal)
  (unless (display-graphic-p)
      (corfu-terminal-mode +1))
#+end_src
** which-key

which-key is an excellent package that helps provide guide posts for command
discoverability. many key commands in Emacs are in sequences. for example,
~org-babel-tangle~ is ~C-c C-v t~. as a user, the commands you use often
become muscle memory, or you map them to something more convienient. but
sometimes you know there is some command to do what you want but you don't
quite remember what the sequence is. or perhaps you're in a new major-mode
and you know there must be several handy commands under ~C-c~. ~which-key~
pops up a buffer with a listing of all the possible continuations from where
you are in a key sequence. for that reason, it can be useful at times even
for experienced users.

this config installs which-key, but does not activate it automatically.
some advice from Daniel Mendler (consult author) suggested that limiting the
amount Emacs pops things up automatically is a better user experience.
which-key is just a toggle away when needed, and there is also [[*embark][embark]]
which has a command to show prefix key continuations.

#+begin_src emacs-lisp
  (package-install 'which-key)
  (which-key-mode)
#+end_src

* Editing
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref editing :noweb-sep "\n\n" :results silent
:END:
** [[info:emacs#Matching][electric pair mode]]

I've been using smartparens -> (bookmark-jump "smartparens package") in my
main config. electric pair mode does some of what smartparens does out of
the box. what i'm missing is the generalized ~sp-hybrid-slurp~ or
whatever it was called. but using the built in is good for now. further
config might get what i want with vanilla built ins.

#+begin_src emacs-lisp
  (electric-pair-mode)
#+end_src

** markdown mode
#+begin_src emacs-lisp
  (package-install 'markdown-mode)
#+end_src

** org mode
*** jump to top level parent heading

a friend was wanting this functionality, so i dug around in the org functions
to find one i could build a command out of:

#+begin_src emacs-lisp
  (defun my-org-top-level-heading ()
    (interactive)
    (let ((moo 1))
      (while moo (setq moo (org-up-heading-safe)))))
#+end_src

~org-up-heading-safe~ is designed not to throw an error and returns the level
of the headline found (as it goes up) or nil if no higher level is found.
this just uses a dumb while loop to call it until it returns nil, which should
get us where we are looking to go ๐Ÿ˜ƒ

*** exporting
#+begin_src
  (require 'ox-md)
#+end_src
**** htmilze

this seems to be required to fontify source blocks

#+begin_src emacs-lisp
  (package-install 'htmlize)
#+end_src

*** babel

#+begin_src emacs-lisp
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((emacs-lisp . t)
     (lisp . t)
     (org . t)
     (plantuml . t)
     (ruby . t)
     (shell . t)))
#+end_src

*** org-tree-slide

This is a simple presentation mode for org documents that uses narrowing
to turn an org tree into slides. I've used it for many presentations,
including some at EmacsConf. Its great for doing presentation run throughs,
since you can edit at the same time you're using the slides.

#+begin_src emacs-lisp
  (package-install 'org-tree-slide)
#+end_src

There are many settings for how it presents available, but I usually
just set them on the fly, depending on the presentation, rather than
in my main config.

** recentf-mode

this tracks recently operated on files (by default) and enables quick selection
from them in various Emacs menus. [[*\[\[info:consult#Top\]\[consult\]\] - Consulting \[\[info:elisp#Minibuffer Completion\]\[completing-read\]\]][consult]] hooks into it as well.

#+begin_src emacs-lisp
  (recentf-mode)
#+end_src

** whitespace, tabs, and spaces

#+begin_src emacs-lisp
  (setq indent-tabs-mode nil
	show-trailing-whitespace t)
#+end_src

** crdt - buffer sharing

#+begin_src emacs-lisp
  (package-install 'crdt)
#+end_src

* Programming
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref programming :noweb-sep "\n\n" :results silent
:END:
** Languages
*** Common Lisp

The ~slime~ package v2.26.1 in the nongnu repo seems to have an issue with
emacs 28.1 (?) so I'm installing it from source.

#+begin_src emacs-lisp
  (defvar slime-repo "https://github.com/slime/slime")
  (defvar slime-src-dir "~/src/slime")

  (unless (file-directory-p slime-src-dir)
    (shell-command
     (format "git clone %s %s" slime-repo slime-src-dir)))

  (add-to-list 'load-path slime-src-dir)

  (require 'slime)

  (setq inferior-lisp-program "sbcl")
#+end_src

*** Javascript

#+begin_src emacs-lisp
  (package-install 'json-mode)
#+end_src

*** Ruby
**** Linting

The Emacs ~ruby-mode~ defines a backend for flymake, which use rubocop by default
if it is available. If it isn't, it will try ~ruby -wc~ which will check syntax
and warnings in the file. If a project is NOT using rubocop, or if it is say...
running in a container, the flymake backend function will need to be hacked on
a bit. A few config variables are available, primarily for rubocop.

  ruby-flymake-use-rubocop-if-available t
  ruby-rubocop-config ".rubocop.yml"

#+begin_src emacs-lisp
  (add-hook 'ruby-mode-hook #'flymake-mode)
#+end_src

**** inf-ruby

inf-ruby provides an interface to the various REPLs available in the Ruby-verse
its currently provided by non-gnu ELPA:

#+begin_src emacs-lisp
  (package-install 'inf-ruby)
#+end_src

I prefer pry when its available, and it generally is on my system:

#+begin_src emacs-lisp
  (when (executable-find "pry")
    (setq inf-ruby-default-implementation "pry"))
#+end_src

**** TODO minitest-emacs

this is a package i happen to maintain, and i would like to get it into non-gnu
elpa. however, at the moment i must install from source:

#+begin_src emacs-lisp
  (defvar minitest-emacs-repo "https://github.com/arthurnn/minitest-emacs.git")
  (defvar minitest-emacs-src-dir "~/src/minitest-emacs")

  (unless (file-directory-p minitest-emacs-src-dir)
    (shell-command
     (format "git clone %s %s" minitest-emacs-repo minitest-emacs-src-dir)))

  (add-to-list 'load-path minitest-emacs-src-dir)
  (require 'minitest)
#+end_src

**** haml mode

#+begin_src emacs-lisp
  (package-install 'haml-mode)
#+end_src

** Dev Docs
#+begin_src emacs-lisp
  (package-install 'devdocs)
#+end_src

** [[info:flymake#Top][Flymake]]

#+begin_src emacs-lisp
  (with-eval-after-load 'flymake
    (define-key flymake-mode-map (kbd "M-n") 'flymake-goto-next-error)
    (define-key flymake-mode-map (kbd "M-p") 'flymake-goto-prev-error))
#+end_src

** PlantUML

#+begin_src emacs-lisp
  (defvar plantuml-mode-repo "https://github.com/skuro/plantuml-mode")
  (defvar plantuml-mode-src-dir "~/src/plantuml-mode")

  (unless (file-directory-p plantuml-mode-src-dir)
    (shell-command
     (format "git clone %s %s" plantuml-mode-repo plantuml-mode-src-dir)))

  (add-to-list 'load-path plantuml-mode-src-dir)
  (require 'plantuml-mode)
  (customize-set-value 'plantuml-default-exec-mode 'executable)
#+end_src

* Projects
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref projects :noweb-sep "\n\n" :results silent
:END:

** project.el

~project.el~ is the built in project management tool. previously, I've used
~projectile~, which is great, but some of the more recent tools (like eglot)
use the built in ~project.el~ api for their features.

I've got the ~project-prefix-map~ on the key =p= in [[*with-map-defkey][my-key-map]].
Interestingly, in order to refer to a a prefix map with a quoted symbol
like you would a command, the keymap must be in the symbol's function
definition place. The ~project.el~ package doesn't do this, which is normally
done with ~define-prefix-command~ (in my experience). In order to make
this prefix map work correctly with the ~with-map-defkey~ macro, we set
the ~symbol-function~ place on the ~project-prefix-map~ to be the value,
the actual keymap structure, of the ~project-prefix-map~ symbol. ๐Ÿ˜ต

#+begin_src emacs-lisp
  (fset 'project-prefix-map project-prefix-map)
#+end_src

** version control
*** magit

its the best! ๐Ÿช„

#+begin_src emacs-lisp
  (package-install 'magit)
#+end_src
* Applications
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref applications :noweb-sep "\n\n" :results silent
:END:

** EMMS

Emacs MultiMedia System.

#+begin_src emacs-lisp
  (require 'emms-setup)
  (emms-all)
  (setq emms-player-list '(emms-player-mpv))
  (setq emms-source-file-default-directory "~/Music/")
  (add-to-list 'emms-tag-editor-tagfile-functions '("wav" . emms-tag-tracktag-file))
#+end_src

** erc

#+begin_src emacs-lisp
    (setq erc-server "irc.libera.chat"
          erc-nick "shoshin"
          erc-autojoin-channels-alist '(("libera.chat" "#emacs" "#emacsconf" "#lispgames"))
          erc-interpret-mirc-color t
          erc-hide-list '("JOIN" "PART" "QUIT"))
#+end_src

* External Services
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref external-services :noweb-sep "\n\n" :results silent
:END:

Packages that enable communication via HTTP or connect with external APIs or other
resources outside of Emacs and/or the local machine.

** plz - http library

this is an http library that intends to solve some of the "pain points" of url.el.
i ran into some of them trying to download and install the Victor Mono font used
by my configuration. the downside of ~plz~ is that it is dependent on ~curl~, rather
than being pure elisp. however, this is a non-issue for me, especially since my
use case had devolved into using ~make-process~ to call ~wget~ and then implement
a "callback" with a process sentinel. kinda neat, but maybe too much.

#+begin_src emacs-lisp
  (package-install 'plz)
#+end_src

the sourcehut package in this config also depends on ~plz~

** sourcehut
there's a new package in GNU ELPA for some basic interaction with the sr.ht http api.
i'm interested to try it out since i still pay for the account, plus the forge is
free software and could be self-hosted if it comes to it.

it also depends on ~plz~ which is another new package providing a nicer API for
HTTP requests.

#+begin_src emacs-lisp
  (package-install 'srht)
  (setq srht-username "shoshin")
#+end_src

an API token is stored in my ~.authinfo~ file.

** paste.cicadas.surf

#+begin_src emacs-lisp
  (defun read-file-into-string (f)
    (with-temp-buffer
      (insert-file-contents f)
      (buffer-string)))

  (setq cicadas-paste-pw (read-file-into-string "~/.bepasty-key"))

  (defun cicadas-paste-current-buffer () 
    (interactive)
    (let ((tmpfile
	   (concat "/tmp/" (buffer-name))))
      (write-region nil nil tmpfile)
      (let ((url 
	     (cl-first (last (butlast
			   (split-string
			    (shell-command-to-string
			     (format "pastebinit -i '%s' -p %s -t '%s' -f text/plain"
				     tmpfile
				     cicadas-paste-pw
				     (buffer-name)))
			    "\n"))))))
	(delete-file tmpfile)
	(kill-new url)
	(message "Pasted to %s (on kill ring)" url))))
#+end_src

** restclient

#+begin_src emacs-lisp
  (defvar restclien-repo "https://github.com/pashky/restclient.el.git")
  (defvar restclient-dir "~/src/restclient.el")

  (unless (file-directory-p restclient-dir)
    (shell-command
     (format "git clone %s %s" restclient-repo restclient-dir)))

  (add-to-list 'load-path restclient-dir)
  (require 'restclient)
#+end_src

* UI
:PROPERTIES:
:header-args:emacs-lisp: :noweb-ref user-interface :noweb-sep "\n\n" :results silent
:END:
** basic Emacs UI tweaks

#+begin_src emacs-lisp
    (when (display-graphic-p)
      (scroll-bar-mode -1)
      (fringe-mode '(8 . 0))
      (menu-bar-mode -1)
      (tool-bar-mode -1))
#+end_src

** darkroom - distraction free writing

the notes suggest using ~darkroom-tentative-mode~ which auto switches
depending on the window layout currently in use.

#+begin_src emacs-lisp
  (package-install 'darkroom)
#+end_src

** Fonts 
For code, I've grown fond of Victor Mono.

#+begin_src emacs-lisp
  (let ((size (if (or (equal (system-name) "zebes") (equal (system-name) "ridley")) 16 12)))
   (set-frame-font (format "Victor Mono-%s" size)))
#+end_src


*** COMMENT Attempt to install the font via Emacs, url.el and ~make-process~

in the end, this "works" but isn't very useful. the font cache needs to be updated
before Emacs runs for the font to register anyway. This will instead be implemented
as a shell script that can be run as a pre-req before initializing emacs.

however, i did learn a bit about [[info:elisp#Sentinels][Sentinels]] they are kinda neat.

#+begin_src emacs-lisp
  ;; this actually doesn't work right, because the process unzipping starts
  ;; before the file is completely written i think.
  (url-retrieve "https://rubjo.github.io/victor-mono/VictorMonoAll.zip"
		#'victor-mono-download-callback)

  (make-process :name "getting victor mono"
		:buffer "*Download Victor Mono*"
		:command '("wget" "--output-document=/home/shoshin/.fonts/VictorMonoAll.zip"
			   "https://rubjo.github.io/victor-mono/VictorMonoAll.zip")
		:sentinel #'victor-mono-download-callback)

  (defun victor-mono-download-callback (process event)
    (if (string-equal "finished\n" event)
	(let ((default-directory  "~/.fonts/VictorMono"))
	  (unless (file-directory-p default-directory)
	    (make-directory default-directory))
	  (make-process :name "unzipping victor mono"
			:buffer "*Unzip Victor Mono*"
			:command `("unzip" ,(expand-file-name "~/.fonts/VictorMonoAll.zip"))
			:sentinel #'victor-mono-sentinel))))

  (defun victor-mono-sentinel (process event)
    (print event)
    (if (string-equal "finished\n" event)
	(progn (message "works!")
	       (make-process :name "run fc-cache" :command '("fc-cache")))))
#+end_src
** Highlights
*** [[help:global-hl-line-mode][global-hl-mode]]
i enjoy having the current line highighted as a visual cue. 

#+begin_src emacs-lisp
  (global-hl-line-mode t)
#+end_src

can be toggled with <leader> l 2 

*** COMMENT lin-global-mode 
Make `hl-line-mode' more suitable for selection UIs

add other hooks to ~lin-mode-hooks~

#+begin_src emacs-lisp
  (setq my-lin-mode-hooks
	'())
#+end_src

#+begin_src emacs-lisp
  (package-install 'lin)
  (require 'lin)
  (setq lin-face 'lin-blue)
  (mapc (lambda (e) (cl-pushnew e lin-mode-hooks)) my-lin-mode-hooks)
  (lin-global-mode)
#+end_src
** Themes
*** theme packages / installation

to keep it concise, i'll define a special variable to hold the theme packages
i'd like to install anytime this config is loaded on a system where they are
not there. then i can ~mapc~ #'package-install over the list of themes.

#+begin_src emacs-lisp :noweb-ref defvars
  (defvar my-themes-to-install
    '(cyberpunk-theme dracula-theme nano-theme ef-themes)
    "List of themes to install when loading shoshimacs config.")
#+end_src

#+begin_src emacs-lisp
  (mapc #'package-install my-themes-to-install)
#+end_src

*** chosen theme list and random loading on init

i generally haven't used built-in themes much, but there's a few i like,
and the ones i specifically install i'd like to keep in a list of "chosen"
ones. i can load one at random if i please, and perhaps provide it as
candidates to ~consult-themes~.

#+begin_src emacs-lisp :noweb-ref defvars
  (defvar my-chosen-themes
    '(cyberpunk dichromacy dracula leuven modus-operandi modus-vivendi
                nano-dark nano-light tango tango-dark
                ef-day ef-dark ef-light ef-night
                ef-autumn ef-spring ef-summer ef-winter
                ef-deuteranopia-dark ef-deuteranopia-light)
    "List of themes I prefer for narrowing and random selection.")
#+end_src

*** autothemer

autothemer is a dependency of some nice themes, and a great tool
for theme development.

#+begin_src emacs-lisp
  (package-install 'autothemer)
#+end_src

** SVG Screenshot

#+begin_src emacs-lisp
(defun screenshot-svg ()
  "Save a screenshot of the current frame as an SVG image.
Saves to a temp file and puts the filename in the kill ring."
  (interactive)
  (let* ((filename (make-temp-file "Emacs" nil ".svg"))
         (data (x-export-frames nil 'svg)))
    (with-temp-file filename
      (insert data))
    (kill-new filename)
    (message filename)))
#+end_src

** windresize

#+begin_src emacs-lisp
  (package-install 'windresize)
#+end_src