]> code.delx.au - gnu-emacs/blob - lisp/cus-edit.el
(time-stamp-old-format-warn): Fix a tag string.
[gnu-emacs] / lisp / cus-edit.el
1 ;;; cus-edit.el --- Tools for customization Emacs.
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.90
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 ;;; Code:
34
35 (require 'cus-face)
36 (require 'wid-edit)
37 (require 'easymenu)
38 (eval-when-compile (require 'cl))
39
40 (condition-case nil
41 (require 'cus-load)
42 (error nil))
43
44 (defun custom-face-display-set (face spec &optional frame)
45 (face-spec-set face spec frame))
46
47 (defun custom-display-match-frame (display frame)
48 (face-spec-set-match-display display frame))
49
50 (define-widget-keywords :custom-prefixes :custom-menu :custom-show
51 :custom-magic :custom-state :custom-level :custom-form
52 :custom-set :custom-save :custom-reset-current :custom-reset-saved
53 :custom-reset-factory)
54
55 (put 'custom-define-hook 'custom-type 'hook)
56 (put 'custom-define-hook 'factory-value '(nil))
57 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
58
59 ;;; Customization Groups.
60
61 (defgroup emacs nil
62 "Customization of the One True Editor."
63 :link '(custom-manual "(emacs)Top"))
64
65 ;; Most of these groups are stolen from `finder.el',
66 (defgroup editing nil
67 "Basic text editing facilities."
68 :group 'emacs)
69
70 (defgroup abbrev nil
71 "Abbreviation handling, typing shortcuts, macros."
72 :tag "Abbreviations"
73 :group 'editing)
74
75 (defgroup matching nil
76 "Various sorts of searching and matching."
77 :group 'editing)
78
79 (defgroup emulations nil
80 "Emulations of other editors."
81 :group 'editing)
82
83 (defgroup mouse nil
84 "Mouse support."
85 :group 'editing)
86
87 (defgroup outlines nil
88 "Support for hierarchical outlining."
89 :group 'editing)
90
91 (defgroup external nil
92 "Interfacing to external utilities."
93 :group 'emacs)
94
95 (defgroup bib nil
96 "Code related to the `bib' bibliography processor."
97 :tag "Bibliography"
98 :group 'external)
99
100 (defgroup processes nil
101 "Process, subshell, compilation, and job control support."
102 :group 'external
103 :group 'development)
104
105 (defgroup programming nil
106 "Support for programming in other languages."
107 :group 'emacs)
108
109 (defgroup languages nil
110 "Specialized modes for editing programming languages."
111 :group 'programming)
112
113 (defgroup lisp nil
114 "Lisp support, including Emacs Lisp."
115 :group 'languages
116 :group 'development)
117
118 (defgroup c nil
119 "Support for the C language and related languages."
120 :group 'languages)
121
122 (defgroup tools nil
123 "Programming tools."
124 :group 'programming)
125
126 (defgroup oop nil
127 "Support for object-oriented programming."
128 :group 'programming)
129
130 (defgroup applications nil
131 "Applications written in Emacs."
132 :group 'emacs)
133
134 (defgroup calendar nil
135 "Calendar and time management support."
136 :group 'applications)
137
138 (defgroup mail nil
139 "Modes for electronic-mail handling."
140 :group 'applications)
141
142 (defgroup news nil
143 "Support for netnews reading and posting."
144 :group 'applications)
145
146 (defgroup games nil
147 "Games, jokes and amusements."
148 :group 'applications)
149
150 (defgroup development nil
151 "Support for further development of Emacs."
152 :group 'emacs)
153
154 (defgroup docs nil
155 "Support for Emacs documentation."
156 :group 'development)
157
158 (defgroup extensions nil
159 "Emacs Lisp language extensions."
160 :group 'development)
161
162 (defgroup internal nil
163 "Code for Emacs internals, build process, defaults."
164 :group 'development)
165
166 (defgroup maint nil
167 "Maintenance aids for the Emacs development group."
168 :tag "Maintenance"
169 :group 'development)
170
171 (defgroup environment nil
172 "Fitting Emacs with its environment."
173 :group 'emacs)
174
175 (defgroup comm nil
176 "Communications, networking, remote access to files."
177 :tag "Communication"
178 :group 'environment)
179
180 (defgroup hardware nil
181 "Support for interfacing with exotic hardware."
182 :group 'environment)
183
184 (defgroup terminals nil
185 "Support for terminal types."
186 :group 'environment)
187
188 (defgroup unix nil
189 "Front-ends/assistants for, or emulators of, UNIX features."
190 :group 'environment)
191
192 (defgroup vms nil
193 "Support code for vms."
194 :group 'environment)
195
196 (defgroup i18n nil
197 "Internationalization and alternate character-set support."
198 :group 'environment
199 :group 'editing)
200
201 (defgroup frames nil
202 "Support for Emacs frames and window systems."
203 :group 'environment)
204
205 (defgroup data nil
206 "Support editing files of data."
207 :group 'emacs)
208
209 (defgroup wp nil
210 "Word processing."
211 :group 'emacs)
212
213 (defgroup tex nil
214 "Code related to the TeX formatter."
215 :group 'wp)
216
217 (defgroup faces nil
218 "Support for multiple fonts."
219 :group 'emacs)
220
221 (defgroup hypermedia nil
222 "Support for links between text or other media types."
223 :group 'emacs)
224
225 (defgroup help nil
226 "Support for on-line help systems."
227 :group 'emacs)
228
229 (defgroup local nil
230 "Code local to your site."
231 :group 'emacs)
232
233 (defgroup customize '((widgets custom-group))
234 "Customization of the Customization support."
235 :link '(custom-manual "(custom)Top")
236 :link '(url-link :tag "Development Page"
237 "http://www.dina.kvl.dk/~abraham/custom/")
238 :prefix "custom-"
239 :group 'help)
240
241 (defgroup custom-faces nil
242 "Faces used by customize."
243 :group 'customize
244 :group 'faces)
245
246 (defgroup abbrev-mode nil
247 "Word abbreviations mode."
248 :group 'abbrev)
249
250 (defgroup alloc nil
251 "Storage allocation and gc for GNU Emacs Lisp interpreter."
252 :tag "Storage Allocation"
253 :group 'internal)
254
255 (defgroup undo nil
256 "Undoing changes in buffers."
257 :group 'editing)
258
259 (defgroup modeline nil
260 "Content of the modeline."
261 :group 'environment)
262
263 (defgroup fill nil
264 "Indenting and filling text."
265 :group 'editing)
266
267 (defgroup editing-basics nil
268 "Most basic editing facilities."
269 :group 'editing)
270
271 (defgroup display nil
272 "How characters are displayed in buffers."
273 :group 'environment)
274
275 (defgroup execute nil
276 "Executing external commands."
277 :group 'processes)
278
279 (defgroup installation nil
280 "The Emacs installation."
281 :group 'environment)
282
283 (defgroup dired nil
284 "Directory editing."
285 :group 'environment)
286
287 (defgroup limits nil
288 "Internal Emacs limits."
289 :group 'internal)
290
291 (defgroup debug nil
292 "Debugging Emacs itself."
293 :group 'development)
294
295 (defgroup minibuffer nil
296 "Controling the behaviour of the minibuffer."
297 :group 'environment)
298
299 (defgroup keyboard nil
300 "Input from the keyboard."
301 :group 'environment)
302
303 (defgroup mouse nil
304 "Input from the mouse."
305 :group 'environment)
306
307 (defgroup menu nil
308 "Input from the menus."
309 :group 'environment)
310
311 (defgroup auto-save nil
312 "Preventing accidential loss of data."
313 :group 'data)
314
315 (defgroup processes-basics nil
316 "Basic stuff dealing with processes."
317 :group 'processes)
318
319 (defgroup windows nil
320 "Windows within a frame."
321 :group 'processes)
322
323 ;;; Utilities.
324
325 (defun custom-quote (sexp)
326 "Quote SEXP iff it is not self quoting."
327 (if (or (memq sexp '(t nil))
328 (and (symbolp sexp)
329 (eq (aref (symbol-name sexp) 0) ?:))
330 (and (listp sexp)
331 (memq (car sexp) '(lambda)))
332 (stringp sexp)
333 (numberp sexp)
334 (and (fboundp 'characterp)
335 (characterp sexp)))
336 sexp
337 (list 'quote sexp)))
338
339 (defun custom-split-regexp-maybe (regexp)
340 "If REGEXP is a string, split it to a list at `\\|'.
341 You can get the original back with from the result with:
342 (mapconcat 'identity result \"\\|\")
343
344 IF REGEXP is not a string, return it unchanged."
345 (if (stringp regexp)
346 (let ((start 0)
347 all)
348 (while (string-match "\\\\|" regexp start)
349 (setq all (cons (substring regexp start (match-beginning 0)) all)
350 start (match-end 0)))
351 (nreverse (cons (substring regexp start) all)))
352 regexp))
353
354 (defun custom-variable-prompt ()
355 ;; Code stolen from `help.el'.
356 "Prompt for a variable, defaulting to the variable at point.
357 Return a list suitable for use in `interactive'."
358 (let ((v (variable-at-point))
359 (enable-recursive-minibuffers t)
360 val)
361 (setq val (completing-read
362 (if v
363 (format "Customize variable (default %s): " v)
364 "Customize variable: ")
365 obarray (lambda (symbol)
366 (and (boundp symbol)
367 (or (get symbol 'custom-type)
368 (user-variable-p symbol))))))
369 (list (if (equal val "")
370 v (intern val)))))
371
372 (defun custom-menu-filter (menu widget)
373 "Convert MENU to the form used by `widget-choose'.
374 MENU should be in the same format as `custom-variable-menu'.
375 WIDGET is the widget to apply the filter entries of MENU on."
376 (let ((result nil)
377 current name action filter)
378 (while menu
379 (setq current (car menu)
380 name (nth 0 current)
381 action (nth 1 current)
382 filter (nth 2 current)
383 menu (cdr menu))
384 (if (or (null filter) (funcall filter widget))
385 (push (cons name action) result)
386 (push name result)))
387 (nreverse result)))
388
389 ;;; Unlispify.
390
391 (defvar custom-prefix-list nil
392 "List of prefixes that should be ignored by `custom-unlispify'")
393
394 (defcustom custom-unlispify-menu-entries t
395 "Display menu entries as words instead of symbols if non nil."
396 :group 'customize
397 :type 'boolean)
398
399 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
400 "Convert symbol into a menu entry."
401 (cond ((not custom-unlispify-menu-entries)
402 (symbol-name symbol))
403 ((get symbol 'custom-tag)
404 (if no-suffix
405 (get symbol 'custom-tag)
406 (concat (get symbol 'custom-tag) "...")))
407 (t
408 (save-excursion
409 (set-buffer (get-buffer-create " *Custom-Work*"))
410 (erase-buffer)
411 (princ symbol (current-buffer))
412 (goto-char (point-min))
413 (when (and (eq (get symbol 'custom-type) 'boolean)
414 (re-search-forward "-p\\'" nil t))
415 (replace-match "" t t)
416 (goto-char (point-min)))
417 (let ((prefixes custom-prefix-list)
418 prefix)
419 (while prefixes
420 (setq prefix (car prefixes))
421 (if (search-forward prefix (+ (point) (length prefix)) t)
422 (progn
423 (setq prefixes nil)
424 (delete-region (point-min) (point)))
425 (setq prefixes (cdr prefixes)))))
426 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
427 (capitalize-region (point-min) (point-max))
428 (unless no-suffix
429 (goto-char (point-max))
430 (insert "..."))
431 (buffer-string)))))
432
433 (defcustom custom-unlispify-tag-names t
434 "Display tag names as words instead of symbols if non nil."
435 :group 'customize
436 :type 'boolean)
437
438 (defun custom-unlispify-tag-name (symbol)
439 "Convert symbol into a menu entry."
440 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
441 (custom-unlispify-menu-entry symbol t)))
442
443 (defun custom-prefix-add (symbol prefixes)
444 ;; Addd SYMBOL to list of ignored PREFIXES.
445 (cons (or (get symbol 'custom-prefix)
446 (concat (symbol-name symbol) "-"))
447 prefixes))
448
449 ;;; Guess.
450
451 (defcustom custom-guess-name-alist
452 '(("-p\\'" boolean)
453 ("-hook\\'" hook)
454 ("-face\\'" face)
455 ("-file\\'" file)
456 ("-function\\'" function)
457 ("-functions\\'" (repeat function))
458 ("-list\\'" (repeat sexp))
459 ("-alist\\'" (repeat (cons sexp sexp))))
460 "Alist of (MATCH TYPE).
461
462 MATCH should be a regexp matching the name of a symbol, and TYPE should
463 be a widget suitable for editing the value of that symbol. The TYPE
464 of the first entry where MATCH matches the name of the symbol will be
465 used.
466
467 This is used for guessing the type of variables not declared with
468 customize."
469 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
470 :group 'customize)
471
472 (defcustom custom-guess-doc-alist
473 '(("\\`\\*?Non-nil " boolean))
474 "Alist of (MATCH TYPE).
475
476 MATCH should be a regexp matching a documentation string, and TYPE
477 should be a widget suitable for editing the value of a variable with
478 that documentation string. The TYPE of the first entry where MATCH
479 matches the name of the symbol will be used.
480
481 This is used for guessing the type of variables not declared with
482 customize."
483 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
484 :group 'customize)
485
486 (defun custom-guess-type (symbol)
487 "Guess a widget suitable for editing the value of SYMBOL.
488 This is done by matching SYMBOL with `custom-guess-name-alist' and
489 if that fails, the doc string with `custom-guess-doc-alist'."
490 (let ((name (symbol-name symbol))
491 (names custom-guess-name-alist)
492 current found)
493 (while names
494 (setq current (car names)
495 names (cdr names))
496 (when (string-match (nth 0 current) name)
497 (setq found (nth 1 current)
498 names nil)))
499 (unless found
500 (let ((doc (documentation-property symbol 'variable-documentation))
501 (docs custom-guess-doc-alist))
502 (when doc
503 (while docs
504 (setq current (car docs)
505 docs (cdr docs))
506 (when (string-match (nth 0 current) doc)
507 (setq found (nth 1 current)
508 docs nil))))))
509 found))
510
511 ;;; Custom Mode Commands.
512
513 (defvar custom-options nil
514 "Customization widgets in the current buffer.")
515
516 (defun custom-set ()
517 "Set changes in all modified options."
518 (interactive)
519 (let ((children custom-options))
520 (mapcar (lambda (child)
521 (when (eq (widget-get child :custom-state) 'modified)
522 (widget-apply child :custom-set)))
523 children)))
524
525 (defun custom-save ()
526 "Set all modified group members and save them."
527 (interactive)
528 (let ((children custom-options))
529 (mapcar (lambda (child)
530 (when (memq (widget-get child :custom-state) '(modified set))
531 (widget-apply child :custom-save)))
532 children))
533 (custom-save-all))
534
535 (defvar custom-reset-menu
536 '(("Current" . custom-reset-current)
537 ("Saved" . custom-reset-saved)
538 ("Factory Settings" . custom-reset-factory))
539 "Alist of actions for the `Reset' button.
540 The key is a string containing the name of the action, the value is a
541 lisp function taking the widget as an element which will be called
542 when the action is chosen.")
543
544 (defun custom-reset (event)
545 "Select item from reset menu."
546 (let* ((completion-ignore-case t)
547 (answer (widget-choose "Reset to"
548 custom-reset-menu
549 event)))
550 (if answer
551 (funcall answer))))
552
553 (defun custom-reset-current ()
554 "Reset all modified group members to their current value."
555 (interactive)
556 (let ((children custom-options))
557 (mapcar (lambda (child)
558 (when (eq (widget-get child :custom-state) 'modified)
559 (widget-apply child :custom-reset-current)))
560 children)))
561
562 (defun custom-reset-saved ()
563 "Reset all modified or set group members to their saved value."
564 (interactive)
565 (let ((children custom-options))
566 (mapcar (lambda (child)
567 (when (eq (widget-get child :custom-state) 'modified)
568 (widget-apply child :custom-reset-current)))
569 children)))
570
571 (defun custom-reset-factory ()
572 "Reset all modified, set, or saved group members to their factory settings."
573 (interactive)
574 (let ((children custom-options))
575 (mapcar (lambda (child)
576 (when (eq (widget-get child :custom-state) 'modified)
577 (widget-apply child :custom-reset-current)))
578 children)))
579
580 ;;; The Customize Commands
581
582 (defun custom-prompt-variable (prompt-var prompt-val)
583 "Prompt for a variable and a value and return them as a list.
584 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
585 prompt for the value. The %s escape in PROMPT-VAL is replaced with
586 the name of the variable.
587
588 If the variable has a `variable-interactive' property, that is used as if
589 it were the arg to `interactive' (which see) to interactively read the value.
590
591 If the variable has a `custom-type' property, it must be a widget and the
592 `:prompt-value' property of that widget will be used for reading the value."
593 (let* ((var (read-variable prompt-var))
594 (minibuffer-help-form '(describe-variable var)))
595 (list var
596 (let ((prop (get var 'variable-interactive))
597 (type (get var 'custom-type))
598 (prompt (format prompt-val var)))
599 (unless (listp type)
600 (setq type (list type)))
601 (cond (prop
602 ;; Use VAR's `variable-interactive' property
603 ;; as an interactive spec for prompting.
604 (call-interactively (list 'lambda '(arg)
605 (list 'interactive prop)
606 'arg)))
607 (type
608 (widget-prompt-value type
609 prompt
610 (if (boundp var)
611 (symbol-value var))
612 (not (boundp var))))
613 (t
614 (eval-minibuffer prompt)))))))
615
616 ;;;###autoload
617 (defun custom-set-value (var val)
618 "Set VARIABLE to VALUE. VALUE is a Lisp object.
619
620 If VARIABLE has a `variable-interactive' property, that is used as if
621 it were the arg to `interactive' (which see) to interactively read the value.
622
623 If VARIABLE has a `custom-type' property, it must be a widget and the
624 `:prompt-value' property of that widget will be used for reading the value."
625 (interactive (custom-prompt-variable "Set variable: "
626 "Set %s to value: "))
627
628 (set var val))
629
630 ;;;###autoload
631 (defun custom-set-variable (var val)
632 "Set the default for VARIABLE to VALUE. VALUE is a Lisp object.
633
634 If VARIABLE has a `custom-set' property, that is used for setting
635 VARIABLE, otherwise `set-default' is used.
636
637 The `customized-value' property of the VARIABLE will be set to a list
638 with a quoted VALUE as its sole list member.
639
640 If VARIABLE has a `variable-interactive' property, that is used as if
641 it were the arg to `interactive' (which see) to interactively read the value.
642
643 If VARIABLE has a `custom-type' property, it must be a widget and the
644 `:prompt-value' property of that widget will be used for reading the value. "
645 (interactive (custom-prompt-variable "Set variable: "
646 "Set customized value for %s to: "))
647 (funcall (or (get var 'custom-set) 'set-default) var val)
648 (put var 'customized-value (list (custom-quote val))))
649
650 ;;;###autoload
651 (defun customize (symbol)
652 "Customize SYMBOL, which must be a customization group."
653 (interactive (list (completing-read "Customize group: (default emacs) "
654 obarray
655 (lambda (symbol)
656 (get symbol 'custom-group))
657 t)))
658
659 (when (stringp symbol)
660 (if (string-equal "" symbol)
661 (setq symbol 'emacs)
662 (setq symbol (intern symbol))))
663 (custom-buffer-create (list (list symbol 'custom-group))))
664
665 ;;;###autoload
666 (defun customize-other-window (symbol)
667 "Customize SYMBOL, which must be a customization group."
668 (interactive (list (completing-read "Customize group: (default emacs) "
669 obarray
670 (lambda (symbol)
671 (get symbol 'custom-group))
672 t)))
673
674 (when (stringp symbol)
675 (if (string-equal "" symbol)
676 (setq symbol 'emacs)
677 (setq symbol (intern symbol))))
678 (custom-buffer-create-other-window (list (list symbol 'custom-group))))
679
680 ;;;###autoload
681 (defun customize-variable (symbol)
682 "Customize SYMBOL, which must be a variable."
683 (interactive (custom-variable-prompt))
684 (custom-buffer-create (list (list symbol 'custom-variable))))
685
686 ;;;###autoload
687 (defun customize-variable-other-window (symbol)
688 "Customize SYMBOL, which must be a variable.
689 Show the buffer in another window, but don't select it."
690 (interactive (custom-variable-prompt))
691 (custom-buffer-create-other-window (list (list symbol 'custom-variable))))
692
693 ;;;###autoload
694 (defun customize-face (&optional symbol)
695 "Customize SYMBOL, which should be a face name or nil.
696 If SYMBOL is nil, customize all faces."
697 (interactive (list (completing-read "Customize face: (default all) "
698 obarray 'custom-facep)))
699 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
700 (let ((found nil))
701 (message "Looking for faces...")
702 (mapcar (lambda (symbol)
703 (setq found (cons (list symbol 'custom-face) found)))
704 (nreverse (mapcar 'intern
705 (sort (mapcar 'symbol-name (face-list))
706 'string<))))
707
708 (custom-buffer-create found))
709 (if (stringp symbol)
710 (setq symbol (intern symbol)))
711 (unless (symbolp symbol)
712 (error "Should be a symbol %S" symbol))
713 (custom-buffer-create (list (list symbol 'custom-face)))))
714
715 ;;;###autoload
716 (defun customize-face-other-window (&optional symbol)
717 "Show customization buffer for FACE in other window."
718 (interactive (list (completing-read "Customize face: "
719 obarray 'custom-facep)))
720 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
721 ()
722 (if (stringp symbol)
723 (setq symbol (intern symbol)))
724 (unless (symbolp symbol)
725 (error "Should be a symbol %S" symbol))
726 (custom-buffer-create-other-window (list (list symbol 'custom-face)))))
727
728 ;;;###autoload
729 (defun customize-customized ()
730 "Customize all user options set since the last save in this session."
731 (interactive)
732 (let ((found nil))
733 (mapatoms (lambda (symbol)
734 (and (get symbol 'customized-face)
735 (custom-facep symbol)
736 (setq found (cons (list symbol 'custom-face) found)))
737 (and (get symbol 'customized-value)
738 (boundp symbol)
739 (setq found
740 (cons (list symbol 'custom-variable) found)))))
741 (if found
742 (custom-buffer-create found)
743 (error "No customized user options"))))
744
745 ;;;###autoload
746 (defun customize-saved ()
747 "Customize all already saved user options."
748 (interactive)
749 (let ((found nil))
750 (mapatoms (lambda (symbol)
751 (and (get symbol 'saved-face)
752 (custom-facep symbol)
753 (setq found (cons (list symbol 'custom-face) found)))
754 (and (get symbol 'saved-value)
755 (boundp symbol)
756 (setq found
757 (cons (list symbol 'custom-variable) found)))))
758 (if found
759 (custom-buffer-create found)
760 (error "No saved user options"))))
761
762 ;;;###autoload
763 (defun customize-apropos (regexp &optional all)
764 "Customize all user options matching REGEXP.
765 If ALL (e.g., started with a prefix key), include options which are not
766 user-settable."
767 (interactive "sCustomize regexp: \nP")
768 (let ((found nil))
769 (mapatoms (lambda (symbol)
770 (when (string-match regexp (symbol-name symbol))
771 (when (get symbol 'custom-group)
772 (setq found (cons (list symbol 'custom-group) found)))
773 (when (custom-facep symbol)
774 (setq found (cons (list symbol 'custom-face) found)))
775 (when (and (boundp symbol)
776 (or (get symbol 'saved-value)
777 (get symbol 'factory-value)
778 (if all
779 (get symbol 'variable-documentation)
780 (user-variable-p symbol))))
781 (setq found
782 (cons (list symbol 'custom-variable) found))))))
783 (if found
784 (custom-buffer-create found)
785 (error "No matches"))))
786
787 ;;; Buffer.
788
789 ;;;###autoload
790 (defun custom-buffer-create (options)
791 "Create a buffer containing OPTIONS.
792 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
793 SYMBOL is a customization option, and WIDGET is a widget for editing
794 that option."
795 (kill-buffer (get-buffer-create "*Customization*"))
796 (switch-to-buffer (get-buffer-create "*Customization*"))
797 (custom-buffer-create-internal options))
798
799 ;;;###autoload
800 (defun custom-buffer-create-other-window (options)
801 "Create a buffer containing OPTIONS.
802 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
803 SYMBOL is a customization option, and WIDGET is a widget for editing
804 that option."
805 (kill-buffer (get-buffer-create "*Customization*"))
806 (let ((window (selected-window)))
807 (switch-to-buffer-other-window (get-buffer-create "*Customization*"))
808 (custom-buffer-create-internal options)
809 (select-window window)))
810
811
812 (defun custom-buffer-create-internal (options)
813 (message "Creating customization buffer...")
814 (custom-mode)
815 (widget-insert "This is a customization buffer.
816 Push RET or click mouse-2 on the word ")
817 ;; (put-text-property 1 2 'start-open nil)
818 (widget-create 'info-link
819 :tag "help"
820 :help-echo "Read the online help."
821 "(custom)The Customization Buffer")
822 (widget-insert " for more information.\n\n")
823 (setq custom-options
824 (if (= (length options) 1)
825 (mapcar (lambda (entry)
826 (widget-create (nth 1 entry)
827 :custom-state 'unknown
828 :tag (custom-unlispify-tag-name
829 (nth 0 entry))
830 :value (nth 0 entry)))
831 options)
832 (let ((count 0)
833 (length (length options)))
834 (mapcar (lambda (entry)
835 (prog2
836 (message "Creating customization items %2d%%..."
837 (/ (* 100.0 count) length))
838 (widget-create (nth 1 entry)
839 :tag (custom-unlispify-tag-name
840 (nth 0 entry))
841 :value (nth 0 entry))
842 (setq count (1+ count))
843 (unless (eq (preceding-char) ?\n)
844 (widget-insert "\n"))
845 (widget-insert "\n")))
846 options))))
847 (unless (eq (preceding-char) ?\n)
848 (widget-insert "\n"))
849 (widget-insert "\n")
850 (message "Creating customization magic...")
851 (mapcar 'custom-magic-reset custom-options)
852 (message "Creating customization buttons...")
853 (widget-create 'push-button
854 :tag "Set"
855 :help-echo "Set all modifications for this session."
856 :action (lambda (widget &optional event)
857 (custom-set)))
858 (widget-insert " ")
859 (widget-create 'push-button
860 :tag "Save"
861 :help-echo "\
862 Make the modifications default for future sessions."
863 :action (lambda (widget &optional event)
864 (custom-save)))
865 (widget-insert " ")
866 (widget-create 'push-button
867 :tag "Reset"
868 :help-echo "Undo all modifications."
869 :action (lambda (widget &optional event)
870 (custom-reset event)))
871 (widget-insert " ")
872 (widget-create 'push-button
873 :tag "Done"
874 :help-echo "Bury the buffer."
875 :action (lambda (widget &optional event)
876 (bury-buffer)
877 ;; Steal button release event.
878 (if (and (fboundp 'button-press-event-p)
879 (fboundp 'next-command-event))
880 ;; XEmacs
881 (and event
882 (button-press-event-p event)
883 (next-command-event))
884 ;; Emacs
885 (when (memq 'down (event-modifiers event))
886 (read-event)))))
887 (widget-insert "\n")
888 (message "Creating customization setup...")
889 (widget-setup)
890 (goto-char (point-min))
891 (forward-line 3) ;Kludge: bob is writable in XEmacs.
892 (message "Creating customization buffer...done"))
893
894 ;;; Modification of Basic Widgets.
895 ;;
896 ;; We add extra properties to the basic widgets needed here. This is
897 ;; fine, as long as we are careful to stay within out own namespace.
898 ;;
899 ;; We want simple widgets to be displayed by default, but complex
900 ;; widgets to be hidden.
901
902 (widget-put (get 'item 'widget-type) :custom-show t)
903 (widget-put (get 'editable-field 'widget-type)
904 :custom-show (lambda (widget value)
905 (let ((pp (pp-to-string value)))
906 (cond ((string-match "\n" pp)
907 nil)
908 ((> (length pp) 40)
909 nil)
910 (t t)))))
911 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
912
913 ;;; The `custom-manual' Widget.
914
915 (define-widget 'custom-manual 'info-link
916 "Link to the manual entry for this customization option."
917 :help-echo "Read the manual entry for this option."
918 :tag "Manual")
919
920 ;;; The `custom-magic' Widget.
921
922 (defface custom-invalid-face '((((class color))
923 (:foreground "yellow" :background "red"))
924 (t
925 (:bold t :italic t :underline t)))
926 "Face used when the customize item is invalid.")
927
928 (defface custom-rogue-face '((((class color))
929 (:foreground "pink" :background "black"))
930 (t
931 (:underline t)))
932 "Face used when the customize item is not defined for customization.")
933
934 (defface custom-modified-face '((((class color))
935 (:foreground "white" :background "blue"))
936 (t
937 (:italic t :bold)))
938 "Face used when the customize item has been modified.")
939
940 (defface custom-set-face '((((class color))
941 (:foreground "blue" :background "white"))
942 (t
943 (:italic t)))
944 "Face used when the customize item has been set.")
945
946 (defface custom-changed-face '((((class color))
947 (:foreground "white" :background "blue"))
948 (t
949 (:italic t)))
950 "Face used when the customize item has been changed.")
951
952 (defface custom-saved-face '((t (:underline t)))
953 "Face used when the customize item has been saved.")
954
955 (defcustom custom-magic-alist '((nil "#" underline "\
956 uninitialized, you should not see this.")
957 (unknown "?" italic "\
958 unknown, you should not see this.")
959 (hidden "-" default "\
960 hidden, press the state button to show.")
961 (invalid "x" custom-invalid-face "\
962 the value displayed for this item is invalid and cannot be set.")
963 (modified "*" custom-modified-face "\
964 you have edited the item, and can now set it.")
965 (set "+" custom-set-face "\
966 you have set this item, but not saved it.")
967 (changed ":" custom-changed-face "\
968 this item has been changed outside customize.")
969 (saved "!" custom-saved-face "\
970 this item has been saved.")
971 (rogue "@" custom-rogue-face "\
972 this item is not prepared for customization.")
973 (factory " " nil "\
974 this item is unchanged from its factory setting."))
975 "Alist of customize option states.
976 Each entry is of the form (STATE MAGIC FACE DESCRIPTION), where
977
978 STATE is one of the following symbols:
979
980 `nil'
981 For internal use, should never occur.
982 `unknown'
983 For internal use, should never occur.
984 `hidden'
985 This item is not being displayed.
986 `invalid'
987 This item is modified, but has an invalid form.
988 `modified'
989 This item is modified, and has a valid form.
990 `set'
991 This item has been set but not saved.
992 `changed'
993 The current value of this item has been changed temporarily.
994 `saved'
995 This item is marked for saving.
996 `rogue'
997 This item has no customization information.
998 `factory'
999 This item is unchanged from the factory default.
1000
1001 MAGIC is a string used to present that state.
1002
1003 FACE is a face used to present the state.
1004
1005 DESCRIPTION is a string describing the state.
1006
1007 The list should be sorted most significant first."
1008 :type '(list (checklist :inline t
1009 (group (const nil)
1010 (string :tag "Magic")
1011 face
1012 (string :tag "Description"))
1013 (group (const unknown)
1014 (string :tag "Magic")
1015 face
1016 (string :tag "Description"))
1017 (group (const hidden)
1018 (string :tag "Magic")
1019 face
1020 (string :tag "Description"))
1021 (group (const invalid)
1022 (string :tag "Magic")
1023 face
1024 (string :tag "Description"))
1025 (group (const modified)
1026 (string :tag "Magic")
1027 face
1028 (string :tag "Description"))
1029 (group (const set)
1030 (string :tag "Magic")
1031 face
1032 (string :tag "Description"))
1033 (group (const changed)
1034 (string :tag "Magic")
1035 face
1036 (string :tag "Description"))
1037 (group (const saved)
1038 (string :tag "Magic")
1039 face
1040 (string :tag "Description"))
1041 (group (const rogue)
1042 (string :tag "Magic")
1043 face
1044 (string :tag "Description"))
1045 (group (const factory)
1046 (string :tag "Magic")
1047 face
1048 (string :tag "Description")))
1049 (editable-list :inline t
1050 (group symbol
1051 (string :tag "Magic")
1052 face
1053 (string :tag "Description"))))
1054 :group 'customize
1055 :group 'custom-faces)
1056
1057 (defcustom custom-magic-show 'long
1058 "Show long description of the state of each customization option."
1059 :type '(choice (const :tag "no" nil)
1060 (const short)
1061 (const long))
1062 :group 'customize)
1063
1064 (defcustom custom-magic-show-button t
1065 "Show a magic button indicating the state of each customization option."
1066 :type 'boolean
1067 :group 'customize)
1068
1069 (define-widget 'custom-magic 'default
1070 "Show and manipulate state for a customization option."
1071 :format "%v"
1072 :action 'widget-choice-item-action
1073 :notify 'ignore
1074 :value-get 'ignore
1075 :value-create 'custom-magic-value-create
1076 :value-delete 'widget-children-value-delete)
1077
1078 (defun custom-magic-value-create (widget)
1079 ;; Create compact status report for WIDGET.
1080 (let* ((parent (widget-get widget :parent))
1081 (state (widget-get parent :custom-state))
1082 (entry (assq state custom-magic-alist))
1083 (magic (nth 1 entry))
1084 (face (nth 2 entry))
1085 (text (nth 3 entry))
1086 (lisp (eq (widget-get parent :custom-form) 'lisp))
1087 children)
1088 (when custom-magic-show
1089 (push (widget-create-child-and-convert widget 'choice-item
1090 :help-echo "\
1091 Change the state of this item."
1092 :format "%[%t%]"
1093 :tag "State")
1094 children)
1095 (insert ": ")
1096 (if (eq custom-magic-show 'long)
1097 (insert text)
1098 (insert (symbol-name state)))
1099 (when lisp
1100 (insert " (lisp)"))
1101 (insert "\n"))
1102 (when custom-magic-show-button
1103 (when custom-magic-show
1104 (let ((indent (widget-get parent :indent)))
1105 (when indent
1106 (insert-char ? indent))))
1107 (push (widget-create-child-and-convert widget 'choice-item
1108 :button-face face
1109 :help-echo "Change the state."
1110 :format "%[%t%]"
1111 :tag (if lisp
1112 (concat "(" magic ")")
1113 (concat "[" magic "]")))
1114 children)
1115 (insert " "))
1116 (widget-put widget :children children)))
1117
1118 (defun custom-magic-reset (widget)
1119 "Redraw the :custom-magic property of WIDGET."
1120 (let ((magic (widget-get widget :custom-magic)))
1121 (widget-value-set magic (widget-value magic))))
1122
1123 ;;; The `custom-level' Widget.
1124
1125 (define-widget 'custom-level 'item
1126 "The custom level buttons."
1127 :format "%[%t%]"
1128 :help-echo "Expand or collapse this item."
1129 :action 'custom-level-action)
1130
1131 (defun custom-level-action (widget &optional event)
1132 "Toggle visibility for parent to WIDGET."
1133 (custom-toggle-hide (widget-get widget :parent)))
1134
1135 ;;; The `custom' Widget.
1136
1137 (define-widget 'custom 'default
1138 "Customize a user option."
1139 :convert-widget 'custom-convert-widget
1140 :format "%l%[%t%]: %v%m%h%a"
1141 :format-handler 'custom-format-handler
1142 :notify 'custom-notify
1143 :custom-level 1
1144 :custom-state 'hidden
1145 :documentation-property 'widget-subclass-responsibility
1146 :value-create 'widget-subclass-responsibility
1147 :value-delete 'widget-children-value-delete
1148 :value-get 'widget-item-value-get
1149 :validate 'widget-editable-list-validate
1150 :match (lambda (widget value) (symbolp value)))
1151
1152 (defun custom-convert-widget (widget)
1153 ;; Initialize :value and :tag from :args in WIDGET.
1154 (let ((args (widget-get widget :args)))
1155 (when args
1156 (widget-put widget :value (widget-apply widget
1157 :value-to-internal (car args)))
1158 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1159 (widget-put widget :args nil)))
1160 widget)
1161
1162 (defun custom-format-handler (widget escape)
1163 ;; We recognize extra escape sequences.
1164 (let* ((buttons (widget-get widget :buttons))
1165 (state (widget-get widget :custom-state))
1166 (level (widget-get widget :custom-level)))
1167 (cond ((eq escape ?l)
1168 (when level
1169 (push (widget-create-child-and-convert
1170 widget 'custom-level (make-string level ?*))
1171 buttons)
1172 (widget-insert " ")
1173 (widget-put widget :buttons buttons)))
1174 ((eq escape ?L)
1175 (when (eq state 'hidden)
1176 (widget-insert " ...")))
1177 ((eq escape ?m)
1178 (and (eq (preceding-char) ?\n)
1179 (widget-get widget :indent)
1180 (insert-char ? (widget-get widget :indent)))
1181 (let ((magic (widget-create-child-and-convert
1182 widget 'custom-magic nil)))
1183 (widget-put widget :custom-magic magic)
1184 (push magic buttons)
1185 (widget-put widget :buttons buttons)))
1186 ((eq escape ?a)
1187 (let* ((symbol (widget-get widget :value))
1188 (links (get symbol 'custom-links))
1189 (many (> (length links) 2)))
1190 (when links
1191 (and (eq (preceding-char) ?\n)
1192 (widget-get widget :indent)
1193 (insert-char ? (widget-get widget :indent)))
1194 (insert "See also ")
1195 (while links
1196 (push (widget-create-child-and-convert widget (car links))
1197 buttons)
1198 (setq links (cdr links))
1199 (cond ((null links)
1200 (insert ".\n"))
1201 ((null (cdr links))
1202 (if many
1203 (insert ", and ")
1204 (insert " and ")))
1205 (t
1206 (insert ", "))))
1207 (widget-put widget :buttons buttons))))
1208 (t
1209 (widget-default-format-handler widget escape)))))
1210
1211 (defun custom-notify (widget &rest args)
1212 "Keep track of changes."
1213 (unless (memq (widget-get widget :custom-state) '(nil unknown hidden))
1214 (widget-put widget :custom-state 'modified))
1215 (let ((buffer-undo-list t))
1216 (custom-magic-reset widget))
1217 (apply 'widget-default-notify widget args))
1218
1219 (defun custom-redraw (widget)
1220 "Redraw WIDGET with current settings."
1221 (let ((line (count-lines (point-min) (point)))
1222 (column (current-column))
1223 (pos (point))
1224 (from (marker-position (widget-get widget :from)))
1225 (to (marker-position (widget-get widget :to))))
1226 (save-excursion
1227 (widget-value-set widget (widget-value widget))
1228 (custom-redraw-magic widget))
1229 (when (and (>= pos from) (<= pos to))
1230 (condition-case nil
1231 (progn
1232 (goto-line line)
1233 (move-to-column column))
1234 (error nil)))))
1235
1236 (defun custom-redraw-magic (widget)
1237 "Redraw WIDGET state with current settings."
1238 (while widget
1239 (let ((magic (widget-get widget :custom-magic)))
1240 (unless magic
1241 (debug))
1242 (widget-value-set magic (widget-value magic))
1243 (when (setq widget (widget-get widget :group))
1244 (custom-group-state-update widget))))
1245 (widget-setup))
1246
1247 (defun custom-show (widget value)
1248 "Non-nil if WIDGET should be shown with VALUE by default."
1249 (let ((show (widget-get widget :custom-show)))
1250 (cond ((null show)
1251 nil)
1252 ((eq t show)
1253 t)
1254 (t
1255 (funcall show widget value)))))
1256
1257 (defvar custom-load-recursion nil
1258 "Hack to avoid recursive dependencies.")
1259
1260 (defun custom-load-symbol (symbol)
1261 "Load all dependencies for SYMBOL."
1262 (unless custom-load-recursion
1263 (let ((custom-load-recursion t)
1264 (loads (get symbol 'custom-loads))
1265 load)
1266 (while loads
1267 (setq load (car loads)
1268 loads (cdr loads))
1269 (cond ((symbolp load)
1270 (condition-case nil
1271 (require load)
1272 (error nil)))
1273 ((assoc load load-history))
1274 (t
1275 (condition-case nil
1276 (load-library load)
1277 (error nil))))))))
1278
1279 (defun custom-load-widget (widget)
1280 "Load all dependencies for WIDGET."
1281 (custom-load-symbol (widget-value widget)))
1282
1283 (defun custom-toggle-hide (widget)
1284 "Toggle visibility of WIDGET."
1285 (let ((state (widget-get widget :custom-state)))
1286 (cond ((memq state '(invalid modified))
1287 (error "There are unset changes"))
1288 ((eq state 'hidden)
1289 (widget-put widget :custom-state 'unknown))
1290 (t
1291 (widget-put widget :custom-state 'hidden)))
1292 (custom-redraw widget)))
1293
1294 ;;; The `custom-variable' Widget.
1295
1296 (defface custom-variable-sample-face '((t (:underline t)))
1297 "Face used for unpushable variable tags."
1298 :group 'custom-faces)
1299
1300 (defface custom-variable-button-face '((t (:underline t :bold t)))
1301 "Face used for pushable variable tags."
1302 :group 'custom-faces)
1303
1304 (define-widget 'custom-variable 'custom
1305 "Customize variable."
1306 :format "%l%v%m%h%a"
1307 :help-echo "Set or reset this variable."
1308 :documentation-property 'variable-documentation
1309 :custom-state nil
1310 :custom-menu 'custom-variable-menu-create
1311 :custom-form 'edit
1312 :value-create 'custom-variable-value-create
1313 :action 'custom-variable-action
1314 :custom-set 'custom-variable-set
1315 :custom-save 'custom-variable-save
1316 :custom-reset-current 'custom-redraw
1317 :custom-reset-saved 'custom-variable-reset-saved
1318 :custom-reset-factory 'custom-variable-reset-factory)
1319
1320 (defun custom-variable-type (symbol)
1321 "Return a widget suitable for editing the value of SYMBOL.
1322 If SYMBOL has a `custom-type' property, use that.
1323 Otherwise, look up symbol in `custom-guess-type-alist'."
1324 (let* ((type (or (get symbol 'custom-type)
1325 (and (not (get symbol 'factory-value))
1326 (custom-guess-type symbol))
1327 'sexp))
1328 (options (get symbol 'custom-options))
1329 (tmp (if (listp type)
1330 (copy-sequence type)
1331 (list type))))
1332 (when options
1333 (widget-put tmp :options options))
1334 tmp))
1335
1336 (defun custom-variable-value-create (widget)
1337 "Here is where you edit the variables value."
1338 (custom-load-widget widget)
1339 (let* ((buttons (widget-get widget :buttons))
1340 (children (widget-get widget :children))
1341 (form (widget-get widget :custom-form))
1342 (state (widget-get widget :custom-state))
1343 (symbol (widget-get widget :value))
1344 (tag (widget-get widget :tag))
1345 (type (custom-variable-type symbol))
1346 (conv (widget-convert type))
1347 (get (or (get symbol 'custom-get) 'default-value))
1348 (set (or (get symbol 'custom-set) 'set-default))
1349 (value (if (default-boundp symbol)
1350 (funcall get symbol)
1351 (widget-get conv :value))))
1352 ;; If the widget is new, the child determine whether it is hidden.
1353 (cond (state)
1354 ((custom-show type value)
1355 (setq state 'unknown))
1356 (t
1357 (setq state 'hidden)))
1358 ;; If we don't know the state, see if we need to edit it in lisp form.
1359 (when (eq state 'unknown)
1360 (unless (widget-apply conv :match value)
1361 ;; (widget-apply (widget-convert type) :match value)
1362 (setq form 'lisp)))
1363 ;; Now we can create the child widget.
1364 (cond ((eq state 'hidden)
1365 ;; Indicate hidden value.
1366 (push (widget-create-child-and-convert
1367 widget 'item
1368 :format "%{%t%}: ..."
1369 :sample-face 'custom-variable-sample-face
1370 :tag tag
1371 :parent widget)
1372 children))
1373 ((eq form 'lisp)
1374 ;; In lisp mode edit the saved value when possible.
1375 (let* ((value (cond ((get symbol 'saved-value)
1376 (car (get symbol 'saved-value)))
1377 ((get symbol 'factory-value)
1378 (car (get symbol 'factory-value)))
1379 ((default-boundp symbol)
1380 (custom-quote (funcall get symbol)))
1381 (t
1382 (custom-quote (widget-get conv :value))))))
1383 (push (widget-create-child-and-convert
1384 widget 'sexp
1385 :button-face 'custom-variable-button-face
1386 :tag (symbol-name symbol)
1387 :parent widget
1388 :value value)
1389 children)))
1390 (t
1391 ;; Edit mode.
1392 (push (widget-create-child-and-convert
1393 widget type
1394 :tag tag
1395 :button-face 'custom-variable-button-face
1396 :sample-face 'custom-variable-sample-face
1397 :value value)
1398 children)))
1399 ;; Now update the state.
1400 (unless (eq (preceding-char) ?\n)
1401 (widget-insert "\n"))
1402 (if (eq state 'hidden)
1403 (widget-put widget :custom-state state)
1404 (custom-variable-state-set widget))
1405 (widget-put widget :custom-form form)
1406 (widget-put widget :buttons buttons)
1407 (widget-put widget :children children)))
1408
1409 (defun custom-variable-state-set (widget)
1410 "Set the state of WIDGET."
1411 (let* ((symbol (widget-value widget))
1412 (get (or (get symbol 'custom-get) 'default-value))
1413 (value (if (default-boundp symbol)
1414 (funcall get symbol)
1415 (widget-get widget :value)))
1416 tmp
1417 (state (cond ((setq tmp (get symbol 'customized-value))
1418 (if (condition-case nil
1419 (equal value (eval (car tmp)))
1420 (error nil))
1421 'set
1422 'changed))
1423 ((setq tmp (get symbol 'saved-value))
1424 (if (condition-case nil
1425 (equal value (eval (car tmp)))
1426 (error nil))
1427 'saved
1428 'changed))
1429 ((setq tmp (get symbol 'factory-value))
1430 (if (condition-case nil
1431 (equal value (eval (car tmp)))
1432 (error nil))
1433 'factory
1434 'changed))
1435 (t 'rogue))))
1436 (widget-put widget :custom-state state)))
1437
1438 (defvar custom-variable-menu
1439 '(("Hide" custom-toggle-hide
1440 (lambda (widget)
1441 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
1442 ("Edit" custom-variable-edit
1443 (lambda (widget)
1444 (not (eq (widget-get widget :custom-form) 'edit))))
1445 ("Edit Lisp" custom-variable-edit-lisp
1446 (lambda (widget)
1447 (not (eq (widget-get widget :custom-form) 'lisp))))
1448 ("Set" custom-variable-set
1449 (lambda (widget)
1450 (eq (widget-get widget :custom-state) 'modified)))
1451 ("Save" custom-variable-save
1452 (lambda (widget)
1453 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1454 ("Reset to Current" custom-redraw
1455 (lambda (widget)
1456 (and (default-boundp (widget-value widget))
1457 (memq (widget-get widget :custom-state) '(modified)))))
1458 ("Reset to Saved" custom-variable-reset-saved
1459 (lambda (widget)
1460 (and (get (widget-value widget) 'saved-value)
1461 (memq (widget-get widget :custom-state)
1462 '(modified set changed rogue)))))
1463 ("Reset to Factory Settings" custom-variable-reset-factory
1464 (lambda (widget)
1465 (and (get (widget-value widget) 'factory-value)
1466 (memq (widget-get widget :custom-state)
1467 '(modified set changed saved rogue))))))
1468 "Alist of actions for the `custom-variable' widget.
1469 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1470 the menu entry, ACTION is the function to call on the widget when the
1471 menu is selected, and FILTER is a predicate which takes a `custom-variable'
1472 widget as an argument, and returns non-nil if ACTION is valid on that
1473 widget. If FILTER is nil, ACTION is always valid.")
1474
1475 (defun custom-variable-action (widget &optional event)
1476 "Show the menu for `custom-variable' WIDGET.
1477 Optional EVENT is the location for the menu."
1478 (if (eq (widget-get widget :custom-state) 'hidden)
1479 (custom-toggle-hide widget)
1480 (let* ((completion-ignore-case t)
1481 (answer (widget-choose (custom-unlispify-tag-name
1482 (widget-get widget :value))
1483 (custom-menu-filter custom-variable-menu
1484 widget)
1485 event)))
1486 (if answer
1487 (funcall answer widget)))))
1488
1489 (defun custom-variable-edit (widget)
1490 "Edit value of WIDGET."
1491 (widget-put widget :custom-state 'unknown)
1492 (widget-put widget :custom-form 'edit)
1493 (custom-redraw widget))
1494
1495 (defun custom-variable-edit-lisp (widget)
1496 "Edit the lisp representation of the value of WIDGET."
1497 (widget-put widget :custom-state 'unknown)
1498 (widget-put widget :custom-form 'lisp)
1499 (custom-redraw widget))
1500
1501 (defun custom-variable-set (widget)
1502 "Set the current value for the variable being edited by WIDGET."
1503 (let* ((form (widget-get widget :custom-form))
1504 (state (widget-get widget :custom-state))
1505 (child (car (widget-get widget :children)))
1506 (symbol (widget-value widget))
1507 (set (or (get symbol 'custom-set) 'set-default))
1508 val)
1509 (cond ((eq state 'hidden)
1510 (error "Cannot set hidden variable."))
1511 ((setq val (widget-apply child :validate))
1512 (goto-char (widget-get val :from))
1513 (error "%s" (widget-get val :error)))
1514 ((eq form 'lisp)
1515 (funcall set symbol (eval (setq val (widget-value child))))
1516 (put symbol 'customized-value (list val)))
1517 (t
1518 (funcall set symbol (setq val (widget-value child)))
1519 (put symbol 'customized-value (list (custom-quote val)))))
1520 (custom-variable-state-set widget)
1521 (custom-redraw-magic widget)))
1522
1523 (defun custom-variable-save (widget)
1524 "Set the default value for the variable being edited by WIDGET."
1525 (let* ((form (widget-get widget :custom-form))
1526 (state (widget-get widget :custom-state))
1527 (child (car (widget-get widget :children)))
1528 (symbol (widget-value widget))
1529 (set (or (get symbol 'custom-set) 'set-default))
1530 val)
1531 (cond ((eq state 'hidden)
1532 (error "Cannot set hidden variable."))
1533 ((setq val (widget-apply child :validate))
1534 (goto-char (widget-get val :from))
1535 (error "%s" (widget-get val :error)))
1536 ((eq form 'lisp)
1537 (put symbol 'saved-value (list (widget-value child)))
1538 (funcall set symbol (eval (widget-value child))))
1539 (t
1540 (put symbol
1541 'saved-value (list (custom-quote (widget-value
1542 child))))
1543 (funcall set symbol (widget-value child))))
1544 (put symbol 'customized-value nil)
1545 (custom-save-all)
1546 (custom-variable-state-set widget)
1547 (custom-redraw-magic widget)))
1548
1549 (defun custom-variable-reset-saved (widget)
1550 "Restore the saved value for the variable being edited by WIDGET."
1551 (let* ((symbol (widget-value widget))
1552 (set (or (get symbol 'custom-set) 'set-default)))
1553 (if (get symbol 'saved-value)
1554 (condition-case nil
1555 (funcall set symbol (eval (car (get symbol 'saved-value))))
1556 (error nil))
1557 (error "No saved value for %s" symbol))
1558 (put symbol 'customized-value nil)
1559 (widget-put widget :custom-state 'unknown)
1560 (custom-redraw widget)))
1561
1562 (defun custom-variable-reset-factory (widget)
1563 "Restore the factory setting for the variable being edited by WIDGET."
1564 (let* ((symbol (widget-value widget))
1565 (set (or (get symbol 'custom-set) 'set-default)))
1566 (if (get symbol 'factory-value)
1567 (funcall set symbol (eval (car (get symbol 'factory-value))))
1568 (error "No factory default for %S" symbol))
1569 (put symbol 'customized-value nil)
1570 (when (get symbol 'saved-value)
1571 (put symbol 'saved-value nil)
1572 (custom-save-all))
1573 (widget-put widget :custom-state 'unknown)
1574 (custom-redraw widget)))
1575
1576 ;;; The `custom-face-edit' Widget.
1577
1578 (define-widget 'custom-face-edit 'checklist
1579 "Edit face attributes."
1580 :format "%t: %v"
1581 :tag "Attributes"
1582 :extra-offset 12
1583 :button-args '(:help-echo "Control whether this attribute have any effect.")
1584 :args (mapcar (lambda (att)
1585 (list 'group
1586 :inline t
1587 :sibling-args (widget-get (nth 1 att) :sibling-args)
1588 (list 'const :format "" :value (nth 0 att))
1589 (nth 1 att)))
1590 custom-face-attributes))
1591
1592 ;;; The `custom-display' Widget.
1593
1594 (define-widget 'custom-display 'menu-choice
1595 "Select a display type."
1596 :tag "Display"
1597 :value t
1598 :help-echo "Specify frames where the face attributes should be used."
1599 :args '((const :tag "all" t)
1600 (checklist
1601 :offset 0
1602 :extra-offset 9
1603 :args ((group :sibling-args (:help-echo "\
1604 Only match the specified window systems.")
1605 (const :format "Type: "
1606 type)
1607 (checklist :inline t
1608 :offset 0
1609 (const :format "X "
1610 :sibling-args (:help-echo "\
1611 The X11 Window System.")
1612 x)
1613 (const :format "PM "
1614 :sibling-args (:help-echo "\
1615 OS/2 Presentation Manager.")
1616 pm)
1617 (const :format "Win32 "
1618 :sibling-args (:help-echo "\
1619 Windows NT/95/97.")
1620 win32)
1621 (const :format "DOS "
1622 :sibling-args (:help-echo "\
1623 Plain MS-DOS.")
1624 pc)
1625 (const :format "TTY%n"
1626 :sibling-args (:help-echo "\
1627 Plain text terminals.")
1628 tty)))
1629 (group :sibling-args (:help-echo "\
1630 Only match the frames with the specified color support.")
1631 (const :format "Class: "
1632 class)
1633 (checklist :inline t
1634 :offset 0
1635 (const :format "Color "
1636 :sibling-args (:help-echo "\
1637 Match color frames.")
1638 color)
1639 (const :format "Grayscale "
1640 :sibling-args (:help-echo "\
1641 Match grayscale frames.")
1642 grayscale)
1643 (const :format "Monochrome%n"
1644 :sibling-args (:help-echo "\
1645 Match frames with no color support.")
1646 mono)))
1647 (group :sibling-args (:help-echo "\
1648 Only match frames with the specified intensity.")
1649 (const :format "\
1650 Background brightness: "
1651 background)
1652 (checklist :inline t
1653 :offset 0
1654 (const :format "Light "
1655 :sibling-args (:help-echo "\
1656 Match frames with light backgrounds.")
1657 light)
1658 (const :format "Dark\n"
1659 :sibling-args (:help-echo "\
1660 Match frames with dark backgrounds.")
1661 dark)))))))
1662
1663 ;;; The `custom-face' Widget.
1664
1665 (defface custom-face-tag-face '((t (:underline t)))
1666 "Face used for face tags."
1667 :group 'custom-faces)
1668
1669 (define-widget 'custom-face 'custom
1670 "Customize face."
1671 :format "%l%{%t%}: %s%m%h%a%v"
1672 :format-handler 'custom-face-format-handler
1673 :sample-face 'custom-face-tag-face
1674 :help-echo "Set or reset this face."
1675 :documentation-property '(lambda (face)
1676 (face-doc-string face))
1677 :value-create 'custom-face-value-create
1678 :action 'custom-face-action
1679 :custom-form 'selected
1680 :custom-set 'custom-face-set
1681 :custom-save 'custom-face-save
1682 :custom-reset-current 'custom-redraw
1683 :custom-reset-saved 'custom-face-reset-saved
1684 :custom-reset-factory 'custom-face-reset-factory
1685 :custom-menu 'custom-face-menu-create)
1686
1687 (defun custom-face-format-handler (widget escape)
1688 ;; We recognize extra escape sequences.
1689 (let (child
1690 (symbol (widget-get widget :value)))
1691 (cond ((eq escape ?s)
1692 (and (string-match "XEmacs" emacs-version)
1693 ;; XEmacs cannot display initialized faces.
1694 (not (custom-facep symbol))
1695 (copy-face 'custom-face-empty symbol))
1696 (setq child (widget-create-child-and-convert
1697 widget 'item
1698 :format "(%{%t%})\n"
1699 :sample-face symbol
1700 :tag "sample")))
1701 (t
1702 (custom-format-handler widget escape)))
1703 (when child
1704 (widget-put widget
1705 :buttons (cons child (widget-get widget :buttons))))))
1706
1707 (define-widget 'custom-face-all 'editable-list
1708 "An editable list of display specifications and attributes."
1709 :entry-format "%i %d %v"
1710 :insert-button-args '(:help-echo "Insert new display specification here.")
1711 :append-button-args '(:help-echo "Append new display specification here.")
1712 :delete-button-args '(:help-echo "Delete this display specification.")
1713 :args '((group :format "%v" custom-display custom-face-edit)))
1714
1715 (defconst custom-face-all (widget-convert 'custom-face-all)
1716 "Converted version of the `custom-face-all' widget.")
1717
1718 (define-widget 'custom-display-unselected 'item
1719 "A display specification that doesn't match the selected display."
1720 :match 'custom-display-unselected-match)
1721
1722 (defun custom-display-unselected-match (widget value)
1723 "Non-nil if VALUE is an unselected display specification."
1724 (not (custom-display-match-frame value (selected-frame))))
1725
1726 (define-widget 'custom-face-selected 'group
1727 "Edit the attributes of the selected display in a face specification."
1728 :args '((repeat :format ""
1729 :inline t
1730 (group custom-display-unselected sexp))
1731 (group (sexp :format "") custom-face-edit)
1732 (repeat :format ""
1733 :inline t
1734 sexp)))
1735
1736 (defconst custom-face-selected (widget-convert 'custom-face-selected)
1737 "Converted version of the `custom-face-selected' widget.")
1738
1739 (defun custom-face-value-create (widget)
1740 ;; Create a list of the display specifications.
1741 (unless (eq (preceding-char) ?\n)
1742 (insert "\n"))
1743 (when (not (eq (widget-get widget :custom-state) 'hidden))
1744 (message "Creating face editor...")
1745 (custom-load-widget widget)
1746 (let* ((symbol (widget-value widget))
1747 (spec (or (get symbol 'saved-face)
1748 (get symbol 'factory-face)
1749 ;; Attempt to construct it.
1750 (list (list t (custom-face-attributes-get
1751 symbol (selected-frame))))))
1752 (form (widget-get widget :custom-form))
1753 (indent (widget-get widget :indent))
1754 (edit (widget-create-child-and-convert
1755 widget
1756 (cond ((and (eq form 'selected)
1757 (widget-apply custom-face-selected :match spec))
1758 (when indent (insert-char ?\ indent))
1759 'custom-face-selected)
1760 ((and (not (eq form 'lisp))
1761 (widget-apply custom-face-all :match spec))
1762 'custom-face-all)
1763 (t
1764 (when indent (insert-char ?\ indent))
1765 'sexp))
1766 :value spec)))
1767 (custom-face-state-set widget)
1768 (widget-put widget :children (list edit)))
1769 (message "Creating face editor...done")))
1770
1771 (defvar custom-face-menu
1772 '(("Hide" custom-toggle-hide
1773 (lambda (widget)
1774 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
1775 ("Edit Selected" custom-face-edit-selected
1776 (lambda (widget)
1777 (not (eq (widget-get widget :custom-form) 'selected))))
1778 ("Edit All" custom-face-edit-all
1779 (lambda (widget)
1780 (not (eq (widget-get widget :custom-form) 'all))))
1781 ("Edit Lisp" custom-face-edit-lisp
1782 (lambda (widget)
1783 (not (eq (widget-get widget :custom-form) 'lisp))))
1784 ("Set" custom-face-set)
1785 ("Save" custom-face-save)
1786 ("Reset to Saved" custom-face-reset-saved
1787 (lambda (widget)
1788 (get (widget-value widget) 'saved-face)))
1789 ("Reset to Factory Setting" custom-face-reset-factory
1790 (lambda (widget)
1791 (get (widget-value widget) 'factory-face))))
1792 "Alist of actions for the `custom-face' widget.
1793 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1794 the menu entry, ACTION is the function to call on the widget when the
1795 menu is selected, and FILTER is a predicate which takes a `custom-face'
1796 widget as an argument, and returns non-nil if ACTION is valid on that
1797 widget. If FILTER is nil, ACTION is always valid.")
1798
1799 (defun custom-face-edit-selected (widget)
1800 "Edit selected attributes of the value of WIDGET."
1801 (widget-put widget :custom-state 'unknown)
1802 (widget-put widget :custom-form 'selected)
1803 (custom-redraw widget))
1804
1805 (defun custom-face-edit-all (widget)
1806 "Edit all attributes of the value of WIDGET."
1807 (widget-put widget :custom-state 'unknown)
1808 (widget-put widget :custom-form 'all)
1809 (custom-redraw widget))
1810
1811 (defun custom-face-edit-lisp (widget)
1812 "Edit the lisp representation of the value of WIDGET."
1813 (widget-put widget :custom-state 'unknown)
1814 (widget-put widget :custom-form 'lisp)
1815 (custom-redraw widget))
1816
1817 (defun custom-face-state-set (widget)
1818 "Set the state of WIDGET."
1819 (let ((symbol (widget-value widget)))
1820 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
1821 'set)
1822 ((get symbol 'saved-face)
1823 'saved)
1824 ((get symbol 'factory-face)
1825 'factory)
1826 (t
1827 'rogue)))))
1828
1829 (defun custom-face-action (widget &optional event)
1830 "Show the menu for `custom-face' WIDGET.
1831 Optional EVENT is the location for the menu."
1832 (if (eq (widget-get widget :custom-state) 'hidden)
1833 (custom-toggle-hide widget)
1834 (let* ((completion-ignore-case t)
1835 (symbol (widget-get widget :value))
1836 (answer (widget-choose (custom-unlispify-tag-name symbol)
1837 (custom-menu-filter custom-face-menu
1838 widget)
1839 event)))
1840 (if answer
1841 (funcall answer widget)))))
1842
1843 (defun custom-face-set (widget)
1844 "Make the face attributes in WIDGET take effect."
1845 (let* ((symbol (widget-value widget))
1846 (child (car (widget-get widget :children)))
1847 (value (widget-value child)))
1848 (put symbol 'customized-face value)
1849 (custom-face-display-set symbol value)
1850 (custom-face-state-set widget)
1851 (custom-redraw-magic widget)))
1852
1853 (defun custom-face-save (widget)
1854 "Make the face attributes in WIDGET default."
1855 (let* ((symbol (widget-value widget))
1856 (child (car (widget-get widget :children)))
1857 (value (widget-value child)))
1858 (custom-face-display-set symbol value)
1859 (put symbol 'saved-face value)
1860 (put symbol 'customized-face nil)
1861 (custom-face-state-set widget)
1862 (custom-redraw-magic widget)))
1863
1864 (defun custom-face-reset-saved (widget)
1865 "Restore WIDGET to the face's default attributes."
1866 (let* ((symbol (widget-value widget))
1867 (child (car (widget-get widget :children)))
1868 (value (get symbol 'saved-face)))
1869 (unless value
1870 (error "No saved value for this face"))
1871 (put symbol 'customized-face nil)
1872 (custom-face-display-set symbol value)
1873 (widget-value-set child value)
1874 (custom-face-state-set widget)
1875 (custom-redraw-magic widget)))
1876
1877 (defun custom-face-reset-factory (widget)
1878 "Restore WIDGET to the face's factory settings."
1879 (let* ((symbol (widget-value widget))
1880 (child (car (widget-get widget :children)))
1881 (value (get symbol 'factory-face)))
1882 (unless value
1883 (error "No factory default for this face"))
1884 (put symbol 'customized-face nil)
1885 (when (get symbol 'saved-face)
1886 (put symbol 'saved-face nil)
1887 (custom-save-all))
1888 (custom-face-display-set symbol value)
1889 (widget-value-set child value)
1890 (custom-face-state-set widget)
1891 (custom-redraw-magic widget)))
1892
1893 ;;; The `face' Widget.
1894
1895 (define-widget 'face 'default
1896 "Select and customize a face."
1897 :convert-widget 'widget-item-convert-widget
1898 :format "%[%t%]: %v"
1899 :tag "Face"
1900 :value 'default
1901 :value-create 'widget-face-value-create
1902 :value-delete 'widget-face-value-delete
1903 :value-get 'widget-item-value-get
1904 :validate 'widget-editable-list-validate
1905 :action 'widget-face-action
1906 :match '(lambda (widget value) (symbolp value)))
1907
1908 (defun widget-face-value-create (widget)
1909 ;; Create a `custom-face' child.
1910 (let* ((symbol (widget-value widget))
1911 (child (widget-create-child-and-convert
1912 widget 'custom-face
1913 :format "%t %s%m%h%v"
1914 :custom-level nil
1915 :value symbol)))
1916 (custom-magic-reset child)
1917 (setq custom-options (cons child custom-options))
1918 (widget-put widget :children (list child))))
1919
1920 (defun widget-face-value-delete (widget)
1921 ;; Remove the child from the options.
1922 (let ((child (car (widget-get widget :children))))
1923 (setq custom-options (delq child custom-options))
1924 (widget-children-value-delete widget)))
1925
1926 (defvar face-history nil
1927 "History of entered face names.")
1928
1929 (defun widget-face-action (widget &optional event)
1930 "Prompt for a face."
1931 (let ((answer (completing-read "Face: "
1932 (mapcar (lambda (face)
1933 (list (symbol-name face)))
1934 (face-list))
1935 nil nil nil
1936 'face-history)))
1937 (unless (zerop (length answer))
1938 (widget-value-set widget (intern answer))
1939 (widget-apply widget :notify widget event)
1940 (widget-setup))))
1941
1942 ;;; The `hook' Widget.
1943
1944 (define-widget 'hook 'list
1945 "A emacs lisp hook"
1946 :convert-widget 'custom-hook-convert-widget
1947 :tag "Hook")
1948
1949 (defun custom-hook-convert-widget (widget)
1950 ;; Handle `:custom-options'.
1951 (let* ((options (widget-get widget :options))
1952 (other `(editable-list :inline t
1953 :entry-format "%i %d%v"
1954 (function :format " %v")))
1955 (args (if options
1956 (list `(checklist :inline t
1957 ,@(mapcar (lambda (entry)
1958 `(function-item ,entry))
1959 options))
1960 other)
1961 (list other))))
1962 (widget-put widget :args args)
1963 widget))
1964
1965 ;;; The `custom-group' Widget.
1966
1967 (defcustom custom-group-tag-faces '(custom-group-tag-face-1)
1968 ;; In XEmacs, this ought to play games with font size.
1969 "Face used for group tags.
1970 The first member is used for level 1 groups, the second for level 2,
1971 and so forth. The remaining group tags are shown with
1972 `custom-group-tag-face'."
1973 :type '(repeat face)
1974 :group 'custom-faces)
1975
1976 (defface custom-group-tag-face-1 '((((class color)
1977 (background dark))
1978 (:foreground "pink" :underline t))
1979 (((class color)
1980 (background light))
1981 (:foreground "red" :underline t))
1982 (t (:underline t)))
1983 "Face used for group tags.")
1984
1985 (defface custom-group-tag-face '((((class color)
1986 (background dark))
1987 (:foreground "light blue" :underline t))
1988 (((class color)
1989 (background light))
1990 (:foreground "blue" :underline t))
1991 (t (:underline t)))
1992 "Face used for low level group tags."
1993 :group 'custom-faces)
1994
1995 (define-widget 'custom-group 'custom
1996 "Customize group."
1997 :format "%l%{%t%}:%L\n%m%h%a%v"
1998 :sample-face-get 'custom-group-sample-face-get
1999 :documentation-property 'group-documentation
2000 :help-echo "Set or reset all members of this group."
2001 :value-create 'custom-group-value-create
2002 :action 'custom-group-action
2003 :custom-set 'custom-group-set
2004 :custom-save 'custom-group-save
2005 :custom-reset-current 'custom-group-reset-current
2006 :custom-reset-saved 'custom-group-reset-saved
2007 :custom-reset-factory 'custom-group-reset-factory
2008 :custom-menu 'custom-group-menu-create)
2009
2010 (defun custom-group-sample-face-get (widget)
2011 ;; Use :sample-face.
2012 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2013 'custom-group-tag-face))
2014
2015 (defun custom-group-value-create (widget)
2016 (let ((state (widget-get widget :custom-state)))
2017 (unless (eq state 'hidden)
2018 (message "Creating group...")
2019 (custom-load-widget widget)
2020 (let* ((level (widget-get widget :custom-level))
2021 (symbol (widget-value widget))
2022 (members (get symbol 'custom-group))
2023 (prefixes (widget-get widget :custom-prefixes))
2024 (custom-prefix-list (custom-prefix-add symbol prefixes))
2025 (length (length members))
2026 (count 0)
2027 (children (mapcar (lambda (entry)
2028 (widget-insert "\n")
2029 (message "Creating group members... %2d%%"
2030 (/ (* 100.0 count) length))
2031 (setq count (1+ count))
2032 (prog1
2033 (widget-create-child-and-convert
2034 widget (nth 1 entry)
2035 :group widget
2036 :tag (custom-unlispify-tag-name
2037 (nth 0 entry))
2038 :custom-prefixes custom-prefix-list
2039 :custom-level (1+ level)
2040 :value (nth 0 entry))
2041 (unless (eq (preceding-char) ?\n)
2042 (widget-insert "\n"))))
2043 members)))
2044 (message "Creating group magic...")
2045 (mapcar 'custom-magic-reset children)
2046 (message "Creating group state...")
2047 (widget-put widget :children children)
2048 (custom-group-state-update widget)
2049 (message "Creating group... done")))))
2050
2051 (defvar custom-group-menu
2052 '(("Hide" custom-toggle-hide
2053 (lambda (widget)
2054 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
2055 ("Set" custom-group-set
2056 (lambda (widget)
2057 (eq (widget-get widget :custom-state) 'modified)))
2058 ("Save" custom-group-save
2059 (lambda (widget)
2060 (memq (widget-get widget :custom-state) '(modified set))))
2061 ("Reset to Current" custom-group-reset-current
2062 (lambda (widget)
2063 (and (default-boundp (widget-value widget))
2064 (memq (widget-get widget :custom-state) '(modified)))))
2065 ("Reset to Saved" custom-group-reset-saved
2066 (lambda (widget)
2067 (and (get (widget-value widget) 'saved-value)
2068 (memq (widget-get widget :custom-state) '(modified set)))))
2069 ("Reset to Factory" custom-group-reset-factory
2070 (lambda (widget)
2071 (and (get (widget-value widget) 'factory-value)
2072 (memq (widget-get widget :custom-state) '(modified set saved))))))
2073 "Alist of actions for the `custom-group' widget.
2074 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2075 the menu entry, ACTION is the function to call on the widget when the
2076 menu is selected, and FILTER is a predicate which takes a `custom-group'
2077 widget as an argument, and returns non-nil if ACTION is valid on that
2078 widget. If FILTER is nil, ACTION is always valid.")
2079
2080 (defun custom-group-action (widget &optional event)
2081 "Show the menu for `custom-group' WIDGET.
2082 Optional EVENT is the location for the menu."
2083 (if (eq (widget-get widget :custom-state) 'hidden)
2084 (custom-toggle-hide widget)
2085 (let* ((completion-ignore-case t)
2086 (answer (widget-choose (custom-unlispify-tag-name
2087 (widget-get widget :value))
2088 (custom-menu-filter custom-group-menu
2089 widget)
2090 event)))
2091 (if answer
2092 (funcall answer widget)))))
2093
2094 (defun custom-group-set (widget)
2095 "Set changes in all modified group members."
2096 (let ((children (widget-get widget :children)))
2097 (mapcar (lambda (child)
2098 (when (eq (widget-get child :custom-state) 'modified)
2099 (widget-apply child :custom-set)))
2100 children )))
2101
2102 (defun custom-group-save (widget)
2103 "Save all modified group members."
2104 (let ((children (widget-get widget :children)))
2105 (mapcar (lambda (child)
2106 (when (memq (widget-get child :custom-state) '(modified set))
2107 (widget-apply child :custom-save)))
2108 children )))
2109
2110 (defun custom-group-reset-current (widget)
2111 "Reset all modified group members."
2112 (let ((children (widget-get widget :children)))
2113 (mapcar (lambda (child)
2114 (when (eq (widget-get child :custom-state) 'modified)
2115 (widget-apply child :custom-reset-current)))
2116 children )))
2117
2118 (defun custom-group-reset-saved (widget)
2119 "Reset all modified or set group members."
2120 (let ((children (widget-get widget :children)))
2121 (mapcar (lambda (child)
2122 (when (memq (widget-get child :custom-state) '(modified set))
2123 (widget-apply child :custom-reset-saved)))
2124 children )))
2125
2126 (defun custom-group-reset-factory (widget)
2127 "Reset all modified, set, or saved group members."
2128 (let ((children (widget-get widget :children)))
2129 (mapcar (lambda (child)
2130 (when (memq (widget-get child :custom-state)
2131 '(modified set saved))
2132 (widget-apply child :custom-reset-factory)))
2133 children )))
2134
2135 (defun custom-group-state-update (widget)
2136 "Update magic."
2137 (unless (eq (widget-get widget :custom-state) 'hidden)
2138 (let* ((children (widget-get widget :children))
2139 (states (mapcar (lambda (child)
2140 (widget-get child :custom-state))
2141 children))
2142 (magics custom-magic-alist)
2143 (found 'factory))
2144 (while magics
2145 (let ((magic (car (car magics))))
2146 (if (and (not (eq magic 'hidden))
2147 (memq magic states))
2148 (setq found magic
2149 magics nil)
2150 (setq magics (cdr magics)))))
2151 (widget-put widget :custom-state found)))
2152 (custom-magic-reset widget))
2153
2154 ;;; The `custom-save-all' Function.
2155
2156 (defcustom custom-file "~/.emacs"
2157 "File used for storing customization information.
2158 If you change this from the default \"~/.emacs\" you need to
2159 explicitly load that file for the settings to take effect."
2160 :type 'file
2161 :group 'customize)
2162
2163 (defun custom-save-delete (symbol)
2164 "Delete the call to SYMBOL form `custom-file'.
2165 Leave point at the location of the call, or after the last expression."
2166 (set-buffer (find-file-noselect custom-file))
2167 (goto-char (point-min))
2168 (catch 'found
2169 (while t
2170 (let ((sexp (condition-case nil
2171 (read (current-buffer))
2172 (end-of-file (throw 'found nil)))))
2173 (when (and (listp sexp)
2174 (eq (car sexp) symbol))
2175 (delete-region (save-excursion
2176 (backward-sexp)
2177 (point))
2178 (point))
2179 (throw 'found nil))))))
2180
2181 (defun custom-save-variables ()
2182 "Save all customized variables in `custom-file'."
2183 (save-excursion
2184 (custom-save-delete 'custom-set-variables)
2185 (let ((standard-output (current-buffer)))
2186 (unless (bolp)
2187 (princ "\n"))
2188 (princ "(custom-set-variables")
2189 (mapatoms (lambda (symbol)
2190 (let ((value (get symbol 'saved-value))
2191 (requests (get symbol 'custom-requests))
2192 (now (not (or (get symbol 'factory-value)
2193 (and (not (boundp symbol))
2194 (not (get symbol 'force-value)))))))
2195 (when value
2196 (princ "\n '(")
2197 (princ symbol)
2198 (princ " ")
2199 (prin1 (car value))
2200 (cond (requests
2201 (if now
2202 (princ " t ")
2203 (princ " nil "))
2204 (prin1 requests)
2205 (princ ")"))
2206 (now
2207 (princ " t)"))
2208 (t
2209 (princ ")")))))))
2210 (princ ")")
2211 (unless (looking-at "\n")
2212 (princ "\n")))))
2213
2214 (defun custom-save-faces ()
2215 "Save all customized faces in `custom-file'."
2216 (save-excursion
2217 (custom-save-delete 'custom-set-faces)
2218 (let ((standard-output (current-buffer)))
2219 (unless (bolp)
2220 (princ "\n"))
2221 (princ "(custom-set-faces")
2222 (let ((value (get 'default 'saved-face)))
2223 ;; The default face must be first, since it affects the others.
2224 (when value
2225 (princ "\n '(default ")
2226 (prin1 value)
2227 (if (or (get 'default 'factory-face)
2228 (and (not (custom-facep 'default))
2229 (not (get 'default 'force-face))))
2230 (princ ")")
2231 (princ " t)"))))
2232 (mapatoms (lambda (symbol)
2233 (let ((value (get symbol 'saved-face)))
2234 (when (and (not (eq symbol 'default))
2235 ;; Don't print default face here.
2236 value)
2237 (princ "\n '(")
2238 (princ symbol)
2239 (princ " ")
2240 (prin1 value)
2241 (if (or (get symbol 'factory-face)
2242 (and (not (custom-facep symbol))
2243 (not (get symbol 'force-face))))
2244 (princ ")")
2245 (princ " t)"))))))
2246 (princ ")")
2247 (unless (looking-at "\n")
2248 (princ "\n")))))
2249
2250 ;;;###autoload
2251 (defun custom-save-customized ()
2252 "Save all user options which have been set in this session."
2253 (interactive)
2254 (mapatoms (lambda (symbol)
2255 (let ((face (get symbol 'customized-face))
2256 (value (get symbol 'customized-value)))
2257 (when face
2258 (put symbol 'saved-face face)
2259 (put symbol 'customized-face nil))
2260 (when value
2261 (put symbol 'saved-value value)
2262 (put symbol 'customized-value nil)))))
2263 ;; We really should update all custom buffers here.
2264 (custom-save-all))
2265
2266 ;;;###autoload
2267 (defun custom-save-all ()
2268 "Save all customizations in `custom-file'."
2269 (custom-save-variables)
2270 (custom-save-faces)
2271 (save-excursion
2272 (set-buffer (find-file-noselect custom-file))
2273 (save-buffer)))
2274
2275 ;;; The Customize Menu.
2276
2277 ;;; Menu support
2278
2279 (unless (string-match "XEmacs" emacs-version)
2280 (defconst custom-help-menu '("Customize"
2281 ["Update menu..." custom-menu-update t]
2282 ["Group..." customize t]
2283 ["Variable..." customize-variable t]
2284 ["Face..." customize-face t]
2285 ["Saved..." customize-customized t]
2286 ["Apropos..." customize-apropos t])
2287 ;; This menu should be identical to the one defined in `menu-bar.el'.
2288 "Customize menu")
2289
2290 (defun custom-menu-reset ()
2291 "Reset customize menu."
2292 (remove-hook 'custom-define-hook 'custom-menu-reset)
2293 (define-key global-map [menu-bar help-menu customize-menu]
2294 (cons (car custom-help-menu)
2295 (easy-menu-create-keymaps (car custom-help-menu)
2296 (cdr custom-help-menu)))))
2297
2298 (defun custom-menu-update (event)
2299 "Update customize menu."
2300 (interactive "e")
2301 (add-hook 'custom-define-hook 'custom-menu-reset)
2302 (let* ((emacs (widget-apply '(custom-group) :custom-menu 'emacs))
2303 (menu `(,(car custom-help-menu)
2304 ,emacs
2305 ,@(cdr (cdr custom-help-menu)))))
2306 (let ((map (easy-menu-create-keymaps (car menu) (cdr menu))))
2307 (define-key global-map [menu-bar help-menu customize-menu]
2308 (cons (car menu) map)))))
2309
2310 (defcustom custom-menu-nesting 2
2311 "Maximum nesting in custom menus."
2312 :type 'integer
2313 :group 'customize))
2314
2315 (defun custom-face-menu-create (widget symbol)
2316 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
2317 (vector (custom-unlispify-menu-entry symbol)
2318 `(custom-buffer-create '((,symbol custom-face)))
2319 t))
2320
2321 (defun custom-variable-menu-create (widget symbol)
2322 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
2323 (let ((type (get symbol 'custom-type)))
2324 (unless (listp type)
2325 (setq type (list type)))
2326 (if (and type (widget-get type :custom-menu))
2327 (widget-apply type :custom-menu symbol)
2328 (vector (custom-unlispify-menu-entry symbol)
2329 `(custom-buffer-create '((,symbol custom-variable)))
2330 t))))
2331
2332 ;; Add checkboxes to boolean variable entries.
2333 (widget-put (get 'boolean 'widget-type)
2334 :custom-menu (lambda (widget symbol)
2335 (vector (custom-unlispify-menu-entry symbol)
2336 `(custom-buffer-create
2337 '((,symbol custom-variable)))
2338 ':style 'toggle
2339 ':selected symbol)))
2340
2341 (if (string-match "XEmacs" emacs-version)
2342 ;; XEmacs can create menus dynamically.
2343 (defun custom-group-menu-create (widget symbol)
2344 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2345 `( ,(custom-unlispify-menu-entry symbol t)
2346 :filter (lambda (&rest junk)
2347 (cdr (custom-menu-create ',symbol)))))
2348 ;; But emacs can't.
2349 (defun custom-group-menu-create (widget symbol)
2350 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2351 ;; Limit the nesting.
2352 (let ((custom-menu-nesting (1- custom-menu-nesting)))
2353 (custom-menu-create symbol))))
2354
2355 ;;;###autoload
2356 (defun custom-menu-create (symbol)
2357 "Create menu for customization group SYMBOL.
2358 The menu is in a format applicable to `easy-menu-define'."
2359 (let* ((item (vector (custom-unlispify-menu-entry symbol)
2360 `(custom-buffer-create '((,symbol custom-group)))
2361 t)))
2362 (if (and (or (not (boundp 'custom-menu-nesting))
2363 (>= custom-menu-nesting 0))
2364 (< (length (get symbol 'custom-group)) widget-menu-max-size))
2365 (let ((custom-prefix-list (custom-prefix-add symbol
2366 custom-prefix-list)))
2367 (custom-load-symbol symbol)
2368 `(,(custom-unlispify-menu-entry symbol t)
2369 ,item
2370 "--"
2371 ,@(mapcar (lambda (entry)
2372 (widget-apply (if (listp (nth 1 entry))
2373 (nth 1 entry)
2374 (list (nth 1 entry)))
2375 :custom-menu (nth 0 entry)))
2376 (get symbol 'custom-group))))
2377 item)))
2378
2379 ;;;###autoload
2380 (defun customize-menu-create (symbol &optional name)
2381 "Return a customize menu for customization group SYMBOL.
2382 If optional NAME is given, use that as the name of the menu.
2383 Otherwise the menu will be named `Customize'.
2384 The format is suitable for use with `easy-menu-define'."
2385 (unless name
2386 (setq name "Customize"))
2387 (if (string-match "XEmacs" emacs-version)
2388 ;; We can delay it under XEmacs.
2389 `(,name
2390 :filter (lambda (&rest junk)
2391 (cdr (custom-menu-create ',symbol))))
2392 ;; But we must create it now under Emacs.
2393 (cons name (cdr (custom-menu-create symbol)))))
2394
2395 ;;; The Custom Mode.
2396
2397 (defvar custom-mode-map nil
2398 "Keymap for `custom-mode'.")
2399
2400 (unless custom-mode-map
2401 (setq custom-mode-map (make-sparse-keymap))
2402 (set-keymap-parent custom-mode-map widget-keymap)
2403 (define-key custom-mode-map "q" 'bury-buffer))
2404
2405 (easy-menu-define custom-mode-customize-menu
2406 custom-mode-map
2407 "Menu used to customize customization buffers."
2408 (customize-menu-create 'customize))
2409
2410 (easy-menu-define custom-mode-menu
2411 custom-mode-map
2412 "Menu used in customization buffers."
2413 `("Custom"
2414 ["Set" custom-set t]
2415 ["Save" custom-save t]
2416 ["Reset to Current" custom-reset-current t]
2417 ["Reset to Saved" custom-reset-saved t]
2418 ["Reset to Factory Settings" custom-reset-factory t]
2419 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
2420
2421 (defcustom custom-mode-hook nil
2422 "Hook called when entering custom-mode."
2423 :type 'hook
2424 :group 'customize)
2425
2426 (defun custom-mode ()
2427 "Major mode for editing customization buffers.
2428
2429 The following commands are available:
2430
2431 Move to next button or editable field. \\[widget-forward]
2432 Move to previous button or editable field. \\[widget-backward]
2433 Activate button under the mouse pointer. \\[widget-button-click]
2434 Activate button under point. \\[widget-button-press]
2435 Set all modifications. \\[custom-set]
2436 Make all modifications default. \\[custom-save]
2437 Reset all modified options. \\[custom-reset-current]
2438 Reset all modified or set options. \\[custom-reset-saved]
2439 Reset all options. \\[custom-reset-factory]
2440
2441 Entry to this mode calls the value of `custom-mode-hook'
2442 if that value is non-nil."
2443 (kill-all-local-variables)
2444 (setq major-mode 'custom-mode
2445 mode-name "Custom")
2446 (use-local-map custom-mode-map)
2447 (easy-menu-add custom-mode-customize-menu)
2448 (easy-menu-add custom-mode-menu)
2449 (make-local-variable 'custom-options)
2450 (run-hooks 'custom-mode-hook))
2451
2452 ;;; The End.
2453
2454 (provide 'cus-edit)
2455
2456 ;; cus-edit.el ends here