]> code.delx.au - gnu-emacs/blob - lisp/cus-edit.el
(custom-comment): Change widget definition.
[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 (defcustom custom-buffer-done-function 'bury-buffer
1157 "*Function called to remove a Custom buffer when the user is done with it.
1158 Called with one argument, the buffer to remove."
1159 :type '(choice (function-item bury-buffer)
1160 (function-item kill-buffer)
1161 (function :tag "Other"))
1162 :version "21.1"
1163 :group 'custom-buffer)
1164
1165 (defcustom custom-buffer-indent 3
1166 "Number of spaces to indent nested groups."
1167 :type 'integer
1168 :group 'custom-buffer)
1169
1170 ;;;###autoload
1171 (defun custom-buffer-create (options &optional name description)
1172 "Create a buffer containing OPTIONS.
1173 Optional NAME is the name of the buffer.
1174 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1175 SYMBOL is a customization option, and WIDGET is a widget for editing
1176 that option."
1177 (unless name (setq name "*Customization*"))
1178 (kill-buffer (get-buffer-create name))
1179 (pop-to-buffer (get-buffer-create name))
1180 (custom-buffer-create-internal options description))
1181
1182 ;;;###autoload
1183 (defun custom-buffer-create-other-window (options &optional name description)
1184 "Create a buffer containing OPTIONS.
1185 Optional NAME is the name of the buffer.
1186 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1187 SYMBOL is a customization option, and WIDGET is a widget for editing
1188 that option."
1189 (unless name (setq name "*Customization*"))
1190 (kill-buffer (get-buffer-create name))
1191 (let ((window (selected-window))
1192 (pop-up-windows t)
1193 (special-display-buffer-names nil)
1194 (special-display-regexps nil)
1195 (same-window-buffer-names nil)
1196 (same-window-regexps nil))
1197 (pop-to-buffer (get-buffer-create name))
1198 (custom-buffer-create-internal options description)
1199 (select-window window)))
1200
1201 (defcustom custom-reset-button-menu nil
1202 "If non-nil, only show a single reset button in customize buffers.
1203 This button will have a menu with all three reset operations."
1204 :type 'boolean
1205 :group 'custom-buffer)
1206
1207 (defun Custom-buffer-done (&rest ignore)
1208 "Remove current buffer by calling `custom-buffer-done-function'."
1209 (interactive)
1210 (funcall custom-buffer-done-function (current-buffer)))
1211
1212 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1213 '(("unspecified" . unspecified))))
1214 "If non-nil, indicate active buttons in a `raised-button' style.
1215 Otherwise use brackets."
1216 :type 'boolean
1217 :version "21.1"
1218 :group 'custom-buffer)
1219
1220 (defun custom-buffer-create-internal (options &optional description)
1221 (message "Creating customization buffer...")
1222 (custom-mode)
1223 (widget-insert "This is a customization buffer")
1224 (if description
1225 (widget-insert description))
1226 (widget-insert (format ".
1227 %s show active fields; type RET or click mouse-1
1228 on an active field to invoke its action. Editing an option value
1229 changes the text in the buffer; invoke the State button and
1230 choose the Set operation to set the option value.
1231 Invoke " (if custom-raised-buttons
1232 "`Raised' buttons"
1233 "Square brackets")))
1234 (widget-create 'info-link
1235 :tag "Help"
1236 :help-echo "Read the online help."
1237 "(emacs)Easy Customization")
1238 (widget-insert " for more information.\n\n")
1239 (message "Creating customization buttons...")
1240 (widget-insert "Operate on everything in this buffer:\n ")
1241 (widget-create 'push-button
1242 :tag "Set for Current Session"
1243 :help-echo "\
1244 Make your editing in this buffer take effect for this session."
1245 :action (lambda (widget &optional event)
1246 (Custom-set)))
1247 (widget-insert " ")
1248 (widget-create 'push-button
1249 :tag "Save for Future Sessions"
1250 :help-echo "\
1251 Make your editing in this buffer take effect for future Emacs sessions."
1252 :action (lambda (widget &optional event)
1253 (Custom-save)))
1254 (if custom-reset-button-menu
1255 (progn
1256 (widget-insert " ")
1257 (widget-create 'push-button
1258 :tag "Reset"
1259 :help-echo "Show a menu with reset operations."
1260 :mouse-down-action (lambda (&rest junk) t)
1261 :action (lambda (widget &optional event)
1262 (custom-reset event))))
1263 (widget-insert "\n ")
1264 (widget-create 'push-button
1265 :tag "Reset"
1266 :help-echo "\
1267 Reset all edited text in this buffer to reflect current values."
1268 :action 'Custom-reset-current)
1269 (widget-insert " ")
1270 (widget-create 'push-button
1271 :tag "Reset to Saved"
1272 :help-echo "\
1273 Reset all values in this buffer to their saved settings."
1274 :action 'Custom-reset-saved)
1275 (widget-insert " ")
1276 (widget-create 'push-button
1277 :tag "Reset to Standard"
1278 :help-echo "\
1279 Reset all values in this buffer to their standard settings."
1280 :action 'Custom-reset-standard))
1281 (widget-insert " ")
1282 (widget-create 'push-button
1283 :tag "Finish"
1284 :help-echo "Bury or kill the buffer."
1285 :action #'Custom-buffer-done)
1286 (widget-insert "\n\n")
1287 (message "Creating customization items...")
1288 (setq custom-options
1289 (if (= (length options) 1)
1290 (mapcar (lambda (entry)
1291 (widget-create (nth 1 entry)
1292 :documentation-shown t
1293 :custom-state 'unknown
1294 :tag (custom-unlispify-tag-name
1295 (nth 0 entry))
1296 :value (nth 0 entry)))
1297 options)
1298 (let ((count 0)
1299 (length (length options)))
1300 (mapcar (lambda (entry)
1301 (prog2
1302 (message "Creating customization items ...%2d%%"
1303 (/ (* 100.0 count) length))
1304 (widget-create (nth 1 entry)
1305 :tag (custom-unlispify-tag-name
1306 (nth 0 entry))
1307 :value (nth 0 entry))
1308 (setq count (1+ count))
1309 (unless (eq (preceding-char) ?\n)
1310 (widget-insert "\n"))
1311 (widget-insert "\n")))
1312 options))))
1313 (unless (eq (preceding-char) ?\n)
1314 (widget-insert "\n"))
1315 (message "Creating customization items ...%2d%%done" 100)
1316 (unless (eq custom-buffer-style 'tree)
1317 (mapcar 'custom-magic-reset custom-options))
1318 (message "Creating customization setup...")
1319 (widget-setup)
1320 (goto-char (point-min))
1321 (message "Creating customization buffer...done"))
1322
1323 ;;; The Tree Browser.
1324
1325 ;;;###autoload
1326 (defun customize-browse (&optional group)
1327 "Create a tree browser for the customize hierarchy."
1328 (interactive)
1329 (unless group
1330 (setq group 'emacs))
1331 (let ((name "*Customize Browser*"))
1332 (kill-buffer (get-buffer-create name))
1333 (pop-to-buffer (get-buffer-create name)))
1334 (custom-mode)
1335 (widget-insert "\
1336 Square brackets show active fields; type RET or click mouse-1
1337 on an active field to invoke its action.
1338 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1339 (if custom-browse-only-groups
1340 (widget-insert "\
1341 Invoke the [Group] button below to edit that item in another window.\n\n")
1342 (widget-insert "Invoke the ")
1343 (widget-create 'item
1344 :format "%t"
1345 :tag "[Group]"
1346 :tag-glyph "folder")
1347 (widget-insert ", ")
1348 (widget-create 'item
1349 :format "%t"
1350 :tag "[Face]"
1351 :tag-glyph "face")
1352 (widget-insert ", and ")
1353 (widget-create 'item
1354 :format "%t"
1355 :tag "[Option]"
1356 :tag-glyph "option")
1357 (widget-insert " buttons below to edit that
1358 item in another window.\n\n"))
1359 (let ((custom-buffer-style 'tree))
1360 (widget-create 'custom-group
1361 :custom-last t
1362 :custom-state 'unknown
1363 :tag (custom-unlispify-tag-name group)
1364 :value group))
1365 (goto-char (point-min)))
1366
1367 (define-widget 'custom-browse-visibility 'item
1368 "Control visibility of items in the customize tree browser."
1369 :format "%[[%t]%]"
1370 :action 'custom-browse-visibility-action)
1371
1372 (defun custom-browse-visibility-action (widget &rest ignore)
1373 (let ((custom-buffer-style 'tree))
1374 (custom-toggle-parent widget)))
1375
1376 (define-widget 'custom-browse-group-tag 'push-button
1377 "Show parent in other window when activated."
1378 :tag "Group"
1379 :tag-glyph "folder"
1380 :action 'custom-browse-group-tag-action)
1381
1382 (defun custom-browse-group-tag-action (widget &rest ignore)
1383 (let ((parent (widget-get widget :parent)))
1384 (customize-group-other-window (widget-value parent))))
1385
1386 (define-widget 'custom-browse-variable-tag 'push-button
1387 "Show parent in other window when activated."
1388 :tag "Option"
1389 :tag-glyph "option"
1390 :action 'custom-browse-variable-tag-action)
1391
1392 (defun custom-browse-variable-tag-action (widget &rest ignore)
1393 (let ((parent (widget-get widget :parent)))
1394 (customize-variable-other-window (widget-value parent))))
1395
1396 (define-widget 'custom-browse-face-tag 'push-button
1397 "Show parent in other window when activated."
1398 :tag "Face"
1399 :tag-glyph "face"
1400 :action 'custom-browse-face-tag-action)
1401
1402 (defun custom-browse-face-tag-action (widget &rest ignore)
1403 (let ((parent (widget-get widget :parent)))
1404 (customize-face-other-window (widget-value parent))))
1405
1406 (defconst custom-browse-alist '((" " "space")
1407 (" | " "vertical")
1408 ("-\\ " "top")
1409 (" |-" "middle")
1410 (" `-" "bottom")))
1411
1412 (defun custom-browse-insert-prefix (prefix)
1413 "Insert PREFIX. On XEmacs convert it to line graphics."
1414 ;; Fixme: do graphics.
1415 (if nil ; (string-match "XEmacs" emacs-version)
1416 (progn
1417 (insert "*")
1418 (while (not (string-equal prefix ""))
1419 (let ((entry (substring prefix 0 3)))
1420 (setq prefix (substring prefix 3))
1421 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1422 (name (nth 1 (assoc entry custom-browse-alist))))
1423 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1424 (overlay-put overlay 'start-open t)
1425 (overlay-put overlay 'end-open t)))))
1426 (insert prefix)))
1427
1428 ;;; Modification of Basic Widgets.
1429 ;;
1430 ;; We add extra properties to the basic widgets needed here. This is
1431 ;; fine, as long as we are careful to stay within out own namespace.
1432 ;;
1433 ;; We want simple widgets to be displayed by default, but complex
1434 ;; widgets to be hidden.
1435
1436 (widget-put (get 'item 'widget-type) :custom-show t)
1437 (widget-put (get 'editable-field 'widget-type)
1438 :custom-show (lambda (widget value)
1439 (let ((pp (pp-to-string value)))
1440 (cond ((string-match "\n" pp)
1441 nil)
1442 ((> (length pp) 40)
1443 nil)
1444 (t t)))))
1445 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1446
1447 ;;; The `custom-manual' Widget.
1448
1449 (define-widget 'custom-manual 'info-link
1450 "Link to the manual entry for this customization option."
1451 :help-echo "Read the manual entry for this option."
1452 :tag "Manual")
1453
1454 ;;; The `custom-magic' Widget.
1455
1456 (defgroup custom-magic-faces nil
1457 "Faces used by the magic button."
1458 :group 'custom-faces
1459 :group 'custom-buffer)
1460
1461 (defface custom-invalid-face '((((class color))
1462 (:foreground "yellow" :background "red"))
1463 (t
1464 (:bold t :italic t :underline t)))
1465 "Face used when the customize item is invalid."
1466 :group 'custom-magic-faces)
1467
1468 (defface custom-rogue-face '((((class color))
1469 (:foreground "pink" :background "black"))
1470 (t
1471 (:underline t)))
1472 "Face used when the customize item is not defined for customization."
1473 :group 'custom-magic-faces)
1474
1475 (defface custom-modified-face '((((class color))
1476 (:foreground "white" :background "blue"))
1477 (t
1478 (:italic t :bold)))
1479 "Face used when the customize item has been modified."
1480 :group 'custom-magic-faces)
1481
1482 (defface custom-set-face '((((class color))
1483 (:foreground "blue" :background "white"))
1484 (t
1485 (:italic t)))
1486 "Face used when the customize item has been set."
1487 :group 'custom-magic-faces)
1488
1489 (defface custom-changed-face '((((class color))
1490 (:foreground "white" :background "blue"))
1491 (t
1492 (:italic t)))
1493 "Face used when the customize item has been changed."
1494 :group 'custom-magic-faces)
1495
1496 (defface custom-saved-face '((t (:underline t)))
1497 "Face used when the customize item has been saved."
1498 :group 'custom-magic-faces)
1499
1500 (defconst custom-magic-alist '((nil "#" underline "\
1501 uninitialized, you should not see this.")
1502 (unknown "?" italic "\
1503 unknown, you should not see this.")
1504 (hidden "-" default "\
1505 hidden, invoke \"Show\" in the previous line to show." "\
1506 group now hidden, invoke \"Show\", above, to show contents.")
1507 (invalid "x" custom-invalid-face "\
1508 the value displayed for this %c is invalid and cannot be set.")
1509 (modified "*" custom-modified-face "\
1510 you have edited the value as text, but you have not set the %c." "\
1511 you have edited something in this group, but not set it.")
1512 (set "+" custom-set-face "\
1513 you have set this %c, but not saved it for future sessions." "\
1514 something in this group has been set, but not saved.")
1515 (changed ":" custom-changed-face "\
1516 this %c has been changed outside the customize buffer." "\
1517 something in this group has been changed outside customize.")
1518 (saved "!" custom-saved-face "\
1519 this %c has been set and saved." "\
1520 something in this group has been set and saved.")
1521 (rogue "@" custom-rogue-face "\
1522 this %c has not been changed with customize." "\
1523 something in this group is not prepared for customization.")
1524 (standard " " nil "\
1525 this %c is unchanged from its standard setting." "\
1526 visible group members are all at standard settings."))
1527 "Alist of customize option states.
1528 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1529
1530 STATE is one of the following symbols:
1531
1532 `nil'
1533 For internal use, should never occur.
1534 `unknown'
1535 For internal use, should never occur.
1536 `hidden'
1537 This item is not being displayed.
1538 `invalid'
1539 This item is modified, but has an invalid form.
1540 `modified'
1541 This item is modified, and has a valid form.
1542 `set'
1543 This item has been set but not saved.
1544 `changed'
1545 The current value of this item has been changed temporarily.
1546 `saved'
1547 This item is marked for saving.
1548 `rogue'
1549 This item has no customization information.
1550 `standard'
1551 This item is unchanged from the standard setting.
1552
1553 MAGIC is a string used to present that state.
1554
1555 FACE is a face used to present the state.
1556
1557 ITEM-DESC is a string describing the state for options.
1558
1559 GROUP-DESC is a string describing the state for groups. If this is
1560 left out, ITEM-DESC will be used.
1561
1562 The string %c in either description will be replaced with the
1563 category of the item. These are `group'. `option', and `face'.
1564
1565 The list should be sorted most significant first.")
1566
1567 (defcustom custom-magic-show 'long
1568 "If non-nil, show textual description of the state.
1569 If `long', show a full-line description, not just one word."
1570 :type '(choice (const :tag "no" nil)
1571 (const long)
1572 (other :tag "short" short))
1573 :group 'custom-buffer)
1574
1575 (defcustom custom-magic-show-hidden '(option face)
1576 "Control whether the State button is shown for hidden items.
1577 The value should be a list with the custom categories where the State
1578 button should be visible. Possible categories are `group', `option',
1579 and `face'."
1580 :type '(set (const group) (const option) (const face))
1581 :group 'custom-buffer)
1582
1583 (defcustom custom-magic-show-button nil
1584 "Show a \"magic\" button indicating the state of each customization option."
1585 :type 'boolean
1586 :group 'custom-buffer)
1587
1588 (define-widget 'custom-magic 'default
1589 "Show and manipulate state for a customization option."
1590 :format "%v"
1591 :action 'widget-parent-action
1592 :notify 'ignore
1593 :value-get 'ignore
1594 :value-create 'custom-magic-value-create
1595 :value-delete 'widget-children-value-delete)
1596
1597 (defun widget-magic-mouse-down-action (widget &optional event)
1598 ;; Non-nil unless hidden.
1599 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1600 :custom-state)
1601 'hidden)))
1602
1603 (defun custom-magic-value-create (widget)
1604 ;; Create compact status report for WIDGET.
1605 (let* ((parent (widget-get widget :parent))
1606 (state (widget-get parent :custom-state))
1607 (hidden (eq state 'hidden))
1608 (entry (assq state custom-magic-alist))
1609 (magic (nth 1 entry))
1610 (face (nth 2 entry))
1611 (category (widget-get parent :custom-category))
1612 (text (or (and (eq category 'group)
1613 (nth 4 entry))
1614 (nth 3 entry)))
1615 (form (widget-get parent :custom-form))
1616 children)
1617 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1618 (setq text (concat (match-string 1 text)
1619 (symbol-name category)
1620 (match-string 2 text))))
1621 (when (and custom-magic-show
1622 (or (not hidden)
1623 (memq category custom-magic-show-hidden)))
1624 (insert " ")
1625 (when (and (eq category 'group)
1626 (not (and (eq custom-buffer-style 'links)
1627 (> (widget-get parent :custom-level) 1))))
1628 (insert-char ?\ (* custom-buffer-indent
1629 (widget-get parent :custom-level))))
1630 (push (widget-create-child-and-convert
1631 widget 'choice-item
1632 :help-echo "Change the state of this item."
1633 :format (if hidden "%t" "%[%t%]")
1634 :button-prefix 'widget-push-button-prefix
1635 :button-suffix 'widget-push-button-suffix
1636 :mouse-down-action 'widget-magic-mouse-down-action
1637 :tag "State")
1638 children)
1639 (insert ": ")
1640 (let ((start (point)))
1641 (if (eq custom-magic-show 'long)
1642 (insert text)
1643 (insert (symbol-name state)))
1644 (cond ((eq form 'lisp)
1645 (insert " (lisp)"))
1646 ((eq form 'mismatch)
1647 (insert " (mismatch)")))
1648 (put-text-property start (point) 'face 'custom-state-face))
1649 (insert "\n"))
1650 (when (and (eq category 'group)
1651 (not (and (eq custom-buffer-style 'links)
1652 (> (widget-get parent :custom-level) 1))))
1653 (insert-char ?\ (* custom-buffer-indent
1654 (widget-get parent :custom-level))))
1655 (when custom-magic-show-button
1656 (when custom-magic-show
1657 (let ((indent (widget-get parent :indent)))
1658 (when indent
1659 (insert-char ? indent))))
1660 (push (widget-create-child-and-convert
1661 widget 'choice-item
1662 :mouse-down-action 'widget-magic-mouse-down-action
1663 :button-face face
1664 :button-prefix ""
1665 :button-suffix ""
1666 :help-echo "Change the state."
1667 :format (if hidden "%t" "%[%t%]")
1668 :tag (if (memq form '(lisp mismatch))
1669 (concat "(" magic ")")
1670 (concat "[" magic "]")))
1671 children)
1672 (insert " "))
1673 (widget-put widget :children children)))
1674
1675 (defun custom-magic-reset (widget)
1676 "Redraw the :custom-magic property of WIDGET."
1677 (let ((magic (widget-get widget :custom-magic)))
1678 (widget-value-set magic (widget-value magic))))
1679
1680 ;;; The `custom' Widget.
1681
1682 (defface custom-button-face
1683 '((((type x) (class color)) ; Like default modeline
1684 (:box (:line-width 2 :style released-button) :background "lightgrey"))
1685 (t
1686 nil))
1687 "Face used for buttons in customization buffers."
1688 :version "21.1"
1689 :group 'custom-faces)
1690
1691 (defface custom-button-pressed-face
1692 '((((type x) (class color))
1693 (:box (:line-width 2 :style pressed-button) :background "lightgrey"))
1694 (t
1695 (:inverse-video t)))
1696 "Face used for buttons in customization buffers."
1697 :version "21.1"
1698 :group 'custom-faces)
1699
1700 (defface custom-documentation-face nil
1701 "Face used for documentation strings in customization buffers."
1702 :group 'custom-faces)
1703
1704 (defface custom-state-face '((((class color)
1705 (background dark))
1706 (:foreground "lime green"))
1707 (((class color)
1708 (background light))
1709 (:foreground "dark green"))
1710 (t nil))
1711 "Face used for State descriptions in the customize buffer."
1712 :group 'custom-faces)
1713
1714 (define-widget 'custom 'default
1715 "Customize a user option."
1716 :format "%v"
1717 :convert-widget 'custom-convert-widget
1718 :notify 'custom-notify
1719 :custom-prefix ""
1720 :custom-level 1
1721 :custom-state 'hidden
1722 :documentation-property 'widget-subclass-responsibility
1723 :value-create 'widget-subclass-responsibility
1724 :value-delete 'widget-children-value-delete
1725 :value-get 'widget-value-value-get
1726 :validate 'widget-children-validate
1727 :match (lambda (widget value) (symbolp value)))
1728
1729 (defun custom-convert-widget (widget)
1730 ;; Initialize :value and :tag from :args in WIDGET.
1731 (let ((args (widget-get widget :args)))
1732 (when args
1733 (widget-put widget :value (widget-apply widget
1734 :value-to-internal (car args)))
1735 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1736 (widget-put widget :args nil)))
1737 widget)
1738
1739 (defun custom-notify (widget &rest args)
1740 "Keep track of changes."
1741 (let ((state (widget-get widget :custom-state)))
1742 (unless (eq state 'modified)
1743 (unless (memq state '(nil unknown hidden))
1744 (widget-put widget :custom-state 'modified))
1745 (custom-magic-reset widget)
1746 (apply 'widget-default-notify widget args))))
1747
1748 (defun custom-redraw (widget)
1749 "Redraw WIDGET with current settings."
1750 (let ((line (count-lines (point-min) (point)))
1751 (column (current-column))
1752 (pos (point))
1753 (from (marker-position (widget-get widget :from)))
1754 (to (marker-position (widget-get widget :to))))
1755 (save-excursion
1756 (widget-value-set widget (widget-value widget))
1757 (custom-redraw-magic widget))
1758 (when (and (>= pos from) (<= pos to))
1759 (condition-case nil
1760 (progn
1761 (if (> column 0)
1762 (goto-line line)
1763 (goto-line (1+ line)))
1764 (move-to-column column))
1765 (error nil)))))
1766
1767 (defun custom-redraw-magic (widget)
1768 "Redraw WIDGET state with current settings."
1769 (while widget
1770 (let ((magic (widget-get widget :custom-magic)))
1771 (cond (magic
1772 (widget-value-set magic (widget-value magic))
1773 (when (setq widget (widget-get widget :group))
1774 (custom-group-state-update widget)))
1775 (t
1776 (setq widget nil)))))
1777 (widget-setup))
1778
1779 (defun custom-show (widget value)
1780 "Non-nil if WIDGET should be shown with VALUE by default."
1781 (let ((show (widget-get widget :custom-show)))
1782 (cond ((null show)
1783 nil)
1784 ((eq t show)
1785 t)
1786 (t
1787 (funcall show widget value)))))
1788
1789 (defvar custom-load-recursion nil
1790 "Hack to avoid recursive dependencies.")
1791
1792 (defun custom-load-symbol (symbol)
1793 "Load all dependencies for SYMBOL."
1794 (unless custom-load-recursion
1795 (let ((custom-load-recursion t)
1796 (loads (get symbol 'custom-loads))
1797 load)
1798 (while loads
1799 (setq load (car loads)
1800 loads (cdr loads))
1801 (cond ((symbolp load)
1802 (condition-case nil
1803 (require load)
1804 (error nil)))
1805 ;; Don't reload a file already loaded.
1806 ((and (boundp 'preloaded-file-list)
1807 (member load preloaded-file-list)))
1808 ((assoc load load-history))
1809 ((assoc (locate-library load) load-history))
1810 (t
1811 (condition-case nil
1812 ;; Without this, we would load cus-edit recursively.
1813 ;; We are still loading it when we call this,
1814 ;; and it is not in load-history yet.
1815 (or (equal load "cus-edit")
1816 (load-library load))
1817 (error nil))))))))
1818
1819 (defun custom-load-widget (widget)
1820 "Load all dependencies for WIDGET."
1821 (custom-load-symbol (widget-value widget)))
1822
1823 (defun custom-unloaded-symbol-p (symbol)
1824 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1825 (let ((found nil)
1826 (loads (get symbol 'custom-loads))
1827 load)
1828 (while loads
1829 (setq load (car loads)
1830 loads (cdr loads))
1831 (cond ((symbolp load)
1832 (unless (featurep load)
1833 (setq found t)))
1834 ((assoc load load-history))
1835 ((assoc (locate-library load) load-history)
1836 (message nil))
1837 (t
1838 (setq found t))))
1839 found))
1840
1841 (defun custom-unloaded-widget-p (widget)
1842 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1843 (custom-unloaded-symbol-p (widget-value widget)))
1844
1845 (defun custom-toggle-hide (widget)
1846 "Toggle visibility of WIDGET."
1847 (custom-load-widget widget)
1848 (let ((state (widget-get widget :custom-state)))
1849 (cond ((memq state '(invalid modified))
1850 (error "There are unset changes"))
1851 ((eq state 'hidden)
1852 (widget-put widget :custom-state 'unknown))
1853 (t
1854 (widget-put widget :documentation-shown nil)
1855 (widget-put widget :custom-state 'hidden)))
1856 (custom-redraw widget)
1857 (widget-setup)))
1858
1859 (defun custom-toggle-parent (widget &rest ignore)
1860 "Toggle visibility of parent of WIDGET."
1861 (custom-toggle-hide (widget-get widget :parent)))
1862
1863 (defun custom-add-see-also (widget &optional prefix)
1864 "Add `See also ...' to WIDGET if there are any links.
1865 Insert PREFIX first if non-nil."
1866 (let* ((symbol (widget-get widget :value))
1867 (links (get symbol 'custom-links))
1868 (many (> (length links) 2))
1869 (buttons (widget-get widget :buttons))
1870 (indent (widget-get widget :indent)))
1871 (when links
1872 (when indent
1873 (insert-char ?\ indent))
1874 (when prefix
1875 (insert prefix))
1876 (insert "See also ")
1877 (while links
1878 (push (widget-create-child-and-convert widget (car links))
1879 buttons)
1880 (setq links (cdr links))
1881 (cond ((null links)
1882 (insert ".\n"))
1883 ((null (cdr links))
1884 (if many
1885 (insert ", and ")
1886 (insert " and ")))
1887 (t
1888 (insert ", "))))
1889 (widget-put widget :buttons buttons))))
1890
1891 (defun custom-add-parent-links (widget &optional initial-string)
1892 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1893 The value if non-nil if any parents were found.
1894 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1895 (let ((name (widget-value widget))
1896 (type (widget-type widget))
1897 (buttons (widget-get widget :buttons))
1898 (start (point))
1899 found)
1900 (insert (or initial-string "Parent groups:"))
1901 (mapatoms (lambda (symbol)
1902 (let ((entry (assq name (get symbol 'custom-group))))
1903 (when (eq (nth 1 entry) type)
1904 (insert " ")
1905 (push (widget-create-child-and-convert
1906 widget 'custom-group-link
1907 :tag (custom-unlispify-tag-name symbol)
1908 symbol)
1909 buttons)
1910 (setq found t)))))
1911 (widget-put widget :buttons buttons)
1912 (if found
1913 (insert "\n")
1914 (delete-region start (point)))
1915 found))
1916
1917 ;;; The `custom-comment' Widget.
1918
1919 ;; like the editable field
1920 (defface custom-comment-face '((((class grayscale color)
1921 (background light))
1922 (:background "gray85"))
1923 (((class grayscale color)
1924 (background dark))
1925 (:background "dim gray"))
1926 (t
1927 (:italic t)))
1928 "Face used for comments on variables or faces"
1929 :version "21.1"
1930 :group 'custom-faces)
1931
1932 ;; like font-lock-comment-face
1933 (defface custom-comment-tag-face
1934 '((((class color) (background dark)) (:foreground "gray80"))
1935 (((class color) (background light)) (:foreground "blue4"))
1936 (((class grayscale) (background light))
1937 (:foreground "DimGray" :bold t :italic t))
1938 (((class grayscale) (background dark))
1939 (:foreground "LightGray" :bold t :italic t))
1940 (t (:bold t)))
1941 "Face used for variables or faces comment tags"
1942 :group 'custom-faces)
1943
1944 (define-widget 'custom-comment 'string
1945 "User comment."
1946 :tag "Comment"
1947 :help-echo "Edit a comment here."
1948 :sample-face 'custom-comment-tag-face
1949 :value-face 'custom-comment-face
1950 :shown nil
1951 :create 'custom-comment-create)
1952
1953 (defun custom-comment-create (widget)
1954 (let* ((null-comment (equal "" (widget-value widget))))
1955 (when (or (widget-get (widget-get widget :parent) :comment-shown)
1956 (not null-comment))
1957 (widget-default-create widget))))
1958
1959 (defun custom-comment-hide (widget)
1960 (widget-put (widget-get widget :parent) :comment-shown nil))
1961
1962 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
1963 ;; the global custom one
1964 (defun custom-comment-show (widget)
1965 (widget-put widget :comment-shown t)
1966 (custom-redraw widget)
1967 (widget-setup))
1968
1969 (defun custom-comment-invisible-p (widget)
1970 (let ((val (widget-value (widget-get widget :comment-widget))))
1971 (and (equal "" val)
1972 (not (widget-get widget :comment-shown)))))
1973
1974 ;;; The `custom-variable' Widget.
1975
1976 (defface custom-variable-tag-face '((((class color)
1977 (background dark))
1978 (:foreground "light blue" :underline t))
1979 (((class color)
1980 (background light))
1981 (:foreground "blue" :underline t))
1982 (t (:underline t)))
1983 "Face used for unpushable variable tags."
1984 :group 'custom-faces)
1985
1986 (defface custom-variable-button-face '((t (:underline t :bold t)))
1987 "Face used for pushable variable tags."
1988 :group 'custom-faces)
1989
1990 (defcustom custom-variable-default-form 'edit
1991 "Default form of displaying variable values."
1992 :type '(choice (const edit)
1993 (const lisp))
1994 :group 'custom-buffer
1995 :version "20.3")
1996
1997 (define-widget 'custom-variable 'custom
1998 "Customize variable."
1999 :format "%v"
2000 :help-echo "Set or reset this variable."
2001 :documentation-property 'variable-documentation
2002 :custom-category 'option
2003 :custom-state nil
2004 :custom-menu 'custom-variable-menu-create
2005 :custom-form nil ; defaults to value of `custom-variable-default-form'
2006 :value-create 'custom-variable-value-create
2007 :action 'custom-variable-action
2008 :custom-set 'custom-variable-set
2009 :custom-save 'custom-variable-save
2010 :custom-reset-current 'custom-redraw
2011 :custom-reset-saved 'custom-variable-reset-saved
2012 :custom-reset-standard 'custom-variable-reset-standard)
2013
2014 (defun custom-variable-type (symbol)
2015 "Return a widget suitable for editing the value of SYMBOL.
2016 If SYMBOL has a `custom-type' property, use that.
2017 Otherwise, look up symbol in `custom-guess-type-alist'."
2018 (let* ((type (or (get symbol 'custom-type)
2019 (and (not (get symbol 'standard-value))
2020 (custom-guess-type symbol))
2021 'sexp))
2022 (options (get symbol 'custom-options))
2023 (tmp (if (listp type)
2024 (copy-sequence type)
2025 (list type))))
2026 (when options
2027 (widget-put tmp :options options))
2028 tmp))
2029
2030 (defun custom-variable-value-create (widget)
2031 "Here is where you edit the variable's value."
2032 (custom-load-widget widget)
2033 (unless (widget-get widget :custom-form)
2034 (widget-put widget :custom-form custom-variable-default-form))
2035 (let* ((buttons (widget-get widget :buttons))
2036 (children (widget-get widget :children))
2037 (form (widget-get widget :custom-form))
2038 (state (widget-get widget :custom-state))
2039 (symbol (widget-get widget :value))
2040 (tag (widget-get widget :tag))
2041 (type (custom-variable-type symbol))
2042 (conv (widget-convert type))
2043 (get (or (get symbol 'custom-get) 'default-value))
2044 (prefix (widget-get widget :custom-prefix))
2045 (last (widget-get widget :custom-last))
2046 (value (if (default-boundp symbol)
2047 (funcall get symbol)
2048 (widget-get conv :value))))
2049 ;; If the widget is new, the child determines whether it is hidden.
2050 (cond (state)
2051 ((custom-show type value)
2052 (setq state 'unknown))
2053 (t
2054 (setq state 'hidden)))
2055 ;; If we don't know the state, see if we need to edit it in lisp form.
2056 (when (eq state 'unknown)
2057 (unless (widget-apply conv :match value)
2058 ;; (widget-apply (widget-convert type) :match value)
2059 (setq form 'mismatch)))
2060 ;; Now we can create the child widget.
2061 (cond ((eq custom-buffer-style 'tree)
2062 (insert prefix (if last " `--- " " |--- "))
2063 (push (widget-create-child-and-convert
2064 widget 'custom-browse-variable-tag)
2065 buttons)
2066 (insert " " tag "\n")
2067 (widget-put widget :buttons buttons))
2068 ((eq state 'hidden)
2069 ;; Indicate hidden value.
2070 (push (widget-create-child-and-convert
2071 widget 'item
2072 :format "%{%t%}: "
2073 :sample-face 'custom-variable-tag-face
2074 :tag tag
2075 :parent widget)
2076 buttons)
2077 (push (widget-create-child-and-convert
2078 widget 'visibility
2079 :help-echo "Show the value of this option."
2080 :action 'custom-toggle-parent
2081 nil)
2082 buttons))
2083 ((memq form '(lisp mismatch))
2084 ;; In lisp mode edit the saved value when possible.
2085 (let* ((value (cond ((get symbol 'saved-value)
2086 (car (get symbol 'saved-value)))
2087 ((get symbol 'standard-value)
2088 (car (get symbol 'standard-value)))
2089 ((default-boundp symbol)
2090 (custom-quote (funcall get symbol)))
2091 (t
2092 (custom-quote (widget-get conv :value))))))
2093 (insert (symbol-name symbol) ": ")
2094 (push (widget-create-child-and-convert
2095 widget 'visibility
2096 :help-echo "Hide the value of this option."
2097 :action 'custom-toggle-parent
2098 t)
2099 buttons)
2100 (insert " ")
2101 (push (widget-create-child-and-convert
2102 widget 'sexp
2103 :button-face 'custom-variable-button-face
2104 :format "%v"
2105 :tag (symbol-name symbol)
2106 :parent widget
2107 :value value)
2108 children)))
2109 (t
2110 ;; Edit mode.
2111 (let* ((format (widget-get type :format))
2112 tag-format value-format)
2113 (unless (string-match ":" format)
2114 (error "Bad format"))
2115 (setq tag-format (substring format 0 (match-end 0)))
2116 (setq value-format (substring format (match-end 0)))
2117 (push (widget-create-child-and-convert
2118 widget 'item
2119 :format tag-format
2120 :action 'custom-tag-action
2121 :help-echo "Change value of this option."
2122 :mouse-down-action 'custom-tag-mouse-down-action
2123 :button-face 'custom-variable-button-face
2124 :sample-face 'custom-variable-tag-face
2125 tag)
2126 buttons)
2127 (insert " ")
2128 (push (widget-create-child-and-convert
2129 widget 'visibility
2130 :help-echo "Hide the value of this option."
2131 :action 'custom-toggle-parent
2132 t)
2133 buttons)
2134 (push (widget-create-child-and-convert
2135 widget type
2136 :format value-format
2137 :value value)
2138 children))))
2139 (unless (eq custom-buffer-style 'tree)
2140 (unless (eq (preceding-char) ?\n)
2141 (widget-insert "\n"))
2142 ;; Create the magic button.
2143 (let ((magic (widget-create-child-and-convert
2144 widget 'custom-magic nil)))
2145 (widget-put widget :custom-magic magic)
2146 (push magic buttons))
2147 ;; ### NOTE: this is ugly!!!! I need to update the :buttons property
2148 ;; before the call to `widget-default-format-handler'. Otherwise, I
2149 ;; loose my current `buttons'. This function shouldn't be called like
2150 ;; this anyway. The doc string widget should be added like the others.
2151 ;; --dv
2152 (widget-put widget :buttons buttons)
2153 ;; Insert documentation.
2154 (widget-default-format-handler widget ?h)
2155
2156 ;; The comment field
2157 (unless (eq state 'hidden)
2158 (let* ((comment (get symbol 'variable-comment))
2159 (comment-widget
2160 (widget-create-child-and-convert
2161 widget 'custom-comment
2162 :parent widget
2163 :value (or comment ""))))
2164 (widget-put widget :comment-widget comment-widget)
2165 ;; Don't push it !!! Custom assumes that the first child is the
2166 ;; value one.
2167 (setq children (append children (list comment-widget)))))
2168 ;; Update the rest of the properties properties.
2169 (widget-put widget :custom-form form)
2170 (widget-put widget :children children)
2171 ;; Now update the state.
2172 (if (eq state 'hidden)
2173 (widget-put widget :custom-state state)
2174 (custom-variable-state-set widget))
2175 ;; See also.
2176 (unless (eq state 'hidden)
2177 (when (eq (widget-get widget :custom-level) 1)
2178 (custom-add-parent-links widget))
2179 (custom-add-see-also widget)))))
2180
2181 (defun custom-tag-action (widget &rest args)
2182 "Pass :action to first child of WIDGET's parent."
2183 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2184 :action args))
2185
2186 (defun custom-tag-mouse-down-action (widget &rest args)
2187 "Pass :mouse-down-action to first child of WIDGET's parent."
2188 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2189 :mouse-down-action args))
2190
2191 (defun custom-variable-state-set (widget)
2192 "Set the state of WIDGET."
2193 (let* ((symbol (widget-value widget))
2194 (get (or (get symbol 'custom-get) 'default-value))
2195 (value (if (default-boundp symbol)
2196 (funcall get symbol)
2197 (widget-get widget :value)))
2198 (comment (get symbol 'variable-comment))
2199 tmp
2200 temp
2201 (state (cond ((progn (setq tmp (get symbol 'customized-value))
2202 (setq temp
2203 (get symbol 'customized-variable-comment))
2204 (or tmp temp))
2205 (if (condition-case nil
2206 (and (equal value (eval (car tmp)))
2207 (equal comment temp))
2208 (error nil))
2209 'set
2210 'changed))
2211 ((progn (setq tmp (get symbol 'saved-value))
2212 (setq temp (get symbol 'saved-variable-comment))
2213 (or tmp temp))
2214 (if (condition-case nil
2215 (and (equal value (eval (car tmp)))
2216 (equal comment temp))
2217 (error nil))
2218 'saved
2219 'changed))
2220 ((setq tmp (get symbol 'standard-value))
2221 (if (condition-case nil
2222 (and (equal value (eval (car tmp)))
2223 (equal comment nil))
2224 (error nil))
2225 'standard
2226 'changed))
2227 (t 'rogue))))
2228 (widget-put widget :custom-state state)))
2229
2230 (defvar custom-variable-menu
2231 '(("Set for Current Session" custom-variable-set
2232 (lambda (widget)
2233 (eq (widget-get widget :custom-state) 'modified)))
2234 ("Save for Future Sessions" custom-variable-save
2235 (lambda (widget)
2236 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
2237 ("Reset to Current" custom-redraw
2238 (lambda (widget)
2239 (and (default-boundp (widget-value widget))
2240 (memq (widget-get widget :custom-state) '(modified changed)))))
2241 ("Reset to Saved" custom-variable-reset-saved
2242 (lambda (widget)
2243 (and (or (get (widget-value widget) 'saved-value)
2244 (get (widget-value widget) 'saved-variable-comment))
2245 (memq (widget-get widget :custom-state)
2246 '(modified set changed rogue)))))
2247 ("Reset to Standard Settings" custom-variable-reset-standard
2248 (lambda (widget)
2249 (and (get (widget-value widget) 'standard-value)
2250 (memq (widget-get widget :custom-state)
2251 '(modified set changed saved rogue)))))
2252 ("---" ignore ignore)
2253 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2254 ("---" ignore ignore)
2255 ("Don't show as Lisp expression" custom-variable-edit
2256 (lambda (widget)
2257 (eq (widget-get widget :custom-form) 'lisp)))
2258 ("Show initial Lisp expression" custom-variable-edit-lisp
2259 (lambda (widget)
2260 (eq (widget-get widget :custom-form) 'edit))))
2261 "Alist of actions for the `custom-variable' widget.
2262 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2263 the menu entry, ACTION is the function to call on the widget when the
2264 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2265 widget as an argument, and returns non-nil if ACTION is valid on that
2266 widget. If FILTER is nil, ACTION is always valid.")
2267
2268 (defun custom-variable-action (widget &optional event)
2269 "Show the menu for `custom-variable' WIDGET.
2270 Optional EVENT is the location for the menu."
2271 (if (eq (widget-get widget :custom-state) 'hidden)
2272 (custom-toggle-hide widget)
2273 (unless (eq (widget-get widget :custom-state) 'modified)
2274 (custom-variable-state-set widget))
2275 (custom-redraw-magic widget)
2276 (let* ((completion-ignore-case t)
2277 (answer (widget-choose (concat "Operation on "
2278 (custom-unlispify-tag-name
2279 (widget-get widget :value)))
2280 (custom-menu-filter custom-variable-menu
2281 widget)
2282 event)))
2283 (if answer
2284 (funcall answer widget)))))
2285
2286 (defun custom-variable-edit (widget)
2287 "Edit value of WIDGET."
2288 (widget-put widget :custom-state 'unknown)
2289 (widget-put widget :custom-form 'edit)
2290 (custom-redraw widget))
2291
2292 (defun custom-variable-edit-lisp (widget)
2293 "Edit the lisp representation of the value of WIDGET."
2294 (widget-put widget :custom-state 'unknown)
2295 (widget-put widget :custom-form 'lisp)
2296 (custom-redraw widget))
2297
2298 (defun custom-variable-set (widget)
2299 "Set the current value for the variable being edited by WIDGET."
2300 (let* ((form (widget-get widget :custom-form))
2301 (state (widget-get widget :custom-state))
2302 (child (car (widget-get widget :children)))
2303 (symbol (widget-value widget))
2304 (set (or (get symbol 'custom-set) 'set-default))
2305 (comment-widget (widget-get widget :comment-widget))
2306 (comment (widget-value comment-widget))
2307 val)
2308 (cond ((eq state 'hidden)
2309 (error "Cannot set hidden variable"))
2310 ((setq val (widget-apply child :validate))
2311 (goto-char (widget-get val :from))
2312 (error "%s" (widget-get val :error)))
2313 ((memq form '(lisp mismatch))
2314 (when (equal comment "")
2315 (setq comment nil)
2316 ;; Make the comment invisible by hand if it's empty
2317 (custom-comment-hide comment-widget))
2318 (funcall set symbol (eval (setq val (widget-value child))))
2319 (put symbol 'customized-value (list val))
2320 (put symbol 'variable-comment comment)
2321 (put symbol 'customized-variable-comment comment))
2322 (t
2323 (when (equal comment "")
2324 (setq comment nil)
2325 ;; Make the comment invisible by hand if it's empty
2326 (custom-comment-hide comment-widget))
2327 (funcall set symbol (setq val (widget-value child)))
2328 (put symbol 'customized-value (list (custom-quote val)))
2329 (put symbol 'variable-comment comment)
2330 (put symbol 'customized-variable-comment comment)))
2331 (custom-variable-state-set widget)
2332 (custom-redraw-magic widget)))
2333
2334 (defun custom-variable-save (widget)
2335 "Set and save the value for the variable being edited by WIDGET."
2336 (let* ((form (widget-get widget :custom-form))
2337 (state (widget-get widget :custom-state))
2338 (child (car (widget-get widget :children)))
2339 (symbol (widget-value widget))
2340 (set (or (get symbol 'custom-set) 'set-default))
2341 (comment-widget (widget-get widget :comment-widget))
2342 (comment (widget-value comment-widget))
2343 val)
2344 (cond ((eq state 'hidden)
2345 (error "Cannot set hidden variable"))
2346 ((setq val (widget-apply child :validate))
2347 (goto-char (widget-get val :from))
2348 (error "%s" (widget-get val :error)))
2349 ((memq form '(lisp mismatch))
2350 (when (equal comment "")
2351 (setq comment nil)
2352 ;; Make the comment invisible by hand if it's empty
2353 (custom-comment-hide comment-widget))
2354 (put symbol 'saved-value (list (widget-value child)))
2355 (funcall set symbol (eval (widget-value child)))
2356 (put symbol 'variable-comment comment)
2357 (put symbol 'saved-variable-comment comment))
2358 (t
2359 (when (equal comment "")
2360 (setq comment nil)
2361 ;; Make the comment invisible by hand if it's empty
2362 (custom-comment-hide comment-widget))
2363 (put symbol 'saved-value
2364 (list (custom-quote (widget-value child))))
2365 (funcall set symbol (widget-value child))
2366 (put symbol 'variable-comment comment)
2367 (put symbol 'saved-variable-comment comment)))
2368 (put symbol 'customized-value nil)
2369 (put symbol 'customized-variable-comment nil)
2370 (custom-save-all)
2371 (custom-variable-state-set widget)
2372 (custom-redraw-magic widget)))
2373
2374 (defun custom-variable-reset-saved (widget)
2375 "Restore the saved value for the variable being edited by WIDGET."
2376 (let* ((symbol (widget-value widget))
2377 (set (or (get symbol 'custom-set) 'set-default))
2378 (comment-widget (widget-get widget :comment-widget))
2379 (value (get symbol 'saved-value))
2380 (comment (get symbol 'saved-variable-comment)))
2381 (cond ((or value comment)
2382 (put symbol 'variable-comment comment)
2383 (condition-case nil
2384 (funcall set symbol (eval (car value)))
2385 (error nil)))
2386 (t
2387 (error "No saved value for %s" symbol)))
2388 (put symbol 'customized-value nil)
2389 (put symbol 'customized-variable-comment nil)
2390 (widget-put widget :custom-state 'unknown)
2391 ;; This call will possibly make the comment invisible
2392 (custom-redraw widget)))
2393
2394 (defun custom-variable-reset-standard (widget)
2395 "Restore the standard setting for the variable being edited by WIDGET."
2396 (let* ((symbol (widget-value widget))
2397 (set (or (get symbol 'custom-set) 'set-default))
2398 (comment-widget (widget-get widget :comment-widget)))
2399 (if (get symbol 'standard-value)
2400 (funcall set symbol (eval (car (get symbol 'standard-value))))
2401 (error "No standard setting known for %S" symbol))
2402 (put symbol 'variable-comment nil)
2403 (put symbol 'customized-value nil)
2404 (put symbol 'customized-variable-comment nil)
2405 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2406 (put symbol 'saved-value nil)
2407 (put symbol 'saved-variable-comment nil)
2408 (custom-save-all))
2409 (widget-put widget :custom-state 'unknown)
2410 ;; This call will possibly make the comment invisible
2411 (custom-redraw widget)))
2412
2413 ;;; The `custom-face-edit' Widget.
2414
2415 (define-widget 'custom-face-edit 'checklist
2416 "Edit face attributes."
2417 :format "%t: %v"
2418 :tag "Attributes"
2419 :extra-offset 12
2420 :button-args '(:help-echo "Control whether this attribute has any effect.")
2421 :args (mapcar (lambda (att)
2422 (list 'group
2423 :inline t
2424 :sibling-args (widget-get (nth 1 att) :sibling-args)
2425 (list 'const :format "" :value (nth 0 att))
2426 (nth 1 att)))
2427 custom-face-attributes))
2428
2429 ;;; The `custom-display' Widget.
2430
2431 (define-widget 'custom-display 'menu-choice
2432 "Select a display type."
2433 :tag "Display"
2434 :value t
2435 :help-echo "Specify frames where the face attributes should be used."
2436 :args '((const :tag "all" t)
2437 (checklist
2438 :offset 0
2439 :extra-offset 9
2440 :args ((group :sibling-args (:help-echo "\
2441 Only match the specified window systems.")
2442 (const :format "Type: "
2443 type)
2444 (checklist :inline t
2445 :offset 0
2446 (const :format "X "
2447 :sibling-args (:help-echo "\
2448 The X11 Window System.")
2449 x)
2450 (const :format "PM "
2451 :sibling-args (:help-echo "\
2452 OS/2 Presentation Manager.")
2453 pm)
2454 (const :format "W32 "
2455 :sibling-args (:help-echo "\
2456 Windows NT/9X.")
2457 w32)
2458 (const :format "DOS "
2459 :sibling-args (:help-echo "\
2460 Plain MS-DOS.")
2461 pc)
2462 (const :format "TTY%n"
2463 :sibling-args (:help-echo "\
2464 Plain text terminals.")
2465 tty)))
2466 (group :sibling-args (:help-echo "\
2467 Only match the frames with the specified color support.")
2468 (const :format "Class: "
2469 class)
2470 (checklist :inline t
2471 :offset 0
2472 (const :format "Color "
2473 :sibling-args (:help-echo "\
2474 Match color frames.")
2475 color)
2476 (const :format "Grayscale "
2477 :sibling-args (:help-echo "\
2478 Match grayscale frames.")
2479 grayscale)
2480 (const :format "Monochrome%n"
2481 :sibling-args (:help-echo "\
2482 Match frames with no color support.")
2483 mono)))
2484 (group :sibling-args (:help-echo "\
2485 Only match frames with the specified intensity.")
2486 (const :format "\
2487 Background brightness: "
2488 background)
2489 (checklist :inline t
2490 :offset 0
2491 (const :format "Light "
2492 :sibling-args (:help-echo "\
2493 Match frames with light backgrounds.")
2494 light)
2495 (const :format "Dark\n"
2496 :sibling-args (:help-echo "\
2497 Match frames with dark backgrounds.")
2498 dark)))))))
2499
2500 ;;; The `custom-face' Widget.
2501
2502 (defface custom-face-tag-face '((t (:underline t)))
2503 "Face used for face tags."
2504 :group 'custom-faces)
2505
2506 (defcustom custom-face-default-form 'selected
2507 "Default form of displaying face definition."
2508 :type '(choice (const all)
2509 (const selected)
2510 (const lisp))
2511 :group 'custom-buffer
2512 :version "20.3")
2513
2514 (define-widget 'custom-face 'custom
2515 "Customize face."
2516 :sample-face 'custom-face-tag-face
2517 :help-echo "Set or reset this face."
2518 :documentation-property '(lambda (face)
2519 (face-doc-string face))
2520 :value-create 'custom-face-value-create
2521 :action 'custom-face-action
2522 :custom-category 'face
2523 :custom-form nil ; defaults to value of `custom-face-default-form'
2524 :custom-set 'custom-face-set
2525 :custom-save 'custom-face-save
2526 :custom-reset-current 'custom-redraw
2527 :custom-reset-saved 'custom-face-reset-saved
2528 :custom-reset-standard 'custom-face-reset-standard
2529 :custom-menu 'custom-face-menu-create)
2530
2531 (define-widget 'custom-face-all 'editable-list
2532 "An editable list of display specifications and attributes."
2533 :entry-format "%i %d %v"
2534 :insert-button-args '(:help-echo "Insert new display specification here.")
2535 :append-button-args '(:help-echo "Append new display specification here.")
2536 :delete-button-args '(:help-echo "Delete this display specification.")
2537 :args '((group :format "%v" custom-display custom-face-edit)))
2538
2539 (defconst custom-face-all (widget-convert 'custom-face-all)
2540 "Converted version of the `custom-face-all' widget.")
2541
2542 (define-widget 'custom-display-unselected 'item
2543 "A display specification that doesn't match the selected display."
2544 :match 'custom-display-unselected-match)
2545
2546 (defun custom-display-unselected-match (widget value)
2547 "Non-nil if VALUE is an unselected display specification."
2548 (not (face-spec-set-match-display value (selected-frame))))
2549
2550 (define-widget 'custom-face-selected 'group
2551 "Edit the attributes of the selected display in a face specification."
2552 :args '((repeat :format ""
2553 :inline t
2554 (group custom-display-unselected sexp))
2555 (group (sexp :format "") custom-face-edit)
2556 (repeat :format ""
2557 :inline t
2558 sexp)))
2559
2560 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2561 "Converted version of the `custom-face-selected' widget.")
2562
2563 (defun custom-face-value-create (widget)
2564 "Create a list of the display specifications for WIDGET."
2565 (let ((buttons (widget-get widget :buttons))
2566 children
2567 (symbol (widget-get widget :value))
2568 (tag (widget-get widget :tag))
2569 (state (widget-get widget :custom-state))
2570 (begin (point))
2571 (is-last (widget-get widget :custom-last))
2572 (prefix (widget-get widget :custom-prefix)))
2573 (unless tag
2574 (setq tag (prin1-to-string symbol)))
2575 (cond ((eq custom-buffer-style 'tree)
2576 (insert prefix (if is-last " `--- " " |--- "))
2577 (push (widget-create-child-and-convert
2578 widget 'custom-browse-face-tag)
2579 buttons)
2580 (insert " " tag "\n")
2581 (widget-put widget :buttons buttons))
2582 (t
2583 ;; Create tag.
2584 (insert tag)
2585 (if (eq custom-buffer-style 'face)
2586 (insert " ")
2587 (widget-specify-sample widget begin (point))
2588 (insert ": "))
2589 ;; Sample.
2590 (push (widget-create-child-and-convert widget 'item
2591 :format "(%{%t%})"
2592 :sample-face symbol
2593 :tag "sample")
2594 buttons)
2595 ;; Visibility.
2596 (insert " ")
2597 (push (widget-create-child-and-convert
2598 widget 'visibility
2599 :help-echo "Hide or show this face."
2600 :action 'custom-toggle-parent
2601 (not (eq state 'hidden)))
2602 buttons)
2603 ;; Magic.
2604 (insert "\n")
2605 (let ((magic (widget-create-child-and-convert
2606 widget 'custom-magic nil)))
2607 (widget-put widget :custom-magic magic)
2608 (push magic buttons))
2609 ;; Update buttons.
2610 (widget-put widget :buttons buttons)
2611 ;; Insert documentation.
2612 (widget-default-format-handler widget ?h)
2613 ;; The comment field
2614 (unless (eq state 'hidden)
2615 (let* ((comment (get symbol 'face-comment))
2616 (comment-widget
2617 (widget-create-child-and-convert
2618 widget 'custom-comment
2619 :parent widget
2620 :value (or comment ""))))
2621 (widget-put widget :comment-widget comment-widget)
2622 (push comment-widget children)))
2623 ;; See also.
2624 (unless (eq state 'hidden)
2625 (when (eq (widget-get widget :custom-level) 1)
2626 (custom-add-parent-links widget))
2627 (custom-add-see-also widget))
2628 ;; Editor.
2629 (unless (eq (preceding-char) ?\n)
2630 (insert "\n"))
2631 (unless (eq state 'hidden)
2632 (message "Creating face editor...")
2633 (custom-load-widget widget)
2634 (unless (widget-get widget :custom-form)
2635 (widget-put widget :custom-form custom-face-default-form))
2636 (let* ((symbol (widget-value widget))
2637 (spec (or (get symbol 'saved-face)
2638 (get symbol 'face-defface-spec)
2639 ;; Attempt to construct it.
2640 (list (list t (custom-face-attributes-get
2641 symbol (selected-frame))))))
2642 (form (widget-get widget :custom-form))
2643 (indent (widget-get widget :indent))
2644 edit)
2645 ;; If the user has changed this face in some other way,
2646 ;; edit it as the user has specified it.
2647 (if (not (face-spec-match-p symbol spec (selected-frame)))
2648 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
2649 (setq edit (widget-create-child-and-convert
2650 widget
2651 (cond ((and (eq form 'selected)
2652 (widget-apply custom-face-selected
2653 :match spec))
2654 (when indent (insert-char ?\ indent))
2655 'custom-face-selected)
2656 ((and (not (eq form 'lisp))
2657 (widget-apply custom-face-all
2658 :match spec))
2659 'custom-face-all)
2660 (t
2661 (when indent (insert-char ?\ indent))
2662 'sexp))
2663 :value spec))
2664 (custom-face-state-set widget)
2665 (push edit children)
2666 (widget-put widget :children children))
2667 (message "Creating face editor...done"))))))
2668
2669 (defvar custom-face-menu
2670 '(("Set for Current Session" custom-face-set)
2671 ("Save for Future Sessions" custom-face-save-command)
2672 ("Reset to Saved" custom-face-reset-saved
2673 (lambda (widget)
2674 (or (get (widget-value widget) 'saved-face)
2675 (get (widget-value widget) 'saved-face-comment))))
2676 ("Reset to Standard Setting" custom-face-reset-standard
2677 (lambda (widget)
2678 (get (widget-value widget) 'face-defface-spec)))
2679 ("---" ignore ignore)
2680 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2681 ("---" ignore ignore)
2682 ("Show all display specs" custom-face-edit-all
2683 (lambda (widget)
2684 (not (eq (widget-get widget :custom-form) 'all))))
2685 ("Just current attributes" custom-face-edit-selected
2686 (lambda (widget)
2687 (not (eq (widget-get widget :custom-form) 'selected))))
2688 ("Show as Lisp expression" custom-face-edit-lisp
2689 (lambda (widget)
2690 (not (eq (widget-get widget :custom-form) 'lisp)))))
2691 "Alist of actions for the `custom-face' widget.
2692 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2693 the menu entry, ACTION is the function to call on the widget when the
2694 menu is selected, and FILTER is a predicate which takes a `custom-face'
2695 widget as an argument, and returns non-nil if ACTION is valid on that
2696 widget. If FILTER is nil, ACTION is always valid.")
2697
2698 (defun custom-face-edit-selected (widget)
2699 "Edit selected attributes of the value of WIDGET."
2700 (widget-put widget :custom-state 'unknown)
2701 (widget-put widget :custom-form 'selected)
2702 (custom-redraw widget))
2703
2704 (defun custom-face-edit-all (widget)
2705 "Edit all attributes of the value of WIDGET."
2706 (widget-put widget :custom-state 'unknown)
2707 (widget-put widget :custom-form 'all)
2708 (custom-redraw widget))
2709
2710 (defun custom-face-edit-lisp (widget)
2711 "Edit the lisp representation of the value of WIDGET."
2712 (widget-put widget :custom-state 'unknown)
2713 (widget-put widget :custom-form 'lisp)
2714 (custom-redraw widget))
2715
2716 (defun custom-face-state-set (widget)
2717 "Set the state of WIDGET."
2718 (let* ((symbol (widget-value widget))
2719 (comment (get symbol 'face-comment))
2720 tmp temp)
2721 (widget-put widget :custom-state
2722 (cond ((progn
2723 (setq tmp (get symbol 'customized-face))
2724 (setq temp (get symbol 'customized-face-comment))
2725 (or tmp temp))
2726 (if (equal temp comment)
2727 'set
2728 'changed))
2729 ((progn
2730 (setq tmp (get symbol 'saved-face))
2731 (setq temp (get symbol 'saved-face-comment))
2732 (or tmp temp))
2733 (if (equal temp comment)
2734 'saved
2735 'changed))
2736 ((get symbol 'face-defface-spec)
2737 (if (equal comment nil)
2738 'standard
2739 'changed))
2740 (t
2741 'rogue)))))
2742
2743 (defun custom-face-action (widget &optional event)
2744 "Show the menu for `custom-face' WIDGET.
2745 Optional EVENT is the location for the menu."
2746 (if (eq (widget-get widget :custom-state) 'hidden)
2747 (custom-toggle-hide widget)
2748 (let* ((completion-ignore-case t)
2749 (symbol (widget-get widget :value))
2750 (answer (widget-choose (concat "Operation on "
2751 (custom-unlispify-tag-name symbol))
2752 (custom-menu-filter custom-face-menu
2753 widget)
2754 event)))
2755 (if answer
2756 (funcall answer widget)))))
2757
2758 (defun custom-face-set (widget)
2759 "Make the face attributes in WIDGET take effect."
2760 (let* ((symbol (widget-value widget))
2761 (child (car (widget-get widget :children)))
2762 (value (widget-value child))
2763 (comment-widget (widget-get widget :comment-widget))
2764 (comment (widget-value comment-widget)))
2765 (when (equal comment "")
2766 (setq comment nil)
2767 ;; Make the comment invisible by hand if it's empty
2768 (custom-comment-hide comment-widget))
2769 (put symbol 'customized-face value)
2770 (face-spec-set symbol value)
2771 (put symbol 'customized-face-comment comment)
2772 (put symbol 'face-comment comment)
2773 (custom-face-state-set widget)
2774 (custom-redraw-magic widget)))
2775
2776 (defun custom-face-save-command (widget)
2777 "Save in `.emacs' the face attributes in WIDGET."
2778 (custom-face-save widget)
2779 (custom-save-all))
2780
2781 (defun custom-face-save (widget)
2782 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
2783 (let* ((symbol (widget-value widget))
2784 (child (car (widget-get widget :children)))
2785 (value (widget-value child))
2786 (comment-widget (widget-get widget :comment-widget))
2787 (comment (widget-value comment-widget)))
2788 (when (equal comment "")
2789 (setq comment nil)
2790 ;; Make the comment invisible by hand if it's empty
2791 (custom-comment-hide comment-widget))
2792 (face-spec-set symbol value)
2793 (put symbol 'saved-face value)
2794 (put symbol 'customized-face nil)
2795 (put symbol 'face-comment comment)
2796 (put symbol 'customized-face-comment nil)
2797 (put symbol 'saved-face-comment comment)
2798 (custom-save-all)
2799 (custom-face-state-set widget)
2800 (custom-redraw-magic widget)))
2801
2802 (defun custom-face-reset-saved (widget)
2803 "Restore WIDGET to the face's default attributes."
2804 (let* ((symbol (widget-value widget))
2805 (child (car (widget-get widget :children)))
2806 (value (get symbol 'saved-face))
2807 (comment (get symbol 'saved-face-comment))
2808 (comment-widget (widget-get widget :comment-widget)))
2809 (unless (or value comment)
2810 (error "No saved value for this face"))
2811 (put symbol 'customized-face nil)
2812 (put symbol 'customized-face-comment nil)
2813 (face-spec-set symbol value)
2814 (put symbol 'face-comment comment)
2815 (widget-value-set child value)
2816 ;; This call manages the comment visibility
2817 (widget-value-set comment-widget (or comment ""))
2818 (custom-face-state-set widget)
2819 (custom-redraw-magic widget)))
2820
2821 (defun custom-face-reset-standard (widget)
2822 "Restore WIDGET to the face's standard settings."
2823 (let* ((symbol (widget-value widget))
2824 (child (car (widget-get widget :children)))
2825 (value (get symbol 'face-defface-spec))
2826 (comment-widget (widget-get widget :comment-widget)))
2827 (unless value
2828 (error "No standard setting for this face"))
2829 (put symbol 'customized-face nil)
2830 (put symbol 'customized-face-comment nil)
2831 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
2832 (put symbol 'saved-face nil)
2833 (put symbol 'saved-face-comment nil)
2834 (custom-save-all))
2835 (face-spec-set symbol value)
2836 (put symbol 'face-comment nil)
2837 (widget-value-set child value)
2838 ;; This call manages the comment visibility
2839 (widget-value-set comment-widget "")
2840 (custom-face-state-set widget)
2841 (custom-redraw-magic widget)))
2842
2843 ;;; The `face' Widget.
2844
2845 (define-widget 'face 'default
2846 "Select and customize a face."
2847 :convert-widget 'widget-value-convert-widget
2848 :button-prefix 'widget-push-button-prefix
2849 :button-suffix 'widget-push-button-suffix
2850 :format "%t: %[select face%] %v"
2851 :tag "Face"
2852 :value 'default
2853 :value-create 'widget-face-value-create
2854 :value-delete 'widget-face-value-delete
2855 :value-get 'widget-value-value-get
2856 :validate 'widget-children-validate
2857 :action 'widget-face-action
2858 :match '(lambda (widget value) (symbolp value)))
2859
2860 (defun widget-face-value-create (widget)
2861 ;; Create a `custom-face' child.
2862 (let* ((symbol (widget-value widget))
2863 (custom-buffer-style 'face)
2864 (child (widget-create-child-and-convert
2865 widget 'custom-face
2866 :custom-level nil
2867 :value symbol)))
2868 (custom-magic-reset child)
2869 (setq custom-options (cons child custom-options))
2870 (widget-put widget :children (list child))))
2871
2872 (defun widget-face-value-delete (widget)
2873 ;; Remove the child from the options.
2874 (let ((child (car (widget-get widget :children))))
2875 (setq custom-options (delq child custom-options))
2876 (widget-children-value-delete widget)))
2877
2878 (defvar face-history nil
2879 "History of entered face names.")
2880
2881 (defun widget-face-action (widget &optional event)
2882 "Prompt for a face."
2883 (let ((answer (completing-read "Face: "
2884 (mapcar (lambda (face)
2885 (list (symbol-name face)))
2886 (face-list))
2887 nil nil nil
2888 'face-history)))
2889 (unless (zerop (length answer))
2890 (widget-value-set widget (intern answer))
2891 (widget-apply widget :notify widget event)
2892 (widget-setup))))
2893
2894 ;;; The `hook' Widget.
2895
2896 (define-widget 'hook 'list
2897 "A emacs lisp hook"
2898 :value-to-internal (lambda (widget value)
2899 (if (and value (symbolp value))
2900 (list value)
2901 value))
2902 :match (lambda (widget value)
2903 (or (symbolp value)
2904 (widget-group-match widget value)))
2905 :convert-widget 'custom-hook-convert-widget
2906 :tag "Hook")
2907
2908 (defun custom-hook-convert-widget (widget)
2909 ;; Handle `:custom-options'.
2910 (let* ((options (widget-get widget :options))
2911 (other `(editable-list :inline t
2912 :entry-format "%i %d%v"
2913 (function :format " %v")))
2914 (args (if options
2915 (list `(checklist :inline t
2916 ,@(mapcar (lambda (entry)
2917 `(function-item ,entry))
2918 options))
2919 other)
2920 (list other))))
2921 (widget-put widget :args args)
2922 widget))
2923
2924 ;;; The `custom-group-link' Widget.
2925
2926 (define-widget 'custom-group-link 'link
2927 "Show parent in other window when activated."
2928 :help-echo "Create customization buffer for this group."
2929 :action 'custom-group-link-action)
2930
2931 (defun custom-group-link-action (widget &rest ignore)
2932 (customize-group (widget-value widget)))
2933
2934 ;;; The `custom-group' Widget.
2935
2936 (defcustom custom-group-tag-faces nil
2937 ;; In XEmacs, this ought to play games with font size.
2938 ;; Fixme: make it do so in Emacs.
2939 "Face used for group tags.
2940 The first member is used for level 1 groups, the second for level 2,
2941 and so forth. The remaining group tags are shown with
2942 `custom-group-tag-face'."
2943 :type '(repeat face)
2944 :group 'custom-faces)
2945
2946 (defface custom-group-tag-face-1 '((((class color)
2947 (background dark))
2948 (:foreground "pink" :underline t))
2949 (((class color)
2950 (background light))
2951 (:foreground "red" :underline t))
2952 (t (:underline t)))
2953 "Face used for group tags.")
2954
2955 (defface custom-group-tag-face '((((class color)
2956 (background dark))
2957 (:foreground "light blue" :underline t))
2958 (((class color)
2959 (background light))
2960 (:foreground "blue" :underline t))
2961 (t (:underline t)))
2962 "Face used for low level group tags."
2963 :group 'custom-faces)
2964
2965 (define-widget 'custom-group 'custom
2966 "Customize group."
2967 :format "%v"
2968 :sample-face-get 'custom-group-sample-face-get
2969 :documentation-property 'group-documentation
2970 :help-echo "Set or reset all members of this group."
2971 :value-create 'custom-group-value-create
2972 :action 'custom-group-action
2973 :custom-category 'group
2974 :custom-set 'custom-group-set
2975 :custom-save 'custom-group-save
2976 :custom-reset-current 'custom-group-reset-current
2977 :custom-reset-saved 'custom-group-reset-saved
2978 :custom-reset-standard 'custom-group-reset-standard
2979 :custom-menu 'custom-group-menu-create)
2980
2981 (defun custom-group-sample-face-get (widget)
2982 ;; Use :sample-face.
2983 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2984 'custom-group-tag-face))
2985
2986 (define-widget 'custom-group-visibility 'visibility
2987 "An indicator and manipulator for hidden group contents."
2988 :create 'custom-group-visibility-create)
2989
2990 (defun custom-group-visibility-create (widget)
2991 (let ((visible (widget-value widget)))
2992 (if visible
2993 (insert "--------")))
2994 (widget-default-create widget))
2995
2996 (defun custom-group-members (symbol groups-only)
2997 "Return SYMBOL's custom group members.
2998 If GROUPS-ONLY non-nil, return only those members that are groups."
2999 (if (not groups-only)
3000 (get symbol 'custom-group)
3001 (let (members)
3002 (dolist (entry (get symbol 'custom-group))
3003 (when (eq (nth 1 entry) 'custom-group)
3004 (push entry members)))
3005 (nreverse members))))
3006
3007 (defun custom-group-value-create (widget)
3008 "Insert a customize group for WIDGET in the current buffer."
3009 (let* ((state (widget-get widget :custom-state))
3010 (level (widget-get widget :custom-level))
3011 ;; (indent (widget-get widget :indent))
3012 (prefix (widget-get widget :custom-prefix))
3013 (buttons (widget-get widget :buttons))
3014 (tag (widget-get widget :tag))
3015 (symbol (widget-value widget))
3016 (members (custom-group-members symbol
3017 (and (eq custom-buffer-style 'tree)
3018 custom-browse-only-groups))))
3019 (cond ((and (eq custom-buffer-style 'tree)
3020 (eq state 'hidden)
3021 (or members (custom-unloaded-widget-p widget)))
3022 (custom-browse-insert-prefix prefix)
3023 (push (widget-create-child-and-convert
3024 widget 'custom-browse-visibility
3025 ;; :tag-glyph "plus"
3026 :tag "+")
3027 buttons)
3028 (insert "-- ")
3029 ;; (widget-glyph-insert nil "-- " "horizontal")
3030 (push (widget-create-child-and-convert
3031 widget 'custom-browse-group-tag)
3032 buttons)
3033 (insert " " tag "\n")
3034 (widget-put widget :buttons buttons))
3035 ((and (eq custom-buffer-style 'tree)
3036 (zerop (length members)))
3037 (custom-browse-insert-prefix prefix)
3038 (insert "[ ]-- ")
3039 ;; (widget-glyph-insert nil "[ ]" "empty")
3040 ;; (widget-glyph-insert nil "-- " "horizontal")
3041 (push (widget-create-child-and-convert
3042 widget 'custom-browse-group-tag)
3043 buttons)
3044 (insert " " tag "\n")
3045 (widget-put widget :buttons buttons))
3046 ((eq custom-buffer-style 'tree)
3047 (custom-browse-insert-prefix prefix)
3048 (custom-load-widget widget)
3049 (if (zerop (length members))
3050 (progn
3051 (custom-browse-insert-prefix prefix)
3052 (insert "[ ]-- ")
3053 ;; (widget-glyph-insert nil "[ ]" "empty")
3054 ;; (widget-glyph-insert nil "-- " "horizontal")
3055 (push (widget-create-child-and-convert
3056 widget 'custom-browse-group-tag)
3057 buttons)
3058 (insert " " tag "\n")
3059 (widget-put widget :buttons buttons))
3060 (push (widget-create-child-and-convert
3061 widget 'custom-browse-visibility
3062 ;; :tag-glyph "minus"
3063 :tag "-")
3064 buttons)
3065 (insert "-\\ ")
3066 ;; (widget-glyph-insert nil "-\\ " "top")
3067 (push (widget-create-child-and-convert
3068 widget 'custom-browse-group-tag)
3069 buttons)
3070 (insert " " tag "\n")
3071 (widget-put widget :buttons buttons)
3072 (message "Creating group...")
3073 (let* ((members (custom-sort-items members
3074 custom-browse-sort-alphabetically
3075 custom-browse-order-groups))
3076 (prefixes (widget-get widget :custom-prefixes))
3077 (custom-prefix-list (custom-prefix-add symbol prefixes))
3078 (extra-prefix (if (widget-get widget :custom-last)
3079 " "
3080 " | "))
3081 (prefix (concat prefix extra-prefix))
3082 children entry)
3083 (while members
3084 (setq entry (car members)
3085 members (cdr members))
3086 (push (widget-create-child-and-convert
3087 widget (nth 1 entry)
3088 :group widget
3089 :tag (custom-unlispify-tag-name (nth 0 entry))
3090 :custom-prefixes custom-prefix-list
3091 :custom-level (1+ level)
3092 :custom-last (null members)
3093 :value (nth 0 entry)
3094 :custom-prefix prefix)
3095 children))
3096 (widget-put widget :children (reverse children)))
3097 (message "Creating group...done")))
3098 ;; Nested style.
3099 ((eq state 'hidden)
3100 ;; Create level indicator.
3101 (unless (eq custom-buffer-style 'links)
3102 (insert-char ?\ (* custom-buffer-indent (1- level)))
3103 (insert "-- "))
3104 ;; Create tag.
3105 (let ((begin (point)))
3106 (insert tag)
3107 (widget-specify-sample widget begin (point)))
3108 (insert " group: ")
3109 ;; Create link/visibility indicator.
3110 (if (eq custom-buffer-style 'links)
3111 (push (widget-create-child-and-convert
3112 widget 'custom-group-link
3113 :tag "Go to Group"
3114 symbol)
3115 buttons)
3116 (push (widget-create-child-and-convert
3117 widget 'custom-group-visibility
3118 :help-echo "Show members of this group."
3119 :action 'custom-toggle-parent
3120 (not (eq state 'hidden)))
3121 buttons))
3122 (insert " \n")
3123 ;; Create magic button.
3124 (let ((magic (widget-create-child-and-convert
3125 widget 'custom-magic nil)))
3126 (widget-put widget :custom-magic magic)
3127 (push magic buttons))
3128 ;; Update buttons.
3129 (widget-put widget :buttons buttons)
3130 ;; Insert documentation.
3131 (if (and (eq custom-buffer-style 'links) (> level 1))
3132 (widget-put widget :documentation-indent 0))
3133 (widget-default-format-handler widget ?h))
3134 ;; Nested style.
3135 (t ;Visible.
3136 ;; Add parent groups references above the group.
3137 (if t ;;; This should test that the buffer
3138 ;;; was made to display a group.
3139 (when (eq level 1)
3140 (if (custom-add-parent-links widget
3141 "Go to parent group:")
3142 (insert "\n"))))
3143 ;; Create level indicator.
3144 (insert-char ?\ (* custom-buffer-indent (1- level)))
3145 (insert "/- ")
3146 ;; Create tag.
3147 (let ((start (point)))
3148 (insert tag)
3149 (widget-specify-sample widget start (point)))
3150 (insert " group: ")
3151 ;; Create visibility indicator.
3152 (unless (eq custom-buffer-style 'links)
3153 (insert "--------")
3154 (push (widget-create-child-and-convert
3155 widget 'visibility
3156 :help-echo "Hide members of this group."
3157 :action 'custom-toggle-parent
3158 (not (eq state 'hidden)))
3159 buttons)
3160 (insert " "))
3161 ;; Create more dashes.
3162 ;; Use 76 instead of 75 to compensate for the temporary "<"
3163 ;; added by `widget-insert'.
3164 (insert-char ?- (- 76 (current-column)
3165 (* custom-buffer-indent level)))
3166 (insert "\\\n")
3167 ;; Create magic button.
3168 (let ((magic (widget-create-child-and-convert
3169 widget 'custom-magic
3170 :indent 0
3171 nil)))
3172 (widget-put widget :custom-magic magic)
3173 (push magic buttons))
3174 ;; Update buttons.
3175 (widget-put widget :buttons buttons)
3176 ;; Insert documentation.
3177 (widget-default-format-handler widget ?h)
3178 ;; Parent groups.
3179 (if nil ;;; This should test that the buffer
3180 ;;; was not made to display a group.
3181 (when (eq level 1)
3182 (insert-char ?\ custom-buffer-indent)
3183 (custom-add-parent-links widget)))
3184 (custom-add-see-also widget
3185 (make-string (* custom-buffer-indent level)
3186 ?\ ))
3187 ;; Members.
3188 (message "Creating group...")
3189 (custom-load-widget widget)
3190 (let* ((members (custom-sort-items members
3191 custom-buffer-sort-alphabetically
3192 custom-buffer-order-groups))
3193 (prefixes (widget-get widget :custom-prefixes))
3194 (custom-prefix-list (custom-prefix-add symbol prefixes))
3195 (length (length members))
3196 (count 0)
3197 (children (mapcar (lambda (entry)
3198 (widget-insert "\n")
3199 (message "\
3200 Creating group members... %2d%%"
3201 (/ (* 100.0 count) length))
3202 (setq count (1+ count))
3203 (prog1
3204 (widget-create-child-and-convert
3205 widget (nth 1 entry)
3206 :group widget
3207 :tag (custom-unlispify-tag-name
3208 (nth 0 entry))
3209 :custom-prefixes custom-prefix-list
3210 :custom-level (1+ level)
3211 :value (nth 0 entry))
3212 (unless (eq (preceding-char) ?\n)
3213 (widget-insert "\n"))))
3214 members)))
3215 (message "Creating group magic...")
3216 (mapcar 'custom-magic-reset children)
3217 (message "Creating group state...")
3218 (widget-put widget :children children)
3219 (custom-group-state-update widget)
3220 (message "Creating group... done"))
3221 ;; End line
3222 (insert "\n")
3223 (insert-char ?\ (* custom-buffer-indent (1- level)))
3224 (insert "\\- " (widget-get widget :tag) " group end ")
3225 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
3226 (insert "/\n")))))
3227
3228 (defvar custom-group-menu
3229 '(("Set for Current Session" custom-group-set
3230 (lambda (widget)
3231 (eq (widget-get widget :custom-state) 'modified)))
3232 ("Save for Future Sessions" custom-group-save
3233 (lambda (widget)
3234 (memq (widget-get widget :custom-state) '(modified set))))
3235 ("Reset to Current" custom-group-reset-current
3236 (lambda (widget)
3237 (memq (widget-get widget :custom-state) '(modified))))
3238 ("Reset to Saved" custom-group-reset-saved
3239 (lambda (widget)
3240 (memq (widget-get widget :custom-state) '(modified set))))
3241 ("Reset to standard setting" custom-group-reset-standard
3242 (lambda (widget)
3243 (memq (widget-get widget :custom-state) '(modified set saved)))))
3244 "Alist of actions for the `custom-group' widget.
3245 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3246 the menu entry, ACTION is the function to call on the widget when the
3247 menu is selected, and FILTER is a predicate which takes a `custom-group'
3248 widget as an argument, and returns non-nil if ACTION is valid on that
3249 widget. If FILTER is nil, ACTION is always valid.")
3250
3251 (defun custom-group-action (widget &optional event)
3252 "Show the menu for `custom-group' WIDGET.
3253 Optional EVENT is the location for the menu."
3254 (if (eq (widget-get widget :custom-state) 'hidden)
3255 (custom-toggle-hide widget)
3256 (let* ((completion-ignore-case t)
3257 (answer (widget-choose (concat "Operation on "
3258 (custom-unlispify-tag-name
3259 (widget-get widget :value)))
3260 (custom-menu-filter custom-group-menu
3261 widget)
3262 event)))
3263 (if answer
3264 (funcall answer widget)))))
3265
3266 (defun custom-group-set (widget)
3267 "Set changes in all modified group members."
3268 (let ((children (widget-get widget :children)))
3269 (mapcar (lambda (child)
3270 (when (eq (widget-get child :custom-state) 'modified)
3271 (widget-apply child :custom-set)))
3272 children )))
3273
3274 (defun custom-group-save (widget)
3275 "Save all modified group members."
3276 (let ((children (widget-get widget :children)))
3277 (mapcar (lambda (child)
3278 (when (memq (widget-get child :custom-state) '(modified set))
3279 (widget-apply child :custom-save)))
3280 children )))
3281
3282 (defun custom-group-reset-current (widget)
3283 "Reset all modified group members."
3284 (let ((children (widget-get widget :children)))
3285 (mapcar (lambda (child)
3286 (when (eq (widget-get child :custom-state) 'modified)
3287 (widget-apply child :custom-reset-current)))
3288 children )))
3289
3290 (defun custom-group-reset-saved (widget)
3291 "Reset all modified or set group members."
3292 (let ((children (widget-get widget :children)))
3293 (mapcar (lambda (child)
3294 (when (memq (widget-get child :custom-state) '(modified set))
3295 (widget-apply child :custom-reset-saved)))
3296 children )))
3297
3298 (defun custom-group-reset-standard (widget)
3299 "Reset all modified, set, or saved group members."
3300 (let ((children (widget-get widget :children)))
3301 (mapcar (lambda (child)
3302 (when (memq (widget-get child :custom-state)
3303 '(modified set saved))
3304 (widget-apply child :custom-reset-standard)))
3305 children )))
3306
3307 (defun custom-group-state-update (widget)
3308 "Update magic."
3309 (unless (eq (widget-get widget :custom-state) 'hidden)
3310 (let* ((children (widget-get widget :children))
3311 (states (mapcar (lambda (child)
3312 (widget-get child :custom-state))
3313 children))
3314 (magics custom-magic-alist)
3315 (found 'standard))
3316 (while magics
3317 (let ((magic (car (car magics))))
3318 (if (and (not (eq magic 'hidden))
3319 (memq magic states))
3320 (setq found magic
3321 magics nil)
3322 (setq magics (cdr magics)))))
3323 (widget-put widget :custom-state found)))
3324 (custom-magic-reset widget))
3325
3326 ;;; The `custom-save-all' Function.
3327 ;;;###autoload
3328 (defcustom custom-file nil
3329 "File used for storing customization information.
3330 The default is nil, which means to use your init file
3331 as specified by `user-init-file'. If you specify some other file,
3332 you need to explicitly load that file for the settings to take effect."
3333 :type '(choice (const :tag "Your Emacs init file" nil) file)
3334 :group 'customize)
3335
3336 (defun custom-file ()
3337 "Return the file name for saving customizations."
3338 (setq custom-file
3339 (or custom-file
3340 user-init-file
3341 (read-file-name "File for customizations: "
3342 "~/" nil nil ".emacs"))))
3343
3344 (defun custom-save-delete (symbol)
3345 "Delete the call to SYMBOL from `custom-file'.
3346 Leave point at the location of the call, or after the last expression."
3347 (let ((default-major-mode))
3348 (set-buffer (find-file-noselect (custom-file))))
3349 (goto-char (point-min))
3350 ;; Skip all whitespace and comments.
3351 (while (forward-comment 1))
3352 (or (eobp)
3353 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
3354 (catch 'found
3355 (while t
3356 ;; Skip all whitespace and comments.
3357 (while (forward-comment 1))
3358 (let ((start (point))
3359 (sexp (condition-case nil
3360 (read (current-buffer))
3361 (end-of-file (throw 'found nil)))))
3362 (when (and (listp sexp)
3363 (eq (car sexp) symbol))
3364 (delete-region start (point))
3365 (throw 'found nil))))))
3366
3367 (defun custom-save-variables ()
3368 "Save all customized variables in `custom-file'."
3369 (save-excursion
3370 (custom-save-delete 'custom-set-variables)
3371 (let ((standard-output (current-buffer))
3372 (saved-list (make-list 1 0))
3373 sort-fold-case)
3374 ;; First create a sorted list of saved variables.
3375 (mapatoms
3376 (lambda (symbol)
3377 (if (get symbol 'saved-value)
3378 (nconc saved-list (list symbol)))))
3379 (setq saved-list (sort (cdr saved-list) 'string<))
3380 (unless (bolp)
3381 (princ "\n"))
3382 (princ "(custom-set-variables")
3383 (mapcar
3384 (lambda (symbol)
3385 (let ((value (get symbol 'saved-value))
3386 (requests (get symbol 'custom-requests))
3387 (now (not (or (get symbol 'standard-value)
3388 (and (not (boundp symbol))
3389 (not (get symbol 'force-value))))))
3390 (comment (get symbol 'saved-variable-comment))
3391 sep)
3392 (when (or value comment)
3393 (princ "\n '(")
3394 (prin1 symbol)
3395 (princ " ")
3396 (prin1 (car value))
3397 (cond ((or now requests comment)
3398 (princ " ")
3399 (if now
3400 (princ "t")
3401 (princ "nil"))
3402 (cond ((or requests comment)
3403 (princ " ")
3404 (if requests
3405 (prin1 requests)
3406 (princ "nil"))
3407 (cond (comment
3408 (princ " ")
3409 (prin1 comment)
3410 (princ ")"))
3411 (t
3412 (princ ")"))))
3413 (t
3414 (princ ")"))))
3415 (t
3416 (princ ")"))))))
3417 saved-list)
3418 (princ ")")
3419 (unless (looking-at "\n")
3420 (princ "\n")))))
3421
3422 (defun custom-save-faces ()
3423 "Save all customized faces in `custom-file'."
3424 (save-excursion
3425 (custom-save-delete 'custom-set-faces)
3426 (let ((standard-output (current-buffer))
3427 (saved-list (make-list 1 0))
3428 sort-fold-case)
3429 ;; First create a sorted list of saved faces.
3430 (mapatoms
3431 (lambda (symbol)
3432 (if (get symbol 'saved-face)
3433 (nconc saved-list (list symbol)))))
3434 (setq saved-list (sort (cdr saved-list) 'string<))
3435 ;; The default face must be first, since it affects the others.
3436 (if (memq 'default saved-list)
3437 (setq saved-list (cons 'default (delq 'default saved-list))))
3438 (unless (bolp)
3439 (princ "\n"))
3440 (princ "(custom-set-faces")
3441 (mapcar
3442 (lambda (symbol)
3443 (let ((value (get symbol 'saved-face))
3444 (now (not (or (get 'default 'face-defface-spec)
3445 (and (not (custom-facep 'default))
3446 (not (get 'default 'force-face))))))
3447 (comment (get 'default 'saved-face-comment)))
3448 (unless (eq symbol 'default))
3449 ;; Don't print default face here.
3450 (princ "\n '(")
3451 (prin1 symbol)
3452 (princ " ")
3453 (prin1 value)
3454 (cond ((or now comment)
3455 (princ " ")
3456 (if now
3457 (princ "t")
3458 (princ "nil"))
3459 (cond (comment
3460 (princ " ")
3461 (prin1 comment)
3462 (princ ")"))
3463 (t
3464 (princ ")"))))
3465 (t
3466 (princ ")")))))
3467 saved-list)
3468 (princ ")")
3469 (unless (looking-at "\n")
3470 (princ "\n")))))
3471
3472 ;;;###autoload
3473 (defun customize-save-customized ()
3474 "Save all user options which have been set in this session."
3475 (interactive)
3476 (mapatoms (lambda (symbol)
3477 (let ((face (get symbol 'customized-face))
3478 (value (get symbol 'customized-value))
3479 (face-comment (get symbol 'customized-face-comment))
3480 (variable-comment
3481 (get symbol 'customized-variable-comment)))
3482 (when face
3483 (put symbol 'saved-face face)
3484 (put symbol 'customized-face nil))
3485 (when value
3486 (put symbol 'saved-value value)
3487 (put symbol 'customized-value nil))
3488 (when variable-comment
3489 (put symbol 'saved-variable-comment variable-comment)
3490 (put symbol 'customized-variable-comment nil))
3491 (when face-comment
3492 (put symbol 'saved-face-comment face-comment)
3493 (put symbol 'customized-face-comment nil)))))
3494 ;; We really should update all custom buffers here.
3495 (custom-save-all))
3496
3497 ;;;###autoload
3498 (defun custom-save-all ()
3499 "Save all customizations in `custom-file'."
3500 (let ((inhibit-read-only t))
3501 (custom-save-variables)
3502 (custom-save-faces)
3503 (save-excursion
3504 (let ((default-major-mode nil))
3505 (set-buffer (find-file-noselect (custom-file))))
3506 (save-buffer))))
3507
3508 ;;; The Customize Menu.
3509
3510 ;;; Menu support
3511
3512 (defcustom custom-menu-nesting 2
3513 "Maximum nesting in custom menus."
3514 :type 'integer
3515 :group 'custom-menu)
3516
3517 (defun custom-face-menu-create (widget symbol)
3518 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3519 (vector (custom-unlispify-menu-entry symbol)
3520 `(customize-face ',symbol)
3521 t))
3522
3523 (defun custom-variable-menu-create (widget symbol)
3524 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3525 (let ((type (get symbol 'custom-type)))
3526 (unless (listp type)
3527 (setq type (list type)))
3528 (if (and type (widget-get type :custom-menu))
3529 (widget-apply type :custom-menu symbol)
3530 (vector (custom-unlispify-menu-entry symbol)
3531 `(customize-variable ',symbol)
3532 t))))
3533
3534 ;; Add checkboxes to boolean variable entries.
3535 (widget-put (get 'boolean 'widget-type)
3536 :custom-menu (lambda (widget symbol)
3537 (vector (custom-unlispify-menu-entry symbol)
3538 `(customize-variable ',symbol)
3539 ':style 'toggle
3540 ':selected symbol)))
3541
3542 ;; Fixme: sort out use of :filter in Emacs
3543 (if nil ; (string-match "XEmacs" emacs-version)
3544 ;; XEmacs can create menus dynamically.
3545 (defun custom-group-menu-create (widget symbol)
3546 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3547 `( ,(custom-unlispify-menu-entry symbol t)
3548 :filter (lambda (&rest junk)
3549 (cdr (custom-menu-create ',symbol)))))
3550 ;; But emacs can't.
3551 (defun custom-group-menu-create (widget symbol)
3552 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3553 ;; Limit the nesting.
3554 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3555 (custom-menu-create symbol))))
3556
3557 ;;;###autoload
3558 (defun custom-menu-create (symbol)
3559 "Create menu for customization group SYMBOL.
3560 The menu is in a format applicable to `easy-menu-define'."
3561 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3562 `(customize-group ',symbol)
3563 t)))
3564 (if (and (or (not (boundp 'custom-menu-nesting))
3565 (>= custom-menu-nesting 0))
3566 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3567 (let ((custom-prefix-list (custom-prefix-add symbol
3568 custom-prefix-list))
3569 (members (custom-sort-items (get symbol 'custom-group)
3570 custom-menu-sort-alphabetically
3571 custom-menu-order-groups)))
3572 (custom-load-symbol symbol)
3573 `(,(custom-unlispify-menu-entry symbol t)
3574 ,item
3575 "--"
3576 ,@(mapcar (lambda (entry)
3577 (widget-apply (if (listp (nth 1 entry))
3578 (nth 1 entry)
3579 (list (nth 1 entry)))
3580 :custom-menu (nth 0 entry)))
3581 members)))
3582 item)))
3583
3584 ;;;###autoload
3585 (defun customize-menu-create (symbol &optional name)
3586 "Return a customize menu for customization group SYMBOL.
3587 If optional NAME is given, use that as the name of the menu.
3588 Otherwise the menu will be named `Customize'.
3589 The format is suitable for use with `easy-menu-define'."
3590 (unless name
3591 (setq name "Customize"))
3592 ;; Fixme: sort out use of :filter in Emacs
3593 (if nil ;(string-match "XEmacs" emacs-version)
3594 ;; We can delay it under XEmacs.
3595 `(,name
3596 :filter (lambda (&rest junk)
3597 (cdr (custom-menu-create ',symbol))))
3598 ;; But we must create it now under Emacs.
3599 (cons name (cdr (custom-menu-create symbol)))))
3600
3601 ;;; The Custom Mode.
3602
3603 (defvar custom-mode-map nil
3604 "Keymap for `custom-mode'.")
3605
3606 (unless custom-mode-map
3607 (setq custom-mode-map (make-sparse-keymap))
3608 (set-keymap-parent custom-mode-map widget-keymap)
3609 (suppress-keymap custom-mode-map)
3610 (define-key custom-mode-map " " 'scroll-up)
3611 (define-key custom-mode-map "\177" 'scroll-down)
3612 (define-key custom-mode-map "q" 'Custom-buffer-done)
3613 (define-key custom-mode-map "u" 'Custom-goto-parent)
3614 (define-key custom-mode-map "n" 'widget-forward)
3615 (define-key custom-mode-map "p" 'widget-backward)
3616 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3617
3618 (defun Custom-move-and-invoke (event)
3619 "Move to where you click, and if it is an active field, invoke it."
3620 (interactive "e")
3621 (mouse-set-point event)
3622 (if (widget-event-point event)
3623 (let* ((pos (widget-event-point event))
3624 (button (get-char-property pos 'button)))
3625 (if button
3626 (widget-button-click event)))))
3627
3628 (easy-menu-define Custom-mode-menu
3629 custom-mode-map
3630 "Menu used in customization buffers."
3631 `("Custom"
3632 ,(customize-menu-create 'customize)
3633 ["Set" Custom-set t]
3634 ["Save" Custom-save t]
3635 ["Reset to Current" Custom-reset-current t]
3636 ["Reset to Saved" Custom-reset-saved t]
3637 ["Reset to Standard Settings" Custom-reset-standard t]
3638 ["Info" (Info-goto-node "(emacs)Easy Customization") t]))
3639
3640 (defun Custom-goto-parent ()
3641 "Go to the parent group listed at the top of this buffer.
3642 If several parents are listed, go to the first of them."
3643 (interactive)
3644 (save-excursion
3645 (goto-char (point-min))
3646 (if (search-forward "\nGo to parent group: " nil t)
3647 (let* ((button (get-char-property (point) 'button))
3648 (parent (downcase (widget-get button :tag))))
3649 (customize-group parent)))))
3650
3651 (defcustom custom-mode-hook nil
3652 "Hook called when entering Custom mode."
3653 :type 'hook
3654 :group 'custom-buffer )
3655
3656 (defun custom-state-buffer-message (widget)
3657 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3658 (message "To install your edits, invoke [State] and choose the Set operation")))
3659
3660 (defun custom-mode ()
3661 "Major mode for editing customization buffers.
3662
3663 The following commands are available:
3664
3665 Move to next button or editable field. \\[widget-forward]
3666 Move to previous button or editable field. \\[widget-backward]
3667 \\<widget-field-keymap>\
3668 Complete content of editable text field. \\[widget-complete]
3669 \\<custom-mode-map>\
3670 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3671 Invoke button under point. \\[widget-button-press]
3672 Set all modifications. \\[Custom-set]
3673 Make all modifications default. \\[Custom-save]
3674 Reset all modified options. \\[Custom-reset-current]
3675 Reset all modified or set options. \\[Custom-reset-saved]
3676 Reset all options. \\[Custom-reset-standard]
3677
3678 Entry to this mode calls the value of `custom-mode-hook'
3679 if that value is non-nil."
3680 (kill-all-local-variables)
3681 (setq major-mode 'custom-mode
3682 mode-name "Custom")
3683 (use-local-map custom-mode-map)
3684 (easy-menu-add Custom-mode-menu)
3685 (make-local-variable 'custom-options)
3686 (make-local-variable 'widget-documentation-face)
3687 (setq widget-documentation-face 'custom-documentation-face)
3688 (make-local-variable 'widget-button-face)
3689 (setq widget-button-face 'custom-button-face)
3690 (set (make-local-variable 'widget-button-pressed-face)
3691 'custom-button-pressed-face)
3692 (set (make-local-variable 'widget-mouse-face)
3693 'custom-button-pressed-face) ; buttons `depress' when moused
3694 ;; When possible, use relief for buttons, not bracketing. This test
3695 ;; may not be optimal.
3696 (when custom-raised-buttons
3697 (set (make-local-variable 'widget-push-button-prefix) "")
3698 (set (make-local-variable 'widget-push-button-suffix) "")
3699 (set (make-local-variable 'widget-link-prefix) "")
3700 (set (make-local-variable 'widget-link-suffix) ""))
3701 (make-local-hook 'widget-edit-functions)
3702 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3703 (run-hooks 'custom-mode-hook))
3704
3705 ;;; The End.
3706
3707 (provide 'cus-edit)
3708
3709 ;;; cus-edit.el ends here