about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/cycle-themes-20150402.2009/cycle-themes.el
blob: 0a936f71eec87eb2b2101c035e02583f935d3214 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;;; cycle-themes.el --- A global minor mode to make switching themes easier

;; Copyright (C) 2015 Katherine Whitlock
;;
;; Authors: Katherine Whitlock <toroidalcode@gmail.com>
;; URL: http://github.com/toroidal-code/cycle-themes.el
;; Package-Version: 20150402.2009
;; Version: 1.0
;; Package-Requires: ((cl-lib "0.5"))
;; Keywords: Themes, Utility, Global Minor Mode

;; This file is not part of GNU Emacs.

;;; Commentary:

;; Allows switching between themes easily.

;;; Installation

;; In your Emacs config, define a list of themes you want to be
;; able to switch between.  Then, enable the global minor mode.
;;
;;     (setq cycle-themes-theme-list
;;           '(leuven monokai solarized-dark))
;;     (require 'cycle-themes)
;;     (cycle-themes-mode)
;;
;; `cycle-themes' is bound to 'C-c C-t' by default.
;;
;; You can optionally add hooks to be run after switching themes:
;;
;; (add-hook 'cycle-themes-after-cycle-hook
;;           #'(lambda () (Do-something-fn ...)))
;;

;;; License:

;; This program is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 3 of the License, or (at your option) any later
;; version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
;; details.
;;
;; You should have received a copy of the GNU General Public License along with
;; GNU Emacs; see the file COPYING.  If not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;; USA.

;;; Code:

(eval-when-compile
  (require 'cl-lib))

(defgroup cycle-themes nil
  "The cycle-themes group"
  :group 'appearance
  :prefix "cycle-themes-")

(defcustom cycle-themes-after-cycle-hook nil
  "Hooks that are run after switching themes."
  :group 'cycle-themes
  :type 'hook)

(defcustom cycle-themes-theme-list (custom-available-themes)
  "The list of themes to cycle through on calling `cycle-themes'."
  :group 'cycle-themes
  :type '(list symbol))

(defcustom cycle-themes-allow-multiple-themes nil
  "Whether to allow the application of more than one theme at once."
  :group 'cycle-themes
  :type 'boolean)

(defconst cycle-themes-last-theme-set custom-enabled-themes
  "Used with multiple theme layering.")

(defconst cycle-themes-first-start t
  "load-theme reapplies all minor-modes, so we need this to avoid a stack overflow.")

(defun cycle-themes-get-next-valid-theme ()
  "Get the next valid theme from the list."
  ;; save our starting theme for a infinite-loop check
  ;; if there's no theme applied,
  (let* ((start-theme (or (first custom-enabled-themes)
                          (car (last cycle-themes-theme-list))))
         (current-theme start-theme))
    ;; do-while
    (while
        (progn
          ;; Fancy way to move to the next theme
          ;; with modular arithmetic so we never reach the end.
          (setq current-theme
                (nth (mod (1+ (cl-position current-theme cycle-themes-theme-list))
                          (length cycle-themes-theme-list))
                     cycle-themes-theme-list))
          ;; Make sure we didn't loop all the way through
          (when (eq current-theme start-theme)
            (error "No valid themes in cycle-themes-theme-list"))
          (not (custom-theme-p current-theme))))
    current-theme))


(defun cycle-themes ()
  "Cycle to the next theme."
  (interactive)
  (let ((new-theme (cycle-themes-get-next-valid-theme))
        (current-theme (first custom-enabled-themes))
        (current-theme-set custom-enabled-themes))
    ;; disable the current theme only if we want multiple themes
    ;; and we had it before
    (unless (and cycle-themes-allow-multiple-themes
                 (member current-theme cycle-themes-last-theme-set))
      (disable-theme current-theme))
    (load-theme new-theme t)
    (setq cycle-themes-last-theme-set current-theme-set)
    (run-hooks 'cycle-themes-after-cycle-hook)))

;;;###autoload
(define-minor-mode cycle-themes-mode
  "Minor mode for cycling between themes."
  :lighter ""
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "C-c C-t") 'cycle-themes)
            map)
  :global t
  (progn
    ;; remove any lingering themes other than the primary
    (dolist (theme (cl-set-difference (custom-available-themes)
                                      custom-enabled-themes))
      (disable-theme theme))

    ;; If we _aren't_ already trying to start up
    (when cycle-themes-first-start
      (setq cycle-themes-first-start nil)

      ;; if there are no themes enabled, enable
      ;; the first one in the list
      (if (null custom-enabled-themes)
          (add-hook 'emacs-startup-hook
                    #'(lambda ()
                        (load-theme (car cycle-themes-theme-list))
                        (run-hooks 'cycle-themes-after-cycle-hook)))
        
        ;; otherwise, ensure they're _actually_ loaded
        (add-hook 'emacs-startup-hook #'(lambda ()
                                          (dolist (theme (reverse custom-enabled-themes))
                                            (load-theme theme))
                                          (run-hooks 'cycle-themes-after-cycle-hook)))))))

(provide 'cycle-themes)
;;; cycle-themes.el ends here