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