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