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