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@srce.hr>
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'
42 (defvar custom-define-hook nil
43 ;; Customize information for this option is in `cus-edit.el'.
44 "Hook called after defining each customize option.")
46 ;;; The `defcustom' Macro.
48 (defun custom-initialize-default (symbol value)
49 "Initialize SYMBOL with VALUE.
50 This will do nothing if symbol already has a default binding.
51 Otherwise, if symbol has a `saved-value' property, it will evaluate
52 the car of that and used as the default binding for symbol.
53 Otherwise, VALUE will be evaluated and used as the default binding for
55 (unless (default-boundp symbol)
56 ;; Use the saved value if it exists, otherwise the standard setting.
57 (set-default symbol (if (get symbol 'saved-value)
58 (eval (car (get symbol 'saved-value)))
61 (defun custom-initialize-set (symbol value)
62 "Initialize SYMBOL with VALUE.
63 Like `custom-initialize-default', but use the function specified by
64 `:set' to initialize SYMBOL."
65 (unless (default-boundp symbol)
66 (funcall (or (get symbol 'custom-set) 'set-default)
68 (if (get symbol 'saved-value)
69 (eval (car (get symbol 'saved-value)))
72 (defun custom-initialize-reset (symbol value)
73 "Initialize SYMBOL with VALUE.
74 Like `custom-initialize-set', but use the function specified by
75 `:get' to reinitialize SYMBOL if it is already bound."
76 (funcall (or (get symbol 'custom-set) 'set-default)
78 (cond ((default-boundp symbol)
79 (funcall (or (get symbol 'custom-get) 'default-value)
81 ((get symbol 'saved-value)
82 (eval (car (get symbol 'saved-value))))
86 (defun custom-initialize-changed (symbol value)
87 "Initialize SYMBOL with VALUE.
88 Like `custom-initialize-reset', but only use the `:set' function if the
89 not using the standard setting. Otherwise, use the `set-default'."
90 (cond ((default-boundp symbol)
91 (funcall (or (get symbol 'custom-set) 'set-default)
93 (funcall (or (get symbol 'custom-get) 'default-value)
95 ((get symbol 'saved-value)
96 (funcall (or (get symbol 'custom-set) 'set-default)
98 (eval (car (get symbol 'saved-value)))))
100 (set-default symbol (eval value)))))
102 (defun custom-declare-variable (symbol value doc &rest args)
103 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
104 ;; Remember the standard setting.
105 (put symbol 'standard-value (list value))
106 ;; Maybe this option was rogue in an earlier version. It no longer is.
107 (when (get symbol 'force-value)
109 (put symbol 'force-value nil))
111 (put symbol 'variable-documentation doc))
112 (let ((initialize 'custom-initialize-reset)
115 (let ((arg (car args)))
116 (setq args (cdr args))
117 (check-argument-type 'keywordp arg)
121 (signal 'error (list "Keyword is missing an argument" keyword)))
122 (setq args (cdr args))
123 (cond ((eq keyword :initialize)
124 (setq initialize value))
126 (put symbol 'custom-set value))
128 (put symbol 'custom-get value))
129 ((eq keyword :require)
130 (setq requests (cons value requests)))
132 (put symbol 'custom-type value))
133 ((eq keyword :options)
134 (if (get symbol 'custom-options)
135 ;; Slow safe code to avoid duplicates.
136 (mapc (lambda (option)
137 (custom-add-option symbol option))
139 ;; Fast code for the common case.
140 (put symbol 'custom-options (copy-sequence value))))
142 (custom-handle-keyword symbol keyword value
143 'custom-variable))))))
144 (put symbol 'custom-requests requests)
145 ;; Do the actual initialization.
146 (funcall initialize symbol value))
147 ;; #### This is a rough equivalent of LOADHIST_ATTACH. However,
148 ;; LOADHIST_ATTACH also checks for `initialized'.
149 (push symbol current-load-list)
150 (run-hooks 'custom-define-hook)
153 (defmacro defcustom (symbol value doc &rest args)
154 "Declare SYMBOL as a customizable variable that defaults to VALUE.
155 DOC is the variable documentation.
157 Neither SYMBOL nor VALUE needs to be quoted.
158 If SYMBOL is not already bound, initialize it to VALUE.
159 The remaining arguments should have the form
163 The following KEYWORD's are defined:
165 :type VALUE should be a widget type for editing the symbols value.
166 The default is `sexp'.
167 :options VALUE should be a list of valid members of the widget type.
168 :group VALUE should be a customization group.
169 Add SYMBOL to that group.
170 :initialize VALUE should be a function used to initialize the
171 variable. It takes two arguments, the symbol and value
172 given in the `defcustom' call. The default is
173 `custom-initialize-set'
174 :set VALUE should be a function to set the value of the symbol.
175 It takes two arguments, the symbol to set and the value to
176 give it. The default is `set-default'.
177 :get VALUE should be a function to extract the value of symbol.
178 The function takes one argument, a symbol, and should return
179 the current value for that symbol. The default is
181 :require VALUE should be a feature symbol. Each feature will be
182 required after initialization, of the the user have saved this
185 Read the section about customization in the Emacs Lisp manual for more
187 `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
189 ;;; The `defface' Macro.
191 (defmacro defface (face spec doc &rest args)
192 "Declare FACE as a customizable face that defaults to SPEC.
193 FACE does not need to be quoted.
195 Third argument DOC is the face documentation.
197 If FACE has been set with `custom-set-face', set the face attributes
198 as specified by that function, otherwise set the face attributes
201 The remaining arguments should have the form
205 The following KEYWORDs are defined:
207 :group VALUE should be a customization group.
208 Add FACE to that group.
210 SPEC should be an alist of the form ((DISPLAY ATTS)...).
212 ATTS is a list of face attributes and their values. The possible
213 attributes are defined in the variable `custom-face-attributes'.
215 The ATTS of the first entry in SPEC where the DISPLAY matches the
216 frame should take effect in that frame. DISPLAY can either be the
217 symbol t, which will match all frames, or an alist of the form
220 For the DISPLAY to match a FRAME, the REQ property of the frame must
221 match one of the ITEM. The following REQ are defined:
223 `type' (the value of `window-system')
224 Should be one of `x' or `tty'.
226 `class' (the frame's color support)
227 Should be one of `color', `grayscale', or `mono'.
229 `background' (what color is used for the background text)
230 Should be one of `light' or `dark'.
232 Read the section about customization in the Emacs Lisp manual for more
234 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
236 ;;; The `defgroup' Macro.
238 (defun custom-declare-group (symbol members doc &rest args)
239 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
241 (apply 'custom-add-to-group symbol (car members))
243 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
245 (put symbol 'group-documentation doc))
247 (let ((arg (car args)))
248 (setq args (cdr args))
249 (check-argument-type 'keywordp arg)
253 (signal 'error (list "Keyword is missing an argument" keyword)))
254 (setq args (cdr args))
255 (cond ((eq keyword :prefix)
256 (put symbol 'custom-prefix value))
258 (custom-handle-keyword symbol keyword value
260 (run-hooks 'custom-define-hook)
263 (defmacro defgroup (symbol members doc &rest args)
264 "Declare SYMBOL as a customization group containing MEMBERS.
265 SYMBOL does not need to be quoted.
267 Third arg DOC is the group documentation.
269 MEMBERS should be an alist of the form ((NAME WIDGET)...) where NAME
270 is a symbol and WIDGET is a widget for editing that symbol. Useful
271 widgets are `custom-variable' for editing variables, `custom-face' for
272 edit faces, and `custom-group' for editing groups.
274 The remaining arguments should have the form
278 The following KEYWORD's are defined:
280 :group VALUE should be a customization group.
281 Add SYMBOL to that group.
283 Read the section about customization in the Emacs Lisp manual for more
285 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
287 (defvar custom-group-hash-table (make-hash-table :size 300 :test 'eq)
288 "Hash-table of non-empty groups.")
290 (defun custom-add-to-group (group option widget)
291 "To existing GROUP add a new OPTION of type WIDGET.
292 If there already is an entry for that option, overwrite it."
293 (let* ((members (get group 'custom-group))
294 (old (assq option members)))
296 (setcar (cdr old) widget)
297 (put group 'custom-group (nconc members (list (list option widget))))))
298 (puthash group t custom-group-hash-table))
302 (defun custom-handle-all-keywords (symbol args type)
303 "For customization option SYMBOL, handle keyword arguments ARGS.
304 Third argument TYPE is the custom option type."
306 (let ((arg (car args)))
307 (setq args (cdr args))
308 (check-argument-type 'keywordp arg)
312 (signal 'error (list "Keyword is missing an argument" keyword)))
313 (setq args (cdr args))
314 (custom-handle-keyword symbol keyword value type)))))
316 (defun custom-handle-keyword (symbol keyword value type)
317 "For customization option SYMBOL, handle KEYWORD with VALUE.
318 Fourth argument TYPE is the custom option type."
319 (cond ((eq keyword :group)
320 (custom-add-to-group value symbol type))
321 ((eq keyword :version)
322 (custom-add-version symbol value))
324 (custom-add-link symbol value))
326 (custom-add-load symbol value))
328 (put symbol 'custom-tag value))
330 (signal 'error (list "Unknown keyword" keyword)))))
332 (defun custom-add-option (symbol option)
333 "To the variable SYMBOL add OPTION.
335 If SYMBOL is a hook variable, OPTION should be a hook member.
336 For other types variables, the effect is undefined."
337 (let ((options (get symbol 'custom-options)))
338 (unless (member option options)
339 (put symbol 'custom-options (cons option options)))))
341 (defun custom-add-link (symbol widget)
342 "To the custom option SYMBOL add the link WIDGET."
343 (let ((links (get symbol 'custom-links)))
344 (unless (member widget links)
345 (put symbol 'custom-links (cons widget links)))))
347 (defun custom-add-version (symbol version)
348 "To the custom option SYMBOL add the version VERSION."
349 (put symbol 'custom-version version))
351 (defun custom-add-load (symbol load)
352 "To the custom option SYMBOL add the dependency LOAD.
353 LOAD should be either a library file name, or a feature name."
354 (puthash symbol t custom-group-hash-table)
355 (let ((loads (get symbol 'custom-loads)))
356 (unless (member load loads)
357 (put symbol 'custom-loads (cons load loads)))))
361 (defun custom-set-variables (&rest args)
362 "Initialize variables according to user preferences.
364 The arguments should be a list where each entry has the form:
368 The unevaluated VALUE is stored as the saved value for SYMBOL.
369 If NOW is present and non-nil, VALUE is also evaluated and bound as
370 the default value for the SYMBOL."
372 (let ((entry (car args)))
374 (let* ((symbol (nth 0 entry))
375 (value (nth 1 entry))
377 (requests (nth 3 entry))
378 (set (or (get symbol 'custom-set) 'set-default)))
379 (put symbol 'saved-value (list value))
381 ;; Rogue variable, set it now.
382 (put symbol 'force-value t)
383 (funcall set symbol (eval value)))
384 ((default-boundp symbol)
385 ;; Something already set this, overwrite it.
386 (funcall set symbol (eval value))))
388 (put symbol 'custom-requests requests)
389 (mapc 'require requests))
390 (setq args (cdr args)))
391 ;; Old format, a plist of SYMBOL VALUE pairs.
392 (message "Warning: old format `custom-set-variables'")
395 (let ((symbol (nth 0 args))
396 (value (nth 1 args)))
397 (put symbol 'saved-value (list value)))
398 (setq args (cdr (cdr args)))))))
404 ;; custom.el ends here