]> code.delx.au - gnu-emacs/blob - lisp/cus-edit.el
*** empty log message ***
[gnu-emacs] / lisp / cus-edit.el
1 ;;; cus-edit.el --- Tools for 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.9954
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)))) t))
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 (or (get symbol 'custom-loads)
813 (get symbol 'custom-group)))
814 t))))
815 (when (stringp group)
816 (if (string-equal "" group)
817 (setq group 'emacs)
818 (setq group (intern group))))
819 (or (get group 'custom-group)
820 (custom-load-symbol group))
821 (let ((name (format "*Customize Group: %s*"
822 (custom-unlispify-tag-name group))))
823 (if (get-buffer name)
824 (switch-to-buffer name)
825 (custom-buffer-create (list (list group 'custom-group))
826 name
827 (concat " for group "
828 (custom-unlispify-tag-name group))))))
829
830 ;;;###autoload
831 (defun customize-group-other-window (symbol)
832 "Customize SYMBOL, which must be a customization group."
833 (interactive (list (completing-read "Customize group: (default emacs) "
834 obarray
835 (lambda (symbol)
836 (get symbol 'custom-group))
837 t)))
838
839 (when (stringp symbol)
840 (if (string-equal "" symbol)
841 (setq symbol 'emacs)
842 (setq symbol (intern symbol))))
843 (custom-buffer-create-other-window
844 (list (list symbol 'custom-group))
845 (format "*Customize Group: %s*" (custom-unlispify-tag-name symbol))))
846
847 ;;;###autoload
848 (defalias 'customize-variable 'customize-option)
849
850 ;;;###autoload
851 (defun customize-option (symbol)
852 "Customize SYMBOL, which must be a user option variable."
853 (interactive (custom-variable-prompt))
854 (custom-buffer-create (list (list symbol 'custom-variable))
855 (format "*Customize Option: %s*"
856 (custom-unlispify-tag-name symbol))))
857
858 ;;;###autoload
859 (defun customize-changed-options (since-version)
860 "Customize all user option variables whose default values changed recently.
861 This means, in other words, variables and groups defined with a `:version'
862 option."
863 (interactive "sCustomize options changed, since version (default all versions): ")
864 (if (equal since-version "")
865 (setq since-version nil))
866 (let ((found nil))
867 (mapatoms (lambda (symbol)
868 (and (or (boundp symbol)
869 ;; For groups the previous test fails, this one
870 ;; could be used to determine if symbol is a
871 ;; group. Is there a better way for this?
872 (get symbol 'group-documentation))
873 (let ((version (get symbol 'custom-version)))
874 (and version
875 (or (null since-version)
876 (customize-version-lessp since-version version))))
877 (setq found
878 ;; We have to set the right thing here,
879 ;; depending if we have a group or a
880 ;; variable.
881 (if (get symbol 'group-documentation)
882 (cons (list symbol 'custom-group) found)
883 (cons (list symbol 'custom-variable) found))))))
884 (if (not found)
885 (error "No user options have changed defaults in recent Emacs versions")
886 (custom-buffer-create (custom-sort-items found t nil)
887 "*Customize Changed Options*"))))
888
889 (defun customize-version-lessp (version1 version2)
890 (let (major1 major2 minor1 minor2)
891 (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version1)
892 (setq major1 (read (match-string 1 version1)))
893 (setq minor1 (read (match-string 2 version1)))
894 (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version2)
895 (setq major2 (read (match-string 1 version2)))
896 (setq minor2 (read (match-string 2 version2)))
897 (or (< major1 major2)
898 (and (= major1 major2)
899 (< minor1 minor2)))))
900
901 ;;;###autoload
902 (defalias 'customize-variable-other-window 'customize-option-other-window)
903
904 ;;;###autoload
905 (defun customize-option-other-window (symbol)
906 "Customize SYMBOL, which must be a user option variable.
907 Show the buffer in another window, but don't select it."
908 (interactive (custom-variable-prompt))
909 (custom-buffer-create-other-window
910 (list (list symbol 'custom-variable))
911 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
912
913 ;;;###autoload
914 (defun customize-face (&optional symbol)
915 "Customize SYMBOL, which should be a face name or nil.
916 If SYMBOL is nil, customize all faces."
917 (interactive (list (completing-read "Customize face: (default all) "
918 obarray 'custom-facep)))
919 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
920 (custom-buffer-create (custom-sort-items
921 (mapcar (lambda (symbol)
922 (list symbol 'custom-face))
923 (face-list))
924 t nil)
925 "*Customize Faces*")
926 (when (stringp symbol)
927 (setq symbol (intern symbol)))
928 (unless (symbolp symbol)
929 (error "Should be a symbol %S" symbol))
930 (custom-buffer-create (list (list symbol 'custom-face))
931 (format "*Customize Face: %s*"
932 (custom-unlispify-tag-name symbol)))))
933
934 ;;;###autoload
935 (defun customize-face-other-window (&optional symbol)
936 "Show customization buffer for FACE in other window."
937 (interactive (list (completing-read "Customize face: "
938 obarray 'custom-facep)))
939 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
940 ()
941 (if (stringp symbol)
942 (setq symbol (intern symbol)))
943 (unless (symbolp symbol)
944 (error "Should be a symbol %S" symbol))
945 (custom-buffer-create-other-window
946 (list (list symbol 'custom-face))
947 (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
948
949 ;;;###autoload
950 (defun customize-customized ()
951 "Customize all user options set since the last save in this session."
952 (interactive)
953 (let ((found nil))
954 (mapatoms (lambda (symbol)
955 (and (get symbol 'customized-face)
956 (custom-facep symbol)
957 (push (list symbol 'custom-face) found))
958 (and (get symbol 'customized-value)
959 (boundp symbol)
960 (push (list symbol 'custom-variable) found))))
961 (if (not found)
962 (error "No customized user options")
963 (custom-buffer-create (custom-sort-items found t nil)
964 "*Customize Customized*"))))
965
966 ;;;###autoload
967 (defun customize-saved ()
968 "Customize all already saved user options."
969 (interactive)
970 (let ((found nil))
971 (mapatoms (lambda (symbol)
972 (and (get symbol 'saved-face)
973 (custom-facep symbol)
974 (push (list symbol 'custom-face) found))
975 (and (get symbol 'saved-value)
976 (boundp symbol)
977 (push (list symbol 'custom-variable) found))))
978 (if (not found )
979 (error "No saved user options")
980 (custom-buffer-create (custom-sort-items found t nil)
981 "*Customize Saved*"))))
982
983 ;;;###autoload
984 (defun customize-apropos (regexp &optional all)
985 "Customize all user options matching REGEXP.
986 If ALL is `options', include only options.
987 If ALL is `faces', include only faces.
988 If ALL is `groups', include only groups.
989 If ALL is t (interactively, with prefix arg), include options which are not
990 user-settable, as well as faces and groups."
991 (interactive "sCustomize regexp: \nP")
992 (let ((found nil))
993 (mapatoms (lambda (symbol)
994 (when (string-match regexp (symbol-name symbol))
995 (when (and (not (memq all '(faces options)))
996 (get symbol 'custom-group))
997 (push (list symbol 'custom-group) found))
998 (when (and (not (memq all '(options groups)))
999 (custom-facep symbol))
1000 (push (list symbol 'custom-face) found))
1001 (when (and (not (memq all '(groups faces)))
1002 (boundp symbol)
1003 (or (get symbol 'saved-value)
1004 (get symbol 'standard-value)
1005 (if (memq all '(nil options))
1006 (user-variable-p symbol)
1007 (get symbol 'variable-documentation))))
1008 (push (list symbol 'custom-variable) found)))))
1009 (if (not found)
1010 (error "No matches")
1011 (custom-buffer-create (custom-sort-items found t
1012 custom-buffer-order-groups)
1013 "*Customize Apropos*"))))
1014
1015 ;;;###autoload
1016 (defun customize-apropos-options (regexp &optional arg)
1017 "Customize all user options matching REGEXP.
1018 With prefix arg, include options which are not user-settable."
1019 (interactive "sCustomize regexp: \nP")
1020 (customize-apropos regexp (or arg 'options)))
1021
1022 ;;;###autoload
1023 (defun customize-apropos-faces (regexp)
1024 "Customize all user faces matching REGEXP."
1025 (interactive "sCustomize regexp: \n")
1026 (customize-apropos regexp 'faces))
1027
1028 ;;;###autoload
1029 (defun customize-apropos-groups (regexp)
1030 "Customize all user groups matching REGEXP."
1031 (interactive "sCustomize regexp: \n")
1032 (customize-apropos regexp 'groups))
1033
1034 ;;; Buffer.
1035
1036 (defcustom custom-buffer-style 'links
1037 "Control the presentation style for customization buffers.
1038 The value should be a symbol, one of:
1039
1040 brackets: groups nest within each other with big horizontal brackets.
1041 links: groups have links to subgroups."
1042 :type '(radio (const brackets)
1043 (const links))
1044 :group 'custom-buffer)
1045
1046 (defcustom custom-buffer-indent 3
1047 "Number of spaces to indent nested groups."
1048 :type 'integer
1049 :group 'custom-buffer)
1050
1051 ;;;###autoload
1052 (defun custom-buffer-create (options &optional name description)
1053 "Create a buffer containing OPTIONS.
1054 Optional NAME is the name of the buffer.
1055 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1056 SYMBOL is a customization option, and WIDGET is a widget for editing
1057 that option."
1058 (unless name (setq name "*Customization*"))
1059 (kill-buffer (get-buffer-create name))
1060 (switch-to-buffer (get-buffer-create name))
1061 (custom-buffer-create-internal options description))
1062
1063 ;;;###autoload
1064 (defun custom-buffer-create-other-window (options &optional name description)
1065 "Create a buffer containing OPTIONS.
1066 Optional NAME is the name of the buffer.
1067 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1068 SYMBOL is a customization option, and WIDGET is a widget for editing
1069 that option."
1070 (unless name (setq name "*Customization*"))
1071 (kill-buffer (get-buffer-create name))
1072 (let ((window (selected-window)))
1073 (switch-to-buffer-other-window (get-buffer-create name))
1074 (custom-buffer-create-internal options description)
1075 (select-window window)))
1076
1077 (defcustom custom-reset-button-menu nil
1078 "If non-nil, only show a single reset button in customize buffers.
1079 This button will have a menu with all three reset operations."
1080 :type 'boolean
1081 :group 'custom-buffer)
1082
1083 (defun custom-buffer-create-internal (options &optional description)
1084 (message "Creating customization buffer...")
1085 (custom-mode)
1086 (widget-insert "This is a customization buffer")
1087 (if description
1088 (widget-insert description))
1089 (widget-insert ".
1090 Square brackets show active fields; type RET or click mouse-1
1091 on an active field to invoke its action. Editing an option value
1092 changes the text in the buffer; invoke the State button and
1093 choose the Set operation to set the option value.
1094 Invoke ")
1095 (widget-create 'info-link
1096 :tag "Help"
1097 :help-echo "Read the online help."
1098 "(emacs)Easy Customization")
1099 (widget-insert " for more information.\n\n")
1100 (message "Creating customization buttons...")
1101 (widget-insert "Operate on everything in this buffer:\n ")
1102 (widget-create 'push-button
1103 :tag "Set for Current Session"
1104 :help-echo "\
1105 Make your editing in this buffer take effect for this session."
1106 :action (lambda (widget &optional event)
1107 (Custom-set)))
1108 (widget-insert " ")
1109 (widget-create 'push-button
1110 :tag "Save for Future Sessions"
1111 :help-echo "\
1112 Make your editing in this buffer take effect for future Emacs sessions."
1113 :action (lambda (widget &optional event)
1114 (Custom-save)))
1115 (if custom-reset-button-menu
1116 (progn
1117 (widget-insert " ")
1118 (widget-create 'push-button
1119 :tag "Reset"
1120 :help-echo "Show a menu with reset operations."
1121 :mouse-down-action (lambda (&rest junk) t)
1122 :action (lambda (widget &optional event)
1123 (custom-reset event))))
1124 (widget-insert "\n ")
1125 (widget-create 'push-button
1126 :tag "Reset"
1127 :help-echo "\
1128 Reset all edited text in this buffer to reflect current values."
1129 :action 'Custom-reset-current)
1130 (widget-insert " ")
1131 (widget-create 'push-button
1132 :tag "Reset to Saved"
1133 :help-echo "\
1134 Reset all values in this buffer to their saved settings."
1135 :action 'Custom-reset-saved)
1136 (widget-insert " ")
1137 (widget-create 'push-button
1138 :tag "Reset to Standard"
1139 :help-echo "\
1140 Reset all values in this buffer to their standard settings."
1141 :action 'Custom-reset-standard))
1142 (widget-insert " ")
1143 (widget-create 'push-button
1144 :tag "Bury Buffer"
1145 :help-echo "Bury the buffer."
1146 :action (lambda (widget &optional event)
1147 (bury-buffer)))
1148 (widget-insert "\n\n")
1149 (message "Creating customization items...")
1150 (setq custom-options
1151 (if (= (length options) 1)
1152 (mapcar (lambda (entry)
1153 (widget-create (nth 1 entry)
1154 :documentation-shown t
1155 :custom-state 'unknown
1156 :tag (custom-unlispify-tag-name
1157 (nth 0 entry))
1158 :value (nth 0 entry)))
1159 options)
1160 (let ((count 0)
1161 (length (length options)))
1162 (mapcar (lambda (entry)
1163 (prog2
1164 (message "Creating customization items %2d%%..."
1165 (/ (* 100.0 count) length))
1166 (widget-create (nth 1 entry)
1167 :tag (custom-unlispify-tag-name
1168 (nth 0 entry))
1169 :value (nth 0 entry))
1170 (setq count (1+ count))
1171 (unless (eq (preceding-char) ?\n)
1172 (widget-insert "\n"))
1173 (widget-insert "\n")))
1174 options))))
1175 (unless (eq (preceding-char) ?\n)
1176 (widget-insert "\n"))
1177 (message "Creating customization items %2d%%...done" 100)
1178 (unless (eq custom-buffer-style 'tree)
1179 (mapcar 'custom-magic-reset custom-options))
1180 (message "Creating customization setup...")
1181 (widget-setup)
1182 (goto-char (point-min))
1183 (message "Creating customization buffer...done"))
1184
1185 ;;; The Tree Browser.
1186
1187 ;;;###autoload
1188 (defun customize-browse (&optional group)
1189 "Create a tree browser for the customize hierarchy."
1190 (interactive)
1191 (unless group
1192 (setq group 'emacs))
1193 (let ((name "*Customize Browser*"))
1194 (kill-buffer (get-buffer-create name))
1195 (switch-to-buffer (get-buffer-create name)))
1196 (custom-mode)
1197 (widget-insert "\
1198 Square brackets show active fields; type RET or click mouse-1
1199 on an active field to invoke its action.
1200 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1201 (if custom-browse-only-groups
1202 (widget-insert "\
1203 Invoke the [Group] button below to edit that item in another window.\n\n")
1204 (widget-insert "Invoke the ")
1205 (widget-create 'item
1206 :format "%t"
1207 :tag "[Group]"
1208 :tag-glyph "folder")
1209 (widget-insert ", ")
1210 (widget-create 'item
1211 :format "%t"
1212 :tag "[Face]"
1213 :tag-glyph "face")
1214 (widget-insert ", and ")
1215 (widget-create 'item
1216 :format "%t"
1217 :tag "[Option]"
1218 :tag-glyph "option")
1219 (widget-insert " buttons below to edit that
1220 item in another window.\n\n"))
1221 (let ((custom-buffer-style 'tree))
1222 (widget-create 'custom-group
1223 :custom-last t
1224 :custom-state 'unknown
1225 :tag (custom-unlispify-tag-name group)
1226 :value group))
1227 (goto-char (point-min)))
1228
1229 (define-widget 'custom-browse-visibility 'item
1230 "Control visibility of items in the customize tree browser."
1231 :format "%[[%t]%]"
1232 :action 'custom-browse-visibility-action)
1233
1234 (defun custom-browse-visibility-action (widget &rest ignore)
1235 (let ((custom-buffer-style 'tree))
1236 (custom-toggle-parent widget)))
1237
1238 (define-widget 'custom-browse-group-tag 'push-button
1239 "Show parent in other window when activated."
1240 :tag "Group"
1241 :tag-glyph "folder"
1242 :action 'custom-browse-group-tag-action)
1243
1244 (defun custom-browse-group-tag-action (widget &rest ignore)
1245 (let ((parent (widget-get widget :parent)))
1246 (customize-group-other-window (widget-value parent))))
1247
1248 (define-widget 'custom-browse-variable-tag 'push-button
1249 "Show parent in other window when activated."
1250 :tag "Option"
1251 :tag-glyph "option"
1252 :action 'custom-browse-variable-tag-action)
1253
1254 (defun custom-browse-variable-tag-action (widget &rest ignore)
1255 (let ((parent (widget-get widget :parent)))
1256 (customize-variable-other-window (widget-value parent))))
1257
1258 (define-widget 'custom-browse-face-tag 'push-button
1259 "Show parent in other window when activated."
1260 :tag "Face"
1261 :tag-glyph "face"
1262 :action 'custom-browse-face-tag-action)
1263
1264 (defun custom-browse-face-tag-action (widget &rest ignore)
1265 (let ((parent (widget-get widget :parent)))
1266 (customize-face-other-window (widget-value parent))))
1267
1268 (defconst custom-browse-alist '((" " "space")
1269 (" | " "vertical")
1270 ("-\\ " "top")
1271 (" |-" "middle")
1272 (" `-" "bottom")))
1273
1274 (defun custom-browse-insert-prefix (prefix)
1275 "Insert PREFIX. On XEmacs convert it to line graphics."
1276 (if nil ; (string-match "XEmacs" emacs-version)
1277 (progn
1278 (insert "*")
1279 (while (not (string-equal prefix ""))
1280 (let ((entry (substring prefix 0 3)))
1281 (setq prefix (substring prefix 3))
1282 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1283 (name (nth 1 (assoc entry custom-browse-alist))))
1284 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1285 (overlay-put overlay 'start-open t)
1286 (overlay-put overlay 'end-open t)))))
1287 (insert prefix)))
1288
1289 ;;; Modification of Basic Widgets.
1290 ;;
1291 ;; We add extra properties to the basic widgets needed here. This is
1292 ;; fine, as long as we are careful to stay within out own namespace.
1293 ;;
1294 ;; We want simple widgets to be displayed by default, but complex
1295 ;; widgets to be hidden.
1296
1297 (widget-put (get 'item 'widget-type) :custom-show t)
1298 (widget-put (get 'editable-field 'widget-type)
1299 :custom-show (lambda (widget value)
1300 (let ((pp (pp-to-string value)))
1301 (cond ((string-match "\n" pp)
1302 nil)
1303 ((> (length pp) 40)
1304 nil)
1305 (t t)))))
1306 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1307
1308 ;;; The `custom-manual' Widget.
1309
1310 (define-widget 'custom-manual 'info-link
1311 "Link to the manual entry for this customization option."
1312 :help-echo "Read the manual entry for this option."
1313 :tag "Manual")
1314
1315 ;;; The `custom-magic' Widget.
1316
1317 (defgroup custom-magic-faces nil
1318 "Faces used by the magic button."
1319 :group 'custom-faces
1320 :group 'custom-buffer)
1321
1322 (defface custom-invalid-face '((((class color))
1323 (:foreground "yellow" :background "red"))
1324 (t
1325 (:bold t :italic t :underline t)))
1326 "Face used when the customize item is invalid."
1327 :group 'custom-magic-faces)
1328
1329 (defface custom-rogue-face '((((class color))
1330 (:foreground "pink" :background "black"))
1331 (t
1332 (:underline t)))
1333 "Face used when the customize item is not defined for customization."
1334 :group 'custom-magic-faces)
1335
1336 (defface custom-modified-face '((((class color))
1337 (:foreground "white" :background "blue"))
1338 (t
1339 (:italic t :bold)))
1340 "Face used when the customize item has been modified."
1341 :group 'custom-magic-faces)
1342
1343 (defface custom-set-face '((((class color))
1344 (:foreground "blue" :background "white"))
1345 (t
1346 (:italic t)))
1347 "Face used when the customize item has been set."
1348 :group 'custom-magic-faces)
1349
1350 (defface custom-changed-face '((((class color))
1351 (:foreground "white" :background "blue"))
1352 (t
1353 (:italic t)))
1354 "Face used when the customize item has been changed."
1355 :group 'custom-magic-faces)
1356
1357 (defface custom-saved-face '((t (:underline t)))
1358 "Face used when the customize item has been saved."
1359 :group 'custom-magic-faces)
1360
1361 (defconst custom-magic-alist '((nil "#" underline "\
1362 uninitialized, you should not see this.")
1363 (unknown "?" italic "\
1364 unknown, you should not see this.")
1365 (hidden "-" default "\
1366 hidden, invoke \"Show\" in the previous line to show." "\
1367 group now hidden, invoke \"Show\", above, to show contents.")
1368 (invalid "x" custom-invalid-face "\
1369 the value displayed for this %c is invalid and cannot be set.")
1370 (modified "*" custom-modified-face "\
1371 you have edited the value as text, but you have not set the %c." "\
1372 you have edited something in this group, but not set it.")
1373 (set "+" custom-set-face "\
1374 you have set this %c, but not saved it for future sessions." "\
1375 something in this group has been set, but not saved.")
1376 (changed ":" custom-changed-face "\
1377 this %c has been changed outside the customize buffer." "\
1378 something in this group has been changed outside customize.")
1379 (saved "!" custom-saved-face "\
1380 this %c has been set and saved." "\
1381 something in this group has been set and saved.")
1382 (rogue "@" custom-rogue-face "\
1383 this %c has not been changed with customize." "\
1384 something in this group is not prepared for customization.")
1385 (standard " " nil "\
1386 this %c is unchanged from its standard setting." "\
1387 visible group members are all at standard settings."))
1388 "Alist of customize option states.
1389 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1390
1391 STATE is one of the following symbols:
1392
1393 `nil'
1394 For internal use, should never occur.
1395 `unknown'
1396 For internal use, should never occur.
1397 `hidden'
1398 This item is not being displayed.
1399 `invalid'
1400 This item is modified, but has an invalid form.
1401 `modified'
1402 This item is modified, and has a valid form.
1403 `set'
1404 This item has been set but not saved.
1405 `changed'
1406 The current value of this item has been changed temporarily.
1407 `saved'
1408 This item is marked for saving.
1409 `rogue'
1410 This item has no customization information.
1411 `standard'
1412 This item is unchanged from the standard setting.
1413
1414 MAGIC is a string used to present that state.
1415
1416 FACE is a face used to present the state.
1417
1418 ITEM-DESC is a string describing the state for options.
1419
1420 GROUP-DESC is a string describing the state for groups. If this is
1421 left out, ITEM-DESC will be used.
1422
1423 The string %c in either description will be replaced with the
1424 category of the item. These are `group'. `option', and `face'.
1425
1426 The list should be sorted most significant first.")
1427
1428 (defcustom custom-magic-show 'long
1429 "If non-nil, show textual description of the state.
1430 If `long', show a full-line description, not just one word."
1431 :type '(choice (const :tag "no" nil)
1432 (const short)
1433 (const long))
1434 :group 'custom-buffer)
1435
1436 (defcustom custom-magic-show-hidden '(option face)
1437 "Control whether the State button is shown for hidden items.
1438 The value should be a list with the custom categories where the State
1439 button should be visible. Possible categories are `group', `option',
1440 and `face'."
1441 :type '(set (const group) (const option) (const face))
1442 :group 'custom-buffer)
1443
1444 (defcustom custom-magic-show-button nil
1445 "Show a \"magic\" button indicating the state of each customization option."
1446 :type 'boolean
1447 :group 'custom-buffer)
1448
1449 (define-widget 'custom-magic 'default
1450 "Show and manipulate state for a customization option."
1451 :format "%v"
1452 :action 'widget-parent-action
1453 :notify 'ignore
1454 :value-get 'ignore
1455 :value-create 'custom-magic-value-create
1456 :value-delete 'widget-children-value-delete)
1457
1458 (defun widget-magic-mouse-down-action (widget &optional event)
1459 ;; Non-nil unless hidden.
1460 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1461 :custom-state)
1462 'hidden)))
1463
1464 (defun custom-magic-value-create (widget)
1465 ;; Create compact status report for WIDGET.
1466 (let* ((parent (widget-get widget :parent))
1467 (state (widget-get parent :custom-state))
1468 (hidden (eq state 'hidden))
1469 (entry (assq state custom-magic-alist))
1470 (magic (nth 1 entry))
1471 (face (nth 2 entry))
1472 (category (widget-get parent :custom-category))
1473 (text (or (and (eq category 'group)
1474 (nth 4 entry))
1475 (nth 3 entry)))
1476 (form (widget-get parent :custom-form))
1477 children)
1478 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1479 (setq text (concat (match-string 1 text)
1480 (symbol-name category)
1481 (match-string 2 text))))
1482 (when (and custom-magic-show
1483 (or (not hidden)
1484 (memq category custom-magic-show-hidden)))
1485 (insert " ")
1486 (when (and (eq category 'group)
1487 (not (and (eq custom-buffer-style 'links)
1488 (> (widget-get parent :custom-level) 1))))
1489 (insert-char ?\ (* custom-buffer-indent
1490 (widget-get parent :custom-level))))
1491 (push (widget-create-child-and-convert
1492 widget 'choice-item
1493 :help-echo "Change the state of this item."
1494 :format (if hidden "%t" "%[%t%]")
1495 :button-prefix 'widget-push-button-prefix
1496 :button-suffix 'widget-push-button-suffix
1497 :mouse-down-action 'widget-magic-mouse-down-action
1498 :tag "State")
1499 children)
1500 (insert ": ")
1501 (let ((start (point)))
1502 (if (eq custom-magic-show 'long)
1503 (insert text)
1504 (insert (symbol-name state)))
1505 (cond ((eq form 'lisp)
1506 (insert " (lisp)"))
1507 ((eq form 'mismatch)
1508 (insert " (mismatch)")))
1509 (put-text-property start (point) 'face 'custom-state-face))
1510 (insert "\n"))
1511 (when (and (eq category 'group)
1512 (not (and (eq custom-buffer-style 'links)
1513 (> (widget-get parent :custom-level) 1))))
1514 (insert-char ?\ (* custom-buffer-indent
1515 (widget-get parent :custom-level))))
1516 (when custom-magic-show-button
1517 (when custom-magic-show
1518 (let ((indent (widget-get parent :indent)))
1519 (when indent
1520 (insert-char ? indent))))
1521 (push (widget-create-child-and-convert
1522 widget 'choice-item
1523 :mouse-down-action 'widget-magic-mouse-down-action
1524 :button-face face
1525 :button-prefix ""
1526 :button-suffix ""
1527 :help-echo "Change the state."
1528 :format (if hidden "%t" "%[%t%]")
1529 :tag (if (memq form '(lisp mismatch))
1530 (concat "(" magic ")")
1531 (concat "[" magic "]")))
1532 children)
1533 (insert " "))
1534 (widget-put widget :children children)))
1535
1536 (defun custom-magic-reset (widget)
1537 "Redraw the :custom-magic property of WIDGET."
1538 (let ((magic (widget-get widget :custom-magic)))
1539 (widget-value-set magic (widget-value magic))))
1540
1541 ;;; The `custom' Widget.
1542
1543 (defface custom-button-face nil
1544 "Face used for buttons in customization buffers."
1545 :group 'custom-faces)
1546
1547 (defface custom-documentation-face nil
1548 "Face used for documentation strings in customization buffers."
1549 :group 'custom-faces)
1550
1551 (defface custom-state-face '((((class color)
1552 (background dark))
1553 (:foreground "lime green"))
1554 (((class color)
1555 (background light))
1556 (:foreground "dark green"))
1557 (t nil))
1558 "Face used for State descriptions in the customize buffer."
1559 :group 'custom-faces)
1560
1561 (define-widget 'custom 'default
1562 "Customize a user option."
1563 :format "%v"
1564 :convert-widget 'custom-convert-widget
1565 :notify 'custom-notify
1566 :custom-prefix ""
1567 :custom-level 1
1568 :custom-state 'hidden
1569 :documentation-property 'widget-subclass-responsibility
1570 :value-create 'widget-subclass-responsibility
1571 :value-delete 'widget-children-value-delete
1572 :value-get 'widget-value-value-get
1573 :validate 'widget-children-validate
1574 :match (lambda (widget value) (symbolp value)))
1575
1576 (defun custom-convert-widget (widget)
1577 ;; Initialize :value and :tag from :args in WIDGET.
1578 (let ((args (widget-get widget :args)))
1579 (when args
1580 (widget-put widget :value (widget-apply widget
1581 :value-to-internal (car args)))
1582 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1583 (widget-put widget :args nil)))
1584 widget)
1585
1586 (defun custom-notify (widget &rest args)
1587 "Keep track of changes."
1588 (let ((state (widget-get widget :custom-state)))
1589 (unless (eq state 'modified)
1590 (unless (memq state '(nil unknown hidden))
1591 (widget-put widget :custom-state 'modified))
1592 (custom-magic-reset widget)
1593 (apply 'widget-default-notify widget args))))
1594
1595 (defun custom-redraw (widget)
1596 "Redraw WIDGET with current settings."
1597 (let ((line (count-lines (point-min) (point)))
1598 (column (current-column))
1599 (pos (point))
1600 (from (marker-position (widget-get widget :from)))
1601 (to (marker-position (widget-get widget :to))))
1602 (save-excursion
1603 (widget-value-set widget (widget-value widget))
1604 (custom-redraw-magic widget))
1605 (when (and (>= pos from) (<= pos to))
1606 (condition-case nil
1607 (progn
1608 (if (> column 0)
1609 (goto-line line)
1610 (goto-line (1+ line)))
1611 (move-to-column column))
1612 (error nil)))))
1613
1614 (defun custom-redraw-magic (widget)
1615 "Redraw WIDGET state with current settings."
1616 (while widget
1617 (let ((magic (widget-get widget :custom-magic)))
1618 (cond (magic
1619 (widget-value-set magic (widget-value magic))
1620 (when (setq widget (widget-get widget :group))
1621 (custom-group-state-update widget)))
1622 (t
1623 (setq widget nil)))))
1624 (widget-setup))
1625
1626 (defun custom-show (widget value)
1627 "Non-nil if WIDGET should be shown with VALUE by default."
1628 (let ((show (widget-get widget :custom-show)))
1629 (cond ((null show)
1630 nil)
1631 ((eq t show)
1632 t)
1633 (t
1634 (funcall show widget value)))))
1635
1636 (defvar custom-load-recursion nil
1637 "Hack to avoid recursive dependencies.")
1638
1639 (defun custom-load-symbol (symbol)
1640 "Load all dependencies for SYMBOL."
1641 (unless custom-load-recursion
1642 (let ((custom-load-recursion t)
1643 (loads (get symbol 'custom-loads))
1644 load)
1645 (while loads
1646 (setq load (car loads)
1647 loads (cdr loads))
1648 (cond ((symbolp load)
1649 (condition-case nil
1650 (require load)
1651 (error nil)))
1652 ;; Don't reload a file already loaded.
1653 ((and (boundp 'preloaded-file-list)
1654 (member load preloaded-file-list)))
1655 ((assoc load load-history))
1656 ((assoc (locate-library load) load-history))
1657 (t
1658 (condition-case nil
1659 ;; Without this, we would load cus-edit recursively.
1660 ;; We are still loading it when we call this,
1661 ;; and it is not in load-history yet.
1662 (or (equal load "cus-edit")
1663 (load-library load))
1664 (error nil))))))))
1665
1666 (defun custom-load-widget (widget)
1667 "Load all dependencies for WIDGET."
1668 (custom-load-symbol (widget-value widget)))
1669
1670 (defun custom-unloaded-symbol-p (symbol)
1671 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1672 (let ((found nil)
1673 (loads (get symbol 'custom-loads))
1674 load)
1675 (while loads
1676 (setq load (car loads)
1677 loads (cdr loads))
1678 (cond ((symbolp load)
1679 (unless (featurep load)
1680 (setq found t)))
1681 ((assoc load load-history))
1682 ((assoc (locate-library load) load-history)
1683 (message nil))
1684 (t
1685 (setq found t))))
1686 found))
1687
1688 (defun custom-unloaded-widget-p (widget)
1689 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1690 (custom-unloaded-symbol-p (widget-value widget)))
1691
1692 (defun custom-toggle-hide (widget)
1693 "Toggle visibility of WIDGET."
1694 (custom-load-widget widget)
1695 (let ((state (widget-get widget :custom-state)))
1696 (cond ((memq state '(invalid modified))
1697 (error "There are unset changes"))
1698 ((eq state 'hidden)
1699 (widget-put widget :custom-state 'unknown))
1700 (t
1701 (widget-put widget :documentation-shown nil)
1702 (widget-put widget :custom-state 'hidden)))
1703 (custom-redraw widget)
1704 (widget-setup)))
1705
1706 (defun custom-toggle-parent (widget &rest ignore)
1707 "Toggle visibility of parent of WIDGET."
1708 (custom-toggle-hide (widget-get widget :parent)))
1709
1710 (defun custom-add-see-also (widget &optional prefix)
1711 "Add `See also ...' to WIDGET if there are any links.
1712 Insert PREFIX first if non-nil."
1713 (let* ((symbol (widget-get widget :value))
1714 (links (get symbol 'custom-links))
1715 (many (> (length links) 2))
1716 (buttons (widget-get widget :buttons))
1717 (indent (widget-get widget :indent)))
1718 (when links
1719 (when indent
1720 (insert-char ?\ indent))
1721 (when prefix
1722 (insert prefix))
1723 (insert "See also ")
1724 (while links
1725 (push (widget-create-child-and-convert widget (car links))
1726 buttons)
1727 (setq links (cdr links))
1728 (cond ((null links)
1729 (insert ".\n"))
1730 ((null (cdr links))
1731 (if many
1732 (insert ", and ")
1733 (insert " and ")))
1734 (t
1735 (insert ", "))))
1736 (widget-put widget :buttons buttons))))
1737
1738 (defun custom-add-parent-links (widget &optional initial-string)
1739 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1740 The value if non-nil if any parents were found.
1741 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1742 (let ((name (widget-value widget))
1743 (type (widget-type widget))
1744 (buttons (widget-get widget :buttons))
1745 (start (point))
1746 found)
1747 (insert (or initial-string "Parent groups:"))
1748 (mapatoms (lambda (symbol)
1749 (let ((entry (assq name (get symbol 'custom-group))))
1750 (when (eq (nth 1 entry) type)
1751 (insert " ")
1752 (push (widget-create-child-and-convert
1753 widget 'custom-group-link
1754 :tag (custom-unlispify-tag-name symbol)
1755 symbol)
1756 buttons)
1757 (setq found t)))))
1758 (widget-put widget :buttons buttons)
1759 (if found
1760 (insert "\n")
1761 (delete-region start (point)))
1762 found))
1763
1764 ;;; The `custom-variable' Widget.
1765
1766 (defface custom-variable-tag-face '((((class color)
1767 (background dark))
1768 (:foreground "light blue" :underline t))
1769 (((class color)
1770 (background light))
1771 (:foreground "blue" :underline t))
1772 (t (:underline t)))
1773 "Face used for unpushable variable tags."
1774 :group 'custom-faces)
1775
1776 (defface custom-variable-button-face '((t (:underline t :bold t)))
1777 "Face used for pushable variable tags."
1778 :group 'custom-faces)
1779
1780 (defcustom custom-variable-default-form 'edit
1781 "Default form of displaying variable values."
1782 :type '(choice (const edit)
1783 (const lisp))
1784 :group 'custom-buffer)
1785
1786 (define-widget 'custom-variable 'custom
1787 "Customize variable."
1788 :format "%v"
1789 :help-echo "Set or reset this variable."
1790 :documentation-property 'variable-documentation
1791 :custom-category 'option
1792 :custom-state nil
1793 :custom-menu 'custom-variable-menu-create
1794 :custom-form nil ; defaults to value of `custom-variable-default-form'
1795 :value-create 'custom-variable-value-create
1796 :action 'custom-variable-action
1797 :custom-set 'custom-variable-set
1798 :custom-save 'custom-variable-save
1799 :custom-reset-current 'custom-redraw
1800 :custom-reset-saved 'custom-variable-reset-saved
1801 :custom-reset-standard 'custom-variable-reset-standard)
1802
1803 (defun custom-variable-type (symbol)
1804 "Return a widget suitable for editing the value of SYMBOL.
1805 If SYMBOL has a `custom-type' property, use that.
1806 Otherwise, look up symbol in `custom-guess-type-alist'."
1807 (let* ((type (or (get symbol 'custom-type)
1808 (and (not (get symbol 'standard-value))
1809 (custom-guess-type symbol))
1810 'sexp))
1811 (options (get symbol 'custom-options))
1812 (tmp (if (listp type)
1813 (copy-sequence type)
1814 (list type))))
1815 (when options
1816 (widget-put tmp :options options))
1817 tmp))
1818
1819 (defun custom-variable-value-create (widget)
1820 "Here is where you edit the variables value."
1821 (custom-load-widget widget)
1822 (unless (widget-get widget :custom-form)
1823 (widget-put widget :custom-form custom-variable-default-form))
1824 (let* ((buttons (widget-get widget :buttons))
1825 (children (widget-get widget :children))
1826 (form (widget-get widget :custom-form))
1827 (state (widget-get widget :custom-state))
1828 (symbol (widget-get widget :value))
1829 (tag (widget-get widget :tag))
1830 (type (custom-variable-type symbol))
1831 (conv (widget-convert type))
1832 (get (or (get symbol 'custom-get) 'default-value))
1833 (prefix (widget-get widget :custom-prefix))
1834 (last (widget-get widget :custom-last))
1835 (value (if (default-boundp symbol)
1836 (funcall get symbol)
1837 (widget-get conv :value))))
1838 ;; If the widget is new, the child determine whether it is hidden.
1839 (cond (state)
1840 ((custom-show type value)
1841 (setq state 'unknown))
1842 (t
1843 (setq state 'hidden)))
1844 ;; If we don't know the state, see if we need to edit it in lisp form.
1845 (when (eq state 'unknown)
1846 (unless (widget-apply conv :match value)
1847 ;; (widget-apply (widget-convert type) :match value)
1848 (setq form 'mismatch)))
1849 ;; Now we can create the child widget.
1850 (cond ((eq custom-buffer-style 'tree)
1851 (insert prefix (if last " `--- " " |--- "))
1852 (push (widget-create-child-and-convert
1853 widget 'custom-browse-variable-tag)
1854 buttons)
1855 (insert " " tag "\n")
1856 (widget-put widget :buttons buttons))
1857 ((eq state 'hidden)
1858 ;; Indicate hidden value.
1859 (push (widget-create-child-and-convert
1860 widget 'item
1861 :format "%{%t%}: "
1862 :sample-face 'custom-variable-tag-face
1863 :tag tag
1864 :parent widget)
1865 buttons)
1866 (push (widget-create-child-and-convert
1867 widget 'visibility
1868 :help-echo "Show the value of this option."
1869 :action 'custom-toggle-parent
1870 nil)
1871 buttons))
1872 ((memq form '(lisp mismatch))
1873 ;; In lisp mode edit the saved value when possible.
1874 (let* ((value (cond ((get symbol 'saved-value)
1875 (car (get symbol 'saved-value)))
1876 ((get symbol 'standard-value)
1877 (car (get symbol 'standard-value)))
1878 ((default-boundp symbol)
1879 (custom-quote (funcall get symbol)))
1880 (t
1881 (custom-quote (widget-get conv :value))))))
1882 (insert (symbol-name symbol) ": ")
1883 (push (widget-create-child-and-convert
1884 widget 'visibility
1885 :help-echo "Hide the value of this option."
1886 :action 'custom-toggle-parent
1887 t)
1888 buttons)
1889 (insert " ")
1890 (push (widget-create-child-and-convert
1891 widget 'sexp
1892 :button-face 'custom-variable-button-face
1893 :format "%v"
1894 :tag (symbol-name symbol)
1895 :parent widget
1896 :value value)
1897 children)))
1898 (t
1899 ;; Edit mode.
1900 (let* ((format (widget-get type :format))
1901 tag-format value-format)
1902 (unless (string-match ":" format)
1903 (error "Bad format"))
1904 (setq tag-format (substring format 0 (match-end 0)))
1905 (setq value-format (substring format (match-end 0)))
1906 (push (widget-create-child-and-convert
1907 widget 'item
1908 :format tag-format
1909 :action 'custom-tag-action
1910 :help-echo "Change value of this option."
1911 :mouse-down-action 'custom-tag-mouse-down-action
1912 :button-face 'custom-variable-button-face
1913 :sample-face 'custom-variable-tag-face
1914 tag)
1915 buttons)
1916 (insert " ")
1917 (push (widget-create-child-and-convert
1918 widget 'visibility
1919 :help-echo "Hide the value of this option."
1920 :action 'custom-toggle-parent
1921 t)
1922 buttons)
1923 (push (widget-create-child-and-convert
1924 widget type
1925 :format value-format
1926 :value value)
1927 children))))
1928 (unless (eq custom-buffer-style 'tree)
1929 ;; Now update the state.
1930 (unless (eq (preceding-char) ?\n)
1931 (widget-insert "\n"))
1932 (if (eq state 'hidden)
1933 (widget-put widget :custom-state state)
1934 (custom-variable-state-set widget))
1935 ;; Create the magic button.
1936 (let ((magic (widget-create-child-and-convert
1937 widget 'custom-magic nil)))
1938 (widget-put widget :custom-magic magic)
1939 (push magic buttons))
1940 ;; Update properties.
1941 (widget-put widget :custom-form form)
1942 (widget-put widget :buttons buttons)
1943 (widget-put widget :children children)
1944 ;; Insert documentation.
1945 (widget-default-format-handler widget ?h)
1946 ;; See also.
1947 (unless (eq state 'hidden)
1948 (when (eq (widget-get widget :custom-level) 1)
1949 (custom-add-parent-links widget))
1950 (custom-add-see-also widget)))))
1951
1952 (defun custom-tag-action (widget &rest args)
1953 "Pass :action to first child of WIDGET's parent."
1954 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1955 :action args))
1956
1957 (defun custom-tag-mouse-down-action (widget &rest args)
1958 "Pass :mouse-down-action to first child of WIDGET's parent."
1959 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1960 :mouse-down-action args))
1961
1962 (defun custom-variable-state-set (widget)
1963 "Set the state of WIDGET."
1964 (let* ((symbol (widget-value widget))
1965 (get (or (get symbol 'custom-get) 'default-value))
1966 (value (if (default-boundp symbol)
1967 (funcall get symbol)
1968 (widget-get widget :value)))
1969 tmp
1970 (state (cond ((setq tmp (get symbol 'customized-value))
1971 (if (condition-case nil
1972 (equal value (eval (car tmp)))
1973 (error nil))
1974 'set
1975 'changed))
1976 ((setq tmp (get symbol 'saved-value))
1977 (if (condition-case nil
1978 (equal value (eval (car tmp)))
1979 (error nil))
1980 'saved
1981 'changed))
1982 ((setq tmp (get symbol 'standard-value))
1983 (if (condition-case nil
1984 (equal value (eval (car tmp)))
1985 (error nil))
1986 'standard
1987 'changed))
1988 (t 'rogue))))
1989 (widget-put widget :custom-state state)))
1990
1991 (defvar custom-variable-menu
1992 '(("Set for Current Session" custom-variable-set
1993 (lambda (widget)
1994 (eq (widget-get widget :custom-state) 'modified)))
1995 ("Save for Future Sessions" custom-variable-save
1996 (lambda (widget)
1997 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1998 ("Reset to Current" custom-redraw
1999 (lambda (widget)
2000 (and (default-boundp (widget-value widget))
2001 (memq (widget-get widget :custom-state) '(modified changed)))))
2002 ("Reset to Saved" custom-variable-reset-saved
2003 (lambda (widget)
2004 (and (get (widget-value widget) 'saved-value)
2005 (memq (widget-get widget :custom-state)
2006 '(modified set changed rogue)))))
2007 ("Reset to Standard Settings" custom-variable-reset-standard
2008 (lambda (widget)
2009 (and (get (widget-value widget) 'standard-value)
2010 (memq (widget-get widget :custom-state)
2011 '(modified set changed saved rogue)))))
2012 ("---" ignore ignore)
2013 ("Don't show as Lisp expression" custom-variable-edit
2014 (lambda (widget)
2015 (eq (widget-get widget :custom-form) 'lisp)))
2016 ("Show initial Lisp expression" custom-variable-edit-lisp
2017 (lambda (widget)
2018 (eq (widget-get widget :custom-form) 'edit))))
2019 "Alist of actions for the `custom-variable' widget.
2020 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2021 the menu entry, ACTION is the function to call on the widget when the
2022 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2023 widget as an argument, and returns non-nil if ACTION is valid on that
2024 widget. If FILTER is nil, ACTION is always valid.")
2025
2026 (defun custom-variable-action (widget &optional event)
2027 "Show the menu for `custom-variable' WIDGET.
2028 Optional EVENT is the location for the menu."
2029 (if (eq (widget-get widget :custom-state) 'hidden)
2030 (custom-toggle-hide widget)
2031 (unless (eq (widget-get widget :custom-state) 'modified)
2032 (custom-variable-state-set widget))
2033 (custom-redraw-magic widget)
2034 (let* ((completion-ignore-case t)
2035 (answer (widget-choose (concat "Operation on "
2036 (custom-unlispify-tag-name
2037 (widget-get widget :value)))
2038 (custom-menu-filter custom-variable-menu
2039 widget)
2040 event)))
2041 (if answer
2042 (funcall answer widget)))))
2043
2044 (defun custom-variable-edit (widget)
2045 "Edit value of WIDGET."
2046 (widget-put widget :custom-state 'unknown)
2047 (widget-put widget :custom-form 'edit)
2048 (custom-redraw widget))
2049
2050 (defun custom-variable-edit-lisp (widget)
2051 "Edit the lisp representation of the value of WIDGET."
2052 (widget-put widget :custom-state 'unknown)
2053 (widget-put widget :custom-form 'lisp)
2054 (custom-redraw widget))
2055
2056 (defun custom-variable-set (widget)
2057 "Set the current value for the variable being edited by WIDGET."
2058 (let* ((form (widget-get widget :custom-form))
2059 (state (widget-get widget :custom-state))
2060 (child (car (widget-get widget :children)))
2061 (symbol (widget-value widget))
2062 (set (or (get symbol 'custom-set) 'set-default))
2063 val)
2064 (cond ((eq state 'hidden)
2065 (error "Cannot set hidden variable"))
2066 ((setq val (widget-apply child :validate))
2067 (goto-char (widget-get val :from))
2068 (error "%s" (widget-get val :error)))
2069 ((memq form '(lisp mismatch))
2070 (funcall set symbol (eval (setq val (widget-value child))))
2071 (put symbol 'customized-value (list val)))
2072 (t
2073 (funcall set symbol (setq val (widget-value child)))
2074 (put symbol 'customized-value (list (custom-quote val)))))
2075 (custom-variable-state-set widget)
2076 (custom-redraw-magic widget)))
2077
2078 (defun custom-variable-save (widget)
2079 "Set and save the value for the variable being edited by WIDGET."
2080 (let* ((form (widget-get widget :custom-form))
2081 (state (widget-get widget :custom-state))
2082 (child (car (widget-get widget :children)))
2083 (symbol (widget-value widget))
2084 (set (or (get symbol 'custom-set) 'set-default))
2085 val)
2086 (cond ((eq state 'hidden)
2087 (error "Cannot set hidden variable"))
2088 ((setq val (widget-apply child :validate))
2089 (goto-char (widget-get val :from))
2090 (error "%s" (widget-get val :error)))
2091 ((memq form '(lisp mismatch))
2092 (put symbol 'saved-value (list (widget-value child)))
2093 (funcall set symbol (eval (widget-value child))))
2094 (t
2095 (put symbol
2096 'saved-value (list (custom-quote (widget-value
2097 child))))
2098 (funcall set symbol (widget-value child))))
2099 (put symbol 'customized-value nil)
2100 (custom-save-all)
2101 (custom-variable-state-set widget)
2102 (custom-redraw-magic widget)))
2103
2104 (defun custom-variable-reset-saved (widget)
2105 "Restore the saved value for the variable being edited by WIDGET."
2106 (let* ((symbol (widget-value widget))
2107 (set (or (get symbol 'custom-set) 'set-default)))
2108 (if (get symbol 'saved-value)
2109 (condition-case nil
2110 (funcall set symbol (eval (car (get symbol 'saved-value))))
2111 (error nil))
2112 (error "No saved value for %s" symbol))
2113 (put symbol 'customized-value nil)
2114 (widget-put widget :custom-state 'unknown)
2115 (custom-redraw widget)))
2116
2117 (defun custom-variable-reset-standard (widget)
2118 "Restore the standard setting for the variable being edited by WIDGET."
2119 (let* ((symbol (widget-value widget))
2120 (set (or (get symbol 'custom-set) 'set-default)))
2121 (if (get symbol 'standard-value)
2122 (funcall set symbol (eval (car (get symbol 'standard-value))))
2123 (error "No standard setting known for %S" symbol))
2124 (put symbol 'customized-value nil)
2125 (when (get symbol 'saved-value)
2126 (put symbol 'saved-value nil)
2127 (custom-save-all))
2128 (widget-put widget :custom-state 'unknown)
2129 (custom-redraw widget)))
2130
2131 ;;; The `custom-face-edit' Widget.
2132
2133 (define-widget 'custom-face-edit 'checklist
2134 "Edit face attributes."
2135 :format "%t: %v"
2136 :tag "Attributes"
2137 :extra-offset 12
2138 :button-args '(:help-echo "Control whether this attribute have any effect.")
2139 :args (mapcar (lambda (att)
2140 (list 'group
2141 :inline t
2142 :sibling-args (widget-get (nth 1 att) :sibling-args)
2143 (list 'const :format "" :value (nth 0 att))
2144 (nth 1 att)))
2145 custom-face-attributes))
2146
2147 ;;; The `custom-display' Widget.
2148
2149 (define-widget 'custom-display 'menu-choice
2150 "Select a display type."
2151 :tag "Display"
2152 :value t
2153 :help-echo "Specify frames where the face attributes should be used."
2154 :args '((const :tag "all" t)
2155 (checklist
2156 :offset 0
2157 :extra-offset 9
2158 :args ((group :sibling-args (:help-echo "\
2159 Only match the specified window systems.")
2160 (const :format "Type: "
2161 type)
2162 (checklist :inline t
2163 :offset 0
2164 (const :format "X "
2165 :sibling-args (:help-echo "\
2166 The X11 Window System.")
2167 x)
2168 (const :format "PM "
2169 :sibling-args (:help-echo "\
2170 OS/2 Presentation Manager.")
2171 pm)
2172 (const :format "W32 "
2173 :sibling-args (:help-echo "\
2174 Windows NT/9X.")
2175 w32)
2176 (const :format "DOS "
2177 :sibling-args (:help-echo "\
2178 Plain MS-DOS.")
2179 pc)
2180 (const :format "TTY%n"
2181 :sibling-args (:help-echo "\
2182 Plain text terminals.")
2183 tty)))
2184 (group :sibling-args (:help-echo "\
2185 Only match the frames with the specified color support.")
2186 (const :format "Class: "
2187 class)
2188 (checklist :inline t
2189 :offset 0
2190 (const :format "Color "
2191 :sibling-args (:help-echo "\
2192 Match color frames.")
2193 color)
2194 (const :format "Grayscale "
2195 :sibling-args (:help-echo "\
2196 Match grayscale frames.")
2197 grayscale)
2198 (const :format "Monochrome%n"
2199 :sibling-args (:help-echo "\
2200 Match frames with no color support.")
2201 mono)))
2202 (group :sibling-args (:help-echo "\
2203 Only match frames with the specified intensity.")
2204 (const :format "\
2205 Background brightness: "
2206 background)
2207 (checklist :inline t
2208 :offset 0
2209 (const :format "Light "
2210 :sibling-args (:help-echo "\
2211 Match frames with light backgrounds.")
2212 light)
2213 (const :format "Dark\n"
2214 :sibling-args (:help-echo "\
2215 Match frames with dark backgrounds.")
2216 dark)))))))
2217
2218 ;;; The `custom-face' Widget.
2219
2220 (defface custom-face-tag-face '((t (:underline t)))
2221 "Face used for face tags."
2222 :group 'custom-faces)
2223
2224 (defcustom custom-face-default-form 'selected
2225 "Default form of displaying face definition."
2226 :type '(choice (const all)
2227 (const selected)
2228 (const lisp))
2229 :group 'custom-buffer)
2230
2231 (define-widget 'custom-face 'custom
2232 "Customize face."
2233 :sample-face 'custom-face-tag-face
2234 :help-echo "Set or reset this face."
2235 :documentation-property '(lambda (face)
2236 (face-doc-string face))
2237 :value-create 'custom-face-value-create
2238 :action 'custom-face-action
2239 :custom-category 'face
2240 :custom-form nil ; defaults to value of `custom-face-default-form'
2241 :custom-set 'custom-face-set
2242 :custom-save 'custom-face-save
2243 :custom-reset-current 'custom-redraw
2244 :custom-reset-saved 'custom-face-reset-saved
2245 :custom-reset-standard 'custom-face-reset-standard
2246 :custom-menu 'custom-face-menu-create)
2247
2248 (define-widget 'custom-face-all 'editable-list
2249 "An editable list of display specifications and attributes."
2250 :entry-format "%i %d %v"
2251 :insert-button-args '(:help-echo "Insert new display specification here.")
2252 :append-button-args '(:help-echo "Append new display specification here.")
2253 :delete-button-args '(:help-echo "Delete this display specification.")
2254 :args '((group :format "%v" custom-display custom-face-edit)))
2255
2256 (defconst custom-face-all (widget-convert 'custom-face-all)
2257 "Converted version of the `custom-face-all' widget.")
2258
2259 (define-widget 'custom-display-unselected 'item
2260 "A display specification that doesn't match the selected display."
2261 :match 'custom-display-unselected-match)
2262
2263 (defun custom-display-unselected-match (widget value)
2264 "Non-nil if VALUE is an unselected display specification."
2265 (not (face-spec-set-match-display value (selected-frame))))
2266
2267 (define-widget 'custom-face-selected 'group
2268 "Edit the attributes of the selected display in a face specification."
2269 :args '((repeat :format ""
2270 :inline t
2271 (group custom-display-unselected sexp))
2272 (group (sexp :format "") custom-face-edit)
2273 (repeat :format ""
2274 :inline t
2275 sexp)))
2276
2277 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2278 "Converted version of the `custom-face-selected' widget.")
2279
2280 (defun custom-face-value-create (widget)
2281 "Create a list of the display specifications for WIDGET."
2282 (let ((buttons (widget-get widget :buttons))
2283 (symbol (widget-get widget :value))
2284 (tag (widget-get widget :tag))
2285 (state (widget-get widget :custom-state))
2286 (begin (point))
2287 (is-last (widget-get widget :custom-last))
2288 (prefix (widget-get widget :custom-prefix)))
2289 (unless tag
2290 (setq tag (prin1-to-string symbol)))
2291 (cond ((eq custom-buffer-style 'tree)
2292 (insert prefix (if is-last " `--- " " |--- "))
2293 (push (widget-create-child-and-convert
2294 widget 'custom-browse-face-tag)
2295 buttons)
2296 (insert " " tag "\n")
2297 (widget-put widget :buttons buttons))
2298 (t
2299 ;; Create tag.
2300 (insert tag)
2301 (if (eq custom-buffer-style 'face)
2302 (insert " ")
2303 (widget-specify-sample widget begin (point))
2304 (insert ": "))
2305 ;; Sample.
2306 (and (string-match "XEmacs" emacs-version)
2307 ;; XEmacs cannot display uninitialized faces.
2308 (not (custom-facep symbol))
2309 (copy-face 'custom-face-empty symbol))
2310 (push (widget-create-child-and-convert widget 'item
2311 :format "(%{%t%})"
2312 :sample-face symbol
2313 :tag "sample")
2314 buttons)
2315 ;; Visibility.
2316 (insert " ")
2317 (push (widget-create-child-and-convert
2318 widget 'visibility
2319 :help-echo "Hide or show this face."
2320 :action 'custom-toggle-parent
2321 (not (eq state 'hidden)))
2322 buttons)
2323 ;; Magic.
2324 (insert "\n")
2325 (let ((magic (widget-create-child-and-convert
2326 widget 'custom-magic nil)))
2327 (widget-put widget :custom-magic magic)
2328 (push magic buttons))
2329 ;; Update buttons.
2330 (widget-put widget :buttons buttons)
2331 ;; Insert documentation.
2332 (widget-default-format-handler widget ?h)
2333 ;; See also.
2334 (unless (eq state 'hidden)
2335 (when (eq (widget-get widget :custom-level) 1)
2336 (custom-add-parent-links widget))
2337 (custom-add-see-also widget))
2338 ;; Editor.
2339 (unless (eq (preceding-char) ?\n)
2340 (insert "\n"))
2341 (unless (eq state 'hidden)
2342 (message "Creating face editor...")
2343 (custom-load-widget widget)
2344 (unless (widget-get widget :custom-form)
2345 (widget-put widget :custom-form custom-face-default-form))
2346 (let* ((symbol (widget-value widget))
2347 (spec (or (get symbol 'saved-face)
2348 (get symbol 'face-defface-spec)
2349 ;; Attempt to construct it.
2350 (list (list t (custom-face-attributes-get
2351 symbol (selected-frame))))))
2352 (form (widget-get widget :custom-form))
2353 (indent (widget-get widget :indent))
2354 edit)
2355 ;; If the user has changed this face in some other way,
2356 ;; edit it as the user has specified it.
2357 (if (not (face-spec-match-p symbol spec (selected-frame)))
2358 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
2359 (setq edit (widget-create-child-and-convert
2360 widget
2361 (cond ((and (eq form 'selected)
2362 (widget-apply custom-face-selected
2363 :match spec))
2364 (when indent (insert-char ?\ indent))
2365 'custom-face-selected)
2366 ((and (not (eq form 'lisp))
2367 (widget-apply custom-face-all
2368 :match spec))
2369 'custom-face-all)
2370 (t
2371 (when indent (insert-char ?\ indent))
2372 'sexp))
2373 :value spec))
2374 (custom-face-state-set widget)
2375 (widget-put widget :children (list edit)))
2376 (message "Creating face editor...done"))))))
2377
2378 (defvar custom-face-menu
2379 '(("Set for Current Session" custom-face-set)
2380 ("Save for Future Sessions" custom-face-save-command)
2381 ("Reset to Saved" custom-face-reset-saved
2382 (lambda (widget)
2383 (get (widget-value widget) 'saved-face)))
2384 ("Reset to Standard Setting" custom-face-reset-standard
2385 (lambda (widget)
2386 (get (widget-value widget) 'face-defface-spec)))
2387 ("---" ignore ignore)
2388 ("Show all display specs" custom-face-edit-all
2389 (lambda (widget)
2390 (not (eq (widget-get widget :custom-form) 'all))))
2391 ("Just current attributes" custom-face-edit-selected
2392 (lambda (widget)
2393 (not (eq (widget-get widget :custom-form) 'selected))))
2394 ("Show as Lisp expression" custom-face-edit-lisp
2395 (lambda (widget)
2396 (not (eq (widget-get widget :custom-form) 'lisp)))))
2397 "Alist of actions for the `custom-face' widget.
2398 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2399 the menu entry, ACTION is the function to call on the widget when the
2400 menu is selected, and FILTER is a predicate which takes a `custom-face'
2401 widget as an argument, and returns non-nil if ACTION is valid on that
2402 widget. If FILTER is nil, ACTION is always valid.")
2403
2404 (defun custom-face-edit-selected (widget)
2405 "Edit selected attributes of the value of WIDGET."
2406 (widget-put widget :custom-state 'unknown)
2407 (widget-put widget :custom-form 'selected)
2408 (custom-redraw widget))
2409
2410 (defun custom-face-edit-all (widget)
2411 "Edit all attributes of the value of WIDGET."
2412 (widget-put widget :custom-state 'unknown)
2413 (widget-put widget :custom-form 'all)
2414 (custom-redraw widget))
2415
2416 (defun custom-face-edit-lisp (widget)
2417 "Edit the lisp representation of the value of WIDGET."
2418 (widget-put widget :custom-state 'unknown)
2419 (widget-put widget :custom-form 'lisp)
2420 (custom-redraw widget))
2421
2422 (defun custom-face-state-set (widget)
2423 "Set the state of WIDGET."
2424 (let ((symbol (widget-value widget)))
2425 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
2426 'set)
2427 ((get symbol 'saved-face)
2428 'saved)
2429 ((get symbol 'face-defface-spec)
2430 'standard)
2431 (t
2432 'rogue)))))
2433
2434 (defun custom-face-action (widget &optional event)
2435 "Show the menu for `custom-face' WIDGET.
2436 Optional EVENT is the location for the menu."
2437 (if (eq (widget-get widget :custom-state) 'hidden)
2438 (custom-toggle-hide widget)
2439 (let* ((completion-ignore-case t)
2440 (symbol (widget-get widget :value))
2441 (answer (widget-choose (concat "Operation on "
2442 (custom-unlispify-tag-name symbol))
2443 (custom-menu-filter custom-face-menu
2444 widget)
2445 event)))
2446 (if answer
2447 (funcall answer widget)))))
2448
2449 (defun custom-face-set (widget)
2450 "Make the face attributes in WIDGET take effect."
2451 (let* ((symbol (widget-value widget))
2452 (child (car (widget-get widget :children)))
2453 (value (widget-value child)))
2454 (put symbol 'customized-face value)
2455 (face-spec-set symbol value)
2456 (custom-face-state-set widget)
2457 (custom-redraw-magic widget)))
2458
2459 (defun custom-face-save-command (widget)
2460 "Save in `.emacs' the face attributes in WIDGET."
2461 (custom-face-save widget)
2462 (custom-save-all))
2463
2464 (defun custom-face-save (widget)
2465 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
2466 (let* ((symbol (widget-value widget))
2467 (child (car (widget-get widget :children)))
2468 (value (widget-value child)))
2469 (face-spec-set symbol value)
2470 (put symbol 'saved-face value)
2471 (put symbol 'customized-face nil)
2472 (custom-save-all)
2473 (custom-face-state-set widget)
2474 (custom-redraw-magic widget)))
2475
2476 (defun custom-face-reset-saved (widget)
2477 "Restore WIDGET to the face's default attributes."
2478 (let* ((symbol (widget-value widget))
2479 (child (car (widget-get widget :children)))
2480 (value (get symbol 'saved-face)))
2481 (unless value
2482 (error "No saved value for this face"))
2483 (put symbol 'customized-face nil)
2484 (face-spec-set symbol value)
2485 (widget-value-set child value)
2486 (custom-face-state-set widget)
2487 (custom-redraw-magic widget)))
2488
2489 (defun custom-face-reset-standard (widget)
2490 "Restore WIDGET to the face's standard settings."
2491 (let* ((symbol (widget-value widget))
2492 (child (car (widget-get widget :children)))
2493 (value (get symbol 'face-defface-spec)))
2494 (unless value
2495 (error "No standard setting for this face"))
2496 (put symbol 'customized-face nil)
2497 (when (get symbol 'saved-face)
2498 (put symbol 'saved-face nil)
2499 (custom-save-all))
2500 (face-spec-set symbol value)
2501 (widget-value-set child value)
2502 (custom-face-state-set widget)
2503 (custom-redraw-magic widget)))
2504
2505 ;;; The `face' Widget.
2506
2507 (define-widget 'face 'default
2508 "Select and customize a face."
2509 :convert-widget 'widget-value-convert-widget
2510 :button-prefix 'widget-push-button-prefix
2511 :button-suffix 'widget-push-button-suffix
2512 :format "%t: %[select face%] %v"
2513 :tag "Face"
2514 :value 'default
2515 :value-create 'widget-face-value-create
2516 :value-delete 'widget-face-value-delete
2517 :value-get 'widget-value-value-get
2518 :validate 'widget-children-validate
2519 :action 'widget-face-action
2520 :match '(lambda (widget value) (symbolp value)))
2521
2522 (defun widget-face-value-create (widget)
2523 ;; Create a `custom-face' child.
2524 (let* ((symbol (widget-value widget))
2525 (custom-buffer-style 'face)
2526 (child (widget-create-child-and-convert
2527 widget 'custom-face
2528 :custom-level nil
2529 :value symbol)))
2530 (custom-magic-reset child)
2531 (setq custom-options (cons child custom-options))
2532 (widget-put widget :children (list child))))
2533
2534 (defun widget-face-value-delete (widget)
2535 ;; Remove the child from the options.
2536 (let ((child (car (widget-get widget :children))))
2537 (setq custom-options (delq child custom-options))
2538 (widget-children-value-delete widget)))
2539
2540 (defvar face-history nil
2541 "History of entered face names.")
2542
2543 (defun widget-face-action (widget &optional event)
2544 "Prompt for a face."
2545 (let ((answer (completing-read "Face: "
2546 (mapcar (lambda (face)
2547 (list (symbol-name face)))
2548 (face-list))
2549 nil nil nil
2550 'face-history)))
2551 (unless (zerop (length answer))
2552 (widget-value-set widget (intern answer))
2553 (widget-apply widget :notify widget event)
2554 (widget-setup))))
2555
2556 ;;; The `hook' Widget.
2557
2558 (define-widget 'hook 'list
2559 "A emacs lisp hook"
2560 :value-to-internal (lambda (widget value)
2561 (if (and value (symbolp value))
2562 (list value)
2563 value))
2564 :match (lambda (widget value)
2565 (or (symbolp value)
2566 (widget-group-match widget value)))
2567 :convert-widget 'custom-hook-convert-widget
2568 :tag "Hook")
2569
2570 (defun custom-hook-convert-widget (widget)
2571 ;; Handle `:custom-options'.
2572 (let* ((options (widget-get widget :options))
2573 (other `(editable-list :inline t
2574 :entry-format "%i %d%v"
2575 (function :format " %v")))
2576 (args (if options
2577 (list `(checklist :inline t
2578 ,@(mapcar (lambda (entry)
2579 `(function-item ,entry))
2580 options))
2581 other)
2582 (list other))))
2583 (widget-put widget :args args)
2584 widget))
2585
2586 ;;; The `custom-group-link' Widget.
2587
2588 (define-widget 'custom-group-link 'link
2589 "Show parent in other window when activated."
2590 :help-echo "Create customization buffer for this group."
2591 :action 'custom-group-link-action)
2592
2593 (defun custom-group-link-action (widget &rest ignore)
2594 (customize-group (widget-value widget)))
2595
2596 ;;; The `custom-group' Widget.
2597
2598 (defcustom custom-group-tag-faces nil
2599 ;; In XEmacs, this ought to play games with font size.
2600 "Face used for group tags.
2601 The first member is used for level 1 groups, the second for level 2,
2602 and so forth. The remaining group tags are shown with
2603 `custom-group-tag-face'."
2604 :type '(repeat face)
2605 :group 'custom-faces)
2606
2607 (defface custom-group-tag-face-1 '((((class color)
2608 (background dark))
2609 (:foreground "pink" :underline t))
2610 (((class color)
2611 (background light))
2612 (:foreground "red" :underline t))
2613 (t (:underline t)))
2614 "Face used for group tags.")
2615
2616 (defface custom-group-tag-face '((((class color)
2617 (background dark))
2618 (:foreground "light blue" :underline t))
2619 (((class color)
2620 (background light))
2621 (:foreground "blue" :underline t))
2622 (t (:underline t)))
2623 "Face used for low level group tags."
2624 :group 'custom-faces)
2625
2626 (define-widget 'custom-group 'custom
2627 "Customize group."
2628 :format "%v"
2629 :sample-face-get 'custom-group-sample-face-get
2630 :documentation-property 'group-documentation
2631 :help-echo "Set or reset all members of this group."
2632 :value-create 'custom-group-value-create
2633 :action 'custom-group-action
2634 :custom-category 'group
2635 :custom-set 'custom-group-set
2636 :custom-save 'custom-group-save
2637 :custom-reset-current 'custom-group-reset-current
2638 :custom-reset-saved 'custom-group-reset-saved
2639 :custom-reset-standard 'custom-group-reset-standard
2640 :custom-menu 'custom-group-menu-create)
2641
2642 (defun custom-group-sample-face-get (widget)
2643 ;; Use :sample-face.
2644 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2645 'custom-group-tag-face))
2646
2647 (define-widget 'custom-group-visibility 'visibility
2648 "An indicator and manipulator for hidden group contents."
2649 :create 'custom-group-visibility-create)
2650
2651 (defun custom-group-visibility-create (widget)
2652 (let ((visible (widget-value widget)))
2653 (if visible
2654 (insert "--------")))
2655 (widget-default-create widget))
2656
2657 (defun custom-group-members (symbol groups-only)
2658 "Return SYMBOL's custom group members.
2659 If GROUPS-ONLY non-nil, return only those members that are groups."
2660 (if (not groups-only)
2661 (get symbol 'custom-group)
2662 (let (members)
2663 (dolist (entry (get symbol 'custom-group))
2664 (when (eq (nth 1 entry) 'custom-group)
2665 (push entry members)))
2666 (nreverse members))))
2667
2668 (defun custom-group-value-create (widget)
2669 "Insert a customize group for WIDGET in the current buffer."
2670 (let* ((state (widget-get widget :custom-state))
2671 (level (widget-get widget :custom-level))
2672 ;; (indent (widget-get widget :indent))
2673 (prefix (widget-get widget :custom-prefix))
2674 (buttons (widget-get widget :buttons))
2675 (tag (widget-get widget :tag))
2676 (symbol (widget-value widget))
2677 (members (custom-group-members symbol
2678 (and (eq custom-buffer-style 'tree)
2679 custom-browse-only-groups))))
2680 (cond ((and (eq custom-buffer-style 'tree)
2681 (eq state 'hidden)
2682 (or members (custom-unloaded-widget-p widget)))
2683 (custom-browse-insert-prefix prefix)
2684 (push (widget-create-child-and-convert
2685 widget 'custom-browse-visibility
2686 ;; :tag-glyph "plus"
2687 :tag "+")
2688 buttons)
2689 (insert "-- ")
2690 ;; (widget-glyph-insert nil "-- " "horizontal")
2691 (push (widget-create-child-and-convert
2692 widget 'custom-browse-group-tag)
2693 buttons)
2694 (insert " " tag "\n")
2695 (widget-put widget :buttons buttons))
2696 ((and (eq custom-buffer-style 'tree)
2697 (zerop (length members)))
2698 (custom-browse-insert-prefix prefix)
2699 (insert "[ ]-- ")
2700 ;; (widget-glyph-insert nil "[ ]" "empty")
2701 ;; (widget-glyph-insert nil "-- " "horizontal")
2702 (push (widget-create-child-and-convert
2703 widget 'custom-browse-group-tag)
2704 buttons)
2705 (insert " " tag "\n")
2706 (widget-put widget :buttons buttons))
2707 ((eq custom-buffer-style 'tree)
2708 (custom-browse-insert-prefix prefix)
2709 (custom-load-widget widget)
2710 (if (zerop (length members))
2711 (progn
2712 (custom-browse-insert-prefix prefix)
2713 (insert "[ ]-- ")
2714 ;; (widget-glyph-insert nil "[ ]" "empty")
2715 ;; (widget-glyph-insert nil "-- " "horizontal")
2716 (push (widget-create-child-and-convert
2717 widget 'custom-browse-group-tag)
2718 buttons)
2719 (insert " " tag "\n")
2720 (widget-put widget :buttons buttons))
2721 (push (widget-create-child-and-convert
2722 widget 'custom-browse-visibility
2723 ;; :tag-glyph "minus"
2724 :tag "-")
2725 buttons)
2726 (insert "-\\ ")
2727 ;; (widget-glyph-insert nil "-\\ " "top")
2728 (push (widget-create-child-and-convert
2729 widget 'custom-browse-group-tag)
2730 buttons)
2731 (insert " " tag "\n")
2732 (widget-put widget :buttons buttons)
2733 (message "Creating group...")
2734 (let* ((members (custom-sort-items members
2735 custom-browse-sort-alphabetically
2736 custom-browse-order-groups))
2737 (prefixes (widget-get widget :custom-prefixes))
2738 (custom-prefix-list (custom-prefix-add symbol prefixes))
2739 (extra-prefix (if (widget-get widget :custom-last)
2740 " "
2741 " | "))
2742 (prefix (concat prefix extra-prefix))
2743 children entry)
2744 (while members
2745 (setq entry (car members)
2746 members (cdr members))
2747 (push (widget-create-child-and-convert
2748 widget (nth 1 entry)
2749 :group widget
2750 :tag (custom-unlispify-tag-name (nth 0 entry))
2751 :custom-prefixes custom-prefix-list
2752 :custom-level (1+ level)
2753 :custom-last (null members)
2754 :value (nth 0 entry)
2755 :custom-prefix prefix)
2756 children))
2757 (widget-put widget :children (reverse children)))
2758 (message "Creating group...done")))
2759 ;; Nested style.
2760 ((eq state 'hidden)
2761 ;; Create level indicator.
2762 (unless (eq custom-buffer-style 'links)
2763 (insert-char ?\ (* custom-buffer-indent (1- level)))
2764 (insert "-- "))
2765 ;; Create tag.
2766 (let ((begin (point)))
2767 (insert tag)
2768 (widget-specify-sample widget begin (point)))
2769 (insert " group: ")
2770 ;; Create link/visibility indicator.
2771 (if (eq custom-buffer-style 'links)
2772 (push (widget-create-child-and-convert
2773 widget 'custom-group-link
2774 :tag "Go to Group"
2775 symbol)
2776 buttons)
2777 (push (widget-create-child-and-convert
2778 widget 'custom-group-visibility
2779 :help-echo "Show members of this group."
2780 :action 'custom-toggle-parent
2781 (not (eq state 'hidden)))
2782 buttons))
2783 (insert " \n")
2784 ;; Create magic button.
2785 (let ((magic (widget-create-child-and-convert
2786 widget 'custom-magic nil)))
2787 (widget-put widget :custom-magic magic)
2788 (push magic buttons))
2789 ;; Update buttons.
2790 (widget-put widget :buttons buttons)
2791 ;; Insert documentation.
2792 (if (and (eq custom-buffer-style 'links) (> level 1))
2793 (widget-put widget :documentation-indent 0))
2794 (widget-default-format-handler widget ?h))
2795 ;; Nested style.
2796 (t ;Visible.
2797 ;; Add parent groups references above the group.
2798 (if t ;;; This should test that the buffer
2799 ;;; was made to display a group.
2800 (when (eq level 1)
2801 (if (custom-add-parent-links widget
2802 "Go to parent group:")
2803 (insert "\n"))))
2804 ;; Create level indicator.
2805 (insert-char ?\ (* custom-buffer-indent (1- level)))
2806 (insert "/- ")
2807 ;; Create tag.
2808 (let ((start (point)))
2809 (insert tag)
2810 (widget-specify-sample widget start (point)))
2811 (insert " group: ")
2812 ;; Create visibility indicator.
2813 (unless (eq custom-buffer-style 'links)
2814 (insert "--------")
2815 (push (widget-create-child-and-convert
2816 widget 'visibility
2817 :help-echo "Hide members of this group."
2818 :action 'custom-toggle-parent
2819 (not (eq state 'hidden)))
2820 buttons)
2821 (insert " "))
2822 ;; Create more dashes.
2823 ;; Use 76 instead of 75 to compensate for the temporary "<"
2824 ;; added by `widget-insert'.
2825 (insert-char ?- (- 76 (current-column)
2826 (* custom-buffer-indent level)))
2827 (insert "\\\n")
2828 ;; Create magic button.
2829 (let ((magic (widget-create-child-and-convert
2830 widget 'custom-magic
2831 :indent 0
2832 nil)))
2833 (widget-put widget :custom-magic magic)
2834 (push magic buttons))
2835 ;; Update buttons.
2836 (widget-put widget :buttons buttons)
2837 ;; Insert documentation.
2838 (widget-default-format-handler widget ?h)
2839 ;; Parent groups.
2840 (if nil ;;; This should test that the buffer
2841 ;;; was not made to display a group.
2842 (when (eq level 1)
2843 (insert-char ?\ custom-buffer-indent)
2844 (custom-add-parent-links widget)))
2845 (custom-add-see-also widget
2846 (make-string (* custom-buffer-indent level)
2847 ?\ ))
2848 ;; Members.
2849 (message "Creating group...")
2850 (custom-load-widget widget)
2851 (let* ((members (custom-sort-items members
2852 custom-buffer-sort-alphabetically
2853 custom-buffer-order-groups))
2854 (prefixes (widget-get widget :custom-prefixes))
2855 (custom-prefix-list (custom-prefix-add symbol prefixes))
2856 (length (length members))
2857 (count 0)
2858 (children (mapcar (lambda (entry)
2859 (widget-insert "\n")
2860 (message "\
2861 Creating group members... %2d%%"
2862 (/ (* 100.0 count) length))
2863 (setq count (1+ count))
2864 (prog1
2865 (widget-create-child-and-convert
2866 widget (nth 1 entry)
2867 :group widget
2868 :tag (custom-unlispify-tag-name
2869 (nth 0 entry))
2870 :custom-prefixes custom-prefix-list
2871 :custom-level (1+ level)
2872 :value (nth 0 entry))
2873 (unless (eq (preceding-char) ?\n)
2874 (widget-insert "\n"))))
2875 members)))
2876 (message "Creating group magic...")
2877 (mapcar 'custom-magic-reset children)
2878 (message "Creating group state...")
2879 (widget-put widget :children children)
2880 (custom-group-state-update widget)
2881 (message "Creating group... done"))
2882 ;; End line
2883 (insert "\n")
2884 (insert-char ?\ (* custom-buffer-indent (1- level)))
2885 (insert "\\- " (widget-get widget :tag) " group end ")
2886 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
2887 (insert "/\n")))))
2888
2889 (defvar custom-group-menu
2890 '(("Set for Current Session" custom-group-set
2891 (lambda (widget)
2892 (eq (widget-get widget :custom-state) 'modified)))
2893 ("Save for Future Sessions" custom-group-save
2894 (lambda (widget)
2895 (memq (widget-get widget :custom-state) '(modified set))))
2896 ("Reset to Current" custom-group-reset-current
2897 (lambda (widget)
2898 (memq (widget-get widget :custom-state) '(modified))))
2899 ("Reset to Saved" custom-group-reset-saved
2900 (lambda (widget)
2901 (memq (widget-get widget :custom-state) '(modified set))))
2902 ("Reset to standard setting" custom-group-reset-standard
2903 (lambda (widget)
2904 (memq (widget-get widget :custom-state) '(modified set saved)))))
2905 "Alist of actions for the `custom-group' widget.
2906 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2907 the menu entry, ACTION is the function to call on the widget when the
2908 menu is selected, and FILTER is a predicate which takes a `custom-group'
2909 widget as an argument, and returns non-nil if ACTION is valid on that
2910 widget. If FILTER is nil, ACTION is always valid.")
2911
2912 (defun custom-group-action (widget &optional event)
2913 "Show the menu for `custom-group' WIDGET.
2914 Optional EVENT is the location for the menu."
2915 (if (eq (widget-get widget :custom-state) 'hidden)
2916 (custom-toggle-hide widget)
2917 (let* ((completion-ignore-case t)
2918 (answer (widget-choose (concat "Operation on "
2919 (custom-unlispify-tag-name
2920 (widget-get widget :value)))
2921 (custom-menu-filter custom-group-menu
2922 widget)
2923 event)))
2924 (if answer
2925 (funcall answer widget)))))
2926
2927 (defun custom-group-set (widget)
2928 "Set changes in all modified group members."
2929 (let ((children (widget-get widget :children)))
2930 (mapcar (lambda (child)
2931 (when (eq (widget-get child :custom-state) 'modified)
2932 (widget-apply child :custom-set)))
2933 children )))
2934
2935 (defun custom-group-save (widget)
2936 "Save all modified group members."
2937 (let ((children (widget-get widget :children)))
2938 (mapcar (lambda (child)
2939 (when (memq (widget-get child :custom-state) '(modified set))
2940 (widget-apply child :custom-save)))
2941 children )))
2942
2943 (defun custom-group-reset-current (widget)
2944 "Reset all modified group members."
2945 (let ((children (widget-get widget :children)))
2946 (mapcar (lambda (child)
2947 (when (eq (widget-get child :custom-state) 'modified)
2948 (widget-apply child :custom-reset-current)))
2949 children )))
2950
2951 (defun custom-group-reset-saved (widget)
2952 "Reset all modified or set group members."
2953 (let ((children (widget-get widget :children)))
2954 (mapcar (lambda (child)
2955 (when (memq (widget-get child :custom-state) '(modified set))
2956 (widget-apply child :custom-reset-saved)))
2957 children )))
2958
2959 (defun custom-group-reset-standard (widget)
2960 "Reset all modified, set, or saved group members."
2961 (let ((children (widget-get widget :children)))
2962 (mapcar (lambda (child)
2963 (when (memq (widget-get child :custom-state)
2964 '(modified set saved))
2965 (widget-apply child :custom-reset-standard)))
2966 children )))
2967
2968 (defun custom-group-state-update (widget)
2969 "Update magic."
2970 (unless (eq (widget-get widget :custom-state) 'hidden)
2971 (let* ((children (widget-get widget :children))
2972 (states (mapcar (lambda (child)
2973 (widget-get child :custom-state))
2974 children))
2975 (magics custom-magic-alist)
2976 (found 'standard))
2977 (while magics
2978 (let ((magic (car (car magics))))
2979 (if (and (not (eq magic 'hidden))
2980 (memq magic states))
2981 (setq found magic
2982 magics nil)
2983 (setq magics (cdr magics)))))
2984 (widget-put widget :custom-state found)))
2985 (custom-magic-reset widget))
2986
2987 ;;; The `custom-save-all' Function.
2988 ;;;###autoload
2989 (defcustom custom-file nil
2990 "File used for storing customization information.
2991 The default is nil, which means to use your init file
2992 as specified by `user-init-file'. If you specify some other file,
2993 you need to explicitly load that file for the settings to take effect."
2994 :type '(choice (const :tag "Your Emacs init file" nil) file)
2995 :group 'customize)
2996
2997 (defun custom-file ()
2998 "Return the file name for saving customizations."
2999 (setq custom-file
3000 (or custom-file
3001 user-init-file
3002 (read-file-name "File for customizations: "
3003 "~/" nil nil ".emacs"))))
3004
3005 (defun custom-save-delete (symbol)
3006 "Delete the call to SYMBOL from `custom-file'.
3007 Leave point at the location of the call, or after the last expression."
3008 (let ((default-major-mode))
3009 (set-buffer (find-file-noselect (custom-file))))
3010 (goto-char (point-min))
3011 (catch 'found
3012 (while t
3013 (let ((sexp (condition-case nil
3014 (read (current-buffer))
3015 (end-of-file (throw 'found nil)))))
3016 (when (and (listp sexp)
3017 (eq (car sexp) symbol))
3018 (delete-region (save-excursion
3019 (backward-sexp)
3020 (point))
3021 (point))
3022 (throw 'found nil))))))
3023
3024 (defun custom-save-variables ()
3025 "Save all customized variables in `custom-file'."
3026 (save-excursion
3027 (custom-save-delete 'custom-set-variables)
3028 (let ((standard-output (current-buffer)))
3029 (unless (bolp)
3030 (princ "\n"))
3031 (princ "(custom-set-variables")
3032 (mapatoms (lambda (symbol)
3033 (let ((value (get symbol 'saved-value))
3034 (requests (get symbol 'custom-requests))
3035 (now (not (or (get symbol 'standard-value)
3036 (and (not (boundp symbol))
3037 (not (get symbol 'force-value)))))))
3038 (when value
3039 (princ "\n '(")
3040 (princ symbol)
3041 (princ " ")
3042 (prin1 (car value))
3043 (cond (requests
3044 (if now
3045 (princ " t ")
3046 (princ " nil "))
3047 (prin1 requests)
3048 (princ ")"))
3049 (now
3050 (princ " t)"))
3051 (t
3052 (princ ")")))))))
3053 (princ ")")
3054 (unless (looking-at "\n")
3055 (princ "\n")))))
3056
3057 (defun custom-save-faces ()
3058 "Save all customized faces in `custom-file'."
3059 (save-excursion
3060 (custom-save-delete 'custom-set-faces)
3061 (let ((standard-output (current-buffer)))
3062 (unless (bolp)
3063 (princ "\n"))
3064 (princ "(custom-set-faces")
3065 (let ((value (get 'default 'saved-face)))
3066 ;; The default face must be first, since it affects the others.
3067 (when value
3068 (princ "\n '(default ")
3069 (prin1 value)
3070 (if (or (get 'default 'face-defface-spec)
3071 (and (not (custom-facep 'default))
3072 (not (get 'default 'force-face))))
3073 (princ ")")
3074 (princ " t)"))))
3075 (mapatoms (lambda (symbol)
3076 (let ((value (get symbol 'saved-face)))
3077 (when (and (not (eq symbol 'default))
3078 ;; Don't print default face here.
3079 value)
3080 (princ "\n '(")
3081 (princ symbol)
3082 (princ " ")
3083 (prin1 value)
3084 (if (or (get symbol 'face-defface-spec)
3085 (and (not (custom-facep symbol))
3086 (not (get symbol 'force-face))))
3087 (princ ")")
3088 (princ " t)"))))))
3089 (princ ")")
3090 (unless (looking-at "\n")
3091 (princ "\n")))))
3092
3093 ;;;###autoload
3094 (defun customize-save-customized ()
3095 "Save all user options which have been set in this session."
3096 (interactive)
3097 (mapatoms (lambda (symbol)
3098 (let ((face (get symbol 'customized-face))
3099 (value (get symbol 'customized-value)))
3100 (when face
3101 (put symbol 'saved-face face)
3102 (put symbol 'customized-face nil))
3103 (when value
3104 (put symbol 'saved-value value)
3105 (put symbol 'customized-value nil)))))
3106 ;; We really should update all custom buffers here.
3107 (custom-save-all))
3108
3109 ;;;###autoload
3110 (defun custom-save-all ()
3111 "Save all customizations in `custom-file'."
3112 (let ((inhibit-read-only t))
3113 (custom-save-variables)
3114 (custom-save-faces)
3115 (save-excursion
3116 (let ((default-major-mode nil))
3117 (set-buffer (find-file-noselect (custom-file))))
3118 (save-buffer))))
3119
3120 ;;; The Customize Menu.
3121
3122 ;;; Menu support
3123
3124 (defcustom custom-menu-nesting 2
3125 "Maximum nesting in custom menus."
3126 :type 'integer
3127 :group 'custom-menu)
3128
3129 (defun custom-face-menu-create (widget symbol)
3130 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3131 (vector (custom-unlispify-menu-entry symbol)
3132 `(customize-face ',symbol)
3133 t))
3134
3135 (defun custom-variable-menu-create (widget symbol)
3136 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3137 (let ((type (get symbol 'custom-type)))
3138 (unless (listp type)
3139 (setq type (list type)))
3140 (if (and type (widget-get type :custom-menu))
3141 (widget-apply type :custom-menu symbol)
3142 (vector (custom-unlispify-menu-entry symbol)
3143 `(customize-variable ',symbol)
3144 t))))
3145
3146 ;; Add checkboxes to boolean variable entries.
3147 (widget-put (get 'boolean 'widget-type)
3148 :custom-menu (lambda (widget symbol)
3149 (vector (custom-unlispify-menu-entry symbol)
3150 `(customize-variable ',symbol)
3151 ':style 'toggle
3152 ':selected symbol)))
3153
3154 (if (string-match "XEmacs" emacs-version)
3155 ;; XEmacs can create menus dynamically.
3156 (defun custom-group-menu-create (widget symbol)
3157 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3158 `( ,(custom-unlispify-menu-entry symbol t)
3159 :filter (lambda (&rest junk)
3160 (cdr (custom-menu-create ',symbol)))))
3161 ;; But emacs can't.
3162 (defun custom-group-menu-create (widget symbol)
3163 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3164 ;; Limit the nesting.
3165 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3166 (custom-menu-create symbol))))
3167
3168 ;;;###autoload
3169 (defun custom-menu-create (symbol)
3170 "Create menu for customization group SYMBOL.
3171 The menu is in a format applicable to `easy-menu-define'."
3172 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3173 `(customize-group ',symbol)
3174 t)))
3175 (if (and (or (not (boundp 'custom-menu-nesting))
3176 (>= custom-menu-nesting 0))
3177 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3178 (let ((custom-prefix-list (custom-prefix-add symbol
3179 custom-prefix-list))
3180 (members (custom-sort-items (get symbol 'custom-group)
3181 custom-menu-sort-alphabetically
3182 custom-menu-order-groups)))
3183 (custom-load-symbol symbol)
3184 `(,(custom-unlispify-menu-entry symbol t)
3185 ,item
3186 "--"
3187 ,@(mapcar (lambda (entry)
3188 (widget-apply (if (listp (nth 1 entry))
3189 (nth 1 entry)
3190 (list (nth 1 entry)))
3191 :custom-menu (nth 0 entry)))
3192 members)))
3193 item)))
3194
3195 ;;;###autoload
3196 (defun customize-menu-create (symbol &optional name)
3197 "Return a customize menu for customization group SYMBOL.
3198 If optional NAME is given, use that as the name of the menu.
3199 Otherwise the menu will be named `Customize'.
3200 The format is suitable for use with `easy-menu-define'."
3201 (unless name
3202 (setq name "Customize"))
3203 (if (string-match "XEmacs" emacs-version)
3204 ;; We can delay it under XEmacs.
3205 `(,name
3206 :filter (lambda (&rest junk)
3207 (cdr (custom-menu-create ',symbol))))
3208 ;; But we must create it now under Emacs.
3209 (cons name (cdr (custom-menu-create symbol)))))
3210
3211 ;;; The Custom Mode.
3212
3213 (defvar custom-mode-map nil
3214 "Keymap for `custom-mode'.")
3215
3216 (unless custom-mode-map
3217 (setq custom-mode-map (make-sparse-keymap))
3218 (set-keymap-parent custom-mode-map widget-keymap)
3219 (suppress-keymap custom-mode-map)
3220 (define-key custom-mode-map " " 'scroll-up)
3221 (define-key custom-mode-map "\177" 'scroll-down)
3222 (define-key custom-mode-map "q" 'bury-buffer)
3223 (define-key custom-mode-map "u" 'Custom-goto-parent)
3224 (define-key custom-mode-map "n" 'widget-forward)
3225 (define-key custom-mode-map "p" 'widget-backward)
3226 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3227
3228 (defun Custom-move-and-invoke (event)
3229 "Move to where you click, and if it is an active field, invoke it."
3230 (interactive "e")
3231 (mouse-set-point event)
3232 (if (widget-event-point event)
3233 (let* ((pos (widget-event-point event))
3234 (button (get-char-property pos 'button)))
3235 (if button
3236 (widget-button-click event)))))
3237
3238 (easy-menu-define Custom-mode-menu
3239 custom-mode-map
3240 "Menu used in customization buffers."
3241 `("Custom"
3242 ,(customize-menu-create 'customize)
3243 ["Set" Custom-set t]
3244 ["Save" Custom-save t]
3245 ["Reset to Current" Custom-reset-current t]
3246 ["Reset to Saved" Custom-reset-saved t]
3247 ["Reset to Standard Settings" Custom-reset-standard t]
3248 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
3249
3250 (defun Custom-goto-parent ()
3251 "Go to the parent group listed at the top of this buffer.
3252 If several parents are listed, go to the first of them."
3253 (interactive)
3254 (save-excursion
3255 (goto-char (point-min))
3256 (if (search-forward "\nGo to parent group: " nil t)
3257 (let* ((button (get-char-property (point) 'button))
3258 (parent (downcase (widget-get button :tag))))
3259 (customize-group parent)))))
3260
3261 (defcustom custom-mode-hook nil
3262 "Hook called when entering custom-mode."
3263 :type 'hook
3264 :group 'custom-buffer )
3265
3266 (defun custom-state-buffer-message (widget)
3267 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3268 (message "To install your edits, invoke [State] and choose the Set operation")))
3269
3270 (defun custom-mode ()
3271 "Major mode for editing customization buffers.
3272
3273 The following commands are available:
3274
3275 Move to next button or editable field. \\[widget-forward]
3276 Move to previous button or editable field. \\[widget-backward]
3277 \\<widget-field-keymap>\
3278 Complete content of editable text field. \\[widget-complete]
3279 \\<custom-mode-map>\
3280 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3281 Invoke button under point. \\[widget-button-press]
3282 Set all modifications. \\[Custom-set]
3283 Make all modifications default. \\[Custom-save]
3284 Reset all modified options. \\[Custom-reset-current]
3285 Reset all modified or set options. \\[Custom-reset-saved]
3286 Reset all options. \\[Custom-reset-standard]
3287
3288 Entry to this mode calls the value of `custom-mode-hook'
3289 if that value is non-nil."
3290 (kill-all-local-variables)
3291 (setq major-mode 'custom-mode
3292 mode-name "Custom")
3293 (use-local-map custom-mode-map)
3294 (easy-menu-add Custom-mode-menu)
3295 (make-local-variable 'custom-options)
3296 (make-local-variable 'widget-documentation-face)
3297 (setq widget-documentation-face 'custom-documentation-face)
3298 (make-local-variable 'widget-button-face)
3299 (setq widget-button-face 'custom-button-face)
3300 (make-local-hook 'widget-edit-functions)
3301 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3302 (run-hooks 'custom-mode-hook))
3303
3304 ;;; The End.
3305
3306 (provide 'cus-edit)
3307
3308 ;; cus-edit.el ends here