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