1 ;;; cus-theme.el -- custom theme creation user interface
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Keywords: help, faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
34 (defvar custom-new-theme-mode-map
35 (let ((map (make-keymap)))
36 (set-keymap-parent map widget-keymap
)
38 (define-key map
"n" 'widget-forward
)
39 (define-key map
"p" 'widget-backward
)
40 (define-key map
[mouse-1
] 'widget-move-and-invoke
)
42 "Keymap for `custom-new-theme-mode'.")
44 (define-derived-mode custom-new-theme-mode nil
"New-Theme"
45 "Major mode for the buffer created by `customize-create-theme'.
46 Do not call this mode function yourself. It is only meant for internal
47 use by `customize-create-theme'."
48 (use-local-map custom-new-theme-mode-map
)
49 (define-key custom-new-theme-mode-map
[mouse-1
] 'widget-move-and-invoke
)
50 (set (make-local-variable 'widget-documentation-face
) 'custom-documentation
)
51 (set (make-local-variable 'widget-button-face
) custom-button
)
52 (set (make-local-variable 'widget-button-pressed-face
) custom-button-pressed
)
53 (set (make-local-variable 'widget-mouse-face
) custom-button-mouse
)
54 (when custom-raised-buttons
55 (set (make-local-variable 'widget-push-button-prefix
) "")
56 (set (make-local-variable 'widget-push-button-suffix
) "")
57 (set (make-local-variable 'widget-link-prefix
) "")
58 (set (make-local-variable 'widget-link-suffix
) "")))
59 (put 'custom-new-theme-mode
'mode-class
'special
)
61 (defvar custom-theme-name nil
)
62 (defvar custom-theme-variables nil
)
63 (defvar custom-theme-faces nil
)
64 (defvar custom-theme-description
)
65 (defvar custom-theme-insert-variable-marker
)
66 (defvar custom-theme-insert-face-marker
)
69 (defun customize-create-theme ()
70 "Create a custom theme."
72 (switch-to-buffer (generate-new-buffer "*New Custom Theme*"))
73 (let ((inhibit-read-only t
))
75 (custom-new-theme-mode)
76 (make-local-variable 'custom-theme-name
)
77 (make-local-variable 'custom-theme-variables
)
78 (make-local-variable 'custom-theme-faces
)
79 (make-local-variable 'custom-theme-description
)
80 (make-local-variable 'custom-theme-insert-variable-marker
)
81 (make-local-variable 'custom-theme-insert-face-marker
)
82 (widget-insert "This buffer helps you write a custom theme elisp file.
83 This will help you share your customizations with other people.
85 Insert the names of all variables and faces you want the theme to include.
86 Invoke \"Save Theme\" to save the theme. The theme file will be saved to
87 the directory " custom-theme-directory
"\n\n")
88 (widget-create 'push-button
90 :help-echo
"Insert the settings of a pre-defined theme."
91 :action
(lambda (widget &optional event
)
92 (call-interactively 'custom-theme-visit-theme
)))
94 (widget-create 'push-button
96 :help-echo
"Merge in the settings of a pre-defined theme."
97 :action
(lambda (widget &optional event
)
98 (call-interactively 'custom-theme-merge-theme
)))
100 (widget-create 'push-button
101 :notify
(lambda (&rest ignore
)
102 (when (y-or-n-p "Discard current changes?")
103 (kill-buffer (current-buffer))
104 (customize-create-theme)))
107 (widget-create 'push-button
108 :notify
(function custom-theme-write
)
112 (widget-insert "\n\nTheme name: ")
113 (setq custom-theme-name
114 (widget-create 'editable-field
117 (widget-insert "\n\nDocumentation:\n")
118 (setq custom-theme-description
120 :value
(format-time-string "Created %Y-%m-%d.")))
122 (widget-create 'push-button
123 :tag
"Insert Variable"
124 :help-echo
"Add another variable to this theme."
125 :action
(lambda (widget &optional event
)
126 (call-interactively 'custom-theme-add-variable
)))
128 (setq custom-theme-insert-variable-marker
(point-marker))
130 (widget-create 'push-button
132 :help-echo
"Add another face to this theme."
133 :action
(lambda (widget &optional event
)
134 (call-interactively 'custom-theme-add-face
)))
136 (setq custom-theme-insert-face-marker
(point-marker))
138 (widget-create 'push-button
139 :notify
(lambda (&rest ignore
)
140 (when (y-or-n-p "Discard current changes?")
141 (kill-buffer (current-buffer))
142 (customize-create-theme)))
145 (widget-create 'push-button
146 :notify
(function custom-theme-write
)
150 (goto-char (point-min))
155 (defun custom-theme-add-variable (symbol)
156 (interactive "vVariable name: ")
158 (goto-char custom-theme-insert-variable-marker
)
159 (if (assq symbol custom-theme-variables
)
160 (message "%s is already in the theme" (symbol-name symbol
))
162 (let ((widget (widget-create 'custom-variable
163 :tag
(custom-unlispify-tag-name symbol
)
165 :action
'custom-theme-variable-action
166 :custom-state
'unknown
168 (push (cons symbol widget
) custom-theme-variables
)
169 (custom-magic-reset widget
))
172 (defvar custom-theme-variable-menu
173 `(("Reset to Current" custom-redraw
175 (and (boundp (widget-value widget
))
176 (memq (widget-get widget
:custom-state
)
177 '(themed modified changed
)))))
178 ("Reset to Theme Value" custom-variable-reset-theme
180 (let ((theme (intern (widget-value custom-theme-name
)))
181 (symbol (widget-value widget
))
183 (and (custom-theme-p theme
)
184 (dolist (setting (get theme
'theme-settings
) found
)
185 (if (and (eq (cadr setting
) symbol
)
186 (eq (car setting
) 'theme-value
))
188 ("---" ignore ignore
)
189 ("Delete" custom-theme-delete-variable nil
))
190 "Alist of actions for the `custom-variable' widget in Custom Theme Mode.
191 See the documentation for `custom-variable'.")
193 (defun custom-theme-variable-action (widget &optional event
)
194 "Show the Custom Theme Mode menu for a `custom-variable' widget.
195 Optional EVENT is the location for the menu."
196 (let ((custom-variable-menu custom-theme-variable-menu
))
197 (custom-variable-action widget event
)))
199 (defun custom-variable-reset-theme (widget)
200 "Reset WIDGET to its value for the currently edited theme."
201 (let ((theme (intern (widget-value custom-theme-name
)))
202 (symbol (widget-value widget
))
204 (dolist (setting (get theme
'theme-settings
))
205 (if (and (eq (cadr setting
) symbol
)
206 (eq (car setting
) 'theme-value
))
207 (setq found setting
)))
208 (widget-value-set (car (widget-get widget
:children
))
210 (widget-put widget
:custom-state
'themed
)
211 (custom-redraw-magic widget
)
214 (defun custom-theme-delete-variable (widget)
215 (setq custom-theme-variables
216 (assq-delete-all (widget-value widget
) custom-theme-variables
))
217 (widget-delete widget
))
221 (defun custom-theme-add-face (symbol)
222 (interactive (list (read-face-name "Face name" nil nil
)))
224 (goto-char custom-theme-insert-face-marker
)
225 (if (assq symbol custom-theme-faces
)
226 (message "%s is already in the theme" (symbol-name symbol
))
228 (let ((widget (widget-create 'custom-face
229 :tag
(custom-unlispify-tag-name symbol
)
231 :action
'custom-theme-face-action
232 :custom-state
'unknown
234 (push (cons symbol widget
) custom-theme-faces
)
235 (custom-magic-reset widget
)
238 (defvar custom-theme-face-menu
239 `(("Reset to Theme Value" custom-face-reset-theme
241 (let ((theme (intern (widget-value custom-theme-name
)))
242 (symbol (widget-value widget
))
244 (and (custom-theme-p theme
)
245 (dolist (setting (get theme
'theme-settings
) found
)
246 (if (and (eq (cadr setting
) symbol
)
247 (eq (car setting
) 'theme-face
))
249 ("---" ignore ignore
)
250 ("Delete" custom-theme-delete-face nil
))
251 "Alist of actions for the `custom-variable' widget in Custom Theme Mode.
252 See the documentation for `custom-variable'.")
254 (defun custom-theme-face-action (widget &optional event
)
255 "Show the Custom Theme Mode menu for a `custom-face' widget.
256 Optional EVENT is the location for the menu."
257 (let ((custom-face-menu custom-theme-face-menu
))
258 (custom-face-action widget event
)))
260 (defun custom-face-reset-theme (widget)
261 "Reset WIDGET to its value for the currently edited theme."
262 (let ((theme (intern (widget-value custom-theme-name
)))
263 (symbol (widget-value widget
))
265 (dolist (setting (get theme
'theme-settings
))
266 (if (and (eq (cadr setting
) symbol
)
267 (eq (car setting
) 'theme-face
))
268 (setq found setting
)))
269 (widget-value-set (car (widget-get widget
:children
))
271 (widget-put widget
:custom-state
'themed
)
272 (custom-redraw-magic widget
)
275 (defun custom-theme-delete-face (widget)
276 (setq custom-theme-faces
277 (assq-delete-all (widget-value widget
) custom-theme-faces
))
278 (widget-delete widget
))
280 ;;; Reading and writing
282 (defun custom-theme-visit-theme ()
284 (when (or (null custom-theme-variables
)
285 (if (y-or-n-p "Discard current changes?")
286 (progn (customize-create-theme) t
)))
287 (let ((theme (call-interactively 'custom-theme-merge-theme
)))
288 (unless (eq theme
'user
)
289 (widget-value-set custom-theme-name
(symbol-name theme
)))
290 (widget-value-set custom-theme-description
291 (or (get theme
'theme-documentation
)
292 (format-time-string "Created %Y-%m-%d.")))
295 (defun custom-theme-merge-theme (theme)
296 (interactive "SCustom theme name: ")
297 (unless (eq theme
'user
)
299 (let ((settings (get theme
'theme-settings
)))
300 (dolist (setting settings
)
301 (if (eq (car setting
) 'theme-value
)
302 (custom-theme-add-variable (cadr setting
))
303 (custom-theme-add-face (cadr setting
)))))
304 (disable-theme theme
)
307 (defun custom-theme-write (&rest ignore
)
308 (let* ((name (widget-value custom-theme-name
))
309 (filename (expand-file-name (concat name
"-theme.el")
310 custom-theme-directory
))
311 (doc (widget-value custom-theme-description
))
312 (vars custom-theme-variables
)
313 (faces custom-theme-faces
))
314 (cond ((or (string-equal name
"")
315 (string-equal name
"user")
316 (string-equal name
"changed"))
317 (error "Custom themes cannot be named `%s'" name
))
318 ((string-match " " name
)
319 (error "Custom theme names should not contain spaces"))
320 ((if (file-exists-p filename
)
322 (format "File %s exists. Overwrite? " filename
))))
326 (unless (file-exists-p custom-theme-directory
)
327 (make-directory (file-name-as-directory custom-theme-directory
) t
))
328 (setq buffer-file-name filename
)
330 (insert "(deftheme " name
)
331 (if doc
(insert "\n \"" doc
"\""))
333 (custom-theme-write-variables name vars
)
334 (custom-theme-write-faces name faces
)
335 (insert "\n(provide-theme '" name
")\n")
338 (widget-put (cdr var
) :custom-state
'saved
)
339 (custom-redraw-magic (cdr var
)))
341 (widget-put (cdr face
) :custom-state
'saved
)
342 (custom-redraw-magic (cdr face
)))))
344 (defun custom-theme-write-variables (theme vars
)
345 "Write a `custom-theme-set-variables' command for THEME.
346 It includes all variables in list VARS."
348 (let ((standard-output (current-buffer)))
349 (princ "\n(custom-theme-set-variables\n")
354 (let* ((symbol (car spec
))
355 (child (car-safe (widget-get (cdr spec
) :children
)))
358 ;; For hidden widgets, use the standard value
359 (get symbol
'standard-value
))))
360 (when (boundp symbol
)
366 (prin1 (custom-quote value
))
372 (unless (looking-at "\n")
375 (defun custom-theme-write-faces (theme faces
)
376 "Write a `custom-theme-set-faces' command for THEME.
377 It includes all faces in list FACES."
379 (let ((standard-output (current-buffer)))
380 (princ "\n(custom-theme-set-faces\n")
385 (let* ((symbol (car spec
))
386 (child (car-safe (widget-get (cdr spec
) :children
)))
387 (value (if child
(widget-value child
))))
388 (when (and (facep symbol
) child
)
400 (unless (looking-at "\n")
403 ;;; arch-tag: cd6919bc-63af-410e-bae2-b6702e762344
404 ;;; cus-theme.el ends here