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