~thecatster/.emacs.d

.emacs.d/exwm.org -rw-r--r-- 40.1 KiB
2c969f2eDaniil Rose Update gitignore to include elpa 7 months ago
                                                                                
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
#+TITLE: EXWM Config
#+AUTHOR: Daniel Rose (The Catster)
#+DESCRIPTION: An EXWM configuration to turn Emacs into the ultimate desktop environment.
#+STARTUP: overview
#+LANGUAGE: en
#+OPTIONS: num:nil
#+ATTR_HTML: :style margin-left: auto; margin-right: auto;
#+PROPERTY: header-args :tangle lisp/catsters-exwm.el :mkdirp yes

Emacs is one of my favorite window managers. Sadly, Emacs and EXWM are
single threaded, but creative solutions and patience have mostly
alleviated this problem for me.

In reality these days (circa June 2021), I've had little issues with
most applications in EXWM. Sure, the occassional thing will freeze,
but I'll go out and say 90% of the time EXWM is a charm to use.

Due to *the problem,* I follow the general idea of opening another
Emacs instance for the larger programs I need to run that may
crash. Heavy programs get their own Emacs instance as well.

So what is a "heavy" program in my experience?
- Literate files of large size (and I mean large size)
- +Zoom+ (not true anymore, as I use Zoom in the browser)
- Literally any Electron app (I don't trust any of them, and they take
  way too many resources)

The above comment about Electron applications is a bit of an
exaggeration, but I would still be careful with them.

If you're unsure if a program should be "Emacsed" (as in put in
another Emacs instance) I go by this rule of thumb:
- Is it an Electron app? Consider containerising it
- Is it an app that may require frequent UI updates? Container it
- Unsure? Container it to play it safe, and then when not working on
  critical material try it without

* Server
If I'm using EXWM, that means I don't have the systemd service on for Emacs. Start the server:

