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