-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode-line.lisp
434 lines (362 loc) · 14.9 KB
/
mode-line.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
;; Copyright (C) 2006-2008 Shawn Betts
;; Copyright (C) 2016 Joram Schrijver
;;
;; This file is part of thesiswm.
;;
;; thesiswm 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.
;; thesiswm 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/>.
(in-package :thesiswm)
(export '(*mode-line-background-color*
*mode-line-border-color*
*mode-line-border-width*
*mode-line-foreground-color*
*mode-line-pad-x*
*mode-line-pad-y*
*mode-line-position*
*mode-line-timeout*
*screen-mode-line-format*
*screen-mode-line-formatters*
add-screen-mode-line-formatter
enable-mode-line
toggle-mode-line))
;;; Settings
(defvar *mode-line-position* :top
"Specifies where the mode line is displayed. Valid values are :top and :bottom.")
(defvar *mode-line-border-width* 1
"Specifies how thick the mode line's border will be. Integer value.")
(defvar *mode-line-pad-x* 5
"Specifies the number of padding pixels between the text and the side of the mode line. Integer value.")
(defvar *mode-line-pad-y* 1
"The number of padding pixels between the modeline text and the top/bottom of the modeline? Integer value.")
(defvar *mode-line-background-color* "Gray20"
"The mode line background color.")
(defvar *mode-line-foreground-color* "Gray50"
"The mode line foreground color.")
(defvar *mode-line-border-color* "Gray30"
"The mode line border color.")
(defvar *screen-mode-line-format* "[^B%n^b] %W"
"This variable describes what will be displayed on the modeline for each screen.
Turn it on with the function TOGGLE-MODE-LINE or the mode-line command.
It is a list where each element may be a string, a symbol, or a list.
For a symbol its value is used.
For a list of the form (:eval FORM) FORM is evaluated and the
result is used as a mode line element.
If it is a string the string is printed with the following formatting
options:
@table @asis
@item %h
List the number of the head the mode-line belongs to
@item %w
List all windows in the current group windows using @var{*window-format*}
@item %W
List all windows on the current head of the current group using
@var{*window-format*}
@item %g
List the groups using @var{*group-format*}
@item %n
The current group's name
@item %u
Using @var{*window-format*}, return a 1 line list of the urgent windows, space seperated.
@item %v
Using @var{*window-format*}, return a 1 line list of the windows, space
separated. The currently focused window is highlighted with
fmt-highlight. Any non-visible windows are colored the
*hidden-window-color*.
@item %d
Using @var{*time-modeline-string*}, print the time.
@end table
A number of modules have been written that extends the possible
formatting strings. See their documentation for details.")
(defvar *screen-mode-line-formatters* ()
"An alist containing format character format function pairs for
formatting screen mode-lines. functions are passed the mode line.")
(defvar *mode-line-timeout* 60
"The modeline updates after each command, when a new window appears or
an existing one disappears, and on a timer. This variable controls how
many seconds elapse between each update. If this variable is changed
while the modeline is visible, you must toggle the modeline to update
timer.")
(defvar *mode-line-timer* nil
"The timer that updates the modeline")
;;; Formatters
(defun add-screen-mode-line-formatter (character fmt-fun)
"Add a format function to a format character (or overwrite an existing one)."
(setf *screen-mode-line-formatters*
(cons (list character fmt-fun)
(remove character *screen-mode-line-formatters* :key #'first))))
;;; Mode lines
(defvar *mode-lines* ()
"All current mode lines.")
(defstruct (mode-line (:constructor %make-mode-line))
screen
head
window
format
position
contents
cc
height
factor
(mode :thesis))
;;; Utilities
(defun screen-mode-lines (screen)
(remove-if (lambda (mode-line)
(not (eq screen (mode-line-screen mode-line))))
*mode-lines*))
(defun head-mode-line (head)
(find head *mode-lines* :key #'mode-line-head))
(defun find-mode-line-by-window (xwin)
(find xwin *mode-lines* :key #'mode-line-window))
(defun mode-line-gc (ml)
(ccontext-gc (mode-line-cc ml)))
(defun turn-on-mode-line-timer ()
(when (timer-p *mode-line-timer*)
(cancel-timer *mode-line-timer*))
(setf *mode-line-timer* (run-with-timer *mode-line-timeout*
*mode-line-timeout*
'update-all-mode-lines)))
(defun maybe-cancel-mode-line-timer ()
(unless *mode-lines*
(when (timer-p *mode-line-timer*)
(cancel-timer *mode-line-timer*)
(setf *mode-line-timer* nil))))
;;; Creation
(defun resize-mode-line (mode-line)
(when (eq (mode-line-mode mode-line) :thesis)
;; This is a StumpWM mode-line
(setf (xlib:drawable-height (mode-line-window mode-line))
(+ (* 2 *mode-line-pad-y*)
(nth-value 1 (rendered-size
(split-string (mode-line-contents mode-line)
(string #\Newline))
(mode-line-cc mode-line))))))
(with-accessors ((window mode-line-window)
(head mode-line-head)
(position mode-line-position)
(height mode-line-height)
(factor mode-line-factor))
mode-line
(setf (xlib:drawable-width window) (- (frame-width head)
(* 2 (xlib:drawable-border-width
window)))
(xlib:drawable-height window) (min (xlib:drawable-height window)
(truncate (head-height head) 4))
height (+ (xlib:drawable-height window)
(* 2 (xlib:drawable-border-width window)))
factor (- 1 (/ height
(head-height head)))
(xlib:drawable-x window) (head-x head)
(xlib:drawable-y window) (if (eq position :top)
(head-y head)
(- (+ (head-y head)
(head-height head))
height)))))
(defun update-mode-line-color-context (ml)
(let* ((cc (mode-line-cc ml))
(screen (mode-line-screen ml))
(bright (lookup-color screen *mode-line-foreground-color*)))
(adjust-color bright 0.25)
(setf (ccontext-default-bright cc) (alloc-color screen bright))))
(defun make-mode-line-window (screen)
"Create a window suitable for a modeline."
(let ((win (xlib:create-window
:parent (screen-root screen)
:x 0 :y 0 :width 1 :height 1
:background (alloc-color screen *mode-line-background-color*)
:border (alloc-color screen *mode-line-border-color*)
:border-width *mode-line-border-width*
;; You can click the modeline
:event-mask (xlib:make-event-mask :button-press :exposure)
;; these windows are not controlled by the window manager
:override-redirect :on)))
(xlib:change-property
win
:_NET_WM_WINDOW_TYPE
(list (xlib:find-atom *display* :_NET_WM_WINDOW_TYPE_DESKTOP))
:atom
32)
win))
(defun make-mode-line-gc (window screen)
(xlib:create-gcontext
:drawable window
:font (when (typep (screen-font screen) 'xlib:font)
(screen-font screen))
:foreground (alloc-color screen *mode-line-foreground-color*)
:background (alloc-color screen *mode-line-background-color*)))
(defun make-mode-line-cc (window screen gc)
(make-ccontext :gc gc
:screen screen
:font (screen-font screen)
:win window
:default-fg (xlib:gcontext-foreground gc)
:default-bg (xlib:gcontext-background gc)))
(defun make-mode-line (screen head format)
(let* ((window (make-mode-line-window screen))
(gc (make-mode-line-gc window screen))
(cc (make-mode-line-cc window screen gc))
(mode-line (%make-mode-line :window window
:screen screen
:head head
:format format
:position *mode-line-position*
:cc cc)))
(prog1 mode-line
(push mode-line *mode-lines*)
(update-mode-line-color-context mode-line)
(resize-mode-line mode-line)
(xlib:map-window window)
(redraw-mode-line mode-line)
(dformat 3 "modeline: ~s~%" mode-line)
(turn-on-mode-line-timer)
(run-hook-with-args *new-mode-line-hook* mode-line))))
;;; Destruction
(defun sync-mode-line (ml)
(dolist (group (screen-groups (mode-line-screen ml)))
(group-sync-head group (mode-line-head ml))))
(defun destroy-mode-line (ml)
(run-hook-with-args *destroy-mode-line-hook* ml)
(xlib:destroy-window (mode-line-window ml))
(xlib:free-gcontext (mode-line-gc ml))
(setf *mode-lines* (remove ml *mode-lines*))
(sync-mode-line ml)
(maybe-cancel-mode-line-timer))
;;; Formatting
(defvar *current-mode-line-formatters* nil
"used in formatting modeline strings.")
(defvar *current-mode-line-formatter-args* nil
"used in formatting modeline strings.")
(defgeneric mode-line-format-elt (elt))
(defmethod mode-line-format-elt ((elt string))
(apply 'format-expand
*current-mode-line-formatters*
elt
*current-mode-line-formatter-args*))
(defmethod mode-line-format-elt ((elt symbol))
(if (boundp elt)
(let ((val (symbol-value elt)))
;; ignore T and nil, like emacs.
(unless (typep val 'boolean)
(mode-line-format-elt val)))
(symbol-name elt)))
(defmethod mode-line-format-elt ((elt null))
"")
(defmethod mode-line-format-elt ((elt list))
(etypecase (first elt)
((or string list)
(apply 'concatenate 'string
(mapcar 'mode-line-format-elt elt)))
(symbol
(mode-line-format-elt
(case (first elt)
;; FIXME: silently failing is probably not the best idea.
(:eval (ignore-errors (eval (second elt))))
(t (and (boundp (first elt))
(symbol-value (first elt))
(second elt))))))))
(defun mode-line-format-string (ml)
(mode-line-format-elt (mode-line-format ml)))
(defun redraw-mode-line (ml &optional force)
(when (eq (mode-line-mode ml) :thesis)
(let* ((*current-mode-line-formatters* *screen-mode-line-formatters*)
(*current-mode-line-formatter-args* (list ml))
(string (mode-line-format-string ml)))
(when (or force (not (string= (mode-line-contents ml) string)))
(setf (mode-line-contents ml) string)
(resize-mode-line ml)
(render-strings (mode-line-cc ml) *mode-line-pad-x* *mode-line-pad-y*
(split-string string (string #\Newline)) ())))))
(defun update-mode-lines (screen)
"Update all mode lines on SCREEN"
(dolist (mode-line (screen-mode-lines screen))
(redraw-mode-line mode-line)))
(defun update-all-mode-lines ()
"Update all mode lines."
(mapc 'redraw-mode-line *mode-lines*))
;;; External mode lines
(defun move-mode-line-to-head (mode-line head)
(cond ((not (head-mode-line head))
(setf (mode-line-head mode-line) head))
((mode-line-head mode-line)
(rotatef (mode-line-head mode-line)
(mode-line-head (head-mode-line head))))))
(defun update-mode-line-position (mode-line x y)
(let ((head (or (find-if (lambda (h) (and (= x (head-x h))
(>= y (head-y h))
(< y (+ (head-y h)
(head-height h)))))
(screen-heads (mode-line-screen mode-line)))
;; No luck. Just try to find a head without a mode-line
;; already.
(find-if-not #'head-mode-line
(screen-heads (mode-line-screen mode-line))))))
(when head
(unless (eq head (mode-line-head mode-line))
(move-mode-line-to-head mode-line head))
(when (mode-line-head mode-line)
(setf (mode-line-position mode-line)
(if (< y (/ (head-height (mode-line-head mode-line)) 2))
:top
:bottom))))))
(defun place-mode-line-window (screen xwin)
(let ((ml (%make-mode-line
:window xwin
:screen screen
:mode :visible
:position *mode-line-position*)))
(push ml *mode-lines*)
(xlib:reparent-window xwin (screen-root screen) 0 0)
(when (update-mode-line-position ml
(xlib:drawable-x xwin)
(xlib:drawable-y xwin))
(resize-mode-line ml)
(xlib:map-window xwin)
(sync-mode-line ml))))
;;; Toggling
(defun toggle-mode-line (screen head
&optional (format '*screen-mode-line-format*))
"Toggle the state of the mode line for the specified screen"
(check-type format (or symbol list string))
(let ((ml (head-mode-line head)))
(if ml
(case (mode-line-mode ml)
(:visible
;; Hide it.
(setf (mode-line-mode ml) :hidden)
(xlib:unmap-window (mode-line-window ml)))
(:hidden
;; Show it.
(setf (mode-line-mode ml) :visible)
(xlib:map-window (mode-line-window ml)))
(:thesis
;; Delete it
(destroy-mode-line ml)))
(make-mode-line screen head format))
(dolist (group (screen-groups screen))
(group-sync-head group head))))
(defun enable-mode-line (screen head state &optional format)
"Set the state of SCREEN's HEAD's mode-line. If STATE is T and FORMAT is
specified, then the mode-line's format is updated."
(check-type screen screen)
(check-type head head)
(check-type format (or symbol list string))
(let ((mode-line (head-mode-line head)))
(cond
((and state mode-line)
(when format
(setf (mode-line-format mode-line) format)))
(state
(toggle-mode-line screen head (or format '*screen-mode-line-format*)))
(mode-line
(toggle-mode-line screen head)))))
(defcommand mode-line () ()
"A command to toggle the mode line visibility."
(toggle-mode-line (current-screen) (current-head)))