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