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