-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm.clj
337 lines (281 loc) · 9.94 KB
/
wm.clj
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
(ns clawe.wm
(:require
[systemic.core :as sys :refer [defsys]]
[ralphie.zsh :as zsh]
[clawe.awesome :as clawe.awesome]
[clawe.config :as clawe.config]
[clawe.wm.protocol :as wm.protocol]
[clawe.yabai :as clawe.yabai]
[clawe.client :as client]
[clojure.string :as string])
(:import
[clawe.awesome Awesome]
[clawe.yabai Yabai]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; window manager sys
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defsys *wm*
:extra-deps
[clawe.config/*config*]
:start
(if (clawe.config/is-mac?) (Yabai.) (Awesome.)))
(defn reload-wm []
(when (sys/running? `*wm*)
(sys/restart! `*wm*)))
(comment
(reload-wm)
(sys/start! `*wm*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; workspace helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def default-directory (zsh/expand "~"))
(defn- ensure-directory
"Makes sure the passed workspace has a `:workspace/directory` set.
If missing, sets it to `default-directory`.
If it starts with `~`, `zsh/expands` it."
[{:keys [workspace/directory] :as wsp}]
(cond
(not directory)
(assoc wsp :workspace/directory default-directory)
(re-seq #"^~" directory)
(assoc wsp :workspace/directory (zsh/expand directory))
;; TODO consider checking/converting mismatched /Users/<user> vs /home/<user> paths
:else wsp))
(defn- merge-with-workspace-def
"Merges the passed workspace with a workspace-def from resouces/clawe.edn.
Matches using the `:workspace/title`."
[{:keys [workspace/title] :as wsp}]
(merge wsp (clawe.config/workspace-def title)))
(defn- merge-with-client-def
"Attempts to merge the passed client with a matching client-def from resouces/clawe.edn.
Matches using `client/match?`."
;; TODO should we supply a workspace-title here for use-workspace-title matches/merges?
[client]
(let [defs (clawe.config/client-defs)
matches
(->> defs (filter
(fn [def]
;; does this imply that clients should carry :match opts?
(client/match?
;; client-def can supply specific :match options
;; client def can 'merge' more freely (via :merge/skip-title)
(if (:merge/skip-title def)
(assoc def :match/skip-title true)
def)
client def))))]
(cond
(> (count matches) 1)
(do
(println "WARN: multiple matching defs found for client" (client/strip client) matches)
(merge client (some->> matches first)))
(= (count matches) 0)
;; TODO this works for most cases but breaks when skipping title on emacs matches
;; b/c the journal and wsp-emacs clients both match for skip-title emacs clients
(do
(println "WARN: zero matching defs found for client" (client/strip client))
client)
:else
(merge client (some->> matches first)))))
(defn- merge-client-defs [wsp]
(update wsp :workspace/clients #(->> % (map merge-with-client-def))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; workspace fetching
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn workspace-defs
"Returns all possible workspaces, as supported by `clawe.config/workspace-defs`,
(backed by `resources/clawe.edn`).
Supports opening a new workspace."
([] (workspace-defs nil))
([_opts]
(->> (clawe.config/workspace-defs)
(map (fn [[title def]] (assoc def :workspace/title title)))
(map ensure-directory))))
(defn current-workspaces
([] (current-workspaces nil))
([opts]
(sys/start! `*wm*)
(->>
(wm.protocol/-current-workspaces *wm* opts)
;; TODO apply malli transform here instead of in the protocols
(map merge-with-workspace-def)
(map ensure-directory)
(map merge-client-defs))))
(defn current-workspace
([] (current-workspace nil))
([opts]
(sys/start! `*wm*)
(some->> (current-workspaces opts) first)))
(defn active-workspaces
([] (active-workspaces nil))
([opts]
(sys/start! `*wm*)
(->> (wm.protocol/-active-workspaces *wm* opts)
;; TODO apply malli transform here instead of in the protocols?
(map merge-with-workspace-def)
(map ensure-directory)
(map merge-client-defs))))
(defn fetch-workspace
([workspace-or-title] (fetch-workspace nil workspace-or-title))
([opts workspace-or-title]
(sys/start! `*wm*)
(let [title (if (string? workspace-or-title)
workspace-or-title (:workspace/title workspace-or-title))]
(some->
(wm.protocol/-fetch-workspace *wm* opts title)
merge-with-workspace-def
ensure-directory
merge-client-defs))))
(comment
clawe.config/*config*
(clawe.config/reload-config)
(current-workspace)
(fetch-workspace "madeup")
(fetch-workspace {:include-clients true}
{:workspace/title "slack"})
(->>
(active-workspaces)
(remove :workspace/title))
(wm.protocol/-fetch-workspace *wm* nil "invented"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; client fetching
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn client-defs
"Returns client-defs, as defined in `resources/clawe.edn`.
Supports opening new clients, finding matching clients, and toggling clients into view."
([] (client-defs nil))
([_opts]
(clawe.config/client-defs)))
;; we could be locally caching calls like these, if we're willing to cache burst
(defn active-clients
([] (active-clients nil))
([opts]
(sys/start! `*wm*)
(->>
(wm.protocol/-active-clients *wm* opts)
(map merge-with-client-def))))
(defn focused-client []
;; TODO refactor into the protocol (perf)
(some->>
(active-clients)
(filter :client/focused)
first))
(defn fetch-client [client]
(when-let [client (cond (map? client) client
(string? client) (clawe.config/client-def client))]
;; TODO refactor into protocol (perf)
(some->>
(active-clients)
(filter (partial client/match? client))
first)))
(comment
(fetch-client "journal")
(focused-client)
(active-clients))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; interactions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; crud
(defn create-workspace
([wsp-title] (create-workspace nil wsp-title))
([opts wsp-title]
(sys/start! `*wm*)
(wm.protocol/-create-workspace *wm* opts wsp-title)))
(defn delete-workspace
[workspace]
(sys/start! `*wm*)
(wm.protocol/-delete-workspace *wm* workspace))
(comment
(->>
(active-workspaces)
(remove :workspace/title)
#_(map delete-workspace)
))
(defn close-client
([client] (close-client nil client))
([opts client]
(sys/start! `*wm*)
(wm.protocol/-close-client *wm* opts client)))
(comment
(close-client nil))
;; focus
(defn focus-workspace
([workspace] (focus-workspace nil workspace))
([opts workspace]
(sys/start! `*wm*)
(wm.protocol/-focus-workspace *wm* opts workspace)))
(defn focus-client
([client] (focus-client
;; client supplies opts in single arity case
client client))
([opts client]
(sys/start! `*wm*)
(let [client (cond (map? client) client
(string? client) (fetch-client client))]
(wm.protocol/-focus-client *wm* opts client))))
(declare move-client-to-workspace)
(defn show-client
"Opts `:current-workspace`, `:focus/float-and-center`, `focus-client` opts"
([client] (show-client nil client))
([opts client]
(let [client (cond (map? client) client
(string? client) (fetch-client client))]
;; TODO would like to focus+center first to avoid the jank on linux
;; but on osx, do it the other way to avoid the auto-space-switch.
;; suppose we need to float and center, then switch, then focus
(move-client-to-workspace
client (or (:current-workspace opts) (current-workspace)))
(focus-client (merge {:float-and-center (:focus/float-and-center client true)} opts)
client))))
(defn client->workspace-title [client]
(or (:client/workspace-title client)
(let [wt (:client/window-title client)]
(cond
(re-seq #" " wt)
;; term before first space
(->> (string/split wt #" ") first)
:else wt))))
(comment
(clawe.config/reload-config)
(->>
(active-clients)
(filter (comp #{"Emacs"} :client/app-name))
(map client/strip)
(map #(assoc % :wsp-title (client->workspace-title %)))
)
)
;; maybe something more like toggle->away - something more specific to the toggle UX
(defn hide-client
([client] (hide-client nil client))
([opts client]
(sys/start! `*wm*)
;; TODO document :hide/ types for clients
(case (:hide/type client :hide/scratchpad)
:hide/scratchpad
(wm.protocol/-move-client-to-workspace
*wm* nil client
(client->workspace-title client))
:hide/os-hide
(println "to impl!")
;; (wm.protocol/-hide-client *wm* opts client)
:hide/close
(wm.protocol/-close-client *wm* opts client)
;; default to os level close.... pr
(println "WARN: unsupported :hide/type" opts))))
;; rearranging
(defn swap-workspaces-by-index
[index-a index-b]
(sys/start! `*wm*)
(wm.protocol/-swap-workspaces-by-index *wm* index-a index-b))
;; TODO consider impling a fallback if this isn't impled
(defn drag-workspace
[dir]
(sys/start! `*wm*)
(wm.protocol/-drag-workspace *wm* dir))
(comment
(drag-workspace :dir/up)
(drag-workspace :dir/down))
(defn move-client-to-workspace
([c wsp] (move-client-to-workspace nil c wsp))
([opts c wsp]
(sys/start! `*wm*)
(wm.protocol/-move-client-to-workspace *wm* opts c wsp)))