XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / lisp / custom.el
1 ;;; custom.el -- Tools for declaring and initializing options.
2
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@srce.hr>
7 ;; Keywords: help, faces, dumped
8 ;; Version: 1.9960-x
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10
11 ;; This file is part of XEmacs.
12
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)
16 ;; any later version.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs.
31
32 ;; This file only contain the code needed to declare and initialize
33 ;; user options.  The code to customize options is autoloaded from
34 ;; `cus-edit.el'. 
35 ;;
36 ;; The code implementing face declarations is in `cus-face.el'
37
38 ;;; Code:
39
40 (require 'widget)
41
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.")
45
46 ;;; The `defcustom' Macro.
47
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
54 symbol."
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)))
59                           (eval value)))))
60
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)
67              symbol 
68              (if (get symbol 'saved-value)
69                  (eval (car (get symbol 'saved-value)))
70                (eval value)))))
71
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)
77              symbol 
78              (cond ((default-boundp symbol)
79                     (funcall (or (get symbol 'custom-get) 'default-value)
80                              symbol))
81                    ((get symbol 'saved-value)
82                     (eval (car (get symbol 'saved-value))))
83                    (t
84                     (eval value)))))
85
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)
92                   symbol
93                   (funcall (or (get symbol 'custom-get) 'default-value)
94                            symbol)))
95         ((get symbol 'saved-value)
96          (funcall (or (get symbol 'custom-set) 'set-default)
97                   symbol
98                   (eval (car (get symbol 'saved-value)))))
99         (t
100          (set-default symbol (eval value)))))
101
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)
108     ;; It no longer is.    
109     (put symbol 'force-value nil))
110   (when doc
111     (put symbol 'variable-documentation doc))
112   (let ((initialize 'custom-initialize-reset)
113         (requests nil))
114     (while args 
115       (let ((arg (car args)))
116         (setq args (cdr args))
117         (check-argument-type 'keywordp arg)
118         (let ((keyword arg)
119               (value (car args)))
120           (unless args
121             (signal 'error (list "Keyword is missing an argument" keyword)))
122           (setq args (cdr args))
123           (cond ((eq keyword :initialize)
124                  (setq initialize value))
125                 ((eq keyword :set)
126                  (put symbol 'custom-set value))
127                 ((eq keyword :get)
128                  (put symbol 'custom-get value))
129                 ((eq keyword :require)
130                  (setq requests (cons value requests)))
131                 ((eq keyword :type)
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))
138                            value)
139                    ;; Fast code for the common case.
140                    (put symbol 'custom-options (copy-sequence value))))
141                 (t
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)
151   symbol)
152
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.
156
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
160
161    [KEYWORD VALUE]... 
162
163 The following KEYWORD's are defined:
164
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
180         `default-value'. 
181 :require VALUE should be a feature symbol.  Each feature will be
182         required after initialization, of the the user have saved this
183         option.
184
185 Read the section about customization in the Emacs Lisp manual for more
186 information."
187   `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
188
189 ;;; The `defface' Macro.
190
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.
194
195 Third argument DOC is the face documentation.
196
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
199 according to SPEC.
200
201 The remaining arguments should have the form
202
203    [KEYWORD VALUE]...
204
205 The following KEYWORDs are defined:
206
207 :group  VALUE should be a customization group.
208         Add FACE to that group.
209
210 SPEC should be an alist of the form ((DISPLAY ATTS)...).
211
212 ATTS is a list of face attributes and their values.  The possible
213 attributes are defined in the variable `custom-face-attributes'.
214
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
218 \((REQ ITEM...)...)
219
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:
222
223 `type' (the value of `window-system')
224   Should be one of `x' or `tty'.
225
226 `class' (the frame's color support)
227   Should be one of `color', `grayscale', or `mono'.
228
229 `background' (what color is used for the background text)
230   Should be one of `light' or `dark'.
231
232 Read the section about customization in the Emacs Lisp manual for more
233 information."
234   `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
235
236 ;;; The `defgroup' Macro.
237
238 (defun custom-declare-group (symbol members doc &rest args)
239   "Like `defgroup', but SYMBOL is evaluated as a normal argument."
240   (while members 
241     (apply 'custom-add-to-group symbol (car members))
242     (pop members))
243   (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
244   (when doc
245     (put symbol 'group-documentation doc))
246   (while args
247     (let ((arg (car args)))
248       (setq args (cdr args))
249       (check-argument-type 'keywordp arg)
250       (let ((keyword arg)
251             (value (car args)))
252         (unless args
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))
257               (t
258                (custom-handle-keyword symbol keyword value
259                                       'custom-group))))))
260   (run-hooks 'custom-define-hook)
261   symbol)
262
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.
266
267 Third arg DOC is the group documentation.
268
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.
273
274 The remaining arguments should have the form
275
276    [KEYWORD VALUE]... 
277
278 The following KEYWORD's are defined:
279
280 :group  VALUE should be a customization group.
281         Add SYMBOL to that group.
282
283 Read the section about customization in the Emacs Lisp manual for more
284 information."
285   `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
286
287 ;; This is preloaded very early, so we avoid using CL features.
288 (defvar custom-group-hash-table (make-hashtable 300 'eq)
289   "Hash-table of non-empty groups.")
290
291 (defun custom-add-to-group (group option widget)
292   "To existing GROUP add a new OPTION of type WIDGET.
293 If there already is an entry for that option, overwrite it."
294   (let* ((members (get group 'custom-group))
295          (old (assq option members)))
296     (if old
297         (setcar (cdr old) widget)
298       (put group 'custom-group (nconc members (list (list option widget))))))
299   (puthash group t custom-group-hash-table))
300
301 ;;; Properties.
302
303 (defun custom-handle-all-keywords (symbol args type)
304   "For customization option SYMBOL, handle keyword arguments ARGS.
305 Third argument TYPE is the custom option type."
306   (while args 
307     (let ((arg (car args)))
308       (setq args (cdr args))
309       (check-argument-type 'keywordp arg)
310       (let ((keyword arg)
311             (value (car args)))
312         (unless args
313           (signal 'error (list "Keyword is missing an argument" keyword)))
314         (setq args (cdr args))
315         (custom-handle-keyword symbol keyword value type)))))  
316
317 (defun custom-handle-keyword (symbol keyword value type)
318   "For customization option SYMBOL, handle KEYWORD with VALUE.
319 Fourth argument TYPE is the custom option type."
320   (cond ((eq keyword :group)
321          (custom-add-to-group value symbol type))
322         ((eq keyword :version)
323          (custom-add-version symbol value))
324         ((eq keyword :link)
325          (custom-add-link symbol value))
326         ((eq keyword :load)
327          (custom-add-load symbol value))
328         ((eq keyword :tag)
329          (put symbol 'custom-tag value))
330         (t
331          (signal 'error (list "Unknown keyword" keyword)))))
332
333 (defun custom-add-option (symbol option)
334   "To the variable SYMBOL add OPTION.
335
336 If SYMBOL is a hook variable, OPTION should be a hook member.
337 For other types variables, the effect is undefined."
338   (let ((options (get symbol 'custom-options)))
339     (unless (member option options)
340       (put symbol 'custom-options (cons option options)))))
341
342 (defun custom-add-link (symbol widget)
343   "To the custom option SYMBOL add the link WIDGET."
344   (let ((links (get symbol 'custom-links)))
345     (unless (member widget links)
346       (put symbol 'custom-links (cons widget links)))))
347
348 (defun custom-add-version (symbol version)
349   "To the custom option SYMBOL add the version VERSION."
350   (put symbol 'custom-version version))
351
352 (defun custom-add-load (symbol load)
353   "To the custom option SYMBOL add the dependency LOAD.
354 LOAD should be either a library file name, or a feature name."
355   (puthash symbol t custom-group-hash-table)
356   (let ((loads (get symbol 'custom-loads)))
357     (unless (member load loads)
358       (put symbol 'custom-loads (cons load loads)))))
359
360 ;;; Initializing.
361
362 (defun custom-set-variables (&rest args)
363   "Initialize variables according to user preferences.  
364
365 The arguments should be a list where each entry has the form:
366
367   (SYMBOL VALUE [NOW])
368
369 The unevaluated VALUE is stored as the saved value for SYMBOL.
370 If NOW is present and non-nil, VALUE is also evaluated and bound as
371 the default value for the SYMBOL."
372   (while args 
373     (let ((entry (car args)))
374       (if (listp entry)
375           (let* ((symbol (nth 0 entry))
376                  (value (nth 1 entry))
377                  (now (nth 2 entry))
378                  (requests (nth 3 entry))
379                  (set (or (get symbol 'custom-set) 'set-default)))
380             (put symbol 'saved-value (list value))
381             (cond (now 
382                    ;; Rogue variable, set it now.
383                    (put symbol 'force-value t)
384                    (funcall set symbol (eval value)))
385                   ((default-boundp symbol)
386                    ;; Something already set this, overwrite it.
387                    (funcall set symbol (eval value))))
388             (when requests
389               (put symbol 'custom-requests requests)
390               (mapc 'require requests))
391             (setq args (cdr args)))
392         ;; Old format, a plist of SYMBOL VALUE pairs.
393         (message "Warning: old format `custom-set-variables'")
394         (ding)
395         (sit-for 2)
396         (let ((symbol (nth 0 args))
397               (value (nth 1 args)))
398           (put symbol 'saved-value (list value)))
399         (setq args (cdr (cdr args)))))))
400
401 ;;; The End.
402
403 (provide 'custom)
404
405 ;; custom.el ends here