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