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