-
Notifications
You must be signed in to change notification settings - Fork 6
/
theme-looper.el
executable file
·327 lines (291 loc) · 13.2 KB
/
theme-looper.el
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
;;; theme-looper.el --- A package for switching themes in Emacs interactively
;; This file is not part of Emacs
;; Author: Mohammed Ismail Ansari <[email protected]>
;; Version: 2.7
;; Keywords: convenience, color-themes
;; Maintainer: Mohammed Ismail Ansari <[email protected]>
;; Created: 2014/03/22
;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
;; Description: Loop through the available color-themes
;; URL: http://ismail.teamfluxion.com
;; Compatibility: Emacs24
;; COPYRIGHT NOTICE
;;
;; 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 2 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.
;;
;;; Install:
;; Put this file on your Emacs-Lisp load path, add following into your
;; ~/.emacs startup file
;;
;; (require 'theme-looper)
;;
;; And assign a set of key-bindings for cycling through themes
;;
;; (global-set-key (kbd "C-}") 'theme-looper-enable-next-theme)
;; (global-set-key (kbd "C-{") 'theme-looper-previous-next-theme)
;;
;; Or you can switch to a random theme
;;
;; (global-set-key (kbd "C-\\") 'theme-looper-enable-random-theme)
;;
;; Or you can choose from a list of themes through a completion interface
;; which could be either ivy or ido
;;
;; (global-set-key (kbd "C-|") 'theme-looper-select-theme)
;;
;; You can also set a list of your favorite themes
;;
;; (theme-looper-set-favorite-themes '(wombat tango-dark wheatgrass))
;;
;; or to use regular expressions
;;
;; (theme-looper-set-favorite-themes-regexp "dark")
;;
;; The special symbol `*default*' represents Emacs defaults (no theme)
;;
;; (theme-looper-set-favorite-themes '(cobalt wheatgrass *default*))
;;
;; You can alternatively set the themes to be ignored
;;
;; (theme-looper-set-ignored-themes '(cobalt))
;;
;; or to use regular expressions
;;
;; (theme-looper-set-ignored-themes-regexp "green")
;;
;; Or you can set both, in which case only the favorite themes that are not
;; among the ones to be ignored are used.
;;
;; If you want to reset your color-theme preferences, simply use
;;
;; (theme-looper-reset-themes-selection)
;;
;; In order to reload the currently activated color-theme, you can use
;;
;; (theme-looper-reload-current-theme)
;;
;; You can set hook functions to be run after every theme switch
;;
;; (add-hook 'theme-looper-post-switch-hook 'my-func)
;;
;;; Commentary:
;; You can use this package to cycle through color themes in Emacs with a
;; shortcut. Select your favorite themes, unfavorite themes and key-bindings
;; to switch color themes in style!
;;
;; Overview of features:
;;
;; o Loop though available color themes conveniently
;; o Narrow down the list of color themes to only the ones you like
;;
;;; Code:
(require 'cl-lib)
(defvar theme-looper--favorite-themes)
(defvar theme-looper--ignored-themes
nil)
(defvar theme-looper-post-switch-hook nil
"Hook that runs after switch.")
(defvar theme-looper--themes-map-separator
" | ")
(defvar theme-looper--initial-theme
nil)
(defun theme-looper-available-themes ()
"Lists the themes available for selection."
(cons '*default*
(custom-available-themes)))
;;;###autoload
(defun theme-looper-set-favorite-themes (themes)
"Sets the list of color-themes to cycle through."
(setq theme-looper--favorite-themes
themes))
;;;###autoload
(defun theme-looper-set-favorite-themes-regexp (regexp)
"Sets the list of color-themes to cycle through, matching a regular expression."
(setq theme-looper--favorite-themes
(cl-remove-if-not (lambda (theme)
(string-match-p regexp
(symbol-name theme)))
(theme-looper-available-themes))))
;;;###autoload
(defun theme-looper-set-ignored-themes (themes)
"Sets the list of color-themes to ignore."
(setq theme-looper--ignored-themes
themes))
;;;###autoload
(defun theme-looper-set-ignored-themes-regexp (regexp)
"Sets the list of color-themes to ignore, matching a regular expression."
(setq theme-looper--ignored-themes
(cl-remove-if-not (lambda (theme)
(string-match-p regexp
(symbol-name theme)))
(theme-looper-available-themes))))
;;;###autoload
(defun theme-looper-reset-themes-selection ()
"Resets themes selection back to default."
(theme-looper-set-favorite-themes (theme-looper-available-themes))
(theme-looper-set-ignored-themes nil))
(defun theme-looper--get-current-theme ()
"Determines the currently enabled theme."
(or (car custom-enabled-themes) '*default*))
(defun theme-looper--get-current-theme-index ()
"Finds the currently enabled color-theme in the list of color-themes."
(cl-position (theme-looper--get-current-theme)
(theme-looper--get-looped-themes)
:test #'equal))
(defun theme-looper--get-looped-themes ()
"Get themes to be looped through."
(cl-remove-if (lambda (theme)
(member theme
theme-looper--ignored-themes))
theme-looper--favorite-themes))
;;;###autoload
(defun theme-looper-enable-theme (theme)
"Enables the specified color-theme.
Pass `*default*' to select Emacs defaults."
(cl-flet* ((disable-all-themes ()
(mapcar 'disable-theme
custom-enabled-themes)))
(disable-all-themes)
(condition-case nil
(when (not (eq theme '*default*))
(load-theme theme t))
(error nil))
(run-hooks 'theme-looper-post-switch-hook)))
(defun theme-looper--enable-theme-with-map (theme)
"Enables a theme with displayed map."
(cl-labels ((nth-cyclic (index collection)
(cond ((< index
0) (nth-cyclic (+ index
(length collection))
collection))
((> index
(1- (length collection))) (nth-cyclic (- index
(length collection))
collection))
(t (nth index
collection))))
(print-theme-path (theme)
(let ((theme-index (cl-position theme
(theme-looper--get-looped-themes)
:test #'equal)))
(message (concat "theme-looper: "
(symbol-name (nth-cyclic (- theme-index 2)
(theme-looper--get-looped-themes)))
theme-looper--themes-map-separator
(symbol-name (nth-cyclic (- theme-index 1)
(theme-looper--get-looped-themes)))
theme-looper--themes-map-separator
(propertize (symbol-name theme)
'face
'(:inverse-video t))
theme-looper--themes-map-separator
(symbol-name (nth-cyclic (+ theme-index 1)
(theme-looper--get-looped-themes)))
theme-looper--themes-map-separator
(symbol-name (nth-cyclic (+ theme-index 2)
(theme-looper--get-looped-themes))))))))
(theme-looper-enable-theme theme)
(print-theme-path theme)))
;;;###autoload
(defun theme-looper-enable-next-theme ()
"Enables the next color-theme in the list."
(interactive)
(cl-flet* ((get-next-theme-index ()
(let ((current-theme-index (theme-looper--get-current-theme-index)))
(cond
((equal current-theme-index
'nil)
0)
((equal current-theme-index
(- (length (theme-looper--get-looped-themes))
1))
0)
((+ current-theme-index
1)))))
(get-next-theme ()
(nth (get-next-theme-index)
(theme-looper--get-looped-themes))))
(let ((next-theme (get-next-theme)))
(theme-looper--enable-theme-with-map next-theme))))
;;;###autoload
(defun theme-looper-enable-previous-theme ()
"Enables the previous color-theme in the list."
(interactive)
(cl-flet* ((get-previous-theme-index ()
(let ((current-theme-index (theme-looper--get-current-theme-index)))
(cond
((equal current-theme-index
'nil)
0)
((equal current-theme-index
0)
(- (length (theme-looper--get-looped-themes))
1))
((- current-theme-index
1)))))
(get-previous-theme ()
(nth (get-previous-theme-index)
(theme-looper--get-looped-themes))))
(let ((previous-theme (get-previous-theme)))
(theme-looper--enable-theme-with-map previous-theme))))
;;;###autoload
(defun theme-looper-enable-random-theme ()
"Enables a random theme from the list."
(interactive)
(cl-flet* ((enable-theme-with-log (theme)
(theme-looper-enable-theme theme)
(message "theme-looper: %s"
theme)))
(let ((some-theme (nth (random (length (theme-looper--get-looped-themes)))
(theme-looper--get-looped-themes))))
(enable-theme-with-log some-theme))))
;;;###autoload
(defun theme-looper-reload-current-theme ()
"Reloads the currently activated theme."
(interactive)
(theme-looper-enable-theme (theme-looper--get-current-theme)))
(defun theme-looper--start-theme-selector (themes-collection)
"Lets user select a theme from a list of specified themes rendered using a completion interface."
(cl-flet* ((preview-theme ()
(let* ((current-selection (ivy-state-current ivy-last))
(th (intern current-selection)))
(if (member th
(theme-looper-available-themes))
(ivy-call)))))
(setq theme-looper--initial-theme
(theme-looper--get-current-theme))
(if (featurep 'ivy)
(let ((ivy-wrap t))
(ivy-read "theme-looper: "
themes-collection
:preselect (symbol-name (theme-looper--get-current-theme))
:update-fn #'preview-theme
:action (lambda (th)
(theme-looper-enable-theme (intern th)))
:unwind (lambda ()
(when theme-looper--initial-theme
(theme-looper-enable-theme theme-looper--initial-theme)))))
(theme-looper-enable-theme (intern (ido-completing-read "theme-looper: "
(mapcar #'symbol-name
themes-collection)))))))
;;;###autoload
(defun theme-looper-select-theme ()
"Lets user select a theme from a list of favorite ones rendered using a completion interface."
(interactive)
(theme-looper--start-theme-selector (theme-looper--get-looped-themes)))
;;;###autoload
(defun theme-looper-select-theme-from-all ()
"Lets user select a theme from a list of all available themes rendered a completion interface."
(interactive)
(theme-looper--start-theme-selector (theme-looper-available-themes)))
(theme-looper-reset-themes-selection)
(provide 'theme-looper)
;;; theme-looper.el ends here