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