1 ;;; cus-theme.el -- custom theme creation user interface
3 ;; Copyright (C) 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Keywords: help, faces
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/>.
33 (defvar custom-new-theme-mode-map
34 (let ((map (make-keymap)))
35 (set-keymap-parent map widget-keymap
)
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
)
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
76 "Faces listed by default in the *Custom Theme* buffer.")
78 (defvar custom-theme--save-name
)
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*."
88 (switch-to-buffer (get-buffer-create (or buffer
"*Custom Theme*")))
89 (let ((inhibit-read-only t
))
91 (dolist (ov (overlays-in (point-min) (point-max)))
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
)
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
111 :help-echo
"Insert the settings of a pre-defined theme."
112 :action
(lambda (widget &optional event
)
113 (call-interactively 'custom-theme-visit-theme
)))
115 (widget-create 'push-button
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
)))
121 (widget-create 'push-button
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
)))
132 (widget-insert "Description: ")
133 (setq custom-theme-description
135 :value
(format-time-string "Created %Y-%m-%d.")))
136 (widget-create 'push-button
137 :notify
(function custom-theme-write
)
139 (when (eq theme
'user
)
140 (setq custom-theme--migrate-settings t
)
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.
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 ")
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))
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 ")
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))
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
)))
205 (goto-char (point-min))
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))))
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."
219 (let ((v (read-variable "Variable name: ")))
220 (list v
(symbol-value v
))))
221 (let ((entry (assq var custom-theme-variables
)))
223 ;; If VAR is not yet in the buffer, add it.
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))
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
)
239 (prog1 (widget-create 'checkbox
241 :help-echo
"Enable/disable this variable.")
243 (widget-create 'custom-variable
244 :tag
(custom-unlispify-tag-name symbol
)
246 :shown-value
(list val
)
249 :custom-state
'hidden
250 :custom-style
'simple
))
251 custom-theme-variables
)
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
)))
264 ;; If FACE is not yet in the buffer, add it.
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))
270 ;; Otherwise, if SPEC is supplied, alter that face widget.
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
)
283 (widget-create 'checkbox
285 :help-echo
"Enable/disable this face.")
287 (widget-create 'custom-face
288 :tag
(custom-unlispify-tag-name symbol
)
289 :documentation-shown t
291 :custom-state
'hidden
292 :custom-style
'simple
298 ;;; Reading and writing
300 (defun custom-theme-visit-theme (theme)
301 "Load the custom theme THEME's settings into the current buffer."
304 (intern (completing-read "Find custom theme: "
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."
319 (intern (completing-read "Merge custom theme: "
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
)
335 (defun custom-theme-write (&rest ignore
)
336 "Write the current custom theme to its theme file."
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
)
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
)))
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
)
361 (insert "(deftheme " name
)
362 (if doc
(insert "\n \"" doc
"\""))
364 (custom-theme-write-variables name
(reverse vars
))
365 (custom-theme-write-faces name
(reverse faces
))
366 (insert "\n(provide-theme '" name
")\n")
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)))
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
)))
379 (when (widget-get (nth 1 face
) :value
)
380 (widget-apply (nth 2 face
) :custom-mark-to-reset-standard
)))
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."
389 (let ((standard-output (current-buffer)))
390 (princ "\n(custom-theme-set-variables\n")
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
)))
401 ;; Child is null if the widget is closed (hidden).
402 (car (widget-get widget
:shown-value
)))))
403 (when (boundp symbol
)
409 (prin1 (custom-quote value
))
414 (unless (looking-at "\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."
421 (let ((standard-output (current-buffer)))
422 (princ "\n(custom-theme-set-faces\n")
427 (when (widget-get (nth 1 spec
) :value
)
428 (let* ((symbol (nth 0 spec
))
429 (widget (nth 2 spec
))
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 '("))
441 (if (bolp) (princ " "))
443 (unless (looking-at "\n")
447 ;;; Describing Custom themes.
450 (defun describe-theme (theme)
451 "Display a description of the Custom theme THEME (a symbol)."
454 (intern (completing-read "Describe custom theme: "
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)
467 (princ " is a custom theme")
468 (let ((fn (locate-file (concat (symbol-name theme
) "-theme.el")
469 (custom-theme--load-path)
474 (help-insert-xref-button (file-name-nondirectory fn
)
478 (if (not (memq theme custom-known-themes
))
480 (princ "It is not loaded.")
481 ;; Attempt to grab the theme documentation
484 (insert-file-contents fn
)
485 (let ((sexp (let ((read-circle 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."))
508 (defvar custom--listed-themes
)
510 (defcustom custom-theme-allow-multiple-selections nil
511 "Whether to allow multi-selections in the *Custom Themes* buffer."
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
)
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
)
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."
543 (switch-to-buffer (get-buffer-create (or buffer
"*Custom Themes*")))
544 (let ((inhibit-read-only t
))
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
))
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
)))
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
)
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")
605 (dolist (theme (custom-available-themes))
606 (setq widget
(widget-create 'checkbox
607 :value
(custom-theme-enabled-p 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))
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.
625 (disable-theme this-theme
)
626 (widget-toggle-action widget event
))
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."
648 (let ((widget (widget-at (line-beginning-position))))
650 (describe-theme (widget-get widget
:theme-name
)))))
652 (defun custom-theme-save (&rest ignore
)
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