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