aboutsummaryrefslogtreecommitdiff
path: root/src/menu.lisp
diff options
context:
space:
mode:
authorChristopher R. Nelson <christopher.nelson@languidnights.com>2026-10-31 23:51:41 -0400
committerChristopher R. Nelson <christopher.nelson@languidnights.com>2026-10-31 23:51:41 -0400
commit0bca7a7eb09f0e094aa721f75d23f771d20c6bf3 (patch)
tree380f0f0d02a504e00bcbdb9c13ca3ba25ca370dc /src/menu.lisp
Initial wallpaper-switcher pipe menu
Common Lisp tool that prints an Openbox pipe menu of the .png/.webp files in ~/.wallpapers, with each item setting the wallpaper via swaybg. Directory, command template and extensions are configurable through ~/.config/wallpaper-switcher/config or command-line options. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/menu.lisp')
-rw-r--r--src/menu.lisp94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/menu.lisp b/src/menu.lisp
new file mode 100644
index 0000000..4bae123
--- /dev/null
+++ b/src/menu.lisp
@@ -0,0 +1,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 "&amp;" out))
+ (#\< (write-string "&lt;" out))
+ (#\> (write-string "&gt;" out))
+ (#\" (write-string "&quot;" out))
+ (#\' (write-string "&apos;" 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>~%")))