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