]> code.delx.au - gnu-emacs/blob - lisp/cus-edit.el
*** empty log message ***
[gnu-emacs] / 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 (get symbol 'custom-group))
813 t))))
814
815 (when (stringp group)
816 (if (string-equal "" group)
817 (setq group 'emacs)
818 (setq group (intern group))))
819 (let ((name (format "*Customize Group: %s*"
820 (custom-unlispify-tag-name group))))
821 (if (get-buffer name)
822 (switch-to-buffer name)
823 (custom-buffer-create (list (list group 'custom-group))
824 name
825 (concat " for group "
826 (custom-unlispify-tag-name group))))))
827
828 ;;;###autoload
829 (defun customize-group-other-window (symbol)
830 "Customize SYMBOL, which must be a customization group."
831 (interactive (list (completing-read "Customize group: (default emacs) "
832 obarray
833 (lambda (symbol)
834 (get symbol 'custom-group))
835 t)))
836
837 (when (stringp symbol)
838 (if (string-equal "" symbol)
839 (setq symbol 'emacs)
840 (setq symbol (intern symbol))))
841 (custom-buffer-create-other-window
842 (list (list symbol 'custom-group))
843 (format "*Customize Group: %s*" (custom-unlispify-tag-name symbol))))
844
845 ;;;###autoload
846 (defalias 'customize-variable 'customize-option)
847
848 ;;;###autoload
849 (defun customize-option (symbol)
850 "Customize SYMBOL, which must be a user option variable."
851 (interactive (custom-variable-prompt))
852 (custom-buffer-create (list (list symbol 'custom-variable))
853 (format "*Customize Option: %s*"
854 (custom-unlispify-tag-name symbol))))
855
856 ;;;###autoload
857 (defalias 'customize-variable-other-window 'customize-option-other-window)
858
859 ;;;###autoload
860 (defun customize-option-other-window (symbol)
861 "Customize SYMBOL, which must be a user option variable.
862 Show the buffer in another window, but don't select it."
863 (interactive (custom-variable-prompt))
864 (custom-buffer-create-other-window
865 (list (list symbol 'custom-variable))
866 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
867
868 ;;;###autoload
869 (defun customize-face (&optional symbol)
870 "Customize SYMBOL, which should be a face name or nil.
871 If SYMBOL is nil, customize all faces."
872 (interactive (list (completing-read "Customize face: (default all) "
873 obarray 'custom-facep)))
874 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
875 (custom-buffer-create (custom-sort-items
876 (mapcar (lambda (symbol)
877 (list symbol 'custom-face))
878 (face-list))
879 t nil)
880 "*Customize Faces*")
881 (when (stringp symbol)
882 (setq symbol (intern symbol)))
883 (unless (symbolp symbol)
884 (error "Should be a symbol %S" symbol))
885 (custom-buffer-create (list (list symbol 'custom-face))
886 (format "*Customize Face: %s*"
887 (custom-unlispify-tag-name symbol)))))
888
889 ;;;###autoload
890 (defun customize-face-other-window (&optional symbol)
891 "Show customization buffer for FACE in other window."
892 (interactive (list (completing-read "Customize face: "
893 obarray 'custom-facep)))
894 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
895 ()
896 (if (stringp symbol)
897 (setq symbol (intern symbol)))
898 (unless (symbolp symbol)
899 (error "Should be a symbol %S" symbol))
900 (custom-buffer-create-other-window
901 (list (list symbol 'custom-face))
902 (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
903
904 ;;;###autoload
905 (defun customize-customized ()
906 "Customize all user options set since the last save in this session."
907 (interactive)
908 (let ((found nil))
909 (mapatoms (lambda (symbol)
910 (and (get symbol 'customized-face)
911 (custom-facep symbol)
912 (push (list symbol 'custom-face) found))
913 (and (get symbol 'customized-value)
914 (boundp symbol)
915 (push (list symbol 'custom-variable) found))))
916 (if (not found)
917 (error "No customized user options")
918 (custom-buffer-create (custom-sort-items found t nil)
919 "*Customize Customized*"))))
920
921 ;;;###autoload
922 (defun customize-saved ()
923 "Customize all already saved user options."
924 (interactive)
925 (let ((found nil))
926 (mapatoms (lambda (symbol)
927 (and (get symbol 'saved-face)
928 (custom-facep symbol)
929 (push (list symbol 'custom-face) found))
930 (and (get symbol 'saved-value)
931 (boundp symbol)
932 (push (list symbol 'custom-variable) found))))
933 (if (not found )
934 (error "No saved user options")
935 (custom-buffer-create (custom-sort-items found t nil)
936 "*Customize Saved*"))))
937
938 ;;;###autoload
939 (defun customize-apropos (regexp &optional all)
940 "Customize all user options matching REGEXP.
941 If ALL is `options', include only options.
942 If ALL is `faces', include only faces.
943 If ALL is `groups', include only groups.
944 If ALL is t (interactively, with prefix arg), include options which are not
945 user-settable, as well as faces and groups."
946 (interactive "sCustomize regexp: \nP")
947 (let ((found nil))
948 (mapatoms (lambda (symbol)
949 (when (string-match regexp (symbol-name symbol))
950 (when (and (not (memq all '(faces options)))
951 (get symbol 'custom-group))
952 (push (list symbol 'custom-group) found))
953 (when (and (not (memq all '(options groups)))
954 (custom-facep symbol))
955 (push (list symbol 'custom-face) found))
956 (when (and (not (memq all '(groups faces)))
957 (boundp symbol)
958 (or (get symbol 'saved-value)
959 (get symbol 'standard-value)
960 (if (memq all '(nil options))
961 (user-variable-p symbol)
962 (get symbol 'variable-documentation))))
963 (push (list symbol 'custom-variable) found)))))
964 (if (not found)
965 (error "No matches")
966 (custom-buffer-create (custom-sort-items found t
967 custom-buffer-order-groups)
968 "*Customize Apropos*"))))
969
970 ;;;###autoload
971 (defun customize-apropos-options (regexp &optional arg)
972 "Customize all user options matching REGEXP.
973 With prefix arg, include options which are not user-settable."
974 (interactive "sCustomize regexp: \nP")
975 (customize-apropos regexp (or arg 'options)))
976
977 ;;;###autoload
978 (defun customize-apropos-faces (regexp)
979 "Customize all user faces matching REGEXP."
980 (interactive "sCustomize regexp: \n")
981 (customize-apropos regexp 'faces))
982
983 ;;;###autoload
984 (defun customize-apropos-groups (regexp)
985 "Customize all user groups matching REGEXP."
986 (interactive "sCustomize regexp: \n")
987 (customize-apropos regexp 'groups))
988
989 ;;; Buffer.
990
991 (defcustom custom-buffer-style 'links
992 "Control the presentation style for customization buffers.
993 The value should be a symbol, one of:
994
995 brackets: groups nest within each other with big horizontal brackets.
996 links: groups have links to subgroups."
997 :type '(radio (const brackets)
998 (const links))
999 :group 'custom-buffer)
1000
1001 (defcustom custom-buffer-indent 3
1002 "Number of spaces to indent nested groups."
1003 :type 'integer
1004 :group 'custom-buffer)
1005
1006 ;;;###autoload
1007 (defun custom-buffer-create (options &optional name description)
1008 "Create a buffer containing OPTIONS.
1009 Optional NAME is the name of the buffer.
1010 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1011 SYMBOL is a customization option, and WIDGET is a widget for editing
1012 that option."
1013 (unless name (setq name "*Customization*"))
1014 (kill-buffer (get-buffer-create name))
1015 (switch-to-buffer (get-buffer-create name))
1016 (custom-buffer-create-internal options description))
1017
1018 ;;;###autoload
1019 (defun custom-buffer-create-other-window (options &optional name description)
1020 "Create a buffer containing OPTIONS.
1021 Optional NAME is the name of the buffer.
1022 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1023 SYMBOL is a customization option, and WIDGET is a widget for editing
1024 that option."
1025 (unless name (setq name "*Customization*"))
1026 (kill-buffer (get-buffer-create name))
1027 (let ((window (selected-window)))
1028 (switch-to-buffer-other-window (get-buffer-create name))
1029 (custom-buffer-create-internal options description)
1030 (select-window window)))
1031
1032 (defcustom custom-reset-button-menu nil
1033 "If non-nil, only show a single reset button in customize buffers.
1034 This button will have a menu with all three reset operations."
1035 :type 'boolean
1036 :group 'custom-buffer)
1037
1038 (defun custom-buffer-create-internal (options &optional description)
1039 (message "Creating customization buffer...")
1040 (custom-mode)
1041 (widget-insert "This is a customization buffer")
1042 (if description
1043 (widget-insert description))
1044 (widget-insert ".
1045 Square brackets show active fields; type RET or click mouse-1
1046 on an active field to invoke its action. Editing an option value
1047 changes the text in the buffer; invoke the State button and
1048 choose the Set operation to set the option value.
1049 Invoke ")
1050 (widget-create 'info-link
1051 :tag "Help"
1052 :help-echo "Read the online help."
1053 "(emacs)Easy Customization")
1054 (widget-insert " for more information.\n\n")
1055 (message "Creating customization buttons...")
1056 (widget-insert "Operate on everything in this buffer:\n ")
1057 (widget-create 'push-button
1058 :tag "Set for Current Session"
1059 :help-echo "\
1060 Make your editing in this buffer take effect for this session."
1061 :action (lambda (widget &optional event)
1062 (Custom-set)))
1063 (widget-insert " ")
1064 (widget-create 'push-button
1065 :tag "Save for Future Sessions"
1066 :help-echo "\
1067 Make your editing in this buffer take effect for future Emacs sessions."
1068 :action (lambda (widget &optional event)
1069 (Custom-save)))
1070 (if custom-reset-button-menu
1071 (progn
1072 (widget-insert " ")
1073 (widget-create 'push-button
1074 :tag "Reset"
1075 :help-echo "Show a menu with reset operations."
1076 :mouse-down-action (lambda (&rest junk) t)
1077 :action (lambda (widget &optional event)
1078 (custom-reset event))))
1079 (widget-insert "\n ")
1080 (widget-create 'push-button
1081 :tag "Reset"
1082 :help-echo "\
1083 Reset all edited text in this buffer to reflect current values."
1084 :action 'Custom-reset-current)
1085 (widget-insert " ")
1086 (widget-create 'push-button
1087 :tag "Reset to Saved"
1088 :help-echo "\
1089 Reset all values in this buffer to their saved settings."
1090 :action 'Custom-reset-saved)
1091 (widget-insert " ")
1092 (widget-create 'push-button
1093 :tag "Reset to Standard"
1094 :help-echo "\
1095 Reset all values in this buffer to their standard settings."
1096 :action 'Custom-reset-standard))
1097 (widget-insert " ")
1098 (widget-create 'push-button
1099 :tag "Bury Buffer"
1100 :help-echo "Bury the buffer."
1101 :action (lambda (widget &optional event)
1102 (bury-buffer)))
1103 (widget-insert "\n\n")
1104 (message "Creating customization items...")
1105 (setq custom-options
1106 (if (= (length options) 1)
1107 (mapcar (lambda (entry)
1108 (widget-create (nth 1 entry)
1109 :documentation-shown t
1110 :custom-state 'unknown
1111 :tag (custom-unlispify-tag-name
1112 (nth 0 entry))
1113 :value (nth 0 entry)))
1114 options)
1115 (let ((count 0)
1116 (length (length options)))
1117 (mapcar (lambda (entry)
1118 (prog2
1119 (message "Creating customization items %2d%%..."
1120 (/ (* 100.0 count) length))
1121 (widget-create (nth 1 entry)
1122 :tag (custom-unlispify-tag-name
1123 (nth 0 entry))
1124 :value (nth 0 entry))
1125 (setq count (1+ count))
1126 (unless (eq (preceding-char) ?\n)
1127 (widget-insert "\n"))
1128 (widget-insert "\n")))
1129 options))))
1130 (unless (eq (preceding-char) ?\n)
1131 (widget-insert "\n"))
1132 (message "Creating customization items %2d%%...done" 100)
1133 (unless (eq custom-buffer-style 'tree)
1134 (mapcar 'custom-magic-reset custom-options))
1135 (message "Creating customization setup...")
1136 (widget-setup)
1137 (goto-char (point-min))
1138 (message "Creating customization buffer...done"))
1139
1140 ;;; The Tree Browser.
1141
1142 ;;;###autoload
1143 (defun customize-browse (&optional group)
1144 "Create a tree browser for the customize hierarchy."
1145 (interactive)
1146 (unless group
1147 (setq group 'emacs))
1148 (let ((name "*Customize Browser*"))
1149 (kill-buffer (get-buffer-create name))
1150 (switch-to-buffer (get-buffer-create name)))
1151 (custom-mode)
1152 (widget-insert "\
1153 Square brackets show active fields; type RET or click mouse-1
1154 on an active field to invoke its action.
1155 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1156 (if custom-browse-only-groups
1157 (widget-insert "\
1158 Invoke the [Group] button below to edit that item in another window.\n\n")
1159 (widget-insert "Invoke the ")
1160 (widget-create 'item
1161 :format "%t"
1162 :tag "[Group]"
1163 :tag-glyph "folder")
1164 (widget-insert ", ")
1165 (widget-create 'item
1166 :format "%t"
1167 :tag "[Face]"
1168 :tag-glyph "face")
1169 (widget-insert ", and ")
1170 (widget-create 'item
1171 :format "%t"
1172 :tag "[Option]"
1173 :tag-glyph "option")
1174 (widget-insert " buttons below to edit that
1175 item in another window.\n\n"))
1176 (let ((custom-buffer-style 'tree))
1177 (widget-create 'custom-group
1178 :custom-last t
1179 :custom-state 'unknown
1180 :tag (custom-unlispify-tag-name group)
1181 :value group))
1182 (goto-char (point-min)))
1183
1184 (define-widget 'custom-browse-visibility 'item
1185 "Control visibility of of items in the customize tree browser."
1186 :format "%[[%t]%]"
1187 :action 'custom-browse-visibility-action)
1188
1189 (defun custom-browse-visibility-action (widget &rest ignore)
1190 (let ((custom-buffer-style 'tree))
1191 (custom-toggle-parent widget)))
1192
1193 (define-widget 'custom-browse-group-tag 'push-button
1194 "Show parent in other window when activated."
1195 :tag "Group"
1196 :tag-glyph "folder"
1197 :action 'custom-browse-group-tag-action)
1198
1199 (defun custom-browse-group-tag-action (widget &rest ignore)
1200 (let ((parent (widget-get widget :parent)))
1201 (customize-group-other-window (widget-value parent))))
1202
1203 (define-widget 'custom-browse-variable-tag 'push-button
1204 "Show parent in other window when activated."
1205 :tag "Option"
1206 :tag-glyph "option"
1207 :action 'custom-browse-variable-tag-action)
1208
1209 (defun custom-browse-variable-tag-action (widget &rest ignore)
1210 (let ((parent (widget-get widget :parent)))
1211 (customize-variable-other-window (widget-value parent))))
1212
1213 (define-widget 'custom-browse-face-tag 'push-button
1214 "Show parent in other window when activated."
1215 :tag "Face"
1216 :tag-glyph "face"
1217 :action 'custom-browse-face-tag-action)
1218
1219 (defun custom-browse-face-tag-action (widget &rest ignore)
1220 (let ((parent (widget-get widget :parent)))
1221 (customize-face-other-window (widget-value parent))))
1222
1223 (defconst custom-browse-alist '((" " "space")
1224 (" | " "vertical")
1225 ("-\\ " "top")
1226 (" |-" "middle")
1227 (" `-" "bottom")))
1228
1229 (defun custom-browse-insert-prefix (prefix)
1230 "Insert PREFIX. On XEmacs convert it to line graphics."
1231 (if nil ; (string-match "XEmacs" emacs-version)
1232 (progn
1233 (insert "*")
1234 (while (not (string-equal prefix ""))
1235 (let ((entry (substring prefix 0 3)))
1236 (setq prefix (substring prefix 3))
1237 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1238 (name (nth 1 (assoc entry custom-browse-alist))))
1239 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1240 (overlay-put overlay 'start-open t)
1241 (overlay-put overlay 'end-open t)))))
1242 (insert prefix)))
1243
1244 ;;; Modification of Basic Widgets.
1245 ;;
1246 ;; We add extra properties to the basic widgets needed here. This is
1247 ;; fine, as long as we are careful to stay within out own namespace.
1248 ;;
1249 ;; We want simple widgets to be displayed by default, but complex
1250 ;; widgets to be hidden.
1251
1252 (widget-put (get 'item 'widget-type) :custom-show t)
1253 (widget-put (get 'editable-field 'widget-type)
1254 :custom-show (lambda (widget value)
1255 (let ((pp (pp-to-string value)))
1256 (cond ((string-match "\n" pp)
1257 nil)
1258 ((> (length pp) 40)
1259 nil)
1260 (t t)))))
1261 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1262
1263 ;;; The `custom-manual' Widget.
1264
1265 (define-widget 'custom-manual 'info-link
1266 "Link to the manual entry for this customization option."
1267 :help-echo "Read the manual entry for this option."
1268 :tag "Manual")
1269
1270 ;;; The `custom-magic' Widget.
1271
1272 (defgroup custom-magic-faces nil
1273 "Faces used by the magic button."
1274 :group 'custom-faces
1275 :group 'custom-buffer)
1276
1277 (defface custom-invalid-face '((((class color))
1278 (:foreground "yellow" :background "red"))
1279 (t
1280 (:bold t :italic t :underline t)))
1281 "Face used when the customize item is invalid."
1282 :group 'custom-magic-faces)
1283
1284 (defface custom-rogue-face '((((class color))
1285 (:foreground "pink" :background "black"))
1286 (t
1287 (:underline t)))
1288 "Face used when the customize item is not defined for customization."
1289 :group 'custom-magic-faces)
1290
1291 (defface custom-modified-face '((((class color))
1292 (:foreground "white" :background "blue"))
1293 (t
1294 (:italic t :bold)))
1295 "Face used when the customize item has been modified."
1296 :group 'custom-magic-faces)
1297
1298 (defface custom-set-face '((((class color))
1299 (:foreground "blue" :background "white"))
1300 (t
1301 (:italic t)))
1302 "Face used when the customize item has been set."
1303 :group 'custom-magic-faces)
1304
1305 (defface custom-changed-face '((((class color))
1306 (:foreground "white" :background "blue"))
1307 (t
1308 (:italic t)))
1309 "Face used when the customize item has been changed."
1310 :group 'custom-magic-faces)
1311
1312 (defface custom-saved-face '((t (:underline t)))
1313 "Face used when the customize item has been saved."
1314 :group 'custom-magic-faces)
1315
1316 (defconst custom-magic-alist '((nil "#" underline "\
1317 uninitialized, you should not see this.")
1318 (unknown "?" italic "\
1319 unknown, you should not see this.")
1320 (hidden "-" default "\
1321 hidden, invoke \"Show\" in the previous line to show." "\
1322 group now hidden, invoke \"Show\", above, to show contents.")
1323 (invalid "x" custom-invalid-face "\
1324 the value displayed for this %c is invalid and cannot be set.")
1325 (modified "*" custom-modified-face "\
1326 you have edited the value as text, but you have not set the %c." "\
1327 you have edited something in this group, but not set it.")
1328 (set "+" custom-set-face "\
1329 you have set this %c, but not saved it for future sessions." "\
1330 something in this group has been set, but not saved.")
1331 (changed ":" custom-changed-face "\
1332 this %c has been changed outside the customize buffer." "\
1333 something in this group has been changed outside customize.")
1334 (saved "!" custom-saved-face "\
1335 this %c has been set and saved." "\
1336 something in this group has been set and saved.")
1337 (rogue "@" custom-rogue-face "\
1338 this %c has not been changed with customize." "\
1339 something in this group is not prepared for customization.")
1340 (standard " " nil "\
1341 this %c is unchanged from its standard setting." "\
1342 visible group members are all at standard settings."))
1343 "Alist of customize option states.
1344 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1345
1346 STATE is one of the following symbols:
1347
1348 `nil'
1349 For internal use, should never occur.
1350 `unknown'
1351 For internal use, should never occur.
1352 `hidden'
1353 This item is not being displayed.
1354 `invalid'
1355 This item is modified, but has an invalid form.
1356 `modified'
1357 This item is modified, and has a valid form.
1358 `set'
1359 This item has been set but not saved.
1360 `changed'
1361 The current value of this item has been changed temporarily.
1362 `saved'
1363 This item is marked for saving.
1364 `rogue'
1365 This item has no customization information.
1366 `standard'
1367 This item is unchanged from the standard setting.
1368
1369 MAGIC is a string used to present that state.
1370
1371 FACE is a face used to present the state.
1372
1373 ITEM-DESC is a string describing the state for options.
1374
1375 GROUP-DESC is a string describing the state for groups. If this is
1376 left out, ITEM-DESC will be used.
1377
1378 The string %c in either description will be replaced with the
1379 category of the item. These are `group'. `option', and `face'.
1380
1381 The list should be sorted most significant first.")
1382
1383 (defcustom custom-magic-show 'long
1384 "If non-nil, show textual description of the state.
1385 If `long', show a full-line description, not just one word."
1386 :type '(choice (const :tag "no" nil)
1387 (const short)
1388 (const long))
1389 :group 'custom-buffer)
1390
1391 (defcustom custom-magic-show-hidden '(option face)
1392 "Control whether the State button is shown for hidden items.
1393 The value should be a list with the custom categories where the State
1394 button should be visible. Possible categories are `group', `option',
1395 and `face'."
1396 :type '(set (const group) (const option) (const face))
1397 :group 'custom-buffer)
1398
1399 (defcustom custom-magic-show-button nil
1400 "Show a \"magic\" button indicating the state of each customization option."
1401 :type 'boolean
1402 :group 'custom-buffer)
1403
1404 (define-widget 'custom-magic 'default
1405 "Show and manipulate state for a customization option."
1406 :format "%v"
1407 :action 'widget-parent-action
1408 :notify 'ignore
1409 :value-get 'ignore
1410 :value-create 'custom-magic-value-create
1411 :value-delete 'widget-children-value-delete)
1412
1413 (defun widget-magic-mouse-down-action (widget &optional event)
1414 ;; Non-nil unless hidden.
1415 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1416 :custom-state)
1417 'hidden)))
1418
1419 (defun custom-magic-value-create (widget)
1420 ;; Create compact status report for WIDGET.
1421 (let* ((parent (widget-get widget :parent))
1422 (state (widget-get parent :custom-state))
1423 (hidden (eq state 'hidden))
1424 (entry (assq state custom-magic-alist))
1425 (magic (nth 1 entry))
1426 (face (nth 2 entry))
1427 (category (widget-get parent :custom-category))
1428 (text (or (and (eq category 'group)
1429 (nth 4 entry))
1430 (nth 3 entry)))
1431 (form (widget-get parent :custom-form))
1432 children)
1433 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1434 (setq text (concat (match-string 1 text)
1435 (symbol-name category)
1436 (match-string 2 text))))
1437 (when (and custom-magic-show
1438 (or (not hidden)
1439 (memq category custom-magic-show-hidden)))
1440 (insert " ")
1441 (when (and (eq category 'group)
1442 (not (and (eq custom-buffer-style 'links)
1443 (> (widget-get parent :custom-level) 1))))
1444 (insert-char ?\ (* custom-buffer-indent
1445 (widget-get parent :custom-level))))
1446 (push (widget-create-child-and-convert
1447 widget 'choice-item
1448 :help-echo "Change the state of this item."
1449 :format (if hidden "%t" "%[%t%]")
1450 :button-prefix 'widget-push-button-prefix
1451 :button-suffix 'widget-push-button-suffix
1452 :mouse-down-action 'widget-magic-mouse-down-action
1453 :tag "State")
1454 children)
1455 (insert ": ")
1456 (let ((start (point)))
1457 (if (eq custom-magic-show 'long)
1458 (insert text)
1459 (insert (symbol-name state)))
1460 (cond ((eq form 'lisp)
1461 (insert " (lisp)"))
1462 ((eq form 'mismatch)
1463 (insert " (mismatch)")))
1464 (put-text-property start (point) 'face 'custom-state-face))
1465 (insert "\n"))
1466 (when (and (eq category 'group)
1467 (not (and (eq custom-buffer-style 'links)
1468 (> (widget-get parent :custom-level) 1))))
1469 (insert-char ?\ (* custom-buffer-indent
1470 (widget-get parent :custom-level))))
1471 (when custom-magic-show-button
1472 (when custom-magic-show
1473 (let ((indent (widget-get parent :indent)))
1474 (when indent
1475 (insert-char ? indent))))
1476 (push (widget-create-child-and-convert
1477 widget 'choice-item
1478 :mouse-down-action 'widget-magic-mouse-down-action
1479 :button-face face
1480 :button-prefix ""
1481 :button-suffix ""
1482 :help-echo "Change the state."
1483 :format (if hidden "%t" "%[%t%]")
1484 :tag (if (memq form '(lisp mismatch))
1485 (concat "(" magic ")")
1486 (concat "[" magic "]")))
1487 children)
1488 (insert " "))
1489 (widget-put widget :children children)))
1490
1491 (defun custom-magic-reset (widget)
1492 "Redraw the :custom-magic property of WIDGET."
1493 (let ((magic (widget-get widget :custom-magic)))
1494 (widget-value-set magic (widget-value magic))))
1495
1496 ;;; The `custom' Widget.
1497
1498 (defface custom-button-face nil
1499 "Face used for buttons in customization buffers."
1500 :group 'custom-faces)
1501
1502 (defface custom-documentation-face nil
1503 "Face used for documentation strings in customization buffers."
1504 :group 'custom-faces)
1505
1506 (defface custom-state-face '((((class color)
1507 (background dark))
1508 (:foreground "lime green"))
1509 (((class color)
1510 (background light))
1511 (:foreground "dark green"))
1512 (t nil))
1513 "Face used for State descriptions in the customize buffer."
1514 :group 'custom-faces)
1515
1516 (define-widget 'custom 'default
1517 "Customize a user option."
1518 :format "%v"
1519 :convert-widget 'custom-convert-widget
1520 :notify 'custom-notify
1521 :custom-prefix ""
1522 :custom-level 1
1523 :custom-state 'hidden
1524 :documentation-property 'widget-subclass-responsibility
1525 :value-create 'widget-subclass-responsibility
1526 :value-delete 'widget-children-value-delete
1527 :value-get 'widget-value-value-get
1528 :validate 'widget-children-validate
1529 :match (lambda (widget value) (symbolp value)))
1530
1531 (defun custom-convert-widget (widget)
1532 ;; Initialize :value and :tag from :args in WIDGET.
1533 (let ((args (widget-get widget :args)))
1534 (when args
1535 (widget-put widget :value (widget-apply widget
1536 :value-to-internal (car args)))
1537 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1538 (widget-put widget :args nil)))
1539 widget)
1540
1541 (defun custom-notify (widget &rest args)
1542 "Keep track of changes."
1543 (let ((state (widget-get widget :custom-state)))
1544 (unless (eq state 'modified)
1545 (unless (memq state '(nil unknown hidden))
1546 (widget-put widget :custom-state 'modified))
1547 (custom-magic-reset widget)
1548 (apply 'widget-default-notify widget args))))
1549
1550 (defun custom-redraw (widget)
1551 "Redraw WIDGET with current settings."
1552 (let ((line (count-lines (point-min) (point)))
1553 (column (current-column))
1554 (pos (point))
1555 (from (marker-position (widget-get widget :from)))
1556 (to (marker-position (widget-get widget :to))))
1557 (save-excursion
1558 (widget-value-set widget (widget-value widget))
1559 (custom-redraw-magic widget))
1560 (when (and (>= pos from) (<= pos to))
1561 (condition-case nil
1562 (progn
1563 (if (> column 0)
1564 (goto-line line)
1565 (goto-line (1+ line)))
1566 (move-to-column column))
1567 (error nil)))))
1568
1569 (defun custom-redraw-magic (widget)
1570 "Redraw WIDGET state with current settings."
1571 (while widget
1572 (let ((magic (widget-get widget :custom-magic)))
1573 (cond (magic
1574 (widget-value-set magic (widget-value magic))
1575 (when (setq widget (widget-get widget :group))
1576 (custom-group-state-update widget)))
1577 (t
1578 (setq widget nil)))))
1579 (widget-setup))
1580
1581 (defun custom-show (widget value)
1582 "Non-nil if WIDGET should be shown with VALUE by default."
1583 (let ((show (widget-get widget :custom-show)))
1584 (cond ((null show)
1585 nil)
1586 ((eq t show)
1587 t)
1588 (t
1589 (funcall show widget value)))))
1590
1591 (defvar custom-load-recursion nil
1592 "Hack to avoid recursive dependencies.")
1593
1594 (defun custom-load-symbol (symbol)
1595 "Load all dependencies for SYMBOL."
1596 (unless custom-load-recursion
1597 (let ((custom-load-recursion t)
1598 (loads (get symbol 'custom-loads))
1599 load)
1600 (while loads
1601 (setq load (car loads)
1602 loads (cdr loads))
1603 (cond ((symbolp load)
1604 (condition-case nil
1605 (require load)
1606 (error nil)))
1607 ;; Don't reload a file already loaded.
1608 ((and (boundp 'preloaded-file-list)
1609 (member load preloaded-file-list)))
1610 ((assoc load load-history))
1611 ((assoc (locate-library load) load-history))
1612 (t
1613 (condition-case nil
1614 ;; Without this, we would load cus-edit recursively.
1615 ;; We are still loading it when we call this,
1616 ;; and it is not in load-history yet.
1617 (or (equal load "cus-edit")
1618 (load-library load))
1619 (error nil))))))))
1620
1621 (defun custom-load-widget (widget)
1622 "Load all dependencies for WIDGET."
1623 (custom-load-symbol (widget-value widget)))
1624
1625 (defun custom-unloaded-symbol-p (symbol)
1626 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1627 (let ((found nil)
1628 (loads (get symbol 'custom-loads))
1629 load)
1630 (while loads
1631 (setq load (car loads)
1632 loads (cdr loads))
1633 (cond ((symbolp load)
1634 (unless (featurep load)
1635 (setq found t)))
1636 ((assoc load load-history))
1637 ((assoc (locate-library load) load-history)
1638 (message nil))
1639 (t
1640 (setq found t))))
1641 found))
1642
1643 (defun custom-unloaded-widget-p (widget)
1644 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1645 (custom-unloaded-symbol-p (widget-value widget)))
1646
1647 (defun custom-toggle-hide (widget)
1648 "Toggle visibility of WIDGET."
1649 (custom-load-widget widget)
1650 (let ((state (widget-get widget :custom-state)))
1651 (cond ((memq state '(invalid modified))
1652 (error "There are unset changes"))
1653 ((eq state 'hidden)
1654 (widget-put widget :custom-state 'unknown))
1655 (t
1656 (widget-put widget :documentation-shown nil)
1657 (widget-put widget :custom-state 'hidden)))
1658 (custom-redraw widget)
1659 (widget-setup)))
1660
1661 (defun custom-toggle-parent (widget &rest ignore)
1662 "Toggle visibility of parent of WIDGET."
1663 (custom-toggle-hide (widget-get widget :parent)))
1664
1665 (defun custom-add-see-also (widget &optional prefix)
1666 "Add `See also ...' to WIDGET if there are any links.
1667 Insert PREFIX first if non-nil."
1668 (let* ((symbol (widget-get widget :value))
1669 (links (get symbol 'custom-links))
1670 (many (> (length links) 2))
1671 (buttons (widget-get widget :buttons))
1672 (indent (widget-get widget :indent)))
1673 (when links
1674 (when indent
1675 (insert-char ?\ indent))
1676 (when prefix
1677 (insert prefix))
1678 (insert "See also ")
1679 (while links
1680 (push (widget-create-child-and-convert widget (car links))
1681 buttons)
1682 (setq links (cdr links))
1683 (cond ((null links)
1684 (insert ".\n"))
1685 ((null (cdr links))
1686 (if many
1687 (insert ", and ")
1688 (insert " and ")))
1689 (t
1690 (insert ", "))))
1691 (widget-put widget :buttons buttons))))
1692
1693 (defun custom-add-parent-links (widget &optional initial-string)
1694 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1695 The value if non-nil if any parents were found.
1696 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1697 (let ((name (widget-value widget))
1698 (type (widget-type widget))
1699 (buttons (widget-get widget :buttons))
1700 (start (point))
1701 found)
1702 (insert (or initial-string "Parent groups:"))
1703 (mapatoms (lambda (symbol)
1704 (let ((entry (assq name (get symbol 'custom-group))))
1705 (when (eq (nth 1 entry) type)
1706 (insert " ")
1707 (push (widget-create-child-and-convert
1708 widget 'custom-group-link
1709 :tag (custom-unlispify-tag-name symbol)
1710 symbol)
1711 buttons)
1712 (setq found t)))))
1713 (widget-put widget :buttons buttons)
1714 (if found
1715 (insert "\n")
1716 (delete-region start (point)))
1717 found))
1718
1719 ;;; The `custom-variable' Widget.
1720
1721 (defface custom-variable-tag-face '((((class color)
1722 (background dark))
1723 (:foreground "light blue" :underline t))
1724 (((class color)
1725 (background light))
1726 (:foreground "blue" :underline t))
1727 (t (:underline t)))
1728 "Face used for unpushable variable tags."
1729 :group 'custom-faces)
1730
1731 (defface custom-variable-button-face '((t (:underline t :bold t)))
1732 "Face used for pushable variable tags."
1733 :group 'custom-faces)
1734
1735 (define-widget 'custom-variable 'custom
1736 "Customize variable."
1737 :format "%v"
1738 :help-echo "Set or reset this variable."
1739 :documentation-property 'variable-documentation
1740 :custom-category 'option
1741 :custom-state nil
1742 :custom-menu 'custom-variable-menu-create
1743 :custom-form 'edit
1744 :value-create 'custom-variable-value-create
1745 :action 'custom-variable-action
1746 :custom-set 'custom-variable-set
1747 :custom-save 'custom-variable-save
1748 :custom-reset-current 'custom-redraw
1749 :custom-reset-saved 'custom-variable-reset-saved
1750 :custom-reset-standard 'custom-variable-reset-standard)
1751
1752 (defun custom-variable-type (symbol)
1753 "Return a widget suitable for editing the value of SYMBOL.
1754 If SYMBOL has a `custom-type' property, use that.
1755 Otherwise, look up symbol in `custom-guess-type-alist'."
1756 (let* ((type (or (get symbol 'custom-type)
1757 (and (not (get symbol 'standard-value))
1758 (custom-guess-type symbol))
1759 'sexp))
1760 (options (get symbol 'custom-options))
1761 (tmp (if (listp type)
1762 (copy-sequence type)
1763 (list type))))
1764 (when options
1765 (widget-put tmp :options options))
1766 tmp))
1767
1768 (defun custom-variable-value-create (widget)
1769 "Here is where you edit the variables value."
1770 (custom-load-widget widget)
1771 (let* ((buttons (widget-get widget :buttons))
1772 (children (widget-get widget :children))
1773 (form (widget-get widget :custom-form))
1774 (state (widget-get widget :custom-state))
1775 (symbol (widget-get widget :value))
1776 (tag (widget-get widget :tag))
1777 (type (custom-variable-type symbol))
1778 (conv (widget-convert type))
1779 (get (or (get symbol 'custom-get) 'default-value))
1780 (prefix (widget-get widget :custom-prefix))
1781 (last (widget-get widget :custom-last))
1782 (value (if (default-boundp symbol)
1783 (funcall get symbol)
1784 (widget-get conv :value))))
1785 ;; If the widget is new, the child determine whether it is hidden.
1786 (cond (state)
1787 ((custom-show type value)
1788 (setq state 'unknown))
1789 (t
1790 (setq state 'hidden)))
1791 ;; If we don't know the state, see if we need to edit it in lisp form.
1792 (when (eq state 'unknown)
1793 (unless (widget-apply conv :match value)
1794 ;; (widget-apply (widget-convert type) :match value)
1795 (setq form 'mismatch)))
1796 ;; Now we can create the child widget.
1797 (cond ((eq custom-buffer-style 'tree)
1798 (insert prefix (if last " `--- " " |--- "))
1799 (push (widget-create-child-and-convert
1800 widget 'custom-browse-variable-tag)
1801 buttons)
1802 (insert " " tag "\n")
1803 (widget-put widget :buttons buttons))
1804 ((eq state 'hidden)
1805 ;; Indicate hidden value.
1806 (push (widget-create-child-and-convert
1807 widget 'item
1808 :format "%{%t%}: "
1809 :sample-face 'custom-variable-tag-face
1810 :tag tag
1811 :parent widget)
1812 buttons)
1813 (push (widget-create-child-and-convert
1814 widget 'visibility
1815 :help-echo "Show the value of this option."
1816 :action 'custom-toggle-parent
1817 nil)
1818 buttons))
1819 ((memq form '(lisp mismatch))
1820 ;; In lisp mode edit the saved value when possible.
1821 (let* ((value (cond ((get symbol 'saved-value)
1822 (car (get symbol 'saved-value)))
1823 ((get symbol 'standard-value)
1824 (car (get symbol 'standard-value)))
1825 ((default-boundp symbol)
1826 (custom-quote (funcall get symbol)))
1827 (t
1828 (custom-quote (widget-get conv :value))))))
1829 (insert (symbol-name symbol) ": ")
1830 (push (widget-create-child-and-convert
1831 widget 'visibility
1832 :help-echo "Hide the value of this option."
1833 :action 'custom-toggle-parent
1834 t)
1835 buttons)
1836 (insert " ")
1837 (push (widget-create-child-and-convert
1838 widget 'sexp
1839 :button-face 'custom-variable-button-face
1840 :format "%v"
1841 :tag (symbol-name symbol)
1842 :parent widget
1843 :value value)
1844 children)))
1845 (t
1846 ;; Edit mode.
1847 (let* ((format (widget-get type :format))
1848 tag-format value-format)
1849 (unless (string-match ":" format)
1850 (error "Bad format."))
1851 (setq tag-format (substring format 0 (match-end 0)))
1852 (setq value-format (substring format (match-end 0)))
1853 (push (widget-create-child-and-convert
1854 widget 'item
1855 :format tag-format
1856 :action 'custom-tag-action
1857 :help-echo "Change value of this option."
1858 :mouse-down-action 'custom-tag-mouse-down-action
1859 :button-face 'custom-variable-button-face
1860 :sample-face 'custom-variable-tag-face
1861 tag)
1862 buttons)
1863 (insert " ")
1864 (push (widget-create-child-and-convert
1865 widget 'visibility
1866 :help-echo "Hide the value of this option."
1867 :action 'custom-toggle-parent
1868 t)
1869 buttons)
1870 (push (widget-create-child-and-convert
1871 widget type
1872 :format value-format
1873 :value value)
1874 children))))
1875 (unless (eq custom-buffer-style 'tree)
1876 ;; Now update the state.
1877 (unless (eq (preceding-char) ?\n)
1878 (widget-insert "\n"))
1879 (if (eq state 'hidden)
1880 (widget-put widget :custom-state state)
1881 (custom-variable-state-set widget))
1882 ;; Create the magic button.
1883 (let ((magic (widget-create-child-and-convert
1884 widget 'custom-magic nil)))
1885 (widget-put widget :custom-magic magic)
1886 (push magic buttons))
1887 ;; Update properties.
1888 (widget-put widget :custom-form form)
1889 (widget-put widget :buttons buttons)
1890 (widget-put widget :children children)
1891 ;; Insert documentation.
1892 (widget-default-format-handler widget ?h)
1893 ;; See also.
1894 (unless (eq state 'hidden)
1895 (when (eq (widget-get widget :custom-level) 1)
1896 (custom-add-parent-links widget))
1897 (custom-add-see-also widget)))))
1898
1899 (defun custom-tag-action (widget &rest args)
1900 "Pass :action to first child of WIDGET's parent."
1901 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1902 :action args))
1903
1904 (defun custom-tag-mouse-down-action (widget &rest args)
1905 "Pass :mouse-down-action to first child of WIDGET's parent."
1906 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1907 :mouse-down-action args))
1908
1909 (defun custom-variable-state-set (widget)
1910 "Set the state of WIDGET."
1911 (let* ((symbol (widget-value widget))
1912 (get (or (get symbol 'custom-get) 'default-value))
1913 (value (if (default-boundp symbol)
1914 (funcall get symbol)
1915 (widget-get widget :value)))
1916 tmp
1917 (state (cond ((setq tmp (get symbol 'customized-value))
1918 (if (condition-case nil
1919 (equal value (eval (car tmp)))
1920 (error nil))
1921 'set
1922 'changed))
1923 ((setq tmp (get symbol 'saved-value))
1924 (if (condition-case nil
1925 (equal value (eval (car tmp)))
1926 (error nil))
1927 'saved
1928 'changed))
1929 ((setq tmp (get symbol 'standard-value))
1930 (if (condition-case nil
1931 (equal value (eval (car tmp)))
1932 (error nil))
1933 'standard
1934 'changed))
1935 (t 'rogue))))
1936 (widget-put widget :custom-state state)))
1937
1938 (defvar custom-variable-menu
1939 '(("Set for Current Session" custom-variable-set
1940 (lambda (widget)
1941 (eq (widget-get widget :custom-state) 'modified)))
1942 ("Save for Future Sessions" custom-variable-save
1943 (lambda (widget)
1944 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1945 ("Reset to Current" custom-redraw
1946 (lambda (widget)
1947 (and (default-boundp (widget-value widget))
1948 (memq (widget-get widget :custom-state) '(modified changed)))))
1949 ("Reset to Saved" custom-variable-reset-saved
1950 (lambda (widget)
1951 (and (get (widget-value widget) 'saved-value)
1952 (memq (widget-get widget :custom-state)
1953 '(modified set changed rogue)))))
1954 ("Reset to Standard Settings" custom-variable-reset-standard
1955 (lambda (widget)
1956 (and (get (widget-value widget) 'standard-value)
1957 (memq (widget-get widget :custom-state)
1958 '(modified set changed saved rogue)))))
1959 ("---" ignore ignore)
1960 ("Don't show as Lisp expression" custom-variable-edit
1961 (lambda (widget)
1962 (eq (widget-get widget :custom-form) 'lisp)))
1963 ("Show as Lisp expression" custom-variable-edit-lisp
1964 (lambda (widget)
1965 (eq (widget-get widget :custom-form) 'edit))))
1966 "Alist of actions for the `custom-variable' widget.
1967 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1968 the menu entry, ACTION is the function to call on the widget when the
1969 menu is selected, and FILTER is a predicate which takes a `custom-variable'
1970 widget as an argument, and returns non-nil if ACTION is valid on that
1971 widget. If FILTER is nil, ACTION is always valid.")
1972
1973 (defun custom-variable-action (widget &optional event)
1974 "Show the menu for `custom-variable' WIDGET.
1975 Optional EVENT is the location for the menu."
1976 (if (eq (widget-get widget :custom-state) 'hidden)
1977 (custom-toggle-hide widget)
1978 (unless (eq (widget-get widget :custom-state) 'modified)
1979 (custom-variable-state-set widget))
1980 (custom-redraw-magic widget)
1981 (let* ((completion-ignore-case t)
1982 (answer (widget-choose (concat "Operation on "
1983 (custom-unlispify-tag-name
1984 (widget-get widget :value)))
1985 (custom-menu-filter custom-variable-menu
1986 widget)
1987 event)))
1988 (if answer
1989 (funcall answer widget)))))
1990
1991 (defun custom-variable-edit (widget)
1992 "Edit value of WIDGET."
1993 (widget-put widget :custom-state 'unknown)
1994 (widget-put widget :custom-form 'edit)
1995 (custom-redraw widget))
1996
1997 (defun custom-variable-edit-lisp (widget)
1998 "Edit the lisp representation of the value of WIDGET."
1999 (widget-put widget :custom-state 'unknown)
2000 (widget-put widget :custom-form 'lisp)
2001 (custom-redraw widget))
2002
2003 (defun custom-variable-set (widget)
2004 "Set the current value for the variable being edited by WIDGET."
2005 (let* ((form (widget-get widget :custom-form))
2006 (state (widget-get widget :custom-state))
2007 (child (car (widget-get widget :children)))
2008 (symbol (widget-value widget))
2009 (set (or (get symbol 'custom-set) 'set-default))
2010 val)
2011 (cond ((eq state 'hidden)
2012 (error "Cannot set hidden variable."))
2013 ((setq val (widget-apply child :validate))
2014 (goto-char (widget-get val :from))
2015 (error "%s" (widget-get val :error)))
2016 ((memq form '(lisp mismatch))
2017 (funcall set symbol (eval (setq val (widget-value child))))
2018 (put symbol 'customized-value (list val)))
2019 (t
2020 (funcall set symbol (setq val (widget-value child)))
2021 (put symbol 'customized-value (list (custom-quote val)))))
2022 (custom-variable-state-set widget)
2023 (custom-redraw-magic widget)))
2024
2025 (defun custom-variable-save (widget)
2026 "Set the default value for the variable being edited by WIDGET."
2027 (let* ((form (widget-get widget :custom-form))
2028 (state (widget-get widget :custom-state))
2029 (child (car (widget-get widget :children)))
2030 (symbol (widget-value widget))
2031 (set (or (get symbol 'custom-set) 'set-default))
2032 val)
2033 (cond ((eq state 'hidden)
2034 (error "Cannot set hidden variable."))
2035 ((setq val (widget-apply child :validate))
2036 (goto-char (widget-get val :from))
2037 (error "%s" (widget-get val :error)))
2038 ((memq form '(lisp mismatch))
2039 (put symbol 'saved-value (list (widget-value child)))
2040 (funcall set symbol (eval (widget-value child))))
2041 (t
2042 (put symbol
2043 'saved-value (list (custom-quote (widget-value
2044 child))))
2045 (funcall set symbol (widget-value child))))
2046 (put symbol 'customized-value nil)
2047 (custom-save-all)
2048 (custom-variable-state-set widget)
2049 (custom-redraw-magic widget)))
2050
2051 (defun custom-variable-reset-saved (widget)
2052 "Restore the saved value for the variable being edited by WIDGET."
2053 (let* ((symbol (widget-value widget))
2054 (set (or (get symbol 'custom-set) 'set-default)))
2055 (if (get symbol 'saved-value)
2056 (condition-case nil
2057 (funcall set symbol (eval (car (get symbol 'saved-value))))
2058 (error nil))
2059 (error "No saved value for %s" symbol))
2060 (put symbol 'customized-value nil)
2061 (widget-put widget :custom-state 'unknown)
2062 (custom-redraw widget)))
2063
2064 (defun custom-variable-reset-standard (widget)
2065 "Restore the standard setting for the variable being edited by WIDGET."
2066 (let* ((symbol (widget-value widget))
2067 (set (or (get symbol 'custom-set) 'set-default)))
2068 (if (get symbol 'standard-value)
2069 (funcall set symbol (eval (car (get symbol 'standard-value))))
2070 (error "No standard setting known for %S" symbol))
2071 (put symbol 'customized-value nil)
2072 (when (get symbol 'saved-value)
2073 (put symbol 'saved-value nil)
2074 (custom-save-all))
2075 (widget-put widget :custom-state 'unknown)
2076 (custom-redraw widget)))
2077
2078 ;;; The `custom-face-edit' Widget.
2079
2080 (define-widget 'custom-face-edit 'checklist
2081 "Edit face attributes."
2082 :format "%t: %v"
2083 :tag "Attributes"
2084 :extra-offset 12
2085 :button-args '(:help-echo "Control whether this attribute have any effect.")
2086 :args (mapcar (lambda (att)
2087 (list 'group
2088 :inline t
2089 :sibling-args (widget-get (nth 1 att) :sibling-args)
2090 (list 'const :format "" :value (nth 0 att))
2091 (nth 1 att)))
2092 custom-face-attributes))
2093
2094 ;;; The `custom-display' Widget.
2095
2096 (define-widget 'custom-display 'menu-choice
2097 "Select a display type."
2098 :tag "Display"
2099 :value t
2100 :help-echo "Specify frames where the face attributes should be used."
2101 :args '((const :tag "all" t)
2102 (checklist
2103 :offset 0
2104 :extra-offset 9
2105 :args ((group :sibling-args (:help-echo "\
2106 Only match the specified window systems.")
2107 (const :format "Type: "
2108 type)
2109 (checklist :inline t
2110 :offset 0
2111 (const :format "X "
2112 :sibling-args (:help-echo "\
2113 The X11 Window System.")
2114 x)
2115 (const :format "PM "
2116 :sibling-args (:help-echo "\
2117 OS/2 Presentation Manager.")
2118 pm)
2119 (const :format "W32 "
2120 :sibling-args (:help-echo "\
2121 Windows NT/9X.")
2122 w32)
2123 (const :format "DOS "
2124 :sibling-args (:help-echo "\
2125 Plain MS-DOS.")
2126 pc)
2127 (const :format "TTY%n"
2128 :sibling-args (:help-echo "\
2129 Plain text terminals.")
2130 tty)))
2131 (group :sibling-args (:help-echo "\
2132 Only match the frames with the specified color support.")
2133 (const :format "Class: "
2134 class)
2135 (checklist :inline t
2136 :offset 0
2137 (const :format "Color "
2138 :sibling-args (:help-echo "\
2139 Match color frames.")
2140 color)
2141 (const :format "Grayscale "
2142 :sibling-args (:help-echo "\
2143 Match grayscale frames.")
2144 grayscale)
2145 (const :format "Monochrome%n"
2146 :sibling-args (:help-echo "\
2147 Match frames with no color support.")
2148 mono)))
2149 (group :sibling-args (:help-echo "\
2150 Only match frames with the specified intensity.")
2151 (const :format "\
2152 Background brightness: "
2153 background)
2154 (checklist :inline t
2155 :offset 0
2156 (const :format "Light "
2157 :sibling-args (:help-echo "\
2158 Match frames with light backgrounds.")
2159 light)
2160 (const :format "Dark\n"
2161 :sibling-args (:help-echo "\
2162 Match frames with dark backgrounds.")
2163 dark)))))))
2164
2165 ;;; The `custom-face' Widget.
2166
2167 (defface custom-face-tag-face '((t (:underline t)))
2168 "Face used for face tags."
2169 :group 'custom-faces)
2170
2171 (define-widget 'custom-face 'custom
2172 "Customize face."
2173 :sample-face 'custom-face-tag-face
2174 :help-echo "Set or reset this face."
2175 :documentation-property '(lambda (face)
2176 (face-doc-string face))
2177 :value-create 'custom-face-value-create
2178 :action 'custom-face-action
2179 :custom-category 'face
2180 :custom-form 'selected
2181 :custom-set 'custom-face-set
2182 :custom-save 'custom-face-save
2183 :custom-reset-current 'custom-redraw
2184 :custom-reset-saved 'custom-face-reset-saved
2185 :custom-reset-standard 'custom-face-reset-standard
2186 :custom-menu 'custom-face-menu-create)
2187
2188 (define-widget 'custom-face-all 'editable-list
2189 "An editable list of display specifications and attributes."
2190 :entry-format "%i %d %v"
2191 :insert-button-args '(:help-echo "Insert new display specification here.")
2192 :append-button-args '(:help-echo "Append new display specification here.")
2193 :delete-button-args '(:help-echo "Delete this display specification.")
2194 :args '((group :format "%v" custom-display custom-face-edit)))
2195
2196 (defconst custom-face-all (widget-convert 'custom-face-all)
2197 "Converted version of the `custom-face-all' widget.")
2198
2199 (define-widget 'custom-display-unselected 'item
2200 "A display specification that doesn't match the selected display."
2201 :match 'custom-display-unselected-match)
2202
2203 (defun custom-display-unselected-match (widget value)
2204 "Non-nil if VALUE is an unselected display specification."
2205 (not (face-spec-set-match-display value (selected-frame))))
2206
2207 (define-widget 'custom-face-selected 'group
2208 "Edit the attributes of the selected display in a face specification."
2209 :args '((repeat :format ""
2210 :inline t
2211 (group custom-display-unselected sexp))
2212 (group (sexp :format "") custom-face-edit)
2213 (repeat :format ""
2214 :inline t
2215 sexp)))
2216
2217 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2218 "Converted version of the `custom-face-selected' widget.")
2219
2220 (defun custom-face-value-create (widget)
2221 "Create a list of the display specifications for WIDGET."
2222 (let ((buttons (widget-get widget :buttons))
2223 (symbol (widget-get widget :value))
2224 (tag (widget-get widget :tag))
2225 (state (widget-get widget :custom-state))
2226 (begin (point))
2227 (is-last (widget-get widget :custom-last))
2228 (prefix (widget-get widget :custom-prefix)))
2229 (unless tag
2230 (setq tag (prin1-to-string symbol)))
2231 (cond ((eq custom-buffer-style 'tree)
2232 (insert prefix (if is-last " `--- " " |--- "))
2233 (push (widget-create-child-and-convert
2234 widget 'custom-browse-face-tag)
2235 buttons)
2236 (insert " " tag "\n")
2237 (widget-put widget :buttons buttons))
2238 (t
2239 ;; Create tag.
2240 (insert tag)
2241 (if (eq custom-buffer-style 'face)
2242 (insert " ")
2243 (widget-specify-sample widget begin (point))
2244 (insert ": "))
2245 ;; Sample.
2246 (and (string-match "XEmacs" emacs-version)
2247 ;; XEmacs cannot display uninitialized faces.
2248 (not (custom-facep symbol))
2249 (copy-face 'custom-face-empty symbol))
2250 (push (widget-create-child-and-convert widget 'item
2251 :format "(%{%t%})"
2252 :sample-face symbol
2253 :tag "sample")
2254 buttons)
2255 ;; Visibility.
2256 (insert " ")
2257 (push (widget-create-child-and-convert
2258 widget 'visibility
2259 :help-echo "Hide or show this face."
2260 :action 'custom-toggle-parent
2261 (not (eq state 'hidden)))
2262 buttons)
2263 ;; Magic.
2264 (insert "\n")
2265 (let ((magic (widget-create-child-and-convert
2266 widget 'custom-magic nil)))
2267 (widget-put widget :custom-magic magic)
2268 (push magic buttons))
2269 ;; Update buttons.
2270 (widget-put widget :buttons buttons)
2271 ;; Insert documentation.
2272 (widget-default-format-handler widget ?h)
2273 ;; See also.
2274 (unless (eq state 'hidden)
2275 (when (eq (widget-get widget :custom-level) 1)
2276 (custom-add-parent-links widget))
2277 (custom-add-see-also widget))
2278 ;; Editor.
2279 (unless (eq (preceding-char) ?\n)
2280 (insert "\n"))
2281 (unless (eq state 'hidden)
2282 (message "Creating face editor...")
2283 (custom-load-widget widget)
2284 (let* ((symbol (widget-value widget))
2285 (spec (or (get symbol 'saved-face)
2286 (get symbol 'face-defface-spec)
2287 ;; Attempt to construct it.
2288 (list (list t (custom-face-attributes-get
2289 symbol (selected-frame))))))
2290 (form (widget-get widget :custom-form))
2291 (indent (widget-get widget :indent))
2292 edit)
2293 ;; If the user has changed this face in some other way,
2294 ;; edit it as the user has specified it.
2295 (if (not (face-spec-match-p symbol spec (selected-frame)))
2296 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
2297 (setq edit (widget-create-child-and-convert
2298 widget
2299 (cond ((and (eq form 'selected)
2300 (widget-apply custom-face-selected
2301 :match spec))
2302 (when indent (insert-char ?\ indent))
2303 'custom-face-selected)
2304 ((and (not (eq form 'lisp))
2305 (widget-apply custom-face-all
2306 :match spec))
2307 'custom-face-all)
2308 (t
2309 (when indent (insert-char ?\ indent))
2310 'sexp))
2311 :value spec))
2312 (custom-face-state-set widget)
2313 (widget-put widget :children (list edit)))
2314 (message "Creating face editor...done"))))))
2315
2316 (defvar custom-face-menu
2317 '(("Set for Current Session" custom-face-set)
2318 ("Save for Future Sessions" custom-face-save)
2319 ("Reset to Saved" custom-face-reset-saved
2320 (lambda (widget)
2321 (get (widget-value widget) 'saved-face)))
2322 ("Reset to Standard Setting" custom-face-reset-standard
2323 (lambda (widget)
2324 (get (widget-value widget) 'face-defface-spec)))
2325 ("---" ignore ignore)
2326 ("Show all display specs" custom-face-edit-all
2327 (lambda (widget)
2328 (not (eq (widget-get widget :custom-form) 'all))))
2329 ("Just current attributes" custom-face-edit-selected
2330 (lambda (widget)
2331 (not (eq (widget-get widget :custom-form) 'selected))))
2332 ("Show as Lisp expression" custom-face-edit-lisp
2333 (lambda (widget)
2334 (not (eq (widget-get widget :custom-form) 'lisp)))))
2335 "Alist of actions for the `custom-face' widget.
2336 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2337 the menu entry, ACTION is the function to call on the widget when the
2338 menu is selected, and FILTER is a predicate which takes a `custom-face'
2339 widget as an argument, and returns non-nil if ACTION is valid on that
2340 widget. If FILTER is nil, ACTION is always valid.")
2341
2342 (defun custom-face-edit-selected (widget)
2343 "Edit selected attributes of the value of WIDGET."
2344 (widget-put widget :custom-state 'unknown)
2345 (widget-put widget :custom-form 'selected)
2346 (custom-redraw widget))
2347
2348 (defun custom-face-edit-all (widget)
2349 "Edit all attributes of the value of WIDGET."
2350 (widget-put widget :custom-state 'unknown)
2351 (widget-put widget :custom-form 'all)
2352 (custom-redraw widget))
2353
2354 (defun custom-face-edit-lisp (widget)
2355 "Edit the lisp representation of the value of WIDGET."
2356 (widget-put widget :custom-state 'unknown)
2357 (widget-put widget :custom-form 'lisp)
2358 (custom-redraw widget))
2359
2360 (defun custom-face-state-set (widget)
2361 "Set the state of WIDGET."
2362 (let ((symbol (widget-value widget)))
2363 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
2364 'set)
2365 ((get symbol 'saved-face)
2366 'saved)
2367 ((get symbol 'face-defface-spec)
2368 'standard)
2369 (t
2370 'rogue)))))
2371
2372 (defun custom-face-action (widget &optional event)
2373 "Show the menu for `custom-face' WIDGET.
2374 Optional EVENT is the location for the menu."
2375 (if (eq (widget-get widget :custom-state) 'hidden)
2376 (custom-toggle-hide widget)
2377 (let* ((completion-ignore-case t)
2378 (symbol (widget-get widget :value))
2379 (answer (widget-choose (concat "Operation on "
2380 (custom-unlispify-tag-name symbol))
2381 (custom-menu-filter custom-face-menu
2382 widget)
2383 event)))
2384 (if answer
2385 (funcall answer widget)))))
2386
2387 (defun custom-face-set (widget)
2388 "Make the face attributes in WIDGET take effect."
2389 (let* ((symbol (widget-value widget))
2390 (child (car (widget-get widget :children)))
2391 (value (widget-value child)))
2392 (put symbol 'customized-face value)
2393 (face-spec-set symbol value)
2394 (custom-face-state-set widget)
2395 (custom-redraw-magic widget)))
2396
2397 (defun custom-face-save (widget)
2398 "Make the face attributes in WIDGET default."
2399 (let* ((symbol (widget-value widget))
2400 (child (car (widget-get widget :children)))
2401 (value (widget-value child)))
2402 (face-spec-set symbol value)
2403 (put symbol 'saved-face value)
2404 (put symbol 'customized-face nil)
2405 (custom-face-state-set widget)
2406 (custom-redraw-magic widget)))
2407
2408 (defun custom-face-reset-saved (widget)
2409 "Restore WIDGET to the face's default attributes."
2410 (let* ((symbol (widget-value widget))
2411 (child (car (widget-get widget :children)))
2412 (value (get symbol 'saved-face)))
2413 (unless value
2414 (error "No saved value for this face"))
2415 (put symbol 'customized-face nil)
2416 (face-spec-set symbol value)
2417 (widget-value-set child value)
2418 (custom-face-state-set widget)
2419 (custom-redraw-magic widget)))
2420
2421 (defun custom-face-reset-standard (widget)
2422 "Restore WIDGET to the face's standard settings."
2423 (let* ((symbol (widget-value widget))
2424 (child (car (widget-get widget :children)))
2425 (value (get symbol 'face-defface-spec)))
2426 (unless value
2427 (error "No standard setting for this face"))
2428 (put symbol 'customized-face nil)
2429 (when (get symbol 'saved-face)
2430 (put symbol 'saved-face nil)
2431 (custom-save-all))
2432 (face-spec-set symbol value)
2433 (widget-value-set child value)
2434 (custom-face-state-set widget)
2435 (custom-redraw-magic widget)))
2436
2437 ;;; The `face' Widget.
2438
2439 (define-widget 'face 'default
2440 "Select and customize a face."
2441 :convert-widget 'widget-value-convert-widget
2442 :button-prefix 'widget-push-button-prefix
2443 :button-suffix 'widget-push-button-suffix
2444 :format "%t: %[select face%] %v"
2445 :tag "Face"
2446 :value 'default
2447 :value-create 'widget-face-value-create
2448 :value-delete 'widget-face-value-delete
2449 :value-get 'widget-value-value-get
2450 :validate 'widget-children-validate
2451 :action 'widget-face-action
2452 :match '(lambda (widget value) (symbolp value)))
2453
2454 (defun widget-face-value-create (widget)
2455 ;; Create a `custom-face' child.
2456 (let* ((symbol (widget-value widget))
2457 (custom-buffer-style 'face)
2458 (child (widget-create-child-and-convert
2459 widget 'custom-face
2460 :custom-level nil
2461 :value symbol)))
2462 (custom-magic-reset child)
2463 (setq custom-options (cons child custom-options))
2464 (widget-put widget :children (list child))))
2465
2466 (defun widget-face-value-delete (widget)
2467 ;; Remove the child from the options.
2468 (let ((child (car (widget-get widget :children))))
2469 (setq custom-options (delq child custom-options))
2470 (widget-children-value-delete widget)))
2471
2472 (defvar face-history nil
2473 "History of entered face names.")
2474
2475 (defun widget-face-action (widget &optional event)
2476 "Prompt for a face."
2477 (let ((answer (completing-read "Face: "
2478 (mapcar (lambda (face)
2479 (list (symbol-name face)))
2480 (face-list))
2481 nil nil nil
2482 'face-history)))
2483 (unless (zerop (length answer))
2484 (widget-value-set widget (intern answer))
2485 (widget-apply widget :notify widget event)
2486 (widget-setup))))
2487
2488 ;;; The `hook' Widget.
2489
2490 (define-widget 'hook 'list
2491 "A emacs lisp hook"
2492 :value-to-internal (lambda (widget value)
2493 (if (symbolp value)
2494 (list value)
2495 value))
2496 :match (lambda (widget value)
2497 (or (symbolp value)
2498 (widget-editable-list-match widget value)))
2499 :convert-widget 'custom-hook-convert-widget
2500 :tag "Hook")
2501
2502 (defun custom-hook-convert-widget (widget)
2503 ;; Handle `:custom-options'.
2504 (let* ((options (widget-get widget :options))
2505 (other `(editable-list :inline t
2506 :entry-format "%i %d%v"
2507 (function :format " %v")))
2508 (args (if options
2509 (list `(checklist :inline t
2510 ,@(mapcar (lambda (entry)
2511 `(function-item ,entry))
2512 options))
2513 other)
2514 (list other))))
2515 (widget-put widget :args args)
2516 widget))
2517
2518 ;;; The `custom-group-link' Widget.
2519
2520 (define-widget 'custom-group-link 'link
2521 "Show parent in other window when activated."
2522 :help-echo "Create customization buffer for this group."
2523 :action 'custom-group-link-action)
2524
2525 (defun custom-group-link-action (widget &rest ignore)
2526 (customize-group (widget-value widget)))
2527
2528 ;;; The `custom-group' Widget.
2529
2530 (defcustom custom-group-tag-faces nil
2531 ;; In XEmacs, this ought to play games with font size.
2532 "Face used for group tags.
2533 The first member is used for level 1 groups, the second for level 2,
2534 and so forth. The remaining group tags are shown with
2535 `custom-group-tag-face'."
2536 :type '(repeat face)
2537 :group 'custom-faces)
2538
2539 (defface custom-group-tag-face-1 '((((class color)
2540 (background dark))
2541 (:foreground "pink" :underline t))
2542 (((class color)
2543 (background light))
2544 (:foreground "red" :underline t))
2545 (t (:underline t)))
2546 "Face used for group tags.")
2547
2548 (defface custom-group-tag-face '((((class color)
2549 (background dark))
2550 (:foreground "light blue" :underline t))
2551 (((class color)
2552 (background light))
2553 (:foreground "blue" :underline t))
2554 (t (:underline t)))
2555 "Face used for low level group tags."
2556 :group 'custom-faces)
2557
2558 (define-widget 'custom-group 'custom
2559 "Customize group."
2560 :format "%v"
2561 :sample-face-get 'custom-group-sample-face-get
2562 :documentation-property 'group-documentation
2563 :help-echo "Set or reset all members of this group."
2564 :value-create 'custom-group-value-create
2565 :action 'custom-group-action
2566 :custom-category 'group
2567 :custom-set 'custom-group-set
2568 :custom-save 'custom-group-save
2569 :custom-reset-current 'custom-group-reset-current
2570 :custom-reset-saved 'custom-group-reset-saved
2571 :custom-reset-standard 'custom-group-reset-standard
2572 :custom-menu 'custom-group-menu-create)
2573
2574 (defun custom-group-sample-face-get (widget)
2575 ;; Use :sample-face.
2576 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2577 'custom-group-tag-face))
2578
2579 (define-widget 'custom-group-visibility 'visibility
2580 "An indicator and manipulator for hidden group contents."
2581 :create 'custom-group-visibility-create)
2582
2583 (defun custom-group-visibility-create (widget)
2584 (let ((visible (widget-value widget)))
2585 (if visible
2586 (insert "--------")))
2587 (widget-default-create widget))
2588
2589 (defun custom-group-members (symbol groups-only)
2590 "Return SYMBOL's custom group members.
2591 If GROUPS-ONLY non-nil, return only those members that are groups."
2592 (if (not groups-only)
2593 (get symbol 'custom-group)
2594 (let (members)
2595 (dolist (entry (get symbol 'custom-group))
2596 (when (eq (nth 1 entry) 'custom-group)
2597 (push entry members)))
2598 (nreverse members))))
2599
2600 (defun custom-group-value-create (widget)
2601 "Insert a customize group for WIDGET in the current buffer."
2602 (let* ((state (widget-get widget :custom-state))
2603 (level (widget-get widget :custom-level))
2604 ;; (indent (widget-get widget :indent))
2605 (prefix (widget-get widget :custom-prefix))
2606 (buttons (widget-get widget :buttons))
2607 (tag (widget-get widget :tag))
2608 (symbol (widget-value widget))
2609 (members (custom-group-members symbol
2610 (and (eq custom-buffer-style 'tree)
2611 custom-browse-only-groups))))
2612 (cond ((and (eq custom-buffer-style 'tree)
2613 (eq state 'hidden)
2614 (or members (custom-unloaded-widget-p widget)))
2615 (custom-browse-insert-prefix prefix)
2616 (push (widget-create-child-and-convert
2617 widget 'custom-browse-visibility
2618 ;; :tag-glyph "plus"
2619 :tag "+")
2620 buttons)
2621 (insert "-- ")
2622 ;; (widget-glyph-insert nil "-- " "horizontal")
2623 (push (widget-create-child-and-convert
2624 widget 'custom-browse-group-tag)
2625 buttons)
2626 (insert " " tag "\n")
2627 (widget-put widget :buttons buttons))
2628 ((and (eq custom-buffer-style 'tree)
2629 (zerop (length members)))
2630 (custom-browse-insert-prefix prefix)
2631 (insert "[ ]-- ")
2632 ;; (widget-glyph-insert nil "[ ]" "empty")
2633 ;; (widget-glyph-insert nil "-- " "horizontal")
2634 (push (widget-create-child-and-convert
2635 widget 'custom-browse-group-tag)
2636 buttons)
2637 (insert " " tag "\n")
2638 (widget-put widget :buttons buttons))
2639 ((eq custom-buffer-style 'tree)
2640 (custom-browse-insert-prefix prefix)
2641 (custom-load-widget widget)
2642 (if (zerop (length members))
2643 (progn
2644 (custom-browse-insert-prefix prefix)
2645 (insert "[ ]-- ")
2646 ;; (widget-glyph-insert nil "[ ]" "empty")
2647 ;; (widget-glyph-insert nil "-- " "horizontal")
2648 (push (widget-create-child-and-convert
2649 widget 'custom-browse-group-tag)
2650 buttons)
2651 (insert " " tag "\n")
2652 (widget-put widget :buttons buttons))
2653 (push (widget-create-child-and-convert
2654 widget 'custom-browse-visibility
2655 ;; :tag-glyph "minus"
2656 :tag "-")
2657 buttons)
2658 (insert "-\\ ")
2659 ;; (widget-glyph-insert nil "-\\ " "top")
2660 (push (widget-create-child-and-convert
2661 widget 'custom-browse-group-tag)
2662 buttons)
2663 (insert " " tag "\n")
2664 (widget-put widget :buttons buttons)
2665 (message "Creating group...")
2666 (let* ((members (custom-sort-items members
2667 custom-browse-sort-alphabetically
2668 custom-browse-order-groups))
2669 (prefixes (widget-get widget :custom-prefixes))
2670 (custom-prefix-list (custom-prefix-add symbol prefixes))
2671 (extra-prefix (if (widget-get widget :custom-last)
2672 " "
2673 " | "))
2674 (prefix (concat prefix extra-prefix))
2675 children entry)
2676 (while members
2677 (setq entry (car members)
2678 members (cdr members))
2679 (push (widget-create-child-and-convert
2680 widget (nth 1 entry)
2681 :group widget
2682 :tag (custom-unlispify-tag-name (nth 0 entry))
2683 :custom-prefixes custom-prefix-list
2684 :custom-level (1+ level)
2685 :custom-last (null members)
2686 :value (nth 0 entry)
2687 :custom-prefix prefix)
2688 children))
2689 (widget-put widget :children (reverse children)))
2690 (message "Creating group...done")))
2691 ;; Nested style.
2692 ((eq state 'hidden)
2693 ;; Create level indicator.
2694 (unless (eq custom-buffer-style 'links)
2695 (insert-char ?\ (* custom-buffer-indent (1- level)))
2696 (insert "-- "))
2697 ;; Create tag.
2698 (let ((begin (point)))
2699 (insert tag)
2700 (widget-specify-sample widget begin (point)))
2701 (insert " group: ")
2702 ;; Create link/visibility indicator.
2703 (if (eq custom-buffer-style 'links)
2704 (push (widget-create-child-and-convert
2705 widget 'custom-group-link
2706 :tag "Go to Group"
2707 symbol)
2708 buttons)
2709 (push (widget-create-child-and-convert
2710 widget 'group-visibility
2711 :help-echo "Show members of this group."
2712 :action 'custom-toggle-parent
2713 (not (eq state 'hidden)))
2714 buttons))
2715 (insert " \n")
2716 ;; Create magic button.
2717 (let ((magic (widget-create-child-and-convert
2718 widget 'custom-magic nil)))
2719 (widget-put widget :custom-magic magic)
2720 (push magic buttons))
2721 ;; Update buttons.
2722 (widget-put widget :buttons buttons)
2723 ;; Insert documentation.
2724 (if (and (eq custom-buffer-style 'links) (> level 1))
2725 (widget-put widget :documentation-indent 0))
2726 (widget-default-format-handler widget ?h))
2727 ;; Nested style.
2728 (t ;Visible.
2729 ;; Add parent groups references above the group.
2730 (if t ;;; This should test that the buffer
2731 ;;; was made to display a group.
2732 (when (eq level 1)
2733 (if (custom-add-parent-links widget
2734 "Go to parent group:")
2735 (insert "\n"))))
2736 ;; Create level indicator.
2737 (insert-char ?\ (* custom-buffer-indent (1- level)))
2738 (insert "/- ")
2739 ;; Create tag.
2740 (let ((start (point)))
2741 (insert tag)
2742 (widget-specify-sample widget start (point)))
2743 (insert " group: ")
2744 ;; Create visibility indicator.
2745 (unless (eq custom-buffer-style 'links)
2746 (insert "--------")
2747 (push (widget-create-child-and-convert
2748 widget 'visibility
2749 :help-echo "Hide members of this group."
2750 :action 'custom-toggle-parent
2751 (not (eq state 'hidden)))
2752 buttons)
2753 (insert " "))
2754 ;; Create more dashes.
2755 ;; Use 76 instead of 75 to compensate for the temporary "<"
2756 ;; added by `widget-insert'.
2757 (insert-char ?- (- 76 (current-column)
2758 (* custom-buffer-indent level)))
2759 (insert "\\\n")
2760 ;; Create magic button.
2761 (let ((magic (widget-create-child-and-convert
2762 widget 'custom-magic
2763 :indent 0
2764 nil)))
2765 (widget-put widget :custom-magic magic)
2766 (push magic buttons))
2767 ;; Update buttons.
2768 (widget-put widget :buttons buttons)
2769 ;; Insert documentation.
2770 (widget-default-format-handler widget ?h)
2771 ;; Parent groups.
2772 (if nil ;;; This should test that the buffer
2773 ;;; was not made to display a group.
2774 (when (eq level 1)
2775 (insert-char ?\ custom-buffer-indent)
2776 (custom-add-parent-links widget)))
2777 (custom-add-see-also widget
2778 (make-string (* custom-buffer-indent level)
2779 ?\ ))
2780 ;; Members.
2781 (message "Creating group...")
2782 (custom-load-widget widget)
2783 (let* ((members (custom-sort-items members
2784 custom-buffer-sort-alphabetically
2785 custom-buffer-order-groups))
2786 (prefixes (widget-get widget :custom-prefixes))
2787 (custom-prefix-list (custom-prefix-add symbol prefixes))
2788 (length (length members))
2789 (count 0)
2790 (children (mapcar (lambda (entry)
2791 (widget-insert "\n")
2792 (message "\
2793 Creating group members... %2d%%"
2794 (/ (* 100.0 count) length))
2795 (setq count (1+ count))
2796 (prog1
2797 (widget-create-child-and-convert
2798 widget (nth 1 entry)
2799 :group widget
2800 :tag (custom-unlispify-tag-name
2801 (nth 0 entry))
2802 :custom-prefixes custom-prefix-list
2803 :custom-level (1+ level)
2804 :value (nth 0 entry))
2805 (unless (eq (preceding-char) ?\n)
2806 (widget-insert "\n"))))
2807 members)))
2808 (message "Creating group magic...")
2809 (mapcar 'custom-magic-reset children)
2810 (message "Creating group state...")
2811 (widget-put widget :children children)
2812 (custom-group-state-update widget)
2813 (message "Creating group... done"))
2814 ;; End line
2815 (insert "\n")
2816 (insert-char ?\ (* custom-buffer-indent (1- level)))
2817 (insert "\\- " (widget-get widget :tag) " group end ")
2818 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
2819 (insert "/\n")))))
2820
2821 (defvar custom-group-menu
2822 '(("Set for Current Session" custom-group-set
2823 (lambda (widget)
2824 (eq (widget-get widget :custom-state) 'modified)))
2825 ("Save for Future Sessions" custom-group-save
2826 (lambda (widget)
2827 (memq (widget-get widget :custom-state) '(modified set))))
2828 ("Reset to Current" custom-group-reset-current
2829 (lambda (widget)
2830 (memq (widget-get widget :custom-state) '(modified))))
2831 ("Reset to Saved" custom-group-reset-saved
2832 (lambda (widget)
2833 (memq (widget-get widget :custom-state) '(modified set))))
2834 ("Reset to standard setting" custom-group-reset-standard
2835 (lambda (widget)
2836 (memq (widget-get widget :custom-state) '(modified set saved)))))
2837 "Alist of actions for the `custom-group' widget.
2838 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2839 the menu entry, ACTION is the function to call on the widget when the
2840 menu is selected, and FILTER is a predicate which takes a `custom-group'
2841 widget as an argument, and returns non-nil if ACTION is valid on that
2842 widget. If FILTER is nil, ACTION is always valid.")
2843
2844 (defun custom-group-action (widget &optional event)
2845 "Show the menu for `custom-group' WIDGET.
2846 Optional EVENT is the location for the menu."
2847 (if (eq (widget-get widget :custom-state) 'hidden)
2848 (custom-toggle-hide widget)
2849 (let* ((completion-ignore-case t)
2850 (answer (widget-choose (concat "Operation on "
2851 (custom-unlispify-tag-name
2852 (widget-get widget :value)))
2853 (custom-menu-filter custom-group-menu
2854 widget)
2855 event)))
2856 (if answer
2857 (funcall answer widget)))))
2858
2859 (defun custom-group-set (widget)
2860 "Set changes in all modified group members."
2861 (let ((children (widget-get widget :children)))
2862 (mapcar (lambda (child)
2863 (when (eq (widget-get child :custom-state) 'modified)
2864 (widget-apply child :custom-set)))
2865 children )))
2866
2867 (defun custom-group-save (widget)
2868 "Save all modified group members."
2869 (let ((children (widget-get widget :children)))
2870 (mapcar (lambda (child)
2871 (when (memq (widget-get child :custom-state) '(modified set))
2872 (widget-apply child :custom-save)))
2873 children )))
2874
2875 (defun custom-group-reset-current (widget)
2876 "Reset all modified group members."
2877 (let ((children (widget-get widget :children)))
2878 (mapcar (lambda (child)
2879 (when (eq (widget-get child :custom-state) 'modified)
2880 (widget-apply child :custom-reset-current)))
2881 children )))
2882
2883 (defun custom-group-reset-saved (widget)
2884 "Reset all modified or set group members."
2885 (let ((children (widget-get widget :children)))
2886 (mapcar (lambda (child)
2887 (when (memq (widget-get child :custom-state) '(modified set))
2888 (widget-apply child :custom-reset-saved)))
2889 children )))
2890
2891 (defun custom-group-reset-standard (widget)
2892 "Reset all modified, set, or saved group members."
2893 (let ((children (widget-get widget :children)))
2894 (mapcar (lambda (child)
2895 (when (memq (widget-get child :custom-state)
2896 '(modified set saved))
2897 (widget-apply child :custom-reset-standard)))
2898 children )))
2899
2900 (defun custom-group-state-update (widget)
2901 "Update magic."
2902 (unless (eq (widget-get widget :custom-state) 'hidden)
2903 (let* ((children (widget-get widget :children))
2904 (states (mapcar (lambda (child)
2905 (widget-get child :custom-state))
2906 children))
2907 (magics custom-magic-alist)
2908 (found 'standard))
2909 (while magics
2910 (let ((magic (car (car magics))))
2911 (if (and (not (eq magic 'hidden))
2912 (memq magic states))
2913 (setq found magic
2914 magics nil)
2915 (setq magics (cdr magics)))))
2916 (widget-put widget :custom-state found)))
2917 (custom-magic-reset widget))
2918
2919 ;;; The `custom-save-all' Function.
2920 ;;;###autoload
2921 (defcustom custom-file nil
2922 "File used for storing customization information.
2923 The default is nil, which means to use your init file
2924 as specified by `user-init-file'. If you specify some other file,
2925 you need to explicitly load that file for the settings to take effect."
2926 :type '(choice (const :tag "Your Emacs init file" nil) file)
2927 :group 'customize)
2928
2929 (defun custom-save-delete (symbol)
2930 "Delete the call to SYMBOL form `custom-file'.
2931 Leave point at the location of the call, or after the last expression."
2932 (set-buffer (find-file-noselect (or custom-file user-init-file)))
2933 (goto-char (point-min))
2934 (catch 'found
2935 (while t
2936 (let ((sexp (condition-case nil
2937 (read (current-buffer))
2938 (end-of-file (throw 'found nil)))))
2939 (when (and (listp sexp)
2940 (eq (car sexp) symbol))
2941 (delete-region (save-excursion
2942 (backward-sexp)
2943 (point))
2944 (point))
2945 (throw 'found nil))))))
2946
2947 (defun custom-save-variables ()
2948 "Save all customized variables in `custom-file'."
2949 (save-excursion
2950 (custom-save-delete 'custom-set-variables)
2951 (let ((standard-output (current-buffer)))
2952 (unless (bolp)
2953 (princ "\n"))
2954 (princ "(custom-set-variables")
2955 (mapatoms (lambda (symbol)
2956 (let ((value (get symbol 'saved-value))
2957 (requests (get symbol 'custom-requests))
2958 (now (not (or (get symbol 'standard-value)
2959 (and (not (boundp symbol))
2960 (not (get symbol 'force-value)))))))
2961 (when value
2962 (princ "\n '(")
2963 (princ symbol)
2964 (princ " ")
2965 (prin1 (car value))
2966 (cond (requests
2967 (if now
2968 (princ " t ")
2969 (princ " nil "))
2970 (prin1 requests)
2971 (princ ")"))
2972 (now
2973 (princ " t)"))
2974 (t
2975 (princ ")")))))))
2976 (princ ")")
2977 (unless (looking-at "\n")
2978 (princ "\n")))))
2979
2980 (defun custom-save-faces ()
2981 "Save all customized faces in `custom-file'."
2982 (save-excursion
2983 (custom-save-delete 'custom-set-faces)
2984 (let ((standard-output (current-buffer)))
2985 (unless (bolp)
2986 (princ "\n"))
2987 (princ "(custom-set-faces")
2988 (let ((value (get 'default 'saved-face)))
2989 ;; The default face must be first, since it affects the others.
2990 (when value
2991 (princ "\n '(default ")
2992 (prin1 value)
2993 (if (or (get 'default 'face-defface-spec)
2994 (and (not (custom-facep 'default))
2995 (not (get 'default 'force-face))))
2996 (princ ")")
2997 (princ " t)"))))
2998 (mapatoms (lambda (symbol)
2999 (let ((value (get symbol 'saved-face)))
3000 (when (and (not (eq symbol 'default))
3001 ;; Don't print default face here.
3002 value)
3003 (princ "\n '(")
3004 (princ symbol)
3005 (princ " ")
3006 (prin1 value)
3007 (if (or (get symbol 'face-defface-spec)
3008 (and (not (custom-facep symbol))
3009 (not (get symbol 'force-face))))
3010 (princ ")")
3011 (princ " t)"))))))
3012 (princ ")")
3013 (unless (looking-at "\n")
3014 (princ "\n")))))
3015
3016 ;;;###autoload
3017 (defun customize-save-customized ()
3018 "Save all user options which have been set in this session."
3019 (interactive)
3020 (mapatoms (lambda (symbol)
3021 (let ((face (get symbol 'customized-face))
3022 (value (get symbol 'customized-value)))
3023 (when face
3024 (put symbol 'saved-face face)
3025 (put symbol 'customized-face nil))
3026 (when value
3027 (put symbol 'saved-value value)
3028 (put symbol 'customized-value nil)))))
3029 ;; We really should update all custom buffers here.
3030 (custom-save-all))
3031
3032 ;;;###autoload
3033 (defun custom-save-all ()
3034 "Save all customizations in `custom-file'."
3035 (let ((inhibit-read-only t))
3036 (custom-save-variables)
3037 (custom-save-faces)
3038 (save-excursion
3039 (set-buffer (find-file-noselect (or custom-file user-init-file)))
3040 (save-buffer))))
3041
3042 ;;; The Customize Menu.
3043
3044 ;;; Menu support
3045
3046 (defcustom custom-menu-nesting 2
3047 "Maximum nesting in custom menus."
3048 :type 'integer
3049 :group 'custom-menu)
3050
3051 (defun custom-face-menu-create (widget symbol)
3052 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3053 (vector (custom-unlispify-menu-entry symbol)
3054 `(customize-face ',symbol)
3055 t))
3056
3057 (defun custom-variable-menu-create (widget symbol)
3058 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3059 (let ((type (get symbol 'custom-type)))
3060 (unless (listp type)
3061 (setq type (list type)))
3062 (if (and type (widget-get type :custom-menu))
3063 (widget-apply type :custom-menu symbol)
3064 (vector (custom-unlispify-menu-entry symbol)
3065 `(customize-variable ',symbol)
3066 t))))
3067
3068 ;; Add checkboxes to boolean variable entries.
3069 (widget-put (get 'boolean 'widget-type)
3070 :custom-menu (lambda (widget symbol)
3071 (vector (custom-unlispify-menu-entry symbol)
3072 `(customize-variable ',symbol)
3073 ':style 'toggle
3074 ':selected symbol)))
3075
3076 (if (string-match "XEmacs" emacs-version)
3077 ;; XEmacs can create menus dynamically.
3078 (defun custom-group-menu-create (widget symbol)
3079 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3080 `( ,(custom-unlispify-menu-entry symbol t)
3081 :filter (lambda (&rest junk)
3082 (cdr (custom-menu-create ',symbol)))))
3083 ;; But emacs can't.
3084 (defun custom-group-menu-create (widget symbol)
3085 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3086 ;; Limit the nesting.
3087 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3088 (custom-menu-create symbol))))
3089
3090 ;;;###autoload
3091 (defun custom-menu-create (symbol)
3092 "Create menu for customization group SYMBOL.
3093 The menu is in a format applicable to `easy-menu-define'."
3094 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3095 `(customize-group ',symbol)
3096 t)))
3097 (if (and (or (not (boundp 'custom-menu-nesting))
3098 (>= custom-menu-nesting 0))
3099 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3100 (let ((custom-prefix-list (custom-prefix-add symbol
3101 custom-prefix-list))
3102 (members (custom-sort-items (get symbol 'custom-group)
3103 custom-menu-sort-alphabetically
3104 custom-menu-order-groups)))
3105 (custom-load-symbol symbol)
3106 `(,(custom-unlispify-menu-entry symbol t)
3107 ,item
3108 "--"
3109 ,@(mapcar (lambda (entry)
3110 (widget-apply (if (listp (nth 1 entry))
3111 (nth 1 entry)
3112 (list (nth 1 entry)))
3113 :custom-menu (nth 0 entry)))
3114 members)))
3115 item)))
3116
3117 ;;;###autoload
3118 (defun customize-menu-create (symbol &optional name)
3119 "Return a customize menu for customization group SYMBOL.
3120 If optional NAME is given, use that as the name of the menu.
3121 Otherwise the menu will be named `Customize'.
3122 The format is suitable for use with `easy-menu-define'."
3123 (unless name
3124 (setq name "Customize"))
3125 (if (string-match "XEmacs" emacs-version)
3126 ;; We can delay it under XEmacs.
3127 `(,name
3128 :filter (lambda (&rest junk)
3129 (cdr (custom-menu-create ',symbol))))
3130 ;; But we must create it now under Emacs.
3131 (cons name (cdr (custom-menu-create symbol)))))
3132
3133 ;;; The Custom Mode.
3134
3135 (defvar custom-mode-map nil
3136 "Keymap for `custom-mode'.")
3137
3138 (unless custom-mode-map
3139 (setq custom-mode-map (make-sparse-keymap))
3140 (set-keymap-parent custom-mode-map widget-keymap)
3141 (suppress-keymap custom-mode-map)
3142 (define-key custom-mode-map " " 'scroll-up)
3143 (define-key custom-mode-map "\177" 'scroll-down)
3144 (define-key custom-mode-map "q" 'bury-buffer)
3145 (define-key custom-mode-map "u" 'Custom-goto-parent)
3146 (define-key custom-mode-map "n" 'widget-forward)
3147 (define-key custom-mode-map "p" 'widget-backward)
3148 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3149
3150 (defun Custom-move-and-invoke (event)
3151 "Move to where you click, and if it is an active field, invoke it."
3152 (interactive "e")
3153 (mouse-set-point event)
3154 (if (widget-event-point event)
3155 (let* ((pos (widget-event-point event))
3156 (button (get-char-property pos 'button)))
3157 (if button
3158 (widget-button-click event)))))
3159
3160 (easy-menu-define Custom-mode-menu
3161 custom-mode-map
3162 "Menu used in customization buffers."
3163 `("Custom"
3164 ,(customize-menu-create 'customize)
3165 ["Set" Custom-set t]
3166 ["Save" Custom-save t]
3167 ["Reset to Current" Custom-reset-current t]
3168 ["Reset to Saved" Custom-reset-saved t]
3169 ["Reset to Standard Settings" Custom-reset-standard t]
3170 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
3171
3172 (defun Custom-goto-parent ()
3173 "Go to the parent group listed at the top of this buffer.
3174 If several parents are listed, go to the first of them."
3175 (interactive)
3176 (save-excursion
3177 (goto-char (point-min))
3178 (if (search-forward "\nGo to parent group: " nil t)
3179 (let* ((button (get-char-property (point) 'button))
3180 (parent (downcase (widget-get button :tag))))
3181 (customize-group parent)))))
3182
3183 (defcustom custom-mode-hook nil
3184 "Hook called when entering custom-mode."
3185 :type 'hook
3186 :group 'custom-buffer )
3187
3188 (defun custom-state-buffer-message (widget)
3189 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3190 (message "To install your edits, invoke [State] and choose the Set operation")))
3191
3192 (defun custom-mode ()
3193 "Major mode for editing customization buffers.
3194
3195 The following commands are available:
3196
3197 Move to next button or editable field. \\[widget-forward]
3198 Move to previous button or editable field. \\[widget-backward]
3199 \\<widget-field-keymap>\
3200 Complete content of editable text field. \\[widget-complete]
3201 \\<custom-mode-map>\
3202 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3203 Invoke button under point. \\[widget-button-press]
3204 Set all modifications. \\[Custom-set]
3205 Make all modifications default. \\[Custom-save]
3206 Reset all modified options. \\[Custom-reset-current]
3207 Reset all modified or set options. \\[Custom-reset-saved]
3208 Reset all options. \\[Custom-reset-standard]
3209
3210 Entry to this mode calls the value of `custom-mode-hook'
3211 if that value is non-nil."
3212 (kill-all-local-variables)
3213 (setq major-mode 'custom-mode
3214 mode-name "Custom")
3215 (use-local-map custom-mode-map)
3216 (easy-menu-add Custom-mode-menu)
3217 (make-local-variable 'custom-options)
3218 (make-local-variable 'widget-documentation-face)
3219 (setq widget-documentation-face 'custom-documentation-face)
3220 (make-local-variable 'widget-button-face)
3221 (setq widget-button-face 'custom-button-face)
3222 (make-local-hook 'widget-edit-functions)
3223 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3224 (run-hooks 'custom-mode-hook))
3225
3226 ;;; The End.
3227
3228 (provide 'cus-edit)
3229
3230 ;; cus-edit.el ends here