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