summaryrefslogtreecommitdiff
path: root/fussy.el
blob: 654594d6dc08399464384e1c2d1b62945f987ace (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(require 'cl-lib)
(require 'package)

(defvar +excluded-package-names+
  '(tramp-theme color-theme airline-themes))

(defun fussy-string-ends-with (str suffix)
  (and (< (length suffix) (length str))
       (string= suffix
                (cl-subseq str (- (length str) (length suffix))))))

(defun fussy-is-theme-p (pkg-entry)
  (let ((name (symbol-name (cl-first pkg-entry))))
   (and (or (fussy-string-ends-with name "-theme")
            (fussy-string-ends-with name "-themes"))
        (not (cl-member (cl-first pkg-entry) +excluded-package-names+)))))

(defun fussy-themes-packages ()
  (cl-remove-if-not 'fussy-is-theme-p package-archive-contents))

(defun fussy-screenshot-svg (filename)
  "Save a screenshot of the current frame as an SVG image to a file
called filename."
  (let* ((tmpfile (make-temp-file "Emacs" nil ".svg"))
         (data (x-export-frames nil 'svg)))
    (with-temp-file tmpfile
      (insert data))
    (copy-file tmpfile filename t)))

(defun install-all-themes ()
  (let ((failed-to-install nil))
   (cl-dolist (theme (fussy-themes-packages))
     (condition-case nil
         (package-install (cl-first theme))
       (error (push (cl-first theme) failed-to-install))))
   failed-to-install))

(defun locate-theme-package (theme)
  "Given a symbol naming a theme, find the name of the package that
the theme came from."
  (let ((theme-file
         (locate-file (concat (symbol-name theme) "-theme.el")
                      (custom-theme--load-path)
                      '("" "c"))))
    (string-join
     (butlast
      (split-string
       (cadr
        (reverse
         (file-name-split
          (file-name-directory
           theme-file))))
       "-"))
     "-")))

(defun fussy-generate-all-theme-images (&rest files)
  (let ((failed-to-load nil)
        (failed-to-generate-image nil)
        (failed-to-install (install-all-themes)))
    (message "All themes have been loaded")
    (cl-dolist (theme (custom-available-themes))
      (message (format "Generating for theme: %s" theme))
      (unwind-protect
          (when (condition-case nil
                    (progn (load-theme theme t)
                           t)
                  (error (push theme failed-to-load)
                         (message "... failed to load!")
                         nil))
            (dolist (file files)
              (let* ((theme-package
                      (locate-theme-package theme))
                     (svg-file
                      (concat
                       (getenv "HOME") "/"
                       theme-package "/"
                       (symbol-name theme) "/"
                       (file-name-base file)
                       "."
                       (file-name-extension file)
                       ".svg")))
                (make-directory (file-name-directory svg-file) t)
                (condition-case nil
                    (progn
                      (find-file file)
                      (delete-other-windows)
                      (fussy-screenshot-svg svg-file))
                  (error (push (cons theme file) failed-to-generate-image)))))))
      (disable-theme theme))
    (with-temp-file (concat (getenv "HOME") "/" "errors.el")
      (insert (prin1-to-string
               (list :load-errors failed-to-load
                     :generation-errors failed-to-generate-image
                     :install-errors failed-to-install))))
    (message "FINISHED GENERATING THEME IMAGES")))

(add-to-list 'package-archives
	     '("melpa" . "https://melpa.org/packages/") t)


(package-initialize)
(package-refresh-contents)
(package-install 'compat)
(toggle-frame-maximized)

(fussy-generate-all-theme-images
 "/home/colin/projects/LearnCPP/chapter1/hello_world/hello.cpp"
 "/home/colin/projects/fussy/fussy.el"
 "/home/colin/projects/INACTIVE/nsa/nsa.py")

(kill-emacs)