Various tweaks and fixes for using EXWM
[~bandali/configs] / .emacs.d / lisp / bandali-exwm.el
1 ;;; bandali-exwm.el --- bandali's EXWM configuration -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2018-2022 Amin Bandali
4
5 ;; Author: Amin Bandali <bandali@gnu.org>
6 ;; Keywords: tools
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; My EXWM setup. Makes good use of its simulation keys.
24
25 ;;; Code:
26
27 (add-to-list 'load-path (b/lisp "xelb"))
28 (add-to-list 'load-path (b/lisp "exwm"))
29 (require 'exwm)
30 (setq ;; exwm-replace t
31 exwm-workspace-show-all-buffers t)
32 ;; make class name the buffer name, truncating beyond 60 characters
33 (defun b/exwm-rename-buffer ()
34 (interactive)
35 (exwm-workspace-rename-buffer
36 (concat exwm-class-name ":"
37 (if (<= (length exwm-title) 60) exwm-title
38 (concat (substring exwm-title 0 59) "...")))))
39 ;; Enable EXWM
40 (exwm-enable)
41 (add-hook 'exwm-update-class-hook #'b/exwm-rename-buffer)
42 (add-hook 'exwm-update-title-hook #'b/exwm-rename-buffer)
43
44 (require 'exwm-config)
45 (add-hook 'exwm-init-hook #'exwm-config--fix/ido-buffer-window-other-frame)
46
47 (require 'exwm-input)
48 (defun b/exwm-ws-prev-index ()
49 "Return the index for the previous EXWM workspace, wrapping
50 around if needed."
51 (if (= exwm-workspace-current-index 0)
52 (1- exwm-workspace-number)
53 (1- exwm-workspace-current-index)))
54
55 (defun b/exwm-ws-next-index ()
56 "Return the index for the next EXWM workspace, wrapping
57 around if needed."
58 (if (= exwm-workspace-current-index
59 (1- exwm-workspace-number))
60 0
61 (1+ exwm-workspace-current-index)))
62
63 (defun b/exwm-ws-prev ()
64 "Switch to previous EXWM workspace, wrapping around if needed."
65 (interactive)
66 (exwm-workspace-switch-create
67 (b/exwm-ws-prev-index)))
68
69 (defun b/exwm-ws-next ()
70 "Switch to next EXWM workspace, wrapping around if needed."
71 (interactive)
72 (exwm-workspace-switch-create
73 (b/exwm-ws-next-index)))
74
75 ;; shorten 'C-c C-q' to 'C-q'
76 (define-key exwm-mode-map [?\C-q] #'exwm-input-send-next-key)
77
78 ;; scroll up/down/left/right on the echo area
79 (define-key minibuffer-inactive-mode-map [mouse-4] #'b/exwm-ws-prev)
80 (define-key minibuffer-inactive-mode-map [mouse-5] #'b/exwm-ws-next)
81 (define-key minibuffer-inactive-mode-map [mouse-6] #'b/exwm-ws-prev)
82 (define-key minibuffer-inactive-mode-map [mouse-7] #'b/exwm-ws-next)
83
84 (defvar b/shifted-ws-names
85 '(0 \) 1 \! 2 \@ 3 \# 4 \$
86 5 \% 6 \^ 7 \& 8 \* 9 \()
87 "Mapping of shifted numbers on my keyboard.")
88
89 (setq
90 exwm-workspace-number 10
91 exwm-input-global-keys
92 `(([?\s-R] . exwm-reset)
93 ;; ([?\s-b] . exwm-workspace-switch-to-buffer)
94 ([?\s-/] . exwm-workspace-switch)
95 ([?\s-\s] . dmenu)
96 ([?\s-\\] . (lambda ()
97 (interactive)
98 (start-process-shell-command
99 "passmenu" nil "passmenu --type")))
100 ([?\S-\s-\s] . (lambda (command)
101 (interactive
102 (list (read-shell-command "➜ ")))
103 (start-process-shell-command
104 command nil command)))
105 ([s-return] . (lambda ()
106 (interactive)
107 (start-process "" nil "xterm")))
108 ([S-s-return] . (lambda ()
109 (interactive)
110 (start-process "" nil "xterm"
111 "-name" "floating")))
112 ([?\C-\s-\s] . counsel-linux-app)
113 ([?\s-h] . windmove-left)
114 ([?\s-j] . windmove-down)
115 ([?\s-k] . windmove-up)
116 ([?\s-l] . windmove-right)
117 ([?\s-H] . windmove-swap-states-left)
118 ([?\s-J] . windmove-swap-states-down)
119 ([?\s-K] . windmove-swap-states-up)
120 ([?\s-L] . windmove-swap-states-right)
121 ([?\s-N ?d] . (lambda ()
122 (interactive)
123 (start-process
124 "" nil "dunstctl" "close")))
125 ([?\s-N ?D] . (lambda ()
126 (interactive)
127 (start-process
128 "" nil "dunstctl" "close-all")))
129 ([?\s-N ?h] . (lambda ()
130 (interactive)
131 (start-process
132 "" nil "dunstctl" "history-pop")))
133 ([?\s-N return] . (lambda ()
134 (interactive)
135 (start-process
136 "" nil "dunstctl" "context")))
137 ([?\M-\s-h] . shrink-window-horizontally)
138 ([?\M-\s-l] . enlarge-window-horizontally)
139 ([?\M-\s-k] . shrink-window)
140 ([?\M-\s-j] . enlarge-window)
141 ([?\s-\[] . b/exwm-ws-prev)
142 ([?\s-\]] . b/exwm-ws-next)
143 ([mode-line mouse-4] . b/exwm-ws-prev) ; up
144 ([mode-line mouse-5] . b/exwm-ws-next) ; down
145 ([mode-line mouse-6] . b/exwm-ws-prev) ; left
146 ([mode-line mouse-7] . b/exwm-ws-next) ; right
147 ([?\s-{] . (lambda ()
148 (interactive)
149 (exwm-workspace-move-window
150 (b/exwm-ws-prev-index))))
151 ([?\s-}] . (lambda ()
152 (interactive)
153 (exwm-workspace-move-window
154 (b/exwm-ws-next-index))))
155 ,@(mapcar (lambda (i)
156 `(,(kbd (format "s-%d" i)) .
157 (lambda ()
158 (interactive)
159 (exwm-workspace-switch-create ,i))))
160 (number-sequence 0 (1- exwm-workspace-number)))
161 ,@(mapcar
162 (lambda (i)
163 `(,(kbd (format "s-%s"
164 (plist-get b/shifted-ws-names i)))
165 .
166 (lambda ()
167 (interactive)
168 (exwm-workspace-move-window ,i))))
169 (number-sequence 0 (1- exwm-workspace-number)))
170 ([?\s-.] . exwm-floating-toggle-floating)
171 ([?\s-f] . exwm-layout-toggle-fullscreen)
172 ([?\s-W] . (lambda ()
173 (interactive)
174 (kill-buffer (current-buffer))))
175 ([?\s-Q] . (lambda ()
176 (interactive)
177 (exwm-manage--kill-client)))
178 ([?\s-\'] . (lambda ()
179 (interactive)
180 (start-process-shell-command
181 "dmneu-light" nil "dmenu-light")))
182 ([?\s-\;] . (lambda ()
183 (interactive)
184 (start-process-shell-command
185 "dmneu-pamixer" nil "dmenu-pamixer")))
186 ([XF86AudioMute] . ; borken on my X200 :-(
187 (lambda ()
188 (interactive)
189 (start-process "" nil "pamixer" "--toggle-mute")))
190 ([XF86Launch1] .
191 (lambda ()
192 (interactive)
193 (start-process "" nil "pamixer" "--toggle-mute")))
194 ([\s-XF86Launch1] . ; toggle mic mute
195 (lambda ()
196 (interactive)
197 (start-process
198 "" nil "pamixer" "--default-source" "--toggle-mute")))
199 ([XF86AudioLowerVolume] .
200 (lambda ()
201 (interactive)
202 (start-process
203 "" nil "pamixer" "--allow-boost" "--decrease" "5")))
204 ([XF86AudioRaiseVolume] .
205 (lambda ()
206 (interactive)
207 (start-process
208 "" nil "pamixer" "--allow-boost" "--increase" "5")))
209 ([XF86AudioPlay] .
210 (lambda ()
211 (interactive)
212 (start-process "" nil "mpc" "toggle")))
213 ([XF86AudioPrev] .
214 (lambda ()
215 (interactive)
216 (start-process "" nil "mpc" "prev")))
217 ([XF86AudioNext] .
218 (lambda ()
219 (interactive)
220 (start-process "" nil "mpc" "next")))
221 ([XF86MonBrightnessDown] .
222 (lambda ()
223 (interactive)
224 (start-process "" nil "light" "-U" "5")))
225 ([XF86MonBrightnessUp] .
226 (lambda ()
227 (interactive)
228 (start-process "" nil "light" "-A" "5")))
229 ([XF86ScreenSaver] .
230 (lambda ()
231 (interactive)
232 (start-process "" nil "dm-tool" "lock")))
233 ([\s-XF86Back] . previous-buffer)
234 ([\s-XF86Forward] . next-buffer)))
235
236 ;; Line-editing shortcuts
237 (setq
238 exwm-input-simulation-keys
239 '(;; movement
240 ([?\C-b] . [left])
241 ([?\M-b] . [C-left])
242 ([?\C-f] . [right])
243 ([?\M-f] . [C-right])
244 ([?\C-p] . [up])
245 ([?\C-n] . [down])
246 ([?\C-a] . [home])
247 ([?\C-e] . [end])
248 ([?\M-v] . [prior])
249 ([?\C-v] . [next])
250 ([?\C-d] . [delete])
251 ([?\C-k] . [S-end ?\C-x])
252 ([?\M-<] . C-home)
253 ([?\M->] . C-end)
254 ;; cut/copy/paste
255 ([?\C-w] . [?\C-x])
256 ([?\M-w] . [?\C-c])
257 ([?\C-y] . [?\C-v])
258 ([?\M-d] . [C-S-right ?\C-x])
259 ([?\M-\d] . [C-S-left ?\C-x])
260 ;; window
261 ([?\s-w] . [?\C-w])
262 ([?\s-q] . [?\C-q])
263 ;; misc
264 ([?\C-s] . [?\C-f])
265 ([?\s-g] . [?\C-g])
266 ([?\s-s] . [?\C-s])
267 ([?\C-g] . [escape])
268 ([?\C-/] . [?\C-z])))
269
270 (require 'exwm-manage)
271 (setq
272 exwm-manage-configurations
273 '(((equal exwm-instance-name "floating")
274 floating t
275 floating-mode-line nil)))
276 (add-hook 'exwm-manage-finish-hook
277 (lambda ()
278 (when exwm-class-name
279 (cond
280 ((member exwm-class-name '("Abrowser" "IceCat" "Iceweasel"))
281 (exwm-input-set-local-simulation-keys
282 `(,@exwm-input-simulation-keys
283 ([?\C-\S-d] . [?\C-d]))))
284 ((member exwm-class-name '("XTerm" "Mate-terminal"))
285 (exwm-input-set-local-simulation-keys
286 '(([?\C-c ?\C-c] . [?\C-c])
287 ([?\C-c ?\C-u] . [?\C-u]))))
288 ((string= exwm-class-name "Zathura")
289 (exwm-input-set-local-simulation-keys
290 '(([?\C-p] . [C-up])
291 ([?\C-n] . [C-down]))))))))
292
293 (require 'exwm-randr)
294 (setq
295 exwm-randr-workspace-monitor-plist
296 '(0 "eDP-1"
297 1 "eDP-1" 2 "eDP-1" 3 "eDP-1"
298 4 "eDP-1" 5 "eDP-1" 6 "eDP-1"
299 7 "HDMI-1" 8 "HDMI-1" 9 "HDMI-1"))
300 ;; (add-hook
301 ;; 'exwm-randr-screen-change-hook
302 ;; (lambda ()
303 ;; (start-process-shell-command
304 ;; "xrandr" nil
305 ;; "xrandr --output HDMI-1 --mode 1280x720 --above eDP-1 --auto")))
306 (exwm-randr-enable)
307
308 (require 'exwm-systemtray)
309 (exwm-systemtray-enable)
310
311 (add-to-list 'load-path (b/lisp "exwm-edit"))
312 (require 'exwm-edit)
313
314 (with-eval-after-load 'exwm-workspace
315 (setq-default
316 mode-line-format
317 (append
318 mode-line-format
319 '((:eval
320 (format
321 " [%s]" (number-to-string
322 exwm-workspace-current-index)))))))
323
324 (provide 'bandali-exwm)
325 ;;; bandali-exwm.el ends here