(texinfo-all-menus-update)
[bpt/emacs.git] / lisp / custom.el
CommitLineData
d543e20b 1;;; custom.el -- Tools for declaring and initializing options.
41487370 2;;
d543e20b 3;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
41487370 4;;
d543e20b
PA
5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6;; Keywords: help, faces
25ac13b5 7;; Version: 1.9900
d543e20b
PA
8;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
c2383d2d
RS
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
d543e20b 27;;; Commentary:
41487370 28;;
d543e20b 29;; If you want to use this code, please visit the URL above.
41487370 30;;
d543e20b
PA
31;; This file only contain the code needed to declare and initialize
32;; user options. The code to customize options is autoloaded from
33;; `cus-edit.el'.
41487370 34
d543e20b 35;; The code implementing face declarations is in `cus-face.el'
41487370
LMI
36
37;;; Code:
38
d543e20b
PA
39(require 'widget)
40
6d528fc5
PA
41(define-widget-keywords :initialize :set :get :require :prefix :tag
42 :load :link :options :type :group)
43
d543e20b 44
bd042c03
PA
45(defvar custom-define-hook nil
46 ;; Customize information for this option is in `cus-edit.el'.
47 "Hook called after defining each customize option.")
48
d543e20b
PA
49;;; The `defcustom' Macro.
50
6d528fc5
PA
51(defun custom-initialize-default (symbol value)
52 "Initialize SYMBOL with VALUE.
53This will do nothing if symbol already has a default binding.
54Otherwise, if symbol has a `saved-value' property, it will evaluate
55the car of that and used as the default binding for symbol.
56Otherwise, VALUE will be evaluated and used as the default binding for
57symbol."
bd042c03 58 (unless (default-boundp symbol)
25ac13b5 59 ;; Use the saved value if it exists, otherwise the standard setting.
d543e20b
PA
60 (set-default symbol (if (get symbol 'saved-value)
61 (eval (car (get symbol 'saved-value)))
6d528fc5
PA
62 (eval value)))))
63
64(defun custom-initialize-set (symbol value)
65 "Initialize SYMBOL with VALUE.
66Like `custom-initialize-default', but use the function specified by
67`:set' to initialize SYMBOL."
68 (unless (default-boundp symbol)
69 (funcall (or (get symbol 'custom-set) 'set-default)
70 symbol
71 (if (get symbol 'saved-value)
72 (eval (car (get symbol 'saved-value)))
73 (eval value)))))
74
75(defun custom-initialize-reset (symbol value)
76 "Initialize SYMBOL with VALUE.
77Like `custom-initialize-set', but use the function specified by
78`:get' to reinitialize SYMBOL if it is already bound."
79 (funcall (or (get symbol 'custom-set) 'set-default)
80 symbol
81 (cond ((default-boundp symbol)
82 (funcall (or (get symbol 'custom-get) 'default-value)
83 symbol))
84 ((get symbol 'saved-value)
85 (eval (car (get symbol 'saved-value))))
86 (t
87 (eval value)))))
88
89(defun custom-initialize-changed (symbol value)
90 "Initialize SYMBOL with VALUE.
91Like `custom-initialize-reset', but only use the `:set' function if the
25ac13b5 92not using the standard setting. Otherwise, use the `set-default'."
6d528fc5
PA
93 (cond ((default-boundp symbol)
94 (funcall (or (get symbol 'custom-set) 'set-default)
95 symbol
96 (funcall (or (get symbol 'custom-get) 'default-value)
97 symbol)))
98 ((get symbol 'saved-value)
99 (funcall (or (get symbol 'custom-set) 'set-default)
100 symbol
101 (eval (car (get symbol 'saved-value)))))
102 (t
103 (set-default symbol (eval value)))))
104
105(defun custom-declare-variable (symbol value doc &rest args)
106 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
25ac13b5
PA
107 ;; Remember the standard setting.
108 (put symbol 'standard-value (list value))
bd042c03
PA
109 ;; Maybe this option was rogue in an earlier version. It no longer is.
110 (when (get symbol 'force-value)
111 ;; It no longer is.
112 (put symbol 'force-value nil))
d543e20b
PA
113 (when doc
114 (put symbol 'variable-documentation doc))
6d528fc5
PA
115 (let ((initialize 'custom-initialize-set)
116 (requests nil))
117 (while args
118 (let ((arg (car args)))
d543e20b 119 (setq args (cdr args))
6d528fc5
PA
120 (unless (symbolp arg)
121 (error "Junk in args %S" args))
122 (let ((keyword arg)
123 (value (car args)))
124 (unless args
125 (error "Keyword %s is missing an argument" keyword))
126 (setq args (cdr args))
127 (cond ((eq keyword :initialize)
128 (setq initialize value))
129 ((eq keyword :set)
130 (put symbol 'custom-set value))
131 ((eq keyword :get)
132 (put symbol 'custom-get value))
133 ((eq keyword :require)
67efacf8 134 (setq requests (cons value requests)))
6d528fc5
PA
135 ((eq keyword :type)
136 (put symbol 'custom-type value))
137 ((eq keyword :options)
138 (if (get symbol 'custom-options)
139 ;; Slow safe code to avoid duplicates.
140 (mapcar (lambda (option)
141 (custom-add-option symbol option))
142 value)
143 ;; Fast code for the common case.
144 (put symbol 'custom-options (copy-sequence value))))
145 (t
146 (custom-handle-keyword symbol keyword value
147 'custom-variable))))))
148 (put symbol 'custom-requests requests)
149 ;; Do the actual initialization.
150 (funcall initialize symbol value))
d543e20b
PA
151 (run-hooks 'custom-define-hook)
152 symbol)
41487370 153
d543e20b
PA
154(defmacro defcustom (symbol value doc &rest args)
155 "Declare SYMBOL as a customizable variable that defaults to VALUE.
156DOC is the variable documentation.
41487370 157
d543e20b
PA
158Neither SYMBOL nor VALUE needs to be quoted.
159If SYMBOL is not already bound, initialize it to VALUE.
160The remaining arguments should have the form
41487370 161
d543e20b 162 [KEYWORD VALUE]...
41487370 163
d543e20b 164The following KEYWORD's are defined:
41487370 165
6d528fc5
PA
166:type VALUE should be a widget type for editing the symbols value.
167 The default is `sexp'.
d543e20b
PA
168:options VALUE should be a list of valid members of the widget type.
169:group VALUE should be a customization group.
170 Add SYMBOL to that group.
6d528fc5
PA
171:initialize VALUE should be a function used to initialize the
172 variable. It takes two arguments, the symbol and value
173 given in the `defcustom' call. The default is
174 `custom-initialize-default'
175:set VALUE should be a function to set the value of the symbol.
176 It takes two arguments, the symbol to set and the value to
177 give it. The default is `set-default'.
178:get VALUE should be a function to extract the value of symbol.
179 The function takes one argument, a symbol, and should return
180 the current value for that symbol. The default is
181 `default-value'.
182:require VALUE should be a feature symbol. Each feature will be
183 required after initialization, of the the user have saved this
184 option.
41487370 185
305ec106 186Read the section about customization in the Emacs Lisp manual for more
d543e20b 187information."
9dfa30b5 188 `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
41487370 189
d543e20b 190;;; The `defface' Macro.
41487370 191
d543e20b
PA
192(defmacro defface (face spec doc &rest args)
193 "Declare FACE as a customizable face that defaults to SPEC.
194FACE does not need to be quoted.
41487370 195
d543e20b 196Third argument DOC is the face documentation.
41487370 197
d543e20b
PA
198If FACE has been set with `custom-set-face', set the face attributes
199as specified by that function, otherwise set the face attributes
200according to SPEC.
41487370 201
d543e20b 202The remaining arguments should have the form
41487370 203
d543e20b 204 [KEYWORD VALUE]...
41487370 205
aafc86d2 206The following KEYWORDs are defined:
41487370 207
d543e20b
PA
208:group VALUE should be a customization group.
209 Add FACE to that group.
41487370 210
d543e20b 211SPEC should be an alist of the form ((DISPLAY ATTS)...).
41487370 212
aafc86d2
RS
213The first element of SPEC where the DISPLAY matches the frame
214is the one that takes effect in that frame. The ATTRs in this
215element take effect; the other elements are ignored, on that frame.
41487370 216
aafc86d2
RS
217ATTS is a list of face attributes followed by their values:
218 (ATTR VALUE ATTR VALUE...)
219The possible attributes are `:bold', `:italic', `:underline',
220`:foreground', `:background', `:stipple' and `:inverse-video'.
41487370 221
aafc86d2
RS
222DISPLAY can either be the symbol t, which will match all frames, or an
223alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
224FRAME, the REQ property of the frame must match one of the ITEM. The
225following REQ are defined:
41487370 226
d543e20b
PA
227`type' (the value of `window-system')
228 Should be one of `x' or `tty'.
41487370 229
d543e20b
PA
230`class' (the frame's color support)
231 Should be one of `color', `grayscale', or `mono'.
41487370 232
d543e20b
PA
233`background' (what color is used for the background text)
234 Should be one of `light' or `dark'.
41487370 235
305ec106 236Read the section about customization in the Emacs Lisp manual for more
d543e20b
PA
237information."
238 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
41487370 239
d543e20b 240;;; The `defgroup' Macro.
41487370 241
d543e20b
PA
242(defun custom-declare-group (symbol members doc &rest args)
243 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
6d528fc5
PA
244 (while members
245 (apply 'custom-add-to-group symbol (car members))
246 (setq members (cdr members)))
d543e20b
PA
247 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
248 (when doc
249 (put symbol 'group-documentation doc))
250 (while args
251 (let ((arg (car args)))
252 (setq args (cdr args))
253 (unless (symbolp arg)
254 (error "Junk in args %S" args))
255 (let ((keyword arg)
256 (value (car args)))
257 (unless args
258 (error "Keyword %s is missing an argument" keyword))
259 (setq args (cdr args))
260 (cond ((eq keyword :prefix)
261 (put symbol 'custom-prefix value))
262 (t
263 (custom-handle-keyword symbol keyword value
264 'custom-group))))))
265 (run-hooks 'custom-define-hook)
266 symbol)
267
268(defmacro defgroup (symbol members doc &rest args)
269 "Declare SYMBOL as a customization group containing MEMBERS.
270SYMBOL does not need to be quoted.
271
272Third arg DOC is the group documentation.
273
274MEMBERS should be an alist of the form ((NAME WIDGET)...) where
275NAME is a symbol and WIDGET is a widget is a widget for editing that
276symbol. Useful widgets are `custom-variable' for editing variables,
277`custom-face' for edit faces, and `custom-group' for editing groups.
278
279The remaining arguments should have the form
280
281 [KEYWORD VALUE]...
282
283The following KEYWORD's are defined:
284
285:group VALUE should be a customization group.
286 Add SYMBOL to that group.
287
305ec106 288Read the section about customization in the Emacs Lisp manual for more
d543e20b
PA
289information."
290 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
291
292(defun custom-add-to-group (group option widget)
293 "To existing GROUP add a new OPTION of type WIDGET.
294If there already is an entry for that option, overwrite it."
295 (let* ((members (get group 'custom-group))
296 (old (assq option members)))
297 (if old
298 (setcar (cdr old) widget)
299 (put group 'custom-group (nconc members (list (list option widget)))))))
300
301;;; Properties.
302
303(defun custom-handle-all-keywords (symbol args type)
304 "For customization option SYMBOL, handle keyword arguments ARGS.
305Third argument TYPE is the custom option type."
306 (while args
307 (let ((arg (car args)))
308 (setq args (cdr args))
309 (unless (symbolp arg)
310 (error "Junk in args %S" args))
311 (let ((keyword arg)
312 (value (car args)))
313 (unless args
314 (error "Keyword %s is missing an argument" keyword))
315 (setq args (cdr args))
316 (custom-handle-keyword symbol keyword value type)))))
317
318(defun custom-handle-keyword (symbol keyword value type)
319 "For customization option SYMBOL, handle KEYWORD with VALUE.
320Fourth argument TYPE is the custom option type."
321 (cond ((eq keyword :group)
322 (custom-add-to-group value symbol type))
323 ((eq keyword :link)
324 (custom-add-link symbol value))
325 ((eq keyword :load)
326 (custom-add-load symbol value))
327 ((eq keyword :tag)
328 (put symbol 'custom-tag value))
41487370 329 (t
d543e20b
PA
330 (error "Unknown keyword %s" symbol))))
331
332(defun custom-add-option (symbol option)
333 "To the variable SYMBOL add OPTION.
334
335If SYMBOL is a hook variable, OPTION should be a hook member.
336For 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)))))
340
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)))))
346
347(defun custom-add-load (symbol load)
348 "To the custom option SYMBOL add the dependency LOAD.
349LOAD should be either a library file name, or a feature name."
350 (let ((loads (get symbol 'custom-loads)))
351 (unless (member load loads)
352 (put symbol 'custom-loads (cons load loads)))))
353
354;;; Initializing.
355
356(defun custom-set-variables (&rest args)
357 "Initialize variables according to user preferences.
358
359The arguments should be a list where each entry has the form:
360
361 (SYMBOL VALUE [NOW])
362
363The unevaluated VALUE is stored as the saved value for SYMBOL.
364If NOW is present and non-nil, VALUE is also evaluated and bound as
365the default value for the SYMBOL."
366 (while args
367 (let ((entry (car args)))
368 (if (listp entry)
6d528fc5
PA
369 (let* ((symbol (nth 0 entry))
370 (value (nth 1 entry))
371 (now (nth 2 entry))
372 (requests (nth 3 entry))
373 (set (or (get symbol 'custom-set) 'set-default)))
d543e20b 374 (put symbol 'saved-value (list value))
bd042c03
PA
375 (cond (now
376 ;; Rogue variable, set it now.
377 (put symbol 'force-value t)
6d528fc5 378 (funcall set symbol (eval value)))
bd042c03
PA
379 ((default-boundp symbol)
380 ;; Something already set this, overwrite it.
6d528fc5
PA
381 (funcall set symbol (eval value))))
382 (when requests
383 (put symbol 'custom-requests requests)
384 (mapcar 'require requests))
d543e20b
PA
385 (setq args (cdr args)))
386 ;; Old format, a plist of SYMBOL VALUE pairs.
bd042c03
PA
387 (message "Warning: old format `custom-set-variables'")
388 (ding)
389 (sit-for 2)
d543e20b
PA
390 (let ((symbol (nth 0 args))
391 (value (nth 1 args)))
392 (put symbol 'saved-value (list value)))
393 (setq args (cdr (cdr args)))))))
394
d543e20b 395;;; The End.
41487370
LMI
396
397(provide 'custom)
398
d543e20b 399;; custom.el ends here