1 ;;; custom.el -- Tools for declaring and initializing options.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: help, faces, dumped
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
11 ;; This file is part of XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This file is dumped with XEmacs.
32 ;; This file only contain the code needed to declare and initialize
33 ;; user options. The code to customize options is autoloaded from
36 ;; The code implementing face declarations is in `cus-face.el'
41 (load "cl-macs" nil t))
43 (autoload 'custom-declare-face "cus-face")
44 (autoload 'defun* "cl-macs")
48 (defvar custom-define-hook nil
49 ;; Customize information for this option is in `cus-edit.el'.
50 "Hook called after defining each customize option.")
52 ;;; The `defcustom' Macro.
54 (defun custom-initialize-default (symbol value)
55 "Initialize SYMBOL with VALUE.
56 This will do nothing if symbol already has a default binding.
57 Otherwise, if symbol has a `saved-value' property, it will evaluate
58 the car of that and used as the default binding for symbol.
59 Otherwise, VALUE will be evaluated and used as the default binding for
61 (unless (default-boundp symbol)
62 ;; Use the saved value if it exists, otherwise the standard setting.
63 (set-default symbol (if (get symbol 'saved-value)
64 (eval (car (get symbol 'saved-value)))
67 (defun custom-initialize-set (symbol value)
68 "Initialize SYMBOL with VALUE.
69 Like `custom-initialize-default', but use the function specified by
70 `:set' to initialize SYMBOL."
71 (unless (default-boundp symbol)
72 (funcall (or (get symbol 'custom-set) 'set-default)
74 (if (get symbol 'saved-value)
75 (eval (car (get symbol 'saved-value)))
78 (defun custom-initialize-reset (symbol value)
79 "Initialize SYMBOL with VALUE.
80 Like `custom-initialize-set', but use the function specified by
81 `:get' to reinitialize SYMBOL if it is already bound."
82 (funcall (or (get symbol 'custom-set) 'set-default)
84 (cond ((default-boundp symbol)
85 (funcall (or (get symbol 'custom-get) 'default-value)
87 ((get symbol 'saved-value)
88 (eval (car (get symbol 'saved-value))))
92 (defun custom-initialize-changed (symbol value)
93 "Initialize SYMBOL with VALUE.
94 Like `custom-initialize-reset', but only use the `:set' function if the
95 not using the standard setting. Otherwise, use the `set-default'."
96 (cond ((default-boundp symbol)
97 (funcall (or (get symbol 'custom-set) 'set-default)
99 (funcall (or (get symbol 'custom-get) 'default-value)
101 ((get symbol 'saved-value)
102 (funcall (or (get symbol 'custom-set) 'set-default)
104 (eval (car (get symbol 'saved-value)))))
106 (set-default symbol (eval value)))))
108 (defun custom-declare-variable (symbol value doc &rest args)
109 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
110 ;; Remember the standard setting.
111 (put symbol 'standard-value (list value))
112 ;; Maybe this option was rogue in an earlier version. It no longer is.
113 (when (eq (get symbol 'force-value) 'rogue)
115 (put symbol 'force-value nil))
117 (put symbol 'variable-documentation doc))
118 (let ((initialize 'custom-initialize-reset)
121 (let ((arg (car args)))
122 (setq args (cdr args))
123 (check-argument-type 'keywordp arg)
127 (signal 'error (list "Keyword is missing an argument" keyword)))
128 (setq args (cdr args))
129 (cond ((eq keyword :initialize)
130 (setq initialize value))
132 (put symbol 'custom-set value))
134 (put symbol 'custom-get value))
135 ((eq keyword :require)
136 (setq requests (cons value requests)))
138 (put symbol 'custom-type value))
139 ((eq keyword :options)
140 (if (get symbol 'custom-options)
141 ;; Slow safe code to avoid duplicates.
142 (mapc (lambda (option)
143 (custom-add-option symbol option))
145 ;; Fast code for the common case.
146 (put symbol 'custom-options (copy-sequence value))))
148 (custom-handle-keyword symbol keyword value
149 'custom-variable))))))
150 (put symbol 'custom-requests requests)
151 ;; Do the actual initialization.
152 (funcall initialize symbol value))
153 ;; #### This is a rough equivalent of LOADHIST_ATTACH. However,
154 ;; LOADHIST_ATTACH also checks for `initialized'.
155 (push symbol current-load-list)
156 (run-hooks 'custom-define-hook)
159 (defmacro defcustom (symbol value doc &rest args)
160 "Declare SYMBOL as a customizable variable that defaults to VALUE.
161 DOC is the variable documentation.
163 Neither SYMBOL nor VALUE needs to be quoted.
164 If SYMBOL is not already bound, initialize it to VALUE.
165 The remaining arguments should have the form
169 The following KEYWORD's are defined:
171 :type VALUE should be a widget type for editing the symbols value.
172 The default is `sexp'.
173 :options VALUE should be a list of valid members of the widget type.
174 :group VALUE should be a customization group.
175 Add SYMBOL to that group.
176 :initialize VALUE should be a function used to initialize the
177 variable. It takes two arguments, the symbol and value
178 given in the `defcustom' call. The default is
179 `custom-initialize-set'
180 :set VALUE should be a function to set the value of the symbol.
181 It takes two arguments, the symbol to set and the value to
182 give it. The default is `custom-set-default'.
183 :get VALUE should be a function to extract the value of symbol.
184 The function takes one argument, a symbol, and should return
185 the current value for that symbol. The default is
187 :require VALUE should be a feature symbol. Each feature will be
188 required after initialization, of the user have saved this
190 :version VALUE should be a string specifying that the variable was
191 first introduced, or its default value was changed, in Emacs
193 :set-after VARIABLE specifies that SYMBOL should be set after VARIABLE when
194 both have been customized.
196 Read the section about customization in the Emacs Lisp manual for more
198 `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
200 ;;; The `defface' Macro.
202 (defmacro defface (face spec doc &rest args)
203 "Declare FACE as a customizable face that defaults to SPEC.
204 FACE does not need to be quoted.
206 Third argument DOC is the face documentation.
208 If FACE has been set with `custom-set-face', set the face attributes
209 as specified by that function, otherwise set the face attributes
212 The remaining arguments should have the form
216 The following KEYWORDs are defined:
218 :group VALUE should be a customization group.
219 Add FACE to that group.
221 SPEC should be an alist of the form ((DISPLAY ATTS)...).
223 ATTS is a list of face attributes and their values. The possible
224 attributes are defined in the variable `custom-face-attributes'.
226 The ATTS of the first entry in SPEC where the DISPLAY matches the
227 frame should take effect in that frame. DISPLAY can either be the
228 symbol t, which will match all frames, or an alist of the form
231 For the DISPLAY to match a FRAME, the REQ property of the frame must
232 match one of the ITEM. The following REQ are defined:
234 `type' (the value of `window-system')
235 Should be one of `x', `mswindows', or `tty'.
237 `class' (the frame's color support)
238 Should be one of `color', `grayscale', or `mono'.
240 `background' (what color is used for the background text)
241 Should be one of `light' or `dark'.
243 Read the section about customization in the Emacs Lisp manual for more
245 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
247 ;;; The `defgroup' Macro.
249 (defun custom-declare-group (symbol members doc &rest args)
250 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
252 (apply 'custom-add-to-group symbol (car members))
254 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
256 (put symbol 'group-documentation doc))
258 (let ((arg (car args)))
259 (setq args (cdr args))
260 (check-argument-type 'keywordp arg)
264 (signal 'error (list "Keyword is missing an argument" keyword)))
265 (setq args (cdr args))
266 (cond ((eq keyword :prefix)
267 (put symbol 'custom-prefix value))
269 (custom-handle-keyword symbol keyword value
271 (run-hooks 'custom-define-hook)
274 (defmacro defgroup (symbol members doc &rest args)
275 "Declare SYMBOL as a customization group containing MEMBERS.
276 SYMBOL does not need to be quoted.
278 Third arg DOC is the group documentation.
280 MEMBERS should be an alist of the form ((NAME WIDGET)...) where NAME
281 is a symbol and WIDGET is a widget for editing that symbol. Useful
282 widgets are `custom-variable' for editing variables, `custom-face' for
283 edit faces, and `custom-group' for editing groups.
285 The remaining arguments should have the form
289 The following KEYWORD's are defined:
291 :group VALUE should be a customization group.
292 Add SYMBOL to that group.
294 Read the section about customization in the Emacs Lisp manual for more
296 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
298 (defvar custom-group-hash-table (make-hash-table :size 300 :test 'eq)
299 "Hash-table of non-empty groups.")
301 (defun custom-add-to-group (group option widget)
302 "To existing GROUP add a new OPTION of type WIDGET.
303 If there already is an entry for that option, overwrite it."
304 (let* ((members (get group 'custom-group))
305 (old (assq option members)))
307 (setcar (cdr old) widget)
308 (put group 'custom-group (nconc members (list (list option widget))))))
309 (puthash group t custom-group-hash-table))
313 (defun custom-handle-all-keywords (symbol args type)
314 "For customization option SYMBOL, handle keyword arguments ARGS.
315 Third argument TYPE is the custom option type."
317 (let ((arg (car args)))
318 (setq args (cdr args))
319 (check-argument-type 'keywordp arg)
323 (signal 'error (list "Keyword is missing an argument" keyword)))
324 (setq args (cdr args))
325 (custom-handle-keyword symbol keyword value type)))))
327 (defun custom-handle-keyword (symbol keyword value type)
328 "For customization option SYMBOL, handle KEYWORD with VALUE.
329 Fourth argument TYPE is the custom option type."
330 (cond ((eq keyword :group)
331 (custom-add-to-group value symbol type))
332 ((eq keyword :version)
333 (custom-add-version symbol value))
335 (custom-add-link symbol value))
337 (custom-add-load symbol value))
339 (put symbol 'custom-tag value))
340 ((eq keyword :set-after)
341 (custom-add-dependencies symbol value))
343 (signal 'error (list "Unknown keyword" keyword)))))
345 (defun custom-add-dependencies (symbol value)
346 "To the custom option SYMBOL, add dependencies specified by VALUE.
347 VALUE should be a list of symbols. For each symbol in that list,
348 this specifies that SYMBOL should be set after the specified symbol, if
349 both appear in constructs like `custom-set-variables'."
350 (unless (listp value)
351 (error "Invalid custom dependency `%s'" value))
352 (let* ((deps (get symbol 'custom-dependencies))
355 (let ((dep (car value)))
356 (unless (symbolp dep)
357 (error "Invalid custom dependency `%s'" dep))
358 (unless (memq dep new-deps)
359 (setq new-deps (cons dep new-deps)))
360 (setq value (cdr value))))
361 (unless (eq deps new-deps)
362 (put symbol 'custom-dependencies new-deps))))
364 (defun custom-add-option (symbol option)
365 "To the variable SYMBOL add OPTION.
367 If SYMBOL is a hook variable, OPTION should be a hook member.
368 For other types variables, the effect is undefined."
369 (let ((options (get symbol 'custom-options)))
370 (unless (member option options)
371 (put symbol 'custom-options (cons option options)))))
373 (defun custom-add-link (symbol widget)
374 "To the custom option SYMBOL add the link WIDGET."
375 (let ((links (get symbol 'custom-links)))
376 (unless (member widget links)
377 (put symbol 'custom-links (cons widget links)))))
379 (defun custom-add-version (symbol version)
380 "To the custom option SYMBOL add the version VERSION."
381 (put symbol 'custom-version version))
383 (defun custom-add-load (symbol load)
384 "To the custom option SYMBOL add the dependency LOAD.
385 LOAD should be either a library file name, or a feature name."
386 (puthash symbol t custom-group-hash-table)
387 (let ((loads (get symbol 'custom-loads)))
388 (unless (member load loads)
389 (put symbol 'custom-loads (cons load loads)))))
393 (defvar custom-known-themes '(user standard)
394 "Themes that have been defthemed.")
396 ;; #### add strings for group
397 ;; #### during bootstrap we cannot use cl-macs stuff
398 (defun* custom-define-theme (theme feature &optional doc
399 &key short-description immediate variable-reset-string
400 variable-set-string face-set-string face-reset-string
402 (push theme custom-known-themes)
403 (put theme 'theme-feature feature)
404 (put theme 'theme-documentation doc)
405 (if immediate (put theme 'theme-immediate immediate))
406 (if variable-reset-string
407 (put theme 'theme-variable-reset-string variable-reset-string ))
408 (if variable-set-string
409 (put theme 'theme-variable-set-string variable-set-string ))
410 (if face-reset-string
411 (put theme 'theme-face-reset-string face-reset-string ))
413 (put theme 'theme-face-set-string face-set-string ))
414 (if short-description
415 (put theme 'theme-short-description short-description )))
417 (defun custom-make-theme-feature (theme)
418 (intern (concat (symbol-name theme) "-theme")))
420 (defmacro deftheme (theme &rest body)
421 "(deftheme THEME &optional DOC &key KEYWORDS)
423 Define a theme labeled by SYMBOL THEME. The optional argument DOC is a
424 doc string describing the theme. It is optionally followed by the
425 following keyword arguments
427 :short-description DESC
428 DESC is a short (one line) description of the theme. If not given DOC
431 If FLAG is non-nil variables set in this theme are bound
432 immediately when loading the theme.
433 :variable-set-string VARIABLE_-SET-STRING
434 A string used by the UI to indicate that the value takes it
435 setting from this theme. It is passed to FORMAT with the
436 name of the theme a additional argument.
437 If not given, a generic description is used.
438 :variable-reset-string VARIABLE-RESET-STRING
439 As above but used in the case the variable has been forced to
440 the value in this theme.
441 :face-set-string FACE-SET-STRING
442 :face-reset-string FACE-RESET-STRING
443 As above but for faces."
444 (let ((feature (custom-make-theme-feature theme)))
445 `(custom-define-theme (quote ,theme) (quote ,feature) ,@body)))
447 (defsubst custom-theme-p (theme)
448 "Non-nil when THEME has been defined."
449 (memq theme custom-known-themes))
451 (defsubst custom-check-theme (theme)
452 "Check whether THEME is valid and signal an error if NOT."
453 (unless (custom-theme-p theme)
454 (error "Unknown theme `%s'" theme)))
457 ; #### do we need to deftheme 'user and/or 'standard here to make the
458 ; code in cus-edit cleaner?.
462 (defun custom-push-theme (prop symbol theme mode value)
463 (let ((old (get symbol prop)))
464 (if (eq (car-safe (car-safe old)) theme)
465 (setq old (cdr old)))
466 (put symbol prop (cons (list theme mode value) old))))
468 (defvar custom-local-buffer nil
469 "Non-nil, in a Customization buffer, means customize a specific buffer.
470 If this variable is non-nil, it should be a buffer,
471 and it means customize the local bindings of that buffer.
472 This variable is a permanent local, and it normally has a local binding
473 in every Customization buffer.")
474 (put 'custom-local-buffer 'permanent-local t)
476 (defun custom-set-variables (&rest args)
477 "Initialize variables according to user preferences.
478 The settings are registered as theme `user'.
479 Each argument should be a list of the form:
481 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
483 The unevaluated VALUE is stored as the saved value for SYMBOL.
484 If NOW is present and non-nil, VALUE is also evaluated and bound as
485 the default value for the SYMBOL.
486 REQUEST is a list of features we must 'require for SYMBOL.
487 COMMENT is a comment string about SYMBOL."
488 (apply 'custom-theme-set-variables 'user args))
490 (defun custom-theme-set-variables (theme &rest args)
491 "Initialize variables according to settings specified by args.
492 Records the settings as belonging to THEME.
494 See `custom-set-variables' for a description of the arguments ARGS."
495 (custom-check-theme theme)
499 (let* ((sym1 (car a1))
501 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
502 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
503 (cond ((and 1-then-2 2-then-1)
504 (error "Circular custom dependency between `%s' and `%s'"
508 ;; Put symbols with :require last. The macro
509 ;; define-minor-mode generates a defcustom
510 ;; with a :require and a :set, where the
511 ;; setter function calls the mode function.
512 ;; Putting symbols with :require last ensures
513 ;; that the mode function will see other
514 ;; customized values rather than default
517 (let ((immediate (get theme 'theme-immediate)))
519 (let ((entry (car args)))
521 (let* ((symbol (nth 0 entry))
522 (value (nth 1 entry))
524 (requests (nth 3 entry))
525 (comment (nth 4 entry))
526 (set (or (get symbol 'custom-set) 'custom-set-default)))
527 (put symbol 'saved-value (list value))
528 (custom-push-theme 'theme-value symbol theme 'set value)
529 (put symbol 'saved-variable-comment comment)
530 ;; Allow for errors in the case where the setter has
531 ;; changed between versions, say, but let the user know.
533 (cond ((or now immediate)
534 ;; Rogue variable, set it now.
535 (put symbol 'force-value (if now 'rogue 'immediate))
536 (funcall set symbol (eval value)))
537 ((default-boundp symbol)
538 ;; Something already set this, overwrite it.
539 (funcall set symbol (eval value))))
541 (message "Error setting %s: %s" symbol data)))
542 (and (or now (default-boundp symbol))
543 (put symbol 'variable-comment comment))
545 (put symbol 'custom-requests requests)
546 (mapc 'require requests))
547 (setq args (cdr args)))
548 ;; Old format, a plist of SYMBOL VALUE pairs.
549 (message "Warning: old format `custom-set-variables'")
552 (let ((symbol (nth 0 args))
553 (value (nth 1 args)))
554 (put symbol 'saved-value (list value))
555 (custom-push-theme 'theme-value symbol theme 'set value))
556 (setq args (cdr (cdr args))))))))
558 (defvar custom-loaded-themes nil
559 "Themes in the order they are loaded.")
561 (defun custom-theme-loaded-p (theme)
562 "Return non-nil when THEME has been loaded."
563 (memq theme custom-loaded-themes))
565 (defun provide-theme (theme)
566 "Indicate that this file provides THEME."
567 (custom-check-theme theme)
568 (provide (get theme 'theme-feature))
569 (push theme custom-loaded-themes))
571 (defun require-theme (theme &optional soft)
572 "Try to load a theme by requiring its feature."
573 ;; Note we do no check for validity of the theme here.
574 ;; This allows to pull in themes by a file-name convention
575 (require (get theme 'theme-feature (custom-make-theme-feature theme))))
577 (defun custom-do-theme-reset (theme)
578 ; #### untested! slow!
580 (mapatoms (lambda (symbol)
581 (setq spec-list (get symbol 'theme-value))
583 (setq spec-list (delete-if (lambda (elt)
584 (eq (car elt) theme))
586 (put symbol 'theme-value spec-list)
587 (custom-theme-reset-internal symbol 'user))
588 (setq spec-list (get symbol 'theme-face))
590 (setq spec-list (delete-if (lambda (elt)
591 (eq (car elt) theme))
593 (put symbol 'theme-face spec-list)
594 (custom-theme-reset-internal-face symbol 'user))))))
596 (defun custom-theme-load-themes (by-theme &rest body)
597 "Load the themes specified by BODY and record them as required by
598 theme BY-THEME. BODY is a sequence of
600 require the theme SYMBOL
601 - a list (reset THEME)
602 Undo all the settings made by THEME.
603 - a list (hidden THEME)
604 require the THEME but hide it from the user."
605 (custom-check-theme by-theme)
607 (cond ((and (consp theme) (eq (car theme) 'reset))
608 (custom-do-theme-reset (cadr theme)))
609 ((and (consp theme) (eq (car theme) 'hidden))
610 (require-theme (cadr theme))
611 (unless (custom-theme-loaded-p (cadr theme))
612 (put (cadr theme) 'theme-hidden t)))
614 (require-theme theme)
615 (remprop theme 'theme-hidden)))
616 (push theme (get by-theme 'theme-loads-themes))))
618 (defun custom-load-themes (&rest body)
619 "Load themes for the USER theme as specified by BODY.
621 BODY is as with custom-theme-load-themes."
622 (apply #'custom-theme-load-themes 'user body))
627 (defsubst copy-upto-last (elt list)
628 "Copy all the elements of the list upto the last occurrence of elt."
629 ;; Is it faster to do more work in C than to do less in elisp?
630 (nreverse (cdr (member elt (reverse list)))))
632 (defun custom-theme-value (theme theme-spec-list)
633 "Determine the value for THEME defined by THEME-SPEC-LIST.
634 Returns (list value) if found. Nil otherwise."
635 ;; Note we do _NOT_ signal an error if the theme is unknown
636 ;; it might have gone away without the user knowing.
637 (let ((theme-or-lower (memq theme (cons 'user custom-loaded-themes)))
639 (mapc #'(lambda (theme-spec)
640 (when (member (car theme-spec) theme-or-lower)
641 (setq value (cdr theme-spec))
642 ;; We need to continue because if theme =A and we found
643 ;; B then if the load order is B A C B
644 ;; we actually want the value in C.
645 (setq theme-or-lower (copy-upto-last (car theme-spec)
647 ;; We could should circuit if this is now nil.
651 (if (eq (car value) 'set)
653 ;; Yet another reset spec. car value = reset
654 (custom-theme-value (cadr value) theme-spec-list)))))
657 (defun custom-theme-variable-value (variable theme)
658 "Return (list value) value of VARIABLE in THEME if the THEME modifies the
659 VARIABLE. Nil otherwise."
660 (custom-theme-value theme (get variable 'theme-value)))
662 (defun custom-theme-reset-internal (symbol to-theme)
663 (let ((value (custom-theme-variable-value symbol to-theme))
665 (setq was-in-theme value)
666 (setq value (or value (get symbol 'standard-value)))
668 (put symbol 'saved-value was-in-theme)
669 (if (or (get 'force-value symbol) (default-boundp symbol))
670 (funcall (get symbol 'custom-set 'set-default) symbol
671 (eval (car value)))))
675 (defun custom-theme-reset-variables (theme &rest args)
676 "Reset the value of the variables to values previously defined.
677 Associate this setting with THEME.
679 ARGS is a list of lists of the form
683 This means reset variable to its value in to-theme."
684 (custom-check-theme theme)
685 (mapc #'(lambda (arg)
686 (apply #'custom-theme-reset-internal arg)
687 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
690 (defun custom-reset-variables (&rest args)
691 "Reset the value of the variables to values previously defined.
692 Associate this setting with the `user' theme.
694 The ARGS are as in `custom-theme-reset-variables'."
695 (apply #'custom-theme-reset-variables 'user args))
697 (defun custom-set-default (variable value)
698 "Default :set function for a customizable variable.
699 Normally, this sets the default value of VARIABLE to VALUE,
700 but if `custom-local-buffer' is non-nil,
701 this sets the local binding in that buffer instead."
702 (if custom-local-buffer
703 (with-current-buffer custom-local-buffer
704 (set variable value))
705 (set-default variable value)))
711 ;; custom.el ends here