about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/wallpaper.el
blob: 9aa41cd364a4c17f680766f962635ab87c7e61fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;; wallpaper.el --- Control Linux desktop wallpaper -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com>

;;; Commentary:
;; Functions for setting desktop wallpaper.

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'prelude)
(require 'fs)
(require 'cycle)
(require 'string)
(require 'general)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defcustom wallpaper/keybindings? t
  "If non-nil, install the keybindings.")

(defcustom wallpaper/path-to-dir
  (f-expand "~/.local/share/wallpaper")
  "Path to the images that will be used as the wallpaper.")

(defconst wallpaper/whitelist
  (cycle/from-list
   (fs/ls wallpaper/path-to-dir t))
  "My preferred computer wallpapers.")

(defun wallpaper/set (path)
  "Set computer wallpaper to image at `PATH' using `feh` under-the-hood.
`PATH' can be absolute or relative since `f-expand' is called in the function
  body to ensure feh can resolve the path."
  (prelude/start-process
   :name "wallpaper/set"
   :command (string/format "feh --bg-scale --no-feh-bg %s" (f-expand path))))

(defun wallpaper/whitelist-set (wallpaper)
  "Focuses the WALLPAPER in the `wallpaper/whitelist' cycle."
  (cycle/focus (lambda (x) (equal x wallpaper)) wallpaper/whitelist)
  (wallpaper/set (wallpaper/current)))

(defun wallpaper/next ()
  "Cycles to the next wallpaper."
  (interactive)
  (let ((wallpaper (cycle/next wallpaper/whitelist)))
    (wallpaper/set wallpaper)
    (message (string/format "Active wallpaper: %s" (f-filename wallpaper)))))

(defun wallpaper/prev ()
  "Cycles to the previous wallpaper."
  (interactive)
  (let ((wallpaper (cycle/prev wallpaper/whitelist)))
    (wallpaper/set wallpaper)
    (message (string/format "Active wallpaper: %s" (f-filename wallpaper)))))

;; TODO: Define a macro that handles, next, prev, select, current for working
;; with cycles, since this is a common pattern.

(defun wallpaper/print-current ()
  "Message the currently enabled wallpaper."
  (interactive)
  (message
   (cycle/current wallpaper/whitelist)))

(defun wallpaper/current ()
  "Return the currently enabled wallpaper."
  (cycle/current wallpaper/whitelist))

(defun wallpaper/ivy-select ()
  "Use `counsel' to select and set a wallpaper from the `wallpaper/whitelist'."
  (interactive)
  (wallpaper/whitelist-set
   (ivy-read "Select wallpaper: " (cycle/to-list wallpaper/whitelist))))

;; TODO: Create macro-based module system that will auto-namespace functions,
;; constants, etc. with the filename like `wallpaper'.

(when wallpaper/keybindings?
  (general-define-key
   :prefix "<SPC>"
   :states '(normal)
   "Fw" #'wallpaper/next
   "Pw" #'wallpaper/prev))

(provide 'wallpaper)
;;; wallpaper.el ends here