blob: da99ab03e03a7e67b674e8c4a8ece4259bbb4979 (
plain)
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
|
(in-package #:wallpaper-switcher)
;;; Finding wallpapers
(defun wallpaper-p (pathname extensions)
(let ((type (pathname-type pathname)))
(and type (member type extensions :test #'string-equal))))
(defun find-wallpapers (directory extensions)
"Image files directly inside DIRECTORY (not subdirectories), sorted by
name. Returns NIL if DIRECTORY does not exist."
(let ((dir (uiop:ensure-directory-pathname (expand-path directory))))
(when (uiop:directory-exists-p dir)
(sort (remove-if-not (lambda (p) (wallpaper-p p extensions))
(uiop:directory-files dir))
#'string-lessp
:key #'file-namestring))))
(defun directory-name (directory)
"The last component of DIRECTORY, e.g. \"nature\" for /walls/nature/."
(let ((last (car (last (pathname-directory directory)))))
(if (stringp last) last (namestring directory))))
(defun hidden-p (name)
(and (plusp (length name)) (char= (char name 0) #\.)))
(defun find-subdirectories (directory)
"Non-hidden subdirectories directly inside DIRECTORY, sorted by name."
(let ((dir (uiop:ensure-directory-pathname (expand-path directory))))
(when (uiop:directory-exists-p dir)
(sort (remove-if (lambda (d) (hidden-p (directory-name d)))
(uiop:subdirectories dir))
#'string-lessp
:key #'directory-name))))
;;; Escaping
(defun shell-quote (string)
"Quote STRING as one POSIX shell word: wrap it in single quotes and write
each embedded ' as '\\''."
(with-output-to-string (out)
(write-char #\' out)
(loop for c across string
do (if (char= c #\')
(write-string "'\\''" out)
(write-char c out)))
(write-char #\' out)))
(defun xml-escape (string)
(with-output-to-string (out)
(loop for c across string
do (case c
(#\& (write-string "&" out))
(#\< (write-string "<" out))
(#\> (write-string ">" out))
(#\" (write-string """ out))
(#\' (write-string "'" out))
(t (write-char c out))))))
(defun escape-mnemonic (name)
"Openbox reads _ in a label as a keyboard shortcut marker, so each _ is
doubled to show it as-is."
(with-output-to-string (out)
(loop for c across name
do (write-char c out)
(when (char= c #\_) (write-char c out)))))
(defun menu-label (pathname)
"Menu label for PATHNAME: a file's name without its extension, or a
directory's last component."
(escape-mnemonic
(if (uiop:directory-pathname-p pathname)
(directory-name pathname)
(or (pathname-name pathname) (file-namestring pathname)))))
;;; Building the Execute command
(defun expand-template (template file)
"Replace %f in TEMPLATE with FILE shell-quoted, and %% with %."
(with-output-to-string (out)
(loop with i = 0
with n = (length template)
while (< i n)
do (let ((c (char template i)))
(if (and (char= c #\%) (< (1+ i) n))
(let ((next (char template (1+ i))))
(case next
(#\f (write-string (shell-quote file) out) (incf i 2))
(#\% (write-char #\% out) (incf i 2))
(t (write-char c out) (incf i))))
(progn (write-char c out) (incf i)))))))
(defun build-command (template pathname)
"The command line for one menu item. Openbox splits Execute commands
like a shell does but does not run them through one, so the expanded
template is passed to `sh -c` as a single quoted argument."
(format nil "sh -c ~A"
(shell-quote (expand-template template (uiop:native-namestring pathname)))))
;;; Rendering
(defun indent (stream depth)
(loop repeat (* 2 depth) do (write-char #\Space stream)))
(defun render-item (stream depth pathname command)
(indent stream depth)
(format stream "<item label=\"~A\">~%" (xml-escape (menu-label pathname)))
(indent stream (1+ depth))
(format stream "<action name=\"Execute\"><command>~A</command></action>~%"
(xml-escape (build-command command pathname)))
(indent stream depth)
(format stream "</item>~%"))
(defun render-empty (stream depth directory)
(indent stream depth)
(format stream "<separator label=\"~A\"/>~%"
(xml-escape (format nil "No wallpapers in ~A"
(uiop:native-namestring directory)))))
(defun submenu-id (directory)
"Openbox needs a unique id for every inline submenu; the path is one."
(format nil "wallpaper-switcher:~A" (uiop:native-namestring directory)))
(defun render-directory (stream depth directory config visited)
"Write DIRECTORY's contents at DEPTH: a submenu for each subdirectory,
then an item for each wallpaper. A directory with neither gets a
\"No wallpapers in ...\" header. VISITED holds the truenames already
rendered on this branch, so a symlink pointing back up the tree is skipped
rather than recursed into forever."
(let ((subdirs (remove-if (lambda (d) (member (truename d) visited :test #'equal))
(find-subdirectories (namestring directory))))
(files (find-wallpapers (namestring directory) (config-extensions config))))
(when (and (null subdirs) (null files))
(render-empty stream depth directory))
(dolist (d subdirs)
(indent stream depth)
(format stream "<menu id=\"~A\" label=\"~A\">~%"
(xml-escape (submenu-id d))
(xml-escape (menu-label d)))
(render-directory stream (1+ depth) d config (cons (truename d) visited))
(indent stream depth)
(format stream "</menu>~%"))
(dolist (f files)
(render-item stream depth f (config-command config)))))
(defun render-menu (config &optional (stream *standard-output*))
"Write the Openbox pipe menu XML for CONFIG to STREAM."
(let ((root (uiop:ensure-directory-pathname
(expand-path (config-directory config)))))
(format stream "<?xml version=\"1.0\" encoding=\"UTF-8\"?>~%<openbox_pipe_menu>~%")
(if (uiop:directory-exists-p root)
(render-directory stream 1 root config (list (truename root)))
(render-empty stream 1 root))
(format stream "</openbox_pipe_menu>~%")))
|