aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/menu.lisp98
-rw-r--r--src/package.lisp1
2 files changed, 80 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 "'" 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>~%")))
diff --git a/src/package.lisp b/src/package.lisp
index 3d36565..dcf297e 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -14,6 +14,7 @@
#:expand-path
;; menu
#:find-wallpapers
+ #:find-subdirectories
#:shell-quote
#:xml-escape
#:menu-label