aboutsummaryrefslogtreecommitdiff
path: root/src/menu.lisp
diff options
context:
space:
mode:
authorChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:16:04 -0400
committerChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:16:04 -0400
commit7fb53e418a48b8baa7bcab89967a0755f2230162 (patch)
treee2e4ce1a04ac33650d5c7be06ebdc295ce9112c8 /src/menu.lisp
parent0bca7a7eb09f0e094aa721f75d23f771d20c6bf3 (diff)
Show subdirectories as nested submenus
Each subdirectory of the wallpaper directory becomes an inline Openbox submenu, recursively, listed before the files at that level. A directory with no wallpapers and no subdirectories gets the same "No wallpapers in ..." header as an empty root. Hidden directories are skipped, and symlinks pointing back up the tree are not followed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/menu.lisp')
-rw-r--r--src/menu.lisp98
1 files changed, 79 insertions, 19 deletions
diff --git a/src/menu.lisp b/src/menu.lisp
index 4bae123..da99ab0 100644
--- a/src/menu.lisp
+++ b/src/menu.lisp
@@ -16,6 +16,23 @@ name. Returns NIL if DIRECTORY does not exist."
#'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)
@@ -40,14 +57,21 @@ each embedded ' as '\\''."
(#\' (write-string "&apos;" 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)
- "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))))))
+ "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
@@ -75,20 +99,56 @@ template is passed to `sh -c` as a single quoted argument."
;;; 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 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 ((files (find-wallpapers (config-directory config)
- (config-extensions config))))
+ (let ((root (uiop:ensure-directory-pathname
+ (expand-path (config-directory 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))))))
+ (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>~%")))