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