Drop use of unsafep for checking Custom themes; bugfixes.
[emacs.git] / lisp / cus-theme.el
blobcdc066aa91a6088630f2f6785cd5e72abc82dc2d
1 ;;; cus-theme.el -- custom theme creation user interface
2 ;;
3 ;; Copyright (C) 2001-2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: help, faces
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Code:
27 (require 'widget)
28 (require 'cus-edit)
30 (eval-when-compile
31 (require 'wid-edit))
33 (defvar custom-new-theme-mode-map
34 (let ((map (make-keymap)))
35 (set-keymap-parent map widget-keymap)
36 (suppress-keymap map)
37 (define-key map "\C-x\C-s" 'custom-theme-write)
38 (define-key map "n" 'widget-forward)
39 (define-key map "p" 'widget-backward)
40 map)
41 "Keymap for `custom-new-theme-mode'.")
43 (define-derived-mode custom-new-theme-mode nil "Custom-Theme"
44 "Major mode for editing Custom themes.
45 Do not call this mode function yourself. It is meant for internal use."
46 (use-local-map custom-new-theme-mode-map)
47 (custom--initialize-widget-variables)
48 (set (make-local-variable 'revert-buffer-function) 'custom-theme-revert))
49 (put 'custom-new-theme-mode 'mode-class 'special)
51 (defvar custom-theme-name nil)
52 ;; Each element has the form (VAR CHECKBOX-WIDGET VAR-WIDGET)
53 (defvar custom-theme-variables nil)
54 ;; Each element has the form (FACE CHECKBOX-WIDGET FACE-WIDGET)
55 (defvar custom-theme-faces nil)
56 (defvar custom-theme-description nil)
57 (defvar custom-theme--migrate-settings nil)
58 (defvar custom-theme-insert-variable-marker nil)
59 (defvar custom-theme-insert-face-marker nil)
61 (defvar custom-theme--listed-faces '(default cursor fixed-pitch
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.")
78 (defvar custom-theme--save-name)
80 ;;;###autoload
81 (defun customize-create-theme (&optional theme buffer)
82 "Create or edit a custom theme.
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.
85 BUFFER, if non-nil, should be a buffer to use; the default is
86 named *Custom Theme*."
87 (interactive)
88 (switch-to-buffer (get-buffer-create (or buffer "*Custom Theme*")))
89 (let ((inhibit-read-only t))
90 (erase-buffer)
91 (dolist (ov (overlays-in (point-min) (point-max)))
92 (delete-overlay ov)))
93 (custom-new-theme-mode)
94 (make-local-variable 'custom-theme-name)
95 (set (make-local-variable 'custom-theme--save-name) theme)
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) "")
99 (set (make-local-variable 'custom-theme--migrate-settings) nil)
100 (make-local-variable 'custom-theme-insert-face-marker)
101 (make-local-variable 'custom-theme-insert-variable-marker)
102 (make-local-variable 'custom-theme--listed-faces)
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"))
109 (widget-create 'push-button
110 :tag " Visit Theme "
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
116 :tag " Merge Theme "
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 " ")
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)))
126 (widget-insert "\n\nTheme name : ")
127 (setq custom-theme-name
128 (widget-create 'editable-field
129 :value (if (and theme (not (eq theme 'user)))
130 (symbol-name theme)
131 "")))
132 (widget-insert "Description: ")
133 (setq custom-theme-description
134 (widget-create 'text
135 :value (format-time-string "Created %Y-%m-%d.")))
136 (widget-create 'push-button
137 :notify (function custom-theme-write)
138 " Save Theme ")
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)))))
149 (widget-insert (propertize " Remove saved theme settings from Custom save file."
150 'face '(variable-pitch (:height 0.9)))))
152 (let (vars values faces face-specs)
154 ;; Load the theme settings.
155 (when theme
156 (unless (eq theme 'user)
157 (load-theme theme t))
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))))
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)))
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
189 (if (eq (car vars) 'custom-enabled-themes)
190 (progn (pop vars) (pop values))
191 (custom-theme-add-var-1 (pop vars) (pop values)))))
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 "")))
208 (defun custom-theme-revert (ignore-auto noconfirm)
209 (when (or noconfirm (y-or-n-p "Discard current changes? "))
210 (customize-create-theme custom-theme--save-name (current-buffer))))
212 ;;; Theme variables
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))))
221 (let ((entry (assq var custom-theme-variables)))
222 (cond ((null entry)
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.
231 (widget-value-set (nth 1 entry) t)
232 (let ((widget (nth 2 entry)))
233 (widget-put widget :shown-value (list value))
234 (custom-redraw widget))))))
236 (defun custom-theme-add-var-1 (symbol val)
237 (widget-insert " ")
238 (push (list symbol
239 (prog1 (widget-create 'checkbox
240 :value t
241 :help-echo "Enable/disable this variable.")
242 (widget-insert " "))
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
250 :custom-style 'simple))
251 custom-theme-variables)
252 (widget-insert " "))
254 ;;; Theme faces
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))
262 (let ((entry (assq face custom-theme-faces)))
263 (cond ((null entry)
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)
268 (move-marker custom-theme-insert-face-marker (point))
269 (widget-setup)))
270 ;; Otherwise, if SPEC is supplied, alter that face widget.
271 (spec
272 (widget-value-set (nth 1 entry) t)
273 (let ((widget (nth 2 entry)))
274 (widget-put widget :shown-value spec)
275 (custom-redraw widget)))
276 ((called-interactively-p 'interactive)
277 (error "`%s' is already present" face)))))
279 (defun custom-theme-add-face-1 (symbol spec)
280 (widget-insert " ")
281 (push (list symbol
282 (prog1
283 (widget-create 'checkbox
284 :value t
285 :help-echo "Enable/disable this face.")
286 (widget-insert " "))
287 (widget-create 'custom-face
288 :tag (custom-unlispify-tag-name symbol)
289 :documentation-shown t
290 :value symbol
291 :custom-state 'hidden
292 :custom-style 'simple
293 :shown-value spec
294 :sample-indent 34))
295 custom-theme-faces)
296 (widget-insert " "))
298 ;;; Reading and writing
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))))
315 (defun custom-theme-merge-theme (theme)
316 "Merge the custom theme THEME's settings into the current buffer."
317 (interactive
318 (list
319 (intern (completing-read "Merge custom theme: "
320 (mapcar 'symbol-name
321 (custom-available-themes))))))
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))))
327 (dolist (setting settings)
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))))
333 theme)
335 (defun custom-theme-write (&rest ignore)
336 "Write the current custom theme to its theme file."
337 (interactive)
338 (let* ((name (widget-value custom-theme-name))
339 (doc (widget-value custom-theme-description))
340 (vars custom-theme-variables)
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))
346 (unless (custom-theme-name-valid-p (intern name))
347 (error "Custom themes cannot be named `%s'" name))
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"))
355 (with-temp-buffer
356 (emacs-lisp-mode)
357 (unless (file-directory-p custom-theme-directory)
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")
364 (custom-theme-write-variables name (reverse vars))
365 (custom-theme-write-faces name (reverse faces))
366 (insert "\n(provide-theme '" name ")\n")
367 (save-buffer))
368 (message "Theme written to %s" filename)
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))))))
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."
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")
394 (dolist (spec vars)
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 ")")))))
411 (if (bolp)
412 (princ " "))
413 (princ ")")
414 (unless (looking-at "\n")
415 (princ "\n")))))
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")
426 (dolist (spec faces)
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 ")")))))
441 (if (bolp) (princ " "))
442 (princ ")")
443 (unless (looking-at "\n")
444 (princ "\n")))))
447 ;;; Describing Custom themes.
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))))
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")
469 (custom-theme--load-path)
470 '("" "c")))
471 doc)
472 (when fn
473 (princ " in `")
474 (help-insert-xref-button (file-name-nondirectory fn)
475 'help-theme-def fn)
476 (princ "'"))
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)))
497 (princ "\n\nDocumentation:\n")
498 (princ (if (stringp doc)
500 "No documentation available.")))
501 (princ "\n\nYou can ")
502 (help-insert-xref-button "customize" 'help-theme-edit theme)
503 (princ " this theme."))
506 ;;; Theme chooser
508 (defvar custom--listed-themes)
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)
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'.")
526 (define-derived-mode custom-theme-choose-mode nil "Themes"
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)
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)
543 (switch-to-buffer (get-buffer-create (or buffer "*Custom Themes*")))
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))
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 `"))
558 (widget-create 'link :value "custom-theme-load-path"
559 :button-face 'custom-link
560 :mouse-face 'highlight
561 :pressed-face 'highlight
562 :help-echo "Describe `custom-theme-load-path'."
563 :keymap custom-mode-link-map
564 :follow-link 'mouse-face
565 :action (lambda (widget &rest ignore)
566 (describe-variable 'custom-theme-load-path)))
567 (widget-insert "'.\n\n")
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)))
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")))
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))))
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))
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.
624 (progn
625 (disable-theme this-theme)
626 (widget-toggle-action widget event))
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))))
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)))))
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)))))
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."))
657 (defun custom-theme-selections-toggle (widget &optional event)
658 (when (widget-value widget)
659 ;; Deactivate multiple-selections.
660 (if (< 1 (length (delq nil (mapcar (lambda (x) (widget-value (cdr x)))
661 custom--listed-themes))))
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)))
666 ;;; cus-theme.el ends here