blob: 4bae12380243805f9a3536ccacb4e4d73b3886d4 (
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
|
(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))))
;;; 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 menu-label (pathname)
"Filename without its extension. Openbox reads _ in a label as a keyboard
shortcut marker, so each _ is doubled to show it as-is."
(let ((name (or (pathname-name pathname) (file-namestring pathname))))
(with-output-to-string (out)
(loop for c across name
do (write-char c out)
(when (char= c #\_) (write-char c out))))))
;;; 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 render-item (stream pathname command)
(format stream " <item label=\"~A\">~% <action name=\"Execute\"><command>~A</command></action>~% </item>~%"
(xml-escape (menu-label pathname))
(xml-escape (build-command command pathname))))
(defun render-menu (config &optional (stream *standard-output*))
"Write the Openbox pipe menu XML for CONFIG to STREAM."
(let ((files (find-wallpapers (config-directory config)
(config-extensions config))))
(format stream "<?xml version=\"1.0\" encoding=\"UTF-8\"?>~%<openbox_pipe_menu>~%")
(if files
(dolist (f files)
(render-item stream f (config-command config)))
(format stream " <separator label=\"~A\"/>~%"
(xml-escape (format nil "No wallpapers in ~A"
(expand-path (config-directory config))))))
(format stream "</openbox_pipe_menu>~%")))
|