* cus-theme.el: Rewrite the Custom New Theme Mode interface.
[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 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: help, faces
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
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 "n" 'widget-forward)
39 (define-key map "p" 'widget-backward)
40 (define-key map [mouse-1] 'widget-move-and-invoke)
41 map)
42 "Keymap for `custom-new-theme-mode'.")
43
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)
60
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)
67
68 ;;;###autoload
69 (defun customize-create-theme ()
70 "Create a custom theme."
71 (interactive)
72 (switch-to-buffer (generate-new-buffer "*New Custom Theme*"))
73 (let ((inhibit-read-only t))
74 (erase-buffer))
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.
84
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
89 :tag "Visit Theme"
90 :help-echo "Insert the settings of a pre-defined theme."
91 :action (lambda (widget &optional event)
92 (call-interactively 'custom-theme-visit-theme)))
93 (widget-insert " ")
94 (widget-create 'push-button
95 :tag "Merge Theme"
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)))
99 (widget-insert " ")
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)))
105 "Reset Buffer")
106 (widget-insert " ")
107 (widget-create 'push-button
108 :notify (function custom-theme-write)
109 "Save Theme")
110 (widget-insert "\n")
111
112 (widget-insert "\n\nTheme name: ")
113 (setq custom-theme-name
114 (widget-create 'editable-field
115 :size 10
116 user-login-name))
117 (widget-insert "\n\nDocumentation:\n")
118 (setq custom-theme-description
119 (widget-create 'text
120 :value (format-time-string "Created %Y-%m-%d.")))
121 (widget-insert "\n")
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)))
127 (widget-insert "\n")
128 (setq custom-theme-insert-variable-marker (point-marker))
129 (widget-insert "\n")
130 (widget-create 'push-button
131 :tag "Insert Face"
132 :help-echo "Add another face to this theme."
133 :action (lambda (widget &optional event)
134 (call-interactively 'custom-theme-add-face)))
135 (widget-insert "\n")
136 (setq custom-theme-insert-face-marker (point-marker))
137 (widget-insert "\n")
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)))
143 "Reset Buffer")
144 (widget-insert " ")
145 (widget-create 'push-button
146 :notify (function custom-theme-write)
147 "Save Theme")
148 (widget-insert "\n")
149 (widget-setup)
150 (goto-char (point-min))
151 (message ""))
152
153 ;;; Theme variables
154
155 (defun custom-theme-add-variable (symbol)
156 (interactive "vVariable name: ")
157 (save-excursion
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))
161 (widget-insert "\n")
162 (let ((widget (widget-create 'custom-variable
163 :tag (custom-unlispify-tag-name symbol)
164 :custom-level 0
165 :action 'custom-theme-variable-action
166 :custom-state 'unknown
167 :value symbol)))
168 (push (cons symbol widget) custom-theme-variables)
169 (custom-magic-reset widget))
170 (widget-setup))))
171
172 (defvar custom-theme-variable-menu
173 `(("Reset to Current" custom-redraw
174 (lambda (widget)
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
179 (lambda (widget)
180 (let ((theme (intern (widget-value custom-theme-name)))
181 (symbol (widget-value widget))
182 found)
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))
187 (setq found t)))))))
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'.")
192
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)))
198
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))
203 found)
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))
209 (nth 3 found)))
210 (widget-put widget :custom-state 'themed)
211 (custom-redraw-magic widget)
212 (widget-setup))
213
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))
218
219 ;;; Theme faces
220
221 (defun custom-theme-add-face (symbol)
222 (interactive (list (read-face-name "Face name" nil nil)))
223 (save-excursion
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))
227 (widget-insert "\n")
228 (let ((widget (widget-create 'custom-face
229 :tag (custom-unlispify-tag-name symbol)
230 :custom-level 0
231 :action 'custom-theme-face-action
232 :custom-state 'unknown
233 :value symbol)))
234 (push (cons symbol widget) custom-theme-faces)
235 (custom-magic-reset widget)
236 (widget-setup)))))
237
238 (defvar custom-theme-face-menu
239 `(("Reset to Theme Value" custom-face-reset-theme
240 (lambda (widget)
241 (let ((theme (intern (widget-value custom-theme-name)))
242 (symbol (widget-value widget))
243 found)
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))
248 (setq found t)))))))
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'.")
253
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)))
259
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))
264 found)
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))
270 (nth 3 found)))
271 (widget-put widget :custom-state 'themed)
272 (custom-redraw-magic widget)
273 (widget-setup))
274
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))
279
280 ;;; Reading and writing
281
282 (defun custom-theme-visit-theme ()
283 (interactive)
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.")))
293 (widget-setup))))
294
295 (defun custom-theme-merge-theme (theme)
296 (interactive "SCustom theme name: ")
297 (unless (eq theme 'user)
298 (load-theme theme))
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)
305 theme)
306
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)
321 (not (y-or-n-p
322 (format "File %s exists. Overwrite? " filename))))
323 (error "Aborted")))
324 (with-temp-buffer
325 (emacs-lisp-mode)
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)
329 (erase-buffer)
330 (insert "(deftheme " name)
331 (if doc (insert "\n \"" doc "\""))
332 (insert ")\n")
333 (custom-theme-write-variables name vars)
334 (custom-theme-write-faces name faces)
335 (insert "\n(provide-theme '" name ")\n")
336 (save-buffer))
337 (dolist (var vars)
338 (widget-put (cdr var) :custom-state 'saved)
339 (custom-redraw-magic (cdr var)))
340 (dolist (face faces)
341 (widget-put (cdr face) :custom-state 'saved)
342 (custom-redraw-magic (cdr face)))))
343
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."
347 (when vars
348 (let ((standard-output (current-buffer)))
349 (princ "\n(custom-theme-set-variables\n")
350 (princ " '")
351 (princ theme)
352 (princ "\n")
353 (mapc (lambda (spec)
354 (let* ((symbol (car spec))
355 (child (car-safe (widget-get (cdr spec) :children)))
356 (value (if child
357 (widget-value child)
358 ;; For hidden widgets, use the standard value
359 (get symbol 'standard-value))))
360 (when (boundp symbol)
361 (unless (bolp)
362 (princ "\n"))
363 (princ " '(")
364 (prin1 symbol)
365 (princ " ")
366 (prin1 (custom-quote value))
367 (princ ")"))))
368 vars)
369 (if (bolp)
370 (princ " "))
371 (princ ")")
372 (unless (looking-at "\n")
373 (princ "\n")))))
374
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."
378 (when faces
379 (let ((standard-output (current-buffer)))
380 (princ "\n(custom-theme-set-faces\n")
381 (princ " '")
382 (princ theme)
383 (princ "\n")
384 (mapc (lambda (spec)
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)
389 (unless (bolp)
390 (princ "\n"))
391 (princ " '(")
392 (prin1 symbol)
393 (princ " ")
394 (prin1 value)
395 (princ ")"))))
396 faces)
397 (if (bolp)
398 (princ " "))
399 (princ ")")
400 (unless (looking-at "\n")
401 (princ "\n")))))
402
403 ;;; arch-tag: cd6919bc-63af-410e-bae2-b6702e762344
404 ;;; cus-theme.el ends here