#+BEGIN_SRC emacs-lisp
(require 'server)
(unless (server-running-p)
   (server-start))
#+END_SRC
* Scripts
The base configuration files are the same across all machines (or
deployed accordingly), and provide basic configurations for EXWM
launching and bash.

*** start-exwm.sh
This is where the action happens; EXWM is launched here, as well as
all the programs I need to initiate at start and whatever else. This
is the same for every machine.

#+BEGIN_SRC sh :tangle exwm/start-exwm.sh :shebang #!/bin/sh
## Run site init scripts. Usually not necessary.
if [ -d /etc/X11/xinit/xinitrc.d ] ; then
    for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
        [ -x "$f" ] && . "$f"
    done
    unset f
fi

. ~/.emacs.d/exwm/exwm_screen_layout
~/.emacs.d/exwm/exwm_xrandr.bash

# Set default cursor, instead of the typical "x."
xsetroot -cursor_name left_ptr

# Start NetworkManager applet for easier WiFi access and preventing network-manager-config from crashing (why does that happen?)
nm-applet &

# Instead of launching multiple Emacsen, I already have a server
# running. Use that instead as the EDITOR.
export VISUAL=emacsclient
export EDITOR="$VISUAL"

# Allows scrolling in GTK 3 apps
export GDK_CORE_DEVICE_EVENTS=1

exec dbus-launch emacs --eval "(progn (add-to-list 'load-path \"~/.emacs.d/lisp/\") (require 'catsters-exwm))" --eval "(dw/start-panel)" --eval "(require 'exwm)" -f exwm-enable
#+END_SRC

*** exwm_screen_layout
This is where screens and layout is defined, different for every
machine. This is used a default, based on =ingenuity=. Copy this file
without the =.example= ending and edit accordingly!

#+BEGIN_SRC sh :tangle exwm/exwm_screen_layout.example :shebang #!/bin/sh
# X11 screens (xrandr graphics outputs) I want to use. Names and values are from the output of /usr/bin/xrandr
# They can be ordered in any way you want. The leftmost available screen will be primary, unless overridden
# by X11_SCREEN_PREFERRED and that screen is available.
# The primary screen is the default screen used for EXWM workspaces.
# It will also be sorted first in Xinerama and RANDR geometry requests.
export X11_SCREEN_LIST="HDMI1 HDMI2 eDP1 DP1-1"
# xrandr --mode for each screen in X11_SCREEN_LIST
export X11_SCREEN_MODE_LIST="1920x1080 1920x1080 1920x1080 1920x1080"
# xrandr --rate for each screen in X11_SCREEN_LIST
export X11_SCREEN_RATE_LIST="60.00 60.00 60.00 60.00"
# How screens are arranged from left to right. Vertical order, and "--same-as" not yet implemented.
export X11_SCREEN_ORDER_LIST="HDMI1 HDMI2 eDP1 DP1-1"
# X11 screens (graphics outputs) that should always be explicitly turned off, if available.
export X11_SCREEN_DISABLED_LIST="VIRTUAL1"
# Primary X11 screen, if available
export X11_SCREEN_PREFERRED="eDP1"
# If X11_SCREEN_USE_ALL_AVAILABLE="yes" then use all available screens in X11_SCREEN_LIST:
# - X11_SCREEN_PREFERRED is primary, if available
# - If X11_SCREEN_PREFERRED is unavailable, primary is first available screen in X11_SCREEN_LIST.
# Otherwise use only one:
# - X11_SCREEN_PREFERRED if available
# - If X11_SCREEN_PREFERRED is unavailable then use first available screen in X11_SCREEN_LIST.
export X11_SCREEN_USE_ALL_AVAILABLE="yes"
# Argument value for "xrandr --dpi", i.e. Dots Per Inch. This is for the X11 DISPLAY, i.e. used for all screens.
export X11_DISPLAY_DPI=96
# List of pairs "workspace-number screen"
# Used to construct exwm-randr-workspace-monitor-plist in emacs.
# If a screen in this list is unavailable, the workspace will be mapped to the primary screen.
# When using the dock, use the following:
export EXWM_WORKSPACE_LIST="0 eDP1 1 DP1-1 2 eDP1 3 DP1-1 4 eDP1 5 DP1-1 6 eDP1 7 DP1-1 8 eDP1 9 DP1-1"
# When using the HDMI port, use the following:
export EXWM_WORKSPACE_LIST="0 eDP1 1 HDMI1 2 eDP1 3 HDMI1 4 eDP1 5 HDMI1 6 eDP1 7 HDMI1 8 eDP1 9 HDMI1"
#+END_SRC

*** exwm_xrandr.bash
This is the same for all. This script is what actually enables the
setup of screens.

#+BEGIN_SRC sh :tangle exwm/exwm_xrandr.bash :shebang #!/bin/sh
# Execute xrandr with appropriate arguments, to configure the X11 screens for EXWM
# For specification see: ".xinitrc.exwm"

grep="grep"
awk="awk"
xrandr="xrandr"

if [[ ! -z "${X11_SCREEN_LIST}" ]]; then
    declare -a x11_screens_a=(${X11_SCREEN_LIST})
    declare -a x11_screen_order_a=(${X11_SCREEN_ORDER_LIST})
    declare -a x11_screens_disabled_a=(${X11_SCREEN_DISABLED_LIST})
    declare -a x11_screen_mode_a=(${X11_SCREEN_MODE_LIST})
    declare -a x11_screen_rate_a=(${X11_SCREEN_RATE_LIST})
    declare -a x11_connected_screens_a=($(${xrandr} | ${grep} " connected " | ${awk} '{ print $1 }'))
    x11_connected_screens_list=" ${x11_connected_screens_a[*]} "

    x11_available_screens_a=()
    x11_available_screen_modes_a=()
    x11_available_screen_rates_a=()
    for ((i=0;i<${#x11_screens_a[@]};++i)); do
        screen=${x11_screens_a[$i]}
        if [[ ${x11_connected_screens_list} =~ " $screen " ]]; then
            x11_available_screens_a+=($screen)
            x11_available_screen_modes_a+=("${x11_screen_mode_a[$i]}")
            x11_available_screen_rates_a+=("${x11_screen_rate_a[$i]}")
        fi
    done

    x11_available_screen_order_a=()
    for ((i=0;i<${#x11_screen_order_a[@]};++i)); do
        screen=${x11_screen_order_a[$i]}
        if [[ ${x11_connected_screens_list} =~ " $screen " ]]; then
            x11_available_screen_order_a+=($screen)
        fi
    done

    x11_available_disabled_screens_a=()
    for screen in ${x11_screens_disabled_a[@]}; do
        if [[ ${x11_connected_screens_list} =~ " $screen " ]]; then
            x11_available_disabled_screens_a+=($screen)
        fi
    done

    if [[ ! -z ${x11_available_screens_a} ]]; then
        x11_available_screens_list=" ${x11_available_screens_a[*]} "
        if [[ ! -z ${X11_SCREEN_PREFERRED} ]] && [[ ${x11_available_screens_list} =~ " ${X11_SCREEN_PREFERRED} " ]]; then
            x11_primary_screen=${X11_SCREEN_PREFERRED}
        else
            x11_primary_screen=${x11_available_screens_a[0]}
        fi

        # Start building xrandr command line
        for ((i=0;i<${#x11_available_screens_a[@]};++i)); do
            if [[ "${x11_available_screens_a[$i]}" == "${x11_primary_screen}" ]]; then
                x11_primary_mode=${x11_available_screen_modes_a[$i]}
                x11_primary_rate=${x11_available_screen_rates_a[$i]}
                break
            fi
        done
        declare -a xrandr_args_a=("--dpi" "${X11_DISPLAY_DPI}" "--output" "${x11_primary_screen}" "--primary"
                                  "--mode" "${x11_primary_mode}"
                                  "--rate" "${x11_primary_rate}")

        for ((i=0;i<${#x11_available_disabled_screens_a[@]};++i)); do
            xrandr_args_a+=("--output" "${x11_available_disabled_screens_a[$i]}" "--off")
        done

        if [[ "${X11_SCREEN_USE_ALL_AVAILABLE}" == "yes" ]]; then
            # Add remaining available screens, except the primary screen
            for ((i=0;i<${#x11_available_screens_a[@]};++i)); do
                screen="${x11_available_screens_a[$i]}"
                if [[ "${screen}" != "${x11_primary_screen}" ]]; then
                    xrandr_args_a+=("--output" "${screen}"
                                    "--mode" "${x11_available_screen_modes_a[$i]}"
                                    "--rate" "${x11_available_screen_rates_a[$i]}")
                fi
            done
        else
            # Disable all remaining available screens except the primary screen
            for ((i=0;i<${#x11_available_screens_a[@]};++i)); do
                screen="${x11_available_screens_a[$i]}"
                if [[ "${screen}" != "${x11_primary_screen}" ]]; then
                    xrandr_args_a+=("--output" "${screen}" "--off")
                fi
            done
        fi
        echo ${xrandr} ${xrandr_args_a[*]}
        ${xrandr} ${xrandr_args_a[*]}
        # Place screens relative to each other, if needed
        if [[ "${X11_SCREEN_USE_ALL_AVAILABLE}" == "yes" ]] && [[ ${#x11_available_screen_order_a[@]} -gt 1 ]]; then
            declare -a xrandr_screen_order_args_a
            left_screen=${x11_available_screen_order_a[0]}
            for ((i=1;i<${#x11_available_screen_order_a[@]};++i)); do
                screen="${x11_available_screens_a[$i]}"
                xrandr_screen_order_args_a+=("--output" "${screen}" "--right-of" "${left_screen}")
                left_screen=${screen}
            done
            echo ${xrandr} ${xrandr_screen_order_args_a[*]}
            ${xrandr} ${xrandr_screen_order_args_a[*]}
        fi
    else
        echo "No available X11 screens in X11_SCREEN_LIST"
    fi
else
    echo "X11_SCREEN_LIST is empty"
fi
#+END_SRC

*** eshell
Below is my eshell config, including aliases and profile.

#+BEGIN_SRC sh :tangle eshell/alias
alias e find-file $1
alias edit find-file $1
alias d dired $1
alias gs magit-status-here
alias gd magit-diff-unstaged
alias sd sync-dotfiles
alias ud update-dotfiles
alias us update-system
alias up update-profiles $*
alias ffo find-file-other-window $1
alias guix-clean sudo guix gc -d 7d
#+END_SRC

#+BEGIN_SRC sh :tangle eshell/profile

#+END_SRC
* xrandr
Credit to johanwiden!

Support for multiple monitors, and plugging and unplugging of monitors.

If you have a static setup, i.e. you will not change the screen configuration
while emacs is running, then you do not need to
define jw/exwm-change-screen-hook.

If you are going to use more than one screen at the same time, you need to
define exwm-randr-workspace-monitor-plist, and call "(exwm-randr-enable)".
"(exwm-randr-enable)" must also be called if you use
exwm-randr-screen-change-hook.

#+BEGIN_SRC emacs-lisp
(use-package exwm)
(require 'exwm)
(require 'exwm-randr)

(defun jw/env-list (env-string)
  "Return list of strings in environment variable env-string.
nil if empty or undefined."
  (let ((env-var (getenv env-string)))
    (if env-var
        (split-string env-var)
      nil)))

(defun jw/env-str (env-string)
  "Return string in environment variable env-string.
nil if empty or undefined."
  (let ((env-var (getenv env-string)))
    (if (> (length env-var) 0)
        env-var
      nil)))

(defun jw/build-workspace-monitor-plist (list)
  (let (transformed-list first second (rev-list (reverse list)))
    (while rev-list
      (setq second (car rev-list))
      (setq first (string-to-number (car (cdr rev-list))))
      (setq transformed-list (cons first (cons second transformed-list)))
      (setq rev-list (cdr (cdr rev-list))))
    transformed-list))

(defun jw/xrandr-output-list ()
  "Return list of connected X11 screens, according to xrandr."
  (interactive)
  (let* ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
         (find-outputs
          (lambda ()
            (let (output-list)
              (call-process "xrandr" nil t nil)
              (goto-char (point-min))
              (while (re-search-forward xrandr-output-regexp nil 'noerror)
                (setq output-list (cons (match-string 1) output-list))
                (forward-line))
              (reverse output-list))))
         (output-list (with-temp-buffer
                        (funcall find-outputs))))
    output-list))

(setq jw/x11-screen-list (jw/env-list "X11_SCREEN_LIST"))
(setq jw/x11-screen-order-list (jw/env-list "X11_SCREEN_ORDER_LIST"))
(setq jw/x11-screen-mode-list (jw/env-list "X11_SCREEN_MODE_LIST"))
(setq jw/x11-screen-rate-list (jw/env-list "X11_SCREEN_RATE_LIST"))
(setq jw/x11-screen-disabled-list (jw/env-list "X11_SCREEN_DISABLED_LIST"))
(setq jw/exwm-workspace-list (jw/env-list "EXWM_WORKSPACE_LIST"))
(setq jw/x11-screen-preferred (jw/env-str "X11_SCREEN_PREFERRED"))
(setq jw/x11-display-dpi (jw/env-str "X11_DISPLAY_DPI"))
(let ((env-var (getenv "X11_SCREEN_USE_ALL_AVAILABLE")))
  (setq jw/x11-screen-use-all-available
        (if (and (> (length env-var) 0) (string= "yes" env-var))
            t
          nil)))

(setq exwm-randr-workspace-monitor-plist (jw/build-workspace-monitor-plist jw/exwm-workspace-list))

(defun jw/exwm-change-screen-hook ()
  "Execute xrandr to select and position available screens according to X11_SCREEN_* environment variables."
  (let* ((output-list (jw/xrandr-output-list))
         (available-screens (seq-intersection jw/x11-screen-list output-list))
         (available-order-screens (seq-intersection jw/x11-screen-order-list output-list))
         ;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529.
         (unavailable-screens (seq-difference jw/x11-screen-list output-list))
         (available-disabled-screens (seq-intersection jw/x11-screen-disabled-list output-list))
         (available-screen-modes
          (let (mode-list
                mode screen
                (x-screen-list jw/x11-screen-list)
                (x-mode-list jw/x11-screen-mode-list))
            (while x-screen-list
              (setq screen (car x-screen-list))
              (setq x-screen-list (cdr x-screen-list))
              (setq mode (car x-mode-list))
              (setq x-mode-list (cdr x-mode-list))
              (if (seq-contains available-screens screen)
                  (setq mode-list (cons mode mode-list))))
            (reverse mode-list)))
         (available-screen-rates
          (let (rate-list
                rate screen
                (x-screen-list jw/x11-screen-list)
                (x-rate-list jw/x11-screen-rate-list))
            (while x-screen-list
              (setq screen (car x-screen-list))
              (setq x-screen-list (cdr x-screen-list))
              (setq rate (car x-rate-list))
              (setq x-rate-list (cdr x-rate-list))
              (if (seq-contains available-screens screen)
                  (setq rate-list (cons rate rate-list))))
            (reverse rate-list))))
    (if available-screens
        ;; Start building xrandr command line
        (let* ((x-primary-screen
                (if (and jw/x11-screen-preferred (seq-contains available-screens jw/x11-screen-preferred))
                    jw/x11-screen-preferred
                  (car available-screens)))
               (screen-pos (seq-position available-screens x-primary-screen))
               (x-primary-mode (elt available-screen-modes screen-pos))
               (x-primary-rate (elt available-screen-rates screen-pos))
               (xrandr-dpi-args
                (if jw/x11-display-dpi
                    (list jw/x11-display-dpi "--dpi")))
               (xrandr-primary-args (list x-primary-rate "--rate" x-primary-mode "--mode" "--primary" x-primary-screen "--output"))
               screen
               disabled-list
               (xrandr-disabled-args
                (progn
                  (while available-disabled-screens
                    (setq screen (car available-disabled-screens))
                    (setq available-disabled-screens (cdr available-disabled-screens))
                    (setq disabled-list (cons "--output" disabled-list))
                    (setq disabled-list (cons screen disabled-list))
                    (setq disabled-list (cons "--off" disabled-list)))
                  disabled-list))
               (unavailable-screen-list unavailable-screens)
               u-s-list
               (xrandr-unavailable-screen-args
                (progn
                  (while unavailable-screen-list
                    (setq screen (car unavailable-screen-list))
                    (setq unavailable-screen-list (cdr unavailable-screen-list))
                    (setq u-s-list (cons "--output" u-s-list))
                    (setq u-s-list (cons screen u-s-list))
                    ;; (setq u-s-list (cons "--auto" u-s-list))
                    (setq u-s-list (cons "--off" u-s-list)))
                  u-s-list))
               (screen-list available-screens)
               rest-list
               (xrandr-rest-available-screen-args
                (if jw/x11-screen-use-all-available
                    ;; Add remaining available screens, except the primary screen
                    (progn
                      (while screen-list
                        (setq screen (car screen-list))
                        (setq screen-list (cdr screen-list))
                        (if (not (string= screen x-primary-screen))
                            (progn
                              (setq rest-list (cons "--output" rest-list))
                              (setq rest-list (cons screen rest-list))
                              (setq rest-list (cons "--mode" rest-list))
                              (setq rest-list (cons (elt available-screen-modes (seq-position available-screens screen)) rest-list))
                              (setq rest-list (cons "--rate" rest-list))
                              (setq rest-list (cons (elt available-screen-rates (seq-position available-screens screen)) rest-list)))))
                      rest-list)
                  ;; Disable remaining available screens, except the primary screen
                  (progn
                    (while screen-list
                      (setq screen (car screen-list))
                      (setq screen-list (cdr screen-list))
                      (if (not (string= screen x-primary-screen))
                          (progn
                            (setq rest-list (cons "--output" rest-list))
                            (setq rest-list (cons screen rest-list))
                            (setq rest-list (cons "--off" rest-list)))))
                    rest-list)))
               (screen-order-list available-order-screens)
               order-list
               left-screen
               (xrandr-screen-order-args
                (if (and jw/x11-screen-use-all-available
                         (> (length screen-order-list) 1))
                    (progn
                      (setq left-screen (car screen-order-list))
                      (setq screen-order-list (cdr screen-order-list))
                      (while screen-order-list
                        (setq screen (car screen-order-list))
                        (setq screen-order-list (cdr screen-order-list))
                        (setq order-list (cons "--output" order-list))
                        (setq order-list (cons screen order-list))
                        (setq order-list (cons "--right-of" order-list))
                        (setq order-list (cons left-screen order-list))
                        (setq left-screen screen))
                      (reverse order-list))))
               (xrandr-args (reverse (append xrandr-rest-available-screen-args xrandr-unavailable-screen-args
                                             xrandr-disabled-args xrandr-primary-args xrandr-dpi-args))))
          (progn
            (setq jw/debug-output-list output-list)
            (setq jw/debug-xrandr-args xrandr-args)
            (setq jw/debug-xrandr-order-args xrandr-screen-order-args)
            (apply #'call-process
                   "xrandr" nil nil nil
                   xrandr-args)
            (if xrandr-screen-order-args
                (apply #'call-process
                       "xrandr" nil nil nil
                       xrandr-screen-order-args)))))))





(add-hook 'exwm-randr-screen-change-hook 'jw/exwm-change-screen-hook)
(exwm-randr-enable)
#+END_SRC

* Polybar
My Polybar displays all the information I need, has a systray, and
looks decent imho!

#+BEGIN_SRC conf :tangle ~/.config/polybar/config :noweb yes
; Docs: https://github.com/polybar/polybar
;==========================================================

[settings]
screenchange-reload = true

[global/wm]
margin-top = 0
margin-bottom = 0

[colors]
background = #f0232635
background-alt = #576075
foreground = #A6Accd
foreground-alt = #555
primary = #ffb52a
secondary = #e60053
alert = #bd2c40
underline-1 = #c792ea

[bar/panel]
width = 100%
height = 15
offset-x = 0
offset-y = 0
fixed-center = true
enable-ipc = true

background = ${colors.background}
foreground = ${colors.foreground}

line-size = 2
line-color = #f00

border-size = 0
border-color = #00000000

padding-top = 5
padding-left = 1
padding-right = 1

module-margin = 1

font-0 = "JetBrainsMono Nerd Font:size=10:weight=bold;2"
font-1 = "Font Awesome 5 Free Regular:size=10;2"
font-2 = "Font Awesome 5 Free Solid:size=12;5"
font-3 = "Font Awesome 5 Brands:size=10;5"
font-4 = "JuliaMono:size=9;-3"

modules-left = date
modules-center = pulseaudio spotify
modules-right = backlight cpu memory battery

tray-position = right
tray-padding = 2
tray-maxsize = 28

cursor-click = pointer
cursor-scroll = ns-resize

[module/spotify]
type = custom/script
exec = ~/.config/polybar/player-status.sh
interval = 3

[module/xkeyboard]
type = internal/xkeyboard
blacklist-0 = num lock

format-prefix-font = 1
format-prefix-foreground = ${colors.foreground-alt}
format-prefix-underline = ${colors.underline-1}

label-layout = %layout%
label-layout-underline = ${colors.underline-1}

label-indicator-padding = 2
label-indicator-margin = 1
label-indicator-underline = ${colors.underline-1}

[module/cpu]
type = internal/cpu
interval = 2
format = " <label>"
format-underline = ${colors.underline-1}
click-left = emacsclient -e "(proced)"
label = %percentage:2%%

[module/backlight]
type = internal/backlight
card = intel_backlight
format = " <label>"
format-underline = ${colors.underline-1}
label = %percentage%%

[module/memory]
type = internal/memory
interval = 2
format-prefix = "RAM: "
format-prefix-foreground = ${colors.foreground-alt}
format-underline = ${colors.underline-1}
label = %gb_used%

[module/pulseaudio]
type = internal/pulseaudio
format-prefix-foreground = ${colors.foreground-alt}
format-underline = ${colors.underline-1}
label-volume = " %percentage%%"
label-muted  = " - Muted"

[module/date]
type = internal/date
interval = 5

date = "W%U: %a %b %e"
date-alt = "%A %B %d %Y"

time = %H:%M
time-alt = %H:%M

format-prefix-foreground = ${colors.foreground-alt}
format-underline = ${colors.underline-1}

label = %date% %time%

[module/battery]
type = internal/battery
battery = BAT0
adapter = ADP1
full-at = 80

label-charging = %percentage%%
format-charging = <animation-charging> <label-charging>
format-charging-underline = ${colors.underline-1}

label-discharging = %percentage%%
format-discharging = <ramp-capacity> <label-discharging>
format-discharging-underline = ${self.format-charging-underline}

format-full = <ramp-capacity> <label-full>
format-full-underline = ${self.format-charging-underline}

ramp-capacity-0 = 
ramp-capacity-1 = 
ramp-capacity-2 = 
ramp-capacity-3 = 
ramp-capacity-4 = 

animation-charging-0 = 
animation-charging-1 = 
animation-charging-2 = 
animation-charging-3 = 
animation-charging-4 = 
animation-charging-framerate = 750
#+END_SRC

daviwil made a simple script to grab information from Spotify and put it in Polybar:

#+BEGIN_SRC sh :tangle ~/.config/polybar/player-status.sh :shebang #!/bin/sh
status="$(playerctl -p spotify status 2>&1)"
if [ "$status" != "No players found" ]
then
  artist="$(playerctl -p spotify metadata artist)"
  if [ "$artist" != "" ]
  then
    echo " $(playerctl -p spotify metadata artist) - $(playerctl -p spotify metadata title)"
  else
    # Clear any string that was previously displayed
    echo ""
  fi
else
  # Clear any string that was previously displayed
  echo ""
fi
#+END_SRC

* EXWM
My EXWM config truly makes Emacs feel like a worthy desktop
environment.

#+BEGIN_SRC emacs-lisp
  (require 'ido)
  (use-package windower)
  (use-package app-launcher
  :straight '(app-launcher :host github :repo "SebastienWae/app-launcher"))


  (require 'browse-url)
  (require 'exwm-manage)

  (defun catster/run-in-background (command)
    "Run command asynchronously in the background."
    (let ((command-parts (split-string command "[ ]+")))
      (apply #'call-process `(,(car command-parts) nil 0 nil ,@(cdr command-parts)))))

  (defun catster/run-external-program (program)
    "Run given external program"
    (interactive)
    (start-process-shell-command "" nil program))

  (defun catster/reboot-linux-computer (program)
    "Run given external program"
    (interactive)
    (start-process-shell-command "" nil "sudo reboot"))
#+END_SRC

Some programs really benefit from being in "char mode," almost all
input given to them. Nyxt, Alacritty, and other Emacs instances
especially need as many keys available as possible.

#+BEGIN_SRC emacs-lisp
(defun catster/exwm-start-nyxt-in-char-mode ()
  (when (string-prefix-p "nyxt" exwm-instance-name)
    (exwm-input-release-keyboard (exwm--buffer->id (window-buffer)))))

(defun catster/exwm-start-firefox-in-char-mode ()
  (when (string-prefix-p "firefox" exwm-instance-name)
    (exwm-input-release-keyboard (exwm--buffer->id (window-buffer)))))

(defun ambrevar/exwm-start-emacs-in-char-mode ()
  (when (string-prefix-p "emacs" exwm-instance-name)
    (exwm-input-release-keyboard (exwm--buffer->id (window-buffer)))))

(defun catster/exwm-start-alacritty-in-char-mode ()
  (when (string-prefix-p "Alacritty" exwm-instance-name)
    (exwm-input-release-keyboard (exwm--buffer->id (window-buffer)))))

(add-hook 'exwm-manage-finish-hook 'catster/exwm-start-nyxt-in-char-mode)
(add-hook 'exwm-manage-finish-hook 'catster/exwm-start-firefox-in-char-mode)
(add-hook 'exwm-manage-finish-hook 'catster/exwm-start-alacritty-in-char-mode)
(add-hook 'exwm-manage-finish-hook 'ambrevar/exwm-start-emacs-in-char-mode)

(defun ambrevar/call-process-to-string (program &rest args)
  "Call PROGRAM with ARGS and return output.
See also `process-lines'."
  (with-output-to-string
    (with-current-buffer standard-output
      (apply 'process-file program nil t nil args))))

(setq browse-url-generic-program
      (or
       (executable-find (or (getenv "BROWSER") ""))
       (when (executable-find "xdg-mime")
         (let ((desktop-browser (ambrevar/call-process-to-string "xdg-mime" "query" "default" "text/html")))
           (substring desktop-browser 0 (string-match "\\.desktop" desktop-browser))))
       (executable-find browse-url-chrome-program)))
#+END_SRC

Silence *Async Shell Command* buffers, but still enable them for
troubleshooting.

#+BEGIN_SRC emacs-lisp
(add-to-list 'display-buffer-alist
             (cons "\\*Async Shell Command\\*.*" (cons #'display-buffer-no-window nil)))
#+END_SRC

Below is my main configuration for EXWM, with 10 workspaces (0-9).

#+BEGIN_SRC emacs-lisp
(defun my-exwm-config-setup ()
  "My modified configuration for EXWM. Based on everything else on the internet ;D"
  (unless (get 'exwm-workspace-number 'saved-value)
    (setq exwm-workspace-number 10))
  ;; Make buffer names more meaningful
  (add-hook 'exwm-update-class-hook
            (lambda ()  (unless (or (string-prefix-p "sun-awt-X11-" exwm-instance-name)  (string= "gimp" exwm-instance-name))
                          (exwm-workspace-rename-buffer exwm-class-name))))
  ;; Prefix keybindings, helpful for using M-x, C-c, and C-x when in EXWM buffers
  (setq exwm-input-prefix-keys
        '(?\M-x
          ?\M-:
          ?\C-x
          ?\C-c))
  ;; Global keybindings. 0-9 bcDfFgGhHijJkKlLmnoOpQrRwW !@#$%^&*() tab f2 backspace volume-keys
  (unless (get 'exwm-input-global-keys 'saved-value)
    (setq exwm-input-global-keys
          `(
            ;; (,(kbd "s-b") . exwm-workspace-switch-to-buffer)
            (,(kbd "s-<return>") . (lambda ()
                                     (interactive)
                                     (start-process "" nil "alacritty"))) ;; vterm is nice, but I want a more real terminal right now.
            (,(kbd "s-b") . switch-to-buffer) ;; List and select buffers
            ;; (,(kbd "s-c") . )
            (,(kbd "s-G") . locate) ;; locate file, based on Linux locate command
            ;; (,(kbd "s-g") . )
            ;; (,(kbd "s-g") . )
            (,(kbd "s-g") . grep) ;; Grep search in files
            (,(kbd "s-d") . app-launcher-run-app) ;; Start an application using dmenu
            ;; (,(kbd "s-W") . )
            (,(kbd "s-m") . (lambda () ;; Toggle display of mode-line and minibuffer, in an EXWM window
                              (interactive)
                              (exwm-layout-toggle-mode-line)
                              (exwm-workspace-toggle-minibuffer)))
            (,(kbd "s-i") . exwm-input-toggle-keyboard) ;; Toggle between "line-mode" and "char-mode" in an EXWM window
            ;; 's-r': Reset (to line-mode).
            (,(kbd "s-R") . exwm-reset) ;; Try to reset EXWM to a sane mode. Panic key
            ;; Interactively select, and switch to, a workspace. Only works in non EXWM windows.
            (,(kbd "s-w") . (lambda ()
                                    (interactive)
                                    (start-process "" nil (getenv "BROWSER"))))
            ;; 's-a': Launch application.
            ;; (,(kbd "s-a") . (lambda (command)
            ;;              (interactive (list (read-shell-command "$ ")))
            ;;              (start-process-shell-command command nil command)))
            ;; 's-N': Switch to a certain workspace.
            ,@(mapcar (lambda (i)
                        `(,(kbd (format "s-%d" i)) .
                          (lambda ()
                            (interactive)
                            (exwm-workspace-switch-create ,i))))
                      (number-sequence 0 9))
            ;; 'S-s-N': Move window to, and switch to, a certain workspace.
            ,@(cl-mapcar (lambda (c n)
                           `(,(kbd (format "s-%c" c)) .
                             (lambda ()
                               (interactive)
                               (exwm-workspace-move-window ,n)
                               (exwm-workspace-switch ,n))))
                         '(?\) ?! ?@ ?# ?$ ?% ?^ ?& ?* ?\()
                         ;; '(?\= ?! ?\" ?# ?¤ ?% ?& ?/ ?\( ?\))
                         (number-sequence 0 9))

            ;; Bind "s-<f2>" to "slock", a simple X display locker.
            (,(kbd "s-<f2>") . (lambda ()
                                 (interactive)
                                 (start-process "" nil "slock")))
            (,(kbd "s-h") . windmove-left)  ;; Move to window to the left of current one. Uses universal arg
            (,(kbd "s-j") . windmove-down)  ;; Move to window below current one. Uses universal arg
            (,(kbd "s-k") . windmove-up)    ;; Move to window above current one. Uses universal arg
            (,(kbd "s-l") . windmove-right) ;; Move to window to the right of current one. Uses universal arg
            (,(kbd "s-x") . split-window-right)  ;; Split window right even when in char mode, such as Nyxt or another Emacs
            (,(kbd "s-X") . split-window-below)  ;; Split window below even when in char mode, such as Nyxt or another Emacs
            (,(kbd "s-f") . find-file)
            (,(kbd "s-<tab>") . windower-switch-to-last-buffer) ;; Switch to last open buffer in current window
            (,(kbd "s-o") . windower-toggle-single) ;; Toggle between multiple windows, and a single window
            (,(kbd "s-O") . windower-toggle-split)  ;; Toggle between vertical and horizontal split. Only works with exactly two windows.
            (,(kbd "s-H") . windower-swap-left)  ;; Swap current window with the window to the left
            (,(kbd "s-J") . windower-swap-below) ;; Swap current window with the window below
            (,(kbd "s-K") . windower-swap-above) ;; Swap current window with the window above
            (,(kbd "s-L") . windower-swap-right) ;; Swap current window with the window to the right
            (,(kbd "s-F") . exwm-floating-toggle-floating) ;; Toggle the current window between floating and non-floating states
            (,(kbd "s-Q") . exwm-layout-toggle-fullscreen) ;; Toggle fullscreen mode, when in an EXWM window.
            (,(kbd "s-q") . kill-this-buffer)
            (,(kbd "s-<backspace>") . kill-this-buffer)
            (,(kbd "s-n") . rename-buffer)
            ;; screenshots
            (,(kbd "s-p") . (lambda ()
                              (interactive)
                              (start-process "" nil "flameshot" "gui")))
            ;; audio controls
            (,(kbd "<XF86AudioRaiseVolume>") . (lambda ()
                                                 (interactive)
                                                 (start-process "" nil "pamixer" "-i" "5")))
            (,(kbd "<XF86AudioLowerVolume>") . (lambda ()
                                                 (interactive)
                                                 (start-process "" nil "pamixer" "-d" "5")))
            (,(kbd "<XF86AudioMute>") . (lambda ()
                                          (interactive)
                                          (start-process "" nil "pamixer" "-t")))
            (,(kbd "<XF86MonBrightnessUp>") . (lambda ()
                                                (interactive)
                                                (start-process "" nil "brightnessctl" "s" "10%+")))
            (,(kbd "<XF86MonBrightnessDown>") . (lambda ()
                                                  (interactive)
                                                  (start-process "" nil "brightnessctl" "s" "10%-"))))))
  ;; Line-editing shortcuts: abBdefFknpsvVwWy
  (unless (get 'exwm-input-simulation-keys 'saved-value)
    (setq exwm-input-simulation-keys
          `((,(kbd "H-b") . ,(kbd "<left>"))
            (,(kbd "H-B") . ,(kbd "C-<left>"))
            (,(kbd "H-f") . ,(kbd "<right>"))
            (,(kbd "H-F") . ,(kbd "C-<right>"))
            (,(kbd "H-p") . ,(kbd "<up>"))
            (,(kbd "H-n") . ,(kbd "<down>"))
            (,(kbd "H-a") . ,(kbd "<home>"))
            (,(kbd "H-e") . ,(kbd "<end>"))
            ;; q and w are convenient if Caps Lock key is Hyper key
            (,(kbd "H-v") . ,(kbd "<prior>"))
            (,(kbd "H-V") . ,(kbd "<next>"))
            (,(kbd "H-d") . ,(kbd "<delete>"))
            (,(kbd "H-k") . ,(kbd "S-<end> <delete>"))
            ;; cut/paste.
            (,(kbd "H-W") . ,(kbd "C-x"))
            (,(kbd "H-w") . ,(kbd "C-c"))
            (,(kbd "H-y") . ,(kbd "C-v"))
            ;; search
            (,(kbd "H-s") . ,(kbd "C-f")))))

  ;; Default is save-buffers-kill-terminal, but that may kill daemon before its finished
  (global-set-key (kbd "C-x C-c") 'save-buffers-kill-emacs)
  (add-hook 'exwm-update-title-hook 'catster/exwm-rename-buffer)
  ;; Do not mess with EXWM windows when considering splitting
  ;; Especially annoying with Magit
  (defun thecatster/window-exwm-p (window)
    "Return t if WINDOW is exwm-mode"
    (equal 'exwm-mode (buffer-local-value 'major-mode (window-buffer window))))
  (defun thecatster/window-list-filter-advice (result)
    "Advice fn to exclude exwm windows from returned list, unless all are exwm."
    (or (-reject 'thecatster/window-exwm-p result) result))
  (defun thecatster/display-buffer-around-advice (orig-fun buffer-or-name
                                                           &optional action frame)
    "Advice for `display-buffer` to only use non-exwm windows if possible."
    (advice-add #'window-list-1 :filter-return #'thecatster/window-list-filter-advice)
    (unwind-protect
        (apply orig-fun buffer-or-name action frame)
      (advice-remove #'window-list-1 #'thecatster/window-list-filter-advice)))
  (advice-add #'display-buffer :around #'thecatster/display-buffer-around-advice)
  ;; Ensure that EXWM input mode is displayed in mode line
  (add-hook 'exwm-input--input-mode-change-hook
            'force-mode-line-update)
  ;; Allow resizing of non-floating windows, with mouse.
  (setq window-divider-default-bottom-width 2
        window-divider-default-right-width 2)
  (window-divider-mode)
  ;; Polybar!
(setq dw/panel-process nil)
(defun dw/kill-panel ()
  (interactive)
  (when dw/panel-process
    (ignore-errors
      (kill-process dw/panel-process)))
  (setq dw/panel-process nil))

(defun dw/start-panel ()
  (interactive)
  (dw/kill-panel)
  (setq dw/panel-process (start-process-shell-command "polybar" nil "polybar panel")))
   (savehist-mode 1)
   (add-to-list 'savehist-additional-variables 'kill-ring)
  ;; Allow switching to EXWM buffers not belonging to current workspace.
  ;; This behaviour takes some getting used to, I guess thats why its not default
   (setq exwm-layout-show-all-buffers t)
   (setq exwm-workspace-show-all-buffers t)
  ;; Mouse auto follow, and auto select the window with mouse
   (setq mouse-autoselect-window t)
   (use-package exwm-mff
     :straight (:host github :repo "ieure/exwm-mff")
     :config
     (exwm-mff-mode 1))
  ;; This causes issues in performance for me for some strange reason,
  ;; so I disable it.
   (setq focus-follows-mouse t)
   (setq x-wait-for-event-timeout nil)
  ;; Configure Ido
   (my-exwm-config-ido)
  ;; Other configurations
   (my-exwm-config-misc))

;; This is copied from exwm-config.el
 (defun my-exwm-config--fix/ido-buffer-window-other-frame ()
   "Fix `ido-buffer-window-other-frame'."
   (defalias 'exwm-config-ido-buffer-window-other-frame
     (symbol-function #'ido-buffer-window-other-frame))
   (defun ido-buffer-window-other-frame (buffer)
     "This is a version redefined by EXWM.

You can find the original one at `exwm-config-ido-buffer-window-other-frame'."
     (with-current-buffer (window-buffer (selected-window))
       (if (and (derived-mode-p 'exwm-mode)
                exwm--floating-frame)
           ;; Switch from a floating frame.
           (with-current-buffer buffer
             (if (and (derived-mode-p 'exwm-mode)
                      exwm--floating-frame
                      (eq exwm--frame exwm-workspace--current))
                 ;; Switch to another floating frame.
                 (frame-root-window exwm--floating-frame)
               ;; Do not switch if the buffer is not on the current workspace.
               (or (get-buffer-window buffer exwm-workspace--current)
                   (selected-window))))
         (with-current-buffer buffer
           (when (derived-mode-p 'exwm-mode)
             (if (eq exwm--frame exwm-workspace--current)
                 (when exwm--floating-frame
                   ;; Switch to a floating frame on the current workspace.
                   (frame-selected-window exwm--floating-frame))
               ;; Do not switch to exwm-mode buffers on other workspace (which
               ;; won't work unless `exwm-layout-show-all-buffers' is set)
               (unless exwm-layout-show-all-buffers
                 (selected-window)))))))))

 (defun my-exwm-config-ido ()
   "Configure Ido to work with EXWM."
   ;; (ido-mode 1)
   (add-hook 'exwm-init-hook #'my-exwm-config--fix/ido-buffer-window-other-frame))

 (defun my-exwm-config-misc ()
   "Other configurations."
   ;; Make more room
   (menu-bar-mode -1)
   (tool-bar-mode -1)
   (scroll-bar-mode -1))

;; Rename buffer to window title.
 (defun catster/exwm-rename-buffer ()
   (interactive)
   (exwm-workspace-rename-buffer (concat exwm-class-name " : " (substring exwm-title 0 (min 12 (length exwm-title))))))

 (my-exwm-config-setup) ;; Does not start X11 or EXWM. Start should be done from commandline.

(provide 'catsters-exwm)
;;; catsters-exwm.el ends here
#+END_SRC