File: group.lisp

package info (click to toggle)
stumpwm 2:1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,216 kB
  • sloc: lisp: 13,721; makefile: 180; sh: 30
file content (496 lines) | stat: -rw-r--r-- 21,523 bytes parent folder | download
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
;; 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-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.")

(defclass group ()
  ((screen :initarg :screen :accessor group-screen)
   (windows :initform nil :accessor group-windows)
   (number :initarg :number :accessor group-number)
   (name :initarg :name :accessor group-name)))

;;; 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-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 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-resize-head (group oh nh)
  (:documentation "A head is being 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."))

(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))))
      (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)
      (update-all-mode-lines)
      (run-hook-with-args *focus-group-hook* new-group old-group))))

(defun move-window-to-group (window 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))))
    (if (eq next-group current)
        nil
        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-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 (dolist (w (group-windows group))
             (xlib:change-property (window-xwin w) :_NET_WM_DESKTOP
                                   (list i)
                                   :cardinal 32))))

(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 (string-to-utf8 (group-name group))
                                                '(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 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)
  (if (or (string= name "")
          (string= name "."))
      (error "Groups must have a name.")
      (let ((ng (or (find-group screen name)
                    (let ((ng (make-instance type
                                             :screen screen
                                             :number (if (char= (char name 0) #\.)
                                                         (find-free-hidden-group-number screen)
                                                         (find-free-group-number screen))
                                             :name name)))
                      (setf (screen-groups screen) (append (screen-groups screen) (list ng)))
                      (netwm-set-group-properties screen)
                      (netwm-update-groups screen)
                      ng))))
        (unless background
          (switch-to-group ng))
        ng)))

(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."
  (let ((ng (next-group current (non-hidden-groups list))))
    (when ng
      (switch-to-group ng)
      ng)))

(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."
  (let ((next (group-forward current list))
        (win (group-current-window current)))
    (when (and next win)
      (move-window-to-group win next)
      (really-raise-window 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."
  (add-group (current-screen) name))

(defcommand gnewbg (name) ((:string "Group Name: "))
  "Create a new group but do not switch to it."
  (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))))
    (when (> (length groups) 1)
      (switch-to-group (second groups)))))

(defcommand grename (name) ((:string "New name for group: "))
  "Rename the current group."
  (let ((group (current-group)))
    (cond ((find-group (current-screen) name)
           (message "^1*^BError: Name already exists"))
          ((or (zerop (length name))
               (string= name "."))
           (message "^1*^BError: empty name"))
          (t
           (cond ((and (char= (char name 0) #\.) ;change to hidden group
                       (not (char= (char (group-name group) 0) #\.)))
                  (setf (group-number group) (find-free-hidden-group-number (current-screen))))
                 ((and (not (char= (char name 0) #\.)) ;change from hidden group
                       (char= (char (group-name group) 0) #\.))
                  (setf (group-number group) (find-free-group-number (current-screen)))))
           (setf (group-name group) name)))))

(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 (to-group) ((:group "Select Group: "))
"Select the first group that starts with
@var{substring}. @var{substring} can also be a number, in which case
@command{gselect} selects the group with that number."
  (when to-group
    (switch-to-group to-group)))

(defcommand grouplist (&optional (fmt *group-format*)) (:rest)
  "Allow the user to select a group from a list, like windowlist but
  for groups"
  (let ((group (second (select-from-menu
		(current-screen)
		(mapcar (lambda (g)
			  (list (format-expand *group-formatters* fmt g) g))
			(screen-groups (current-screen)))))))
    (when group
      (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)
    (gselect 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))))
            (progn
              (switch-to-group to-group)
              (kill-group dead-group to-group)
              (message "Deleted"))
            (message "Canceled"))
        (message "There's only one group left"))))

(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))))