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