*** empty log message ***
[gnus] / lisp / cus-edit.el
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.55
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         (message "Creating customization buffer...")
460         (custom-buffer-create found))
461     (if (stringp symbol)
462         (setq symbol (intern symbol)))
463     (unless (symbolp symbol)
464       (error "Should be a symbol %S" symbol))
465     (custom-buffer-create (list (list symbol 'custom-face)))))
466
467 ;;;###autoload
468 (defun customize-customized ()
469   "Customize all already customized user options."
470   (interactive)
471   (let ((found nil))
472     (mapatoms (lambda (symbol)
473                 (and (get symbol 'saved-face)
474                      (custom-facep symbol)
475                      (setq found (cons (list symbol 'custom-face) found)))
476                 (and (get symbol 'saved-value)
477                      (boundp symbol)
478                      (setq found
479                            (cons (list symbol 'custom-variable) found)))))
480     (if found
481         (custom-buffer-create found)
482       (error "No customized user options"))))
483
484 ;;;###autoload
485 (defun customize-apropos (regexp &optional all)
486   "Customize all user options matching REGEXP.
487 If ALL (e.g., started with a prefix key), include options which are not
488 user-settable."
489   (interactive "sCustomize regexp: \nP")
490   (let ((found nil))
491     (mapatoms (lambda (symbol)
492                 (when (string-match regexp (symbol-name symbol))
493                   (when (get symbol 'custom-group)
494                     (setq found (cons (list symbol 'custom-group) found)))
495                   (when (custom-facep symbol)
496                     (setq found (cons (list symbol 'custom-face) found)))
497                   (when (and (boundp symbol)
498                              (or (get symbol 'saved-value)
499                                  (get symbol 'factory-value)
500                                  (if all
501                                      (get symbol 'variable-documentation)
502                                    (user-variable-p symbol))))
503                     (setq found
504                           (cons (list symbol 'custom-variable) found))))))
505     (if found
506         (custom-buffer-create found)
507       (error "No matches"))))
508
509 ;;;###autoload
510 (defun custom-buffer-create (options)
511   "Create a buffer containing OPTIONS.
512 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
513 SYMBOL is a customization option, and WIDGET is a widget for editing
514 that option."
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   (widget-create 'info-link
521                  :tag "help"
522                  :help-echo "Push me for help."
523                  "(custom)The Customization Buffer")
524   (widget-insert " for more information.\n\n")
525   (setq custom-options
526         (mapcar (lambda (entry)
527                   (prog1
528                       (if (> (length options) 1)
529                           (widget-create (nth 1 entry)
530                                          :tag (custom-unlispify-tag-name
531                                                (nth 0 entry))
532                                          :value (nth 0 entry))
533                         ;; If there is only one entry, don't hide it!
534                         (widget-create (nth 1 entry)
535                                        :custom-state 'unknown
536                                        :tag (custom-unlispify-tag-name
537                                                (nth 0 entry))
538                                        :value (nth 0 entry)))
539                     (unless (eq (preceding-char) ?\n)
540                       (widget-insert "\n"))
541                     (widget-insert "\n")))
542                 options))
543   (mapcar 'custom-magic-reset custom-options)
544   (widget-create 'push-button
545                  :tag "Set"
546                  :help-echo "Push me to set all modifications."
547                  :action (lambda (widget &optional event)
548                            (custom-set)))
549   (widget-insert " ")
550   (widget-create 'push-button
551                  :tag "Save"
552                  :help-echo "Push me to make the modifications default."
553                  :action (lambda (widget &optional event)
554                            (custom-save)))
555   (widget-insert " ")
556   (widget-create 'push-button
557                  :tag "Reset"
558                  :help-echo "Push me to undo all modifications."
559                  :action (lambda (widget &optional event)
560                            (custom-reset event)))
561   (widget-insert " ")
562   (widget-create 'push-button
563                  :tag "Done"
564                  :help-echo "Push me to bury the buffer."
565                  :action (lambda (widget &optional event)
566                            (bury-buffer)
567                            ;; Steal button release event.
568                            (if (and (fboundp 'button-press-event-p)
569                                     (fboundp 'next-command-event))
570                                ;; XEmacs
571                                (and event
572                                     (button-press-event-p event)
573                                     (next-command-event))
574                              ;; Emacs
575                              (when (memq 'down (event-modifiers event))
576                                (read-event)))))
577   (widget-insert "\n")
578   (widget-setup))
579
580 ;;; Modification of Basic Widgets.
581 ;;
582 ;; We add extra properties to the basic widgets needed here.  This is
583 ;; fine, as long as we are careful to stay within out own namespace.
584 ;;
585 ;; We want simple widgets to be displayed by default, but complex
586 ;; widgets to be hidden.
587
588 (widget-put (get 'item 'widget-type) :custom-show t)
589 (widget-put (get 'editable-field 'widget-type)
590             :custom-show (lambda (widget value)
591                            (let ((pp (pp-to-string value)))
592                              (cond ((string-match "\n" pp)
593                                     nil)
594                                    ((> (length pp) 40)
595                                     nil)
596                                    (t t)))))
597 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
598
599 ;;; The `custom-manual' Widget.
600
601 (define-widget 'custom-manual 'info-link
602   "Link to the manual entry for this customization option."
603   :help-echo "Push me to read the manual."
604   :tag "Manual")
605
606 ;;; The `custom-magic' Widget.
607
608 (defface custom-invalid-face '((((class color))
609                                 (:foreground "yellow" :background "red"))
610                                (t
611                                 (:bold t :italic t :underline t)))
612   "Face used when the customize item is invalid.")
613
614 (defface custom-rogue-face '((((class color))
615                               (:foreground "pink" :background "black"))
616                              (t
617                               (:underline t)))
618   "Face used when the customize item is not defined for customization.")
619
620 (defface custom-modified-face '((((class color))
621                                  (:foreground "white" :background "blue"))
622                                 (t
623                                  (:italic t :bold)))
624   "Face used when the customize item has been modified.")
625
626 (defface custom-set-face '((((class color))
627                                 (:foreground "blue" :background "white"))
628                                (t
629                                 (:italic t)))
630   "Face used when the customize item has been set.")
631
632 (defface custom-changed-face '((((class color))
633                                 (:foreground "white" :background "blue"))
634                                (t
635                                 (:italic t)))
636   "Face used when the customize item has been changed.")
637
638 (defface custom-saved-face '((t (:underline t)))
639   "Face used when the customize item has been saved.")
640
641 (defcustom custom-magic-alist '((nil "#" underline "\
642 uninitialized, you should not see this.")
643                                 (unknown "?" italic "\
644 unknown, you should not see this.")
645                                 (hidden "-" default "\
646 hidden, press the state button to show.")
647                                 (invalid "x" custom-invalid-face "\
648 the value displayed for this item is invalid and cannot be set.")
649                                 (modified "*" custom-modified-face "\
650 you have edited the item, and can now set it.")
651                                 (set "+" custom-set-face "\
652 you have set this item, but not saved it.")
653                                 (changed ":" custom-changed-face "\
654 this item has been changed outside customize.")
655                                 (saved "!" custom-saved-face "\
656 this item has been saved.")
657                                 (rogue "@" custom-rogue-face "\
658 this item is not prepared for customization.")
659                                 (factory " " nil "\
660 this item is unchanged from its factory setting."))
661   "Alist of customize option states.
662 Each entry is of the form (STATE MAGIC FACE DESCRIPTION), where
663
664 STATE is one of the following symbols:
665
666 `nil'
667    For internal use, should never occur.
668 `unknown'
669    For internal use, should never occur.
670 `hidden'
671    This item is not being displayed.
672 `invalid'
673    This item is modified, but has an invalid form.
674 `modified'
675    This item is modified, and has a valid form.
676 `set'
677    This item has been set but not saved.
678 `changed'
679    The current value of this item has been changed temporarily.
680 `saved'
681    This item is marked for saving.
682 `rogue'
683    This item has no customization information.
684 `factory'
685    This item is unchanged from the factory default.
686
687 MAGIC is a string used to present that state.
688
689 FACE is a face used to present the state.
690
691 DESCRIPTION is a string describing the state.
692
693 The list should be sorted most significant first."
694   :type '(list (checklist :inline t
695                           (group (const nil)
696                                  (string :tag "Magic")
697                                  face
698                                  (string :tag "Description"))
699                           (group (const unknown)
700                                  (string :tag "Magic")
701                                  face
702                                  (string :tag "Description"))
703                           (group (const hidden)
704                                  (string :tag "Magic")
705                                  face
706                                  (string :tag "Description"))
707                           (group (const invalid)
708                                  (string :tag "Magic")
709                                  face
710                                  (string :tag "Description"))
711                           (group (const modified)
712                                  (string :tag "Magic")
713                                  face
714                                  (string :tag "Description"))
715                           (group (const set)
716                                  (string :tag "Magic")
717                                  face
718                                  (string :tag "Description"))
719                           (group (const changed)
720                                  (string :tag "Magic")
721                                  face
722                                  (string :tag "Description"))
723                           (group (const saved)
724                                  (string :tag "Magic")
725                                  face
726                                  (string :tag "Description"))
727                           (group (const rogue)
728                                  (string :tag "Magic")
729                                  face
730                                  (string :tag "Description"))
731                           (group (const factory)
732                                  (string :tag "Magic")
733                                  face
734                                  (string :tag "Description")))
735                (editable-list :inline t
736                               (group symbol
737                                      (string :tag "Magic")
738                                      face
739                                      (string :tag "Description"))))
740   :group 'customize)
741
742 (defcustom custom-magic-show 'long
743   "Show long description of the state of each customization option."
744   :type '(choice (const :tag "no" nil)
745                  (const short)
746                  (const long))
747   :group 'customize)
748
749 (defcustom custom-magic-show-button t
750   "Show a magic button indicating the state of each customization option."
751   :type 'boolean
752   :group 'customize)
753
754 (define-widget 'custom-magic 'default
755   "Show and manipulate state for a customization option."
756   :format "%v"
757   :action 'widget-choice-item-action
758   :value-get 'ignore
759   :value-create 'custom-magic-value-create
760   :value-delete 'widget-children-value-delete)
761
762 (defun custom-magic-value-create (widget)
763   ;; Create compact status report for WIDGET.
764   (let* ((parent (widget-get widget :parent))
765          (state (widget-get parent :custom-state))
766          (entry (assq state custom-magic-alist))
767          (magic (nth 1 entry))
768          (face (nth 2 entry))
769          (text (nth 3 entry))
770          (lisp (eq (widget-get parent :custom-form) 'lisp))
771          children)
772     (when custom-magic-show
773       (push (widget-create-child-and-convert widget 'choice-item
774                                              :help-echo "\
775 Push me to change the state of this item."
776                                              :format "%[%t%]"
777                                              :tag "State")
778             children)
779       (insert ": ")
780       (if (eq custom-magic-show 'long)
781           (insert text)
782         (insert (symbol-name state)))
783       (when lisp
784         (insert " (lisp)"))
785       (insert "\n"))
786     (when custom-magic-show-button
787       (when custom-magic-show
788         (let ((indent (widget-get parent :indent)))
789           (when indent
790             (insert-char ?  indent))))
791       (push (widget-create-child-and-convert widget 'choice-item
792                                              :button-face face
793                                              :help-echo "\
794 Push me to change the state."
795                                              :format "%[%t%]"
796                                              :tag (if lisp
797                                                       (concat "(" magic ")")
798                                                     (concat "[" magic "]")))
799             children)
800       (insert " "))
801     (widget-put widget :children children)))
802
803 (defun custom-magic-reset (widget)
804   "Redraw the :custom-magic property of WIDGET."
805   (let ((magic (widget-get widget :custom-magic)))
806     (widget-value-set magic (widget-value magic))))
807
808 ;;; The `custom-level' Widget.
809
810 (define-widget 'custom-level 'item
811   "The custom level buttons."
812   :format "%[%t%]"
813   :help-echo "Push me to expand or collapse this item."
814   :action 'custom-level-action)
815
816 (defun custom-level-action (widget &optional event)
817   "Toggle visibility for parent to WIDGET."
818   (let* ((parent (widget-get widget :parent))
819          (state (widget-get parent :custom-state)))
820     (cond ((memq state '(invalid modified))
821            (error "There are unset changes"))
822           ((eq state 'hidden)
823            (widget-put parent :custom-state 'unknown))
824           (t
825            (widget-put parent :custom-state 'hidden)))
826     (custom-redraw parent)))
827
828 ;;; The `custom' Widget.
829
830 (define-widget 'custom 'default
831   "Customize a user option."
832   :convert-widget 'custom-convert-widget
833   :format "%l%[%t%]: %v%m%h%a"
834   :format-handler 'custom-format-handler
835   :notify 'custom-notify
836   :custom-level 1
837   :custom-state 'hidden
838   :documentation-property 'widget-subclass-responsibility
839   :value-create 'widget-subclass-responsibility
840   :value-delete 'widget-children-value-delete
841   :value-get 'widget-item-value-get
842   :validate 'widget-editable-list-validate
843   :match (lambda (widget value) (symbolp value)))
844
845 (defun custom-convert-widget (widget)
846   ;; Initialize :value and :tag from :args in WIDGET.
847   (let ((args (widget-get widget :args)))
848     (when args
849       (widget-put widget :value (widget-apply widget
850                                               :value-to-internal (car args)))
851       (widget-put widget :tag (custom-unlispify-tag-name (car args)))
852       (widget-put widget :args nil)))
853   widget)
854
855 (defun custom-format-handler (widget escape)
856   ;; We recognize extra escape sequences.
857   (let* ((buttons (widget-get widget :buttons))
858          (state (widget-get widget :custom-state))
859          (level (widget-get widget :custom-level)))
860     (cond ((eq escape ?l)
861            (when level
862              (push (widget-create-child-and-convert
863                     widget 'custom-level (make-string level ?*))
864                    buttons)
865              (widget-insert " ")
866              (widget-put widget :buttons buttons)))
867           ((eq escape ?L)
868            (when (eq state 'hidden)
869              (widget-insert " ...")))
870           ((eq escape ?m)
871            (and (eq (preceding-char) ?\n)
872                 (widget-get widget :indent)
873                 (insert-char ?  (widget-get widget :indent)))
874            (let ((magic (widget-create-child-and-convert
875                          widget 'custom-magic nil)))
876              (widget-put widget :custom-magic magic)
877              (push magic buttons)
878              (widget-put widget :buttons buttons)))
879           ((eq escape ?a)
880            (let* ((symbol (widget-get widget :value))
881                   (links (get symbol 'custom-links))
882                   (many (> (length links) 2)))
883              (when links
884                (and (eq (preceding-char) ?\n)
885                     (widget-get widget :indent)
886                     (insert-char ?  (widget-get widget :indent)))
887                (insert "See also ")
888                (while links
889                  (push (widget-create-child-and-convert widget (car links))
890                        buttons)
891                  (setq links (cdr links))
892                  (cond ((null links)
893                         (insert ".\n"))
894                        ((null (cdr links))
895                         (if many
896                             (insert ", and ")
897                           (insert " and ")))
898                        (t
899                         (insert ", "))))
900                (widget-put widget :buttons buttons))))
901           (t
902            (widget-default-format-handler widget escape)))))
903
904 (defun custom-notify (widget &rest args)
905   "Keep track of changes."
906   (widget-put widget :custom-state 'modified)
907   (let ((buffer-undo-list t))
908     (custom-magic-reset widget))
909   (apply 'widget-default-notify widget args))
910
911 (defun custom-redraw (widget)
912   "Redraw WIDGET with current settings."
913   (let ((pos (point))
914         (from (marker-position (widget-get widget :from)))
915         (to (marker-position (widget-get widget :to))))
916     (save-excursion
917       (widget-value-set widget (widget-value widget))
918       (custom-redraw-magic widget))
919     (when (and (>= pos from) (<= pos to))
920       (goto-char pos))))
921
922 (defun custom-redraw-magic (widget)
923   "Redraw WIDGET state with current settings."
924   (while widget
925     (let ((magic (widget-get widget :custom-magic)))
926       (unless magic
927         (debug))
928       (widget-value-set magic (widget-value magic))
929       (when (setq widget (widget-get widget :group))
930         (custom-group-state-update widget))))
931   (widget-setup))
932
933 (defun custom-show (widget value)
934   "Non-nil if WIDGET should be shown with VALUE by default."
935   (let ((show (widget-get widget :custom-show)))
936     (cond ((null show)
937            nil)
938           ((eq t show)
939            t)
940           (t
941            (funcall show widget value)))))
942
943 (defun custom-load-symbol (symbol)
944   "Load all dependencies for SYMBOL."
945   (let ((loads (get symbol 'custom-loads))
946         load)
947     (while loads
948       (setq load (car loads)
949             loads (cdr loads))
950       (cond ((symbolp load)
951              (condition-case nil
952                  (require load)
953                (error nil)))
954             ((assoc load load-history))
955             (t
956              (condition-case nil
957                  (load-library load)
958                (error nil)))))))
959
960 (defun custom-load-widget (widget)
961   "Load all dependencies for WIDGET."
962   (custom-load-symbol (widget-value widget)))
963
964 ;;; The `custom-variable' Widget.
965
966 (defface custom-variable-sample-face '((t (:underline t)))
967   "Face used for unpushable variable tags."
968   :group 'customize)
969
970 (defface custom-variable-button-face '((t (:underline t :bold t)))
971   "Face used for pushable variable tags."
972   :group 'customize)
973
974 (define-widget 'custom-variable 'custom
975   "Customize variable."
976   :format "%l%v%m%h%a"
977   :help-echo "Push me to set or reset this variable."
978   :documentation-property 'variable-documentation
979   :custom-state nil
980   :custom-menu 'custom-variable-menu-create
981   :custom-form 'edit
982   :value-create 'custom-variable-value-create
983   :action 'custom-variable-action
984   :custom-set 'custom-variable-set
985   :custom-save 'custom-variable-save
986   :custom-reset-current 'custom-redraw
987   :custom-reset-saved 'custom-variable-reset-saved
988   :custom-reset-factory 'custom-variable-reset-factory)
989
990 (defun custom-variable-value-create (widget)
991   "Here is where you edit the variables value."
992   (custom-load-widget widget)
993   (let* ((buttons (widget-get widget :buttons))
994          (children (widget-get widget :children))
995          (form (widget-get widget :custom-form))
996          (state (widget-get widget :custom-state))
997          (symbol (widget-get widget :value))
998          (options (get symbol 'custom-options))
999          (child-type (or (get symbol 'custom-type) 'sexp))
1000          (tag (widget-get widget :tag))
1001          (type (let ((tmp (if (listp child-type)
1002                               (copy-list child-type)
1003                             (list child-type))))
1004                  (when options
1005                    (widget-put tmp :options options))
1006                  tmp))
1007          (conv (widget-convert type))
1008          (value (if (default-boundp symbol)
1009                     (default-value symbol)
1010                   (widget-get conv :value))))
1011     ;; If the widget is new, the child determine whether it is hidden.
1012     (cond (state)
1013           ((custom-show type value)
1014            (setq state 'unknown))
1015           (t
1016            (setq state 'hidden)))
1017     ;; If we don't know the state, see if we need to edit it in lisp form.
1018     (when (eq state 'unknown)
1019       (unless (widget-apply conv :match value)
1020         ;; (widget-apply (widget-convert type) :match value)
1021         (setq form 'lisp)))
1022     ;; Now we can create the child widget.
1023     (cond ((eq state 'hidden)
1024            ;; Indicate hidden value.
1025            (push (widget-create-child-and-convert
1026                   widget 'item
1027                   :format "%{%t%}: ..."
1028                   :sample-face 'custom-variable-sample-face
1029                   :tag tag
1030                   :parent widget)
1031                  children))
1032           ((eq form 'lisp)
1033            ;; In lisp mode edit the saved value when possible.
1034            (let* ((value (cond ((get symbol 'saved-value)
1035                                 (car (get symbol 'saved-value)))
1036                                ((get symbol 'factory-value)
1037                                 (car (get symbol 'factory-value)))
1038                                ((default-boundp symbol)
1039                                 (custom-quote (default-value symbol)))
1040                                (t
1041                                 (custom-quote (widget-get conv :value))))))
1042              (push (widget-create-child-and-convert
1043                     widget 'sexp
1044                     :button-face 'custom-variable-button-face
1045                     :tag (symbol-name symbol)
1046                     :parent widget
1047                     :value value)
1048                    children)))
1049           (t
1050            ;; Edit mode.
1051            (push (widget-create-child-and-convert
1052                   widget type
1053                   :tag tag
1054                   :button-face 'custom-variable-button-face
1055                   :sample-face 'custom-variable-sample-face
1056                   :value value)
1057                  children)))
1058     ;; Now update the state.
1059     (unless (eq (preceding-char) ?\n)
1060       (widget-insert "\n"))
1061     (if (eq state 'hidden)
1062         (widget-put widget :custom-state state)
1063       (custom-variable-state-set widget))
1064     (widget-put widget :custom-form form)
1065     (widget-put widget :buttons buttons)
1066     (widget-put widget :children children)))
1067
1068 (defun custom-variable-state-set (widget)
1069   "Set the state of WIDGET."
1070   (let* ((symbol (widget-value widget))
1071          (value (if (default-boundp symbol)
1072                     (default-value symbol)
1073                   (widget-get widget :value)))
1074          tmp
1075          (state (cond ((setq tmp (get symbol 'customized-value))
1076                        (if (condition-case nil
1077                                (equal value (eval (car tmp)))
1078                              (error nil))
1079                            'set
1080                          'changed))
1081                       ((setq tmp (get symbol 'saved-value))
1082                        (if (condition-case nil
1083                                (equal value (eval (car tmp)))
1084                              (error nil))
1085                            'saved
1086                          'changed))
1087                       ((setq tmp (get symbol 'factory-value))
1088                        (if (condition-case nil
1089                                (equal value (eval (car tmp)))
1090                              (error nil))
1091                            'factory
1092                          'changed))
1093                       (t 'rogue))))
1094     (widget-put widget :custom-state state)))
1095
1096 (defvar custom-variable-menu
1097   '(("Edit" . custom-variable-edit)
1098     ("Edit Lisp" . custom-variable-edit-lisp)
1099     ("Set" . custom-variable-set)
1100     ("Save" . custom-variable-save)
1101     ("Reset to Current" . custom-redraw)
1102     ("Reset to Saved" . custom-variable-reset-saved)
1103     ("Reset to Factory Settings" . custom-variable-reset-factory))
1104   "Alist of actions for the `custom-variable' widget.
1105 The key is a string containing the name of the action, the value is a
1106 lisp function taking the widget as an element which will be called
1107 when the action is chosen.")
1108
1109 (defun custom-variable-action (widget &optional event)
1110   "Show the menu for `custom-variable' WIDGET.
1111 Optional EVENT is the location for the menu."
1112   (if (eq (widget-get widget :custom-state) 'hidden)
1113       (progn
1114         (widget-put widget :custom-state 'unknown)
1115         (custom-redraw widget))
1116     (let* ((completion-ignore-case t)
1117            (answer (widget-choose (custom-unlispify-tag-name
1118                                    (widget-get widget :value))
1119                                   custom-variable-menu
1120                                   event)))
1121       (if answer
1122           (funcall answer widget)))))
1123
1124 (defun custom-variable-edit (widget)
1125   "Edit value of WIDGET."
1126   (widget-put widget :custom-state 'unknown)
1127   (widget-put widget :custom-form 'edit)
1128   (custom-redraw widget))
1129
1130 (defun custom-variable-edit-lisp (widget)
1131   "Edit the lisp representation of the value of WIDGET."
1132   (widget-put widget :custom-state 'unknown)
1133   (widget-put widget :custom-form 'lisp)
1134   (custom-redraw widget))
1135
1136 (defun custom-variable-set (widget)
1137   "Set the current value for the variable being edited by WIDGET."
1138   (let ((form (widget-get widget :custom-form))
1139         (state (widget-get widget :custom-state))
1140         (child (car (widget-get widget :children)))
1141         (symbol (widget-value widget))
1142         val)
1143     (cond ((eq state 'hidden)
1144            (error "Cannot set hidden variable."))
1145           ((setq val (widget-apply child :validate))
1146            (goto-char (widget-get val :from))
1147            (error "%s" (widget-get val :error)))
1148           ((eq form 'lisp)
1149            (set symbol (eval (setq val (widget-value child))))
1150            (put symbol 'customized-value (list val)))
1151           (t
1152            (set symbol (setq val (widget-value child)))
1153            (put symbol 'customized-value (list (custom-quote val)))))
1154     (custom-variable-state-set widget)
1155     (custom-redraw-magic widget)))
1156
1157 (defun custom-variable-save (widget)
1158   "Set the default value for the variable being edited by WIDGET."
1159   (let ((form (widget-get widget :custom-form))
1160         (state (widget-get widget :custom-state))
1161         (child (car (widget-get widget :children)))
1162         (symbol (widget-value widget))
1163         val)
1164     (cond ((eq state 'hidden)
1165            (error "Cannot set hidden variable."))
1166           ((setq val (widget-apply child :validate))
1167            (goto-char (widget-get val :from))
1168            (error "%s" (widget-get val :error)))
1169           ((eq form 'lisp)
1170            (put symbol 'saved-value (list (widget-value child)))
1171            (set symbol (eval (widget-value child))))
1172           (t
1173            (put symbol
1174                 'saved-value (list (custom-quote (widget-value
1175                                                   child))))
1176            (set symbol (widget-value child))))
1177     (put symbol 'customized-value nil)
1178     (custom-save-all)
1179     (custom-variable-state-set widget)
1180     (custom-redraw-magic widget)))
1181
1182 (defun custom-variable-reset-saved (widget)
1183   "Restore the saved value for the variable being edited by WIDGET."
1184   (let ((symbol (widget-value widget)))
1185     (if (get symbol 'saved-value)
1186         (condition-case nil
1187             (set symbol (eval (car (get symbol 'saved-value))))
1188           (error nil))
1189       (error "No saved value for %s" symbol))
1190     (put symbol 'customized-value nil)
1191     (widget-put widget :custom-state 'unknown)
1192     (custom-redraw widget)))
1193
1194 (defun custom-variable-reset-factory (widget)
1195   "Restore the factory setting for the variable being edited by WIDGET."
1196   (let ((symbol (widget-value widget)))
1197     (if (get symbol 'factory-value)
1198         (set symbol (eval (car (get symbol 'factory-value))))
1199       (error "No factory default for %S" symbol))
1200     (put symbol 'customized-value nil)
1201     (when (get symbol 'saved-value)
1202       (put symbol 'saved-value nil)
1203       (custom-save-all))
1204     (widget-put widget :custom-state 'unknown)
1205     (custom-redraw widget)))
1206
1207 ;;; The `custom-face-edit' Widget.
1208
1209 (defvar custom-face-edit-args
1210   (mapcar (lambda (att)
1211             (list 'group
1212                   :inline t
1213                   (list 'const :format "" :value (nth 0 att))
1214                   (nth 1 att)))
1215           custom-face-attributes))
1216
1217 (define-widget 'custom-face-edit 'checklist
1218   "Edit face attributes."
1219   :format "%t: %v"
1220   :tag "Attributes"
1221   :extra-offset 12
1222   :args (mapcar (lambda (att)
1223                   (list 'group
1224                         :inline t
1225                         (list 'const :format "" :value (nth 0 att))
1226                         (nth 1 att)))
1227                 custom-face-attributes))
1228
1229 ;;; The `custom-display' Widget.
1230
1231 (define-widget 'custom-display 'menu-choice
1232   "Select a display type."
1233   :tag "Display"
1234   :value t
1235   :args '((const :tag "all" t)
1236           (checklist :offset 0
1237                      :extra-offset 9
1238                      :args ((group (const :format "Type: " type)
1239                                    (checklist :inline t
1240                                               :offset 0
1241                                               (const :format "X "
1242                                                      x)
1243                                               (const :format "PM "
1244                                                      pm)
1245                                               (const :format "Win32 "
1246                                                      win32)
1247                                               (const :format "DOS "
1248                                                      pc)
1249                                               (const :format "TTY%n"
1250                                                      tty)))
1251                             (group (const :format "Class: " class)
1252                                    (checklist :inline t
1253                                               :offset 0
1254                                               (const :format "Color "
1255                                                      color)
1256                                               (const :format
1257                                                      "Grayscale "
1258                                                      grayscale)
1259                                               (const :format "Monochrome%n"
1260                                                      mono)))
1261                             (group  (const :format "Background: " background)
1262                                     (checklist :inline t
1263                                                :offset 0
1264                                                (const :format "Light "
1265                                                       light)
1266                                                (const :format "Dark\n"
1267                                                       dark)))))))
1268
1269 ;;; The `custom-face' Widget.
1270
1271 (defface custom-face-tag-face '((t (:underline t)))
1272   "Face used for face tags."
1273   :group 'customize)
1274
1275 (define-widget 'custom-face 'custom
1276   "Customize face."
1277   :format "%l%{%t%}: %s%m%h%a%v"
1278   :format-handler 'custom-face-format-handler
1279   :sample-face 'custom-face-tag-face
1280   :help-echo "Push me to set or reset this face."
1281   :documentation-property '(lambda (face)
1282                              (face-doc-string face))
1283   :value-create 'custom-face-value-create
1284   :action 'custom-face-action
1285   :custom-set 'custom-face-set
1286   :custom-save 'custom-face-save
1287   :custom-reset-current 'custom-redraw
1288   :custom-reset-saved 'custom-face-reset-saved
1289   :custom-reset-factory 'custom-face-reset-factory
1290   :custom-menu 'custom-face-menu-create)
1291
1292 (defun custom-face-format-handler (widget escape)
1293   ;; We recognize extra escape sequences.
1294   (let (child
1295         (symbol (widget-get widget :value)))
1296     (cond ((eq escape ?s)
1297            (and (string-match "XEmacs" emacs-version)
1298                 ;; XEmacs cannot display initialized faces.
1299                 (not (custom-facep symbol))
1300                 (copy-face 'custom-face-empty symbol))
1301            (setq child (widget-create-child-and-convert
1302                         widget 'item
1303                         :format "(%{%t%})\n"
1304                         :sample-face symbol
1305                         :tag "sample")))
1306           (t
1307            (custom-format-handler widget escape)))
1308     (when child
1309       (widget-put widget
1310                   :buttons (cons child (widget-get widget :buttons))))))
1311
1312 (defun custom-face-value-create (widget)
1313   ;; Create a list of the display specifications.
1314   (unless (eq (preceding-char) ?\n)
1315     (insert "\n"))
1316   (when (not (eq (widget-get widget :custom-state) 'hidden))
1317     (custom-load-widget widget)
1318     (let* ((symbol (widget-value widget))
1319            (edit (widget-create-child-and-convert
1320                   widget 'editable-list
1321                   :entry-format "%i %d %v"
1322                   :value (or (get symbol 'saved-face)
1323                              (get symbol 'factory-face))
1324                   '(group :format "%v"
1325                           custom-display custom-face-edit))))
1326       (custom-face-state-set widget)
1327       (widget-put widget :children (list edit)))))
1328
1329 (defvar custom-face-menu
1330   '(("Set" . custom-face-set)
1331     ("Save" . custom-face-save)
1332     ("Reset to Saved" . custom-face-reset-saved)
1333     ("Reset to Factory Setting" . custom-face-reset-factory))
1334   "Alist of actions for the `custom-face' widget.
1335 The key is a string containing the name of the action, the value is a
1336 lisp function taking the widget as an element which will be called
1337 when the action is chosen.")
1338
1339 (defun custom-face-state-set (widget)
1340   "Set the state of WIDGET."
1341   (let ((symbol (widget-value widget)))
1342     (widget-put widget :custom-state (cond ((get symbol 'customized-face)
1343                                             'set)
1344                                            ((get symbol 'saved-face)
1345                                             'saved)
1346                                            ((get symbol 'factory-face)
1347                                             'factory)
1348                                            (t
1349                                             'rogue)))))
1350
1351 (defun custom-face-action (widget &optional event)
1352   "Show the menu for `custom-face' WIDGET.
1353 Optional EVENT is the location for the menu."
1354   (if (eq (widget-get widget :custom-state) 'hidden)
1355       (progn
1356         (widget-put widget :custom-state 'unknown)
1357         (custom-redraw widget))
1358     (let* ((completion-ignore-case t)
1359            (symbol (widget-get widget :value))
1360            (answer (widget-choose (custom-unlispify-tag-name symbol)
1361                                   custom-face-menu event)))
1362       (if answer
1363           (funcall answer widget)))))
1364
1365 (defun custom-face-set (widget)
1366   "Make the face attributes in WIDGET take effect."
1367   (let* ((symbol (widget-value widget))
1368          (child (car (widget-get widget :children)))
1369          (value (widget-value child)))
1370     (put symbol 'customized-face value)
1371     (when (fboundp 'copy-face)
1372       (copy-face 'custom-face-empty symbol))
1373     (custom-face-display-set symbol value)
1374     (custom-face-state-set widget)
1375     (custom-redraw-magic widget)))
1376
1377 (defun custom-face-save (widget)
1378   "Make the face attributes in WIDGET default."
1379   (let* ((symbol (widget-value widget))
1380          (child (car (widget-get widget :children)))
1381          (value (widget-value child)))
1382     (when (fboundp 'copy-face)
1383       (copy-face 'custom-face-empty symbol))
1384     (custom-face-display-set symbol value)
1385     (put symbol 'saved-face value)
1386     (put symbol 'customized-face nil)
1387     (custom-face-state-set widget)
1388     (custom-redraw-magic widget)))
1389
1390 (defun custom-face-reset-saved (widget)
1391   "Restore WIDGET to the face's default attributes."
1392   (let* ((symbol (widget-value widget))
1393          (child (car (widget-get widget :children)))
1394          (value (get symbol 'saved-face)))
1395     (unless value
1396       (error "No saved value for this face"))
1397     (put symbol 'customized-face nil)
1398     (when (fboundp 'copy-face)
1399       (copy-face 'custom-face-empty symbol))
1400     (custom-face-display-set symbol value)
1401     (widget-value-set child value)
1402     (custom-face-state-set widget)
1403     (custom-redraw-magic widget)))
1404
1405 (defun custom-face-reset-factory (widget)
1406   "Restore WIDGET to the face's factory settings."
1407   (let* ((symbol (widget-value widget))
1408          (child (car (widget-get widget :children)))
1409          (value (get symbol 'factory-face)))
1410     (unless value
1411       (error "No factory default for this face"))
1412     (put symbol 'customized-face nil)
1413     (when (get symbol 'saved-face)
1414       (put symbol 'saved-face nil)
1415       (custom-save-all))
1416     (when (fboundp 'copy-face)
1417       (copy-face 'custom-face-empty symbol))
1418     (custom-face-display-set symbol value)
1419     (widget-value-set child value)
1420     (custom-face-state-set widget)
1421     (custom-redraw-magic widget)))
1422
1423 ;;; The `face' Widget.
1424
1425 (define-widget 'face 'default
1426   "Select and customize a face."
1427   :convert-widget 'widget-item-convert-widget
1428   :format "%[%t%]: %v"
1429   :tag "Face"
1430   :value 'default
1431   :value-create 'widget-face-value-create
1432   :value-delete 'widget-face-value-delete
1433   :value-get 'widget-item-value-get
1434   :validate 'widget-editable-list-validate
1435   :action 'widget-face-action
1436   :match '(lambda (widget value) (symbolp value)))
1437
1438 (defun widget-face-value-create (widget)
1439   ;; Create a `custom-face' child.
1440   (let* ((symbol (widget-value widget))
1441          (child (widget-create-child-and-convert
1442                  widget 'custom-face
1443                  :format "%t %s%m%h%v"
1444                  :custom-level nil
1445                  :value symbol)))
1446     (custom-magic-reset child)
1447     (setq custom-options (cons child custom-options))
1448     (widget-put widget :children (list child))))
1449
1450 (defun widget-face-value-delete (widget)
1451   ;; Remove the child from the options.
1452   (let ((child (car (widget-get widget :children))))
1453     (setq custom-options (delq child custom-options))
1454     (widget-children-value-delete widget)))
1455
1456 (defvar face-history nil
1457   "History of entered face names.")
1458
1459 (defun widget-face-action (widget &optional event)
1460   "Prompt for a face."
1461   (let ((answer (completing-read "Face: "
1462                                  (mapcar (lambda (face)
1463                                            (list (symbol-name face)))
1464                                          (face-list))
1465                                  nil nil nil
1466                                  'face-history)))
1467     (unless (zerop (length answer))
1468       (widget-value-set widget (intern answer))
1469       (widget-apply widget :notify widget event)
1470       (widget-setup))))
1471
1472 ;;; The `hook' Widget.
1473
1474 (define-widget 'hook 'list
1475   "A emacs lisp hook"
1476   :convert-widget 'custom-hook-convert-widget
1477   :tag "Hook")
1478
1479 (defun custom-hook-convert-widget (widget)
1480   ;; Handle `:custom-options'.
1481   (let* ((options (widget-get widget :options))
1482          (other `(editable-list :inline t
1483                                 :entry-format "%i %d%v"
1484                                 (function :format " %v")))
1485          (args (if options
1486                    (list `(checklist :inline t
1487                                      ,@(mapcar (lambda (entry)
1488                                                  `(function-item ,entry))
1489                                                options))
1490                          other)
1491                  (list other))))
1492     (widget-put widget :args args)
1493     widget))
1494
1495 ;;; The `custom-group' Widget.
1496
1497 (defcustom custom-group-tag-faces '(custom-group-tag-face-1)
1498   ;; In XEmacs, this ought to play games with font size.
1499   "Face used for group tags.
1500 The first member is used for level 1 groups, the second for level 2,
1501 and so forth.  The remaining group tags are shown with
1502 `custom-group-tag-face'."
1503   :type '(repeat face)
1504   :group 'customize)
1505
1506 (defface custom-group-tag-face-1 '((((class color)
1507                                    (background dark))
1508                                   (:foreground "pink" :underline t))
1509                                  (((class color)
1510                                    (background light))
1511                                   (:foreground "red" :underline t))
1512                                  (t (:underline t)))
1513   "Face used for group tags.")
1514
1515 (defface custom-group-tag-face '((((class color)
1516                                    (background dark))
1517                                   (:foreground "light blue" :underline t))
1518                                  (((class color)
1519                                    (background light))
1520                                   (:foreground "blue" :underline t))
1521                                  (t (:underline t)))
1522   "Face used for low level group tags."
1523   :group 'customize)
1524
1525 (define-widget 'custom-group 'custom
1526   "Customize group."
1527   :format "%l%{%t%}:%L\n%m%h%a%v"
1528   :sample-face-get 'custom-group-sample-face-get
1529   :documentation-property 'group-documentation
1530   :help-echo "Push me to set or reset all members of this group."
1531   :value-create 'custom-group-value-create
1532   :action 'custom-group-action
1533   :custom-set 'custom-group-set
1534   :custom-save 'custom-group-save
1535   :custom-reset-current 'custom-group-reset-current
1536   :custom-reset-saved 'custom-group-reset-saved
1537   :custom-reset-factory 'custom-group-reset-factory
1538   :custom-menu 'custom-group-menu-create)
1539
1540 (defun custom-group-sample-face-get (widget)
1541   ;; Use :sample-face.
1542   (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
1543       'custom-group-tag-face))
1544
1545 (defun custom-group-value-create (widget)
1546   (let ((state (widget-get widget :custom-state)))
1547     (unless (eq state 'hidden)
1548       (custom-load-widget widget)
1549       (let* ((level (widget-get widget :custom-level))
1550              (symbol (widget-value widget))
1551              (members (get symbol 'custom-group))
1552              (prefixes (widget-get widget :custom-prefixes))
1553              (custom-prefix-list (custom-prefix-add symbol prefixes))
1554              (children (mapcar (lambda (entry)
1555                                  (widget-insert "\n")
1556                                  (prog1
1557                                      (widget-create-child-and-convert
1558                                       widget (nth 1 entry)
1559                                       :group widget
1560                                       :tag (custom-unlispify-tag-name
1561                                             (nth 0 entry))
1562                                       :custom-prefixes custom-prefix-list
1563                                       :custom-level (1+ level)
1564                                       :value (nth 0 entry))
1565                                    (unless (eq (preceding-char) ?\n)
1566                                      (widget-insert "\n"))))
1567                                members)))
1568         (mapcar 'custom-magic-reset children)
1569         (widget-put widget :children children)
1570         (custom-group-state-update widget)))))
1571
1572 (defvar custom-group-menu
1573   '(("Set" . custom-group-set)
1574     ("Save" . custom-group-save)
1575     ("Reset to Current" . custom-group-reset-current)
1576     ("Reset to Saved" . custom-group-reset-saved)
1577     ("Reset to Factory" . custom-group-reset-factory))
1578   "Alist of actions for the `custom-group' widget.
1579 The key is a string containing the name of the action, the value is a
1580 lisp function taking the widget as an element which will be called
1581 when the action is chosen.")
1582
1583 (defun custom-group-action (widget &optional event)
1584   "Show the menu for `custom-group' WIDGET.
1585 Optional EVENT is the location for the menu."
1586   (if (eq (widget-get widget :custom-state) 'hidden)
1587       (progn
1588         (widget-put widget :custom-state 'unknown)
1589         (custom-redraw widget))
1590     (let* ((completion-ignore-case t)
1591            (answer (widget-choose (custom-unlispify-tag-name
1592                                    (widget-get widget :value))
1593                                   custom-group-menu
1594                                   event)))
1595       (if answer
1596           (funcall answer widget)))))
1597
1598 (defun custom-group-set (widget)
1599   "Set changes in all modified group members."
1600   (let ((children (widget-get widget :children)))
1601     (mapcar (lambda (child)
1602               (when (eq (widget-get child :custom-state) 'modified)
1603                 (widget-apply child :custom-set)))
1604             children )))
1605
1606 (defun custom-group-save (widget)
1607   "Save all modified group members."
1608   (let ((children (widget-get widget :children)))
1609     (mapcar (lambda (child)
1610               (when (memq (widget-get child :custom-state) '(modified set))
1611                 (widget-apply child :custom-save)))
1612             children )))
1613
1614 (defun custom-group-reset-current (widget)
1615   "Reset all modified group members."
1616   (let ((children (widget-get widget :children)))
1617     (mapcar (lambda (child)
1618               (when (eq (widget-get child :custom-state) 'modified)
1619                 (widget-apply child :custom-reset-current)))
1620             children )))
1621
1622 (defun custom-group-reset-saved (widget)
1623   "Reset all modified or set group members."
1624   (let ((children (widget-get widget :children)))
1625     (mapcar (lambda (child)
1626               (when (memq (widget-get child :custom-state) '(modified set))
1627                 (widget-apply child :custom-reset-saved)))
1628             children )))
1629
1630 (defun custom-group-reset-factory (widget)
1631   "Reset all modified, set, or saved group members."
1632   (let ((children (widget-get widget :children)))
1633     (mapcar (lambda (child)
1634               (when (memq (widget-get child :custom-state)
1635                           '(modified set saved))
1636                 (widget-apply child :custom-reset-factory)))
1637             children )))
1638
1639 (defun custom-group-state-update (widget)
1640   "Update magic."
1641   (unless (eq (widget-get widget :custom-state) 'hidden)
1642     (let* ((children (widget-get widget :children))
1643            (states (mapcar (lambda (child)
1644                              (widget-get child :custom-state))
1645                            children))
1646            (magics custom-magic-alist)
1647            (found 'factory))
1648       (while magics
1649         (let ((magic (car (car magics))))
1650           (if (and (not (eq magic 'hidden))
1651                    (memq magic states))
1652               (setq found magic
1653                     magics nil)
1654             (setq magics (cdr magics)))))
1655       (widget-put widget :custom-state found)))
1656   (custom-magic-reset widget))
1657
1658 ;;; The `custom-save-all' Function.
1659
1660 (defcustom custom-file "~/.emacs"
1661   "File used for storing customization information.
1662 If you change this from the default \"~/.emacs\" you need to
1663 explicitly load that file for the settings to take effect."
1664   :type 'file
1665   :group 'customize)
1666
1667 (defun custom-save-delete (symbol)
1668   "Delete the call to SYMBOL form `custom-file'.
1669 Leave point at the location of the call, or after the last expression."
1670   (set-buffer (find-file-noselect custom-file))
1671   (goto-char (point-min))
1672   (catch 'found
1673     (while t
1674       (let ((sexp (condition-case nil
1675                       (read (current-buffer))
1676                     (end-of-file (throw 'found nil)))))
1677         (when (and (listp sexp)
1678                    (eq (car sexp) symbol))
1679           (delete-region (save-excursion
1680                            (backward-sexp)
1681                            (point))
1682                          (point))
1683           (throw 'found nil))))))
1684
1685 (defun custom-save-variables ()
1686   "Save all customized variables in `custom-file'."
1687   (save-excursion
1688     (custom-save-delete 'custom-set-variables)
1689     (let ((standard-output (current-buffer)))
1690       (unless (bolp)
1691         (princ "\n"))
1692       (princ "(custom-set-variables")
1693       (mapatoms (lambda (symbol)
1694                   (let ((value (get symbol 'saved-value)))
1695                     (when value
1696                       (princ "\n '(")
1697                       (princ symbol)
1698                       (princ " ")
1699                       (prin1 (car value))
1700                       (if (or (get symbol 'factory-value)
1701                               (and (not (boundp symbol))
1702                                    (not (get symbol 'force-value))))
1703                           (princ ")")
1704                         (princ " t)"))))))
1705       (princ ")")
1706       (unless (eolp)
1707         (princ "\n")))))
1708
1709 (defun custom-save-faces ()
1710   "Save all customized faces in `custom-file'."
1711   (save-excursion
1712     (custom-save-delete 'custom-set-faces)
1713     (let ((standard-output (current-buffer)))
1714       (unless (bolp)
1715         (princ "\n"))
1716       (princ "(custom-set-faces")
1717       (mapatoms (lambda (symbol)
1718                   (let ((value (get symbol 'saved-face)))
1719                     (when value
1720                       (princ "\n '(")
1721                       (princ symbol)
1722                       (princ " ")
1723                       (prin1 value)
1724                       (if (or (get symbol 'factory-face)
1725                               (and (not (custom-facep symbol))
1726                                    (not (get symbol 'force-face))))
1727                           (princ ")")
1728                         (princ " t)"))))))
1729       (princ ")")
1730       (unless (eolp)
1731         (princ "\n")))))
1732
1733 (defun custom-save-all ()
1734   "Save all customizations in `custom-file'."
1735   (custom-save-variables)
1736   (custom-save-faces)
1737   (save-excursion
1738     (set-buffer (find-file-noselect custom-file))
1739     (save-buffer)))
1740
1741 ;;; The Customize Menu.
1742
1743 (defcustom custom-menu-nesting 2
1744   "Maximum nesting in custom menus."
1745   :type 'integer
1746   :group 'customize)
1747
1748 (defun custom-face-menu-create (widget symbol)
1749   "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
1750   (vector (custom-unlispify-menu-entry symbol)
1751           `(custom-buffer-create '((,symbol custom-face)))
1752           t))
1753
1754 (defun custom-variable-menu-create (widget symbol)
1755   "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
1756   (let ((type (get symbol 'custom-type)))
1757     (unless (listp type)
1758       (setq type (list type)))
1759     (if (and type (widget-get type :custom-menu))
1760         (widget-apply type :custom-menu symbol)
1761       (vector (custom-unlispify-menu-entry symbol)
1762               `(custom-buffer-create '((,symbol custom-variable)))
1763               t))))
1764
1765 (widget-put (get 'boolean 'widget-type)
1766             :custom-menu (lambda (widget symbol)
1767                            (vector (custom-unlispify-menu-entry symbol)
1768                                    `(custom-buffer-create
1769                                      '((,symbol custom-variable)))
1770                                    ':style 'toggle
1771                                    ':selected symbol)))
1772
1773 (defun custom-group-menu-create (widget symbol)
1774   "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
1775   (custom-menu-create symbol))
1776
1777 (defun custom-menu-create (symbol &optional name)
1778   "Create menu for customization group SYMBOL.
1779 If optional NAME is given, use that as the name of the menu.
1780 Otherwise make up a name from SYMBOL.
1781 The menu is in a format applicable to `easy-menu-define'."
1782   (unless name
1783     (setq name (custom-unlispify-menu-entry symbol)))
1784   (let ((item (vector name
1785                       `(custom-buffer-create '((,symbol custom-group)))
1786                       t)))
1787     (if (and (> custom-menu-nesting 0)
1788              (< (length (get symbol 'custom-group)) widget-menu-max-size))
1789         (let ((custom-menu-nesting (1- custom-menu-nesting))
1790               (custom-prefix-list (custom-prefix-add symbol
1791                                                      custom-prefix-list)))
1792           (custom-load-symbol symbol)
1793           `(,(custom-unlispify-menu-entry symbol t)
1794             ,item
1795             "--"
1796             ,@(mapcar (lambda (entry)
1797                         (widget-apply (if (listp (nth 1 entry))
1798                                           (nth 1 entry)
1799                                         (list (nth 1 entry)))
1800                                       :custom-menu (nth 0 entry)))
1801                       (get symbol 'custom-group))))
1802       item)))
1803
1804 ;;;###autoload
1805 (defun custom-menu-update ()
1806   "Update customize menu."
1807   (interactive)
1808   (add-hook 'custom-define-hook 'custom-menu-reset)
1809   (let ((menu `(,(car custom-help-menu)
1810                 ,(widget-apply '(custom-group) :custom-menu 'emacs)
1811                 ,@(cdr (cdr custom-help-menu)))))
1812     (if (fboundp 'add-submenu)
1813         (add-submenu '("Help") menu)
1814       (define-key global-map [menu-bar help-menu customize-menu]
1815         (cons (car menu) (easy-menu-create-keymaps (car menu) (cdr menu)))))))
1816
1817 ;;; Dependencies.
1818
1819 ;;;###autoload
1820 (defun custom-make-dependencies ()
1821   "Batch function to extract custom dependencies from .el files.
1822 Usage: emacs -batch *.el -f custom-make-dependencies > deps.el"
1823   (let ((buffers (buffer-list)))
1824     (while buffers
1825       (set-buffer (car buffers))
1826       (setq buffers (cdr buffers))
1827       (let ((file (buffer-file-name)))
1828         (when (and file (string-match "\\`\\(.*\\)\\.el\\'" file))
1829           (goto-char (point-min))
1830           (condition-case nil
1831               (let ((name (file-name-nondirectory (match-string 1 file))))
1832                 (while t
1833                   (let ((expr (read (current-buffer))))
1834                     (when (and (listp expr)
1835                                (memq (car expr) '(defcustom defface defgroup)))
1836                       (eval expr)
1837                       (put (nth 1 expr) 'custom-where name)))))
1838             (error nil))))))
1839   (mapatoms (lambda (symbol)
1840               (let ((members (get symbol 'custom-group))
1841                     item where found)
1842                 (when members
1843                   (princ "(put '")
1844                   (princ symbol)
1845                   (princ " 'custom-loads '(")
1846                   (while members
1847                     (setq item (car (car members))
1848                           members (cdr members)
1849                           where (get item 'custom-where))
1850                     (unless (or (null where)
1851                                 (member where found))
1852                       (when found
1853                         (princ " "))
1854                       (prin1 where)
1855                       (push where found)))
1856                   (princ "))\n"))))))
1857
1858 ;;; The End.
1859
1860 (provide 'cus-edit)
1861
1862 ;; cus-edit.el ends here