Commit | Line | Data |
---|---|---|
3656dac0 RS |
1 | ;;; cus-theme.el -- custom theme creation user interface |
2 | ;; | |
73b0cd50 | 3 | ;; Copyright (C) 2001-2011 Free Software Foundation, Inc. |
3656dac0 RS |
4 | ;; |
5 | ;; Author: Alex Schroeder <alex@gnu.org> | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: help, faces | |
bd78fa1d | 8 | ;; Package: emacs |
3656dac0 RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3656dac0 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
3656dac0 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
3656dac0 RS |
24 | |
25 | ;;; Code: | |
26 | ||
27 | (require 'widget) | |
28 | (require 'cus-edit) | |
29 | ||
30 | (eval-when-compile | |
31 | (require 'wid-edit)) | |
32 | ||
bdeaa675 | 33 | (defvar custom-new-theme-mode-map |
bdeaa675 CY |
34 | (let ((map (make-keymap))) |
35 | (set-keymap-parent map widget-keymap) | |
36 | (suppress-keymap map) | |
6b09b5d1 | 37 | (define-key map "\C-x\C-s" 'custom-theme-write) |
bdeaa675 CY |
38 | (define-key map "n" 'widget-forward) |
39 | (define-key map "p" 'widget-backward) | |
bdeaa675 CY |
40 | map) |
41 | "Keymap for `custom-new-theme-mode'.") | |
42 | ||
647bc502 | 43 | (define-derived-mode custom-new-theme-mode nil "Custom-Theme" |
6b09b5d1 CY |
44 | "Major mode for editing Custom themes. |
45 | Do not call this mode function yourself. It is meant for internal use." | |
bdeaa675 | 46 | (use-local-map custom-new-theme-mode-map) |
6b09b5d1 CY |
47 | (custom--initialize-widget-variables) |
48 | (set (make-local-variable 'revert-buffer-function) 'custom-theme-revert)) | |
077ad61c LT |
49 | (put 'custom-new-theme-mode 'mode-class 'special) |
50 | ||
d0f1e2f8 | 51 | (defvar custom-theme-name nil) |
29a4c45b | 52 | ;; Each element has the form (VAR CHECKBOX-WIDGET VAR-WIDGET) |
d0f1e2f8 | 53 | (defvar custom-theme-variables nil) |
29a4c45b | 54 | ;; Each element has the form (FACE CHECKBOX-WIDGET FACE-WIDGET) |
d0f1e2f8 | 55 | (defvar custom-theme-faces nil) |
76c16af8 | 56 | (defvar custom-theme-description nil) |
29a4c45b | 57 | (defvar custom-theme--migrate-settings nil) |
76c16af8 CY |
58 | (defvar custom-theme-insert-variable-marker nil) |
59 | (defvar custom-theme-insert-face-marker nil) | |
60 | ||
2919746c | 61 | (defvar custom-theme--listed-faces '(default cursor fixed-pitch |
76c16af8 CY |
62 | variable-pitch escape-glyph minibuffer-prompt highlight region |
63 | shadow secondary-selection trailing-whitespace | |
64 | font-lock-builtin-face font-lock-comment-delimiter-face | |
65 | font-lock-comment-face font-lock-constant-face | |
66 | font-lock-doc-face font-lock-function-name-face | |
67 | font-lock-keyword-face font-lock-negation-char-face | |
68 | font-lock-preprocessor-face font-lock-regexp-grouping-backslash | |
69 | font-lock-regexp-grouping-construct font-lock-string-face | |
70 | font-lock-type-face font-lock-variable-name-face | |
71 | font-lock-warning-face button link link-visited fringe | |
72 | header-line tooltip mode-line mode-line-buffer-id | |
73 | mode-line-emphasis mode-line-highlight mode-line-inactive | |
74 | isearch isearch-fail lazy-highlight match next-error | |
75 | query-replace) | |
76 | "Faces listed by default in the *Custom Theme* buffer.") | |
077ad61c | 77 | |
6b09b5d1 CY |
78 | (defvar custom-theme--save-name) |
79 | ||
87e391a9 | 80 | ;;;###autoload |
6b09b5d1 CY |
81 | (defun customize-create-theme (&optional theme buffer) |
82 | "Create or edit a custom theme. | |
29a4c45b CY |
83 | THEME, if non-nil, should be an existing theme to edit. If THEME |
84 | is `user', provide an option to remove these as custom settings. | |
da16abfc CY |
85 | BUFFER, if non-nil, should be a buffer to use; the default is |
86 | named *Custom Theme*." | |
3656dac0 | 87 | (interactive) |
6b09b5d1 | 88 | (switch-to-buffer (get-buffer-create (or buffer "*Custom Theme*"))) |
077ad61c | 89 | (let ((inhibit-read-only t)) |
da16abfc CY |
90 | (erase-buffer) |
91 | (dolist (ov (overlays-in (point-min) (point-max))) | |
92 | (delete-overlay ov))) | |
077ad61c | 93 | (custom-new-theme-mode) |
3656dac0 | 94 | (make-local-variable 'custom-theme-name) |
6b09b5d1 | 95 | (set (make-local-variable 'custom-theme--save-name) theme) |
76c16af8 CY |
96 | (set (make-local-variable 'custom-theme-faces) nil) |
97 | (set (make-local-variable 'custom-theme-variables) nil) | |
98 | (set (make-local-variable 'custom-theme-description) "") | |
29a4c45b | 99 | (set (make-local-variable 'custom-theme--migrate-settings) nil) |
d0f1e2f8 | 100 | (make-local-variable 'custom-theme-insert-face-marker) |
76c16af8 CY |
101 | (make-local-variable 'custom-theme-insert-variable-marker) |
102 | (make-local-variable 'custom-theme--listed-faces) | |
077ad61c | 103 | |
29a4c45b CY |
104 | (if (eq theme 'user) |
105 | (widget-insert "This buffer contains all the Custom settings you have made. | |
106 | You can convert them into a new custom theme, and optionally | |
107 | remove them from your saved Custom file.\n\n")) | |
108 | ||
d0f1e2f8 | 109 | (widget-create 'push-button |
76c16af8 | 110 | :tag " Visit Theme " |
d0f1e2f8 CY |
111 | :help-echo "Insert the settings of a pre-defined theme." |
112 | :action (lambda (widget &optional event) | |
113 | (call-interactively 'custom-theme-visit-theme))) | |
114 | (widget-insert " ") | |
115 | (widget-create 'push-button | |
76c16af8 | 116 | :tag " Merge Theme " |
d0f1e2f8 CY |
117 | :help-echo "Merge in the settings of a pre-defined theme." |
118 | :action (lambda (widget &optional event) | |
119 | (call-interactively 'custom-theme-merge-theme))) | |
120 | (widget-insert " ") | |
29a4c45b CY |
121 | (widget-create 'push-button |
122 | :tag " Revert " | |
123 | :help-echo "Revert this buffer to its original state." | |
124 | :action (lambda (&rest ignored) (revert-buffer))) | |
077ad61c | 125 | |
76c16af8 | 126 | (widget-insert "\n\nTheme name : ") |
3656dac0 | 127 | (setq custom-theme-name |
6b09b5d1 | 128 | (widget-create 'editable-field |
29a4c45b CY |
129 | :value (if (and theme (not (eq theme 'user))) |
130 | (symbol-name theme) | |
131 | ""))) | |
76c16af8 | 132 | (widget-insert "Description: ") |
3656dac0 | 133 | (setq custom-theme-description |
71296446 | 134 | (widget-create 'text |
3656dac0 | 135 | :value (format-time-string "Created %Y-%m-%d."))) |
3656dac0 | 136 | (widget-create 'push-button |
76c16af8 CY |
137 | :notify (function custom-theme-write) |
138 | " Save Theme ") | |
29a4c45b CY |
139 | (when (eq theme 'user) |
140 | (setq custom-theme--migrate-settings t) | |
141 | (widget-insert " ") | |
142 | (widget-create 'checkbox | |
143 | :value custom-theme--migrate-settings | |
144 | :action (lambda (widget &optional event) | |
145 | (when (widget-value widget) | |
146 | (widget-toggle-action widget event) | |
147 | (setq custom-theme--migrate-settings | |
148 | (widget-value widget))))) | |
392f875a | 149 | (widget-insert (propertize " Remove saved theme settings from Custom save file." |
29a4c45b | 150 | 'face '(variable-pitch (:height 0.9))))) |
da16abfc CY |
151 | |
152 | (let (vars values faces face-specs) | |
153 | ||
154 | ;; Load the theme settings. | |
155 | (when theme | |
29a4c45b CY |
156 | (unless (eq theme 'user) |
157 | (load-theme theme t)) | |
da16abfc CY |
158 | (dolist (setting (get theme 'theme-settings)) |
159 | (if (eq (car setting) 'theme-value) | |
160 | (progn (push (nth 1 setting) vars) | |
161 | (push (nth 3 setting) values)) | |
162 | (push (nth 1 setting) faces) | |
163 | (push (nth 3 setting) face-specs)))) | |
164 | ||
165 | ;; If THEME is non-nil, insert all of that theme's faces. | |
166 | ;; Otherwise, insert those in `custom-theme--listed-faces'. | |
167 | (widget-insert "\n\n Theme faces:\n ") | |
168 | (if theme | |
169 | (while faces | |
170 | (custom-theme-add-face-1 (pop faces) (pop face-specs))) | |
171 | (dolist (face custom-theme--listed-faces) | |
172 | (custom-theme-add-face-1 face nil))) | |
173 | (setq custom-theme-insert-face-marker (point-marker)) | |
174 | (widget-insert " ") | |
175 | (widget-create 'push-button | |
176 | :tag "Insert Additional Face" | |
177 | :help-echo "Add another face to this theme." | |
178 | :follow-link 'mouse-face | |
179 | :button-face 'custom-link | |
180 | :mouse-face 'highlight | |
181 | :pressed-face 'highlight | |
182 | :action (lambda (widget &optional event) | |
183 | (call-interactively 'custom-theme-add-face))) | |
184 | ||
185 | ;; If THEME is non-nil, insert all of that theme's variables. | |
186 | (widget-insert "\n\n Theme variables:\n ") | |
187 | (if theme | |
188 | (while vars | |
29a4c45b CY |
189 | (if (eq (car vars) 'custom-enabled-themes) |
190 | (progn (pop vars) (pop values)) | |
191 | (custom-theme-add-var-1 (pop vars) (pop values))))) | |
da16abfc CY |
192 | (setq custom-theme-insert-variable-marker (point-marker)) |
193 | (widget-insert " ") | |
194 | (widget-create 'push-button | |
195 | :tag "Insert Variable" | |
196 | :help-echo "Add another variable to this theme." | |
197 | :follow-link 'mouse-face | |
198 | :button-face 'custom-link | |
199 | :mouse-face 'highlight | |
200 | :pressed-face 'highlight | |
201 | :action (lambda (widget &optional event) | |
202 | (call-interactively 'custom-theme-add-variable))) | |
203 | (widget-insert ?\n) | |
204 | (widget-setup) | |
205 | (goto-char (point-min)) | |
206 | (message ""))) | |
d0f1e2f8 | 207 | |
76c16af8 CY |
208 | (defun custom-theme-revert (ignore-auto noconfirm) |
209 | (when (or noconfirm (y-or-n-p "Discard current changes? ")) | |
6b09b5d1 | 210 | (customize-create-theme custom-theme--save-name (current-buffer)))) |
76c16af8 | 211 | |
d0f1e2f8 CY |
212 | ;;; Theme variables |
213 | ||
da16abfc CY |
214 | (defun custom-theme-add-variable (var value) |
215 | "Add a widget for VAR (a symbol) to the *New Custom Theme* buffer. | |
216 | VALUE should be a value to which to set the widget; when called | |
217 | interactively, this defaults to the current value of VAR." | |
218 | (interactive | |
219 | (let ((v (read-variable "Variable name: "))) | |
220 | (list v (symbol-value v)))) | |
29a4c45b CY |
221 | (let ((entry (assq var custom-theme-variables))) |
222 | (cond ((null entry) | |
da16abfc CY |
223 | ;; If VAR is not yet in the buffer, add it. |
224 | (save-excursion | |
225 | (goto-char custom-theme-insert-variable-marker) | |
226 | (custom-theme-add-var-1 var value) | |
227 | (move-marker custom-theme-insert-variable-marker (point)) | |
228 | (widget-setup))) | |
229 | ;; Otherwise, alter that var widget. | |
230 | (t | |
29a4c45b CY |
231 | (widget-value-set (nth 1 entry) t) |
232 | (let ((widget (nth 2 entry))) | |
da16abfc CY |
233 | (widget-put widget :shown-value (list value)) |
234 | (custom-redraw widget)))))) | |
235 | ||
236 | (defun custom-theme-add-var-1 (symbol val) | |
237 | (widget-insert " ") | |
29a4c45b CY |
238 | (push (list symbol |
239 | (prog1 (widget-create 'checkbox | |
240 | :value t | |
241 | :help-echo "Enable/disable this variable.") | |
242 | (widget-insert " ")) | |
da16abfc CY |
243 | (widget-create 'custom-variable |
244 | :tag (custom-unlispify-tag-name symbol) | |
245 | :value symbol | |
246 | :shown-value (list val) | |
247 | :notify 'ignore | |
248 | :custom-level 0 | |
249 | :custom-state 'hidden | |
647bc502 | 250 | :custom-style 'simple)) |
da16abfc CY |
251 | custom-theme-variables) |
252 | (widget-insert " ")) | |
d0f1e2f8 CY |
253 | |
254 | ;;; Theme faces | |
255 | ||
da16abfc CY |
256 | (defun custom-theme-add-face (face &optional spec) |
257 | "Add a widget for FACE (a symbol) to the *New Custom Theme* buffer. | |
258 | SPEC, if non-nil, should be a face spec to which to set the widget." | |
259 | (interactive (list (read-face-name "Face name" nil nil) nil)) | |
260 | (unless (or (facep face) spec) | |
261 | (error "`%s' has no face definition" face)) | |
29a4c45b CY |
262 | (let ((entry (assq face custom-theme-faces))) |
263 | (cond ((null entry) | |
da16abfc CY |
264 | ;; If FACE is not yet in the buffer, add it. |
265 | (save-excursion | |
266 | (goto-char custom-theme-insert-face-marker) | |
267 | (custom-theme-add-face-1 face spec) | |
76c16af8 | 268 | (move-marker custom-theme-insert-face-marker (point)) |
da16abfc CY |
269 | (widget-setup))) |
270 | ;; Otherwise, if SPEC is supplied, alter that face widget. | |
271 | (spec | |
29a4c45b CY |
272 | (widget-value-set (nth 1 entry) t) |
273 | (let ((widget (nth 2 entry))) | |
da16abfc CY |
274 | (widget-put widget :shown-value spec) |
275 | (custom-redraw widget))) | |
276 | ((called-interactively-p 'interactive) | |
277 | (error "`%s' is already present" face))))) | |
278 | ||
279 | (defun custom-theme-add-face-1 (symbol spec) | |
280 | (widget-insert " ") | |
29a4c45b CY |
281 | (push (list symbol |
282 | (prog1 | |
283 | (widget-create 'checkbox | |
284 | :value t | |
285 | :help-echo "Enable/disable this face.") | |
286 | (widget-insert " ")) | |
da16abfc CY |
287 | (widget-create 'custom-face |
288 | :tag (custom-unlispify-tag-name symbol) | |
289 | :documentation-shown t | |
290 | :value symbol | |
291 | :custom-state 'hidden | |
647bc502 | 292 | :custom-style 'simple |
da16abfc | 293 | :shown-value spec |
da16abfc CY |
294 | :sample-indent 34)) |
295 | custom-theme-faces) | |
296 | (widget-insert " ")) | |
d0f1e2f8 CY |
297 | |
298 | ;;; Reading and writing | |
299 | ||
da16abfc CY |
300 | (defun custom-theme-visit-theme (theme) |
301 | "Load the custom theme THEME's settings into the current buffer." | |
302 | (interactive | |
303 | (list | |
304 | (intern (completing-read "Find custom theme: " | |
305 | (mapcar 'symbol-name | |
306 | (custom-available-themes)))))) | |
307 | (unless (custom-theme-name-valid-p theme) | |
308 | (error "No valid theme named `%s'" theme)) | |
309 | (cond ((not (eq major-mode 'custom-new-theme-mode)) | |
310 | (customize-create-theme theme)) | |
311 | ((y-or-n-p "Discard current changes? ") | |
312 | (setq custom-theme--save-name theme) | |
313 | (custom-theme-revert nil t)))) | |
d0f1e2f8 CY |
314 | |
315 | (defun custom-theme-merge-theme (theme) | |
da16abfc | 316 | "Merge the custom theme THEME's settings into the current buffer." |
6b09b5d1 CY |
317 | (interactive |
318 | (list | |
319 | (intern (completing-read "Merge custom theme: " | |
320 | (mapcar 'symbol-name | |
321 | (custom-available-themes)))))) | |
da16abfc CY |
322 | (unless (eq theme 'user) |
323 | (unless (custom-theme-name-valid-p theme) | |
324 | (error "Invalid theme name `%s'" theme)) | |
325 | (load-theme theme t)) | |
326 | (let ((settings (reverse (get theme 'theme-settings)))) | |
d0f1e2f8 | 327 | (dolist (setting settings) |
da16abfc CY |
328 | (funcall (if (eq (car setting) 'theme-value) |
329 | 'custom-theme-add-variable | |
330 | 'custom-theme-add-face) | |
331 | (nth 1 setting) | |
332 | (nth 3 setting)))) | |
d0f1e2f8 CY |
333 | theme) |
334 | ||
3656dac0 | 335 | (defun custom-theme-write (&rest ignore) |
da16abfc | 336 | "Write the current custom theme to its theme file." |
6b09b5d1 | 337 | (interactive) |
d0f1e2f8 | 338 | (let* ((name (widget-value custom-theme-name)) |
29a4c45b CY |
339 | (doc (widget-value custom-theme-description)) |
340 | (vars custom-theme-variables) | |
76c16af8 CY |
341 | (faces custom-theme-faces) |
342 | filename) | |
343 | (when (string-equal name "") | |
344 | (setq name (read-from-minibuffer "Theme name: " (user-login-name))) | |
345 | (widget-value-set custom-theme-name name)) | |
6b09b5d1 CY |
346 | (unless (custom-theme-name-valid-p (intern name)) |
347 | (error "Custom themes cannot be named `%s'" name)) | |
76c16af8 CY |
348 | |
349 | (setq filename (expand-file-name (concat name "-theme.el") | |
350 | custom-theme-directory)) | |
351 | (and (file-exists-p filename) | |
352 | (not (y-or-n-p (format "File %s exists. Overwrite? " filename))) | |
353 | (error "Aborted")) | |
354 | ||
d0f1e2f8 CY |
355 | (with-temp-buffer |
356 | (emacs-lisp-mode) | |
782b5e8d | 357 | (unless (file-directory-p custom-theme-directory) |
d0f1e2f8 CY |
358 | (make-directory (file-name-as-directory custom-theme-directory) t)) |
359 | (setq buffer-file-name filename) | |
360 | (erase-buffer) | |
361 | (insert "(deftheme " name) | |
362 | (if doc (insert "\n \"" doc "\"")) | |
363 | (insert ")\n") | |
29a4c45b CY |
364 | (custom-theme-write-variables name (reverse vars)) |
365 | (custom-theme-write-faces name (reverse faces)) | |
d0f1e2f8 CY |
366 | (insert "\n(provide-theme '" name ")\n") |
367 | (save-buffer)) | |
29a4c45b CY |
368 | (message "Theme written to %s" filename) |
369 | ||
370 | (when custom-theme--migrate-settings | |
371 | ;; Remove these settings from the Custom file. | |
372 | (let ((custom-reset-standard-variables-list '(t)) | |
373 | (custom-reset-standard-faces-list '(t))) | |
374 | (dolist (var vars) | |
375 | (when (and (not (eq (car var) 'custom-enabled-themes)) | |
376 | (widget-get (nth 1 var) :value)) | |
377 | (widget-apply (nth 2 var) :custom-mark-to-reset-standard))) | |
378 | (dolist (face faces) | |
379 | (when (widget-get (nth 1 face) :value) | |
380 | (widget-apply (nth 2 face) :custom-mark-to-reset-standard))) | |
381 | (custom-save-all)) | |
382 | (let ((custom-theme-load-path (list 'custom-theme-directory))) | |
383 | (load-theme (intern name)))))) | |
3656dac0 RS |
384 | |
385 | (defun custom-theme-write-variables (theme vars) | |
386 | "Write a `custom-theme-set-variables' command for THEME. | |
387 | It includes all variables in list VARS." | |
3656dac0 RS |
388 | (when vars |
389 | (let ((standard-output (current-buffer))) | |
390 | (princ "\n(custom-theme-set-variables\n") | |
391 | (princ " '") | |
392 | (princ theme) | |
393 | (princ "\n") | |
76c16af8 | 394 | (dolist (spec vars) |
29a4c45b CY |
395 | (when (widget-get (nth 1 spec) :value) |
396 | (let* ((symbol (nth 0 spec)) | |
397 | (widget (nth 2 spec)) | |
398 | (child (car-safe (widget-get widget :children))) | |
399 | (value (if child | |
400 | (widget-value child) | |
401 | ;; Child is null if the widget is closed (hidden). | |
402 | (car (widget-get widget :shown-value))))) | |
403 | (when (boundp symbol) | |
404 | (unless (bolp) | |
405 | (princ "\n")) | |
406 | (princ " '(") | |
407 | (prin1 symbol) | |
408 | (princ " ") | |
409 | (prin1 (custom-quote value)) | |
410 | (princ ")"))))) | |
3656dac0 RS |
411 | (if (bolp) |
412 | (princ " ")) | |
413 | (princ ")") | |
414 | (unless (looking-at "\n") | |
415 | (princ "\n"))))) | |
416 | ||
417 | (defun custom-theme-write-faces (theme faces) | |
418 | "Write a `custom-theme-set-faces' command for THEME. | |
419 | It includes all faces in list FACES." | |
420 | (when faces | |
421 | (let ((standard-output (current-buffer))) | |
422 | (princ "\n(custom-theme-set-faces\n") | |
423 | (princ " '") | |
424 | (princ theme) | |
425 | (princ "\n") | |
76c16af8 | 426 | (dolist (spec faces) |
29a4c45b CY |
427 | (when (widget-get (nth 1 spec) :value) |
428 | (let* ((symbol (nth 0 spec)) | |
429 | (widget (nth 2 spec)) | |
430 | (value | |
431 | (if (car-safe (widget-get widget :children)) | |
432 | (custom-face-widget-to-spec widget) | |
433 | ;; Child is null if the widget is closed (hidden). | |
434 | (widget-get widget :shown-value)))) | |
435 | (when (and (facep symbol) value) | |
436 | (princ (if (bolp) " '(" "\n '(")) | |
437 | (prin1 symbol) | |
438 | (princ " ") | |
439 | (prin1 value) | |
440 | (princ ")"))))) | |
da16abfc | 441 | (if (bolp) (princ " ")) |
3656dac0 RS |
442 | (princ ")") |
443 | (unless (looking-at "\n") | |
444 | (princ "\n"))))) | |
445 | ||
6b09b5d1 CY |
446 | \f |
447 | ;;; Describing Custom themes. | |
448 | ||
449 | ;;;###autoload | |
450 | (defun describe-theme (theme) | |
451 | "Display a description of the Custom theme THEME (a symbol)." | |
452 | (interactive | |
453 | (list | |
454 | (intern (completing-read "Describe custom theme: " | |
455 | (mapcar 'symbol-name | |
456 | (custom-available-themes)))))) | |
457 | (unless (custom-theme-name-valid-p theme) | |
458 | (error "Invalid theme name `%s'" theme)) | |
459 | (help-setup-xref (list 'describe-theme theme) | |
460 | (called-interactively-p 'interactive)) | |
461 | (with-help-window (help-buffer) | |
462 | (with-current-buffer standard-output | |
463 | (describe-theme-1 theme)))) | |
464 | ||
465 | (defun describe-theme-1 (theme) | |
466 | (prin1 theme) | |
467 | (princ " is a custom theme") | |
468 | (let ((fn (locate-file (concat (symbol-name theme) "-theme.el") | |
782b5e8d | 469 | (custom-theme--load-path) |
2919746c CY |
470 | '("" "c"))) |
471 | doc) | |
6b09b5d1 CY |
472 | (when fn |
473 | (princ " in `") | |
474 | (help-insert-xref-button (file-name-nondirectory fn) | |
475 | 'help-theme-def fn) | |
476 | (princ "'")) | |
2919746c CY |
477 | (princ ".\n") |
478 | (if (not (memq theme custom-known-themes)) | |
479 | (progn | |
480 | (princ "It is not loaded.") | |
481 | ;; Attempt to grab the theme documentation | |
482 | (when fn | |
483 | (with-temp-buffer | |
484 | (insert-file-contents fn) | |
485 | (let ((sexp (let ((read-circle nil)) | |
486 | (condition-case nil | |
487 | (read (current-buffer)) | |
488 | (end-of-file nil))))) | |
489 | (and sexp (listp sexp) | |
490 | (eq (car sexp) 'deftheme) | |
491 | (setq doc (nth 2 sexp))))))) | |
492 | (if (custom-theme-enabled-p theme) | |
493 | (princ "It is loaded and enabled.") | |
494 | (princ "It is loaded but disabled.")) | |
495 | (setq doc (get theme 'theme-documentation))) | |
496 | ||
497 | (princ "\n\nDocumentation:\n") | |
498 | (princ (if (stringp doc) | |
499 | doc | |
500 | "No documentation available."))) | |
6b09b5d1 CY |
501 | (princ "\n\nYou can ") |
502 | (help-insert-xref-button "customize" 'help-theme-edit theme) | |
503 | (princ " this theme.")) | |
504 | ||
505 | \f | |
506 | ;;; Theme chooser | |
507 | ||
508 | (defvar custom--listed-themes) | |
509 | ||
510 | (defcustom custom-theme-allow-multiple-selections nil | |
511 | "Whether to allow multi-selections in the *Custom Themes* buffer." | |
512 | :type 'boolean | |
513 | :group 'custom-buffer) | |
514 | ||
515 | (defvar custom-theme-choose-mode-map | |
516 | (let ((map (make-keymap))) | |
517 | (set-keymap-parent map widget-keymap) | |
518 | (suppress-keymap map) | |
519 | (define-key map "\C-x\C-s" 'custom-theme-save) | |
520 | (define-key map "n" 'widget-forward) | |
521 | (define-key map "p" 'widget-backward) | |
522 | (define-key map "?" 'custom-describe-theme) | |
523 | map) | |
524 | "Keymap for `custom-theme-choose-mode'.") | |
525 | ||
647bc502 | 526 | (define-derived-mode custom-theme-choose-mode nil "Themes" |
6b09b5d1 CY |
527 | "Major mode for selecting Custom themes. |
528 | Do not call this mode function yourself. It is meant for internal use." | |
529 | (use-local-map custom-theme-choose-mode-map) | |
530 | (custom--initialize-widget-variables) | |
531 | (set (make-local-variable 'revert-buffer-function) | |
532 | (lambda (ignore-auto noconfirm) | |
533 | (when (or noconfirm (y-or-n-p "Discard current choices? ")) | |
534 | (customize-themes (current-buffer)))))) | |
535 | (put 'custom-theme-choose-mode 'mode-class 'special) | |
536 | ||
537 | ;;;###autoload | |
538 | (defun customize-themes (&optional buffer) | |
539 | "Display a selectable list of Custom themes. | |
540 | When called from Lisp, BUFFER should be the buffer to use; if | |
541 | omitted, a buffer named *Custom Themes* is used." | |
542 | (interactive) | |
b2948a87 | 543 | (switch-to-buffer (get-buffer-create (or buffer "*Custom Themes*"))) |
6b09b5d1 CY |
544 | (let ((inhibit-read-only t)) |
545 | (erase-buffer)) | |
546 | (custom-theme-choose-mode) | |
547 | (set (make-local-variable 'custom--listed-themes) nil) | |
548 | (make-local-variable 'custom-theme-allow-multiple-selections) | |
549 | (and (null custom-theme-allow-multiple-selections) | |
550 | (> (length custom-enabled-themes) 1) | |
551 | (setq custom-theme-allow-multiple-selections t)) | |
552 | ||
553 | (widget-insert | |
554 | (substitute-command-keys | |
555 | "Type RET or click to enable/disable listed custom themes. | |
556 | Type \\[custom-describe-theme] to describe the theme at point. | |
557 | Theme files are named *-theme.el in `")) | |
782b5e8d | 558 | (widget-create 'link :value "custom-theme-load-path" |
6b09b5d1 CY |
559 | :button-face 'custom-link |
560 | :mouse-face 'highlight | |
561 | :pressed-face 'highlight | |
782b5e8d | 562 | :help-echo "Describe `custom-theme-load-path'." |
6b09b5d1 CY |
563 | :keymap custom-mode-link-map |
564 | :follow-link 'mouse-face | |
565 | :action (lambda (widget &rest ignore) | |
782b5e8d | 566 | (describe-variable 'custom-theme-load-path))) |
6b09b5d1 | 567 | (widget-insert "'.\n\n") |
da16abfc CY |
568 | |
569 | ;; If the user has made customizations, display a warning and | |
570 | ;; provide buttons to disable or convert them. | |
571 | (let ((user-settings (get 'user 'theme-settings))) | |
572 | (unless (or (null user-settings) | |
573 | (and (null (cdr user-settings)) | |
574 | (eq (caar user-settings) 'theme-value) | |
575 | (eq (cadr (car user-settings)) 'custom-enabled-themes))) | |
29a4c45b CY |
576 | (widget-insert |
577 | (propertize | |
578 | " Note: Your custom settings take precedence over theme settings. | |
579 | To migrate your settings into a theme, click " | |
580 | 'face 'font-lock-warning-face)) | |
581 | (widget-create 'link :value "here" | |
582 | :button-face 'custom-link | |
583 | :mouse-face 'highlight | |
584 | :pressed-face 'highlight | |
585 | :help-echo "Migrate." | |
586 | :keymap custom-mode-link-map | |
587 | :follow-link 'mouse-face | |
588 | :action (lambda (widget &rest ignore) | |
589 | (customize-create-theme 'user))) | |
590 | (widget-insert ".\n\n"))) | |
da16abfc | 591 | |
6b09b5d1 CY |
592 | (widget-create 'push-button |
593 | :tag " Save Theme Settings " | |
594 | :help-echo "Save the selected themes for future sessions." | |
595 | :action 'custom-theme-save) | |
596 | (widget-insert ?\n) | |
597 | (widget-create 'checkbox | |
598 | :value custom-theme-allow-multiple-selections | |
599 | :action 'custom-theme-selections-toggle) | |
600 | (widget-insert (propertize " Allow more than one theme at a time" | |
601 | 'face '(variable-pitch (:height 0.9)))) | |
602 | ||
603 | (widget-insert "\n\nAvailable Custom Themes:\n") | |
604 | (let (widget) | |
605 | (dolist (theme (custom-available-themes)) | |
606 | (setq widget (widget-create 'checkbox | |
607 | :value (custom-theme-enabled-p theme) | |
608 | :theme-name theme | |
609 | :action 'custom-theme-checkbox-toggle)) | |
610 | (push (cons theme widget) custom--listed-themes) | |
611 | (widget-create-child-and-convert widget 'push-button | |
612 | :button-face-get 'ignore | |
613 | :mouse-face-get 'ignore | |
614 | :value (format " %s" theme) | |
615 | :action 'widget-parent-action) | |
616 | (widget-insert ?\n))) | |
617 | (goto-char (point-min)) | |
618 | (widget-setup)) | |
619 | ||
620 | (defun custom-theme-checkbox-toggle (widget &optional event) | |
621 | (let ((this-theme (widget-get widget :theme-name))) | |
622 | (if (widget-value widget) | |
623 | ;; Disable the theme. | |
928f4e73 CY |
624 | (progn |
625 | (disable-theme this-theme) | |
626 | (widget-toggle-action widget event)) | |
6b09b5d1 CY |
627 | ;; Enable the theme. |
628 | (unless custom-theme-allow-multiple-selections | |
629 | ;; If only one theme is allowed, disable all other themes and | |
630 | ;; uncheck their boxes. | |
631 | (dolist (theme custom-enabled-themes) | |
632 | (and (not (eq theme this-theme)) | |
633 | (assq theme custom--listed-themes) | |
634 | (disable-theme theme))) | |
635 | (dolist (theme custom--listed-themes) | |
636 | (unless (eq (car theme) this-theme) | |
637 | (widget-value-set (cdr theme) nil) | |
638 | (widget-apply (cdr theme) :notify (cdr theme) event)))) | |
928f4e73 CY |
639 | (when (load-theme this-theme) |
640 | (widget-toggle-action widget event))) | |
641 | ;; Mark `custom-enabled-themes' as "set for current session". | |
642 | (put 'custom-enabled-themes 'customized-value | |
643 | (list (custom-quote custom-enabled-themes))))) | |
6b09b5d1 CY |
644 | |
645 | (defun custom-describe-theme () | |
646 | "Describe the Custom theme on the current line." | |
647 | (interactive) | |
648 | (let ((widget (widget-at (line-beginning-position)))) | |
649 | (and widget | |
650 | (describe-theme (widget-get widget :theme-name))))) | |
651 | ||
652 | (defun custom-theme-save (&rest ignore) | |
653 | (interactive) | |
654 | (customize-save-variable 'custom-enabled-themes custom-enabled-themes) | |
655 | (message "Custom themes saved for future sessions.")) | |
656 | ||
657 | (defun custom-theme-selections-toggle (widget &optional event) | |
658 | (when (widget-value widget) | |
659 | ;; Deactivate multiple-selections. | |
29a4c45b CY |
660 | (if (< 1 (length (delq nil (mapcar (lambda (x) (widget-value (cdr x))) |
661 | custom--listed-themes)))) | |
6b09b5d1 CY |
662 | (error "More than one theme is currently selected"))) |
663 | (widget-toggle-action widget event) | |
664 | (setq custom-theme-allow-multiple-selections (widget-value widget))) | |
665 | ||
3656dac0 | 666 | ;;; cus-theme.el ends here |