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