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