-
Notifications
You must be signed in to change notification settings - Fork 266
/
group.lisp
593 lines (531 loc) · 25.3 KB
/
group.lisp
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
;; Copyright (C) 2003-2008 Shawn Betts
;;
;; This file is part of stumpwm.
;;
;; stumpwm is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; stumpwm is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING. If not, see
;; <http://www.gnu.org/licenses/>.
;; Commentary:
;;
;; All group related code resides here
;;
;; Code:
(in-package #:stumpwm)
(export '(current-group group-windows move-window-to-group add-group
;; Group accessors
group group-screen group-windows group-number group-name
;; Group API
group-startup group-add-window group-delete-window group-wake-up
group-suspend group-current-window group-current-head
group-resize-request group-move-request group-raise-request
group-lost-focus group-indicate-focus group-focus-window
group-button-press group-root-exposure group-add-head
group-remove-head group-before-resize-head group-after-resize-head
group-sync-all-heads group-sync-head))
(defvar *default-group-type* 'tile-group
"The type of group that should be created by default.")
(defvar *always-show-windows* ()
"The list of windows shown in all groups")
;;; The group API
(defgeneric group-startup (group)
(:documentation "Called on all groups while stumpwm is starting up."))
(defgeneric group-add-window (group window &key &allow-other-keys)
(:documentation "Called when a window is added to the group. All
house keeping is already taken care of. Only the group's specific
window managing housekeeping need be done. This function accepts keys
to inform the group on how to place the window."))
(defgeneric group-delete-window (group window)
(:documentation "Called when a window is removed from thegroup. All
house keeping is already taken care of. Only the group's specific
window managing housekeeping need be done."))
(defgeneric group-wake-up (group)
(:documentation "When the group becomes the current group, this
function is called. This call is expected to set the focus."))
(defgeneric group-suspend (group)
(:documentation "When the group is no longer the current group, this
function is called."))
(defgeneric group-current-window (group)
(:documentation "The group is asked to return its focused window."))
(defgeneric group-raised-window (group)
(:documentation "The group is asked to return its topmost window."))
(defgeneric group-current-head (group)
(:documentation "The group is asked to return its current head."))
(defgeneric group-resize-request (group window width height)
(:documentation "The window requested a width and/or height change."))
(defgeneric group-move-request (group window x y relative-to)
(:documentation "The window requested a position change."))
(defgeneric group-raise-request (group window type)
(:documentation "A request has been made to raise the window. TYPE
is the type of raise request being made. :MAP means the window has
made requested to be mapped. :above means the window has requested to
to be placed above its siblings."))
(defgeneric group-lost-focus (group)
(:documentation "The current window was hidden or destroyed or
something happened to it. So the group is asked to do something smart
about it."))
(defgeneric group-indicate-focus (group)
(:documentation "The group is asked to in some way show the user where the keyboard focus is."))
(defgeneric group-focus-window (group win)
(:documentation "The group is asked to focus the specified window wherever it is."))
(defgeneric group-button-press (group button x y child)
(:documentation "The user clicked somewhere in the group."))
(defgeneric group-root-exposure (group)
(:documentation "The root window got an exposure event. If the group
needs to redraw anything on it, this is where it should do it."))
(defgeneric group-add-head (group head)
(:documentation "A head is being added to this group's screen."))
(defgeneric group-remove-head (group head)
(:documentation "A head is being removed from this group's screen."))
(defgeneric group-replace-head (screen group old-head new-head)
(:documentation "A head is being replaced by another on this group's screen."))
(defgeneric group-before-resize-head (group oh nh)
(:documentation "A head is about to be resized on this group's screen."))
(defgeneric group-after-resize-head (group head)
(:documentation "A head has been resized on this group's screen."))
(defgeneric group-sync-all-heads (group)
(:documentation "Called when the head configuration for the group changes."))
(defgeneric group-sync-head (group head)
(:documentation "When a head or its usable area is resized, this is
called. When the modeline size changes, this is called."))
(defgeneric group-repack-frame-numbers (group)
(:documentation "Repack frame numbers to range from zero to the number of
frames such that there are no numerical gaps."))
(defgeneric group-adopt-orphaned-windows (group &optional screen)
(:documentation "Adopts window that have been orphaned (such as being in a frame
that no longer belongs to any group) into the given group, defaults to searching
the current screen"))
(define-swm-class group ()
((screen :initarg :screen :accessor group-screen)
(windows :initform nil :accessor group-windows)
(current-window :initform nil :accessor group-current-window)
(raised-window :initform nil :accessor group-raised-window)
(number :initarg :number :accessor group-number)
(name :initarg :name :accessor group-name)
(on-top-windows :initform nil :accessor group-on-top-windows)))
(defmethod print-swm-object ((object group) stream)
(format stream "GROUP ~A" (ignore-errors (group-name object))))
(defmethod group-delete-window (group window)
(when (find window *always-show-windows*)
(disable-always-show-window window (current-screen)))
(call-next-method))
(defun current-group (&optional (screen (current-screen)))
"Return the current group for the current screen, unless
otherwise specified."
(screen-current-group screen))
(defun move-group-to-head (screen group)
"Move window to the head of the group's window list."
;(assert (member window (screen-mapped-windows screen)))
(move-to-head (screen-groups screen) group))
(defun sort-groups (screen)
"Return a copy of the screen's group list sorted by number."
(sort1 (screen-groups screen) '< :key 'group-number))
(defun group-map-number (group)
(let* ((num (group-number group))
(index (1- (abs num))))
(if (and (>= index 0)
(< index (length *group-number-map*)))
(format nil "~:[~;-~]~a" (minusp num) (elt *group-number-map* index))
(princ-to-string num))))
(defun fmt-group-status (group)
(let ((screen (group-screen group)))
(cond ((eq group (screen-current-group screen))
#\*)
((and (typep (second (screen-groups screen)) 'group)
(eq group (second (screen-groups screen))))
#\+)
(t #\-))))
(defun find-free-group-number (screen)
"Return a free group number in SCREEN."
(find-free-number (mapcar 'group-number (screen-groups screen)) 1))
(defun find-free-hidden-group-number (screen)
"Return a free hidden group number for SCREEN. Hidden group numbers
start at -1 and go down."
(find-free-number (mapcar 'group-number (screen-groups screen)) -1 :negative))
(defun non-hidden-groups (groups)
"Return only those groups that are not hidden."
(remove-if (lambda (g)
(< (group-number g) 1))
groups))
(defun netwm-group-id (group)
"netwm specifies that desktop/group numbers are contiguous and start
at 0. Return a netwm compliant group id."
(let ((screen (group-screen group)))
(position group (sort-groups screen))))
(defun switch-to-group (new-group)
(let* ((screen (group-screen new-group))
(old-group (screen-current-group screen)))
(unless (eq new-group old-group)
;; restore the visible windows
(dolist (w (group-windows new-group))
(when (eq (window-state w) +normal-state+)
(xwin-unhide (window-xwin w) (window-parent w))))
;; hide the old group's windows
(dolist (w (reverse (group-windows old-group)))
(when (eq (window-state w) +normal-state+)
(xwin-hide w)))
(setf (screen-current-group screen) new-group)
(move-group-to-head screen new-group)
;; restore the focus
(setf (screen-focus screen) nil)
(group-wake-up new-group)
(xlib:change-property (screen-root screen) :_NET_CURRENT_DESKTOP
(list (netwm-group-id new-group))
:cardinal 32)
(mapc (lambda (w)
(xwin-unhide (window-xwin w) (window-parent w)))
*always-show-windows*)
(update-all-mode-lines)
(run-hook-with-args *focus-group-hook* new-group old-group))))
(defun copy-window-to-group (group window)
(setf (window-number window) (find-free-window-number group))
(push window (group-windows group))
(group-add-window group window))
(defun always-show-window (window screen)
(let ((groups-to-add-w-to (remove (current-group) (screen-groups screen))))
(mapc (lambda (group) (copy-window-to-group group window))
groups-to-add-w-to))
(xlib:change-property (window-xwin window) :_NET_WM_DESKTOP
(list #xFFFFFFFF)
:cardinal 32)
(push window *always-show-windows*))
(defun disable-always-show-window (window screen)
(let* ((g (current-group))
(groups-to-remove-w-from (remove g (screen-groups screen))))
(mapc (lambda (group)
(setf (group-windows group)
(remove window (group-windows group))))
groups-to-remove-w-from)
(setf (window-group window) g
(window-number window) (find-free-window-number g)
*always-show-windows* (remove window *always-show-windows*))
(xlib:change-property (window-xwin window) :_NET_WM_DESKTOP
(list (netwm-group-id g))
:cardinal 32)))
(defcommand toggle-always-show () ()
"Toggle whether the current window is shown in all groups."
(let ((w (current-window))
(screen (current-screen)))
(when w
(if (find w *always-show-windows*)
(disable-always-show-window w screen)
(always-show-window w screen)))))
(defun move-window-to-group (window to-group)
(if (equalp to-group (window-group window))
(message "That window is already in the group ~a." (group-name to-group))
(labels ((really-move-window (window to-group)
(unless (eq (window-group window) to-group)
(hide-window window)
;; house keeping
(setf (group-windows (window-group window))
(remove window (group-windows (window-group window))))
(group-delete-window (window-group window) window)
(setf (window-group window) to-group
(window-number window) (find-free-window-number to-group))
(push window (group-windows to-group))
(xlib:change-property (window-xwin window) :_NET_WM_DESKTOP
(list (netwm-group-id to-group))
:cardinal 32)
(group-add-window to-group window))))
;; When a modal window is moved, all the windows it shadows must be moved
;; as well. When a shadowed window is moved, the modal shadowing it must
;; be moved.
(cond
((window-modal-p window)
(mapc (lambda (w)
(really-move-window w to-group))
(append (list window) (shadows-of window))))
((modals-of window)
(mapc (lambda (w)
(move-window-to-group w to-group))
(modals-of window)))
(t
(really-move-window window to-group))))))
(defun next-group (current &optional
(groups (non-hidden-groups (screen-groups
(group-screen current)))))
"Return the group following @var{current} in @var{groups}. If none
are found return @code{NIL}."
(let* ((matches (member current groups))
(next-group (if (null (cdr matches))
;; If the last one in the list is current, then
;; use the first one.
(car groups)
;; Otherwise, use the next one in the list.
(cadr matches))))
(unless (eq next-group current)
next-group)))
(defun merge-groups (from-group to-group)
"Merge all windows in FROM-GROUP into TO-GROUP."
(dolist (window (group-windows from-group))
(move-window-to-group window to-group)))
(defun netwm-group (window &optional (screen (window-screen window)))
"Get the window's desktop property and return a matching group, if
there exists one."
(let ((id (first (xlib:get-property (window-xwin window) :_NET_WM_DESKTOP))))
(when (and id (< id (length (screen-groups screen))))
(elt (sort-groups screen) id))))
(defun netwm-set-group (window)
"Set the desktop property for the given window."
(xlib:change-property (window-xwin window) :_NET_WM_DESKTOP
(list (netwm-group-id (window-group window)))
:cardinal 32))
(defun netwm-set-allowed-actions (window)
(xlib:change-property (window-xwin window) :_NET_WM_ALLOWED_ACTIONS
(mapcar (lambda (a)
(xlib:intern-atom *display* a))
+netwm-allowed-actions+)
:atom 32))
(defun netwm-update-group (group index)
(dolist (w (group-windows group))
(xlib:change-property (window-xwin w) :_NET_WM_DESKTOP
(list index)
:cardinal 32)))
(defun netwm-update-groups (screen)
"Update all windows to reflect a change in the group list."
;; FIXME: This could be optimized only to update windows when there
;; is a need.
(loop for i from 0
for group in (sort-groups screen)
do (netwm-update-group group i)))
(defun netwm-set-group-properties (screen)
"Set NETWM properties regarding groups of SCREEN.
Groups are known as \"virtual desktops\" in the NETWM standard."
(let ((root (screen-root screen)))
;; _NET_NUMBER_OF_DESKTOPS
(xlib:change-property root :_NET_NUMBER_OF_DESKTOPS
(list (length (screen-groups screen)))
:cardinal 32)
(unless *initializing*
;; _NET_CURRENT_DESKTOP
(xlib:change-property root :_NET_CURRENT_DESKTOP
(list (netwm-group-id (screen-current-group screen)))
:cardinal 32))
;; _NET_DESKTOP_NAMES
(xlib:change-property root :_NET_DESKTOP_NAMES
(let ((names (mapcan
(lambda (group)
(list (sb-ext:string-to-octets
(group-name group)
:external-format :utf-8)
'(0)))
(sort-groups screen))))
(apply #'concatenate 'list names))
:UTF8_STRING 8)))
(defun kill-group (group to-group)
(unless (eq group to-group)
(let ((screen (group-screen group)))
(merge-groups group to-group)
(setf (screen-groups screen) (remove group (screen-groups screen)))
(netwm-update-groups screen)
(netwm-set-group-properties screen))))
(defun %ensure-group (group-name group-type screen)
"If there is a group named with GROUP-NAME in SCREEN return it, otherwise create it."
(or (find-group screen group-name)
(let ((group (make-swm-class-instance
group-type
:screen screen
:number (if (char= (char group-name 0) #\.)
(find-free-hidden-group-number screen)
(find-free-group-number screen))
:name group-name)))
(setf (screen-groups screen) (append (screen-groups screen) (list group)))
(netwm-set-group-properties screen)
(netwm-update-groups screen)
group)))
(defun add-group (screen name &key background (type *default-group-type*))
"Create a new group in SCREEN with the supplied name. group names
starting with a . are considered hidden groups. Hidden groups are
skipped by gprev and gnext and do not show up in the group
listings (unless *list-hidden-groups* is T). They also use negative
numbers."
(check-type screen screen)
(check-type name string)
(assert (not (member name '("" ".") :test #'string=)) (name) "Groups must have a name.")
(let ((group (%ensure-group name type screen)))
(unless background
(switch-to-group group))
(mapc (lambda (window) (copy-window-to-group group window))
*always-show-windows*)
group))
(defun find-group (screen name)
"Return the group with the name, NAME. Or NIL if none exists."
(find name (screen-groups screen) :key 'group-name :test 'string=))
;;; Group commands
;; FIXME: groups are to screens exactly as windows are to
;; groups. There is a lot of duplicate code that could be globbed
;; together.
(defun group-forward (current list)
"Switch to the next non-hidden-group in the list, if one
exists. Returns the new group."
(if-let ((next (next-group current (non-hidden-groups list))))
(progn (switch-to-group next)
next)
(message "No other group.")))
(defun group-forward-with-window (current list)
"Switch to the next group in the list, if one exists, and moves the
current window of the current group to the new one."
(when-let ((next (group-forward current list))
(win (group-current-window current)))
(move-window-to-group win next)
(focus-all win)))
(defcommand gnew (name) ((:string "Group name: "))
"Create a new group with the specified name. The new group becomes the
current group. If @var{name} begins with a dot (``.'') the group new
group will be created in the hidden state. Hidden groups have group
numbers less than one and are invisible to from gprev, gnext, and, optionally,
groups and vgroups commands."
(unless name
(throw 'error :abort))
(add-group (current-screen) name))
(defcommand gnewbg (name) ((:string "Group name: "))
"Create a new group but do not switch to it."
(unless name
(throw 'error :abort))
(add-group (current-screen) name :background t))
(defcommand gnext () ()
"Cycle to the next group in the group list."
(group-forward (current-group)
(sort-groups (current-screen))))
(defcommand gprev () ()
"Cycle to the previous group in the group list."
(group-forward (current-group)
(reverse (sort-groups (current-screen)))))
(defcommand gnext-with-window () ()
"Cycle to the next group in the group list, taking the current
window along."
(group-forward-with-window (current-group)
(sort-groups (current-screen))))
(defcommand gprev-with-window () ()
"Cycle to the previous group in the group list, taking the current
window along."
(group-forward-with-window (current-group)
(reverse (sort-groups (current-screen)))))
(defcommand gother () ()
"Go back to the last group."
(let ((groups (screen-groups (current-screen))))
(if (> (length groups) 1)
(switch-to-group (second groups))
(message "No other group."))))
(defun %grename (name group)
(let ((group-name (group-name group)))
(cond ((and (starts-with #\. name) ; change to hidden group
(not (starts-with #\. group-name)))
(setf (group-number group)
(find-free-hidden-group-number (current-screen))))
((and (not (starts-with #\. name)) ; change from hidden group
(starts-with #\. group-name))
(setf (group-number group)
(find-free-group-number (current-screen))))))
(setf (group-name group) name))
(defcommand grename (name) ((:string "New name for group: "))
"Rename the current group."
(cond ((find-group (current-screen) name)
(message "^1*^BError: Name already exists."))
((or (zerop (length name))
(string= name "."))
(message "^1*^BError: Name cannot be empty name."))
(t (%grename name (current-group)))))
(defun echo-groups (screen fmt &optional verbose (wfmt *window-format*))
"Print a list of the windows to the screen."
(let* ((groups (sort-groups screen))
(names (mapcan (lambda (g)
(list*
(format-expand *group-formatters* fmt g)
(when verbose
(mapcar (lambda (w)
(format-expand *window-formatters*
(concatenate 'string " " wfmt)
w))
(sort-windows g)))))
(if *list-hidden-groups* groups (non-hidden-groups groups)))))
(echo-string-list screen names)))
(defcommand groups (&optional (fmt *group-format*)) (:rest)
"Display the list of groups with their number and
name. @var{*group-format*} controls the formatting. The optional
argument @var{fmt} can be used to override the default group
formatting."
(echo-groups (current-screen) fmt))
(defcommand vgroups (&optional gfmt wfmt) (:string :rest)
"Like @command{groups} but also display the windows in each group. The
optional arguments @var{gfmt} and @var{wfmt} can be used to override
the default group formatting and window formatting, respectively."
(echo-groups (current-screen)
(or gfmt *group-format*)
t (or wfmt *window-format*)))
(defcommand gselect (&optional to-group) (:rest)
"Accepts numbers to select a group, otherwise grouplist selects."
(if-let ((to-group (when to-group
(select-group (current-screen) to-group))))
(switch-to-group to-group)
(grouplist)))
(defcommand grouplist (&optional (fmt *group-format*)) (:rest)
"Allow the user to select a group from a list, like windowlist for groups."
(when-let ((group (second (select-from-menu
(current-screen)
(mapcar (lambda (g)
(list (format-expand *group-formatters* fmt g) g))
(screen-groups (current-screen)))))))
(switch-to-group group)))
(defcommand gmove (to-group) ((:group "To group: "))
"Move the current window to the specified group."
(when (and to-group
(current-window))
(move-window-to-group (current-window) to-group)))
(defcommand gmove-and-follow (to-group) ((:group "To group: "))
"Move the current window to the specified group, and switch to it."
(let ((window (current-window)))
(gmove to-group)
(switch-to-group to-group)
(when window (really-raise-window window))))
(defcommand gmove-marked (to-group) ((:group "To group: "))
"move the marked windows to the specified group."
(when to-group
(let ((group (current-group)))
(dolist (i (marked-windows group))
(setf (window-marked i) nil)
(move-window-to-group i to-group)))))
(defcommand gkill () ()
"Kill the current group. All windows in the current group are migrated
to the next group."
(let* ((dead-group (current-group))
(groups (screen-groups (current-screen)))
;; If no "visible" group is found, try with all groups
(to-group (or (next-group dead-group (non-hidden-groups groups))
(next-group dead-group groups))))
(if to-group
(if (or (not %interactivep%)
(not (group-windows dead-group))
(y-or-n-p
(format nil "You are about to kill non-empty group \"^B^3*~a^n\"
The windows will be moved to group \"^B^2*~a^n\"
^B^6*Confirm?^n " (group-name dead-group) (group-name to-group))))
(let ((dead-group-name (group-name dead-group)))
(switch-to-group to-group)
(kill-group dead-group to-group)
(message "Deleted ~a." dead-group-name))
(message "Canceled."))
(message "There's only one group left."))))
(defcommand gkill-other () ()
"Kill other groups. All windows in other groups are migrated
to the current group."
(let* ((current-group (current-group))
(groups (remove current-group
(screen-groups (current-screen)))))
(if (null groups)
(message "No other groups.")
(progn (dolist (dead-group groups)
(kill-group dead-group current-group))
(message "Killed other groups.")))))
(defcommand gmerge (from) ((:group "From group: "))
"Merge @var{from} into the current group. @var{from} is not deleted."
(if (eq from (current-group))
(message "^B^3*Cannot merge group with itself!")
(merge-groups from (current-group))))