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