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