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