comparison lisp/cus-edit.el @ 17334:1effe507ea85

Initial revision
author Per Abrahamsen <abraham@dina.kvl.dk>
date Mon, 07 Apr 1997 13:42:59 +0000
parents
children 30a567b89fb6
comparison
equal deleted inserted replaced
17333:0cc83e8612f0 17334:1effe507ea85
1 ;;; cus-edit.el --- Tools for customization Emacs.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7 ;; Version: 1.71
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
10 ;;; Commentary:
11 ;;
12 ;; See `custom.el'.
13
14 ;;; Code:
15
16 (require 'cus-face)
17 (require 'wid-edit)
18 (require 'easymenu)
19
20 (define-widget-keywords :custom-prefixes :custom-menu :custom-show
21 :custom-magic :custom-state :custom-level :custom-form
22 :custom-set :custom-save :custom-reset-current :custom-reset-saved
23 :custom-reset-factory)
24
25 ;;; Customization Groups.
26
27 (defgroup emacs nil
28 "Customization of the One True Editor."
29 :link '(custom-manual "(emacs)Top"))
30
31 ;; Most of these groups are stolen from `finder.el',
32 (defgroup editing nil
33 "Basic text editing facilities."
34 :group 'emacs)
35
36 (defgroup abbrev nil
37 "Abbreviation handling, typing shortcuts, macros."
38 :tag "Abbreviations"
39 :group 'editing)
40
41 (defgroup matching nil
42 "Various sorts of searching and matching."
43 :group 'editing)
44
45 (defgroup emulations nil
46 "Emulations of other editors."
47 :group 'editing)
48
49 (defgroup mouse nil
50 "Mouse support."
51 :group 'editing)
52
53 (defgroup outlines nil
54 "Support for hierarchical outlining."
55 :group 'editing)
56
57 (defgroup external nil
58 "Interfacing to external utilities."
59 :group 'emacs)
60
61 (defgroup bib nil
62 "Code related to the `bib' bibliography processor."
63 :tag "Bibliography"
64 :group 'external)
65
66 (defgroup processes nil
67 "Process, subshell, compilation, and job control support."
68 :group 'external
69 :group 'development)
70
71 (defgroup programming nil
72 "Support for programming in other languages."
73 :group 'emacs)
74
75 (defgroup languages nil
76 "Specialized modes for editing programming languages."
77 :group 'programming)
78
79 (defgroup lisp nil
80 "Lisp support, including Emacs Lisp."
81 :group 'languages
82 :group 'development)
83
84 (defgroup c nil
85 "Support for the C language and related languages."
86 :group 'languages)
87
88 (defgroup tools nil
89 "Programming tools."
90 :group 'programming)
91
92 (defgroup oop nil
93 "Support for object-oriented programming."
94 :group 'programming)
95
96 (defgroup applications nil
97 "Applications written in Emacs."
98 :group 'emacs)
99
100 (defgroup calendar nil
101 "Calendar and time management support."
102 :group 'applications)
103
104 (defgroup mail nil
105 "Modes for electronic-mail handling."
106 :group 'applications)
107
108 (defgroup news nil
109 "Support for netnews reading and posting."
110 :group 'applications)
111
112 (defgroup games nil
113 "Games, jokes and amusements."
114 :group 'applications)
115
116 (defgroup development nil
117 "Support for further development of Emacs."
118 :group 'emacs)
119
120 (defgroup docs nil
121 "Support for Emacs documentation."
122 :group 'development)
123
124 (defgroup extensions nil
125 "Emacs Lisp language extensions."
126 :group 'development)
127
128 (defgroup internal nil
129 "Code for Emacs internals, build process, defaults."
130 :group 'development)
131
132 (defgroup maint nil
133 "Maintenance aids for the Emacs development group."
134 :tag "Maintenance"
135 :group 'development)
136
137 (defgroup environment nil
138 "Fitting Emacs with its environment."
139 :group 'emacs)
140
141 (defgroup comm nil
142 "Communications, networking, remote access to files."
143 :tag "Communication"
144 :group 'environment)
145
146 (defgroup hardware nil
147 "Support for interfacing with exotic hardware."
148 :group 'environment)
149
150 (defgroup terminals nil
151 "Support for terminal types."
152 :group 'environment)
153
154 (defgroup unix nil
155 "Front-ends/assistants for, or emulators of, UNIX features."
156 :group 'environment)
157
158 (defgroup vms nil
159 "Support code for vms."
160 :group 'environment)
161
162 (defgroup i18n nil
163 "Internationalization and alternate character-set support."
164 :group 'environment
165 :group 'editing)
166
167 (defgroup frames nil
168 "Support for Emacs frames and window systems."
169 :group 'environment)
170
171 (defgroup data nil
172 "Support editing files of data."
173 :group 'emacs)
174
175 (defgroup wp nil
176 "Word processing."
177 :group 'emacs)
178
179 (defgroup tex nil
180 "Code related to the TeX formatter."
181 :group 'wp)
182
183 (defgroup faces nil
184 "Support for multiple fonts."
185 :group 'emacs)
186
187 (defgroup hypermedia nil
188 "Support for links between text or other media types."
189 :group 'emacs)
190
191 (defgroup help nil
192 "Support for on-line help systems."
193 :group 'emacs)
194
195 (defgroup local nil
196 "Code local to your site."
197 :group 'emacs)
198
199 (defgroup customize '((widgets custom-group))
200 "Customization of the Customization support."
201 :link '(custom-manual "(custom)Top")
202 :link '(url-link :tag "Development Page"
203 "http://www.dina.kvl.dk/~abraham/custom/")
204 :prefix "custom-"
205 :group 'help
206 :group 'faces)
207
208 ;;; Utilities.
209
210 (defun custom-quote (sexp)
211 "Quote SEXP iff it is not self quoting."
212 (if (or (memq sexp '(t nil))
213 (and (symbolp sexp)
214 (eq (aref (symbol-name sexp) 0) ?:))
215 (and (listp sexp)
216 (memq (car sexp) '(lambda)))
217 (stringp sexp)
218 (numberp sexp)
219 (and (fboundp 'characterp)
220 (characterp sexp)))
221 sexp
222 (list 'quote sexp)))
223
224 (defun custom-split-regexp-maybe (regexp)
225 "If REGEXP is a string, split it to a list at `\\|'.
226 You can get the original back with from the result with:
227 (mapconcat 'identity result \"\\|\")
228
229 IF REGEXP is not a string, return it unchanged."
230 (if (stringp regexp)
231 (let ((start 0)
232 all)
233 (while (string-match "\\\\|" regexp start)
234 (setq all (cons (substring regexp start (match-beginning 0)) all)
235 start (match-end 0)))
236 (nreverse (cons (substring regexp start) all)))
237 regexp))
238
239 (defvar custom-prefix-list nil
240 "List of prefixes that should be ignored by `custom-unlispify'")
241
242 (defcustom custom-unlispify-menu-entries t
243 "Display menu entries as words instead of symbols if non nil."
244 :group 'customize
245 :type 'boolean)
246
247 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
248 "Convert symbol into a menu entry."
249 (cond ((not custom-unlispify-menu-entries)
250 (symbol-name symbol))
251 ((get symbol 'custom-tag)
252 (if no-suffix
253 (get symbol 'custom-tag)
254 (concat (get symbol 'custom-tag) "...")))
255 (t
256 (save-excursion
257 (set-buffer (get-buffer-create " *Custom-Work*"))
258 (erase-buffer)
259 (princ symbol (current-buffer))
260 (goto-char (point-min))
261 (let ((prefixes custom-prefix-list)
262 prefix)
263 (while prefixes
264 (setq prefix (car prefixes))
265 (if (search-forward prefix (+ (point) (length prefix)) t)
266 (progn
267 (setq prefixes nil)
268 (delete-region (point-min) (point)))
269 (setq prefixes (cdr prefixes)))))
270 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
271 (capitalize-region (point-min) (point-max))
272 (unless no-suffix
273 (goto-char (point-max))
274 (insert "..."))
275 (buffer-string)))))
276
277 (defcustom custom-unlispify-tag-names t
278 "Display tag names as words instead of symbols if non nil."
279 :group 'customize
280 :type 'boolean)
281
282 (defun custom-unlispify-tag-name (symbol)
283 "Convert symbol into a menu entry."
284 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
285 (custom-unlispify-menu-entry symbol t)))
286
287 (defun custom-prefix-add (symbol prefixes)
288 ;; Addd SYMBOL to list of ignored PREFIXES.
289 (cons (or (get symbol 'custom-prefix)
290 (concat (symbol-name symbol) "-"))
291 prefixes))
292
293 ;;; The Custom Mode.
294
295 (defvar custom-options nil
296 "Customization widgets in the current buffer.")
297
298 (defvar custom-mode-map nil
299 "Keymap for `custom-mode'.")
300
301 (unless custom-mode-map
302 (setq custom-mode-map (make-sparse-keymap))
303 (set-keymap-parent custom-mode-map widget-keymap)
304 (define-key custom-mode-map "q" 'bury-buffer))
305
306 (easy-menu-define custom-mode-menu
307 custom-mode-map
308 "Menu used in customization buffers."
309 '("Custom"
310 ["Set" custom-set t]
311 ["Save" custom-save t]
312 ["Reset to Current" custom-reset-current t]
313 ["Reset to Saved" custom-reset-saved t]
314 ["Reset to Factory Settings" custom-reset-factory t]
315 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
316
317 (defcustom custom-mode-hook nil
318 "Hook called when entering custom-mode."
319 :type 'hook
320 :group 'customize)
321
322 (defun custom-mode ()
323 "Major mode for editing customization buffers.
324
325 The following commands are available:
326
327 \\[widget-forward] Move to next button or editable field.
328 \\[widget-backward] Move to previous button or editable field.
329 \\[widget-button-click] Activate button under the mouse pointer.
330 \\[widget-button-press] Activate button under point.
331 \\[custom-set] Set all modifications.
332 \\[custom-save] Make all modifications default.
333 \\[custom-reset-current] Reset all modified options.
334 \\[custom-reset-saved] Reset all modified or set options.
335 \\[custom-reset-factory] Reset all options.
336
337 Entry to this mode calls the value of `custom-mode-hook'
338 if that value is non-nil."
339 (kill-all-local-variables)
340 (setq major-mode 'custom-mode
341 mode-name "Custom")
342 (use-local-map custom-mode-map)
343 (easy-menu-add custom-mode-menu)
344 (make-local-variable 'custom-options)
345 (run-hooks 'custom-mode-hook))
346
347 ;;; Custom Mode Commands.
348
349 (defun custom-set ()
350 "Set changes in all modified options."
351 (interactive)
352 (let ((children custom-options))
353 (mapcar (lambda (child)
354 (when (eq (widget-get child :custom-state) 'modified)
355 (widget-apply child :custom-set)))
356 children)))
357
358 (defun custom-save ()
359 "Set all modified group members and save them."
360 (interactive)
361 (let ((children custom-options))
362 (mapcar (lambda (child)
363 (when (memq (widget-get child :custom-state) '(modified set))
364 (widget-apply child :custom-save)))
365 children))
366 (custom-save-all))
367
368 (defvar custom-reset-menu
369 '(("Current" . custom-reset-current)
370 ("Saved" . custom-reset-saved)
371 ("Factory Settings" . custom-reset-factory))
372 "Alist of actions for the `Reset' button.
373 The key is a string containing the name of the action, the value is a
374 lisp function taking the widget as an element which will be called
375 when the action is chosen.")
376
377 (defun custom-reset (event)
378 "Select item from reset menu."
379 (let* ((completion-ignore-case t)
380 (answer (widget-choose "Reset to"
381 custom-reset-menu
382 event)))
383 (if answer
384 (funcall answer))))
385
386 (defun custom-reset-current ()
387 "Reset all modified group members to their current value."
388 (interactive)
389 (let ((children custom-options))
390 (mapcar (lambda (child)
391 (when (eq (widget-get child :custom-state) 'modified)
392 (widget-apply child :custom-reset-current)))
393 children)))
394
395 (defun custom-reset-saved ()
396 "Reset all modified or set group members to their saved value."
397 (interactive)
398 (let ((children custom-options))
399 (mapcar (lambda (child)
400 (when (eq (widget-get child :custom-state) 'modified)
401 (widget-apply child :custom-reset-current)))
402 children)))
403
404 (defun custom-reset-factory ()
405 "Reset all modified, set, or saved group members to their factory settings."
406 (interactive)
407 (let ((children custom-options))
408 (mapcar (lambda (child)
409 (when (eq (widget-get child :custom-state) 'modified)
410 (widget-apply child :custom-reset-current)))
411 children)))
412
413 ;;; The Customize Commands
414
415 ;;;###autoload
416 (defun customize (symbol)
417 "Customize SYMBOL, which must be a customization group."
418 (interactive (list (completing-read "Customize group: (default emacs) "
419 obarray
420 (lambda (symbol)
421 (get symbol 'custom-group))
422 t)))
423
424 (when (stringp symbol)
425 (if (string-equal "" symbol)
426 (setq symbol 'emacs)
427 (setq symbol (intern symbol))))
428 (custom-buffer-create (list (list symbol 'custom-group))))
429
430 ;;;###autoload
431 (defun customize-variable (symbol)
432 "Customize SYMBOL, which must be a variable."
433 (interactive
434 ;; Code stolen from `help.el'.
435 (let ((v (variable-at-point))
436 (enable-recursive-minibuffers t)
437 val)
438 (setq val (completing-read
439 (if v
440 (format "Customize variable (default %s): " v)
441 "Customize variable: ")
442 obarray 'boundp t))
443 (list (if (equal val "")
444 v (intern val)))))
445 (custom-buffer-create (list (list symbol 'custom-variable))))
446
447 ;;;###autoload
448 (defun customize-face (&optional symbol)
449 "Customize SYMBOL, which should be a face name or nil.
450 If SYMBOL is nil, customize all faces."
451 (interactive (list (completing-read "Customize face: (default all) "
452 obarray 'custom-facep)))
453 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
454 (let ((found nil))
455 (message "Looking for faces...")
456 (mapcar (lambda (symbol)
457 (setq found (cons (list symbol 'custom-face) found)))
458 (face-list))
459 (custom-buffer-create found))
460 (if (stringp symbol)
461 (setq symbol (intern symbol)))
462 (unless (symbolp symbol)
463 (error "Should be a symbol %S" symbol))
464 (custom-buffer-create (list (list symbol 'custom-face)))))
465
466 ;;;###autoload
467 (defun customize-customized ()
468 "Customize all already customized user options."
469 (interactive)
470 (let ((found nil))
471 (mapatoms (lambda (symbol)
472 (and (get symbol 'saved-face)
473 (custom-facep symbol)
474 (setq found (cons (list symbol 'custom-face) found)))
475 (and (get symbol 'saved-value)
476 (boundp symbol)
477 (setq found
478 (cons (list symbol 'custom-variable) found)))))
479 (if found
480 (custom-buffer-create found)
481 (error "No customized user options"))))
482
483 ;;;###autoload
484 (defun customize-apropos (regexp &optional all)
485 "Customize all user options matching REGEXP.
486 If ALL (e.g., started with a prefix key), include options which are not
487 user-settable."
488 (interactive "sCustomize regexp: \nP")
489 (let ((found nil))
490 (mapatoms (lambda (symbol)
491 (when (string-match regexp (symbol-name symbol))
492 (when (get symbol 'custom-group)
493 (setq found (cons (list symbol 'custom-group) found)))
494 (when (custom-facep symbol)
495 (setq found (cons (list symbol 'custom-face) found)))
496 (when (and (boundp symbol)
497 (or (get symbol 'saved-value)
498 (get symbol 'factory-value)
499 (if all
500 (get symbol 'variable-documentation)
501 (user-variable-p symbol))))
502 (setq found
503 (cons (list symbol 'custom-variable) found))))))
504 (if found
505 (custom-buffer-create found)
506 (error "No matches"))))
507
508 ;;;###autoload
509 (defun custom-buffer-create (options)
510 "Create a buffer containing OPTIONS.
511 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
512 SYMBOL is a customization option, and WIDGET is a widget for editing
513 that option."
514 (message "Creating customization buffer...")
515 (kill-buffer (get-buffer-create "*Customization*"))
516 (switch-to-buffer (get-buffer-create "*Customization*"))
517 (custom-mode)
518 (widget-insert "This is a customization buffer.
519 Push RET or click mouse-2 on the word ")
520 ;; (put-text-property 1 2 'start-open nil)
521 (widget-create 'info-link
522 :tag "help"
523 :help-echo "Read the online help."
524 "(custom)The Customization Buffer")
525 (widget-insert " for more information.\n\n")
526 (setq custom-options
527 (if (= (length options) 1)
528 (mapcar (lambda (entry)
529 (widget-create (nth 1 entry)
530 :custom-state 'unknown
531 :tag (custom-unlispify-tag-name
532 (nth 0 entry))
533 :value (nth 0 entry)))
534 options)
535 (let ((count 0)
536 (length (length options)))
537 (mapcar (lambda (entry)
538 (prog2
539 (message "Creating customization items %2d%%..."
540 (/ (* 100.0 count) length))
541 (widget-create (nth 1 entry)
542 :tag (custom-unlispify-tag-name
543 (nth 0 entry))
544 :value (nth 0 entry))
545 (setq count (1+ count))
546 (unless (eq (preceding-char) ?\n)
547 (widget-insert "\n"))
548 (widget-insert "\n")))
549 options))))
550 (unless (eq (preceding-char) ?\n)
551 (widget-insert "\n"))
552 (widget-insert "\n")
553 (message "Creating customization magic...")
554 (mapcar 'custom-magic-reset custom-options)
555 (message "Creating customization buttons...")
556 (widget-create 'push-button
557 :tag "Set"
558 :help-echo "Set all modifications for this session."
559 :action (lambda (widget &optional event)
560 (custom-set)))
561 (widget-insert " ")
562 (widget-create 'push-button
563 :tag "Save"
564 :help-echo "\
565 Make the modifications default for future sessions."
566 :action (lambda (widget &optional event)
567 (custom-save)))
568 (widget-insert " ")
569 (widget-create 'push-button
570 :tag "Reset"
571 :help-echo "Undo all modifications."
572 :action (lambda (widget &optional event)
573 (custom-reset event)))
574 (widget-insert " ")
575 (widget-create 'push-button
576 :tag "Done"
577 :help-echo "Bury the buffer."
578 :action (lambda (widget &optional event)
579 (bury-buffer)
580 ;; Steal button release event.
581 (if (and (fboundp 'button-press-event-p)
582 (fboundp 'next-command-event))
583 ;; XEmacs
584 (and event
585 (button-press-event-p event)
586 (next-command-event))
587 ;; Emacs
588 (when (memq 'down (event-modifiers event))
589 (read-event)))))
590 (widget-insert "\n")
591 (message "Creating customization setup...")
592 (widget-setup)
593 (goto-char (point-min))
594 (message "Creating customization buffer...done"))
595
596 ;;; Modification of Basic Widgets.
597 ;;
598 ;; We add extra properties to the basic widgets needed here. This is
599 ;; fine, as long as we are careful to stay within out own namespace.
600 ;;
601 ;; We want simple widgets to be displayed by default, but complex
602 ;; widgets to be hidden.
603
604 (widget-put (get 'item 'widget-type) :custom-show t)
605 (widget-put (get 'editable-field 'widget-type)
606 :custom-show (lambda (widget value)
607 (let ((pp (pp-to-string value)))
608 (cond ((string-match "\n" pp)
609 nil)
610 ((> (length pp) 40)
611 nil)
612 (t t)))))
613 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
614
615 ;;; The `custom-manual' Widget.
616
617 (define-widget 'custom-manual 'info-link
618 "Link to the manual entry for this customization option."
619 :help-echo "Read the manual entry for this option."
620 :tag "Manual")
621
622 ;;; The `custom-magic' Widget.
623
624 (defface custom-invalid-face '((((class color))
625 (:foreground "yellow" :background "red"))
626 (t
627 (:bold t :italic t :underline t)))
628 "Face used when the customize item is invalid.")
629
630 (defface custom-rogue-face '((((class color))
631 (:foreground "pink" :background "black"))
632 (t
633 (:underline t)))
634 "Face used when the customize item is not defined for customization.")
635
636 (defface custom-modified-face '((((class color))
637 (:foreground "white" :background "blue"))
638 (t
639 (:italic t :bold)))
640 "Face used when the customize item has been modified.")
641
642 (defface custom-set-face '((((class color))
643 (:foreground "blue" :background "white"))
644 (t
645 (:italic t)))
646 "Face used when the customize item has been set.")
647
648 (defface custom-changed-face '((((class color))
649 (:foreground "white" :background "blue"))
650 (t
651 (:italic t)))
652 "Face used when the customize item has been changed.")
653
654 (defface custom-saved-face '((t (:underline t)))
655 "Face used when the customize item has been saved.")
656
657 (defcustom custom-magic-alist '((nil "#" underline "\
658 uninitialized, you should not see this.")
659 (unknown "?" italic "\
660 unknown, you should not see this.")
661 (hidden "-" default "\
662 hidden, press the state button to show.")
663 (invalid "x" custom-invalid-face "\
664 the value displayed for this item is invalid and cannot be set.")
665 (modified "*" custom-modified-face "\
666 you have edited the item, and can now set it.")
667 (set "+" custom-set-face "\
668 you have set this item, but not saved it.")
669 (changed ":" custom-changed-face "\
670 this item has been changed outside customize.")
671 (saved "!" custom-saved-face "\
672 this item has been saved.")
673 (rogue "@" custom-rogue-face "\
674 this item is not prepared for customization.")
675 (factory " " nil "\
676 this item is unchanged from its factory setting."))
677 "Alist of customize option states.
678 Each entry is of the form (STATE MAGIC FACE DESCRIPTION), where
679
680 STATE is one of the following symbols:
681
682 `nil'
683 For internal use, should never occur.
684 `unknown'
685 For internal use, should never occur.
686 `hidden'
687 This item is not being displayed.
688 `invalid'
689 This item is modified, but has an invalid form.
690 `modified'
691 This item is modified, and has a valid form.
692 `set'
693 This item has been set but not saved.
694 `changed'
695 The current value of this item has been changed temporarily.
696 `saved'
697 This item is marked for saving.
698 `rogue'
699 This item has no customization information.
700 `factory'
701 This item is unchanged from the factory default.
702
703 MAGIC is a string used to present that state.
704
705 FACE is a face used to present the state.
706
707 DESCRIPTION is a string describing the state.
708
709 The list should be sorted most significant first."
710 :type '(list (checklist :inline t
711 (group (const nil)
712 (string :tag "Magic")
713 face
714 (string :tag "Description"))
715 (group (const unknown)
716 (string :tag "Magic")
717 face
718 (string :tag "Description"))
719 (group (const hidden)
720 (string :tag "Magic")
721 face
722 (string :tag "Description"))
723 (group (const invalid)
724 (string :tag "Magic")
725 face
726 (string :tag "Description"))
727 (group (const modified)
728 (string :tag "Magic")
729 face
730 (string :tag "Description"))
731 (group (const set)
732 (string :tag "Magic")
733 face
734 (string :tag "Description"))
735 (group (const changed)
736 (string :tag "Magic")
737 face
738 (string :tag "Description"))
739 (group (const saved)
740 (string :tag "Magic")
741 face
742 (string :tag "Description"))
743 (group (const rogue)
744 (string :tag "Magic")
745 face
746 (string :tag "Description"))
747 (group (const factory)
748 (string :tag "Magic")
749 face
750 (string :tag "Description")))
751 (editable-list :inline t
752 (group symbol
753 (string :tag "Magic")
754 face
755 (string :tag "Description"))))
756 :group 'customize)
757
758 (defcustom custom-magic-show 'long
759 "Show long description of the state of each customization option."
760 :type '(choice (const :tag "no" nil)
761 (const short)
762 (const long))
763 :group 'customize)
764
765 (defcustom custom-magic-show-button t
766 "Show a magic button indicating the state of each customization option."
767 :type 'boolean
768 :group 'customize)
769
770 (define-widget 'custom-magic 'default
771 "Show and manipulate state for a customization option."
772 :format "%v"
773 :action 'widget-choice-item-action
774 :value-get 'ignore
775 :value-create 'custom-magic-value-create
776 :value-delete 'widget-children-value-delete)
777
778 (defun custom-magic-value-create (widget)
779 ;; Create compact status report for WIDGET.
780 (let* ((parent (widget-get widget :parent))
781 (state (widget-get parent :custom-state))
782 (entry (assq state custom-magic-alist))
783 (magic (nth 1 entry))
784 (face (nth 2 entry))
785 (text (nth 3 entry))
786 (lisp (eq (widget-get parent :custom-form) 'lisp))
787 children)
788 (when custom-magic-show
789 (push (widget-create-child-and-convert widget 'choice-item
790 :help-echo "\
791 Change the state of this item."
792 :format "%[%t%]"
793 :tag "State")
794 children)
795 (insert ": ")
796 (if (eq custom-magic-show 'long)
797 (insert text)
798 (insert (symbol-name state)))
799 (when lisp
800 (insert " (lisp)"))
801 (insert "\n"))
802 (when custom-magic-show-button
803 (when custom-magic-show
804 (let ((indent (widget-get parent :indent)))
805 (when indent
806 (insert-char ? indent))))
807 (push (widget-create-child-and-convert widget 'choice-item
808 :button-face face
809 :help-echo "Change the state."
810 :format "%[%t%]"
811 :tag (if lisp
812 (concat "(" magic ")")
813 (concat "[" magic "]")))
814 children)
815 (insert " "))
816 (widget-put widget :children children)))
817
818 (defun custom-magic-reset (widget)
819 "Redraw the :custom-magic property of WIDGET."
820 (let ((magic (widget-get widget :custom-magic)))
821 (widget-value-set magic (widget-value magic))))
822
823 ;;; The `custom-level' Widget.
824
825 (define-widget 'custom-level 'item
826 "The custom level buttons."
827 :format "%[%t%]"
828 :help-echo "Expand or collapse this item."
829 :action 'custom-level-action)
830
831 (defun custom-level-action (widget &optional event)
832 "Toggle visibility for parent to WIDGET."
833 (let* ((parent (widget-get widget :parent))
834 (state (widget-get parent :custom-state)))
835 (cond ((memq state '(invalid modified))
836 (error "There are unset changes"))
837 ((eq state 'hidden)
838 (widget-put parent :custom-state 'unknown))
839 (t
840 (widget-put parent :custom-state 'hidden)))
841 (custom-redraw parent)))
842
843 ;;; The `custom' Widget.
844
845 (define-widget 'custom 'default
846 "Customize a user option."
847 :convert-widget 'custom-convert-widget
848 :format "%l%[%t%]: %v%m%h%a"
849 :format-handler 'custom-format-handler
850 :notify 'custom-notify
851 :custom-level 1
852 :custom-state 'hidden
853 :documentation-property 'widget-subclass-responsibility
854 :value-create 'widget-subclass-responsibility
855 :value-delete 'widget-children-value-delete
856 :value-get 'widget-item-value-get
857 :validate 'widget-editable-list-validate
858 :match (lambda (widget value) (symbolp value)))
859
860 (defun custom-convert-widget (widget)
861 ;; Initialize :value and :tag from :args in WIDGET.
862 (let ((args (widget-get widget :args)))
863 (when args
864 (widget-put widget :value (widget-apply widget
865 :value-to-internal (car args)))
866 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
867 (widget-put widget :args nil)))
868 widget)
869
870 (defun custom-format-handler (widget escape)
871 ;; We recognize extra escape sequences.
872 (let* ((buttons (widget-get widget :buttons))
873 (state (widget-get widget :custom-state))
874 (level (widget-get widget :custom-level)))
875 (cond ((eq escape ?l)
876 (when level
877 (push (widget-create-child-and-convert
878 widget 'custom-level (make-string level ?*))
879 buttons)
880 (widget-insert " ")
881 (widget-put widget :buttons buttons)))
882 ((eq escape ?L)
883 (when (eq state 'hidden)
884 (widget-insert " ...")))
885 ((eq escape ?m)
886 (and (eq (preceding-char) ?\n)
887 (widget-get widget :indent)
888 (insert-char ? (widget-get widget :indent)))
889 (let ((magic (widget-create-child-and-convert
890 widget 'custom-magic nil)))
891 (widget-put widget :custom-magic magic)
892 (push magic buttons)
893 (widget-put widget :buttons buttons)))
894 ((eq escape ?a)
895 (let* ((symbol (widget-get widget :value))
896 (links (get symbol 'custom-links))
897 (many (> (length links) 2)))
898 (when links
899 (and (eq (preceding-char) ?\n)
900 (widget-get widget :indent)
901 (insert-char ? (widget-get widget :indent)))
902 (insert "See also ")
903 (while links
904 (push (widget-create-child-and-convert widget (car links))
905 buttons)
906 (setq links (cdr links))
907 (cond ((null links)
908 (insert ".\n"))
909 ((null (cdr links))
910 (if many
911 (insert ", and ")
912 (insert " and ")))
913 (t
914 (insert ", "))))
915 (widget-put widget :buttons buttons))))
916 (t
917 (widget-default-format-handler widget escape)))))
918
919 (defun custom-notify (widget &rest args)
920 "Keep track of changes."
921 (unless (memq (widget-get widget :custom-state) '(nil unknown hidden))
922 (widget-put widget :custom-state 'modified))
923 (let ((buffer-undo-list t))
924 (custom-magic-reset widget))
925 (apply 'widget-default-notify widget args))
926
927 (defun custom-redraw (widget)
928 "Redraw WIDGET with current settings."
929 (let ((pos (point))
930 (from (marker-position (widget-get widget :from)))
931 (to (marker-position (widget-get widget :to))))
932 (save-excursion
933 (widget-value-set widget (widget-value widget))
934 (custom-redraw-magic widget))
935 (when (and (>= pos from) (<= pos to))
936 (goto-char pos))))
937
938 (defun custom-redraw-magic (widget)
939 "Redraw WIDGET state with current settings."
940 (while widget
941 (let ((magic (widget-get widget :custom-magic)))
942 (unless magic
943 (debug))
944 (widget-value-set magic (widget-value magic))
945 (when (setq widget (widget-get widget :group))
946 (custom-group-state-update widget))))
947 (widget-setup))
948
949 (defun custom-show (widget value)
950 "Non-nil if WIDGET should be shown with VALUE by default."
951 (let ((show (widget-get widget :custom-show)))
952 (cond ((null show)
953 nil)
954 ((eq t show)
955 t)
956 (t
957 (funcall show widget value)))))
958
959 (defun custom-load-symbol (symbol)
960 "Load all dependencies for SYMBOL."
961 (let ((loads (get symbol 'custom-loads))
962 load)
963 (while loads
964 (setq load (car loads)
965 loads (cdr loads))
966 (cond ((symbolp load)
967 (condition-case nil
968 (require load)
969 (error nil)))
970 ((assoc load load-history))
971 (t
972 (condition-case nil
973 (load-library load)
974 (error nil)))))))
975
976 (defun custom-load-widget (widget)
977 "Load all dependencies for WIDGET."
978 (custom-load-symbol (widget-value widget)))
979
980 ;;; The `custom-variable' Widget.
981
982 (defface custom-variable-sample-face '((t (:underline t)))
983 "Face used for unpushable variable tags."
984 :group 'customize)
985
986 (defface custom-variable-button-face '((t (:underline t :bold t)))
987 "Face used for pushable variable tags."
988 :group 'customize)
989
990 (define-widget 'custom-variable 'custom
991 "Customize variable."
992 :format "%l%v%m%h%a"
993 :help-echo "Set or reset this variable."
994 :documentation-property 'variable-documentation
995 :custom-state nil
996 :custom-menu 'custom-variable-menu-create
997 :custom-form 'edit
998 :value-create 'custom-variable-value-create
999 :action 'custom-variable-action
1000 :custom-set 'custom-variable-set
1001 :custom-save 'custom-variable-save
1002 :custom-reset-current 'custom-redraw
1003 :custom-reset-saved 'custom-variable-reset-saved
1004 :custom-reset-factory 'custom-variable-reset-factory)
1005
1006 (defun custom-variable-value-create (widget)
1007 "Here is where you edit the variables value."
1008 (custom-load-widget widget)
1009 (let* ((buttons (widget-get widget :buttons))
1010 (children (widget-get widget :children))
1011 (form (widget-get widget :custom-form))
1012 (state (widget-get widget :custom-state))
1013 (symbol (widget-get widget :value))
1014 (options (get symbol 'custom-options))
1015 (child-type (or (get symbol 'custom-type) 'sexp))
1016 (tag (widget-get widget :tag))
1017 (type (let ((tmp (if (listp child-type)
1018 (copy-list child-type)
1019 (list child-type))))
1020 (when options
1021 (widget-put tmp :options options))
1022 tmp))
1023 (conv (widget-convert type))
1024 (value (if (default-boundp symbol)
1025 (default-value symbol)
1026 (widget-get conv :value))))
1027 ;; If the widget is new, the child determine whether it is hidden.
1028 (cond (state)
1029 ((custom-show type value)
1030 (setq state 'unknown))
1031 (t
1032 (setq state 'hidden)))
1033 ;; If we don't know the state, see if we need to edit it in lisp form.
1034 (when (eq state 'unknown)
1035 (unless (widget-apply conv :match value)
1036 ;; (widget-apply (widget-convert type) :match value)
1037 (setq form 'lisp)))
1038 ;; Now we can create the child widget.
1039 (cond ((eq state 'hidden)
1040 ;; Indicate hidden value.
1041 (push (widget-create-child-and-convert
1042 widget 'item
1043 :format "%{%t%}: ..."
1044 :sample-face 'custom-variable-sample-face
1045 :tag tag
1046 :parent widget)
1047 children))
1048 ((eq form 'lisp)
1049 ;; In lisp mode edit the saved value when possible.
1050 (let* ((value (cond ((get symbol 'saved-value)
1051 (car (get symbol 'saved-value)))
1052 ((get symbol 'factory-value)
1053 (car (get symbol 'factory-value)))
1054 ((default-boundp symbol)
1055 (custom-quote (default-value symbol)))
1056 (t
1057 (custom-quote (widget-get conv :value))))))
1058 (push (widget-create-child-and-convert
1059 widget 'sexp
1060 :button-face 'custom-variable-button-face
1061 :tag (symbol-name symbol)
1062 :parent widget
1063 :value value)
1064 children)))
1065 (t
1066 ;; Edit mode.
1067 (push (widget-create-child-and-convert
1068 widget type
1069 :tag tag
1070 :button-face 'custom-variable-button-face
1071 :sample-face 'custom-variable-sample-face
1072 :value value)
1073 children)))
1074 ;; Now update the state.
1075 (unless (eq (preceding-char) ?\n)
1076 (widget-insert "\n"))
1077 (if (eq state 'hidden)
1078 (widget-put widget :custom-state state)
1079 (custom-variable-state-set widget))
1080 (widget-put widget :custom-form form)
1081 (widget-put widget :buttons buttons)
1082 (widget-put widget :children children)))
1083
1084 (defun custom-variable-state-set (widget)
1085 "Set the state of WIDGET."
1086 (let* ((symbol (widget-value widget))
1087 (value (if (default-boundp symbol)
1088 (default-value symbol)
1089 (widget-get widget :value)))
1090 tmp
1091 (state (cond ((setq tmp (get symbol 'customized-value))
1092 (if (condition-case nil
1093 (equal value (eval (car tmp)))
1094 (error nil))
1095 'set
1096 'changed))
1097 ((setq tmp (get symbol 'saved-value))
1098 (if (condition-case nil
1099 (equal value (eval (car tmp)))
1100 (error nil))
1101 'saved
1102 'changed))
1103 ((setq tmp (get symbol 'factory-value))
1104 (if (condition-case nil
1105 (equal value (eval (car tmp)))
1106 (error nil))
1107 'factory
1108 'changed))
1109 (t 'rogue))))
1110 (widget-put widget :custom-state state)))
1111
1112 (defvar custom-variable-menu
1113 '(("Edit" . custom-variable-edit)
1114 ("Edit Lisp" . custom-variable-edit-lisp)
1115 ("Set" . custom-variable-set)
1116 ("Save" . custom-variable-save)
1117 ("Reset to Current" . custom-redraw)
1118 ("Reset to Saved" . custom-variable-reset-saved)
1119 ("Reset to Factory Settings" . custom-variable-reset-factory))
1120 "Alist of actions for the `custom-variable' widget.
1121 The key is a string containing the name of the action, the value is a
1122 lisp function taking the widget as an element which will be called
1123 when the action is chosen.")
1124
1125 (defun custom-variable-action (widget &optional event)
1126 "Show the menu for `custom-variable' WIDGET.
1127 Optional EVENT is the location for the menu."
1128 (if (eq (widget-get widget :custom-state) 'hidden)
1129 (progn
1130 (widget-put widget :custom-state 'unknown)
1131 (custom-redraw widget))
1132 (let* ((completion-ignore-case t)
1133 (answer (widget-choose (custom-unlispify-tag-name
1134 (widget-get widget :value))
1135 custom-variable-menu
1136 event)))
1137 (if answer
1138 (funcall answer widget)))))
1139
1140 (defun custom-variable-edit (widget)
1141 "Edit value of WIDGET."
1142 (widget-put widget :custom-state 'unknown)
1143 (widget-put widget :custom-form 'edit)
1144 (custom-redraw widget))
1145
1146 (defun custom-variable-edit-lisp (widget)
1147 "Edit the lisp representation of the value of WIDGET."
1148 (widget-put widget :custom-state 'unknown)
1149 (widget-put widget :custom-form 'lisp)
1150 (custom-redraw widget))
1151
1152 (defun custom-variable-set (widget)
1153 "Set the current value for the variable being edited by WIDGET."
1154 (let ((form (widget-get widget :custom-form))
1155 (state (widget-get widget :custom-state))
1156 (child (car (widget-get widget :children)))
1157 (symbol (widget-value widget))
1158 val)
1159 (cond ((eq state 'hidden)
1160 (error "Cannot set hidden variable."))
1161 ((setq val (widget-apply child :validate))
1162 (goto-char (widget-get val :from))
1163 (error "%s" (widget-get val :error)))
1164 ((eq form 'lisp)
1165 (set symbol (eval (setq val (widget-value child))))
1166 (put symbol 'customized-value (list val)))
1167 (t
1168 (set symbol (setq val (widget-value child)))
1169 (put symbol 'customized-value (list (custom-quote val)))))
1170 (custom-variable-state-set widget)
1171 (custom-redraw-magic widget)))
1172
1173 (defun custom-variable-save (widget)
1174 "Set the default value for the variable being edited by WIDGET."
1175 (let ((form (widget-get widget :custom-form))
1176 (state (widget-get widget :custom-state))
1177 (child (car (widget-get widget :children)))
1178 (symbol (widget-value widget))
1179 val)
1180 (cond ((eq state 'hidden)
1181 (error "Cannot set hidden variable."))
1182 ((setq val (widget-apply child :validate))
1183 (goto-char (widget-get val :from))
1184 (error "%s" (widget-get val :error)))
1185 ((eq form 'lisp)
1186 (put symbol 'saved-value (list (widget-value child)))
1187 (set symbol (eval (widget-value child))))
1188 (t
1189 (put symbol
1190 'saved-value (list (custom-quote (widget-value
1191 child))))
1192 (set symbol (widget-value child))))
1193 (put symbol 'customized-value nil)
1194 (custom-save-all)
1195 (custom-variable-state-set widget)
1196 (custom-redraw-magic widget)))
1197
1198 (defun custom-variable-reset-saved (widget)
1199 "Restore the saved value for the variable being edited by WIDGET."
1200 (let ((symbol (widget-value widget)))
1201 (if (get symbol 'saved-value)
1202 (condition-case nil
1203 (set symbol (eval (car (get symbol 'saved-value))))
1204 (error nil))
1205 (error "No saved value for %s" symbol))
1206 (put symbol 'customized-value nil)
1207 (widget-put widget :custom-state 'unknown)
1208 (custom-redraw widget)))
1209
1210 (defun custom-variable-reset-factory (widget)
1211 "Restore the factory setting for the variable being edited by WIDGET."
1212 (let ((symbol (widget-value widget)))
1213 (if (get symbol 'factory-value)
1214 (set symbol (eval (car (get symbol 'factory-value))))
1215 (error "No factory default for %S" symbol))
1216 (put symbol 'customized-value nil)
1217 (when (get symbol 'saved-value)
1218 (put symbol 'saved-value nil)
1219 (custom-save-all))
1220 (widget-put widget :custom-state 'unknown)
1221 (custom-redraw widget)))
1222
1223 ;;; The `custom-face-edit' Widget.
1224
1225 (define-widget 'custom-face-edit 'checklist
1226 "Edit face attributes."
1227 :format "%t: %v"
1228 :tag "Attributes"
1229 :extra-offset 12
1230 :button-args '(:help-echo "Control whether this attribute have any effect.")
1231 :args (mapcar (lambda (att)
1232 (list 'group
1233 :inline t
1234 :sibling-args (widget-get (nth 1 att) :sibling-args)
1235 (list 'const :format "" :value (nth 0 att))
1236 (nth 1 att)))
1237 custom-face-attributes))
1238
1239 ;;; The `custom-display' Widget.
1240
1241 (define-widget 'custom-display 'menu-choice
1242 "Select a display type."
1243 :tag "Display"
1244 :value t
1245 :help-echo "Specify frames where the face attributes should be used."
1246 :args '((const :tag "all" t)
1247 (checklist
1248 :offset 0
1249 :extra-offset 9
1250 :args ((group :sibling-args (:help-echo "\
1251 Only match the specified window systems.")
1252 (const :format "Type: "
1253 type)
1254 (checklist :inline t
1255 :offset 0
1256 (const :format "X "
1257 :sibling-args (:help-echo "\
1258 The X11 Window System.")
1259 x)
1260 (const :format "PM "
1261 :sibling-args (:help-echo "\
1262 OS/2 Presentation Manager.")
1263 pm)
1264 (const :format "Win32 "
1265 :sibling-args (:help-echo "\
1266 Windows NT/95/97.")
1267 win32)
1268 (const :format "DOS "
1269 :sibling-args (:help-echo "\
1270 Plain MS-DOS.")
1271 pc)
1272 (const :format "TTY%n"
1273 :sibling-args (:help-echo "\
1274 Plain text terminals.")
1275 tty)))
1276 (group :sibling-args (:help-echo "\
1277 Only match the frames with the specified color support.")
1278 (const :format "Class: "
1279 class)
1280 (checklist :inline t
1281 :offset 0
1282 (const :format "Color "
1283 :sibling-args (:help-echo "\
1284 Match color frames.")
1285 color)
1286 (const :format "Grayscale "
1287 :sibling-args (:help-echo "\
1288 Match grayscale frames.")
1289 grayscale)
1290 (const :format "Monochrome%n"
1291 :sibling-args (:help-echo "\
1292 Match frames with no color support.")
1293 mono)))
1294 (group :sibling-args (:help-echo "\
1295 Only match frames with the specified intensity.")
1296 (const :format "\
1297 Background brightness: "
1298 background)
1299 (checklist :inline t
1300 :offset 0
1301 (const :format "Light "
1302 :sibling-args (:help-echo "\
1303 Match frames with light backgrounds.")
1304 light)
1305 (const :format "Dark\n"
1306 :sibling-args (:help-echo "\
1307 Match frames with dark backgrounds.")
1308 dark)))))))
1309
1310 ;;; The `custom-face' Widget.
1311
1312 (defface custom-face-tag-face '((t (:underline t)))
1313 "Face used for face tags."
1314 :group 'customize)
1315
1316 (define-widget 'custom-face 'custom
1317 "Customize face."
1318 :format "%l%{%t%}: %s%m%h%a%v"
1319 :format-handler 'custom-face-format-handler
1320 :sample-face 'custom-face-tag-face
1321 :help-echo "Set or reset this face."
1322 :documentation-property '(lambda (face)
1323 (face-doc-string face))
1324 :value-create 'custom-face-value-create
1325 :action 'custom-face-action
1326 :custom-form 'selected
1327 :custom-set 'custom-face-set
1328 :custom-save 'custom-face-save
1329 :custom-reset-current 'custom-redraw
1330 :custom-reset-saved 'custom-face-reset-saved
1331 :custom-reset-factory 'custom-face-reset-factory
1332 :custom-menu 'custom-face-menu-create)
1333
1334 (defun custom-face-format-handler (widget escape)
1335 ;; We recognize extra escape sequences.
1336 (let (child
1337 (symbol (widget-get widget :value)))
1338 (cond ((eq escape ?s)
1339 (and (string-match "XEmacs" emacs-version)
1340 ;; XEmacs cannot display initialized faces.
1341 (not (custom-facep symbol))
1342 (copy-face 'custom-face-empty symbol))
1343 (setq child (widget-create-child-and-convert
1344 widget 'item
1345 :format "(%{%t%})\n"
1346 :sample-face symbol
1347 :tag "sample")))
1348 (t
1349 (custom-format-handler widget escape)))
1350 (when child
1351 (widget-put widget
1352 :buttons (cons child (widget-get widget :buttons))))))
1353
1354 (define-widget 'custom-face-all 'editable-list
1355 "An editable list of display specifications and attributes."
1356 :entry-format "%i %d %v"
1357 :insert-button-args '(:help-echo "Insert new display specification here.")
1358 :append-button-args '(:help-echo "Append new display specification here.")
1359 :delete-button-args '(:help-echo "Delete this display specification.")
1360 :args '((group :format "%v" custom-display custom-face-edit)))
1361
1362 (defconst custom-face-all (widget-convert 'custom-face-all)
1363 "Converted version of the `custom-face-all' widget.")
1364
1365 (define-widget 'custom-display-unselected 'item
1366 "A display specification that doesn't match the selected display."
1367 :match 'custom-display-unselected-match)
1368
1369 (defun custom-display-unselected-match (widget value)
1370 "Non-nil if VALUE is an unselected display specification."
1371 (and (listp value)
1372 (eq (length value) 2)
1373 (not (custom-display-match-frame value (selected-frame)))))
1374
1375 (define-widget 'custom-face-selected 'group
1376 "Edit the attributes of the selected display in a face specification."
1377 :args '((repeat :format ""
1378 :inline t
1379 (group custom-display-unselected sexp))
1380 (group (sexp :format "") custom-face-edit)
1381 (repeat :format ""
1382 :inline t
1383 sexp)))
1384
1385 (defconst custom-face-selected (widget-convert 'custom-face-selected)
1386 "Converted version of the `custom-face-selected' widget.")
1387
1388 (defun custom-face-value-create (widget)
1389 ;; Create a list of the display specifications.
1390 (unless (eq (preceding-char) ?\n)
1391 (insert "\n"))
1392 (when (not (eq (widget-get widget :custom-state) 'hidden))
1393 (message "Creating face editor...")
1394 (custom-load-widget widget)
1395 (let* ((symbol (widget-value widget))
1396 (spec (or (get symbol 'saved-face)
1397 (get symbol 'factory-face)
1398 ;; Attempt to construct it.
1399 (list (list t (custom-face-attributes-get
1400 symbol (selected-frame))))))
1401 (form (widget-get widget :custom-form))
1402 (indent (widget-get widget :indent))
1403 (edit (widget-create-child-and-convert
1404 widget
1405 (cond ((and (eq form 'selected)
1406 (widget-apply custom-face-selected :match spec))
1407 (when indent (insert-char ?\ indent))
1408 'custom-face-selected)
1409 ((and (not (eq form 'lisp))
1410 (widget-apply custom-face-all :match spec))
1411 'custom-face-all)
1412 (t
1413 (when indent (insert-char ?\ indent))
1414 'sexp))
1415 :value spec)))
1416 (custom-face-state-set widget)
1417 (widget-put widget :children (list edit)))
1418 (message "Creating face editor...done")))
1419
1420 (defvar custom-face-menu
1421 '(("Edit Selected" . custom-face-edit-selected)
1422 ("Edit All" . custom-face-edit-all)
1423 ("Edit Lisp" . custom-face-edit-lisp)
1424 ("Set" . custom-face-set)
1425 ("Save" . custom-face-save)
1426 ("Reset to Saved" . custom-face-reset-saved)
1427 ("Reset to Factory Setting" . custom-face-reset-factory))
1428 "Alist of actions for the `custom-face' widget.
1429 The key is a string containing the name of the action, the value is a
1430 lisp function taking the widget as an element which will be called
1431 when the action is chosen.")
1432
1433 (defun custom-face-edit-selected (widget)
1434 "Edit selected attributes of the value of WIDGET."
1435 (widget-put widget :custom-state 'unknown)
1436 (widget-put widget :custom-form 'selected)
1437 (custom-redraw widget))
1438
1439 (defun custom-face-edit-all (widget)
1440 "Edit all attributes of the value of WIDGET."
1441 (widget-put widget :custom-state 'unknown)
1442 (widget-put widget :custom-form 'all)
1443 (custom-redraw widget))
1444
1445 (defun custom-face-edit-lisp (widget)
1446 "Edit the lisp representation of the value of WIDGET."
1447 (widget-put widget :custom-state 'unknown)
1448 (widget-put widget :custom-form 'lisp)
1449 (custom-redraw widget))
1450
1451 (defun custom-face-state-set (widget)
1452 "Set the state of WIDGET."
1453 (let ((symbol (widget-value widget)))
1454 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
1455 'set)
1456 ((get symbol 'saved-face)
1457 'saved)
1458 ((get symbol 'factory-face)
1459 'factory)
1460 (t
1461 'rogue)))))
1462
1463 (defun custom-face-action (widget &optional event)
1464 "Show the menu for `custom-face' WIDGET.
1465 Optional EVENT is the location for the menu."
1466 (if (eq (widget-get widget :custom-state) 'hidden)
1467 (progn
1468 (widget-put widget :custom-state 'unknown)
1469 (custom-redraw widget))
1470 (let* ((completion-ignore-case t)
1471 (symbol (widget-get widget :value))
1472 (answer (widget-choose (custom-unlispify-tag-name symbol)
1473 custom-face-menu event)))
1474 (if answer
1475 (funcall answer widget)))))
1476
1477 (defun custom-face-set (widget)
1478 "Make the face attributes in WIDGET take effect."
1479 (let* ((symbol (widget-value widget))
1480 (child (car (widget-get widget :children)))
1481 (value (widget-value child)))
1482 (put symbol 'customized-face value)
1483 (when (fboundp 'copy-face)
1484 (copy-face 'custom-face-empty symbol))
1485 (custom-face-display-set symbol value)
1486 (custom-face-state-set widget)
1487 (custom-redraw-magic widget)))
1488
1489 (defun custom-face-save (widget)
1490 "Make the face attributes in WIDGET default."
1491 (let* ((symbol (widget-value widget))
1492 (child (car (widget-get widget :children)))
1493 (value (widget-value child)))
1494 (when (fboundp 'copy-face)
1495 (copy-face 'custom-face-empty symbol))
1496 (custom-face-display-set symbol value)
1497 (put symbol 'saved-face value)
1498 (put symbol 'customized-face nil)
1499 (custom-face-state-set widget)
1500 (custom-redraw-magic widget)))
1501
1502 (defun custom-face-reset-saved (widget)
1503 "Restore WIDGET to the face's default attributes."
1504 (let* ((symbol (widget-value widget))
1505 (child (car (widget-get widget :children)))
1506 (value (get symbol 'saved-face)))
1507 (unless value
1508 (error "No saved value for this face"))
1509 (put symbol 'customized-face nil)
1510 (when (fboundp 'copy-face)
1511 (copy-face 'custom-face-empty symbol))
1512 (custom-face-display-set symbol value)
1513 (widget-value-set child value)
1514 (custom-face-state-set widget)
1515 (custom-redraw-magic widget)))
1516
1517 (defun custom-face-reset-factory (widget)
1518 "Restore WIDGET to the face's factory settings."
1519 (let* ((symbol (widget-value widget))
1520 (child (car (widget-get widget :children)))
1521 (value (get symbol 'factory-face)))
1522 (unless value
1523 (error "No factory default for this face"))
1524 (put symbol 'customized-face nil)
1525 (when (get symbol 'saved-face)
1526 (put symbol 'saved-face nil)
1527 (custom-save-all))
1528 (when (fboundp 'copy-face)
1529 (copy-face 'custom-face-empty symbol))
1530 (custom-face-display-set symbol value)
1531 (widget-value-set child value)
1532 (custom-face-state-set widget)
1533 (custom-redraw-magic widget)))
1534
1535 ;;; The `face' Widget.
1536
1537 (define-widget 'face 'default
1538 "Select and customize a face."
1539 :convert-widget 'widget-item-convert-widget
1540 :format "%[%t%]: %v"
1541 :tag "Face"
1542 :value 'default
1543 :value-create 'widget-face-value-create
1544 :value-delete 'widget-face-value-delete
1545 :value-get 'widget-item-value-get
1546 :validate 'widget-editable-list-validate
1547 :action 'widget-face-action
1548 :match '(lambda (widget value) (symbolp value)))
1549
1550 (defun widget-face-value-create (widget)
1551 ;; Create a `custom-face' child.
1552 (let* ((symbol (widget-value widget))
1553 (child (widget-create-child-and-convert
1554 widget 'custom-face
1555 :format "%t %s%m%h%v"
1556 :custom-level nil
1557 :value symbol)))
1558 (custom-magic-reset child)
1559 (setq custom-options (cons child custom-options))
1560 (widget-put widget :children (list child))))
1561
1562 (defun widget-face-value-delete (widget)
1563 ;; Remove the child from the options.
1564 (let ((child (car (widget-get widget :children))))
1565 (setq custom-options (delq child custom-options))
1566 (widget-children-value-delete widget)))
1567
1568 (defvar face-history nil
1569 "History of entered face names.")
1570
1571 (defun widget-face-action (widget &optional event)
1572 "Prompt for a face."
1573 (let ((answer (completing-read "Face: "
1574 (mapcar (lambda (face)
1575 (list (symbol-name face)))
1576 (face-list))
1577 nil nil nil
1578 'face-history)))
1579 (unless (zerop (length answer))
1580 (widget-value-set widget (intern answer))
1581 (widget-apply widget :notify widget event)
1582 (widget-setup))))
1583
1584 ;;; The `hook' Widget.
1585
1586 (define-widget 'hook 'list
1587 "A emacs lisp hook"
1588 :convert-widget 'custom-hook-convert-widget
1589 :tag "Hook")
1590
1591 (defun custom-hook-convert-widget (widget)
1592 ;; Handle `:custom-options'.
1593 (let* ((options (widget-get widget :options))
1594 (other `(editable-list :inline t
1595 :entry-format "%i %d%v"
1596 (function :format " %v")))
1597 (args (if options
1598 (list `(checklist :inline t
1599 ,@(mapcar (lambda (entry)
1600 `(function-item ,entry))
1601 options))
1602 other)
1603 (list other))))
1604 (widget-put widget :args args)
1605 widget))
1606
1607 ;;; The `custom-group' Widget.
1608
1609 (defcustom custom-group-tag-faces '(custom-group-tag-face-1)
1610 ;; In XEmacs, this ought to play games with font size.
1611 "Face used for group tags.
1612 The first member is used for level 1 groups, the second for level 2,
1613 and so forth. The remaining group tags are shown with
1614 `custom-group-tag-face'."
1615 :type '(repeat face)
1616 :group 'customize)
1617
1618 (defface custom-group-tag-face-1 '((((class color)
1619 (background dark))
1620 (:foreground "pink" :underline t))
1621 (((class color)
1622 (background light))
1623 (:foreground "red" :underline t))
1624 (t (:underline t)))
1625 "Face used for group tags.")
1626
1627 (defface custom-group-tag-face '((((class color)
1628 (background dark))
1629 (:foreground "light blue" :underline t))
1630 (((class color)
1631 (background light))
1632 (:foreground "blue" :underline t))
1633 (t (:underline t)))
1634 "Face used for low level group tags."
1635 :group 'customize)
1636
1637 (define-widget 'custom-group 'custom
1638 "Customize group."
1639 :format "%l%{%t%}:%L\n%m%h%a%v"
1640 :sample-face-get 'custom-group-sample-face-get
1641 :documentation-property 'group-documentation
1642 :help-echo "Set or reset all members of this group."
1643 :value-create 'custom-group-value-create
1644 :action 'custom-group-action
1645 :custom-set 'custom-group-set
1646 :custom-save 'custom-group-save
1647 :custom-reset-current 'custom-group-reset-current
1648 :custom-reset-saved 'custom-group-reset-saved
1649 :custom-reset-factory 'custom-group-reset-factory
1650 :custom-menu 'custom-group-menu-create)
1651
1652 (defun custom-group-sample-face-get (widget)
1653 ;; Use :sample-face.
1654 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
1655 'custom-group-tag-face))
1656
1657 (defun custom-group-value-create (widget)
1658 (let ((state (widget-get widget :custom-state)))
1659 (unless (eq state 'hidden)
1660 (message "Creating group...")
1661 (custom-load-widget widget)
1662 (let* ((level (widget-get widget :custom-level))
1663 (symbol (widget-value widget))
1664 (members (get symbol 'custom-group))
1665 (prefixes (widget-get widget :custom-prefixes))
1666 (custom-prefix-list (custom-prefix-add symbol prefixes))
1667 (length (length members))
1668 (count 0)
1669 (children (mapcar (lambda (entry)
1670 (widget-insert "\n")
1671 (message "Creating group members... %2d%%"
1672 (/ (* 100.0 count) length))
1673 (setq count (1+ count))
1674 (prog1
1675 (widget-create-child-and-convert
1676 widget (nth 1 entry)
1677 :group widget
1678 :tag (custom-unlispify-tag-name
1679 (nth 0 entry))
1680 :custom-prefixes custom-prefix-list
1681 :custom-level (1+ level)
1682 :value (nth 0 entry))
1683 (unless (eq (preceding-char) ?\n)
1684 (widget-insert "\n"))))
1685 members)))
1686 (message "Creating group magic...")
1687 (mapcar 'custom-magic-reset children)
1688 (message "Creating group state...")
1689 (widget-put widget :children children)
1690 (custom-group-state-update widget)
1691 (message "Creating group... done")))))
1692
1693 (defvar custom-group-menu
1694 '(("Set" . custom-group-set)
1695 ("Save" . custom-group-save)
1696 ("Reset to Current" . custom-group-reset-current)
1697 ("Reset to Saved" . custom-group-reset-saved)
1698 ("Reset to Factory" . custom-group-reset-factory))
1699 "Alist of actions for the `custom-group' widget.
1700 The key is a string containing the name of the action, the value is a
1701 lisp function taking the widget as an element which will be called
1702 when the action is chosen.")
1703
1704 (defun custom-group-action (widget &optional event)
1705 "Show the menu for `custom-group' WIDGET.
1706 Optional EVENT is the location for the menu."
1707 (if (eq (widget-get widget :custom-state) 'hidden)
1708 (progn
1709 (widget-put widget :custom-state 'unknown)
1710 (custom-redraw widget))
1711 (let* ((completion-ignore-case t)
1712 (answer (widget-choose (custom-unlispify-tag-name
1713 (widget-get widget :value))
1714 custom-group-menu
1715 event)))
1716 (if answer
1717 (funcall answer widget)))))
1718
1719 (defun custom-group-set (widget)
1720 "Set changes in all modified group members."
1721 (let ((children (widget-get widget :children)))
1722 (mapcar (lambda (child)
1723 (when (eq (widget-get child :custom-state) 'modified)
1724 (widget-apply child :custom-set)))
1725 children )))
1726
1727 (defun custom-group-save (widget)
1728 "Save all modified group members."
1729 (let ((children (widget-get widget :children)))
1730 (mapcar (lambda (child)
1731 (when (memq (widget-get child :custom-state) '(modified set))
1732 (widget-apply child :custom-save)))
1733 children )))
1734
1735 (defun custom-group-reset-current (widget)
1736 "Reset all modified group members."
1737 (let ((children (widget-get widget :children)))
1738 (mapcar (lambda (child)
1739 (when (eq (widget-get child :custom-state) 'modified)
1740 (widget-apply child :custom-reset-current)))
1741 children )))
1742
1743 (defun custom-group-reset-saved (widget)
1744 "Reset all modified or set group members."
1745 (let ((children (widget-get widget :children)))
1746 (mapcar (lambda (child)
1747 (when (memq (widget-get child :custom-state) '(modified set))
1748 (widget-apply child :custom-reset-saved)))
1749 children )))
1750
1751 (defun custom-group-reset-factory (widget)
1752 "Reset all modified, set, or saved group members."
1753 (let ((children (widget-get widget :children)))
1754 (mapcar (lambda (child)
1755 (when (memq (widget-get child :custom-state)
1756 '(modified set saved))
1757 (widget-apply child :custom-reset-factory)))
1758 children )))
1759
1760 (defun custom-group-state-update (widget)
1761 "Update magic."
1762 (unless (eq (widget-get widget :custom-state) 'hidden)
1763 (let* ((children (widget-get widget :children))
1764 (states (mapcar (lambda (child)
1765 (widget-get child :custom-state))
1766 children))
1767 (magics custom-magic-alist)
1768 (found 'factory))
1769 (while magics
1770 (let ((magic (car (car magics))))
1771 (if (and (not (eq magic 'hidden))
1772 (memq magic states))
1773 (setq found magic
1774 magics nil)
1775 (setq magics (cdr magics)))))
1776 (widget-put widget :custom-state found)))
1777 (custom-magic-reset widget))
1778
1779 ;;; The `custom-save-all' Function.
1780
1781 (defcustom custom-file "~/.emacs"
1782 "File used for storing customization information.
1783 If you change this from the default \"~/.emacs\" you need to
1784 explicitly load that file for the settings to take effect."
1785 :type 'file
1786 :group 'customize)
1787
1788 (defun custom-save-delete (symbol)
1789 "Delete the call to SYMBOL form `custom-file'.
1790 Leave point at the location of the call, or after the last expression."
1791 (set-buffer (find-file-noselect custom-file))
1792 (goto-char (point-min))
1793 (catch 'found
1794 (while t
1795 (let ((sexp (condition-case nil
1796 (read (current-buffer))
1797 (end-of-file (throw 'found nil)))))
1798 (when (and (listp sexp)
1799 (eq (car sexp) symbol))
1800 (delete-region (save-excursion
1801 (backward-sexp)
1802 (point))
1803 (point))
1804 (throw 'found nil))))))
1805
1806 (defun custom-save-variables ()
1807 "Save all customized variables in `custom-file'."
1808 (save-excursion
1809 (custom-save-delete 'custom-set-variables)
1810 (let ((standard-output (current-buffer)))
1811 (unless (bolp)
1812 (princ "\n"))
1813 (princ "(custom-set-variables")
1814 (mapatoms (lambda (symbol)
1815 (let ((value (get symbol 'saved-value)))
1816 (when value
1817 (princ "\n '(")
1818 (princ symbol)
1819 (princ " ")
1820 (prin1 (car value))
1821 (if (or (get symbol 'factory-value)
1822 (and (not (boundp symbol))
1823 (not (get symbol 'force-value))))
1824 (princ ")")
1825 (princ " t)"))))))
1826 (princ ")")
1827 (unless (looking-at "\n")
1828 (princ "\n")))))
1829
1830 (defun custom-save-faces ()
1831 "Save all customized faces in `custom-file'."
1832 (save-excursion
1833 (custom-save-delete 'custom-set-faces)
1834 (let ((standard-output (current-buffer)))
1835 (unless (bolp)
1836 (princ "\n"))
1837 (princ "(custom-set-faces")
1838 (mapatoms (lambda (symbol)
1839 (let ((value (get symbol 'saved-face)))
1840 (when value
1841 (princ "\n '(")
1842 (princ symbol)
1843 (princ " ")
1844 (prin1 value)
1845 (if (or (get symbol 'factory-face)
1846 (and (not (custom-facep symbol))
1847 (not (get symbol 'force-face))))
1848 (princ ")")
1849 (princ " t)"))))))
1850 (princ ")")
1851 (unless (looking-at "\n")
1852 (princ "\n")))))
1853
1854 ;;;###autoload
1855 (defun custom-save-all ()
1856 "Save all customizations in `custom-file'."
1857 (custom-save-variables)
1858 (custom-save-faces)
1859 (save-excursion
1860 (set-buffer (find-file-noselect custom-file))
1861 (save-buffer)))
1862
1863 ;;; The Customize Menu.
1864
1865 (defcustom custom-menu-nesting 2
1866 "Maximum nesting in custom menus."
1867 :type 'integer
1868 :group 'customize)
1869
1870 (defun custom-face-menu-create (widget symbol)
1871 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
1872 (vector (custom-unlispify-menu-entry symbol)
1873 `(custom-buffer-create '((,symbol custom-face)))
1874 t))
1875
1876 (defun custom-variable-menu-create (widget symbol)
1877 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
1878 (let ((type (get symbol 'custom-type)))
1879 (unless (listp type)
1880 (setq type (list type)))
1881 (if (and type (widget-get type :custom-menu))
1882 (widget-apply type :custom-menu symbol)
1883 (vector (custom-unlispify-menu-entry symbol)
1884 `(custom-buffer-create '((,symbol custom-variable)))
1885 t))))
1886
1887 (widget-put (get 'boolean 'widget-type)
1888 :custom-menu (lambda (widget symbol)
1889 (vector (custom-unlispify-menu-entry symbol)
1890 `(custom-buffer-create
1891 '((,symbol custom-variable)))
1892 ':style 'toggle
1893 ':selected symbol)))
1894
1895 (if (string-match "XEmacs" emacs-version)
1896 ;; XEmacs can create menus dynamically.
1897 (defun custom-group-menu-create (widget symbol)
1898 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
1899 `( ,(custom-unlispify-menu-entry symbol t)
1900 :filter (lambda (&rest junk)
1901 (cdr (custom-menu-create ',symbol)))))
1902 ;; But emacs can't.
1903 (defun custom-group-menu-create (widget symbol)
1904 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
1905 ;; Limit the nesting.
1906 (let ((custom-menu-nesting (1- custom-menu-nesting)))
1907 (custom-menu-create symbol))))
1908
1909 (defun custom-menu-create (symbol &optional name)
1910 "Create menu for customization group SYMBOL.
1911 If optional NAME is given, use that as the name of the menu.
1912 Otherwise make up a name from SYMBOL.
1913 The menu is in a format applicable to `easy-menu-define'."
1914 (unless name
1915 (setq name (custom-unlispify-menu-entry symbol)))
1916 (let ((item (vector name
1917 `(custom-buffer-create '((,symbol custom-group)))
1918 t)))
1919 (if (and (>= custom-menu-nesting 0)
1920 (< (length (get symbol 'custom-group)) widget-menu-max-size))
1921 (let ((custom-prefix-list (custom-prefix-add symbol
1922 custom-prefix-list)))
1923 (custom-load-symbol symbol)
1924 `(,(custom-unlispify-menu-entry symbol t)
1925 ,item
1926 "--"
1927 ,@(mapcar (lambda (entry)
1928 (widget-apply (if (listp (nth 1 entry))
1929 (nth 1 entry)
1930 (list (nth 1 entry)))
1931 :custom-menu (nth 0 entry)))
1932 (get symbol 'custom-group))))
1933 item)))
1934
1935 ;;;###autoload
1936 (defun custom-menu-update (event)
1937 "Update customize menu."
1938 (interactive "e")
1939 (add-hook 'custom-define-hook 'custom-menu-reset)
1940 (let* ((emacs (widget-apply '(custom-group) :custom-menu 'emacs))
1941 (menu `(,(car custom-help-menu)
1942 ,emacs
1943 ,@(cdr (cdr custom-help-menu)))))
1944 (let ((map (easy-menu-create-keymaps (car menu) (cdr menu))))
1945 (define-key global-map [menu-bar help-menu customize-menu]
1946 (cons (car menu) map)))))
1947
1948 ;;; Dependencies.
1949
1950 ;;;###autoload
1951 (defun custom-make-dependencies ()
1952 "Batch function to extract custom dependencies from .el files.
1953 Usage: emacs -batch *.el -f custom-make-dependencies > deps.el"
1954 (let ((buffers (buffer-list)))
1955 (while buffers
1956 (set-buffer (car buffers))
1957 (setq buffers (cdr buffers))
1958 (let ((file (buffer-file-name)))
1959 (when (and file (string-match "\\`\\(.*\\)\\.el\\'" file))
1960 (goto-char (point-min))
1961 (condition-case nil
1962 (let ((name (file-name-nondirectory (match-string 1 file))))
1963 (while t
1964 (let ((expr (read (current-buffer))))
1965 (when (and (listp expr)
1966 (memq (car expr) '(defcustom defface defgroup)))
1967 (eval expr)
1968 (put (nth 1 expr) 'custom-where name)))))
1969 (error nil))))))
1970 (mapatoms (lambda (symbol)
1971 (let ((members (get symbol 'custom-group))
1972 item where found)
1973 (when members
1974 (princ "(put '")
1975 (princ symbol)
1976 (princ " 'custom-loads '(")
1977 (while members
1978 (setq item (car (car members))
1979 members (cdr members)
1980 where (get item 'custom-where))
1981 (unless (or (null where)
1982 (member where found))
1983 (when found
1984 (princ " "))
1985 (prin1 where)
1986 (push where found)))
1987 (princ "))\n"))))))
1988
1989 ;;; The End.
1990
1991 (provide 'cus-edit)
1992
1993 ;; cus-edit.el ends here