about summary refs log tree commit diff
path: root/tools/emacs/config/desktop.el
blob: d923ab584c750a9e88ee2f1404b457443ffe3acc (plain) (blame)
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
;; -*- lexical-binding: t; -*-
;;
;; Configure desktop environment settings, including both
;; window-management (EXWM) as well as additional system-wide
;; commands.

(require 's)
(require 'f)
(require 'dash)
(require 'exwm)
(require 'exwm-config)
(require 'exwm-randr)
(require 'exwm-systemtray)

(defun pactl (cmd)
  (shell-command (concat "pactl " cmd))
  (message "Volume command: %s" cmd))

(defun volume-mute () (interactive) (pactl "set-sink-mute @DEFAULT_SINK@ toggle"))
(defun volume-up () (interactive) (pactl "set-sink-volume @DEFAULT_SINK@ +5%"))
(defun volume-down () (interactive) (pactl "set-sink-volume @DEFAULT_SINK@ -5%"))

(defun brightness-up ()
  (interactive)
  (shell-command "xbacklight -inc 5")
  (message "Brightness increased"))

(defun brightness-down ()
  (interactive)
  (shell-command "xbacklight -dec 5")
  (message "Brightness decreased"))

(defun lock-screen ()
  (interactive)
  ;; A sudoers configuration is in place that lets me execute this
  ;; particular command without having to enter a password.
  ;;
  ;; The reason for things being set up this way is that I want
  ;; xsecurelock.service to be started as a system-wide service that
  ;; is tied to suspend.target.
  (shell-command "/usr/bin/sudo /usr/bin/systemctl start xsecurelock.service"))

(defun generate-randr-config (primary secondary)
  (-flatten `(,(-map (lambda (n) (list n primary)) (number-sequence 1 7))
              (0 secondary)
              ,(-map (lambda (n) (list n secondary)) (number-sequence 8 9)))))

(defun randr-layout-dp1-extend ()
  "Layout for connecting my X1 Carbon to my screen at home."

  (interactive)
  (setq exwm-randr-workspace-monitor-plist (generate-randr-config "DP1-1" "eDP1"))
  (exwm-randr-refresh)
  (shell-command "xrandr --output DP1-1 --right-of eDP1 --auto --primary"))

(defun randr-layout-hdmi1-extend ()
  "Office layout for The Big Screen(tm)"

  (interactive)
  (setq exwm-randr-workspace-monitor-plist (generate-randr-config "HDMI1" "eDP1"))
  (exwm-randr-refresh)
  (shell-command "xrandr --output HDMI1 --dpi 144 --auto --right-of eDP1 --primary")
  (set-default-text-scale nil 165))

(defun randr-layout-single ()
  "Laptop screen only!"

  (interactive)
  (shell-command "xrandr --output HDMI1 --off")
  (shell-command "xrandr --output DP1-1 --off")
  (exwm-randr-refresh)
  (set-default-text-scale nil))

(defun set-xkb-layout (layout)
  "Set the current X keyboard layout."

  (shell-command (format "setxkbmap %s" layout))
  (message "Set X11 keyboard layout to '%s'" layout))

(defun create-window-name ()
  "Construct window names to be used for EXWM buffers by
  inspecting the window's X11 class and title.

  A lot of commonly used applications either create titles that
  are too long by default, or in the case of web
  applications (such as Cider) end up being constructed in
  awkward ways.

  To avoid this issue, some rewrite rules are applied for more
  human-accessible titles."

  (pcase (list (or exwm-class-name "unknown") (or exwm-title "unknown"))
    ;; In Cider windows, rename the class and keep the workspace/file
    ;; as the title.
    (`("Google-chrome" ,(and (pred (lambda (title) (s-ends-with? " - Cider" title))) title))
     (format "Cider<%s>" (s-chop-suffix " - Cider" title)))

    ;; Attempt to detect IRCCloud windows via their title, which is a
    ;; combination of the channel name and network.
    ;;
    ;; This is what would often be referred to as a "hack". The regexp
    ;; will not work if a network connection buffer is selected in
    ;; IRCCloud, but since the title contains no other indication that
    ;; we're dealing with an IRCCloud window
    (`("Google-chrome"
       ,(and (pred (lambda (title)
                     (s-matches? "^[\*\+]\s#[a-zA-Z0-9/\-]+\s\|\s[a-zA-Z\.]+$" title)))
             title))
     (format "IRCCloud<%s>" title))

    ;; For other Chrome windows, make the title shorter.
    (`("Google-chrome" ,title)
     (format "Chrome<%s>" (s-truncate 42 (s-chop-suffix " - Google Chrome" title))))

    ;; Gnome-terminal -> Term
    (`("Gnome-terminal" ,title)
     ;; fish-shell buffers contain some unnecessary whitespace and
     ;; such before the current working directory. This can be
     ;; stripped since most of my terminals are fish shells anyways.
     (format "Term<%s>" (s-trim-left (s-chop-prefix "fish" title))))

    ;; For any other application, a name is constructed from the
    ;; window's class and name.
    (`(,class ,title) (format "%s<%s>" class (s-truncate 12 title)))))

;; EXWM launch configuration
;;
;; This used to use use-package, but when something breaks use-package
;; it doesn't exactly make debugging any easier.

(let ((titlef (lambda ()
                (exwm-workspace-rename-buffer (create-window-name)))))
  (add-hook 'exwm-update-class-hook titlef)
  (add-hook 'exwm-update-title-hook titlef))

(fringe-mode 3)
(exwm-enable)

;; 's-N': Switch to certain workspace
(setq exwm-workspace-number 10)
(dotimes (i 10)
  (exwm-input-set-key (kbd (format "s-%d" i))
                      `(lambda ()
                         (interactive)
                         (exwm-workspace-switch-create ,i))))

;; Launch applications / any command  with completion (dmenu style!)
(exwm-input-set-key (kbd "s-d") #'counsel-linux-app)
(exwm-input-set-key (kbd "s-x") #'ivy-run-external-command)
(exwm-input-set-key (kbd "s-p") #'ivy-password-store)

;; Add X11 terminal selector to a key
(exwm-input-set-key (kbd "C-x t") #'ts/switch-to-terminal)

;; Toggle between line-mode / char-mode
(exwm-input-set-key (kbd "C-c C-t C-t") #'exwm-input-toggle-keyboard)

;; Volume keys
(exwm-input-set-key (kbd "<XF86AudioMute>") #'volume-mute)
(exwm-input-set-key (kbd "<XF86AudioRaiseVolume>") #'volume-up)
(exwm-input-set-key (kbd "<XF86AudioLowerVolume>") #'volume-down)

;; Brightness keys
(exwm-input-set-key (kbd "<XF86MonBrightnessDown>") #'brightness-down)
(exwm-input-set-key (kbd "<XF86MonBrightnessUp>") #'brightness-up)
(exwm-input-set-key (kbd "<XF86Display>") #'lock-screen)

;; Keyboard layouts (these are bound separately in Cyrillic
;; because I don't use reverse-im)
;; (-map
;;  (lambda (pair)
;;    (exwm-input-set-key
;;     (kbd (format "s-%s" (cadr pair)))
;;     `(lambda () (interactive) (set-xkb-layout ,(car pair)))))
;;  '(("de" "k d")
;;    ("de" "л в")
;;    ("no" "k n")
;;    ("no" "л т")
;;    ("ru" "k r")
;;    ("ru" "л к")
;;    ("us" "k u")
;;    ("us" "л г")))

;; Line-editing shortcuts
(exwm-input-set-simulation-keys
 '(([?\C-d] . delete)
   ([?\C-w] . ?\C-c)))

;; Show time & battery status in the mode line
(display-time-mode)
(display-battery-mode)

;; enable display of X11 system tray within Emacs
(exwm-systemtray-enable)

;; Configure xrandr (multi-monitor setup)
(setq exwm-randr-workspace-monitor-plist (generate-randr-config "HDMI1" "eDP1"))
(exwm-randr-enable)

;; Let buffers move seamlessly between workspaces by making them
;; accessible in selectors on all frames.
(setq exwm-workspace-show-all-buffers t)
(setq exwm-layout-show-all-buffers t)

;; Monitor layouts
;;
;; TODO(tazjin): Desired layout should be inferred based on
;; connected screens - autorandr or something?
(exwm-input-set-key (kbd "s-m d") #'randr-layout-dp1-extend)
(exwm-input-set-key (kbd "s-m h") #'randr-layout-hdmi1-extend)
(exwm-input-set-key (kbd "s-m s") #'randr-layout-single)

(provide 'desktop)