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