]> code.delx.au - gnu-emacs/blob - lisp/cus-edit.el
(describe-function-1): Kluge around cases of functions fset to subrs
[gnu-emacs] / 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 since-version
941 (setq since-version customize-changed-options-previous-release))
942 (let ((found nil)
943 (versions nil))
944 (mapatoms (lambda (symbol)
945 (and (or (boundp symbol)
946 ;; For variables not yet loaded.
947 (get symbol 'standard-value)
948 ;; For groups the previous test fails, this one
949 ;; could be used to determine if symbol is a
950 ;; group. Is there a better way for this?
951 (get symbol 'group-documentation))
952 (let ((version (get symbol 'custom-version)))
953 (and version
954 (or (null since-version)
955 (customize-version-lessp since-version version))
956 (if (member version versions)
957 t
958 ;;; Collect all versions that we use.
959 (push version versions))))
960 (setq found
961 ;; We have to set the right thing here,
962 ;; depending if we have a group or a
963 ;; variable.
964 (if (get symbol 'group-documentation)
965 (cons (list symbol 'custom-group) found)
966 (cons (list symbol 'custom-variable) found))))))
967 (if (not found)
968 (error "No user option defaults have been changed since Emacs %s"
969 since-version)
970 (let ((flist nil))
971 (while versions
972 (push (copy-sequence
973 (cdr (assoc (car versions) custom-versions-load-alist)))
974 flist)
975 (setq versions (cdr versions)))
976 (put 'custom-versions-load-alist 'custom-loads
977 ;; Get all the files that correspond to element from the
978 ;; VERSIONS list. This could use some simplification.
979 (apply 'nconc flist)))
980 ;; Because we set all the files needed to be loaded as a
981 ;; `custom-loads' property to `custom-versions-load-alist' this
982 ;; call will actually load them.
983 (custom-load-symbol 'custom-versions-load-alist)
984 ;; Clean up
985 (put 'custom-versions-load-alist 'custom-loads nil)
986 (custom-buffer-create (custom-sort-items found t 'first)
987 "*Customize Changed Options*"))))
988
989 (defun customize-version-lessp (version1 version2)
990 ;; In case someone made a mistake and left out the quotes
991 ;; in the :version value.
992 (if (numberp version2)
993 (setq version2 (prin1-to-string version2)))
994 (let (major1 major2 minor1 minor2)
995 (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version1)
996 (setq major1 (read (match-string 1 version1)))
997 (setq minor1 (read (match-string 2 version1)))
998 (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version2)
999 (setq major2 (read (match-string 1 version2)))
1000 (setq minor2 (read (match-string 2 version2)))
1001 (or (< major1 major2)
1002 (and (= major1 major2)
1003 (< minor1 minor2)))))
1004
1005 ;;;###autoload
1006 (defalias 'customize-variable-other-window 'customize-option-other-window)
1007
1008 ;;;###autoload
1009 (defun customize-option-other-window (symbol)
1010 "Customize SYMBOL, which must be a user option variable.
1011 Show the buffer in another window, but don't select it."
1012 (interactive (custom-variable-prompt))
1013 (custom-buffer-create-other-window
1014 (list (list symbol 'custom-variable))
1015 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
1016
1017 ;;;###autoload
1018 (defun customize-face (&optional symbol)
1019 "Customize SYMBOL, which should be a face name or nil.
1020 If SYMBOL is nil, customize all faces."
1021 (interactive (list (completing-read "Customize face: (default all) "
1022 obarray 'custom-facep t)))
1023 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
1024 (custom-buffer-create (custom-sort-items
1025 (mapcar (lambda (symbol)
1026 (list symbol 'custom-face))
1027 (face-list))
1028 t nil)
1029 "*Customize Faces*")
1030 (when (stringp symbol)
1031 (setq symbol (intern symbol)))
1032 (unless (symbolp symbol)
1033 (error "Should be a symbol %S" symbol))
1034 (custom-buffer-create (list (list symbol 'custom-face))
1035 (format "*Customize Face: %s*"
1036 (custom-unlispify-tag-name symbol)))))
1037
1038 ;;;###autoload
1039 (defun customize-face-other-window (&optional symbol)
1040 "Show customization buffer for face SYMBOL in other window."
1041 (interactive (list (completing-read "Customize face: "
1042 obarray 'custom-facep t)))
1043 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
1044 ()
1045 (if (stringp symbol)
1046 (setq symbol (intern symbol)))
1047 (unless (symbolp symbol)
1048 (error "Should be a symbol %S" symbol))
1049 (custom-buffer-create-other-window
1050 (list (list symbol 'custom-face))
1051 (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
1052
1053 ;;;###autoload
1054 (defun customize-customized ()
1055 "Customize all user options set since the last save in this session."
1056 (interactive)
1057 (let ((found nil))
1058 (mapatoms (lambda (symbol)
1059 (and (or (get symbol 'customized-face)
1060 (get symbol 'customized-face-comment))
1061 (custom-facep symbol)
1062 (push (list symbol 'custom-face) found))
1063 (and (or (get symbol 'customized-value)
1064 (get symbol 'customized-variable-comment))
1065 (boundp symbol)
1066 (push (list symbol 'custom-variable) found))))
1067 (if (not found)
1068 (error "No customized user options")
1069 (custom-buffer-create (custom-sort-items found t nil)
1070 "*Customize Customized*"))))
1071
1072 ;;;###autoload
1073 (defun customize-saved ()
1074 "Customize all already saved user options."
1075 (interactive)
1076 (let ((found nil))
1077 (mapatoms (lambda (symbol)
1078 (and (or (get symbol 'saved-face)
1079 (get symbol 'saved-face-comment))
1080 (custom-facep symbol)
1081 (push (list symbol 'custom-face) found))
1082 (and (or (get symbol 'saved-value)
1083 (get symbol 'saved-variable-comment))
1084 (boundp symbol)
1085 (push (list symbol 'custom-variable) found))))
1086 (if (not found )
1087 (error "No saved user options")
1088 (custom-buffer-create (custom-sort-items found t nil)
1089 "*Customize Saved*"))))
1090
1091 ;;;###autoload
1092 (defun customize-apropos (regexp &optional all)
1093 "Customize all user options matching REGEXP.
1094 If ALL is `options', include only options.
1095 If ALL is `faces', include only faces.
1096 If ALL is `groups', include only groups.
1097 If ALL is t (interactively, with prefix arg), include options which are not
1098 user-settable, as well as faces and groups."
1099 (interactive "sCustomize regexp: \nP")
1100 (let ((found nil))
1101 (mapatoms (lambda (symbol)
1102 (when (string-match regexp (symbol-name symbol))
1103 (when (and (not (memq all '(faces options)))
1104 (get symbol 'custom-group))
1105 (push (list symbol 'custom-group) found))
1106 (when (and (not (memq all '(options groups)))
1107 (custom-facep symbol))
1108 (push (list symbol 'custom-face) found))
1109 (when (and (not (memq all '(groups faces)))
1110 (boundp symbol)
1111 (or (get symbol 'saved-value)
1112 (get symbol 'standard-value)
1113 (if (memq all '(nil options))
1114 (user-variable-p symbol)
1115 (get symbol 'variable-documentation))))
1116 (push (list symbol 'custom-variable) found)))))
1117 (if (not found)
1118 (error "No matches")
1119 (custom-buffer-create (custom-sort-items found t
1120 custom-buffer-order-groups)
1121 "*Customize Apropos*"))))
1122
1123 ;;;###autoload
1124 (defun customize-apropos-options (regexp &optional arg)
1125 "Customize all user options matching REGEXP.
1126 With prefix arg, include options which are not user-settable."
1127 (interactive "sCustomize regexp: \nP")
1128 (customize-apropos regexp (or arg 'options)))
1129
1130 ;;;###autoload
1131 (defun customize-apropos-faces (regexp)
1132 "Customize all user faces matching REGEXP."
1133 (interactive "sCustomize regexp: \n")
1134 (customize-apropos regexp 'faces))
1135
1136 ;;;###autoload
1137 (defun customize-apropos-groups (regexp)
1138 "Customize all user groups matching REGEXP."
1139 (interactive "sCustomize regexp: \n")
1140 (customize-apropos regexp 'groups))
1141
1142 ;;; Buffer.
1143
1144 (defcustom custom-buffer-style 'links
1145 "Control the presentation style for customization buffers.
1146 The value should be a symbol, one of:
1147
1148 brackets: groups nest within each other with big horizontal brackets.
1149 links: groups have links to subgroups."
1150 :type '(radio (const brackets)
1151 (const links))
1152 :group 'custom-buffer)
1153
1154 ;; If we pass BUFFER to `bury-buffer', the buffer isn't removed from
1155 ;; the window.
1156 (defun custom-bury-buffer (buffer)
1157 (bury-buffer))
1158
1159 (defcustom custom-buffer-done-function 'custom-bury-buffer
1160 "*Function called to remove a Custom buffer when the user is done with it.
1161 Called with one argument, the buffer to remove."
1162 :type '(choice (function-item :tag "Bury buffer" custom-bury-buffer)
1163 (function-item :tag "Kill buffer" kill-buffer)
1164 (function :tag "Other"))
1165 :version "21.1"
1166 :group 'custom-buffer)
1167
1168 (defcustom custom-buffer-indent 3
1169 "Number of spaces to indent nested groups."
1170 :type 'integer
1171 :group 'custom-buffer)
1172
1173 ;;;###autoload
1174 (defun custom-buffer-create (options &optional name description)
1175 "Create a buffer containing OPTIONS.
1176 Optional NAME is the name of the buffer.
1177 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1178 SYMBOL is a customization option, and WIDGET is a widget for editing
1179 that option."
1180 (unless name (setq name "*Customization*"))
1181 (kill-buffer (get-buffer-create name))
1182 (pop-to-buffer (get-buffer-create name))
1183 (custom-buffer-create-internal options description))
1184
1185 ;;;###autoload
1186 (defun custom-buffer-create-other-window (options &optional name description)
1187 "Create a buffer containing OPTIONS.
1188 Optional NAME is the name of the buffer.
1189 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1190 SYMBOL is a customization option, and WIDGET is a widget for editing
1191 that option."
1192 (unless name (setq name "*Customization*"))
1193 (kill-buffer (get-buffer-create name))
1194 (let ((window (selected-window))
1195 (pop-up-windows t)
1196 (special-display-buffer-names nil)
1197 (special-display-regexps nil)
1198 (same-window-buffer-names nil)
1199 (same-window-regexps nil))
1200 (pop-to-buffer (get-buffer-create name))
1201 (custom-buffer-create-internal options description)
1202 (select-window window)))
1203
1204 (defcustom custom-reset-button-menu nil
1205 "If non-nil, only show a single reset button in customize buffers.
1206 This button will have a menu with all three reset operations."
1207 :type 'boolean
1208 :group 'custom-buffer)
1209
1210 (defun Custom-buffer-done (&rest ignore)
1211 "Remove current buffer by calling `custom-buffer-done-function'."
1212 (interactive)
1213 (funcall custom-buffer-done-function (current-buffer)))
1214
1215 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1216 '(("unspecified" . unspecified))))
1217 "If non-nil, indicate active buttons in a `raised-button' style.
1218 Otherwise use brackets."
1219 :type 'boolean
1220 :version "21.1"
1221 :group 'custom-buffer)
1222
1223 (defun custom-buffer-create-internal (options &optional description)
1224 (message "Creating customization buffer...")
1225 (custom-mode)
1226 (widget-insert "This is a customization buffer")
1227 (if description
1228 (widget-insert description))
1229 (widget-insert (format ".
1230 %s show active fields; type RET or click mouse-1
1231 on an active field to invoke its action. Editing an option value
1232 changes the text in the buffer; invoke the State button and
1233 choose the Set operation to set the option value.
1234 Invoke " (if custom-raised-buttons
1235 "`Raised' buttons"
1236 "Square brackets")))
1237 (widget-create 'info-link
1238 :tag "Help"
1239 :help-echo "Read the online help."
1240 "(emacs)Easy Customization")
1241 (widget-insert " for more information.\n\n")
1242 (message "Creating customization buttons...")
1243 (widget-insert "Operate on everything in this buffer:\n ")
1244 (widget-create 'push-button
1245 :tag "Set for Current Session"
1246 :help-echo "\
1247 Make your editing in this buffer take effect for this session."
1248 :action (lambda (widget &optional event)
1249 (Custom-set)))
1250 (widget-insert " ")
1251 (widget-create 'push-button
1252 :tag "Save for Future Sessions"
1253 :help-echo "\
1254 Make your editing in this buffer take effect for future Emacs sessions."
1255 :action (lambda (widget &optional event)
1256 (Custom-save)))
1257 (if custom-reset-button-menu
1258 (progn
1259 (widget-insert " ")
1260 (widget-create 'push-button
1261 :tag "Reset"
1262 :help-echo "Show a menu with reset operations."
1263 :mouse-down-action (lambda (&rest junk) t)
1264 :action (lambda (widget &optional event)
1265 (custom-reset event))))
1266 (widget-insert "\n ")
1267 (widget-create 'push-button
1268 :tag "Reset"
1269 :help-echo "\
1270 Reset all edited text in this buffer to reflect current values."
1271 :action 'Custom-reset-current)
1272 (widget-insert " ")
1273 (widget-create 'push-button
1274 :tag "Reset to Saved"
1275 :help-echo "\
1276 Reset all values in this buffer to their saved settings."
1277 :action 'Custom-reset-saved)
1278 (widget-insert " ")
1279 (widget-create 'push-button
1280 :tag "Erase Customization"
1281 :help-echo "\
1282 Un-customize all values in this buffer. They get their standard settings."
1283 :action 'Custom-reset-standard))
1284 (widget-insert " ")
1285 (widget-create 'push-button
1286 :tag "Finish"
1287 :help-echo "Bury or kill the buffer."
1288 :action #'Custom-buffer-done)
1289 (widget-insert "\n\n")
1290 (message "Creating customization items...")
1291 (buffer-disable-undo)
1292 (setq custom-options
1293 (if (= (length options) 1)
1294 (mapcar (lambda (entry)
1295 (widget-create (nth 1 entry)
1296 :documentation-shown t
1297 :custom-state 'unknown
1298 :tag (custom-unlispify-tag-name
1299 (nth 0 entry))
1300 :value (nth 0 entry)))
1301 options)
1302 (let ((count 0)
1303 (length (length options)))
1304 (mapcar (lambda (entry)
1305 (prog2
1306 (message "Creating customization items ...%2d%%"
1307 (/ (* 100.0 count) length))
1308 (widget-create (nth 1 entry)
1309 :tag (custom-unlispify-tag-name
1310 (nth 0 entry))
1311 :value (nth 0 entry))
1312 (setq count (1+ count))
1313 (unless (eq (preceding-char) ?\n)
1314 (widget-insert "\n"))
1315 (widget-insert "\n")))
1316 options))))
1317 (unless (eq (preceding-char) ?\n)
1318 (widget-insert "\n"))
1319 (message "Creating customization items ...%2d%%done" 100)
1320 (unless (eq custom-buffer-style 'tree)
1321 (mapc 'custom-magic-reset custom-options))
1322 (message "Creating customization setup...")
1323 (widget-setup)
1324 (buffer-enable-undo)
1325 (goto-char (point-min))
1326 (message "Creating customization buffer...done"))
1327
1328 ;;; The Tree Browser.
1329
1330 ;;;###autoload
1331 (defun customize-browse (&optional group)
1332 "Create a tree browser for the customize hierarchy."
1333 (interactive)
1334 (unless group
1335 (setq group 'emacs))
1336 (let ((name "*Customize Browser*"))
1337 (kill-buffer (get-buffer-create name))
1338 (pop-to-buffer (get-buffer-create name)))
1339 (custom-mode)
1340 (widget-insert "\
1341 Square brackets show active fields; type RET or click mouse-1
1342 on an active field to invoke its action.
1343 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1344 (if custom-browse-only-groups
1345 (widget-insert "\
1346 Invoke the [Group] button below to edit that item in another window.\n\n")
1347 (widget-insert "Invoke the ")
1348 (widget-create 'item
1349 :format "%t"
1350 :tag "[Group]"
1351 :tag-glyph "folder")
1352 (widget-insert ", ")
1353 (widget-create 'item
1354 :format "%t"
1355 :tag "[Face]"
1356 :tag-glyph "face")
1357 (widget-insert ", and ")
1358 (widget-create 'item
1359 :format "%t"
1360 :tag "[Option]"
1361 :tag-glyph "option")
1362 (widget-insert " buttons below to edit that
1363 item in another window.\n\n"))
1364 (let ((custom-buffer-style 'tree))
1365 (widget-create 'custom-group
1366 :custom-last t
1367 :custom-state 'unknown
1368 :tag (custom-unlispify-tag-name group)
1369 :value group))
1370 (goto-char (point-min)))
1371
1372 (define-widget 'custom-browse-visibility 'item
1373 "Control visibility of items in the customize tree browser."
1374 :format "%[[%t]%]"
1375 :action 'custom-browse-visibility-action)
1376
1377 (defun custom-browse-visibility-action (widget &rest ignore)
1378 (let ((custom-buffer-style 'tree))
1379 (custom-toggle-parent widget)))
1380
1381 (define-widget 'custom-browse-group-tag 'push-button
1382 "Show parent in other window when activated."
1383 :tag "Group"
1384 :tag-glyph "folder"
1385 :action 'custom-browse-group-tag-action)
1386
1387 (defun custom-browse-group-tag-action (widget &rest ignore)
1388 (let ((parent (widget-get widget :parent)))
1389 (customize-group-other-window (widget-value parent))))
1390
1391 (define-widget 'custom-browse-variable-tag 'push-button
1392 "Show parent in other window when activated."
1393 :tag "Option"
1394 :tag-glyph "option"
1395 :action 'custom-browse-variable-tag-action)
1396
1397 (defun custom-browse-variable-tag-action (widget &rest ignore)
1398 (let ((parent (widget-get widget :parent)))
1399 (customize-variable-other-window (widget-value parent))))
1400
1401 (define-widget 'custom-browse-face-tag 'push-button
1402 "Show parent in other window when activated."
1403 :tag "Face"
1404 :tag-glyph "face"
1405 :action 'custom-browse-face-tag-action)
1406
1407 (defun custom-browse-face-tag-action (widget &rest ignore)
1408 (let ((parent (widget-get widget :parent)))
1409 (customize-face-other-window (widget-value parent))))
1410
1411 (defconst custom-browse-alist '((" " "space")
1412 (" | " "vertical")
1413 ("-\\ " "top")
1414 (" |-" "middle")
1415 (" `-" "bottom")))
1416
1417 (defun custom-browse-insert-prefix (prefix)
1418 "Insert PREFIX. On XEmacs convert it to line graphics."
1419 ;; Fixme: do graphics.
1420 (if nil ; (string-match "XEmacs" emacs-version)
1421 (progn
1422 (insert "*")
1423 (while (not (string-equal prefix ""))
1424 (let ((entry (substring prefix 0 3)))
1425 (setq prefix (substring prefix 3))
1426 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1427 (name (nth 1 (assoc entry custom-browse-alist))))
1428 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1429 (overlay-put overlay 'start-open t)
1430 (overlay-put overlay 'end-open t)))))
1431 (insert prefix)))
1432
1433 ;;; Modification of Basic Widgets.
1434 ;;
1435 ;; We add extra properties to the basic widgets needed here. This is
1436 ;; fine, as long as we are careful to stay within out own namespace.
1437 ;;
1438 ;; We want simple widgets to be displayed by default, but complex
1439 ;; widgets to be hidden.
1440
1441 (widget-put (get 'item 'widget-type) :custom-show t)
1442 (widget-put (get 'editable-field 'widget-type)
1443 :custom-show (lambda (widget value)
1444 (let ((pp (pp-to-string value)))
1445 (cond ((string-match "\n" pp)
1446 nil)
1447 ((> (length pp) 40)
1448 nil)
1449 (t t)))))
1450 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1451
1452 ;;; The `custom-manual' Widget.
1453
1454 (define-widget 'custom-manual 'info-link
1455 "Link to the manual entry for this customization option."
1456 :help-echo "Read the manual entry for this option."
1457 :tag "Manual")
1458
1459 ;;; The `custom-magic' Widget.
1460
1461 (defgroup custom-magic-faces nil
1462 "Faces used by the magic button."
1463 :group 'custom-faces
1464 :group 'custom-buffer)
1465
1466 (defface custom-invalid-face '((((class color))
1467 (:foreground "yellow" :background "red"))
1468 (t
1469 (:bold t :italic t :underline t)))
1470 "Face used when the customize item is invalid."
1471 :group 'custom-magic-faces)
1472
1473 (defface custom-rogue-face '((((class color))
1474 (:foreground "pink" :background "black"))
1475 (t
1476 (:underline t)))
1477 "Face used when the customize item is not defined for customization."
1478 :group 'custom-magic-faces)
1479
1480 (defface custom-modified-face '((((class color))
1481 (:foreground "white" :background "blue"))
1482 (t
1483 (:italic t :bold)))
1484 "Face used when the customize item has been modified."
1485 :group 'custom-magic-faces)
1486
1487 (defface custom-set-face '((((class color))
1488 (:foreground "blue" :background "white"))
1489 (t
1490 (:italic t)))
1491 "Face used when the customize item has been set."
1492 :group 'custom-magic-faces)
1493
1494 (defface custom-changed-face '((((class color))
1495 (:foreground "white" :background "blue"))
1496 (t
1497 (:italic t)))
1498 "Face used when the customize item has been changed."
1499 :group 'custom-magic-faces)
1500
1501 (defface custom-saved-face '((t (:underline t)))
1502 "Face used when the customize item has been saved."
1503 :group 'custom-magic-faces)
1504
1505 (defconst custom-magic-alist '((nil "#" underline "\
1506 uninitialized, you should not see this.")
1507 (unknown "?" italic "\
1508 unknown, you should not see this.")
1509 (hidden "-" default "\
1510 hidden, invoke \"Show\" in the previous line to show." "\
1511 group now hidden, invoke \"Show\", above, to show contents.")
1512 (invalid "x" custom-invalid-face "\
1513 the value displayed for this %c is invalid and cannot be set.")
1514 (modified "*" custom-modified-face "\
1515 you have edited the value as text, but you have not set the %c." "\
1516 you have edited something in this group, but not set it.")
1517 (set "+" custom-set-face "\
1518 you have set this %c, but not saved it for future sessions." "\
1519 something in this group has been set, but not saved.")
1520 (changed ":" custom-changed-face "\
1521 this %c has been changed outside the customize buffer." "\
1522 something in this group has been changed outside customize.")
1523 (saved "!" custom-saved-face "\
1524 this %c has been set and saved." "\
1525 something in this group has been set and saved.")
1526 (rogue "@" custom-rogue-face "\
1527 this %c has not been changed with customize." "\
1528 something in this group is not prepared for customization.")
1529 (standard " " nil "\
1530 this %c is unchanged from its standard setting." "\
1531 visible group members are all at standard settings."))
1532 "Alist of customize option states.
1533 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1534
1535 STATE is one of the following symbols:
1536
1537 `nil'
1538 For internal use, should never occur.
1539 `unknown'
1540 For internal use, should never occur.
1541 `hidden'
1542 This item is not being displayed.
1543 `invalid'
1544 This item is modified, but has an invalid form.
1545 `modified'
1546 This item is modified, and has a valid form.
1547 `set'
1548 This item has been set but not saved.
1549 `changed'
1550 The current value of this item has been changed temporarily.
1551 `saved'
1552 This item is marked for saving.
1553 `rogue'
1554 This item has no customization information.
1555 `standard'
1556 This item is unchanged from the standard setting.
1557
1558 MAGIC is a string used to present that state.
1559
1560 FACE is a face used to present the state.
1561
1562 ITEM-DESC is a string describing the state for options.
1563
1564 GROUP-DESC is a string describing the state for groups. If this is
1565 left out, ITEM-DESC will be used.
1566
1567 The string %c in either description will be replaced with the
1568 category of the item. These are `group'. `option', and `face'.
1569
1570 The list should be sorted most significant first.")
1571
1572 (defcustom custom-magic-show 'long
1573 "If non-nil, show textual description of the state.
1574 If `long', show a full-line description, not just one word."
1575 :type '(choice (const :tag "no" nil)
1576 (const long)
1577 (other :tag "short" short))
1578 :group 'custom-buffer)
1579
1580 (defcustom custom-magic-show-hidden '(option face)
1581 "Control whether the State button is shown for hidden items.
1582 The value should be a list with the custom categories where the State
1583 button should be visible. Possible categories are `group', `option',
1584 and `face'."
1585 :type '(set (const group) (const option) (const face))
1586 :group 'custom-buffer)
1587
1588 (defcustom custom-magic-show-button nil
1589 "Show a \"magic\" button indicating the state of each customization option."
1590 :type 'boolean
1591 :group 'custom-buffer)
1592
1593 (define-widget 'custom-magic 'default
1594 "Show and manipulate state for a customization option."
1595 :format "%v"
1596 :action 'widget-parent-action
1597 :notify 'ignore
1598 :value-get 'ignore
1599 :value-create 'custom-magic-value-create
1600 :value-delete 'widget-children-value-delete)
1601
1602 (defun widget-magic-mouse-down-action (widget &optional event)
1603 ;; Non-nil unless hidden.
1604 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1605 :custom-state)
1606 'hidden)))
1607
1608 (defun custom-magic-value-create (widget)
1609 "Create compact status report for WIDGET."
1610 (let* ((parent (widget-get widget :parent))
1611 (state (widget-get parent :custom-state))
1612 (hidden (eq state 'hidden))
1613 (entry (assq state custom-magic-alist))
1614 (magic (nth 1 entry))
1615 (face (nth 2 entry))
1616 (category (widget-get parent :custom-category))
1617 (text (or (and (eq category 'group)
1618 (nth 4 entry))
1619 (nth 3 entry)))
1620 (form (widget-get parent :custom-form))
1621 children)
1622 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1623 (setq text (concat (match-string 1 text)
1624 (symbol-name category)
1625 (match-string 2 text))))
1626 (when (and custom-magic-show
1627 (or (not hidden)
1628 (memq category custom-magic-show-hidden)))
1629 (insert " ")
1630 (when (and (eq category 'group)
1631 (not (and (eq custom-buffer-style 'links)
1632 (> (widget-get parent :custom-level) 1))))
1633 (insert-char ?\ (* custom-buffer-indent
1634 (widget-get parent :custom-level))))
1635 (push (widget-create-child-and-convert
1636 widget 'choice-item
1637 :help-echo "Change the state of this item."
1638 :format (if hidden "%t" "%[%t%]")
1639 :button-prefix 'widget-push-button-prefix
1640 :button-suffix 'widget-push-button-suffix
1641 :mouse-down-action 'widget-magic-mouse-down-action
1642 :tag "State")
1643 children)
1644 (insert ": ")
1645 (let ((start (point)))
1646 (if (eq custom-magic-show 'long)
1647 (insert text)
1648 (insert (symbol-name state)))
1649 (cond ((eq form 'lisp)
1650 (insert " (lisp)"))
1651 ((eq form 'mismatch)
1652 (insert " (mismatch)")))
1653 (put-text-property start (point) 'face 'custom-state-face))
1654 (insert "\n"))
1655 (when (and (eq category 'group)
1656 (not (and (eq custom-buffer-style 'links)
1657 (> (widget-get parent :custom-level) 1))))
1658 (insert-char ?\ (* custom-buffer-indent
1659 (widget-get parent :custom-level))))
1660 (when custom-magic-show-button
1661 (when custom-magic-show
1662 (let ((indent (widget-get parent :indent)))
1663 (when indent
1664 (insert-char ? indent))))
1665 (push (widget-create-child-and-convert
1666 widget 'choice-item
1667 :mouse-down-action 'widget-magic-mouse-down-action
1668 :button-face face
1669 :button-prefix ""
1670 :button-suffix ""
1671 :help-echo "Change the state."
1672 :format (if hidden "%t" "%[%t%]")
1673 :tag (if (memq form '(lisp mismatch))
1674 (concat "(" magic ")")
1675 (concat "[" magic "]")))
1676 children)
1677 (insert " "))
1678 (widget-put widget :children children)))
1679
1680 (defun custom-magic-reset (widget)
1681 "Redraw the :custom-magic property of WIDGET."
1682 (let ((magic (widget-get widget :custom-magic)))
1683 (widget-value-set magic (widget-value magic))))
1684
1685 ;;; The `custom' Widget.
1686
1687 (defface custom-button-face
1688 '((((type x) (class color)) ; Like default modeline
1689 (:box (:line-width 2 :style released-button) :background "lightgrey"))
1690 (((type w32) (class color)) ; Like default modeline
1691 (:box (:line-width 2 :style released-button) :background "lightgrey"))
1692 (t
1693 nil))
1694 "Face used for buttons in customization buffers."
1695 :version "21.1"
1696 :group 'custom-faces)
1697
1698 (defface custom-button-pressed-face
1699 '((((type x) (class color))
1700 (:box (:line-width 2 :style pressed-button) :background "lightgrey"))
1701 (((type w32) (class color))
1702 (:box (:line-width 2 :style pressed-button) :background "lightgrey"))
1703 (t
1704 (:inverse-video t)))
1705 "Face used for buttons in customization buffers."
1706 :version "21.1"
1707 :group 'custom-faces)
1708
1709 (defface custom-documentation-face nil
1710 "Face used for documentation strings in customization buffers."
1711 :group 'custom-faces)
1712
1713 (defface custom-state-face '((((class color)
1714 (background dark))
1715 (:foreground "lime green"))
1716 (((class color)
1717 (background light))
1718 (:foreground "dark green"))
1719 (t nil))
1720 "Face used for State descriptions in the customize buffer."
1721 :group 'custom-faces)
1722
1723 (define-widget 'custom 'default
1724 "Customize a user option."
1725 :format "%v"
1726 :convert-widget 'custom-convert-widget
1727 :notify 'custom-notify
1728 :custom-prefix ""
1729 :custom-level 1
1730 :custom-state 'hidden
1731 :documentation-property 'widget-subclass-responsibility
1732 :value-create 'widget-subclass-responsibility
1733 :value-delete 'widget-children-value-delete
1734 :value-get 'widget-value-value-get
1735 :validate 'widget-children-validate
1736 :match (lambda (widget value) (symbolp value)))
1737
1738 (defun custom-convert-widget (widget)
1739 "Initialize :value and :tag from :args in WIDGET."
1740 (let ((args (widget-get widget :args)))
1741 (when args
1742 (widget-put widget :value (widget-apply widget
1743 :value-to-internal (car args)))
1744 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1745 (widget-put widget :args nil)))
1746 widget)
1747
1748 (defun custom-notify (widget &rest args)
1749 "Keep track of changes."
1750 (let ((state (widget-get widget :custom-state)))
1751 (unless (eq state 'modified)
1752 (unless (memq state '(nil unknown hidden))
1753 (widget-put widget :custom-state 'modified))
1754 (custom-magic-reset widget)
1755 (apply 'widget-default-notify widget args))))
1756
1757 (defun custom-redraw (widget)
1758 "Redraw WIDGET with current settings."
1759 (let ((line (count-lines (point-min) (point)))
1760 (column (current-column))
1761 (pos (point))
1762 (from (marker-position (widget-get widget :from)))
1763 (to (marker-position (widget-get widget :to))))
1764 (save-excursion
1765 (widget-value-set widget (widget-value widget))
1766 (custom-redraw-magic widget))
1767 (when (and (>= pos from) (<= pos to))
1768 (condition-case nil
1769 (progn
1770 (if (> column 0)
1771 (goto-line line)
1772 (goto-line (1+ line)))
1773 (move-to-column column))
1774 (error nil)))))
1775
1776 (defun custom-redraw-magic (widget)
1777 "Redraw WIDGET state with current settings."
1778 (while widget
1779 (let ((magic (widget-get widget :custom-magic)))
1780 (cond (magic
1781 (widget-value-set magic (widget-value magic))
1782 (when (setq widget (widget-get widget :group))
1783 (custom-group-state-update widget)))
1784 (t
1785 (setq widget nil)))))
1786 (widget-setup))
1787
1788 (defun custom-show (widget value)
1789 "Non-nil if WIDGET should be shown with VALUE by default."
1790 (let ((show (widget-get widget :custom-show)))
1791 (cond ((null show)
1792 nil)
1793 ((eq t show)
1794 t)
1795 (t
1796 (funcall show widget value)))))
1797
1798 (defvar custom-load-recursion nil
1799 "Hack to avoid recursive dependencies.")
1800
1801 (defun custom-load-symbol (symbol)
1802 "Load all dependencies for SYMBOL."
1803 (unless custom-load-recursion
1804 (let ((custom-load-recursion t)
1805 (loads (get symbol 'custom-loads))
1806 load)
1807 (while loads
1808 (setq load (car loads)
1809 loads (cdr loads))
1810 (cond ((symbolp load)
1811 (condition-case nil
1812 (require load)
1813 (error nil)))
1814 ;; Don't reload a file already loaded.
1815 ((and (boundp 'preloaded-file-list)
1816 (member load preloaded-file-list)))
1817 ((assoc load load-history))
1818 ((assoc (locate-library load) load-history))
1819 (t
1820 (condition-case nil
1821 ;; Without this, we would load cus-edit recursively.
1822 ;; We are still loading it when we call this,
1823 ;; and it is not in load-history yet.
1824 (or (equal load "cus-edit")
1825 (load-library load))
1826 (error nil))))))))
1827
1828 (defun custom-load-widget (widget)
1829 "Load all dependencies for WIDGET."
1830 (custom-load-symbol (widget-value widget)))
1831
1832 (defun custom-unloaded-symbol-p (symbol)
1833 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1834 (let ((found nil)
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 (unless (featurep load)
1842 (setq found t)))
1843 ((assoc load load-history))
1844 ((assoc (locate-library load) load-history)
1845 (message nil))
1846 (t
1847 (setq found t))))
1848 found))
1849
1850 (defun custom-unloaded-widget-p (widget)
1851 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1852 (custom-unloaded-symbol-p (widget-value widget)))
1853
1854 (defun custom-toggle-hide (widget)
1855 "Toggle visibility of WIDGET."
1856 (custom-load-widget widget)
1857 (let ((state (widget-get widget :custom-state)))
1858 (cond ((memq state '(invalid modified))
1859 (error "There are unset changes"))
1860 ((eq state 'hidden)
1861 (widget-put widget :custom-state 'unknown))
1862 (t
1863 (widget-put widget :documentation-shown nil)
1864 (widget-put widget :custom-state 'hidden)))
1865 (custom-redraw widget)
1866 (widget-setup)))
1867
1868 (defun custom-toggle-parent (widget &rest ignore)
1869 "Toggle visibility of parent of WIDGET."
1870 (custom-toggle-hide (widget-get widget :parent)))
1871
1872 (defun custom-add-see-also (widget &optional prefix)
1873 "Add `See also ...' to WIDGET if there are any links.
1874 Insert PREFIX first if non-nil."
1875 (let* ((symbol (widget-get widget :value))
1876 (links (get symbol 'custom-links))
1877 (many (> (length links) 2))
1878 (buttons (widget-get widget :buttons))
1879 (indent (widget-get widget :indent)))
1880 (when links
1881 (when indent
1882 (insert-char ?\ indent))
1883 (when prefix
1884 (insert prefix))
1885 (insert "See also ")
1886 (while links
1887 (push (widget-create-child-and-convert widget (car links))
1888 buttons)
1889 (setq links (cdr links))
1890 (cond ((null links)
1891 (insert ".\n"))
1892 ((null (cdr links))
1893 (if many
1894 (insert ", and ")
1895 (insert " and ")))
1896 (t
1897 (insert ", "))))
1898 (widget-put widget :buttons buttons))))
1899
1900 (defun custom-add-parent-links (widget &optional initial-string)
1901 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1902 The value if non-nil if any parents were found.
1903 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1904 (let ((name (widget-value widget))
1905 (type (widget-type widget))
1906 (buttons (widget-get widget :buttons))
1907 (start (point))
1908 found)
1909 (insert (or initial-string "Parent groups:"))
1910 (mapatoms (lambda (symbol)
1911 (let ((entry (assq name (get symbol 'custom-group))))
1912 (when (eq (nth 1 entry) type)
1913 (insert " ")
1914 (push (widget-create-child-and-convert
1915 widget 'custom-group-link
1916 :tag (custom-unlispify-tag-name symbol)
1917 symbol)
1918 buttons)
1919 (setq found t)))))
1920 (widget-put widget :buttons buttons)
1921 (if found
1922 (insert "\n")
1923 (delete-region start (point)))
1924 found))
1925
1926 ;;; The `custom-comment' Widget.
1927
1928 ;; like the editable field
1929 (defface custom-comment-face '((((class grayscale color)
1930 (background light))
1931 (:background "gray85"))
1932 (((class grayscale color)
1933 (background dark))
1934 (:background "dim gray"))
1935 (t
1936 (:italic t)))
1937 "Face used for comments on variables or faces"
1938 :version "21.1"
1939 :group 'custom-faces)
1940
1941 ;; like font-lock-comment-face
1942 (defface custom-comment-tag-face
1943 '((((class color) (background dark)) (:foreground "gray80"))
1944 (((class color) (background light)) (:foreground "blue4"))
1945 (((class grayscale) (background light))
1946 (:foreground "DimGray" :bold t :italic t))
1947 (((class grayscale) (background dark))
1948 (:foreground "LightGray" :bold t :italic t))
1949 (t (:bold t)))
1950 "Face used for variables or faces comment tags"
1951 :group 'custom-faces)
1952
1953 (define-widget 'custom-comment 'string
1954 "User comment."
1955 :tag "Comment"
1956 :help-echo "Edit a comment here."
1957 :sample-face 'custom-comment-tag-face
1958 :value-face 'custom-comment-face
1959 :shown nil
1960 :create 'custom-comment-create)
1961
1962 (defun custom-comment-create (widget)
1963 (let* ((null-comment (equal "" (widget-value widget))))
1964 (if (or (widget-get (widget-get widget :parent) :comment-shown)
1965 (not null-comment))
1966 (widget-default-create widget)
1967 ;; `widget-default-delete' expects markers in these slots --
1968 ;; maybe it shouldn't.
1969 (widget-put widget :from (point-marker))
1970 (widget-put widget :to (point-marker)))))
1971
1972 (defun custom-comment-hide (widget)
1973 (widget-put (widget-get widget :parent) :comment-shown nil))
1974
1975 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
1976 ;; the global custom one
1977 (defun custom-comment-show (widget)
1978 (widget-put widget :comment-shown t)
1979 (custom-redraw widget)
1980 (widget-setup))
1981
1982 (defun custom-comment-invisible-p (widget)
1983 (let ((val (widget-value (widget-get widget :comment-widget))))
1984 (and (equal "" val)
1985 (not (widget-get widget :comment-shown)))))
1986
1987 ;;; The `custom-variable' Widget.
1988
1989 ;; When this was underlined blue, users confused it with a
1990 ;; Mosaic-style hyperlink...
1991 (defface custom-variable-tag-face
1992 `((((class color)
1993 (background dark))
1994 (:foreground "light blue" :bold t :family "helv"
1995 :height ,(let ((height (face-attribute 'default :height)))
1996 (if (numberp height)
1997 (floor height 0.9)
1998 height))))
1999 (((class color)
2000 (background light))
2001 (:foreground "blue" :family "helv" :bold t
2002 :height ,(let ((height (face-attribute 'default :height)))
2003 (if (numberp height)
2004 (floor height 0.9)
2005 height))))
2006 (t (:bold t)))
2007 "Face used for unpushable variable tags."
2008 :group 'custom-faces)
2009
2010 (defface custom-variable-button-face '((t (:underline t :bold t)))
2011 "Face used for pushable variable tags."
2012 :group 'custom-faces)
2013
2014 (defcustom custom-variable-default-form 'edit
2015 "Default form of displaying variable values."
2016 :type '(choice (const edit)
2017 (const lisp))
2018 :group 'custom-buffer
2019 :version "20.3")
2020
2021 (define-widget 'custom-variable 'custom
2022 "Customize variable."
2023 :format "%v"
2024 :help-echo "Set or reset this variable."
2025 :documentation-property 'variable-documentation
2026 :custom-category 'option
2027 :custom-state nil
2028 :custom-menu 'custom-variable-menu-create
2029 :custom-form nil ; defaults to value of `custom-variable-default-form'
2030 :value-create 'custom-variable-value-create
2031 :action 'custom-variable-action
2032 :custom-set 'custom-variable-set
2033 :custom-save 'custom-variable-save
2034 :custom-reset-current 'custom-redraw
2035 :custom-reset-saved 'custom-variable-reset-saved
2036 :custom-reset-standard 'custom-variable-reset-standard)
2037
2038 (defun custom-variable-type (symbol)
2039 "Return a widget suitable for editing the value of SYMBOL.
2040 If SYMBOL has a `custom-type' property, use that.
2041 Otherwise, look up symbol in `custom-guess-type-alist'."
2042 (let* ((type (or (get symbol 'custom-type)
2043 (and (not (get symbol 'standard-value))
2044 (custom-guess-type symbol))
2045 'sexp))
2046 (options (get symbol 'custom-options))
2047 (tmp (if (listp type)
2048 (copy-sequence type)
2049 (list type))))
2050 (when options
2051 (widget-put tmp :options options))
2052 tmp))
2053
2054 (defun custom-variable-value-create (widget)
2055 "Here is where you edit the variable's value."
2056 (custom-load-widget widget)
2057 (unless (widget-get widget :custom-form)
2058 (widget-put widget :custom-form custom-variable-default-form))
2059 (let* ((buttons (widget-get widget :buttons))
2060 (children (widget-get widget :children))
2061 (form (widget-get widget :custom-form))
2062 (state (widget-get widget :custom-state))
2063 (symbol (widget-get widget :value))
2064 (tag (widget-get widget :tag))
2065 (type (custom-variable-type symbol))
2066 (conv (widget-convert type))
2067 (get (or (get symbol 'custom-get) 'default-value))
2068 (prefix (widget-get widget :custom-prefix))
2069 (last (widget-get widget :custom-last))
2070 (value (if (default-boundp symbol)
2071 (funcall get symbol)
2072 (widget-get conv :value))))
2073 ;; If the widget is new, the child determines whether it is hidden.
2074 (cond (state)
2075 ((custom-show type value)
2076 (setq state 'unknown))
2077 (t
2078 (setq state 'hidden)))
2079 ;; If we don't know the state, see if we need to edit it in lisp form.
2080 (when (eq state 'unknown)
2081 (unless (widget-apply conv :match value)
2082 ;; (widget-apply (widget-convert type) :match value)
2083 (setq form 'mismatch)))
2084 ;; Now we can create the child widget.
2085 (cond ((eq custom-buffer-style 'tree)
2086 (insert prefix (if last " `--- " " |--- "))
2087 (push (widget-create-child-and-convert
2088 widget 'custom-browse-variable-tag)
2089 buttons)
2090 (insert " " tag "\n")
2091 (widget-put widget :buttons buttons))
2092 ((eq state 'hidden)
2093 ;; Indicate hidden value.
2094 (push (widget-create-child-and-convert
2095 widget 'item
2096 :format "%{%t%}: "
2097 :sample-face 'custom-variable-tag-face
2098 :tag tag
2099 :parent widget)
2100 buttons)
2101 (push (widget-create-child-and-convert
2102 widget 'visibility
2103 :help-echo "Show the value of this option."
2104 :action 'custom-toggle-parent
2105 nil)
2106 buttons))
2107 ((memq form '(lisp mismatch))
2108 ;; In lisp mode edit the saved value when possible.
2109 (let* ((value (cond ((get symbol 'saved-value)
2110 (car (get symbol 'saved-value)))
2111 ((get symbol 'standard-value)
2112 (car (get symbol 'standard-value)))
2113 ((default-boundp symbol)
2114 (custom-quote (funcall get symbol)))
2115 (t
2116 (custom-quote (widget-get conv :value))))))
2117 (insert (symbol-name symbol) ": ")
2118 (push (widget-create-child-and-convert
2119 widget 'visibility
2120 :help-echo "Hide the value of this option."
2121 :action 'custom-toggle-parent
2122 t)
2123 buttons)
2124 (insert " ")
2125 (push (widget-create-child-and-convert
2126 widget 'sexp
2127 :button-face 'custom-variable-button-face
2128 :format "%v"
2129 :tag (symbol-name symbol)
2130 :parent widget
2131 :value value)
2132 children)))
2133 (t
2134 ;; Edit mode.
2135 (let* ((format (widget-get type :format))
2136 tag-format value-format)
2137 (unless (string-match ":" format)
2138 (error "Bad format"))
2139 (setq tag-format (substring format 0 (match-end 0)))
2140 (setq value-format (substring format (match-end 0)))
2141 (push (widget-create-child-and-convert
2142 widget 'item
2143 :format tag-format
2144 :action 'custom-tag-action
2145 :help-echo "Change value of this option."
2146 :mouse-down-action 'custom-tag-mouse-down-action
2147 :button-face 'custom-variable-button-face
2148 :sample-face 'custom-variable-tag-face
2149 tag)
2150 buttons)
2151 (insert " ")
2152 (push (widget-create-child-and-convert
2153 widget 'visibility
2154 :help-echo "Hide the value of this option."
2155 :action 'custom-toggle-parent
2156 t)
2157 buttons)
2158 (push (widget-create-child-and-convert
2159 widget type
2160 :format value-format
2161 :value value)
2162 children))))
2163 (unless (eq custom-buffer-style 'tree)
2164 (unless (eq (preceding-char) ?\n)
2165 (widget-insert "\n"))
2166 ;; Create the magic button.
2167 (let ((magic (widget-create-child-and-convert
2168 widget 'custom-magic nil)))
2169 (widget-put widget :custom-magic magic)
2170 (push magic buttons))
2171 ;; ### NOTE: this is ugly!!!! I need to update the :buttons property
2172 ;; before the call to `widget-default-format-handler'. Otherwise, I
2173 ;; loose my current `buttons'. This function shouldn't be called like
2174 ;; this anyway. The doc string widget should be added like the others.
2175 ;; --dv
2176 (widget-put widget :buttons buttons)
2177 ;; Insert documentation.
2178 (widget-default-format-handler widget ?h)
2179
2180 ;; The comment field
2181 (unless (eq state 'hidden)
2182 (let* ((comment (get symbol 'variable-comment))
2183 (comment-widget
2184 (widget-create-child-and-convert
2185 widget 'custom-comment
2186 :parent widget
2187 :value (or comment ""))))
2188 (widget-put widget :comment-widget comment-widget)
2189 ;; Don't push it !!! Custom assumes that the first child is the
2190 ;; value one.
2191 (setq children (append children (list comment-widget)))))
2192 ;; Update the rest of the properties properties.
2193 (widget-put widget :custom-form form)
2194 (widget-put widget :children children)
2195 ;; Now update the state.
2196 (if (eq state 'hidden)
2197 (widget-put widget :custom-state state)
2198 (custom-variable-state-set widget))
2199 ;; See also.
2200 (unless (eq state 'hidden)
2201 (when (eq (widget-get widget :custom-level) 1)
2202 (custom-add-parent-links widget))
2203 (custom-add-see-also widget)))))
2204
2205 (defun custom-tag-action (widget &rest args)
2206 "Pass :action to first child of WIDGET's parent."
2207 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2208 :action args))
2209
2210 (defun custom-tag-mouse-down-action (widget &rest args)
2211 "Pass :mouse-down-action to first child of WIDGET's parent."
2212 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2213 :mouse-down-action args))
2214
2215 (defun custom-variable-state-set (widget)
2216 "Set the state of WIDGET."
2217 (let* ((symbol (widget-value widget))
2218 (get (or (get symbol 'custom-get) 'default-value))
2219 (value (if (default-boundp symbol)
2220 (funcall get symbol)
2221 (widget-get widget :value)))
2222 (comment (get symbol 'variable-comment))
2223 tmp
2224 temp
2225 (state (cond ((progn (setq tmp (get symbol 'customized-value))
2226 (setq temp
2227 (get symbol 'customized-variable-comment))
2228 (or tmp temp))
2229 (if (condition-case nil
2230 (and (equal value (eval (car tmp)))
2231 (equal comment temp))
2232 (error nil))
2233 'set
2234 'changed))
2235 ((progn (setq tmp (get symbol 'saved-value))
2236 (setq temp (get symbol 'saved-variable-comment))
2237 (or tmp temp))
2238 (if (condition-case nil
2239 (and (equal value (eval (car tmp)))
2240 (equal comment temp))
2241 (error nil))
2242 'saved
2243 'changed))
2244 ((setq tmp (get symbol 'standard-value))
2245 (if (condition-case nil
2246 (and (equal value (eval (car tmp)))
2247 (equal comment nil))
2248 (error nil))
2249 'standard
2250 'changed))
2251 (t 'rogue))))
2252 (widget-put widget :custom-state state)))
2253
2254 (defvar custom-variable-menu
2255 '(("Set for Current Session" custom-variable-set
2256 (lambda (widget)
2257 (eq (widget-get widget :custom-state) 'modified)))
2258 ("Save for Future Sessions" custom-variable-save
2259 (lambda (widget)
2260 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
2261 ("Reset to Current" custom-redraw
2262 (lambda (widget)
2263 (and (default-boundp (widget-value widget))
2264 (memq (widget-get widget :custom-state) '(modified changed)))))
2265 ("Reset to Saved" custom-variable-reset-saved
2266 (lambda (widget)
2267 (and (or (get (widget-value widget) 'saved-value)
2268 (get (widget-value widget) 'saved-variable-comment))
2269 (memq (widget-get widget :custom-state)
2270 '(modified set changed rogue)))))
2271 ("Erase Customization" custom-variable-reset-standard
2272 (lambda (widget)
2273 (and (get (widget-value widget) 'standard-value)
2274 (memq (widget-get widget :custom-state)
2275 '(modified set changed saved rogue)))))
2276 ("---" ignore ignore)
2277 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2278 ("---" ignore ignore)
2279 ("Don't show as Lisp expression" custom-variable-edit
2280 (lambda (widget)
2281 (eq (widget-get widget :custom-form) 'lisp)))
2282 ("Show initial Lisp expression" custom-variable-edit-lisp
2283 (lambda (widget)
2284 (eq (widget-get widget :custom-form) 'edit))))
2285 "Alist of actions for the `custom-variable' widget.
2286 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2287 the menu entry, ACTION is the function to call on the widget when the
2288 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2289 widget as an argument, and returns non-nil if ACTION is valid on that
2290 widget. If FILTER is nil, ACTION is always valid.")
2291
2292 (defun custom-variable-action (widget &optional event)
2293 "Show the menu for `custom-variable' WIDGET.
2294 Optional EVENT is the location for the menu."
2295 (if (eq (widget-get widget :custom-state) 'hidden)
2296 (custom-toggle-hide widget)
2297 (unless (eq (widget-get widget :custom-state) 'modified)
2298 (custom-variable-state-set widget))
2299 (custom-redraw-magic widget)
2300 (let* ((completion-ignore-case t)
2301 (answer (widget-choose (concat "Operation on "
2302 (custom-unlispify-tag-name
2303 (widget-get widget :value)))
2304 (custom-menu-filter custom-variable-menu
2305 widget)
2306 event)))
2307 (if answer
2308 (funcall answer widget)))))
2309
2310 (defun custom-variable-edit (widget)
2311 "Edit value of WIDGET."
2312 (widget-put widget :custom-state 'unknown)
2313 (widget-put widget :custom-form 'edit)
2314 (custom-redraw widget))
2315
2316 (defun custom-variable-edit-lisp (widget)
2317 "Edit the Lisp representation of the value of WIDGET."
2318 (widget-put widget :custom-state 'unknown)
2319 (widget-put widget :custom-form 'lisp)
2320 (custom-redraw widget))
2321
2322 (defun custom-variable-set (widget)
2323 "Set the current value for the variable being edited by WIDGET."
2324 (let* ((form (widget-get widget :custom-form))
2325 (state (widget-get widget :custom-state))
2326 (child (car (widget-get widget :children)))
2327 (symbol (widget-value widget))
2328 (set (or (get symbol 'custom-set) 'set-default))
2329 (comment-widget (widget-get widget :comment-widget))
2330 (comment (widget-value comment-widget))
2331 val)
2332 (cond ((eq state 'hidden)
2333 (error "Cannot set hidden variable"))
2334 ((setq val (widget-apply child :validate))
2335 (goto-char (widget-get val :from))
2336 (error "%s" (widget-get val :error)))
2337 ((memq form '(lisp mismatch))
2338 (when (equal comment "")
2339 (setq comment nil)
2340 ;; Make the comment invisible by hand if it's empty
2341 (custom-comment-hide comment-widget))
2342 (funcall set symbol (eval (setq val (widget-value child))))
2343 (put symbol 'customized-value (list val))
2344 (put symbol 'variable-comment comment)
2345 (put symbol 'customized-variable-comment comment))
2346 (t
2347 (when (equal comment "")
2348 (setq comment nil)
2349 ;; Make the comment invisible by hand if it's empty
2350 (custom-comment-hide comment-widget))
2351 (funcall set symbol (setq val (widget-value child)))
2352 (put symbol 'customized-value (list (custom-quote val)))
2353 (put symbol 'variable-comment comment)
2354 (put symbol 'customized-variable-comment comment)))
2355 (custom-variable-state-set widget)
2356 (custom-redraw-magic widget)))
2357
2358 (defun custom-variable-save (widget)
2359 "Set and save the value for the variable being edited by WIDGET."
2360 (let* ((form (widget-get widget :custom-form))
2361 (state (widget-get widget :custom-state))
2362 (child (car (widget-get widget :children)))
2363 (symbol (widget-value widget))
2364 (set (or (get symbol 'custom-set) 'set-default))
2365 (comment-widget (widget-get widget :comment-widget))
2366 (comment (widget-value comment-widget))
2367 val)
2368 (cond ((eq state 'hidden)
2369 (error "Cannot set hidden variable"))
2370 ((setq val (widget-apply child :validate))
2371 (goto-char (widget-get val :from))
2372 (error "%s" (widget-get val :error)))
2373 ((memq form '(lisp mismatch))
2374 (when (equal comment "")
2375 (setq comment nil)
2376 ;; Make the comment invisible by hand if it's empty
2377 (custom-comment-hide comment-widget))
2378 (put symbol 'saved-value (list (widget-value child)))
2379 (funcall set symbol (eval (widget-value child)))
2380 (put symbol 'variable-comment comment)
2381 (put symbol 'saved-variable-comment comment))
2382 (t
2383 (when (equal comment "")
2384 (setq comment nil)
2385 ;; Make the comment invisible by hand if it's empty
2386 (custom-comment-hide comment-widget))
2387 (put symbol 'saved-value
2388 (list (custom-quote (widget-value child))))
2389 (funcall set symbol (widget-value child))
2390 (put symbol 'variable-comment comment)
2391 (put symbol 'saved-variable-comment comment)))
2392 (put symbol 'customized-value nil)
2393 (put symbol 'customized-variable-comment nil)
2394 (custom-save-all)
2395 (custom-variable-state-set widget)
2396 (custom-redraw-magic widget)))
2397
2398 (defun custom-variable-reset-saved (widget)
2399 "Restore the saved value for the variable being edited by WIDGET."
2400 (let* ((symbol (widget-value widget))
2401 (set (or (get symbol 'custom-set) 'set-default))
2402 (comment-widget (widget-get widget :comment-widget))
2403 (value (get symbol 'saved-value))
2404 (comment (get symbol 'saved-variable-comment)))
2405 (cond ((or value comment)
2406 (put symbol 'variable-comment comment)
2407 (condition-case nil
2408 (funcall set symbol (eval (car value)))
2409 (error nil)))
2410 (t
2411 (error "No saved value for %s" symbol)))
2412 (put symbol 'customized-value nil)
2413 (put symbol 'customized-variable-comment nil)
2414 (widget-put widget :custom-state 'unknown)
2415 ;; This call will possibly make the comment invisible
2416 (custom-redraw widget)))
2417
2418 (defun custom-variable-reset-standard (widget)
2419 "Restore the standard setting for the variable being edited by WIDGET.
2420 This operation eliminates any saved setting for the variable,
2421 restoring it to the state of a variable that has never been customized."
2422 (let* ((symbol (widget-value widget))
2423 (set (or (get symbol 'custom-set) 'set-default))
2424 (comment-widget (widget-get widget :comment-widget)))
2425 (if (get symbol 'standard-value)
2426 (funcall set symbol (eval (car (get symbol 'standard-value))))
2427 (error "No standard setting known for %S" symbol))
2428 (put symbol 'variable-comment nil)
2429 (put symbol 'customized-value nil)
2430 (put symbol 'customized-variable-comment nil)
2431 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2432 (put symbol 'saved-value nil)
2433 (put symbol 'saved-variable-comment nil)
2434 (custom-save-all))
2435 (widget-put widget :custom-state 'unknown)
2436 ;; This call will possibly make the comment invisible
2437 (custom-redraw widget)))
2438
2439 ;;; The `custom-face-edit' Widget.
2440
2441 (define-widget 'custom-face-edit 'checklist
2442 "Edit face attributes."
2443 :format "%t: %v"
2444 :tag "Attributes"
2445 :extra-offset 12
2446 :button-args '(:help-echo "Control whether this attribute has any effect.")
2447 :args (mapcar (lambda (att)
2448 (list 'group
2449 :inline t
2450 :sibling-args (widget-get (nth 1 att) :sibling-args)
2451 (list 'const :format "" :value (nth 0 att))
2452 (nth 1 att)))
2453 custom-face-attributes))
2454
2455 ;;; The `custom-display' Widget.
2456
2457 (define-widget 'custom-display 'menu-choice
2458 "Select a display type."
2459 :tag "Display"
2460 :value t
2461 :help-echo "Specify frames where the face attributes should be used."
2462 :args '((const :tag "all" t)
2463 (checklist
2464 :offset 0
2465 :extra-offset 9
2466 :args ((group :sibling-args (:help-echo "\
2467 Only match the specified window systems.")
2468 (const :format "Type: "
2469 type)
2470 (checklist :inline t
2471 :offset 0
2472 (const :format "X "
2473 :sibling-args (:help-echo "\
2474 The X11 Window System.")
2475 x)
2476 (const :format "PM "
2477 :sibling-args (:help-echo "\
2478 OS/2 Presentation Manager.")
2479 pm)
2480 (const :format "W32 "
2481 :sibling-args (:help-echo "\
2482 Windows NT/9X.")
2483 w32)
2484 (const :format "DOS "
2485 :sibling-args (:help-echo "\
2486 Plain MS-DOS.")
2487 pc)
2488 (const :format "TTY%n"
2489 :sibling-args (:help-echo "\
2490 Plain text terminals.")
2491 tty)))
2492 (group :sibling-args (:help-echo "\
2493 Only match the frames with the specified color support.")
2494 (const :format "Class: "
2495 class)
2496 (checklist :inline t
2497 :offset 0
2498 (const :format "Color "
2499 :sibling-args (:help-echo "\
2500 Match color frames.")
2501 color)
2502 (const :format "Grayscale "
2503 :sibling-args (:help-echo "\
2504 Match grayscale frames.")
2505 grayscale)
2506 (const :format "Monochrome%n"
2507 :sibling-args (:help-echo "\
2508 Match frames with no color support.")
2509 mono)))
2510 (group :sibling-args (:help-echo "\
2511 Only match frames with the specified intensity.")
2512 (const :format "\
2513 Background brightness: "
2514 background)
2515 (checklist :inline t
2516 :offset 0
2517 (const :format "Light "
2518 :sibling-args (:help-echo "\
2519 Match frames with light backgrounds.")
2520 light)
2521 (const :format "Dark\n"
2522 :sibling-args (:help-echo "\
2523 Match frames with dark backgrounds.")
2524 dark)))))))
2525
2526 ;;; The `custom-face' Widget.
2527
2528 (defface custom-face-tag-face
2529 `((t (:bold t :family "helv"
2530 :height ,(let ((height (face-attribute 'default :height)))
2531 (if (numberp height)
2532 (floor height 0.9)
2533 height)))))
2534 "Face used for face tags."
2535 :group 'custom-faces)
2536
2537 (defcustom custom-face-default-form 'selected
2538 "Default form of displaying face definition."
2539 :type '(choice (const all)
2540 (const selected)
2541 (const lisp))
2542 :group 'custom-buffer
2543 :version "20.3")
2544
2545 (define-widget 'custom-face 'custom
2546 "Customize face."
2547 :sample-face 'custom-face-tag-face
2548 :help-echo "Set or reset this face."
2549 :documentation-property #'face-doc-string
2550 :value-create 'custom-face-value-create
2551 :action 'custom-face-action
2552 :custom-category 'face
2553 :custom-form nil ; defaults to value of `custom-face-default-form'
2554 :custom-set 'custom-face-set
2555 :custom-save 'custom-face-save
2556 :custom-reset-current 'custom-redraw
2557 :custom-reset-saved 'custom-face-reset-saved
2558 :custom-reset-standard 'custom-face-reset-standard
2559 :custom-menu 'custom-face-menu-create)
2560
2561 (define-widget 'custom-face-all 'editable-list
2562 "An editable list of display specifications and attributes."
2563 :entry-format "%i %d %v"
2564 :insert-button-args '(:help-echo "Insert new display specification here.")
2565 :append-button-args '(:help-echo "Append new display specification here.")
2566 :delete-button-args '(:help-echo "Delete this display specification.")
2567 :args '((group :format "%v" custom-display custom-face-edit)))
2568
2569 (defconst custom-face-all (widget-convert 'custom-face-all)
2570 "Converted version of the `custom-face-all' widget.")
2571
2572 (define-widget 'custom-display-unselected 'item
2573 "A display specification that doesn't match the selected display."
2574 :match 'custom-display-unselected-match)
2575
2576 (defun custom-display-unselected-match (widget value)
2577 "Non-nil if VALUE is an unselected display specification."
2578 (not (face-spec-set-match-display value (selected-frame))))
2579
2580 (define-widget 'custom-face-selected 'group
2581 "Edit the attributes of the selected display in a face specification."
2582 :args '((repeat :format ""
2583 :inline t
2584 (group custom-display-unselected sexp))
2585 (group (sexp :format "") custom-face-edit)
2586 (repeat :format ""
2587 :inline t
2588 sexp)))
2589
2590 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2591 "Converted version of the `custom-face-selected' widget.")
2592
2593 (defun custom-face-value-create (widget)
2594 "Create a list of the display specifications for WIDGET."
2595 (let ((buttons (widget-get widget :buttons))
2596 children
2597 (symbol (widget-get widget :value))
2598 (tag (widget-get widget :tag))
2599 (state (widget-get widget :custom-state))
2600 (begin (point))
2601 (is-last (widget-get widget :custom-last))
2602 (prefix (widget-get widget :custom-prefix)))
2603 (unless tag
2604 (setq tag (prin1-to-string symbol)))
2605 (cond ((eq custom-buffer-style 'tree)
2606 (insert prefix (if is-last " `--- " " |--- "))
2607 (push (widget-create-child-and-convert
2608 widget 'custom-browse-face-tag)
2609 buttons)
2610 (insert " " tag "\n")
2611 (widget-put widget :buttons buttons))
2612 (t
2613 ;; Create tag.
2614 (insert tag)
2615 (if (eq custom-buffer-style 'face)
2616 (insert " ")
2617 (widget-specify-sample widget begin (point))
2618 (insert ": "))
2619 ;; Sample.
2620 (push (widget-create-child-and-convert widget 'item
2621 :format "(%{%t%})"
2622 :sample-face symbol
2623 :tag "sample")
2624 buttons)
2625 ;; Visibility.
2626 (insert " ")
2627 (push (widget-create-child-and-convert
2628 widget 'visibility
2629 :help-echo "Hide or show this face."
2630 :action 'custom-toggle-parent
2631 (not (eq state 'hidden)))
2632 buttons)
2633 ;; Magic.
2634 (insert "\n")
2635 (let ((magic (widget-create-child-and-convert
2636 widget 'custom-magic nil)))
2637 (widget-put widget :custom-magic magic)
2638 (push magic buttons))
2639 ;; Update buttons.
2640 (widget-put widget :buttons buttons)
2641 ;; Insert documentation.
2642 (widget-default-format-handler widget ?h)
2643 ;; The comment field
2644 (unless (eq state 'hidden)
2645 (let* ((comment (get symbol 'face-comment))
2646 (comment-widget
2647 (widget-create-child-and-convert
2648 widget 'custom-comment
2649 :parent widget
2650 :value (or comment ""))))
2651 (widget-put widget :comment-widget comment-widget)
2652 (push comment-widget children)))
2653 ;; See also.
2654 (unless (eq state 'hidden)
2655 (when (eq (widget-get widget :custom-level) 1)
2656 (custom-add-parent-links widget))
2657 (custom-add-see-also widget))
2658 ;; Editor.
2659 (unless (eq (preceding-char) ?\n)
2660 (insert "\n"))
2661 (unless (eq state 'hidden)
2662 (message "Creating face editor...")
2663 (custom-load-widget widget)
2664 (unless (widget-get widget :custom-form)
2665 (widget-put widget :custom-form custom-face-default-form))
2666 (let* ((symbol (widget-value widget))
2667 (spec (or (get symbol 'saved-face)
2668 (get symbol 'face-defface-spec)
2669 ;; Attempt to construct it.
2670 (list (list t (custom-face-attributes-get
2671 symbol (selected-frame))))))
2672 (form (widget-get widget :custom-form))
2673 (indent (widget-get widget :indent))
2674 edit)
2675 ;; If the user has changed this face in some other way,
2676 ;; edit it as the user has specified it.
2677 (if (not (face-spec-match-p symbol spec (selected-frame)))
2678 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
2679 (setq edit (widget-create-child-and-convert
2680 widget
2681 (cond ((and (eq form 'selected)
2682 (widget-apply custom-face-selected
2683 :match spec))
2684 (when indent (insert-char ?\ indent))
2685 'custom-face-selected)
2686 ((and (not (eq form 'lisp))
2687 (widget-apply custom-face-all
2688 :match spec))
2689 'custom-face-all)
2690 (t
2691 (when indent (insert-char ?\ indent))
2692 'sexp))
2693 :value spec))
2694 (custom-face-state-set widget)
2695 (push edit children)
2696 (widget-put widget :children children))
2697 (message "Creating face editor...done"))))))
2698
2699 (defvar custom-face-menu
2700 '(("Set for Current Session" custom-face-set)
2701 ("Save for Future Sessions" custom-face-save-command)
2702 ("Reset to Saved" custom-face-reset-saved
2703 (lambda (widget)
2704 (or (get (widget-value widget) 'saved-face)
2705 (get (widget-value widget) 'saved-face-comment))))
2706 ("Erase Customization" custom-face-reset-standard
2707 (lambda (widget)
2708 (get (widget-value widget) 'face-defface-spec)))
2709 ("---" ignore ignore)
2710 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2711 ("---" ignore ignore)
2712 ("Show all display specs" custom-face-edit-all
2713 (lambda (widget)
2714 (not (eq (widget-get widget :custom-form) 'all))))
2715 ("Just current attributes" custom-face-edit-selected
2716 (lambda (widget)
2717 (not (eq (widget-get widget :custom-form) 'selected))))
2718 ("Show as Lisp expression" custom-face-edit-lisp
2719 (lambda (widget)
2720 (not (eq (widget-get widget :custom-form) 'lisp)))))
2721 "Alist of actions for the `custom-face' widget.
2722 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2723 the menu entry, ACTION is the function to call on the widget when the
2724 menu is selected, and FILTER is a predicate which takes a `custom-face'
2725 widget as an argument, and returns non-nil if ACTION is valid on that
2726 widget. If FILTER is nil, ACTION is always valid.")
2727
2728 (defun custom-face-edit-selected (widget)
2729 "Edit selected attributes of the value of WIDGET."
2730 (widget-put widget :custom-state 'unknown)
2731 (widget-put widget :custom-form 'selected)
2732 (custom-redraw widget))
2733
2734 (defun custom-face-edit-all (widget)
2735 "Edit all attributes of the value of WIDGET."
2736 (widget-put widget :custom-state 'unknown)
2737 (widget-put widget :custom-form 'all)
2738 (custom-redraw widget))
2739
2740 (defun custom-face-edit-lisp (widget)
2741 "Edit the Lisp representation of the value of WIDGET."
2742 (widget-put widget :custom-state 'unknown)
2743 (widget-put widget :custom-form 'lisp)
2744 (custom-redraw widget))
2745
2746 (defun custom-face-state-set (widget)
2747 "Set the state of WIDGET."
2748 (let* ((symbol (widget-value widget))
2749 (comment (get symbol 'face-comment))
2750 tmp temp)
2751 (widget-put widget :custom-state
2752 (cond ((progn
2753 (setq tmp (get symbol 'customized-face))
2754 (setq temp (get symbol 'customized-face-comment))
2755 (or tmp temp))
2756 (if (equal temp comment)
2757 'set
2758 'changed))
2759 ((progn
2760 (setq tmp (get symbol 'saved-face))
2761 (setq temp (get symbol 'saved-face-comment))
2762 (or tmp temp))
2763 (if (equal temp comment)
2764 'saved
2765 'changed))
2766 ((get symbol 'face-defface-spec)
2767 (if (equal comment nil)
2768 'standard
2769 'changed))
2770 (t
2771 'rogue)))))
2772
2773 (defun custom-face-action (widget &optional event)
2774 "Show the menu for `custom-face' WIDGET.
2775 Optional EVENT is the location for the menu."
2776 (if (eq (widget-get widget :custom-state) 'hidden)
2777 (custom-toggle-hide widget)
2778 (let* ((completion-ignore-case t)
2779 (symbol (widget-get widget :value))
2780 (answer (widget-choose (concat "Operation on "
2781 (custom-unlispify-tag-name symbol))
2782 (custom-menu-filter custom-face-menu
2783 widget)
2784 event)))
2785 (if answer
2786 (funcall answer widget)))))
2787
2788 (defun custom-face-set (widget)
2789 "Make the face attributes in WIDGET take effect."
2790 (let* ((symbol (widget-value widget))
2791 (child (car (widget-get widget :children)))
2792 (value (widget-value child))
2793 (comment-widget (widget-get widget :comment-widget))
2794 (comment (widget-value comment-widget)))
2795 (when (equal comment "")
2796 (setq comment nil)
2797 ;; Make the comment invisible by hand if it's empty
2798 (custom-comment-hide comment-widget))
2799 (put symbol 'customized-face value)
2800 (face-spec-set symbol value)
2801 (put symbol 'customized-face-comment comment)
2802 (put symbol 'face-comment comment)
2803 (custom-face-state-set widget)
2804 (custom-redraw-magic widget)))
2805
2806 (defun custom-face-save-command (widget)
2807 "Save in `.emacs' the face attributes in WIDGET."
2808 (custom-face-save widget)
2809 (custom-save-all))
2810
2811 (defun custom-face-save (widget)
2812 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
2813 (let* ((symbol (widget-value widget))
2814 (child (car (widget-get widget :children)))
2815 (value (widget-value child))
2816 (comment-widget (widget-get widget :comment-widget))
2817 (comment (widget-value comment-widget)))
2818 (when (equal comment "")
2819 (setq comment nil)
2820 ;; Make the comment invisible by hand if it's empty
2821 (custom-comment-hide comment-widget))
2822 (face-spec-set symbol value)
2823 (put symbol 'saved-face value)
2824 (put symbol 'customized-face nil)
2825 (put symbol 'face-comment comment)
2826 (put symbol 'customized-face-comment nil)
2827 (put symbol 'saved-face-comment comment)
2828 (custom-save-all)
2829 (custom-face-state-set widget)
2830 (custom-redraw-magic widget)))
2831
2832 (defun custom-face-reset-saved (widget)
2833 "Restore WIDGET to the face's default attributes."
2834 (let* ((symbol (widget-value widget))
2835 (child (car (widget-get widget :children)))
2836 (value (get symbol 'saved-face))
2837 (comment (get symbol 'saved-face-comment))
2838 (comment-widget (widget-get widget :comment-widget)))
2839 (unless (or value comment)
2840 (error "No saved value for this face"))
2841 (put symbol 'customized-face nil)
2842 (put symbol 'customized-face-comment nil)
2843 (face-spec-set symbol value)
2844 (put symbol 'face-comment comment)
2845 (widget-value-set child value)
2846 ;; This call manages the comment visibility
2847 (widget-value-set comment-widget (or comment ""))
2848 (custom-face-state-set widget)
2849 (custom-redraw-magic widget)))
2850
2851 (defun custom-face-reset-standard (widget)
2852 "Restore WIDGET to the face's standard settings.
2853 This operation eliminates any saved setting for the face,
2854 restoring it to the state of a face that has never been customized."
2855 (let* ((symbol (widget-value widget))
2856 (child (car (widget-get widget :children)))
2857 (value (get symbol 'face-defface-spec))
2858 (comment-widget (widget-get widget :comment-widget)))
2859 (unless value
2860 (error "No standard setting for this face"))
2861 (put symbol 'customized-face nil)
2862 (put symbol 'customized-face-comment nil)
2863 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
2864 (put symbol 'saved-face nil)
2865 (put symbol 'saved-face-comment nil)
2866 (custom-save-all))
2867 (face-spec-set symbol value)
2868 (put symbol 'face-comment nil)
2869 (widget-value-set child value)
2870 ;; This call manages the comment visibility
2871 (widget-value-set comment-widget "")
2872 (custom-face-state-set widget)
2873 (custom-redraw-magic widget)))
2874
2875 ;;; The `face' Widget.
2876
2877 (define-widget 'face 'default
2878 "Select and customize a face."
2879 :convert-widget 'widget-value-convert-widget
2880 :button-prefix 'widget-push-button-prefix
2881 :button-suffix 'widget-push-button-suffix
2882 :format "%t: %[select face%] %v"
2883 :tag "Face"
2884 :value 'default
2885 :value-create 'widget-face-value-create
2886 :value-delete 'widget-face-value-delete
2887 :value-get 'widget-value-value-get
2888 :validate 'widget-children-validate
2889 :action 'widget-face-action
2890 :match (lambda (widget value) (symbolp value)))
2891
2892 (defun widget-face-value-create (widget)
2893 "Create a `custom-face' child."
2894 (let* ((symbol (widget-value widget))
2895 (custom-buffer-style 'face)
2896 (child (widget-create-child-and-convert
2897 widget 'custom-face
2898 :custom-level nil
2899 :value symbol)))
2900 (custom-magic-reset child)
2901 (setq custom-options (cons child custom-options))
2902 (widget-put widget :children (list child))))
2903
2904 (defun widget-face-value-delete (widget)
2905 "Remove the child from the options."
2906 (let ((child (car (widget-get widget :children))))
2907 (setq custom-options (delq child custom-options))
2908 (widget-children-value-delete widget)))
2909
2910 (defvar face-history nil
2911 "History of entered face names.")
2912
2913 (defun widget-face-action (widget &optional event)
2914 "Prompt for a face."
2915 (let ((answer (completing-read "Face: "
2916 (mapcar (lambda (face)
2917 (list (symbol-name face)))
2918 (face-list))
2919 nil nil nil
2920 'face-history)))
2921 (unless (zerop (length answer))
2922 (widget-value-set widget (intern answer))
2923 (widget-apply widget :notify widget event)
2924 (widget-setup))))
2925
2926 ;;; The `hook' Widget.
2927
2928 (define-widget 'hook 'list
2929 "A emacs lisp hook"
2930 :value-to-internal (lambda (widget value)
2931 (if (and value (symbolp value))
2932 (list value)
2933 value))
2934 :match (lambda (widget value)
2935 (or (symbolp value)
2936 (widget-group-match widget value)))
2937 ;; Avoid adding undefined functions to the hook, especially for
2938 ;; things like `find-file-hook' or even more basic ones, to avoid
2939 ;; chaos.
2940 :set (lambda (symbol value)
2941 (dolist (elt value)
2942 (if (fboundp elt)
2943 (add-hook symbol elt))))
2944 :convert-widget 'custom-hook-convert-widget
2945 :tag "Hook")
2946
2947 (defun custom-hook-convert-widget (widget)
2948 ;; Handle `:options'.
2949 (let* ((options (widget-get widget :options))
2950 (other `(editable-list :inline t
2951 :entry-format "%i %d%v"
2952 (function :format " %v")))
2953 (args (if options
2954 (list `(checklist :inline t
2955 ,@(mapcar (lambda (entry)
2956 `(function-item ,entry))
2957 options))
2958 other)
2959 (list other))))
2960 (widget-put widget :args args)
2961 widget))
2962
2963 ;;; The `custom-group-link' Widget.
2964
2965 (define-widget 'custom-group-link 'link
2966 "Show parent in other window when activated."
2967 :help-echo "Create customization buffer for this group."
2968 :action 'custom-group-link-action)
2969
2970 (defun custom-group-link-action (widget &rest ignore)
2971 (customize-group (widget-value widget)))
2972
2973 ;;; The `custom-group' Widget.
2974
2975 (defcustom custom-group-tag-faces nil
2976 ;; In XEmacs, this ought to play games with font size.
2977 ;; Fixme: make it do so in Emacs.
2978 "Face used for group tags.
2979 The first member is used for level 1 groups, the second for level 2,
2980 and so forth. The remaining group tags are shown with
2981 `custom-group-tag-face'."
2982 :type '(repeat face)
2983 :group 'custom-faces)
2984
2985 (defface custom-group-tag-face-1
2986 `((((class color)
2987 (background dark))
2988 (:foreground "pink" :family "helv"
2989 :height ,(let ((height (face-attribute 'default :height)))
2990 (if (numberp height)
2991 (floor height 0.9)
2992 height))
2993 :bold t))
2994 (((class color)
2995 (background light))
2996 (:foreground "red" :bold t
2997 :height ,(let ((height (face-attribute 'default :height)))
2998 (if (numberp height)
2999 (floor height 0.9)
3000 height))))
3001 (t (:bold t)))
3002 "Face used for group tags."
3003 :group 'custom-faces)
3004
3005 (defface custom-group-tag-face
3006 `((((class color)
3007 (background dark))
3008 (:foreground "light blue" :bold t
3009 :height ,(let ((height (face-attribute 'default :height)))
3010 (if (numberp height)
3011 (floor height 0.9)
3012 height))))
3013 (((class color)
3014 (background light))
3015 (:foreground "blue" :bold t
3016 :height ,(let ((height (face-attribute 'default :height)))
3017 (if (numberp height)
3018 (floor height 0.9)
3019 height))))
3020 (t (:bold t)))
3021 "Face used for low level group tags."
3022 :group 'custom-faces)
3023
3024 (define-widget 'custom-group 'custom
3025 "Customize group."
3026 :format "%v"
3027 :sample-face-get 'custom-group-sample-face-get
3028 :documentation-property 'group-documentation
3029 :help-echo "Set or reset all members of this group."
3030 :value-create 'custom-group-value-create
3031 :action 'custom-group-action
3032 :custom-category 'group
3033 :custom-set 'custom-group-set
3034 :custom-save 'custom-group-save
3035 :custom-reset-current 'custom-group-reset-current
3036 :custom-reset-saved 'custom-group-reset-saved
3037 :custom-reset-standard 'custom-group-reset-standard
3038 :custom-menu 'custom-group-menu-create)
3039
3040 (defun custom-group-sample-face-get (widget)
3041 ;; Use :sample-face.
3042 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3043 'custom-group-tag-face))
3044
3045 (define-widget 'custom-group-visibility 'visibility
3046 "An indicator and manipulator for hidden group contents."
3047 :create 'custom-group-visibility-create)
3048
3049 (defun custom-group-visibility-create (widget)
3050 (let ((visible (widget-value widget)))
3051 (if visible
3052 (insert "--------")))
3053 (widget-default-create widget))
3054
3055 (defun custom-group-members (symbol groups-only)
3056 "Return SYMBOL's custom group members.
3057 If GROUPS-ONLY non-nil, return only those members that are groups."
3058 (if (not groups-only)
3059 (get symbol 'custom-group)
3060 (let (members)
3061 (dolist (entry (get symbol 'custom-group))
3062 (when (eq (nth 1 entry) 'custom-group)
3063 (push entry members)))
3064 (nreverse members))))
3065
3066 (defun custom-group-value-create (widget)
3067 "Insert a customize group for WIDGET in the current buffer."
3068 (let* ((state (widget-get widget :custom-state))
3069 (level (widget-get widget :custom-level))
3070 ;; (indent (widget-get widget :indent))
3071 (prefix (widget-get widget :custom-prefix))
3072 (buttons (widget-get widget :buttons))
3073 (tag (widget-get widget :tag))
3074 (symbol (widget-value widget))
3075 (members (custom-group-members symbol
3076 (and (eq custom-buffer-style 'tree)
3077 custom-browse-only-groups))))
3078 (cond ((and (eq custom-buffer-style 'tree)
3079 (eq state 'hidden)
3080 (or members (custom-unloaded-widget-p widget)))
3081 (custom-browse-insert-prefix prefix)
3082 (push (widget-create-child-and-convert
3083 widget 'custom-browse-visibility
3084 ;; :tag-glyph "plus"
3085 :tag "+")
3086 buttons)
3087 (insert "-- ")
3088 ;; (widget-glyph-insert nil "-- " "horizontal")
3089 (push (widget-create-child-and-convert
3090 widget 'custom-browse-group-tag)
3091 buttons)
3092 (insert " " tag "\n")
3093 (widget-put widget :buttons buttons))
3094 ((and (eq custom-buffer-style 'tree)
3095 (zerop (length members)))
3096 (custom-browse-insert-prefix prefix)
3097 (insert "[ ]-- ")
3098 ;; (widget-glyph-insert nil "[ ]" "empty")
3099 ;; (widget-glyph-insert nil "-- " "horizontal")
3100 (push (widget-create-child-and-convert
3101 widget 'custom-browse-group-tag)
3102 buttons)
3103 (insert " " tag "\n")
3104 (widget-put widget :buttons buttons))
3105 ((eq custom-buffer-style 'tree)
3106 (custom-browse-insert-prefix prefix)
3107 (custom-load-widget widget)
3108 (if (zerop (length members))
3109 (progn
3110 (custom-browse-insert-prefix prefix)
3111 (insert "[ ]-- ")
3112 ;; (widget-glyph-insert nil "[ ]" "empty")
3113 ;; (widget-glyph-insert nil "-- " "horizontal")
3114 (push (widget-create-child-and-convert
3115 widget 'custom-browse-group-tag)
3116 buttons)
3117 (insert " " tag "\n")
3118 (widget-put widget :buttons buttons))
3119 (push (widget-create-child-and-convert
3120 widget 'custom-browse-visibility
3121 ;; :tag-glyph "minus"
3122 :tag "-")
3123 buttons)
3124 (insert "-\\ ")
3125 ;; (widget-glyph-insert nil "-\\ " "top")
3126 (push (widget-create-child-and-convert
3127 widget 'custom-browse-group-tag)
3128 buttons)
3129 (insert " " tag "\n")
3130 (widget-put widget :buttons buttons)
3131 (message "Creating group...")
3132 (let* ((members (custom-sort-items members
3133 custom-browse-sort-alphabetically
3134 custom-browse-order-groups))
3135 (prefixes (widget-get widget :custom-prefixes))
3136 (custom-prefix-list (custom-prefix-add symbol prefixes))
3137 (extra-prefix (if (widget-get widget :custom-last)
3138 " "
3139 " | "))
3140 (prefix (concat prefix extra-prefix))
3141 children entry)
3142 (while members
3143 (setq entry (car members)
3144 members (cdr members))
3145 (push (widget-create-child-and-convert
3146 widget (nth 1 entry)
3147 :group widget
3148 :tag (custom-unlispify-tag-name (nth 0 entry))
3149 :custom-prefixes custom-prefix-list
3150 :custom-level (1+ level)
3151 :custom-last (null members)
3152 :value (nth 0 entry)
3153 :custom-prefix prefix)
3154 children))
3155 (widget-put widget :children (reverse children)))
3156 (message "Creating group...done")))
3157 ;; Nested style.
3158 ((eq state 'hidden)
3159 ;; Create level indicator.
3160 (unless (eq custom-buffer-style 'links)
3161 (insert-char ?\ (* custom-buffer-indent (1- level)))
3162 (insert "-- "))
3163 ;; Create tag.
3164 (let ((begin (point)))
3165 (insert tag)
3166 (widget-specify-sample widget begin (point)))
3167 (insert " group: ")
3168 ;; Create link/visibility indicator.
3169 (if (eq custom-buffer-style 'links)
3170 (push (widget-create-child-and-convert
3171 widget 'custom-group-link
3172 :tag "Go to Group"
3173 symbol)
3174 buttons)
3175 (push (widget-create-child-and-convert
3176 widget 'custom-group-visibility
3177 :help-echo "Show members of this group."
3178 :action 'custom-toggle-parent
3179 (not (eq state 'hidden)))
3180 buttons))
3181 (insert " \n")
3182 ;; Create magic button.
3183 (let ((magic (widget-create-child-and-convert
3184 widget 'custom-magic nil)))
3185 (widget-put widget :custom-magic magic)
3186 (push magic buttons))
3187 ;; Update buttons.
3188 (widget-put widget :buttons buttons)
3189 ;; Insert documentation.
3190 (if (and (eq custom-buffer-style 'links) (> level 1))
3191 (widget-put widget :documentation-indent 0))
3192 (widget-default-format-handler widget ?h))
3193 ;; Nested style.
3194 (t ;Visible.
3195 ;; Add parent groups references above the group.
3196 (if t ;;; This should test that the buffer
3197 ;;; was made to display a group.
3198 (when (eq level 1)
3199 (if (custom-add-parent-links widget
3200 "Go to parent group:")
3201 (insert "\n"))))
3202 ;; Create level indicator.
3203 (insert-char ?\ (* custom-buffer-indent (1- level)))
3204 (insert "/- ")
3205 ;; Create tag.
3206 (let ((start (point)))
3207 (insert tag)
3208 (widget-specify-sample widget start (point)))
3209 (insert " group: ")
3210 ;; Create visibility indicator.
3211 (unless (eq custom-buffer-style 'links)
3212 (insert "--------")
3213 (push (widget-create-child-and-convert
3214 widget 'visibility
3215 :help-echo "Hide members of this group."
3216 :action 'custom-toggle-parent
3217 (not (eq state 'hidden)))
3218 buttons)
3219 (insert " "))
3220 ;; Create more dashes.
3221 ;; Use 76 instead of 75 to compensate for the temporary "<"
3222 ;; added by `widget-insert'.
3223 (insert-char ?- (- 76 (current-column)
3224 (* custom-buffer-indent level)))
3225 (insert "\\\n")
3226 ;; Create magic button.
3227 (let ((magic (widget-create-child-and-convert
3228 widget 'custom-magic
3229 :indent 0
3230 nil)))
3231 (widget-put widget :custom-magic magic)
3232 (push magic buttons))
3233 ;; Update buttons.
3234 (widget-put widget :buttons buttons)
3235 ;; Insert documentation.
3236 (widget-default-format-handler widget ?h)
3237 ;; Parent groups.
3238 (if nil ;;; This should test that the buffer
3239 ;;; was not made to display a group.
3240 (when (eq level 1)
3241 (insert-char ?\ custom-buffer-indent)
3242 (custom-add-parent-links widget)))
3243 (custom-add-see-also widget
3244 (make-string (* custom-buffer-indent level)
3245 ?\ ))
3246 ;; Members.
3247 (message "Creating group...")
3248 (custom-load-widget widget)
3249 (let* ((members (custom-sort-items members
3250 custom-buffer-sort-alphabetically
3251 custom-buffer-order-groups))
3252 (prefixes (widget-get widget :custom-prefixes))
3253 (custom-prefix-list (custom-prefix-add symbol prefixes))
3254 (length (length members))
3255 (count 0)
3256 (children (mapcar (lambda (entry)
3257 (widget-insert "\n")
3258 (message "\
3259 Creating group members... %2d%%"
3260 (/ (* 100.0 count) length))
3261 (setq count (1+ count))
3262 (prog1
3263 (widget-create-child-and-convert
3264 widget (nth 1 entry)
3265 :group widget
3266 :tag (custom-unlispify-tag-name
3267 (nth 0 entry))
3268 :custom-prefixes custom-prefix-list
3269 :custom-level (1+ level)
3270 :value (nth 0 entry))
3271 (unless (eq (preceding-char) ?\n)
3272 (widget-insert "\n"))))
3273 members)))
3274 (message "Creating group magic...")
3275 (mapc 'custom-magic-reset children)
3276 (message "Creating group state...")
3277 (widget-put widget :children children)
3278 (custom-group-state-update widget)
3279 (message "Creating group... done"))
3280 ;; End line
3281 (insert "\n")
3282 (insert-char ?\ (* custom-buffer-indent (1- level)))
3283 (insert "\\- " (widget-get widget :tag) " group end ")
3284 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
3285 (insert "/\n")))))
3286
3287 (defvar custom-group-menu
3288 '(("Set for Current Session" custom-group-set
3289 (lambda (widget)
3290 (eq (widget-get widget :custom-state) 'modified)))
3291 ("Save for Future Sessions" custom-group-save
3292 (lambda (widget)
3293 (memq (widget-get widget :custom-state) '(modified set))))
3294 ("Reset to Current" custom-group-reset-current
3295 (lambda (widget)
3296 (memq (widget-get widget :custom-state) '(modified))))
3297 ("Reset to Saved" custom-group-reset-saved
3298 (lambda (widget)
3299 (memq (widget-get widget :custom-state) '(modified set))))
3300 ("Reset to standard setting" custom-group-reset-standard
3301 (lambda (widget)
3302 (memq (widget-get widget :custom-state) '(modified set saved)))))
3303 "Alist of actions for the `custom-group' widget.
3304 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3305 the menu entry, ACTION is the function to call on the widget when the
3306 menu is selected, and FILTER is a predicate which takes a `custom-group'
3307 widget as an argument, and returns non-nil if ACTION is valid on that
3308 widget. If FILTER is nil, ACTION is always valid.")
3309
3310 (defun custom-group-action (widget &optional event)
3311 "Show the menu for `custom-group' WIDGET.
3312 Optional EVENT is the location for the menu."
3313 (if (eq (widget-get widget :custom-state) 'hidden)
3314 (custom-toggle-hide widget)
3315 (let* ((completion-ignore-case t)
3316 (answer (widget-choose (concat "Operation on "
3317 (custom-unlispify-tag-name
3318 (widget-get widget :value)))
3319 (custom-menu-filter custom-group-menu
3320 widget)
3321 event)))
3322 (if answer
3323 (funcall answer widget)))))
3324
3325 (defun custom-group-set (widget)
3326 "Set changes in all modified group members."
3327 (let ((children (widget-get widget :children)))
3328 (mapc (lambda (child)
3329 (when (eq (widget-get child :custom-state) 'modified)
3330 (widget-apply child :custom-set)))
3331 children )))
3332
3333 (defun custom-group-save (widget)
3334 "Save all modified group members."
3335 (let ((children (widget-get widget :children)))
3336 (mapc (lambda (child)
3337 (when (memq (widget-get child :custom-state) '(modified set))
3338 (widget-apply child :custom-save)))
3339 children )))
3340
3341 (defun custom-group-reset-current (widget)
3342 "Reset all modified group members."
3343 (let ((children (widget-get widget :children)))
3344 (mapc (lambda (child)
3345 (when (eq (widget-get child :custom-state) 'modified)
3346 (widget-apply child :custom-reset-current)))
3347 children )))
3348
3349 (defun custom-group-reset-saved (widget)
3350 "Reset all modified or set group members."
3351 (let ((children (widget-get widget :children)))
3352 (mapc (lambda (child)
3353 (when (memq (widget-get child :custom-state) '(modified set))
3354 (widget-apply child :custom-reset-saved)))
3355 children )))
3356
3357 (defun custom-group-reset-standard (widget)
3358 "Reset all modified, set, or saved group members."
3359 (let ((children (widget-get widget :children)))
3360 (mapc (lambda (child)
3361 (when (memq (widget-get child :custom-state)
3362 '(modified set saved))
3363 (widget-apply child :custom-reset-standard)))
3364 children )))
3365
3366 (defun custom-group-state-update (widget)
3367 "Update magic."
3368 (unless (eq (widget-get widget :custom-state) 'hidden)
3369 (let* ((children (widget-get widget :children))
3370 (states (mapcar (lambda (child)
3371 (widget-get child :custom-state))
3372 children))
3373 (magics custom-magic-alist)
3374 (found 'standard))
3375 (while magics
3376 (let ((magic (car (car magics))))
3377 (if (and (not (eq magic 'hidden))
3378 (memq magic states))
3379 (setq found magic
3380 magics nil)
3381 (setq magics (cdr magics)))))
3382 (widget-put widget :custom-state found)))
3383 (custom-magic-reset widget))
3384
3385 ;;; The `custom-save-all' Function.
3386 ;;;###autoload
3387 (defcustom custom-file nil
3388 "File used for storing customization information.
3389 The default is nil, which means to use your init file
3390 as specified by `user-init-file'. If you specify some other file,
3391 you need to explicitly load that file for the settings to take effect.
3392
3393 When you change this variable, look in the previous custom file
3394 \(usually your init file) for the forms `(custom-set-variables ...)'
3395 and `(custom-set-faces ...)', and copy them (whichever ones you find)
3396 to the new custom file. This will preserve your existing customizations."
3397 :type '(choice (const :tag "Your Emacs init file" nil) file)
3398 :group 'customize)
3399
3400 (defun custom-file ()
3401 "Return the file name for saving customizations."
3402 (setq custom-file
3403 (or custom-file
3404 user-init-file
3405 (read-file-name "File for customizations: "
3406 "~/" nil nil ".emacs"))))
3407
3408 (defun custom-save-delete (symbol)
3409 "Visit `custom-file' and delete all calls to SYMBOL from it.
3410 Leave point at the old location of the first such call,
3411 or (if there were none) at the end of the buffer."
3412 (let ((default-major-mode))
3413 (set-buffer (find-file-noselect (custom-file))))
3414 (goto-char (point-min))
3415 ;; Skip all whitespace and comments.
3416 (while (forward-comment 1))
3417 (or (eobp)
3418 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
3419 (let (first)
3420 (catch 'found
3421 (while t ;; We exit this loop only via throw.
3422 ;; Skip all whitespace and comments.
3423 (while (forward-comment 1))
3424 (let ((start (point))
3425 (sexp (condition-case nil
3426 (read (current-buffer))
3427 (end-of-file (throw 'found nil)))))
3428 (when (and (listp sexp)
3429 (eq (car sexp) symbol))
3430 (delete-region start (point))
3431 (unless first
3432 (setq first (point)))))))
3433 (if first
3434 (goto-char first)
3435 (goto-char (point-max)))))
3436
3437 (defun custom-save-variables ()
3438 "Save all customized variables in `custom-file'."
3439 (save-excursion
3440 (custom-save-delete 'custom-set-variables)
3441 (let ((standard-output (current-buffer))
3442 (saved-list (make-list 1 0))
3443 sort-fold-case)
3444 ;; First create a sorted list of saved variables.
3445 (mapatoms
3446 (lambda (symbol)
3447 (if (get symbol 'saved-value)
3448 (nconc saved-list (list symbol)))))
3449 (setq saved-list (sort (cdr saved-list) 'string<))
3450 (unless (bolp)
3451 (princ "\n"))
3452 (princ "(custom-set-variables
3453 ;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
3454 ;; Your init file must only contain one such instance.\n")
3455 (mapcar
3456 (lambda (symbol)
3457 (let ((value (get symbol 'saved-value))
3458 (requests (get symbol 'custom-requests))
3459 (now (not (or (get symbol 'standard-value)
3460 (and (not (boundp symbol))
3461 (not (get symbol 'force-value))))))
3462 (comment (get symbol 'saved-variable-comment))
3463 sep)
3464 (when (or value comment)
3465 (unless (bolp)
3466 (princ "\n"))
3467 (princ " '(")
3468 (prin1 symbol)
3469 (princ " ")
3470 (prin1 (car value))
3471 (cond ((or now requests comment)
3472 (princ " ")
3473 (if now
3474 (princ "t")
3475 (princ "nil"))
3476 (cond ((or requests comment)
3477 (princ " ")
3478 (if requests
3479 (prin1 requests)
3480 (princ "nil"))
3481 (cond (comment
3482 (princ " ")
3483 (prin1 comment)
3484 (princ ")"))
3485 (t
3486 (princ ")"))))
3487 (t
3488 (princ ")"))))
3489 (t
3490 (princ ")"))))))
3491 saved-list)
3492 (if (bolp)
3493 (princ " "))
3494 (princ ")")
3495 (unless (looking-at "\n")
3496 (princ "\n")))))
3497
3498 (defun custom-save-faces ()
3499 "Save all customized faces in `custom-file'."
3500 (save-excursion
3501 (custom-save-delete 'custom-set-faces)
3502 (let ((standard-output (current-buffer))
3503 (saved-list (make-list 1 0))
3504 sort-fold-case)
3505 ;; First create a sorted list of saved faces.
3506 (mapatoms
3507 (lambda (symbol)
3508 (if (get symbol 'saved-face)
3509 (nconc saved-list (list symbol)))))
3510 (setq saved-list (sort (cdr saved-list) 'string<))
3511 ;; The default face must be first, since it affects the others.
3512 (if (memq 'default saved-list)
3513 (setq saved-list (cons 'default (delq 'default saved-list))))
3514 (unless (bolp)
3515 (princ "\n"))
3516 (princ "(custom-set-faces
3517 ;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
3518 ;; Your init file must only contain one such instance.\n")
3519 (mapcar
3520 (lambda (symbol)
3521 (let ((value (get symbol 'saved-face))
3522 (now (not (or (get 'default 'face-defface-spec)
3523 (and (not (custom-facep 'default))
3524 (not (get 'default 'force-face))))))
3525 (comment (get 'default 'saved-face-comment)))
3526 (unless (eq symbol 'default))
3527 ;; Don't print default face here.
3528 (unless (bolp)
3529 (princ "\n"))
3530 (princ " '(")
3531 (prin1 symbol)
3532 (princ " ")
3533 (prin1 value)
3534 (cond ((or now comment)
3535 (princ " ")
3536 (if now
3537 (princ "t")
3538 (princ "nil"))
3539 (cond (comment
3540 (princ " ")
3541 (prin1 comment)
3542 (princ ")"))
3543 (t
3544 (princ ")"))))
3545 (t
3546 (princ ")")))))
3547 saved-list)
3548 (if (bolp)
3549 (princ " "))
3550 (princ ")")
3551 (unless (looking-at "\n")
3552 (princ "\n")))))
3553
3554 ;;;###autoload
3555 (defun customize-save-customized ()
3556 "Save all user options which have been set in this session."
3557 (interactive)
3558 (mapatoms (lambda (symbol)
3559 (let ((face (get symbol 'customized-face))
3560 (value (get symbol 'customized-value))
3561 (face-comment (get symbol 'customized-face-comment))
3562 (variable-comment
3563 (get symbol 'customized-variable-comment)))
3564 (when face
3565 (put symbol 'saved-face face)
3566 (put symbol 'customized-face nil))
3567 (when value
3568 (put symbol 'saved-value value)
3569 (put symbol 'customized-value nil))
3570 (when variable-comment
3571 (put symbol 'saved-variable-comment variable-comment)
3572 (put symbol 'customized-variable-comment nil))
3573 (when face-comment
3574 (put symbol 'saved-face-comment face-comment)
3575 (put symbol 'customized-face-comment nil)))))
3576 ;; We really should update all custom buffers here.
3577 (custom-save-all))
3578
3579 ;;;###autoload
3580 (defun custom-save-all ()
3581 "Save all customizations in `custom-file'."
3582 (let ((inhibit-read-only t))
3583 (custom-save-variables)
3584 (custom-save-faces)
3585 (save-excursion
3586 (let ((default-major-mode nil))
3587 (set-buffer (find-file-noselect (custom-file))))
3588 (save-buffer))))
3589
3590 ;;; The Customize Menu.
3591
3592 ;;; Menu support
3593
3594 (defcustom custom-menu-nesting 2
3595 "Maximum nesting in custom menus."
3596 :type 'integer
3597 :group 'custom-menu)
3598
3599 (defun custom-face-menu-create (widget symbol)
3600 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3601 (vector (custom-unlispify-menu-entry symbol)
3602 `(customize-face ',symbol)
3603 t))
3604
3605 (defun custom-variable-menu-create (widget symbol)
3606 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3607 (let ((type (get symbol 'custom-type)))
3608 (unless (listp type)
3609 (setq type (list type)))
3610 (if (and type (widget-get type :custom-menu))
3611 (widget-apply type :custom-menu symbol)
3612 (vector (custom-unlispify-menu-entry symbol)
3613 `(customize-variable ',symbol)
3614 t))))
3615
3616 ;; Add checkboxes to boolean variable entries.
3617 (widget-put (get 'boolean 'widget-type)
3618 :custom-menu (lambda (widget symbol)
3619 (vector (custom-unlispify-menu-entry symbol)
3620 `(customize-variable ',symbol)
3621 ':style 'toggle
3622 ':selected symbol)))
3623
3624 ;; Fixme: sort out use of :filter in Emacs 21.
3625 (if nil ; (string-match "XEmacs" emacs-version)
3626 ;; XEmacs can create menus dynamically.
3627 (defun custom-group-menu-create (widget symbol)
3628 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3629 `( ,(custom-unlispify-menu-entry symbol t)
3630 :filter (lambda (&rest junk)
3631 (cdr (custom-menu-create ',symbol)))))
3632 ;; But emacs can't.
3633 (defun custom-group-menu-create (widget symbol)
3634 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3635 ;; Limit the nesting.
3636 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3637 (custom-menu-create symbol))))
3638
3639 ;;;###autoload
3640 (defun custom-menu-create (symbol)
3641 "Create menu for customization group SYMBOL.
3642 The menu is in a format applicable to `easy-menu-define'."
3643 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3644 `(customize-group ',symbol)
3645 t)))
3646 (if (and (or (not (boundp 'custom-menu-nesting))
3647 (>= custom-menu-nesting 0))
3648 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3649 (let ((custom-prefix-list (custom-prefix-add symbol
3650 custom-prefix-list))
3651 (members (custom-sort-items (get symbol 'custom-group)
3652 custom-menu-sort-alphabetically
3653 custom-menu-order-groups)))
3654 (custom-load-symbol symbol)
3655 `(,(custom-unlispify-menu-entry symbol t)
3656 ,item
3657 "--"
3658 ,@(mapcar (lambda (entry)
3659 (widget-apply (if (listp (nth 1 entry))
3660 (nth 1 entry)
3661 (list (nth 1 entry)))
3662 :custom-menu (nth 0 entry)))
3663 members)))
3664 item)))
3665
3666 ;;;###autoload
3667 (defun customize-menu-create (symbol &optional name)
3668 "Return a customize menu for customization group SYMBOL.
3669 If optional NAME is given, use that as the name of the menu.
3670 Otherwise the menu will be named `Customize'.
3671 The format is suitable for use with `easy-menu-define'."
3672 (unless name
3673 (setq name "Customize"))
3674 ;; Fixme: sort out use of :filter in Emacs 21.
3675 (if nil ;(string-match "XEmacs" emacs-version)
3676 ;; We can delay it under XEmacs.
3677 `(,name
3678 :filter (lambda (&rest junk)
3679 (cdr (custom-menu-create ',symbol))))
3680 ;; But we must create it now under Emacs.
3681 (cons name (cdr (custom-menu-create symbol)))))
3682
3683 ;;; The Custom Mode.
3684
3685 (defvar custom-mode-map nil
3686 "Keymap for `custom-mode'.")
3687
3688 (unless custom-mode-map
3689 (setq custom-mode-map (make-sparse-keymap))
3690 (set-keymap-parent custom-mode-map widget-keymap)
3691 (suppress-keymap custom-mode-map)
3692 (define-key custom-mode-map " " 'scroll-up)
3693 (define-key custom-mode-map "\177" 'scroll-down)
3694 (define-key custom-mode-map "q" 'Custom-buffer-done)
3695 (define-key custom-mode-map "u" 'Custom-goto-parent)
3696 (define-key custom-mode-map "n" 'widget-forward)
3697 (define-key custom-mode-map "p" 'widget-backward)
3698 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3699
3700 (defun Custom-move-and-invoke (event)
3701 "Move to where you click, and if it is an active field, invoke it."
3702 (interactive "e")
3703 (mouse-set-point event)
3704 (if (widget-event-point event)
3705 (let* ((pos (widget-event-point event))
3706 (button (get-char-property pos 'button)))
3707 (if button
3708 (widget-button-click event)))))
3709
3710 (easy-menu-define Custom-mode-menu
3711 custom-mode-map
3712 "Menu used in customization buffers."
3713 `("Custom"
3714 ,(customize-menu-create 'customize)
3715 ["Set" Custom-set t]
3716 ["Save" Custom-save t]
3717 ["Reset to Current" Custom-reset-current t]
3718 ["Reset to Saved" Custom-reset-saved t]
3719 ["Reset to Standard Settings" Custom-reset-standard t]
3720 ["Info" (Info-goto-node "(emacs)Easy Customization") t]))
3721
3722 (defun Custom-goto-parent ()
3723 "Go to the parent group listed at the top of this buffer.
3724 If several parents are listed, go to the first of them."
3725 (interactive)
3726 (save-excursion
3727 (goto-char (point-min))
3728 (if (search-forward "\nGo to parent group: " nil t)
3729 (let* ((button (get-char-property (point) 'button))
3730 (parent (downcase (widget-get button :tag))))
3731 (customize-group parent)))))
3732
3733 (defcustom custom-mode-hook nil
3734 "Hook called when entering Custom mode."
3735 :type 'hook
3736 :group 'custom-buffer )
3737
3738 (defun custom-state-buffer-message (widget)
3739 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3740 (message "To install your edits, invoke [State] and choose the Set operation")))
3741
3742 (defun custom-mode ()
3743 "Major mode for editing customization buffers.
3744
3745 The following commands are available:
3746
3747 Move to next button or editable field. \\[widget-forward]
3748 Move to previous button or editable field. \\[widget-backward]
3749 \\<widget-field-keymap>\
3750 Complete content of editable text field. \\[widget-complete]
3751 \\<custom-mode-map>\
3752 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3753 Invoke button under point. \\[widget-button-press]
3754 Set all modifications. \\[Custom-set]
3755 Make all modifications default. \\[Custom-save]
3756 Reset all modified options. \\[Custom-reset-current]
3757 Reset all modified or set options. \\[Custom-reset-saved]
3758 Reset all options. \\[Custom-reset-standard]
3759
3760 Entry to this mode calls the value of `custom-mode-hook'
3761 if that value is non-nil."
3762 (kill-all-local-variables)
3763 (setq major-mode 'custom-mode
3764 mode-name "Custom")
3765 (use-local-map custom-mode-map)
3766 (easy-menu-add Custom-mode-menu)
3767 (make-local-variable 'custom-options)
3768 (make-local-variable 'widget-documentation-face)
3769 (setq widget-documentation-face 'custom-documentation-face)
3770 (make-local-variable 'widget-button-face)
3771 (setq widget-button-face 'custom-button-face)
3772 (set (make-local-variable 'widget-button-pressed-face)
3773 'custom-button-pressed-face)
3774 (set (make-local-variable 'widget-mouse-face)
3775 'custom-button-pressed-face) ; buttons `depress' when moused
3776 ;; When possible, use relief for buttons, not bracketing. This test
3777 ;; may not be optimal.
3778 (when custom-raised-buttons
3779 (set (make-local-variable 'widget-push-button-prefix) "")
3780 (set (make-local-variable 'widget-push-button-suffix) "")
3781 (set (make-local-variable 'widget-link-prefix) "")
3782 (set (make-local-variable 'widget-link-suffix) ""))
3783 (make-local-hook 'widget-edit-functions)
3784 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3785 (run-hooks 'custom-mode-hook))
3786
3787 (add-to-list
3788 'debug-ignored-errors
3789 "^No user options have changed defaults in recent Emacs versions$")
3790
3791 ;;; The End.
3792
3793 (provide 'cus-edit)
3794
3795 ;;; cus-edit.el ends here