* .local/bin/my-i3status: Switch to Python 3.
[~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-\s] . (lambda ()
97 ;; (interactive)
98 ;; (start-process-shell-command
99 ;; "rofi" nil "rofi -show run")))
100 ([?\S-\s-\s] . (lambda (command) ; doesn't work in X windows
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 "urxvt")))
108 ([S-s-return] . (lambda ()
109 (interactive)
110 (start-process "" nil "urxvt"
111 "-name" "floating")))
112 ([?\C-\s-\s] . counsel-linux-app)
113 ([?\M-\s-\s] . (lambda ()
114 (interactive)
115 (start-process-shell-command
116 "rofi-pass" nil "rofi-pass")))
117 ([?\s-h] . windmove-left)
118 ([?\s-j] . windmove-down)
119 ([?\s-k] . windmove-up)
120 ([?\s-l] . windmove-right)
121 ([?\s-H] . windmove-swap-states-left)
122 ([?\s-J] . windmove-swap-states-down)
123 ([?\s-K] . windmove-swap-states-up)
124 ([?\s-L] . windmove-swap-states-right)
125 ([?\s-N ?d] . (lambda ()
126 (interactive)
127 (start-process
128 "" nil "dunstctl" "close")))
129 ([?\s-N ?D] . (lambda ()
130 (interactive)
131 (start-process
132 "" nil "dunstctl" "close-all")))
133 ([?\s-N ?h] . (lambda ()
134 (interactive)
135 (start-process
136 "" nil "dunstctl" "history-pop")))
137 ([?\s-N return] . (lambda ()
138 (interactive)
139 (start-process
140 "" nil "dunstctl" "context")))
141 ([?\M-\s-h] . shrink-window-horizontally)
142 ([?\M-\s-l] . enlarge-window-horizontally)
143 ([?\M-\s-k] . shrink-window)
144 ([?\M-\s-j] . enlarge-window)
145 ([?\s-\[] . b/exwm-ws-prev)
146 ([?\s-\]] . b/exwm-ws-next)
147 ([mode-line mouse-4] . b/exwm-ws-prev) ; up
148 ([mode-line mouse-5] . b/exwm-ws-next) ; down
149 ([mode-line mouse-6] . b/exwm-ws-prev) ; left
150 ([mode-line mouse-7] . b/exwm-ws-next) ; right
151 ([?\s-{] . (lambda ()
152 (interactive)
153 (exwm-workspace-move-window
154 (b/exwm-ws-prev-index))))
155 ([?\s-}] . (lambda ()
156 (interactive)
157 (exwm-workspace-move-window
158 (b/exwm-ws-next-index))))
159 ,@(mapcar (lambda (i)
160 `(,(kbd (format "s-%d" i)) .
161 (lambda ()
162 (interactive)
163 (exwm-workspace-switch-create ,i))))
164 (number-sequence 0 (1- exwm-workspace-number)))
165 ,@(mapcar
166 (lambda (i)
167 `(,(kbd (format "s-%s"
168 (plist-get b/shifted-ws-names i)))
169 .
170 (lambda ()
171 (interactive)
172 (exwm-workspace-move-window ,i))))
173 (number-sequence 0 (1- exwm-workspace-number)))
174 ([?\s-F] . exwm-floating-toggle-floating)
175 ([?\s-f] . exwm-layout-toggle-fullscreen)
176 ([?\s-W] . (lambda ()
177 (interactive)
178 (kill-buffer (current-buffer))))
179 ([?\s-Q] . (lambda ()
180 (interactive)
181 (exwm-manage--kill-client)))
182 ([?\s-\'] . (lambda ()
183 (interactive)
184 (start-process-shell-command
185 "rofi-light" nil "rofi-light")))
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-s] . [?\C-s])
266 ([?\C-g] . [escape])))
267
268 (require 'exwm-manage)
269 (setq
270 exwm-manage-configurations
271 '(((equal exwm-instance-name "floating")
272 floating t
273 floating-mode-line nil)))
274 (add-hook 'exwm-manage-finish-hook
275 (lambda ()
276 (when exwm-class-name
277 (cond
278 ((member exwm-class-name '("Abrowser" "IceCat" "Iceweasel"))
279 (exwm-input-set-local-simulation-keys
280 `(,@exwm-input-simulation-keys
281 ([?\C-\S-d] . [?\C-d]))))
282 ((member exwm-class-name '("URxvt" "Mate-terminal"))
283 (exwm-input-set-local-simulation-keys
284 '(([?\C-c ?\C-c] . [?\C-c])
285 ([?\C-c ?\C-u] . [?\C-u]))))
286 ((string= exwm-class-name "Zathura")
287 (exwm-input-set-local-simulation-keys
288 '(([?\C-p] . [C-up])
289 ([?\C-n] . [C-down]))))))))
290
291 (require 'exwm-randr)
292 (setq
293 exwm-randr-workspace-monitor-plist
294 '(0 "eDP-1"
295 1 "eDP-1" 2 "eDP-1" 3 "eDP-1"
296 4 "eDP-1" 5 "eDP-1" 6 "eDP-1"
297 7 "HDMI-1" 8 "HDMI-1" 9 "HDMI-1"))
298 ;; (add-hook
299 ;; 'exwm-randr-screen-change-hook
300 ;; (lambda ()
301 ;; (start-process-shell-command
302 ;; "xrandr" nil
303 ;; "xrandr --output HDMI-1 --mode 1280x720 --above eDP-1 --auto")))
304 (exwm-randr-enable)
305
306 (require 'exwm-systemtray)
307 (exwm-systemtray-enable)
308
309 (add-to-list 'load-path (b/lisp "exwm-edit"))
310 (require 'exwm-edit)
311
312 (provide 'bandali-exwm)
313 ;;; bandali-exwm.el ends here