1 ;;; cus-theme.el -- custom theme creation user interface
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Alex Schroeder <alex@gnu.org>
8 ;; 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
"n" 'widget-forward
)
38 (define-key map
"p" 'widget-backward
)
40 "Keymap for `custom-new-theme-mode'.")
42 (define-derived-mode custom-new-theme-mode nil
"New-Theme"
43 "Major mode for the buffer created by `customize-create-theme'.
44 Do not call this mode function yourself. It is only meant for internal
45 use by `customize-create-theme'."
46 (use-local-map custom-new-theme-mode-map
)
47 (define-key custom-new-theme-mode-map
[mouse-1
] 'widget-move-and-invoke
)
48 (set (make-local-variable 'widget-documentation-face
) 'custom-documentation
)
49 (set (make-local-variable 'widget-button-face
) custom-button
)
50 (set (make-local-variable 'widget-button-pressed-face
) custom-button-pressed
)
51 (set (make-local-variable 'widget-mouse-face
) custom-button-mouse
)
52 (when custom-raised-buttons
53 (set (make-local-variable 'widget-push-button-prefix
) "")
54 (set (make-local-variable 'widget-push-button-suffix
) "")
55 (set (make-local-variable 'widget-link-prefix
) "")
56 (set (make-local-variable 'widget-link-suffix
) "")))
57 (put 'custom-new-theme-mode
'mode-class
'special
)
59 (defvar custom-theme-name nil
)
60 (defvar custom-theme-variables nil
)
61 (defvar custom-theme-faces nil
)
62 (defvar custom-theme-description
)
63 (defvar custom-theme-insert-variable-marker
)
64 (defvar custom-theme-insert-face-marker
)
67 (defun customize-create-theme ()
68 "Create a custom theme."
70 (switch-to-buffer (generate-new-buffer "*New Custom Theme*"))
71 (let ((inhibit-read-only t
))
73 (custom-new-theme-mode)
74 (make-local-variable 'custom-theme-name
)
75 (make-local-variable 'custom-theme-variables
)
76 (make-local-variable 'custom-theme-faces
)
77 (make-local-variable 'custom-theme-description
)
78 (make-local-variable 'custom-theme-insert-variable-marker
)
79 (make-local-variable 'custom-theme-insert-face-marker
)
80 (widget-insert "This buffer helps you write a custom theme elisp file.
81 This will help you share your customizations with other people.
83 Insert the names of all variables and faces you want the theme to include.
84 Invoke \"Save Theme\" to save the theme. The theme file will be saved to
85 the directory " custom-theme-directory
"\n\n")
86 (widget-create 'push-button
88 :help-echo
"Insert the settings of a pre-defined theme."
89 :action
(lambda (widget &optional event
)
90 (call-interactively 'custom-theme-visit-theme
)))
92 (widget-create 'push-button
94 :help-echo
"Merge in the settings of a pre-defined theme."
95 :action
(lambda (widget &optional event
)
96 (call-interactively 'custom-theme-merge-theme
)))
98 (widget-create 'push-button
99 :notify
(lambda (&rest ignore
)
100 (when (y-or-n-p "Discard current changes? ")
101 (kill-buffer (current-buffer))
102 (customize-create-theme)))
105 (widget-create 'push-button
106 :notify
(function custom-theme-write
)
110 (widget-insert "\n\nTheme name: ")
111 (setq custom-theme-name
112 (widget-create 'editable-field
115 (widget-insert "\n\nDocumentation:\n")
116 (setq custom-theme-description
118 :value
(format-time-string "Created %Y-%m-%d.")))
120 (widget-create 'push-button
121 :tag
"Insert Variable"
122 :help-echo
"Add another variable to this theme."
123 :action
(lambda (widget &optional event
)
124 (call-interactively 'custom-theme-add-variable
)))
126 (setq custom-theme-insert-variable-marker
(point-marker))
128 (widget-create 'push-button
130 :help-echo
"Add another face to this theme."
131 :action
(lambda (widget &optional event
)
132 (call-interactively 'custom-theme-add-face
)))
134 (setq custom-theme-insert-face-marker
(point-marker))
136 (widget-create 'push-button
137 :notify
(lambda (&rest ignore
)
138 (when (y-or-n-p "Discard current changes? ")
139 (kill-buffer (current-buffer))
140 (customize-create-theme)))
143 (widget-create 'push-button
144 :notify
(function custom-theme-write
)
148 (goto-char (point-min))
153 (defun custom-theme-add-variable (symbol)
154 (interactive "vVariable name: ")
155 (cond ((assq symbol custom-theme-variables
)
156 (message "%s is already in the theme" (symbol-name symbol
)))
157 ((not (boundp symbol
))
158 (message "%s is not defined as a variable" (symbol-name symbol
)))
159 ((eq symbol
'custom-enabled-themes
)
160 (message "Custom theme cannot contain `custom-enabled-themes'"))
163 (goto-char custom-theme-insert-variable-marker
)
165 (let ((widget (widget-create 'custom-variable
166 :tag
(custom-unlispify-tag-name symbol
)
168 :action
'custom-theme-variable-action
169 :custom-state
'unknown
171 (push (cons symbol widget
) custom-theme-variables
)
172 (custom-magic-reset widget
))
175 (defvar custom-theme-variable-menu
176 `(("Reset to Current" custom-redraw
178 (and (boundp (widget-value widget
))
179 (memq (widget-get widget
:custom-state
)
180 '(themed modified changed
)))))
181 ("Reset to Theme Value" custom-variable-reset-theme
183 (let ((theme (intern (widget-value custom-theme-name
)))
184 (symbol (widget-value widget
))
186 (and (custom-theme-p theme
)
187 (dolist (setting (get theme
'theme-settings
) found
)
188 (if (and (eq (cadr setting
) symbol
)
189 (eq (car setting
) 'theme-value
))
191 ("---" ignore ignore
)
192 ("Delete" custom-theme-delete-variable nil
))
193 "Alist of actions for the `custom-variable' widget in Custom Theme Mode.
194 See the documentation for `custom-variable'.")
196 (defun custom-theme-variable-action (widget &optional event
)
197 "Show the Custom Theme Mode menu for a `custom-variable' widget.
198 Optional EVENT is the location for the menu."
199 (let ((custom-variable-menu custom-theme-variable-menu
))
200 (custom-variable-action widget event
)))
202 (defun custom-variable-reset-theme (widget)
203 "Reset WIDGET to its value for the currently edited theme."
204 (let ((theme (intern (widget-value custom-theme-name
)))
205 (symbol (widget-value widget
))
207 (dolist (setting (get theme
'theme-settings
))
208 (if (and (eq (cadr setting
) symbol
)
209 (eq (car setting
) 'theme-value
))
210 (setq found setting
)))
211 (widget-value-set (car (widget-get widget
:children
))
213 (widget-put widget
:custom-state
'themed
)
214 (custom-redraw-magic widget
)
217 (defun custom-theme-delete-variable (widget)
218 (setq custom-theme-variables
219 (assq-delete-all (widget-value widget
) custom-theme-variables
))
220 (widget-delete widget
))
224 (defun custom-theme-add-face (symbol)
225 (interactive (list (read-face-name "Face name" nil nil
)))
226 (cond ((assq symbol custom-theme-faces
)
227 (message "%s is already in the theme" (symbol-name symbol
)))
228 ((not (facep symbol
))
229 (message "%s is not defined as a face" (symbol-name symbol
)))
232 (goto-char custom-theme-insert-face-marker
)
234 (let ((widget (widget-create 'custom-face
235 :tag
(custom-unlispify-tag-name symbol
)
237 :action
'custom-theme-face-action
238 :custom-state
'unknown
240 (push (cons symbol widget
) custom-theme-faces
)
241 (custom-magic-reset widget
)
244 (defvar custom-theme-face-menu
245 `(("Reset to Theme Value" custom-face-reset-theme
247 (let ((theme (intern (widget-value custom-theme-name
)))
248 (symbol (widget-value widget
))
250 (and (custom-theme-p theme
)
251 (dolist (setting (get theme
'theme-settings
) found
)
252 (if (and (eq (cadr setting
) symbol
)
253 (eq (car setting
) 'theme-face
))
255 ("---" ignore ignore
)
256 ("Delete" custom-theme-delete-face nil
))
257 "Alist of actions for the `custom-variable' widget in Custom Theme Mode.
258 See the documentation for `custom-variable'.")
260 (defun custom-theme-face-action (widget &optional event
)
261 "Show the Custom Theme Mode menu for a `custom-face' widget.
262 Optional EVENT is the location for the menu."
263 (let ((custom-face-menu custom-theme-face-menu
))
264 (custom-face-action widget event
)))
266 (defun custom-face-reset-theme (widget)
267 "Reset WIDGET to its value for the currently edited theme."
268 (let ((theme (intern (widget-value custom-theme-name
)))
269 (symbol (widget-value widget
))
271 (dolist (setting (get theme
'theme-settings
))
272 (if (and (eq (cadr setting
) symbol
)
273 (eq (car setting
) 'theme-face
))
274 (setq found setting
)))
275 (widget-value-set (car (widget-get widget
:children
))
277 (widget-put widget
:custom-state
'themed
)
278 (custom-redraw-magic widget
)
281 (defun custom-theme-delete-face (widget)
282 (setq custom-theme-faces
283 (assq-delete-all (widget-value widget
) custom-theme-faces
))
284 (widget-delete widget
))
286 ;;; Reading and writing
288 (defun custom-theme-visit-theme ()
290 (when (or (null custom-theme-variables
)
291 (if (y-or-n-p "Discard current changes? ")
292 (progn (customize-create-theme) t
)))
293 (let ((theme (call-interactively 'custom-theme-merge-theme
)))
294 (unless (eq theme
'user
)
295 (widget-value-set custom-theme-name
(symbol-name theme
)))
296 (widget-value-set custom-theme-description
297 (or (get theme
'theme-documentation
)
298 (format-time-string "Created %Y-%m-%d.")))
301 (defun custom-theme-merge-theme (theme)
302 (interactive "SCustom theme name: ")
303 (unless (eq theme
'user
)
305 (let ((settings (get theme
'theme-settings
)))
306 (dolist (setting settings
)
307 (if (eq (car setting
) 'theme-value
)
308 (custom-theme-add-variable (cadr setting
))
309 (custom-theme-add-face (cadr setting
)))))
310 (disable-theme theme
)
313 (defun custom-theme-write (&rest ignore
)
314 (let* ((name (widget-value custom-theme-name
))
315 (filename (expand-file-name (concat name
"-theme.el")
316 custom-theme-directory
))
317 (doc (widget-value custom-theme-description
))
318 (vars custom-theme-variables
)
319 (faces custom-theme-faces
))
320 (cond ((or (string-equal name
"")
321 (string-equal name
"user")
322 (string-equal name
"changed"))
323 (error "Custom themes cannot be named `%s'" name
))
324 ((string-match " " name
)
325 (error "Custom theme names should not contain spaces"))
326 ((if (file-exists-p filename
)
328 (format "File %s exists. Overwrite? " filename
))))
332 (unless (file-exists-p custom-theme-directory
)
333 (make-directory (file-name-as-directory custom-theme-directory
) t
))
334 (setq buffer-file-name filename
)
336 (insert "(deftheme " name
)
337 (if doc
(insert "\n \"" doc
"\""))
339 (custom-theme-write-variables name vars
)
340 (custom-theme-write-faces name faces
)
341 (insert "\n(provide-theme '" name
")\n")
344 (widget-put (cdr var
) :custom-state
'saved
)
345 (custom-redraw-magic (cdr var
)))
347 (widget-put (cdr face
) :custom-state
'saved
)
348 (custom-redraw-magic (cdr face
)))))
350 (defun custom-theme-write-variables (theme vars
)
351 "Write a `custom-theme-set-variables' command for THEME.
352 It includes all variables in list VARS."
354 (let ((standard-output (current-buffer)))
355 (princ "\n(custom-theme-set-variables\n")
360 (let* ((symbol (car spec
))
361 (child (car-safe (widget-get (cdr spec
) :children
)))
364 ;; For hidden widgets, use the standard value
365 (get symbol
'standard-value
))))
366 (when (boundp symbol
)
372 (prin1 (custom-quote value
))
378 (unless (looking-at "\n")
381 (defun custom-theme-write-faces (theme faces
)
382 "Write a `custom-theme-set-faces' command for THEME.
383 It includes all faces in list FACES."
385 (let ((standard-output (current-buffer)))
386 (princ "\n(custom-theme-set-faces\n")
391 (let* ((symbol (car spec
))
392 (child (car-safe (widget-get (cdr spec
) :children
)))
393 (value (if child
(widget-value child
))))
394 (when (and (facep symbol
) child
)
406 (unless (looking-at "\n")
409 ;; arch-tag: cd6919bc-63af-410e-bae2-b6702e762344
410 ;;; cus-theme.el ends here