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