unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* desktop-background.el as a new library?
@ 2021-10-27 16:41 Stefan Kangas
  2021-10-27 19:02 ` Tassilo Horn
                   ` (2 more replies)
  0 siblings, 3 replies; 39+ messages in thread
From: Stefan Kangas @ 2021-10-27 16:41 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1016 bytes --]

I'm looking at teaching image-dired.el to set your desktop background.
Because A) this finishes a TODO that has been in that file for going on
15 years, and B) any respectable image viewer must know how to set your
desktop background.

However, this code is not really tied to image-dired.el; only one line
and the name of the defun ties it to that library.  For example, it
could be used in thumbs.el (that has similar capabilities), and it could
even be useful as a stand-alone command.

So I started thinking that just like the recently added gravatar.el,
perhaps we should put this stuff in a new library
lisp/image/desktop-background.el?

(Or some better name, suggestions welcome.)

Please see the attached tentative diff for image-dired.el to get an
idea.  (Please ignore the somewhat unwieldy defcustoms; I followed the
scheme in image-dired.el and intend to clean that up.)

Maybe this will even work automagically on other operating systems than
GNU/Linux, if the commands it uses has been ported to them.

[-- Attachment #2: desktop-background.diff --]
[-- Type: text/x-diff, Size: 3667 bytes --]

diff --git a/lisp/image-dired.el b/lisp/image-dired.el
index f6a263749f..0b357332a1 100644
--- a/lisp/image-dired.el
+++ b/lisp/image-dired.el
@@ -117,8 +117,6 @@
 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
 ;;   for thumbnail directory.
 ;;
-;; * From thumbs.el: Add setroot function.
-;;
 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
 ;;  "image fit" might be enough)
 ;;
@@ -2818,6 +2816,71 @@ image-dired-save-information-from-widgets
        (dolist (tag tag-list)
          (push (cons file tag) lst))))))
 
+\f
+;;;; Setting background.
+
+(defcustom image-dired-set-background-program
+  (cond ((executable-find "feh") "feh")
+        ((executable-find "gm") "gm")
+        ((executable-find "display") "display")
+        ((executable-find "xloadimage") "xloadimage")
+        ((executable-find "xsetbg") "xsetbg"))
+  "Command to set the desktop background.
+You must also set `image-dired-cmd-set-background-options' or
+this will not work.
+
+Note: If you find that you need to use a different command on
+your machine, we would like to hear about it!  Please send an
+email to bug-gnu-emacs@gnu.org and tell us which
+command (including all options) that worked for you."
+  :type 'string
+  :version "29.1")
+
+(defcustom image-dired-cmd-set-background-options
+  (cond ((executable-find "feh") '("--bg-scale" "%f"))
+        ((executable-find "gm") '("display" "-resize" "%wx%h" "-window" "root" "%f"))
+        ((executable-find "display") '("-resize" "%wx%h" "-window" "root" "%f"))
+        ((executable-find "xloadimage") '("-onroot" "-fullscreen" "%f"))
+        ((executable-find "xsetbg") '"%f"))
+  "Options to pas to the program setting the desktop background.
+The default scales the image to fit the background.
+
+To see which options are available, see the documentation for
+your chosen `image-dired-cmd-set-background-program'."
+  :type '(repeat string)
+  :version "29.1")
+
+(defun image-dired-set-desktop-background (file)
+  "Set the desktop background to FILE in a graphical environment."
+  (interactive (list (or (image-dired-original-file-name)
+                         (read-file-name "Set desktop background to:"
+                                         default-directory nil t nil
+                                         (lambda (fn)
+                                           (string-match (image-file-name-regexp) fn))))))
+  (when (display-graphic-p)
+    (let* ((spec `((?f . ,file)
+                   (?h . ,(display-pixel-height))
+                   (?w . ,(display-pixel-width))))
+           (buf (format " *desktop-background-%s*"
+                        (random)))
+           (process
+            (apply #'start-process "set-desktop-background" buf
+                   image-dired-cmd-set-background-program
+                   (mapcar (lambda (arg) (format-spec arg spec))
+                           image-dired-cmd-set-background-options))))
+      (setf (process-sentinel process)
+            (lambda (process status)
+              (unwind-protect
+                  (unless (and (eq (process-status process) 'exit)
+                               (zerop (process-exit-status process)))
+                    (message "command %S %s: %S" (string-join (process-command process) " ")
+                             (string-replace "\n" "" status)
+                             (with-current-buffer (process-buffer process)
+                               (string-clean-whitespace (buffer-string)))))
+                (ignore-errors
+                  (kill-buffer (process-buffer process))))))
+      process)))
+
 \f
 ;;;; bookmark.el support
 

^ permalink raw reply related	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2022-09-16  1:27 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-27 16:41 desktop-background.el as a new library? Stefan Kangas
2021-10-27 19:02 ` Tassilo Horn
2021-10-27 19:51   ` Stefan Kangas
2021-10-28  2:36     ` Po Lu
2021-10-29 17:26 ` Arthur Miller
2021-10-29 17:44   ` Stefan Kangas
2021-11-15 20:51     ` Arthur Miller
2022-09-13 16:28 ` New library wallpaper.el pushed to master Stefan Kangas
2022-09-13 16:54   ` Eli Zaretskii
2022-09-13 17:09     ` Stefan Kangas
2022-09-13 17:22       ` Eli Zaretskii
2022-09-14  7:13         ` Stefan Kangas
2022-09-14  7:36           ` Stefan Kangas
2022-09-14  7:36           ` Po Lu
2022-09-14 11:10           ` Stefan Kangas
2022-09-14 11:44           ` Eli Zaretskii
2022-09-14 12:01             ` Stefan Kangas
2022-09-15 16:48               ` Stefan Kangas
2022-09-15 11:59           ` Eli Zaretskii
2022-09-15 13:39             ` Po Lu
2022-09-15 13:44               ` Eli Zaretskii
2022-09-15 13:49                 ` Po Lu
2022-09-14  2:46   ` Po Lu
2022-09-14  6:39     ` Po Lu
2022-09-14  7:04       ` Stefan Kangas
2022-09-14  7:33         ` Po Lu
2022-09-14 11:53           ` Stefan Kangas
2022-09-14 12:30             ` Stefan Kangas
2022-09-14 13:42             ` Po Lu
2022-09-14 16:58           ` Sean Whitton
2022-09-15  7:28             ` Stefan Kangas
2022-09-15 16:19               ` Sean Whitton
2022-09-16  1:27                 ` Po Lu
2022-09-14 11:39       ` Eli Zaretskii
2022-09-14 11:52         ` Stefan Kangas
2022-09-14 12:01           ` Eli Zaretskii
2022-09-14 12:07             ` Stefan Kangas
2022-09-14 12:24             ` Po Lu
2022-09-14 12:41               ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).