(replace_buffer_in_all_windows):
[bpt/emacs.git] / lisp / cus-edit.el
1 ;;; cus-edit.el --- Tools for customizating Emacs and Lisp packages.
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.9954
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28 ;;
29 ;; This file implements the code to create and edit customize buffers.
30 ;;
31 ;; See `custom.el'.
32
33 ;; No commands should have names starting with `custom-' because
34 ;; that interferes with completion. Use `customize-' for commands
35 ;; that the user will run with M-x, and `Custom-' for interactive commands.
36
37 ;;; Code:
38
39 (require 'cus-face)
40 (require 'wid-edit)
41 (require 'easymenu)
42 (eval-when-compile (require 'cl))
43
44 (condition-case nil
45 (require 'cus-load)
46 (error nil))
47
48 (condition-case nil
49 (require 'cus-start)
50 (error nil))
51
52 (define-widget-keywords :custom-last :custom-prefix :custom-category
53 :custom-prefixes :custom-menu
54 :custom-show
55 :custom-magic :custom-state :custom-level :custom-form
56 :custom-set :custom-save :custom-reset-current :custom-reset-saved
57 :custom-reset-standard)
58
59 (put 'custom-define-hook 'custom-type 'hook)
60 (put 'custom-define-hook 'standard-value '(nil))
61 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
62
63 ;;; Customization Groups.
64
65 (defgroup emacs nil
66 "Customization of the One True Editor."
67 :link '(custom-manual "(emacs)Top"))
68
69 ;; Most of these groups are stolen from `finder.el',
70 (defgroup editing nil
71 "Basic text editing facilities."
72 :group 'emacs)
73
74 (defgroup abbrev nil
75 "Abbreviation handling, typing shortcuts, macros."
76 :tag "Abbreviations"
77 :group 'editing)
78
79 (defgroup matching nil
80 "Various sorts of searching and matching."
81 :group 'editing)
82
83 (defgroup emulations nil
84 "Emulations of other editors."
85 :group 'editing)
86
87 (defgroup mouse nil
88 "Mouse support."
89 :group 'editing)
90
91 (defgroup outlines nil
92 "Support for hierarchical outlining."
93 :group 'editing)
94
95 (defgroup external nil
96 "Interfacing to external utilities."
97 :group 'emacs)
98
99 (defgroup bib nil
100 "Code related to the `bib' bibliography processor."
101 :tag "Bibliography"
102 :group 'external)
103
104 (defgroup processes nil
105 "Process, subshell, compilation, and job control support."
106 :group 'external
107 :group 'development)
108
109 (defgroup programming nil
110 "Support for programming in other languages."
111 :group 'emacs)
112
113 (defgroup languages nil
114 "Specialized modes for editing programming languages."
115 :group 'programming)
116
117 (defgroup lisp nil
118 "Lisp support, including Emacs Lisp."
119 :group 'languages
120 :group 'development)
121
122 (defgroup c nil
123 "Support for the C language and related languages."
124 :group 'languages)
125
126 (defgroup tools nil
127 "Programming tools."
128 :group 'programming)
129
130 (defgroup oop nil
131 "Support for object-oriented programming."
132 :group 'programming)
133
134 (defgroup applications nil
135 "Applications written in Emacs."
136 :group 'emacs)
137
138 (defgroup calendar nil
139 "Calendar and time management support."
140 :group 'applications)
141
142 (defgroup mail nil
143 "Modes for electronic-mail handling."
144 :group 'applications)
145
146 (defgroup news nil
147 "Support for netnews reading and posting."
148 :group 'applications)
149
150 (defgroup games nil
151 "Games, jokes and amusements."
152 :group 'applications)
153
154 (defgroup development nil
155 "Support for further development of Emacs."
156 :group 'emacs)
157
158 (defgroup docs nil
159 "Support for Emacs documentation."
160 :group 'development)
161
162 (defgroup extensions nil
163 "Emacs Lisp language extensions."
164 :group 'development)
165
166 (defgroup internal nil
167 "Code for Emacs internals, build process, defaults."
168 :group 'development)
169
170 (defgroup maint nil
171 "Maintenance aids for the Emacs development group."
172 :tag "Maintenance"
173 :group 'development)
174
175 (defgroup environment nil
176 "Fitting Emacs with its environment."
177 :group 'emacs)
178
179 (defgroup comm nil
180 "Communications, networking, remote access to files."
181 :tag "Communication"
182 :group 'environment)
183
184 (defgroup hardware nil
185 "Support for interfacing with exotic hardware."
186 :group 'environment)
187
188 (defgroup terminals nil
189 "Support for terminal types."
190 :group 'environment)
191
192 (defgroup unix nil
193 "Front-ends/assistants for, or emulators of, UNIX features."
194 :group 'environment)
195
196 (defgroup vms nil
197 "Support code for vms."
198 :group 'environment)
199
200 (defgroup i18n nil
201 "Internationalization and alternate character-set support."
202 :group 'environment
203 :group 'editing)
204
205 (defgroup x nil
206 "The X Window system."
207 :group 'environment)
208
209 (defgroup frames nil
210 "Support for Emacs frames and window systems."
211 :group 'environment)
212
213 (defgroup data nil
214 "Support editing files of data."
215 :group 'emacs)
216
217 (defgroup files nil
218 "Support editing files."
219 :group 'emacs)
220
221 (defgroup wp nil
222 "Word processing."
223 :group 'emacs)
224
225 (defgroup tex nil
226 "Code related to the TeX formatter."
227 :group 'wp)
228
229 (defgroup faces nil
230 "Support for multiple fonts."
231 :group 'emacs)
232
233 (defgroup hypermedia nil
234 "Support for links between text or other media types."
235 :group 'emacs)
236
237 (defgroup help nil
238 "Support for on-line help systems."
239 :group 'emacs)
240
241 (defgroup local nil
242 "Code local to your site."
243 :group 'emacs)
244
245 (defgroup customize '((widgets custom-group))
246 "Customization of the Customization support."
247 :link '(custom-manual "(custom)Top")
248 :link '(url-link :tag "Development Page"
249 "http://www.dina.kvl.dk/~abraham/custom/")
250 :prefix "custom-"
251 :group 'help)
252
253 (defgroup custom-faces nil
254 "Faces used by customize."
255 :group 'customize
256 :group 'faces)
257
258 (defgroup custom-browse nil
259 "Control customize browser."
260 :prefix "custom-"
261 :group 'customize)
262
263 (defgroup custom-buffer nil
264 "Control customize buffers."
265 :prefix "custom-"
266 :group 'customize)
267
268 (defgroup custom-menu nil
269 "Control customize menus."
270 :prefix "custom-"
271 :group 'customize)
272
273 (defgroup abbrev-mode nil
274 "Word abbreviations mode."
275 :group 'abbrev)
276
277 (defgroup alloc nil
278 "Storage allocation and gc for GNU Emacs Lisp interpreter."
279 :tag "Storage Allocation"
280 :group 'internal)
281
282 (defgroup undo nil
283 "Undoing changes in buffers."
284 :group 'editing)
285
286 (defgroup modeline nil
287 "Content of the modeline."
288 :group 'environment)
289
290 (defgroup fill nil
291 "Indenting and filling text."
292 :group 'editing)
293
294 (defgroup editing-basics nil
295 "Most basic editing facilities."
296 :group 'editing)
297
298 (defgroup display nil
299 "How characters are displayed in buffers."
300 :group 'environment)
301
302 (defgroup execute nil
303 "Executing external commands."
304 :group 'processes)
305
306 (defgroup installation nil
307 "The Emacs installation."
308 :group 'environment)
309
310 (defgroup dired nil
311 "Directory editing."
312 :group 'environment)
313
314 (defgroup limits nil
315 "Internal Emacs limits."
316 :group 'internal)
317
318 (defgroup debug nil
319 "Debugging Emacs itself."
320 :group 'development)
321
322 (defgroup minibuffer nil
323 "Controling the behaviour of the minibuffer."
324 :group 'environment)
325
326 (defgroup keyboard nil
327 "Input from the keyboard."
328 :group 'environment)
329
330 (defgroup mouse nil
331 "Input from the mouse."
332 :group 'environment)
333
334 (defgroup menu nil
335 "Input from the menus."
336 :group 'environment)
337
338 (defgroup auto-save nil
339 "Preventing accidential loss of data."
340 :group 'files)
341
342 (defgroup processes-basics nil
343 "Basic stuff dealing with processes."
344 :group 'processes)
345
346 (defgroup mule nil
347 "MULE Emacs internationalization."
348 :group 'i18n)
349
350 (defgroup windows nil
351 "Windows within a frame."
352 :group 'environment)
353
354 ;;; Utilities.
355
356 (defun custom-last (x &optional n)
357 ;; Stolen from `cl.el'.
358 "Returns the last link in the list LIST.
359 With optional argument N, returns Nth-to-last link (default 1)."
360 (if n
361 (let ((m 0) (p x))
362 (while (consp p) (incf m) (pop p))
363 (if (<= n 0) p
364 (if (< n m) (nthcdr (- m n) x) x)))
365 (while (consp (cdr x)) (pop x))
366 x))
367
368 (defun custom-quote (sexp)
369 "Quote SEXP iff it is not self quoting."
370 (if (or (memq sexp '(t nil))
371 (and (symbolp sexp)
372 (eq (aref (symbol-name sexp) 0) ?:))
373 (and (listp sexp)
374 (memq (car sexp) '(lambda)))
375 (stringp sexp)
376 (numberp sexp)
377 (and (fboundp 'characterp)
378 (characterp sexp)))
379 sexp
380 (list 'quote sexp)))
381
382 (defun custom-split-regexp-maybe (regexp)
383 "If REGEXP is a string, split it to a list at `\\|'.
384 You can get the original back with from the result with:
385 (mapconcat 'identity result \"\\|\")
386
387 IF REGEXP is not a string, return it unchanged."
388 (if (stringp regexp)
389 (let ((start 0)
390 all)
391 (while (string-match "\\\\|" regexp start)
392 (setq all (cons (substring regexp start (match-beginning 0)) all)
393 start (match-end 0)))
394 (nreverse (cons (substring regexp start) all)))
395 regexp))
396
397 (defun custom-variable-prompt ()
398 ;; Code stolen from `help.el'.
399 "Prompt for a variable, defaulting to the variable at point.
400 Return a list suitable for use in `interactive'."
401 (let ((v (variable-at-point))
402 (enable-recursive-minibuffers t)
403 val)
404 (setq val (completing-read
405 (if (symbolp v)
406 (format "Customize option: (default %s) " v)
407 "Customize variable: ")
408 obarray (lambda (symbol)
409 (and (boundp symbol)
410 (or (get symbol 'custom-type)
411 (user-variable-p symbol))))))
412 (list (if (equal val "")
413 (if (symbolp v) v nil)
414 (intern val)))))
415
416 (defun custom-menu-filter (menu widget)
417 "Convert MENU to the form used by `widget-choose'.
418 MENU should be in the same format as `custom-variable-menu'.
419 WIDGET is the widget to apply the filter entries of MENU on."
420 (let ((result nil)
421 current name action filter)
422 (while menu
423 (setq current (car menu)
424 name (nth 0 current)
425 action (nth 1 current)
426 filter (nth 2 current)
427 menu (cdr menu))
428 (if (or (null filter) (funcall filter widget))
429 (push (cons name action) result)
430 (push name result)))
431 (nreverse result)))
432
433 ;;; Unlispify.
434
435 (defvar custom-prefix-list nil
436 "List of prefixes that should be ignored by `custom-unlispify'")
437
438 (defcustom custom-unlispify-menu-entries t
439 "Display menu entries as words instead of symbols if non nil."
440 :group 'custom-menu
441 :type 'boolean)
442
443 (defcustom custom-unlispify-remove-prefixes nil
444 "Non-nil means remove group prefixes from option names in buffer."
445 :group 'custom-menu
446 :type 'boolean)
447
448 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
449 "Convert symbol into a menu entry."
450 (cond ((not custom-unlispify-menu-entries)
451 (symbol-name symbol))
452 ((get symbol 'custom-tag)
453 (if no-suffix
454 (get symbol 'custom-tag)
455 (concat (get symbol 'custom-tag) "...")))
456 (t
457 (save-excursion
458 (set-buffer (get-buffer-create " *Custom-Work*"))
459 (erase-buffer)
460 (princ symbol (current-buffer))
461 (goto-char (point-min))
462 (when (and (eq (get symbol 'custom-type) 'boolean)
463 (re-search-forward "-p\\'" nil t))
464 (replace-match "" t t)
465 (goto-char (point-min)))
466 (if custom-unlispify-remove-prefixes
467 (let ((prefixes custom-prefix-list)
468 prefix)
469 (while prefixes
470 (setq prefix (car prefixes))
471 (if (search-forward prefix (+ (point) (length prefix)) t)
472 (progn
473 (setq prefixes nil)
474 (delete-region (point-min) (point)))
475 (setq prefixes (cdr prefixes))))))
476 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
477 (capitalize-region (point-min) (point-max))
478 (unless no-suffix
479 (goto-char (point-max))
480 (insert "..."))
481 (buffer-string)))))
482
483 (defcustom custom-unlispify-tag-names t
484 "Display tag names as words instead of symbols if non nil."
485 :group 'custom-buffer
486 :type 'boolean)
487
488 (defun custom-unlispify-tag-name (symbol)
489 "Convert symbol into a menu entry."
490 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
491 (custom-unlispify-menu-entry symbol t)))
492
493 (defun custom-prefix-add (symbol prefixes)
494 ;; Addd SYMBOL to list of ignored PREFIXES.
495 (cons (or (get symbol 'custom-prefix)
496 (concat (symbol-name symbol) "-"))
497 prefixes))
498
499 ;;; Guess.
500
501 (defcustom custom-guess-name-alist
502 '(("-p\\'" boolean)
503 ("-hook\\'" hook)
504 ("-face\\'" face)
505 ("-file\\'" file)
506 ("-function\\'" function)
507 ("-functions\\'" (repeat function))
508 ("-list\\'" (repeat sexp))
509 ("-alist\\'" (repeat (cons sexp sexp))))
510 "Alist of (MATCH TYPE).
511
512 MATCH should be a regexp matching the name of a symbol, and TYPE should
513 be a widget suitable for editing the value of that symbol. The TYPE
514 of the first entry where MATCH matches the name of the symbol will be
515 used.
516
517 This is used for guessing the type of variables not declared with
518 customize."
519 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
520 :group 'customize)
521
522 (defcustom custom-guess-doc-alist
523 '(("\\`\\*?Non-nil " boolean))
524 "Alist of (MATCH TYPE).
525
526 MATCH should be a regexp matching a documentation string, and TYPE
527 should be a widget suitable for editing the value of a variable with
528 that documentation string. The TYPE of the first entry where MATCH
529 matches the name of the symbol will be used.
530
531 This is used for guessing the type of variables not declared with
532 customize."
533 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
534 :group 'customize)
535
536 (defun custom-guess-type (symbol)
537 "Guess a widget suitable for editing the value of SYMBOL.
538 This is done by matching SYMBOL with `custom-guess-name-alist' and
539 if that fails, the doc string with `custom-guess-doc-alist'."
540 (let ((name (symbol-name symbol))
541 (names custom-guess-name-alist)
542 current found)
543 (while names
544 (setq current (car names)
545 names (cdr names))
546 (when (string-match (nth 0 current) name)
547 (setq found (nth 1 current)
548 names nil)))
549 (unless found
550 (let ((doc (documentation-property symbol 'variable-documentation))
551 (docs custom-guess-doc-alist))
552 (when doc
553 (while docs
554 (setq current (car docs)
555 docs (cdr docs))
556 (when (string-match (nth 0 current) doc)
557 (setq found (nth 1 current)
558 docs nil))))))
559 found))
560
561 ;;; Sorting.
562
563 (defcustom custom-browse-sort-alphabetically nil
564 "If non-nil, sort members of each customization group alphabetically."
565 :type 'boolean
566 :group 'custom-browse)
567
568 (defcustom custom-browse-order-groups nil
569 "If non-nil, order group members within each customization group.
570 If `first', order groups before non-groups.
571 If `last', order groups after non-groups."
572 :type '(choice (const first)
573 (const last)
574 (const :tag "none" nil))
575 :group 'custom-browse)
576
577 (defcustom custom-browse-only-groups nil
578 "If non-nil, show group members only within each customization group."
579 :type 'boolean
580 :group 'custom-browse)
581
582 (defcustom custom-buffer-sort-alphabetically nil
583 "If non-nil, sort members of each customization group alphabetically."
584 :type 'boolean
585 :group 'custom-buffer)
586
587 (defcustom custom-buffer-order-groups 'last
588 "If non-nil, order group members within each customization group.
589 If `first', order groups before non-groups.
590 If `last', order groups after non-groups."
591 :type '(choice (const first)
592 (const last)
593 (const :tag "none" nil))
594 :group 'custom-buffer)
595
596 (defcustom custom-menu-sort-alphabetically nil
597 "If non-nil, sort members of each customization group alphabetically."
598 :type 'boolean
599 :group 'custom-menu)
600
601 (defcustom custom-menu-order-groups 'first
602 "If non-nil, order group members within each customization group.
603 If `first', order groups before non-groups.
604 If `last', order groups after non-groups."
605 :type '(choice (const first)
606 (const last)
607 (const :tag "none" nil))
608 :group 'custom-menu)
609
610 (defun custom-sort-items (items sort-alphabetically order-groups)
611 "Return a sorted copy of ITEMS.
612 ITEMS should be a `custom-group' property.
613 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
614 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
615 groups after non-groups, if nil do not order groups at all."
616 (sort (copy-sequence items)
617 (lambda (a b)
618 (let ((typea (nth 1 a)) (typeb (nth 1 b))
619 (namea (symbol-name (nth 0 a))) (nameb (symbol-name (nth 0 b))))
620 (cond ((not order-groups)
621 ;; Since we don't care about A and B order, maybe sort.
622 (when sort-alphabetically
623 (string-lessp namea nameb)))
624 ((eq typea 'custom-group)
625 ;; If B is also a group, maybe sort. Otherwise, order A and B.
626 (if (eq typeb 'custom-group)
627 (when sort-alphabetically
628 (string-lessp namea nameb))
629 (eq order-groups 'first)))
630 ((eq typeb 'custom-group)
631 ;; Since A cannot be a group, order A and B.
632 (eq order-groups 'last))
633 (sort-alphabetically
634 ;; Since A and B cannot be groups, sort.
635 (string-lessp namea nameb)))))))
636
637 ;;; Custom Mode Commands.
638
639 (defvar custom-options nil
640 "Customization widgets in the current buffer.")
641
642 (defun Custom-set ()
643 "Set changes in all modified options."
644 (interactive)
645 (let ((children custom-options))
646 (mapcar (lambda (child)
647 (when (eq (widget-get child :custom-state) 'modified)
648 (widget-apply child :custom-set)))
649 children)))
650
651 (defun Custom-save ()
652 "Set all modified group members and save them."
653 (interactive)
654 (let ((children custom-options))
655 (mapcar (lambda (child)
656 (when (memq (widget-get child :custom-state) '(modified set))
657 (widget-apply child :custom-save)))
658 children))
659 (custom-save-all))
660
661 (defvar custom-reset-menu
662 '(("Current" . Custom-reset-current)
663 ("Saved" . Custom-reset-saved)
664 ("Standard Settings" . Custom-reset-standard))
665 "Alist of actions for the `Reset' button.
666 The key is a string containing the name of the action, the value is a
667 lisp function taking the widget as an element which will be called
668 when the action is chosen.")
669
670 (defun custom-reset (event)
671 "Select item from reset menu."
672 (let* ((completion-ignore-case t)
673 (answer (widget-choose "Reset to"
674 custom-reset-menu
675 event)))
676 (if answer
677 (funcall answer))))
678
679 (defun Custom-reset-current (&rest ignore)
680 "Reset all modified group members to their current value."
681 (interactive)
682 (let ((children custom-options))
683 (mapcar (lambda (child)
684 (when (eq (widget-get child :custom-state) 'modified)
685 (widget-apply child :custom-reset-current)))
686 children)))
687
688 (defun Custom-reset-saved (&rest ignore)
689 "Reset all modified or set group members to their saved value."
690 (interactive)
691 (let ((children custom-options))
692 (mapcar (lambda (child)
693 (when (eq (widget-get child :custom-state) 'modified)
694 (widget-apply child :custom-reset-saved)))
695 children)))
696
697 (defun Custom-reset-standard (&rest ignore)
698 "Reset all modified, set, or saved group members to their standard settings."
699 (interactive)
700 (let ((children custom-options))
701 (mapcar (lambda (child)
702 (when (eq (widget-get child :custom-state) 'modified)
703 (widget-apply child :custom-reset-standard)))
704 children)))
705
706 ;;; The Customize Commands
707
708 (defun custom-prompt-variable (prompt-var prompt-val)
709 "Prompt for a variable and a value and return them as a list.
710 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
711 prompt for the value. The %s escape in PROMPT-VAL is replaced with
712 the name of the variable.
713
714 If the variable has a `variable-interactive' property, that is used as if
715 it were the arg to `interactive' (which see) to interactively read the value.
716
717 If the variable has a `custom-type' property, it must be a widget and the
718 `:prompt-value' property of that widget will be used for reading the value."
719 (let* ((var (read-variable prompt-var))
720 (minibuffer-help-form '(describe-variable var)))
721 (list var
722 (let ((prop (get var 'variable-interactive))
723 (type (get var 'custom-type))
724 (prompt (format prompt-val var)))
725 (unless (listp type)
726 (setq type (list type)))
727 (cond (prop
728 ;; Use VAR's `variable-interactive' property
729 ;; as an interactive spec for prompting.
730 (call-interactively (list 'lambda '(arg)
731 (list 'interactive prop)
732 'arg)))
733 (type
734 (widget-prompt-value type
735 prompt
736 (if (boundp var)
737 (symbol-value var))
738 (not (boundp var))))
739 (t
740 (eval-minibuffer prompt)))))))
741
742 ;;;###autoload
743 (defun customize-set-value (var val)
744 "Set VARIABLE to VALUE. VALUE is a Lisp object.
745
746 If VARIABLE has a `variable-interactive' property, that is used as if
747 it were the arg to `interactive' (which see) to interactively read the value.
748
749 If VARIABLE has a `custom-type' property, it must be a widget and the
750 `:prompt-value' property of that widget will be used for reading the value."
751 (interactive (custom-prompt-variable "Set variable: "
752 "Set %s to value: "))
753
754 (set var val))
755
756 ;;;###autoload
757 (defun customize-set-variable (var val)
758 "Set the default for VARIABLE to VALUE. VALUE is a Lisp object.
759
760 If VARIABLE has a `custom-set' property, that is used for setting
761 VARIABLE, otherwise `set-default' is used.
762
763 The `customized-value' property of the VARIABLE will be set to a list
764 with a quoted VALUE as its sole list member.
765
766 If VARIABLE has a `variable-interactive' property, that is used as if
767 it were the arg to `interactive' (which see) to interactively read the value.
768
769 If VARIABLE has a `custom-type' property, it must be a widget and the
770 `:prompt-value' property of that widget will be used for reading the value. "
771 (interactive (custom-prompt-variable "Set variable: "
772 "Set customized value for %s to: "))
773 (funcall (or (get var 'custom-set) 'set-default) var val)
774 (put var 'customized-value (list (custom-quote val))))
775
776 ;;;###autoload
777 (defun customize-save-variable (var val)
778 "Set the default for VARIABLE to VALUE, and save it for future sessions.
779 If VARIABLE has a `custom-set' property, that is used for setting
780 VARIABLE, otherwise `set-default' is used.
781
782 The `customized-value' property of the VARIABLE will be set to a list
783 with a quoted VALUE as its sole list member.
784
785 If VARIABLE has a `variable-interactive' property, that is used as if
786 it were the arg to `interactive' (which see) to interactively read the value.
787
788 If VARIABLE has a `custom-type' property, it must be a widget and the
789 `:prompt-value' property of that widget will be used for reading the value. "
790 (interactive (custom-prompt-variable "Set and ave variable: "
791 "Set and save value for %s as: "))
792 (funcall (or (get var 'custom-set) 'set-default) var val)
793 (put var 'saved-value (list (custom-quote val)))
794 (custom-save-all))
795
796 ;;;###autoload
797 (defun customize ()
798 "Select a customization buffer which you can use to set user options.
799 User options are structured into \"groups\".
800 Initially the top-level group `Emacs' and its immediate subgroups
801 are shown; the contents of those subgroups are initially hidden."
802 (interactive)
803 (customize-group 'emacs))
804
805 ;;;###autoload
806 (defun customize-group (group)
807 "Customize GROUP, which must be a customization group."
808 (interactive (list (let ((completion-ignore-case t))
809 (completing-read "Customize group: (default emacs) "
810 obarray
811 (lambda (symbol)
812 (or (get symbol 'custom-loads)
813 (get symbol 'custom-group)))
814 t))))
815 (when (stringp group)
816 (if (string-equal "" group)
817 (setq group 'emacs)
818 (setq group (intern group))))
819 (or (get group 'custom-group)
820 (custom-load-symbol group))
821 (let ((name (format "*Customize Group: %s*"
822 (custom-unlispify-tag-name group))))
823 (if (get-buffer name)
824 (switch-to-buffer name)
825 (custom-buffer-create (list (list group 'custom-group))
826 name
827 (concat " for group "
828 (custom-unlispify-tag-name group))))))
829
830 ;;;###autoload
831 (defun customize-group-other-window (symbol)
832 "Customize SYMBOL, which must be a customization group."
833 (interactive (list (completing-read "Customize group: (default emacs) "
834 obarray
835 (lambda (symbol)
836 (get symbol 'custom-group))
837 t)))
838
839 (when (stringp symbol)
840 (if (string-equal "" symbol)
841 (setq symbol 'emacs)
842 (setq symbol (intern symbol))))
843 (custom-buffer-create-other-window
844 (list (list symbol 'custom-group))
845 (format "*Customize Group: %s*" (custom-unlispify-tag-name symbol))))
846
847 ;;;###autoload
848 (defalias 'customize-variable 'customize-option)
849
850 ;;;###autoload
851 (defun customize-option (symbol)
852 "Customize SYMBOL, which must be a user option variable."
853 (interactive (custom-variable-prompt))
854 (custom-buffer-create (list (list symbol 'custom-variable))
855 (format "*Customize Option: %s*"
856 (custom-unlispify-tag-name symbol))))
857
858 ;;;###autoload
859 (defalias 'customize-variable-other-window 'customize-option-other-window)
860
861 ;;;###autoload
862 (defun customize-option-other-window (symbol)
863 "Customize SYMBOL, which must be a user option variable.
864 Show the buffer in another window, but don't select it."
865 (interactive (custom-variable-prompt))
866 (custom-buffer-create-other-window
867 (list (list symbol 'custom-variable))
868 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
869
870 ;;;###autoload
871 (defun customize-face (&optional symbol)
872 "Customize SYMBOL, which should be a face name or nil.
873 If SYMBOL is nil, customize all faces."
874 (interactive (list (completing-read "Customize face: (default all) "
875 obarray 'custom-facep)))
876 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
877 (custom-buffer-create (custom-sort-items
878 (mapcar (lambda (symbol)
879 (list symbol 'custom-face))
880 (face-list))
881 t nil)
882 "*Customize Faces*")
883 (when (stringp symbol)
884 (setq symbol (intern symbol)))
885 (unless (symbolp symbol)
886 (error "Should be a symbol %S" symbol))
887 (custom-buffer-create (list (list symbol 'custom-face))
888 (format "*Customize Face: %s*"
889 (custom-unlispify-tag-name symbol)))))
890
891 ;;;###autoload
892 (defun customize-face-other-window (&optional symbol)
893 "Show customization buffer for FACE in other window."
894 (interactive (list (completing-read "Customize face: "
895 obarray 'custom-facep)))
896 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
897 ()
898 (if (stringp symbol)
899 (setq symbol (intern symbol)))
900 (unless (symbolp symbol)
901 (error "Should be a symbol %S" symbol))
902 (custom-buffer-create-other-window
903 (list (list symbol 'custom-face))
904 (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
905
906 ;;;###autoload
907 (defun customize-customized ()
908 "Customize all user options set since the last save in this session."
909 (interactive)
910 (let ((found nil))
911 (mapatoms (lambda (symbol)
912 (and (get symbol 'customized-face)
913 (custom-facep symbol)
914 (push (list symbol 'custom-face) found))
915 (and (get symbol 'customized-value)
916 (boundp symbol)
917 (push (list symbol 'custom-variable) found))))
918 (if (not found)
919 (error "No customized user options")
920 (custom-buffer-create (custom-sort-items found t nil)
921 "*Customize Customized*"))))
922
923 ;;;###autoload
924 (defun customize-saved ()
925 "Customize all already saved user options."
926 (interactive)
927 (let ((found nil))
928 (mapatoms (lambda (symbol)
929 (and (get symbol 'saved-face)
930 (custom-facep symbol)
931 (push (list symbol 'custom-face) found))
932 (and (get symbol 'saved-value)
933 (boundp symbol)
934 (push (list symbol 'custom-variable) found))))
935 (if (not found )
936 (error "No saved user options")
937 (custom-buffer-create (custom-sort-items found t nil)
938 "*Customize Saved*"))))
939
940 ;;;###autoload
941 (defun customize-apropos (regexp &optional all)
942 "Customize all user options matching REGEXP.
943 If ALL is `options', include only options.
944 If ALL is `faces', include only faces.
945 If ALL is `groups', include only groups.
946 If ALL is t (interactively, with prefix arg), include options which are not
947 user-settable, as well as faces and groups."
948 (interactive "sCustomize regexp: \nP")
949 (let ((found nil))
950 (mapatoms (lambda (symbol)
951 (when (string-match regexp (symbol-name symbol))
952 (when (and (not (memq all '(faces options)))
953 (get symbol 'custom-group))
954 (push (list symbol 'custom-group) found))
955 (when (and (not (memq all '(options groups)))
956 (custom-facep symbol))
957 (push (list symbol 'custom-face) found))
958 (when (and (not (memq all '(groups faces)))
959 (boundp symbol)
960 (or (get symbol 'saved-value)
961 (get symbol 'standard-value)
962 (if (memq all '(nil options))
963 (user-variable-p symbol)
964 (get symbol 'variable-documentation))))
965 (push (list symbol 'custom-variable) found)))))
966 (if (not found)
967 (error "No matches")
968 (custom-buffer-create (custom-sort-items found t
969 custom-buffer-order-groups)
970 "*Customize Apropos*"))))
971
972 ;;;###autoload
973 (defun customize-apropos-options (regexp &optional arg)
974 "Customize all user options matching REGEXP.
975 With prefix arg, include options which are not user-settable."
976 (interactive "sCustomize regexp: \nP")
977 (customize-apropos regexp (or arg 'options)))
978
979 ;;;###autoload
980 (defun customize-apropos-faces (regexp)
981 "Customize all user faces matching REGEXP."
982 (interactive "sCustomize regexp: \n")
983 (customize-apropos regexp 'faces))
984
985 ;;;###autoload
986 (defun customize-apropos-groups (regexp)
987 "Customize all user groups matching REGEXP."
988 (interactive "sCustomize regexp: \n")
989 (customize-apropos regexp 'groups))
990
991 ;;; Buffer.
992
993 (defcustom custom-buffer-style 'links
994 "Control the presentation style for customization buffers.
995 The value should be a symbol, one of:
996
997 brackets: groups nest within each other with big horizontal brackets.
998 links: groups have links to subgroups."
999 :type '(radio (const brackets)
1000 (const links))
1001 :group 'custom-buffer)
1002
1003 (defcustom custom-buffer-indent 3
1004 "Number of spaces to indent nested groups."
1005 :type 'integer
1006 :group 'custom-buffer)
1007
1008 ;;;###autoload
1009 (defun custom-buffer-create (options &optional name description)
1010 "Create a buffer containing OPTIONS.
1011 Optional NAME is the name of the buffer.
1012 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1013 SYMBOL is a customization option, and WIDGET is a widget for editing
1014 that option."
1015 (unless name (setq name "*Customization*"))
1016 (kill-buffer (get-buffer-create name))
1017 (switch-to-buffer (get-buffer-create name))
1018 (custom-buffer-create-internal options description))
1019
1020 ;;;###autoload
1021 (defun custom-buffer-create-other-window (options &optional name description)
1022 "Create a buffer containing OPTIONS.
1023 Optional NAME is the name of the buffer.
1024 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1025 SYMBOL is a customization option, and WIDGET is a widget for editing
1026 that option."
1027 (unless name (setq name "*Customization*"))
1028 (kill-buffer (get-buffer-create name))
1029 (let ((window (selected-window)))
1030 (switch-to-buffer-other-window (get-buffer-create name))
1031 (custom-buffer-create-internal options description)
1032 (select-window window)))
1033
1034 (defcustom custom-reset-button-menu nil
1035 "If non-nil, only show a single reset button in customize buffers.
1036 This button will have a menu with all three reset operations."
1037 :type 'boolean
1038 :group 'custom-buffer)
1039
1040 (defun custom-buffer-create-internal (options &optional description)
1041 (message "Creating customization buffer...")
1042 (custom-mode)
1043 (widget-insert "This is a customization buffer")
1044 (if description
1045 (widget-insert description))
1046 (widget-insert ".
1047 Square brackets show active fields; type RET or click mouse-1
1048 on an active field to invoke its action. Editing an option value
1049 changes the text in the buffer; invoke the State button and
1050 choose the Set operation to set the option value.
1051 Invoke ")
1052 (widget-create 'info-link
1053 :tag "Help"
1054 :help-echo "Read the online help."
1055 "(emacs)Easy Customization")
1056 (widget-insert " for more information.\n\n")
1057 (message "Creating customization buttons...")
1058 (widget-insert "Operate on everything in this buffer:\n ")
1059 (widget-create 'push-button
1060 :tag "Set for Current Session"
1061 :help-echo "\
1062 Make your editing in this buffer take effect for this session."
1063 :action (lambda (widget &optional event)
1064 (Custom-set)))
1065 (widget-insert " ")
1066 (widget-create 'push-button
1067 :tag "Save for Future Sessions"
1068 :help-echo "\
1069 Make your editing in this buffer take effect for future Emacs sessions."
1070 :action (lambda (widget &optional event)
1071 (Custom-save)))
1072 (if custom-reset-button-menu
1073 (progn
1074 (widget-insert " ")
1075 (widget-create 'push-button
1076 :tag "Reset"
1077 :help-echo "Show a menu with reset operations."
1078 :mouse-down-action (lambda (&rest junk) t)
1079 :action (lambda (widget &optional event)
1080 (custom-reset event))))
1081 (widget-insert "\n ")
1082 (widget-create 'push-button
1083 :tag "Reset"
1084 :help-echo "\
1085 Reset all edited text in this buffer to reflect current values."
1086 :action 'Custom-reset-current)
1087 (widget-insert " ")
1088 (widget-create 'push-button
1089 :tag "Reset to Saved"
1090 :help-echo "\
1091 Reset all values in this buffer to their saved settings."
1092 :action 'Custom-reset-saved)
1093 (widget-insert " ")
1094 (widget-create 'push-button
1095 :tag "Reset to Standard"
1096 :help-echo "\
1097 Reset all values in this buffer to their standard settings."
1098 :action 'Custom-reset-standard))
1099 (widget-insert " ")
1100 (widget-create 'push-button
1101 :tag "Bury Buffer"
1102 :help-echo "Bury the buffer."
1103 :action (lambda (widget &optional event)
1104 (bury-buffer)))
1105 (widget-insert "\n\n")
1106 (message "Creating customization items...")
1107 (setq custom-options
1108 (if (= (length options) 1)
1109 (mapcar (lambda (entry)
1110 (widget-create (nth 1 entry)
1111 :documentation-shown t
1112 :custom-state 'unknown
1113 :tag (custom-unlispify-tag-name
1114 (nth 0 entry))
1115 :value (nth 0 entry)))
1116 options)
1117 (let ((count 0)
1118 (length (length options)))
1119 (mapcar (lambda (entry)
1120 (prog2
1121 (message "Creating customization items %2d%%..."
1122 (/ (* 100.0 count) length))
1123 (widget-create (nth 1 entry)
1124 :tag (custom-unlispify-tag-name
1125 (nth 0 entry))
1126 :value (nth 0 entry))
1127 (setq count (1+ count))
1128 (unless (eq (preceding-char) ?\n)
1129 (widget-insert "\n"))
1130 (widget-insert "\n")))
1131 options))))
1132 (unless (eq (preceding-char) ?\n)
1133 (widget-insert "\n"))
1134 (message "Creating customization items %2d%%...done" 100)
1135 (unless (eq custom-buffer-style 'tree)
1136 (mapcar 'custom-magic-reset custom-options))
1137 (message "Creating customization setup...")
1138 (widget-setup)
1139 (goto-char (point-min))
1140 (message "Creating customization buffer...done"))
1141
1142 ;;; The Tree Browser.
1143
1144 ;;;###autoload
1145 (defun customize-browse (&optional group)
1146 "Create a tree browser for the customize hierarchy."
1147 (interactive)
1148 (unless group
1149 (setq group 'emacs))
1150 (let ((name "*Customize Browser*"))
1151 (kill-buffer (get-buffer-create name))
1152 (switch-to-buffer (get-buffer-create name)))
1153 (custom-mode)
1154 (widget-insert "\
1155 Square brackets show active fields; type RET or click mouse-1
1156 on an active field to invoke its action.
1157 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1158 (if custom-browse-only-groups
1159 (widget-insert "\
1160 Invoke the [Group] button below to edit that item in another window.\n\n")
1161 (widget-insert "Invoke the ")
1162 (widget-create 'item
1163 :format "%t"
1164 :tag "[Group]"
1165 :tag-glyph "folder")
1166 (widget-insert ", ")
1167 (widget-create 'item
1168 :format "%t"
1169 :tag "[Face]"
1170 :tag-glyph "face")
1171 (widget-insert ", and ")
1172 (widget-create 'item
1173 :format "%t"
1174 :tag "[Option]"
1175 :tag-glyph "option")
1176 (widget-insert " buttons below to edit that
1177 item in another window.\n\n"))
1178 (let ((custom-buffer-style 'tree))
1179 (widget-create 'custom-group
1180 :custom-last t
1181 :custom-state 'unknown
1182 :tag (custom-unlispify-tag-name group)
1183 :value group))
1184 (goto-char (point-min)))
1185
1186 (define-widget 'custom-browse-visibility 'item
1187 "Control visibility of of items in the customize tree browser."
1188 :format "%[[%t]%]"
1189 :action 'custom-browse-visibility-action)
1190
1191 (defun custom-browse-visibility-action (widget &rest ignore)
1192 (let ((custom-buffer-style 'tree))
1193 (custom-toggle-parent widget)))
1194
1195 (define-widget 'custom-browse-group-tag 'push-button
1196 "Show parent in other window when activated."
1197 :tag "Group"
1198 :tag-glyph "folder"
1199 :action 'custom-browse-group-tag-action)
1200
1201 (defun custom-browse-group-tag-action (widget &rest ignore)
1202 (let ((parent (widget-get widget :parent)))
1203 (customize-group-other-window (widget-value parent))))
1204
1205 (define-widget 'custom-browse-variable-tag 'push-button
1206 "Show parent in other window when activated."
1207 :tag "Option"
1208 :tag-glyph "option"
1209 :action 'custom-browse-variable-tag-action)
1210
1211 (defun custom-browse-variable-tag-action (widget &rest ignore)
1212 (let ((parent (widget-get widget :parent)))
1213 (customize-variable-other-window (widget-value parent))))
1214
1215 (define-widget 'custom-browse-face-tag 'push-button
1216 "Show parent in other window when activated."
1217 :tag "Face"
1218 :tag-glyph "face"
1219 :action 'custom-browse-face-tag-action)
1220
1221 (defun custom-browse-face-tag-action (widget &rest ignore)
1222 (let ((parent (widget-get widget :parent)))
1223 (customize-face-other-window (widget-value parent))))
1224
1225 (defconst custom-browse-alist '((" " "space")
1226 (" | " "vertical")
1227 ("-\\ " "top")
1228 (" |-" "middle")
1229 (" `-" "bottom")))
1230
1231 (defun custom-browse-insert-prefix (prefix)
1232 "Insert PREFIX. On XEmacs convert it to line graphics."
1233 (if nil ; (string-match "XEmacs" emacs-version)
1234 (progn
1235 (insert "*")
1236 (while (not (string-equal prefix ""))
1237 (let ((entry (substring prefix 0 3)))
1238 (setq prefix (substring prefix 3))
1239 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1240 (name (nth 1 (assoc entry custom-browse-alist))))
1241 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1242 (overlay-put overlay 'start-open t)
1243 (overlay-put overlay 'end-open t)))))
1244 (insert prefix)))
1245
1246 ;;; Modification of Basic Widgets.
1247 ;;
1248 ;; We add extra properties to the basic widgets needed here. This is
1249 ;; fine, as long as we are careful to stay within out own namespace.
1250 ;;
1251 ;; We want simple widgets to be displayed by default, but complex
1252 ;; widgets to be hidden.
1253
1254 (widget-put (get 'item 'widget-type) :custom-show t)
1255 (widget-put (get 'editable-field 'widget-type)
1256 :custom-show (lambda (widget value)
1257 (let ((pp (pp-to-string value)))
1258 (cond ((string-match "\n" pp)
1259 nil)
1260 ((> (length pp) 40)
1261 nil)
1262 (t t)))))
1263 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1264
1265 ;;; The `custom-manual' Widget.
1266
1267 (define-widget 'custom-manual 'info-link
1268 "Link to the manual entry for this customization option."
1269 :help-echo "Read the manual entry for this option."
1270 :tag "Manual")
1271
1272 ;;; The `custom-magic' Widget.
1273
1274 (defgroup custom-magic-faces nil
1275 "Faces used by the magic button."
1276 :group 'custom-faces
1277 :group 'custom-buffer)
1278
1279 (defface custom-invalid-face '((((class color))
1280 (:foreground "yellow" :background "red"))
1281 (t
1282 (:bold t :italic t :underline t)))
1283 "Face used when the customize item is invalid."
1284 :group 'custom-magic-faces)
1285
1286 (defface custom-rogue-face '((((class color))
1287 (:foreground "pink" :background "black"))
1288 (t
1289 (:underline t)))
1290 "Face used when the customize item is not defined for customization."
1291 :group 'custom-magic-faces)
1292
1293 (defface custom-modified-face '((((class color))
1294 (:foreground "white" :background "blue"))
1295 (t
1296 (:italic t :bold)))
1297 "Face used when the customize item has been modified."
1298 :group 'custom-magic-faces)
1299
1300 (defface custom-set-face '((((class color))
1301 (:foreground "blue" :background "white"))
1302 (t
1303 (:italic t)))
1304 "Face used when the customize item has been set."
1305 :group 'custom-magic-faces)
1306
1307 (defface custom-changed-face '((((class color))
1308 (:foreground "white" :background "blue"))
1309 (t
1310 (:italic t)))
1311 "Face used when the customize item has been changed."
1312 :group 'custom-magic-faces)
1313
1314 (defface custom-saved-face '((t (:underline t)))
1315 "Face used when the customize item has been saved."
1316 :group 'custom-magic-faces)
1317
1318 (defconst custom-magic-alist '((nil "#" underline "\
1319 uninitialized, you should not see this.")
1320 (unknown "?" italic "\
1321 unknown, you should not see this.")
1322 (hidden "-" default "\
1323 hidden, invoke \"Show\" in the previous line to show." "\
1324 group now hidden, invoke \"Show\", above, to show contents.")
1325 (invalid "x" custom-invalid-face "\
1326 the value displayed for this %c is invalid and cannot be set.")
1327 (modified "*" custom-modified-face "\
1328 you have edited the value as text, but you have not set the %c." "\
1329 you have edited something in this group, but not set it.")
1330 (set "+" custom-set-face "\
1331 you have set this %c, but not saved it for future sessions." "\
1332 something in this group has been set, but not saved.")
1333 (changed ":" custom-changed-face "\
1334 this %c has been changed outside the customize buffer." "\
1335 something in this group has been changed outside customize.")
1336 (saved "!" custom-saved-face "\
1337 this %c has been set and saved." "\
1338 something in this group has been set and saved.")
1339 (rogue "@" custom-rogue-face "\
1340 this %c has not been changed with customize." "\
1341 something in this group is not prepared for customization.")
1342 (standard " " nil "\
1343 this %c is unchanged from its standard setting." "\
1344 visible group members are all at standard settings."))
1345 "Alist of customize option states.
1346 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1347
1348 STATE is one of the following symbols:
1349
1350 `nil'
1351 For internal use, should never occur.
1352 `unknown'
1353 For internal use, should never occur.
1354 `hidden'
1355 This item is not being displayed.
1356 `invalid'
1357 This item is modified, but has an invalid form.
1358 `modified'
1359 This item is modified, and has a valid form.
1360 `set'
1361 This item has been set but not saved.
1362 `changed'
1363 The current value of this item has been changed temporarily.
1364 `saved'
1365 This item is marked for saving.
1366 `rogue'
1367 This item has no customization information.
1368 `standard'
1369 This item is unchanged from the standard setting.
1370
1371 MAGIC is a string used to present that state.
1372
1373 FACE is a face used to present the state.
1374
1375 ITEM-DESC is a string describing the state for options.
1376
1377 GROUP-DESC is a string describing the state for groups. If this is
1378 left out, ITEM-DESC will be used.
1379
1380 The string %c in either description will be replaced with the
1381 category of the item. These are `group'. `option', and `face'.
1382
1383 The list should be sorted most significant first.")
1384
1385 (defcustom custom-magic-show 'long
1386 "If non-nil, show textual description of the state.
1387 If `long', show a full-line description, not just one word."
1388 :type '(choice (const :tag "no" nil)
1389 (const short)
1390 (const long))
1391 :group 'custom-buffer)
1392
1393 (defcustom custom-magic-show-hidden '(option face)
1394 "Control whether the State button is shown for hidden items.
1395 The value should be a list with the custom categories where the State
1396 button should be visible. Possible categories are `group', `option',
1397 and `face'."
1398 :type '(set (const group) (const option) (const face))
1399 :group 'custom-buffer)
1400
1401 (defcustom custom-magic-show-button nil
1402 "Show a \"magic\" button indicating the state of each customization option."
1403 :type 'boolean
1404 :group 'custom-buffer)
1405
1406 (define-widget 'custom-magic 'default
1407 "Show and manipulate state for a customization option."
1408 :format "%v"
1409 :action 'widget-parent-action
1410 :notify 'ignore
1411 :value-get 'ignore
1412 :value-create 'custom-magic-value-create
1413 :value-delete 'widget-children-value-delete)
1414
1415 (defun widget-magic-mouse-down-action (widget &optional event)
1416 ;; Non-nil unless hidden.
1417 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1418 :custom-state)
1419 'hidden)))
1420
1421 (defun custom-magic-value-create (widget)
1422 ;; Create compact status report for WIDGET.
1423 (let* ((parent (widget-get widget :parent))
1424 (state (widget-get parent :custom-state))
1425 (hidden (eq state 'hidden))
1426 (entry (assq state custom-magic-alist))
1427 (magic (nth 1 entry))
1428 (face (nth 2 entry))
1429 (category (widget-get parent :custom-category))
1430 (text (or (and (eq category 'group)
1431 (nth 4 entry))
1432 (nth 3 entry)))
1433 (form (widget-get parent :custom-form))
1434 children)
1435 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1436 (setq text (concat (match-string 1 text)
1437 (symbol-name category)
1438 (match-string 2 text))))
1439 (when (and custom-magic-show
1440 (or (not hidden)
1441 (memq category custom-magic-show-hidden)))
1442 (insert " ")
1443 (when (and (eq category 'group)
1444 (not (and (eq custom-buffer-style 'links)
1445 (> (widget-get parent :custom-level) 1))))
1446 (insert-char ?\ (* custom-buffer-indent
1447 (widget-get parent :custom-level))))
1448 (push (widget-create-child-and-convert
1449 widget 'choice-item
1450 :help-echo "Change the state of this item."
1451 :format (if hidden "%t" "%[%t%]")
1452 :button-prefix 'widget-push-button-prefix
1453 :button-suffix 'widget-push-button-suffix
1454 :mouse-down-action 'widget-magic-mouse-down-action
1455 :tag "State")
1456 children)
1457 (insert ": ")
1458 (let ((start (point)))
1459 (if (eq custom-magic-show 'long)
1460 (insert text)
1461 (insert (symbol-name state)))
1462 (cond ((eq form 'lisp)
1463 (insert " (lisp)"))
1464 ((eq form 'mismatch)
1465 (insert " (mismatch)")))
1466 (put-text-property start (point) 'face 'custom-state-face))
1467 (insert "\n"))
1468 (when (and (eq category 'group)
1469 (not (and (eq custom-buffer-style 'links)
1470 (> (widget-get parent :custom-level) 1))))
1471 (insert-char ?\ (* custom-buffer-indent
1472 (widget-get parent :custom-level))))
1473 (when custom-magic-show-button
1474 (when custom-magic-show
1475 (let ((indent (widget-get parent :indent)))
1476 (when indent
1477 (insert-char ? indent))))
1478 (push (widget-create-child-and-convert
1479 widget 'choice-item
1480 :mouse-down-action 'widget-magic-mouse-down-action
1481 :button-face face
1482 :button-prefix ""
1483 :button-suffix ""
1484 :help-echo "Change the state."
1485 :format (if hidden "%t" "%[%t%]")
1486 :tag (if (memq form '(lisp mismatch))
1487 (concat "(" magic ")")
1488 (concat "[" magic "]")))
1489 children)
1490 (insert " "))
1491 (widget-put widget :children children)))
1492
1493 (defun custom-magic-reset (widget)
1494 "Redraw the :custom-magic property of WIDGET."
1495 (let ((magic (widget-get widget :custom-magic)))
1496 (widget-value-set magic (widget-value magic))))
1497
1498 ;;; The `custom' Widget.
1499
1500 (defface custom-button-face nil
1501 "Face used for buttons in customization buffers."
1502 :group 'custom-faces)
1503
1504 (defface custom-documentation-face nil
1505 "Face used for documentation strings in customization buffers."
1506 :group 'custom-faces)
1507
1508 (defface custom-state-face '((((class color)
1509 (background dark))
1510 (:foreground "lime green"))
1511 (((class color)
1512 (background light))
1513 (:foreground "dark green"))
1514 (t nil))
1515 "Face used for State descriptions in the customize buffer."
1516 :group 'custom-faces)
1517
1518 (define-widget 'custom 'default
1519 "Customize a user option."
1520 :format "%v"
1521 :convert-widget 'custom-convert-widget
1522 :notify 'custom-notify
1523 :custom-prefix ""
1524 :custom-level 1
1525 :custom-state 'hidden
1526 :documentation-property 'widget-subclass-responsibility
1527 :value-create 'widget-subclass-responsibility
1528 :value-delete 'widget-children-value-delete
1529 :value-get 'widget-value-value-get
1530 :validate 'widget-children-validate
1531 :match (lambda (widget value) (symbolp value)))
1532
1533 (defun custom-convert-widget (widget)
1534 ;; Initialize :value and :tag from :args in WIDGET.
1535 (let ((args (widget-get widget :args)))
1536 (when args
1537 (widget-put widget :value (widget-apply widget
1538 :value-to-internal (car args)))
1539 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1540 (widget-put widget :args nil)))
1541 widget)
1542
1543 (defun custom-notify (widget &rest args)
1544 "Keep track of changes."
1545 (let ((state (widget-get widget :custom-state)))
1546 (unless (eq state 'modified)
1547 (unless (memq state '(nil unknown hidden))
1548 (widget-put widget :custom-state 'modified))
1549 (custom-magic-reset widget)
1550 (apply 'widget-default-notify widget args))))
1551
1552 (defun custom-redraw (widget)
1553 "Redraw WIDGET with current settings."
1554 (let ((line (count-lines (point-min) (point)))
1555 (column (current-column))
1556 (pos (point))
1557 (from (marker-position (widget-get widget :from)))
1558 (to (marker-position (widget-get widget :to))))
1559 (save-excursion
1560 (widget-value-set widget (widget-value widget))
1561 (custom-redraw-magic widget))
1562 (when (and (>= pos from) (<= pos to))
1563 (condition-case nil
1564 (progn
1565 (if (> column 0)
1566 (goto-line line)
1567 (goto-line (1+ line)))
1568 (move-to-column column))
1569 (error nil)))))
1570
1571 (defun custom-redraw-magic (widget)
1572 "Redraw WIDGET state with current settings."
1573 (while widget
1574 (let ((magic (widget-get widget :custom-magic)))
1575 (cond (magic
1576 (widget-value-set magic (widget-value magic))
1577 (when (setq widget (widget-get widget :group))
1578 (custom-group-state-update widget)))
1579 (t
1580 (setq widget nil)))))
1581 (widget-setup))
1582
1583 (defun custom-show (widget value)
1584 "Non-nil if WIDGET should be shown with VALUE by default."
1585 (let ((show (widget-get widget :custom-show)))
1586 (cond ((null show)
1587 nil)
1588 ((eq t show)
1589 t)
1590 (t
1591 (funcall show widget value)))))
1592
1593 (defvar custom-load-recursion nil
1594 "Hack to avoid recursive dependencies.")
1595
1596 (defun custom-load-symbol (symbol)
1597 "Load all dependencies for SYMBOL."
1598 (unless custom-load-recursion
1599 (let ((custom-load-recursion t)
1600 (loads (get symbol 'custom-loads))
1601 load)
1602 (while loads
1603 (setq load (car loads)
1604 loads (cdr loads))
1605 (cond ((symbolp load)
1606 (condition-case nil
1607 (require load)
1608 (error nil)))
1609 ;; Don't reload a file already loaded.
1610 ((and (boundp 'preloaded-file-list)
1611 (member load preloaded-file-list)))
1612 ((assoc load load-history))
1613 ((assoc (locate-library load) load-history))
1614 (t
1615 (condition-case nil
1616 ;; Without this, we would load cus-edit recursively.
1617 ;; We are still loading it when we call this,
1618 ;; and it is not in load-history yet.
1619 (or (equal load "cus-edit")
1620 (load-library load))
1621 (error nil))))))))
1622
1623 (defun custom-load-widget (widget)
1624 "Load all dependencies for WIDGET."
1625 (custom-load-symbol (widget-value widget)))
1626
1627 (defun custom-unloaded-symbol-p (symbol)
1628 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1629 (let ((found nil)
1630 (loads (get symbol 'custom-loads))
1631 load)
1632 (while loads
1633 (setq load (car loads)
1634 loads (cdr loads))
1635 (cond ((symbolp load)
1636 (unless (featurep load)
1637 (setq found t)))
1638 ((assoc load load-history))
1639 ((assoc (locate-library load) load-history)
1640 (message nil))
1641 (t
1642 (setq found t))))
1643 found))
1644
1645 (defun custom-unloaded-widget-p (widget)
1646 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1647 (custom-unloaded-symbol-p (widget-value widget)))
1648
1649 (defun custom-toggle-hide (widget)
1650 "Toggle visibility of WIDGET."
1651 (custom-load-widget widget)
1652 (let ((state (widget-get widget :custom-state)))
1653 (cond ((memq state '(invalid modified))
1654 (error "There are unset changes"))
1655 ((eq state 'hidden)
1656 (widget-put widget :custom-state 'unknown))
1657 (t
1658 (widget-put widget :documentation-shown nil)
1659 (widget-put widget :custom-state 'hidden)))
1660 (custom-redraw widget)
1661 (widget-setup)))
1662
1663 (defun custom-toggle-parent (widget &rest ignore)
1664 "Toggle visibility of parent of WIDGET."
1665 (custom-toggle-hide (widget-get widget :parent)))
1666
1667 (defun custom-add-see-also (widget &optional prefix)
1668 "Add `See also ...' to WIDGET if there are any links.
1669 Insert PREFIX first if non-nil."
1670 (let* ((symbol (widget-get widget :value))
1671 (links (get symbol 'custom-links))
1672 (many (> (length links) 2))
1673 (buttons (widget-get widget :buttons))
1674 (indent (widget-get widget :indent)))
1675 (when links
1676 (when indent
1677 (insert-char ?\ indent))
1678 (when prefix
1679 (insert prefix))
1680 (insert "See also ")
1681 (while links
1682 (push (widget-create-child-and-convert widget (car links))
1683 buttons)
1684 (setq links (cdr links))
1685 (cond ((null links)
1686 (insert ".\n"))
1687 ((null (cdr links))
1688 (if many
1689 (insert ", and ")
1690 (insert " and ")))
1691 (t
1692 (insert ", "))))
1693 (widget-put widget :buttons buttons))))
1694
1695 (defun custom-add-parent-links (widget &optional initial-string)
1696 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1697 The value if non-nil if any parents were found.
1698 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1699 (let ((name (widget-value widget))
1700 (type (widget-type widget))
1701 (buttons (widget-get widget :buttons))
1702 (start (point))
1703 found)
1704 (insert (or initial-string "Parent groups:"))
1705 (mapatoms (lambda (symbol)
1706 (let ((entry (assq name (get symbol 'custom-group))))
1707 (when (eq (nth 1 entry) type)
1708 (insert " ")
1709 (push (widget-create-child-and-convert
1710 widget 'custom-group-link
1711 :tag (custom-unlispify-tag-name symbol)
1712 symbol)
1713 buttons)
1714 (setq found t)))))
1715 (widget-put widget :buttons buttons)
1716 (if found
1717 (insert "\n")
1718 (delete-region start (point)))
1719 found))
1720
1721 ;;; The `custom-variable' Widget.
1722
1723 (defface custom-variable-tag-face '((((class color)
1724 (background dark))
1725 (:foreground "light blue" :underline t))
1726 (((class color)
1727 (background light))
1728 (:foreground "blue" :underline t))
1729 (t (:underline t)))
1730 "Face used for unpushable variable tags."
1731 :group 'custom-faces)
1732
1733 (defface custom-variable-button-face '((t (:underline t :bold t)))
1734 "Face used for pushable variable tags."
1735 :group 'custom-faces)
1736
1737 (define-widget 'custom-variable 'custom
1738 "Customize variable."
1739 :format "%v"
1740 :help-echo "Set or reset this variable."
1741 :documentation-property 'variable-documentation
1742 :custom-category 'option
1743 :custom-state nil
1744 :custom-menu 'custom-variable-menu-create
1745 :custom-form 'edit
1746 :value-create 'custom-variable-value-create
1747 :action 'custom-variable-action
1748 :custom-set 'custom-variable-set
1749 :custom-save 'custom-variable-save
1750 :custom-reset-current 'custom-redraw
1751 :custom-reset-saved 'custom-variable-reset-saved
1752 :custom-reset-standard 'custom-variable-reset-standard)
1753
1754 (defun custom-variable-type (symbol)
1755 "Return a widget suitable for editing the value of SYMBOL.
1756 If SYMBOL has a `custom-type' property, use that.
1757 Otherwise, look up symbol in `custom-guess-type-alist'."
1758 (let* ((type (or (get symbol 'custom-type)
1759 (and (not (get symbol 'standard-value))
1760 (custom-guess-type symbol))
1761 'sexp))
1762 (options (get symbol 'custom-options))
1763 (tmp (if (listp type)
1764 (copy-sequence type)
1765 (list type))))
1766 (when options
1767 (widget-put tmp :options options))
1768 tmp))
1769
1770 (defun custom-variable-value-create (widget)
1771 "Here is where you edit the variables value."
1772 (custom-load-widget widget)
1773 (let* ((buttons (widget-get widget :buttons))
1774 (children (widget-get widget :children))
1775 (form (widget-get widget :custom-form))
1776 (state (widget-get widget :custom-state))
1777 (symbol (widget-get widget :value))
1778 (tag (widget-get widget :tag))
1779 (type (custom-variable-type symbol))
1780 (conv (widget-convert type))
1781 (get (or (get symbol 'custom-get) 'default-value))
1782 (prefix (widget-get widget :custom-prefix))
1783 (last (widget-get widget :custom-last))
1784 (value (if (default-boundp symbol)
1785 (funcall get symbol)
1786 (widget-get conv :value))))
1787 ;; If the widget is new, the child determine whether it is hidden.
1788 (cond (state)
1789 ((custom-show type value)
1790 (setq state 'unknown))
1791 (t
1792 (setq state 'hidden)))
1793 ;; If we don't know the state, see if we need to edit it in lisp form.
1794 (when (eq state 'unknown)
1795 (unless (widget-apply conv :match value)
1796 ;; (widget-apply (widget-convert type) :match value)
1797 (setq form 'mismatch)))
1798 ;; Now we can create the child widget.
1799 (cond ((eq custom-buffer-style 'tree)
1800 (insert prefix (if last " `--- " " |--- "))
1801 (push (widget-create-child-and-convert
1802 widget 'custom-browse-variable-tag)
1803 buttons)
1804 (insert " " tag "\n")
1805 (widget-put widget :buttons buttons))
1806 ((eq state 'hidden)
1807 ;; Indicate hidden value.
1808 (push (widget-create-child-and-convert
1809 widget 'item
1810 :format "%{%t%}: "
1811 :sample-face 'custom-variable-tag-face
1812 :tag tag
1813 :parent widget)
1814 buttons)
1815 (push (widget-create-child-and-convert
1816 widget 'visibility
1817 :help-echo "Show the value of this option."
1818 :action 'custom-toggle-parent
1819 nil)
1820 buttons))
1821 ((memq form '(lisp mismatch))
1822 ;; In lisp mode edit the saved value when possible.
1823 (let* ((value (cond ((get symbol 'saved-value)
1824 (car (get symbol 'saved-value)))
1825 ((get symbol 'standard-value)
1826 (car (get symbol 'standard-value)))
1827 ((default-boundp symbol)
1828 (custom-quote (funcall get symbol)))
1829 (t
1830 (custom-quote (widget-get conv :value))))))
1831 (insert (symbol-name symbol) ": ")
1832 (push (widget-create-child-and-convert
1833 widget 'visibility
1834 :help-echo "Hide the value of this option."
1835 :action 'custom-toggle-parent
1836 t)
1837 buttons)
1838 (insert " ")
1839 (push (widget-create-child-and-convert
1840 widget 'sexp
1841 :button-face 'custom-variable-button-face
1842 :format "%v"
1843 :tag (symbol-name symbol)
1844 :parent widget
1845 :value value)
1846 children)))
1847 (t
1848 ;; Edit mode.
1849 (let* ((format (widget-get type :format))
1850 tag-format value-format)
1851 (unless (string-match ":" format)
1852 (error "Bad format"))
1853 (setq tag-format (substring format 0 (match-end 0)))
1854 (setq value-format (substring format (match-end 0)))
1855 (push (widget-create-child-and-convert
1856 widget 'item
1857 :format tag-format
1858 :action 'custom-tag-action
1859 :help-echo "Change value of this option."
1860 :mouse-down-action 'custom-tag-mouse-down-action
1861 :button-face 'custom-variable-button-face
1862 :sample-face 'custom-variable-tag-face
1863 tag)
1864 buttons)
1865 (insert " ")
1866 (push (widget-create-child-and-convert
1867 widget 'visibility
1868 :help-echo "Hide the value of this option."
1869 :action 'custom-toggle-parent
1870 t)
1871 buttons)
1872 (push (widget-create-child-and-convert
1873 widget type
1874 :format value-format
1875 :value value)
1876 children))))
1877 (unless (eq custom-buffer-style 'tree)
1878 ;; Now update the state.
1879 (unless (eq (preceding-char) ?\n)
1880 (widget-insert "\n"))
1881 (if (eq state 'hidden)
1882 (widget-put widget :custom-state state)
1883 (custom-variable-state-set widget))
1884 ;; Create the magic button.
1885 (let ((magic (widget-create-child-and-convert
1886 widget 'custom-magic nil)))
1887 (widget-put widget :custom-magic magic)
1888 (push magic buttons))
1889 ;; Update properties.
1890 (widget-put widget :custom-form form)
1891 (widget-put widget :buttons buttons)
1892 (widget-put widget :children children)
1893 ;; Insert documentation.
1894 (widget-default-format-handler widget ?h)
1895 ;; See also.
1896 (unless (eq state 'hidden)
1897 (when (eq (widget-get widget :custom-level) 1)
1898 (custom-add-parent-links widget))
1899 (custom-add-see-also widget)))))
1900
1901 (defun custom-tag-action (widget &rest args)
1902 "Pass :action to first child of WIDGET's parent."
1903 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1904 :action args))
1905
1906 (defun custom-tag-mouse-down-action (widget &rest args)
1907 "Pass :mouse-down-action to first child of WIDGET's parent."
1908 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1909 :mouse-down-action args))
1910
1911 (defun custom-variable-state-set (widget)
1912 "Set the state of WIDGET."
1913 (let* ((symbol (widget-value widget))
1914 (get (or (get symbol 'custom-get) 'default-value))
1915 (value (if (default-boundp symbol)
1916 (funcall get symbol)
1917 (widget-get widget :value)))
1918 tmp
1919 (state (cond ((setq tmp (get symbol 'customized-value))
1920 (if (condition-case nil
1921 (equal value (eval (car tmp)))
1922 (error nil))
1923 'set
1924 'changed))
1925 ((setq tmp (get symbol 'saved-value))
1926 (if (condition-case nil
1927 (equal value (eval (car tmp)))
1928 (error nil))
1929 'saved
1930 'changed))
1931 ((setq tmp (get symbol 'standard-value))
1932 (if (condition-case nil
1933 (equal value (eval (car tmp)))
1934 (error nil))
1935 'standard
1936 'changed))
1937 (t 'rogue))))
1938 (widget-put widget :custom-state state)))
1939
1940 (defvar custom-variable-menu
1941 '(("Set for Current Session" custom-variable-set
1942 (lambda (widget)
1943 (eq (widget-get widget :custom-state) 'modified)))
1944 ("Save for Future Sessions" custom-variable-save
1945 (lambda (widget)
1946 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1947 ("Reset to Current" custom-redraw
1948 (lambda (widget)
1949 (and (default-boundp (widget-value widget))
1950 (memq (widget-get widget :custom-state) '(modified changed)))))
1951 ("Reset to Saved" custom-variable-reset-saved
1952 (lambda (widget)
1953 (and (get (widget-value widget) 'saved-value)
1954 (memq (widget-get widget :custom-state)
1955 '(modified set changed rogue)))))
1956 ("Reset to Standard Settings" custom-variable-reset-standard
1957 (lambda (widget)
1958 (and (get (widget-value widget) 'standard-value)
1959 (memq (widget-get widget :custom-state)
1960 '(modified set changed saved rogue)))))
1961 ("---" ignore ignore)
1962 ("Don't show as Lisp expression" custom-variable-edit
1963 (lambda (widget)
1964 (eq (widget-get widget :custom-form) 'lisp)))
1965 ("Show initial Lisp expression" custom-variable-edit-lisp
1966 (lambda (widget)
1967 (eq (widget-get widget :custom-form) 'edit))))
1968 "Alist of actions for the `custom-variable' widget.
1969 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1970 the menu entry, ACTION is the function to call on the widget when the
1971 menu is selected, and FILTER is a predicate which takes a `custom-variable'
1972 widget as an argument, and returns non-nil if ACTION is valid on that
1973 widget. If FILTER is nil, ACTION is always valid.")
1974
1975 (defun custom-variable-action (widget &optional event)
1976 "Show the menu for `custom-variable' WIDGET.
1977 Optional EVENT is the location for the menu."
1978 (if (eq (widget-get widget :custom-state) 'hidden)
1979 (custom-toggle-hide widget)
1980 (unless (eq (widget-get widget :custom-state) 'modified)
1981 (custom-variable-state-set widget))
1982 (custom-redraw-magic widget)
1983 (let* ((completion-ignore-case t)
1984 (answer (widget-choose (concat "Operation on "
1985 (custom-unlispify-tag-name
1986 (widget-get widget :value)))
1987 (custom-menu-filter custom-variable-menu
1988 widget)
1989 event)))
1990 (if answer
1991 (funcall answer widget)))))
1992
1993 (defun custom-variable-edit (widget)
1994 "Edit value of WIDGET."
1995 (widget-put widget :custom-state 'unknown)
1996 (widget-put widget :custom-form 'edit)
1997 (custom-redraw widget))
1998
1999 (defun custom-variable-edit-lisp (widget)
2000 "Edit the lisp representation of the value of WIDGET."
2001 (widget-put widget :custom-state 'unknown)
2002 (widget-put widget :custom-form 'lisp)
2003 (custom-redraw widget))
2004
2005 (defun custom-variable-set (widget)
2006 "Set the current value for the variable being edited by WIDGET."
2007 (let* ((form (widget-get widget :custom-form))
2008 (state (widget-get widget :custom-state))
2009 (child (car (widget-get widget :children)))
2010 (symbol (widget-value widget))
2011 (set (or (get symbol 'custom-set) 'set-default))
2012 val)
2013 (cond ((eq state 'hidden)
2014 (error "Cannot set hidden variable"))
2015 ((setq val (widget-apply child :validate))
2016 (goto-char (widget-get val :from))
2017 (error "%s" (widget-get val :error)))
2018 ((memq form '(lisp mismatch))
2019 (funcall set symbol (eval (setq val (widget-value child))))
2020 (put symbol 'customized-value (list val)))
2021 (t
2022 (funcall set symbol (setq val (widget-value child)))
2023 (put symbol 'customized-value (list (custom-quote val)))))
2024 (custom-variable-state-set widget)
2025 (custom-redraw-magic widget)))
2026
2027 (defun custom-variable-save (widget)
2028 "Set and save the value for the variable being edited by WIDGET."
2029 (let* ((form (widget-get widget :custom-form))
2030 (state (widget-get widget :custom-state))
2031 (child (car (widget-get widget :children)))
2032 (symbol (widget-value widget))
2033 (set (or (get symbol 'custom-set) 'set-default))
2034 val)
2035 (cond ((eq state 'hidden)
2036 (error "Cannot set hidden variable"))
2037 ((setq val (widget-apply child :validate))
2038 (goto-char (widget-get val :from))
2039 (error "%s" (widget-get val :error)))
2040 ((memq form '(lisp mismatch))
2041 (put symbol 'saved-value (list (widget-value child)))
2042 (funcall set symbol (eval (widget-value child))))
2043 (t
2044 (put symbol
2045 'saved-value (list (custom-quote (widget-value
2046 child))))
2047 (funcall set symbol (widget-value child))))
2048 (put symbol 'customized-value nil)
2049 (custom-save-all)
2050 (custom-variable-state-set widget)
2051 (custom-redraw-magic widget)))
2052
2053 (defun custom-variable-reset-saved (widget)
2054 "Restore the saved value for the variable being edited by WIDGET."
2055 (let* ((symbol (widget-value widget))
2056 (set (or (get symbol 'custom-set) 'set-default)))
2057 (if (get symbol 'saved-value)
2058 (condition-case nil
2059 (funcall set symbol (eval (car (get symbol 'saved-value))))
2060 (error nil))
2061 (error "No saved value for %s" symbol))
2062 (put symbol 'customized-value nil)
2063 (widget-put widget :custom-state 'unknown)
2064 (custom-redraw widget)))
2065
2066 (defun custom-variable-reset-standard (widget)
2067 "Restore the standard setting for the variable being edited by WIDGET."
2068 (let* ((symbol (widget-value widget))
2069 (set (or (get symbol 'custom-set) 'set-default)))
2070 (if (get symbol 'standard-value)
2071 (funcall set symbol (eval (car (get symbol 'standard-value))))
2072 (error "No standard setting known for %S" symbol))
2073 (put symbol 'customized-value nil)
2074 (when (get symbol 'saved-value)
2075 (put symbol 'saved-value nil)
2076 (custom-save-all))
2077 (widget-put widget :custom-state 'unknown)
2078 (custom-redraw widget)))
2079
2080 ;;; The `custom-face-edit' Widget.
2081
2082 (define-widget 'custom-face-edit 'checklist
2083 "Edit face attributes."
2084 :format "%t: %v"
2085 :tag "Attributes"
2086 :extra-offset 12
2087 :button-args '(:help-echo "Control whether this attribute have any effect.")
2088 :args (mapcar (lambda (att)
2089 (list 'group
2090 :inline t
2091 :sibling-args (widget-get (nth 1 att) :sibling-args)
2092 (list 'const :format "" :value (nth 0 att))
2093 (nth 1 att)))
2094 custom-face-attributes))
2095
2096 ;;; The `custom-display' Widget.
2097
2098 (define-widget 'custom-display 'menu-choice
2099 "Select a display type."
2100 :tag "Display"
2101 :value t
2102 :help-echo "Specify frames where the face attributes should be used."
2103 :args '((const :tag "all" t)
2104 (checklist
2105 :offset 0
2106 :extra-offset 9
2107 :args ((group :sibling-args (:help-echo "\
2108 Only match the specified window systems.")
2109 (const :format "Type: "
2110 type)
2111 (checklist :inline t
2112 :offset 0
2113 (const :format "X "
2114 :sibling-args (:help-echo "\
2115 The X11 Window System.")
2116 x)
2117 (const :format "PM "
2118 :sibling-args (:help-echo "\
2119 OS/2 Presentation Manager.")
2120 pm)
2121 (const :format "W32 "
2122 :sibling-args (:help-echo "\
2123 Windows NT/9X.")
2124 w32)
2125 (const :format "DOS "
2126 :sibling-args (:help-echo "\
2127 Plain MS-DOS.")
2128 pc)
2129 (const :format "TTY%n"
2130 :sibling-args (:help-echo "\
2131 Plain text terminals.")
2132 tty)))
2133 (group :sibling-args (:help-echo "\
2134 Only match the frames with the specified color support.")
2135 (const :format "Class: "
2136 class)
2137 (checklist :inline t
2138 :offset 0
2139 (const :format "Color "
2140 :sibling-args (:help-echo "\
2141 Match color frames.")
2142 color)
2143 (const :format "Grayscale "
2144 :sibling-args (:help-echo "\
2145 Match grayscale frames.")
2146 grayscale)
2147 (const :format "Monochrome%n"
2148 :sibling-args (:help-echo "\
2149 Match frames with no color support.")
2150 mono)))
2151 (group :sibling-args (:help-echo "\
2152 Only match frames with the specified intensity.")
2153 (const :format "\
2154 Background brightness: "
2155 background)
2156 (checklist :inline t
2157 :offset 0
2158 (const :format "Light "
2159 :sibling-args (:help-echo "\
2160 Match frames with light backgrounds.")
2161 light)
2162 (const :format "Dark\n"
2163 :sibling-args (:help-echo "\
2164 Match frames with dark backgrounds.")
2165 dark)))))))
2166
2167 ;;; The `custom-face' Widget.
2168
2169 (defface custom-face-tag-face '((t (:underline t)))
2170 "Face used for face tags."
2171 :group 'custom-faces)
2172
2173 (define-widget 'custom-face 'custom
2174 "Customize face."
2175 :sample-face 'custom-face-tag-face
2176 :help-echo "Set or reset this face."
2177 :documentation-property '(lambda (face)
2178 (face-doc-string face))
2179 :value-create 'custom-face-value-create
2180 :action 'custom-face-action
2181 :custom-category 'face
2182 :custom-form 'selected
2183 :custom-set 'custom-face-set
2184 :custom-save 'custom-face-save
2185 :custom-reset-current 'custom-redraw
2186 :custom-reset-saved 'custom-face-reset-saved
2187 :custom-reset-standard 'custom-face-reset-standard
2188 :custom-menu 'custom-face-menu-create)
2189
2190 (define-widget 'custom-face-all 'editable-list
2191 "An editable list of display specifications and attributes."
2192 :entry-format "%i %d %v"
2193 :insert-button-args '(:help-echo "Insert new display specification here.")
2194 :append-button-args '(:help-echo "Append new display specification here.")
2195 :delete-button-args '(:help-echo "Delete this display specification.")
2196 :args '((group :format "%v" custom-display custom-face-edit)))
2197
2198 (defconst custom-face-all (widget-convert 'custom-face-all)
2199 "Converted version of the `custom-face-all' widget.")
2200
2201 (define-widget 'custom-display-unselected 'item
2202 "A display specification that doesn't match the selected display."
2203 :match 'custom-display-unselected-match)
2204
2205 (defun custom-display-unselected-match (widget value)
2206 "Non-nil if VALUE is an unselected display specification."
2207 (not (face-spec-set-match-display value (selected-frame))))
2208
2209 (define-widget 'custom-face-selected 'group
2210 "Edit the attributes of the selected display in a face specification."
2211 :args '((repeat :format ""
2212 :inline t
2213 (group custom-display-unselected sexp))
2214 (group (sexp :format "") custom-face-edit)
2215 (repeat :format ""
2216 :inline t
2217 sexp)))
2218
2219 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2220 "Converted version of the `custom-face-selected' widget.")
2221
2222 (defun custom-face-value-create (widget)
2223 "Create a list of the display specifications for WIDGET."
2224 (let ((buttons (widget-get widget :buttons))
2225 (symbol (widget-get widget :value))
2226 (tag (widget-get widget :tag))
2227 (state (widget-get widget :custom-state))
2228 (begin (point))
2229 (is-last (widget-get widget :custom-last))
2230 (prefix (widget-get widget :custom-prefix)))
2231 (unless tag
2232 (setq tag (prin1-to-string symbol)))
2233 (cond ((eq custom-buffer-style 'tree)
2234 (insert prefix (if is-last " `--- " " |--- "))
2235 (push (widget-create-child-and-convert
2236 widget 'custom-browse-face-tag)
2237 buttons)
2238 (insert " " tag "\n")
2239 (widget-put widget :buttons buttons))
2240 (t
2241 ;; Create tag.
2242 (insert tag)
2243 (if (eq custom-buffer-style 'face)
2244 (insert " ")
2245 (widget-specify-sample widget begin (point))
2246 (insert ": "))
2247 ;; Sample.
2248 (and (string-match "XEmacs" emacs-version)
2249 ;; XEmacs cannot display uninitialized faces.
2250 (not (custom-facep symbol))
2251 (copy-face 'custom-face-empty symbol))
2252 (push (widget-create-child-and-convert widget 'item
2253 :format "(%{%t%})"
2254 :sample-face symbol
2255 :tag "sample")
2256 buttons)
2257 ;; Visibility.
2258 (insert " ")
2259 (push (widget-create-child-and-convert
2260 widget 'visibility
2261 :help-echo "Hide or show this face."
2262 :action 'custom-toggle-parent
2263 (not (eq state 'hidden)))
2264 buttons)
2265 ;; Magic.
2266 (insert "\n")
2267 (let ((magic (widget-create-child-and-convert
2268 widget 'custom-magic nil)))
2269 (widget-put widget :custom-magic magic)
2270 (push magic buttons))
2271 ;; Update buttons.
2272 (widget-put widget :buttons buttons)
2273 ;; Insert documentation.
2274 (widget-default-format-handler widget ?h)
2275 ;; See also.
2276 (unless (eq state 'hidden)
2277 (when (eq (widget-get widget :custom-level) 1)
2278 (custom-add-parent-links widget))
2279 (custom-add-see-also widget))
2280 ;; Editor.
2281 (unless (eq (preceding-char) ?\n)
2282 (insert "\n"))
2283 (unless (eq state 'hidden)
2284 (message "Creating face editor...")
2285 (custom-load-widget widget)
2286 (let* ((symbol (widget-value widget))
2287 (spec (or (get symbol 'saved-face)
2288 (get symbol 'face-defface-spec)
2289 ;; Attempt to construct it.
2290 (list (list t (custom-face-attributes-get
2291 symbol (selected-frame))))))
2292 (form (widget-get widget :custom-form))
2293 (indent (widget-get widget :indent))
2294 edit)
2295 ;; If the user has changed this face in some other way,
2296 ;; edit it as the user has specified it.
2297 (if (not (face-spec-match-p symbol spec (selected-frame)))
2298 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
2299 (setq edit (widget-create-child-and-convert
2300 widget
2301 (cond ((and (eq form 'selected)
2302 (widget-apply custom-face-selected
2303 :match spec))
2304 (when indent (insert-char ?\ indent))
2305 'custom-face-selected)
2306 ((and (not (eq form 'lisp))
2307 (widget-apply custom-face-all
2308 :match spec))
2309 'custom-face-all)
2310 (t
2311 (when indent (insert-char ?\ indent))
2312 'sexp))
2313 :value spec))
2314 (custom-face-state-set widget)
2315 (widget-put widget :children (list edit)))
2316 (message "Creating face editor...done"))))))
2317
2318 (defvar custom-face-menu
2319 '(("Set for Current Session" custom-face-set)
2320 ("Save for Future Sessions" custom-face-save-command)
2321 ("Reset to Saved" custom-face-reset-saved
2322 (lambda (widget)
2323 (get (widget-value widget) 'saved-face)))
2324 ("Reset to Standard Setting" custom-face-reset-standard
2325 (lambda (widget)
2326 (get (widget-value widget) 'face-defface-spec)))
2327 ("---" ignore ignore)
2328 ("Show all display specs" custom-face-edit-all
2329 (lambda (widget)
2330 (not (eq (widget-get widget :custom-form) 'all))))
2331 ("Just current attributes" custom-face-edit-selected
2332 (lambda (widget)
2333 (not (eq (widget-get widget :custom-form) 'selected))))
2334 ("Show as Lisp expression" custom-face-edit-lisp
2335 (lambda (widget)
2336 (not (eq (widget-get widget :custom-form) 'lisp)))))
2337 "Alist of actions for the `custom-face' widget.
2338 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2339 the menu entry, ACTION is the function to call on the widget when the
2340 menu is selected, and FILTER is a predicate which takes a `custom-face'
2341 widget as an argument, and returns non-nil if ACTION is valid on that
2342 widget. If FILTER is nil, ACTION is always valid.")
2343
2344 (defun custom-face-edit-selected (widget)
2345 "Edit selected attributes of the value of WIDGET."
2346 (widget-put widget :custom-state 'unknown)
2347 (widget-put widget :custom-form 'selected)
2348 (custom-redraw widget))
2349
2350 (defun custom-face-edit-all (widget)
2351 "Edit all attributes of the value of WIDGET."
2352 (widget-put widget :custom-state 'unknown)
2353 (widget-put widget :custom-form 'all)
2354 (custom-redraw widget))
2355
2356 (defun custom-face-edit-lisp (widget)
2357 "Edit the lisp representation of the value of WIDGET."
2358 (widget-put widget :custom-state 'unknown)
2359 (widget-put widget :custom-form 'lisp)
2360 (custom-redraw widget))
2361
2362 (defun custom-face-state-set (widget)
2363 "Set the state of WIDGET."
2364 (let ((symbol (widget-value widget)))
2365 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
2366 'set)
2367 ((get symbol 'saved-face)
2368 'saved)
2369 ((get symbol 'face-defface-spec)
2370 'standard)
2371 (t
2372 'rogue)))))
2373
2374 (defun custom-face-action (widget &optional event)
2375 "Show the menu for `custom-face' WIDGET.
2376 Optional EVENT is the location for the menu."
2377 (if (eq (widget-get widget :custom-state) 'hidden)
2378 (custom-toggle-hide widget)
2379 (let* ((completion-ignore-case t)
2380 (symbol (widget-get widget :value))
2381 (answer (widget-choose (concat "Operation on "
2382 (custom-unlispify-tag-name symbol))
2383 (custom-menu-filter custom-face-menu
2384 widget)
2385 event)))
2386 (if answer
2387 (funcall answer widget)))))
2388
2389 (defun custom-face-set (widget)
2390 "Make the face attributes in WIDGET take effect."
2391 (let* ((symbol (widget-value widget))
2392 (child (car (widget-get widget :children)))
2393 (value (widget-value child)))
2394 (put symbol 'customized-face value)
2395 (face-spec-set symbol value)
2396 (custom-face-state-set widget)
2397 (custom-redraw-magic widget)))
2398
2399 (defun custom-face-save-command (widget)
2400 "Save in `.emacs' the face attributes in WIDGET."
2401 (custom-face-save widget)
2402 (custom-save-all))
2403
2404 (defun custom-face-save (widget)
2405 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
2406 (let* ((symbol (widget-value widget))
2407 (child (car (widget-get widget :children)))
2408 (value (widget-value child)))
2409 (face-spec-set symbol value)
2410 (put symbol 'saved-face value)
2411 (put symbol 'customized-face nil)
2412 (custom-face-state-set widget)
2413 (custom-redraw-magic widget)))
2414
2415 (defun custom-face-reset-saved (widget)
2416 "Restore WIDGET to the face's default attributes."
2417 (let* ((symbol (widget-value widget))
2418 (child (car (widget-get widget :children)))
2419 (value (get symbol 'saved-face)))
2420 (unless value
2421 (error "No saved value for this face"))
2422 (put symbol 'customized-face nil)
2423 (face-spec-set symbol value)
2424 (widget-value-set child value)
2425 (custom-face-state-set widget)
2426 (custom-redraw-magic widget)))
2427
2428 (defun custom-face-reset-standard (widget)
2429 "Restore WIDGET to the face's standard settings."
2430 (let* ((symbol (widget-value widget))
2431 (child (car (widget-get widget :children)))
2432 (value (get symbol 'face-defface-spec)))
2433 (unless value
2434 (error "No standard setting for this face"))
2435 (put symbol 'customized-face nil)
2436 (when (get symbol 'saved-face)
2437 (put symbol 'saved-face nil)
2438 (custom-save-all))
2439 (face-spec-set symbol value)
2440 (widget-value-set child value)
2441 (custom-face-state-set widget)
2442 (custom-redraw-magic widget)))
2443
2444 ;;; The `face' Widget.
2445
2446 (define-widget 'face 'default
2447 "Select and customize a face."
2448 :convert-widget 'widget-value-convert-widget
2449 :button-prefix 'widget-push-button-prefix
2450 :button-suffix 'widget-push-button-suffix
2451 :format "%t: %[select face%] %v"
2452 :tag "Face"
2453 :value 'default
2454 :value-create 'widget-face-value-create
2455 :value-delete 'widget-face-value-delete
2456 :value-get 'widget-value-value-get
2457 :validate 'widget-children-validate
2458 :action 'widget-face-action
2459 :match '(lambda (widget value) (symbolp value)))
2460
2461 (defun widget-face-value-create (widget)
2462 ;; Create a `custom-face' child.
2463 (let* ((symbol (widget-value widget))
2464 (custom-buffer-style 'face)
2465 (child (widget-create-child-and-convert
2466 widget 'custom-face
2467 :custom-level nil
2468 :value symbol)))
2469 (custom-magic-reset child)
2470 (setq custom-options (cons child custom-options))
2471 (widget-put widget :children (list child))))
2472
2473 (defun widget-face-value-delete (widget)
2474 ;; Remove the child from the options.
2475 (let ((child (car (widget-get widget :children))))
2476 (setq custom-options (delq child custom-options))
2477 (widget-children-value-delete widget)))
2478
2479 (defvar face-history nil
2480 "History of entered face names.")
2481
2482 (defun widget-face-action (widget &optional event)
2483 "Prompt for a face."
2484 (let ((answer (completing-read "Face: "
2485 (mapcar (lambda (face)
2486 (list (symbol-name face)))
2487 (face-list))
2488 nil nil nil
2489 'face-history)))
2490 (unless (zerop (length answer))
2491 (widget-value-set widget (intern answer))
2492 (widget-apply widget :notify widget event)
2493 (widget-setup))))
2494
2495 ;;; The `hook' Widget.
2496
2497 (define-widget 'hook 'list
2498 "A emacs lisp hook"
2499 :value-to-internal (lambda (widget value)
2500 (if (and value (symbolp value))
2501 (list value)
2502 value))
2503 :match (lambda (widget value)
2504 (or (symbolp value)
2505 (widget-editable-list-match widget value)))
2506 :convert-widget 'custom-hook-convert-widget
2507 :tag "Hook")
2508
2509 (defun custom-hook-convert-widget (widget)
2510 ;; Handle `:custom-options'.
2511 (let* ((options (widget-get widget :options))
2512 (other `(editable-list :inline t
2513 :entry-format "%i %d%v"
2514 (function :format " %v")))
2515 (args (if options
2516 (list `(checklist :inline t
2517 ,@(mapcar (lambda (entry)
2518 `(function-item ,entry))
2519 options))
2520 other)
2521 (list other))))
2522 (widget-put widget :args args)
2523 widget))
2524
2525 ;;; The `custom-group-link' Widget.
2526
2527 (define-widget 'custom-group-link 'link
2528 "Show parent in other window when activated."
2529 :help-echo "Create customization buffer for this group."
2530 :action 'custom-group-link-action)
2531
2532 (defun custom-group-link-action (widget &rest ignore)
2533 (customize-group (widget-value widget)))
2534
2535 ;;; The `custom-group' Widget.
2536
2537 (defcustom custom-group-tag-faces nil
2538 ;; In XEmacs, this ought to play games with font size.
2539 "Face used for group tags.
2540 The first member is used for level 1 groups, the second for level 2,
2541 and so forth. The remaining group tags are shown with
2542 `custom-group-tag-face'."
2543 :type '(repeat face)
2544 :group 'custom-faces)
2545
2546 (defface custom-group-tag-face-1 '((((class color)
2547 (background dark))
2548 (:foreground "pink" :underline t))
2549 (((class color)
2550 (background light))
2551 (:foreground "red" :underline t))
2552 (t (:underline t)))
2553 "Face used for group tags.")
2554
2555 (defface custom-group-tag-face '((((class color)
2556 (background dark))
2557 (:foreground "light blue" :underline t))
2558 (((class color)
2559 (background light))
2560 (:foreground "blue" :underline t))
2561 (t (:underline t)))
2562 "Face used for low level group tags."
2563 :group 'custom-faces)
2564
2565 (define-widget 'custom-group 'custom
2566 "Customize group."
2567 :format "%v"
2568 :sample-face-get 'custom-group-sample-face-get
2569 :documentation-property 'group-documentation
2570 :help-echo "Set or reset all members of this group."
2571 :value-create 'custom-group-value-create
2572 :action 'custom-group-action
2573 :custom-category 'group
2574 :custom-set 'custom-group-set
2575 :custom-save 'custom-group-save
2576 :custom-reset-current 'custom-group-reset-current
2577 :custom-reset-saved 'custom-group-reset-saved
2578 :custom-reset-standard 'custom-group-reset-standard
2579 :custom-menu 'custom-group-menu-create)
2580
2581 (defun custom-group-sample-face-get (widget)
2582 ;; Use :sample-face.
2583 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2584 'custom-group-tag-face))
2585
2586 (define-widget 'custom-group-visibility 'visibility
2587 "An indicator and manipulator for hidden group contents."
2588 :create 'custom-group-visibility-create)
2589
2590 (defun custom-group-visibility-create (widget)
2591 (let ((visible (widget-value widget)))
2592 (if visible
2593 (insert "--------")))
2594 (widget-default-create widget))
2595
2596 (defun custom-group-members (symbol groups-only)
2597 "Return SYMBOL's custom group members.
2598 If GROUPS-ONLY non-nil, return only those members that are groups."
2599 (if (not groups-only)
2600 (get symbol 'custom-group)
2601 (let (members)
2602 (dolist (entry (get symbol 'custom-group))
2603 (when (eq (nth 1 entry) 'custom-group)
2604 (push entry members)))
2605 (nreverse members))))
2606
2607 (defun custom-group-value-create (widget)
2608 "Insert a customize group for WIDGET in the current buffer."
2609 (let* ((state (widget-get widget :custom-state))
2610 (level (widget-get widget :custom-level))
2611 ;; (indent (widget-get widget :indent))
2612 (prefix (widget-get widget :custom-prefix))
2613 (buttons (widget-get widget :buttons))
2614 (tag (widget-get widget :tag))
2615 (symbol (widget-value widget))
2616 (members (custom-group-members symbol
2617 (and (eq custom-buffer-style 'tree)
2618 custom-browse-only-groups))))
2619 (cond ((and (eq custom-buffer-style 'tree)
2620 (eq state 'hidden)
2621 (or members (custom-unloaded-widget-p widget)))
2622 (custom-browse-insert-prefix prefix)
2623 (push (widget-create-child-and-convert
2624 widget 'custom-browse-visibility
2625 ;; :tag-glyph "plus"
2626 :tag "+")
2627 buttons)
2628 (insert "-- ")
2629 ;; (widget-glyph-insert nil "-- " "horizontal")
2630 (push (widget-create-child-and-convert
2631 widget 'custom-browse-group-tag)
2632 buttons)
2633 (insert " " tag "\n")
2634 (widget-put widget :buttons buttons))
2635 ((and (eq custom-buffer-style 'tree)
2636 (zerop (length members)))
2637 (custom-browse-insert-prefix prefix)
2638 (insert "[ ]-- ")
2639 ;; (widget-glyph-insert nil "[ ]" "empty")
2640 ;; (widget-glyph-insert nil "-- " "horizontal")
2641 (push (widget-create-child-and-convert
2642 widget 'custom-browse-group-tag)
2643 buttons)
2644 (insert " " tag "\n")
2645 (widget-put widget :buttons buttons))
2646 ((eq custom-buffer-style 'tree)
2647 (custom-browse-insert-prefix prefix)
2648 (custom-load-widget widget)
2649 (if (zerop (length members))
2650 (progn
2651 (custom-browse-insert-prefix prefix)
2652 (insert "[ ]-- ")
2653 ;; (widget-glyph-insert nil "[ ]" "empty")
2654 ;; (widget-glyph-insert nil "-- " "horizontal")
2655 (push (widget-create-child-and-convert
2656 widget 'custom-browse-group-tag)
2657 buttons)
2658 (insert " " tag "\n")
2659 (widget-put widget :buttons buttons))
2660 (push (widget-create-child-and-convert
2661 widget 'custom-browse-visibility
2662 ;; :tag-glyph "minus"
2663 :tag "-")
2664 buttons)
2665 (insert "-\\ ")
2666 ;; (widget-glyph-insert nil "-\\ " "top")
2667 (push (widget-create-child-and-convert
2668 widget 'custom-browse-group-tag)
2669 buttons)
2670 (insert " " tag "\n")
2671 (widget-put widget :buttons buttons)
2672 (message "Creating group...")
2673 (let* ((members (custom-sort-items members
2674 custom-browse-sort-alphabetically
2675 custom-browse-order-groups))
2676 (prefixes (widget-get widget :custom-prefixes))
2677 (custom-prefix-list (custom-prefix-add symbol prefixes))
2678 (extra-prefix (if (widget-get widget :custom-last)
2679 " "
2680 " | "))
2681 (prefix (concat prefix extra-prefix))
2682 children entry)
2683 (while members
2684 (setq entry (car members)
2685 members (cdr members))
2686 (push (widget-create-child-and-convert
2687 widget (nth 1 entry)
2688 :group widget
2689 :tag (custom-unlispify-tag-name (nth 0 entry))
2690 :custom-prefixes custom-prefix-list
2691 :custom-level (1+ level)
2692 :custom-last (null members)
2693 :value (nth 0 entry)
2694 :custom-prefix prefix)
2695 children))
2696 (widget-put widget :children (reverse children)))
2697 (message "Creating group...done")))
2698 ;; Nested style.
2699 ((eq state 'hidden)
2700 ;; Create level indicator.
2701 (unless (eq custom-buffer-style 'links)
2702 (insert-char ?\ (* custom-buffer-indent (1- level)))
2703 (insert "-- "))
2704 ;; Create tag.
2705 (let ((begin (point)))
2706 (insert tag)
2707 (widget-specify-sample widget begin (point)))
2708 (insert " group: ")
2709 ;; Create link/visibility indicator.
2710 (if (eq custom-buffer-style 'links)
2711 (push (widget-create-child-and-convert
2712 widget 'custom-group-link
2713 :tag "Go to Group"
2714 symbol)
2715 buttons)
2716 (push (widget-create-child-and-convert
2717 widget 'group-visibility
2718 :help-echo "Show members of this group."
2719 :action 'custom-toggle-parent
2720 (not (eq state 'hidden)))
2721 buttons))
2722 (insert " \n")
2723 ;; Create magic button.
2724 (let ((magic (widget-create-child-and-convert
2725 widget 'custom-magic nil)))
2726 (widget-put widget :custom-magic magic)
2727 (push magic buttons))
2728 ;; Update buttons.
2729 (widget-put widget :buttons buttons)
2730 ;; Insert documentation.
2731 (if (and (eq custom-buffer-style 'links) (> level 1))
2732 (widget-put widget :documentation-indent 0))
2733 (widget-default-format-handler widget ?h))
2734 ;; Nested style.
2735 (t ;Visible.
2736 ;; Add parent groups references above the group.
2737 (if t ;;; This should test that the buffer
2738 ;;; was made to display a group.
2739 (when (eq level 1)
2740 (if (custom-add-parent-links widget
2741 "Go to parent group:")
2742 (insert "\n"))))
2743 ;; Create level indicator.
2744 (insert-char ?\ (* custom-buffer-indent (1- level)))
2745 (insert "/- ")
2746 ;; Create tag.
2747 (let ((start (point)))
2748 (insert tag)
2749 (widget-specify-sample widget start (point)))
2750 (insert " group: ")
2751 ;; Create visibility indicator.
2752 (unless (eq custom-buffer-style 'links)
2753 (insert "--------")
2754 (push (widget-create-child-and-convert
2755 widget 'visibility
2756 :help-echo "Hide members of this group."
2757 :action 'custom-toggle-parent
2758 (not (eq state 'hidden)))
2759 buttons)
2760 (insert " "))
2761 ;; Create more dashes.
2762 ;; Use 76 instead of 75 to compensate for the temporary "<"
2763 ;; added by `widget-insert'.
2764 (insert-char ?- (- 76 (current-column)
2765 (* custom-buffer-indent level)))
2766 (insert "\\\n")
2767 ;; Create magic button.
2768 (let ((magic (widget-create-child-and-convert
2769 widget 'custom-magic
2770 :indent 0
2771 nil)))
2772 (widget-put widget :custom-magic magic)
2773 (push magic buttons))
2774 ;; Update buttons.
2775 (widget-put widget :buttons buttons)
2776 ;; Insert documentation.
2777 (widget-default-format-handler widget ?h)
2778 ;; Parent groups.
2779 (if nil ;;; This should test that the buffer
2780 ;;; was not made to display a group.
2781 (when (eq level 1)
2782 (insert-char ?\ custom-buffer-indent)
2783 (custom-add-parent-links widget)))
2784 (custom-add-see-also widget
2785 (make-string (* custom-buffer-indent level)
2786 ?\ ))
2787 ;; Members.
2788 (message "Creating group...")
2789 (custom-load-widget widget)
2790 (let* ((members (custom-sort-items members
2791 custom-buffer-sort-alphabetically
2792 custom-buffer-order-groups))
2793 (prefixes (widget-get widget :custom-prefixes))
2794 (custom-prefix-list (custom-prefix-add symbol prefixes))
2795 (length (length members))
2796 (count 0)
2797 (children (mapcar (lambda (entry)
2798 (widget-insert "\n")
2799 (message "\
2800 Creating group members... %2d%%"
2801 (/ (* 100.0 count) length))
2802 (setq count (1+ count))
2803 (prog1
2804 (widget-create-child-and-convert
2805 widget (nth 1 entry)
2806 :group widget
2807 :tag (custom-unlispify-tag-name
2808 (nth 0 entry))
2809 :custom-prefixes custom-prefix-list
2810 :custom-level (1+ level)
2811 :value (nth 0 entry))
2812 (unless (eq (preceding-char) ?\n)
2813 (widget-insert "\n"))))
2814 members)))
2815 (message "Creating group magic...")
2816 (mapcar 'custom-magic-reset children)
2817 (message "Creating group state...")
2818 (widget-put widget :children children)
2819 (custom-group-state-update widget)
2820 (message "Creating group... done"))
2821 ;; End line
2822 (insert "\n")
2823 (insert-char ?\ (* custom-buffer-indent (1- level)))
2824 (insert "\\- " (widget-get widget :tag) " group end ")
2825 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
2826 (insert "/\n")))))
2827
2828 (defvar custom-group-menu
2829 '(("Set for Current Session" custom-group-set
2830 (lambda (widget)
2831 (eq (widget-get widget :custom-state) 'modified)))
2832 ("Save for Future Sessions" custom-group-save
2833 (lambda (widget)
2834 (memq (widget-get widget :custom-state) '(modified set))))
2835 ("Reset to Current" custom-group-reset-current
2836 (lambda (widget)
2837 (memq (widget-get widget :custom-state) '(modified))))
2838 ("Reset to Saved" custom-group-reset-saved
2839 (lambda (widget)
2840 (memq (widget-get widget :custom-state) '(modified set))))
2841 ("Reset to standard setting" custom-group-reset-standard
2842 (lambda (widget)
2843 (memq (widget-get widget :custom-state) '(modified set saved)))))
2844 "Alist of actions for the `custom-group' widget.
2845 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2846 the menu entry, ACTION is the function to call on the widget when the
2847 menu is selected, and FILTER is a predicate which takes a `custom-group'
2848 widget as an argument, and returns non-nil if ACTION is valid on that
2849 widget. If FILTER is nil, ACTION is always valid.")
2850
2851 (defun custom-group-action (widget &optional event)
2852 "Show the menu for `custom-group' WIDGET.
2853 Optional EVENT is the location for the menu."
2854 (if (eq (widget-get widget :custom-state) 'hidden)
2855 (custom-toggle-hide widget)
2856 (let* ((completion-ignore-case t)
2857 (answer (widget-choose (concat "Operation on "
2858 (custom-unlispify-tag-name
2859 (widget-get widget :value)))
2860 (custom-menu-filter custom-group-menu
2861 widget)
2862 event)))
2863 (if answer
2864 (funcall answer widget)))))
2865
2866 (defun custom-group-set (widget)
2867 "Set changes in all modified group members."
2868 (let ((children (widget-get widget :children)))
2869 (mapcar (lambda (child)
2870 (when (eq (widget-get child :custom-state) 'modified)
2871 (widget-apply child :custom-set)))
2872 children )))
2873
2874 (defun custom-group-save (widget)
2875 "Save all modified group members."
2876 (let ((children (widget-get widget :children)))
2877 (mapcar (lambda (child)
2878 (when (memq (widget-get child :custom-state) '(modified set))
2879 (widget-apply child :custom-save)))
2880 children )))
2881
2882 (defun custom-group-reset-current (widget)
2883 "Reset all modified group members."
2884 (let ((children (widget-get widget :children)))
2885 (mapcar (lambda (child)
2886 (when (eq (widget-get child :custom-state) 'modified)
2887 (widget-apply child :custom-reset-current)))
2888 children )))
2889
2890 (defun custom-group-reset-saved (widget)
2891 "Reset all modified or set group members."
2892 (let ((children (widget-get widget :children)))
2893 (mapcar (lambda (child)
2894 (when (memq (widget-get child :custom-state) '(modified set))
2895 (widget-apply child :custom-reset-saved)))
2896 children )))
2897
2898 (defun custom-group-reset-standard (widget)
2899 "Reset all modified, set, or saved group members."
2900 (let ((children (widget-get widget :children)))
2901 (mapcar (lambda (child)
2902 (when (memq (widget-get child :custom-state)
2903 '(modified set saved))
2904 (widget-apply child :custom-reset-standard)))
2905 children )))
2906
2907 (defun custom-group-state-update (widget)
2908 "Update magic."
2909 (unless (eq (widget-get widget :custom-state) 'hidden)
2910 (let* ((children (widget-get widget :children))
2911 (states (mapcar (lambda (child)
2912 (widget-get child :custom-state))
2913 children))
2914 (magics custom-magic-alist)
2915 (found 'standard))
2916 (while magics
2917 (let ((magic (car (car magics))))
2918 (if (and (not (eq magic 'hidden))
2919 (memq magic states))
2920 (setq found magic
2921 magics nil)
2922 (setq magics (cdr magics)))))
2923 (widget-put widget :custom-state found)))
2924 (custom-magic-reset widget))
2925
2926 ;;; The `custom-save-all' Function.
2927 ;;;###autoload
2928 (defcustom custom-file nil
2929 "File used for storing customization information.
2930 The default is nil, which means to use your init file
2931 as specified by `user-init-file'. If you specify some other file,
2932 you need to explicitly load that file for the settings to take effect."
2933 :type '(choice (const :tag "Your Emacs init file" nil) file)
2934 :group 'customize)
2935
2936 (defun custom-save-delete (symbol)
2937 "Delete the call to SYMBOL form `custom-file'.
2938 Leave point at the location of the call, or after the last expression."
2939 (set-buffer (find-file-noselect (or custom-file user-init-file)))
2940 (goto-char (point-min))
2941 (catch 'found
2942 (while t
2943 (let ((sexp (condition-case nil
2944 (read (current-buffer))
2945 (end-of-file (throw 'found nil)))))
2946 (when (and (listp sexp)
2947 (eq (car sexp) symbol))
2948 (delete-region (save-excursion
2949 (backward-sexp)
2950 (point))
2951 (point))
2952 (throw 'found nil))))))
2953
2954 (defun custom-save-variables ()
2955 "Save all customized variables in `custom-file'."
2956 (save-excursion
2957 (custom-save-delete 'custom-set-variables)
2958 (let ((standard-output (current-buffer)))
2959 (unless (bolp)
2960 (princ "\n"))
2961 (princ "(custom-set-variables")
2962 (mapatoms (lambda (symbol)
2963 (let ((value (get symbol 'saved-value))
2964 (requests (get symbol 'custom-requests))
2965 (now (not (or (get symbol 'standard-value)
2966 (and (not (boundp symbol))
2967 (not (get symbol 'force-value)))))))
2968 (when value
2969 (princ "\n '(")
2970 (princ symbol)
2971 (princ " ")
2972 (prin1 (car value))
2973 (cond (requests
2974 (if now
2975 (princ " t ")
2976 (princ " nil "))
2977 (prin1 requests)
2978 (princ ")"))
2979 (now
2980 (princ " t)"))
2981 (t
2982 (princ ")")))))))
2983 (princ ")")
2984 (unless (looking-at "\n")
2985 (princ "\n")))))
2986
2987 (defun custom-save-faces ()
2988 "Save all customized faces in `custom-file'."
2989 (save-excursion
2990 (custom-save-delete 'custom-set-faces)
2991 (let ((standard-output (current-buffer)))
2992 (unless (bolp)
2993 (princ "\n"))
2994 (princ "(custom-set-faces")
2995 (let ((value (get 'default 'saved-face)))
2996 ;; The default face must be first, since it affects the others.
2997 (when value
2998 (princ "\n '(default ")
2999 (prin1 value)
3000 (if (or (get 'default 'face-defface-spec)
3001 (and (not (custom-facep 'default))
3002 (not (get 'default 'force-face))))
3003 (princ ")")
3004 (princ " t)"))))
3005 (mapatoms (lambda (symbol)
3006 (let ((value (get symbol 'saved-face)))
3007 (when (and (not (eq symbol 'default))
3008 ;; Don't print default face here.
3009 value)
3010 (princ "\n '(")
3011 (princ symbol)
3012 (princ " ")
3013 (prin1 value)
3014 (if (or (get symbol 'face-defface-spec)
3015 (and (not (custom-facep symbol))
3016 (not (get symbol 'force-face))))
3017 (princ ")")
3018 (princ " t)"))))))
3019 (princ ")")
3020 (unless (looking-at "\n")
3021 (princ "\n")))))
3022
3023 ;;;###autoload
3024 (defun customize-save-customized ()
3025 "Save all user options which have been set in this session."
3026 (interactive)
3027 (mapatoms (lambda (symbol)
3028 (let ((face (get symbol 'customized-face))
3029 (value (get symbol 'customized-value)))
3030 (when face
3031 (put symbol 'saved-face face)
3032 (put symbol 'customized-face nil))
3033 (when value
3034 (put symbol 'saved-value value)
3035 (put symbol 'customized-value nil)))))
3036 ;; We really should update all custom buffers here.
3037 (custom-save-all))
3038
3039 ;;;###autoload
3040 (defun custom-save-all ()
3041 "Save all customizations in `custom-file'."
3042 (let ((inhibit-read-only t))
3043 (custom-save-variables)
3044 (custom-save-faces)
3045 (save-excursion
3046 (set-buffer (find-file-noselect (or custom-file user-init-file)))
3047 (save-buffer))))
3048
3049 ;;; The Customize Menu.
3050
3051 ;;; Menu support
3052
3053 (defcustom custom-menu-nesting 2
3054 "Maximum nesting in custom menus."
3055 :type 'integer
3056 :group 'custom-menu)
3057
3058 (defun custom-face-menu-create (widget symbol)
3059 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3060 (vector (custom-unlispify-menu-entry symbol)
3061 `(customize-face ',symbol)
3062 t))
3063
3064 (defun custom-variable-menu-create (widget symbol)
3065 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3066 (let ((type (get symbol 'custom-type)))
3067 (unless (listp type)
3068 (setq type (list type)))
3069 (if (and type (widget-get type :custom-menu))
3070 (widget-apply type :custom-menu symbol)
3071 (vector (custom-unlispify-menu-entry symbol)
3072 `(customize-variable ',symbol)
3073 t))))
3074
3075 ;; Add checkboxes to boolean variable entries.
3076 (widget-put (get 'boolean 'widget-type)
3077 :custom-menu (lambda (widget symbol)
3078 (vector (custom-unlispify-menu-entry symbol)
3079 `(customize-variable ',symbol)
3080 ':style 'toggle
3081 ':selected symbol)))
3082
3083 (if (string-match "XEmacs" emacs-version)
3084 ;; XEmacs can create menus dynamically.
3085 (defun custom-group-menu-create (widget symbol)
3086 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3087 `( ,(custom-unlispify-menu-entry symbol t)
3088 :filter (lambda (&rest junk)
3089 (cdr (custom-menu-create ',symbol)))))
3090 ;; But emacs can't.
3091 (defun custom-group-menu-create (widget symbol)
3092 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3093 ;; Limit the nesting.
3094 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3095 (custom-menu-create symbol))))
3096
3097 ;;;###autoload
3098 (defun custom-menu-create (symbol)
3099 "Create menu for customization group SYMBOL.
3100 The menu is in a format applicable to `easy-menu-define'."
3101 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3102 `(customize-group ',symbol)
3103 t)))
3104 (if (and (or (not (boundp 'custom-menu-nesting))
3105 (>= custom-menu-nesting 0))
3106 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3107 (let ((custom-prefix-list (custom-prefix-add symbol
3108 custom-prefix-list))
3109 (members (custom-sort-items (get symbol 'custom-group)
3110 custom-menu-sort-alphabetically
3111 custom-menu-order-groups)))
3112 (custom-load-symbol symbol)
3113 `(,(custom-unlispify-menu-entry symbol t)
3114 ,item
3115 "--"
3116 ,@(mapcar (lambda (entry)
3117 (widget-apply (if (listp (nth 1 entry))
3118 (nth 1 entry)
3119 (list (nth 1 entry)))
3120 :custom-menu (nth 0 entry)))
3121 members)))
3122 item)))
3123
3124 ;;;###autoload
3125 (defun customize-menu-create (symbol &optional name)
3126 "Return a customize menu for customization group SYMBOL.
3127 If optional NAME is given, use that as the name of the menu.
3128 Otherwise the menu will be named `Customize'.
3129 The format is suitable for use with `easy-menu-define'."
3130 (unless name
3131 (setq name "Customize"))
3132 (if (string-match "XEmacs" emacs-version)
3133 ;; We can delay it under XEmacs.
3134 `(,name
3135 :filter (lambda (&rest junk)
3136 (cdr (custom-menu-create ',symbol))))
3137 ;; But we must create it now under Emacs.
3138 (cons name (cdr (custom-menu-create symbol)))))
3139
3140 ;;; The Custom Mode.
3141
3142 (defvar custom-mode-map nil
3143 "Keymap for `custom-mode'.")
3144
3145 (unless custom-mode-map
3146 (setq custom-mode-map (make-sparse-keymap))
3147 (set-keymap-parent custom-mode-map widget-keymap)
3148 (suppress-keymap custom-mode-map)
3149 (define-key custom-mode-map " " 'scroll-up)
3150 (define-key custom-mode-map "\177" 'scroll-down)
3151 (define-key custom-mode-map "q" 'bury-buffer)
3152 (define-key custom-mode-map "u" 'Custom-goto-parent)
3153 (define-key custom-mode-map "n" 'widget-forward)
3154 (define-key custom-mode-map "p" 'widget-backward)
3155 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3156
3157 (defun Custom-move-and-invoke (event)
3158 "Move to where you click, and if it is an active field, invoke it."
3159 (interactive "e")
3160 (mouse-set-point event)
3161 (if (widget-event-point event)
3162 (let* ((pos (widget-event-point event))
3163 (button (get-char-property pos 'button)))
3164 (if button
3165 (widget-button-click event)))))
3166
3167 (easy-menu-define Custom-mode-menu
3168 custom-mode-map
3169 "Menu used in customization buffers."
3170 `("Custom"
3171 ,(customize-menu-create 'customize)
3172 ["Set" Custom-set t]
3173 ["Save" Custom-save t]
3174 ["Reset to Current" Custom-reset-current t]
3175 ["Reset to Saved" Custom-reset-saved t]
3176 ["Reset to Standard Settings" Custom-reset-standard t]
3177 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
3178
3179 (defun Custom-goto-parent ()
3180 "Go to the parent group listed at the top of this buffer.
3181 If several parents are listed, go to the first of them."
3182 (interactive)
3183 (save-excursion
3184 (goto-char (point-min))
3185 (if (search-forward "\nGo to parent group: " nil t)
3186 (let* ((button (get-char-property (point) 'button))
3187 (parent (downcase (widget-get button :tag))))
3188 (customize-group parent)))))
3189
3190 (defcustom custom-mode-hook nil
3191 "Hook called when entering custom-mode."
3192 :type 'hook
3193 :group 'custom-buffer )
3194
3195 (defun custom-state-buffer-message (widget)
3196 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3197 (message "To install your edits, invoke [State] and choose the Set operation")))
3198
3199 (defun custom-mode ()
3200 "Major mode for editing customization buffers.
3201
3202 The following commands are available:
3203
3204 Move to next button or editable field. \\[widget-forward]
3205 Move to previous button or editable field. \\[widget-backward]
3206 \\<widget-field-keymap>\
3207 Complete content of editable text field. \\[widget-complete]
3208 \\<custom-mode-map>\
3209 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3210 Invoke button under point. \\[widget-button-press]
3211 Set all modifications. \\[Custom-set]
3212 Make all modifications default. \\[Custom-save]
3213 Reset all modified options. \\[Custom-reset-current]
3214 Reset all modified or set options. \\[Custom-reset-saved]
3215 Reset all options. \\[Custom-reset-standard]
3216
3217 Entry to this mode calls the value of `custom-mode-hook'
3218 if that value is non-nil."
3219 (kill-all-local-variables)
3220 (setq major-mode 'custom-mode
3221 mode-name "Custom")
3222 (use-local-map custom-mode-map)
3223 (easy-menu-add Custom-mode-menu)
3224 (make-local-variable 'custom-options)
3225 (make-local-variable 'widget-documentation-face)
3226 (setq widget-documentation-face 'custom-documentation-face)
3227 (make-local-variable 'widget-button-face)
3228 (setq widget-button-face 'custom-button-face)
3229 (make-local-hook 'widget-edit-functions)
3230 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3231 (run-hooks 'custom-mode-hook))
3232
3233 ;;; The End.
3234
3235 (provide 'cus-edit)
3236
3237 ;; cus-edit.el ends here