;Ok, I've been using sawmill for about a week now. The first thing I had to 
;change was it's lack of backgrounds. So here it is. You tell this little guy
;what directories to look for backgrounds in. It creates a menu and uses 
;whatever application you specify to actually set the background. Some 
;customization options are added to let you change the default background and
;what app is being used to set it. It will also set the background on startup.
;I have also set up a popup-background-menu command so that you can bind it.

;add these to ~/.sawmillrc

;(require 'backgrounds)
;(add-hook 'after-initialization-hook set-background-hook)

(provide 'backgrounds)

;; Path we search for backgrounds
(setq background-image-path (list (concat (user-home-directory) ".backgrounds")))

(defun cut-last-two (lst)
  (reverse (cdr (cdr (reverse lst)))))

(defun basename (path)
  "Returns just the file name. No path or extension (thanks oGMo)"
  (let ((name (file-name-nondirectory path)))
    (if (string-match "\\.[^.]+$" name)
	(substring name 0 (match-start))
      name)))

(defun read-files-recursively (directory)
  (if (file-directory-p directory)
      (cons directory
	    (mapcar 
	     (lambda (file)
	       (let ((path-to-file (concat directory "/" file)))
		 (cond ((file-directory-p path-to-file) (read-files-recursively path-to-file))
		       (t path-to-file))))
	     (cut-last-two (directory-files directory))))))

(defun read-files-path (pathlist)
  (mapcar 
   (lambda (path)
     (read-files-recursively path))
   pathlist))

(defun set-background (path)
  (cond ((null path) nil)
	(t (system (concat background-set-app " " path " &"))
	   (if (not (null menu-changes-default-bg))
	       (setq root-background path)))))


(defun set-background-hook ()
  (set-background root-background))

;; CUSTOMIZATION STUFF
(defgroup background "Background")

(defcustom root-background nil
  "Current root background"
  :type file-name
  :group background
  :after-set set-background-hook)

(defcustom background-set-app "Esetroot"
  "Application to set desktop background"
  :type program-name
  :group background
  :after-set set-background-hook)

(defcustom menu-changes-default-bg nil
  "Does the menu change the default background?"
  :type boolean
  :group background)

;; MENU STUFF
(defun popup-backgrounds-menu ()
  (interactive)
  (popup-menu (backgrounds-menu)))

(defun backgrounds-menu ( &optional lst)
  "Creates the backgrounds menu"
  (mapcar
   (lambda (file)
     (cond ((listp file) `(,(file-name-nondirectory (car file)) . ,(cdr (backgrounds-menu file))))
	   ((file-readable-p file) `(,(basename file) (set-background ,file)))
	   (t nil)))
   (or lst (read-files-path background-image-path))))
