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