]> code.delx.au - gnu-emacs/blob - lisp/custom.el
(transient-mark-mode): Print message if used interactively.
[gnu-emacs] / 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 ;; Keywords: help, faces
7 ;; Version: 1.84
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
10 ;;; Commentary:
11 ;;
12 ;; If you want to use this code, please visit the URL above.
13 ;;
14 ;; This file only contain the code needed to declare and initialize
15 ;; user options. The code to customize options is autoloaded from
16 ;; `cus-edit.el'.
17
18 ;; The code implementing face declarations is in `cus-face.el'
19
20 ;;; Code:
21
22 (require 'widget)
23
24 (define-widget-keywords :prefix :tag :load :link :options :type :group)
25
26 (defvar custom-define-hook nil
27 ;; Customize information for this option is in `cus-edit.el'.
28 "Hook called after defining each customize option.")
29
30 ;;; The `defcustom' Macro.
31
32 (defun custom-declare-variable (symbol value doc &rest args)
33 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
34 ;; Bind this variable unless it already is bound.
35 (unless (default-boundp symbol)
36 ;; Use the saved value if it exists, otherwise the factory setting.
37 (set-default symbol (if (get symbol 'saved-value)
38 (eval (car (get symbol 'saved-value)))
39 (eval value))))
40 ;; Remember the factory setting.
41 (put symbol 'factory-value (list value))
42 ;; Maybe this option was rogue in an earlier version. It no longer is.
43 (when (get symbol 'force-value)
44 ;; It no longer is.
45 (put symbol 'force-value nil))
46 (when doc
47 (put symbol 'variable-documentation doc))
48 (while args
49 (let ((arg (car args)))
50 (setq args (cdr args))
51 (unless (symbolp arg)
52 (error "Junk in args %S" args))
53 (let ((keyword arg)
54 (value (car args)))
55 (unless args
56 (error "Keyword %s is missing an argument" keyword))
57 (setq args (cdr args))
58 (cond ((eq keyword :type)
59 (put symbol 'custom-type value))
60 ((eq keyword :options)
61 (if (get symbol 'custom-options)
62 ;; Slow safe code to avoid duplicates.
63 (mapcar (lambda (option)
64 (custom-add-option symbol option))
65 value)
66 ;; Fast code for the common case.
67 (put symbol 'custom-options (copy-list value))))
68 (t
69 (custom-handle-keyword symbol keyword value
70 'custom-variable))))))
71 (run-hooks 'custom-define-hook)
72 symbol)
73
74 (defmacro defcustom (symbol value doc &rest args)
75 "Declare SYMBOL as a customizable variable that defaults to VALUE.
76 DOC is the variable documentation.
77
78 Neither SYMBOL nor VALUE needs to be quoted.
79 If SYMBOL is not already bound, initialize it to VALUE.
80 The remaining arguments should have the form
81
82 [KEYWORD VALUE]...
83
84 The following KEYWORD's are defined:
85
86 :type VALUE should be a widget type.
87 :options VALUE should be a list of valid members of the widget type.
88 :group VALUE should be a customization group.
89 Add SYMBOL to that group.
90
91 Read the section about customization in the Emacs Lisp manual for more
92 information."
93 `(eval-and-compile
94 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
95
96 ;;; The `defface' Macro.
97
98 (defmacro defface (face spec doc &rest args)
99 "Declare FACE as a customizable face that defaults to SPEC.
100 FACE does not need to be quoted.
101
102 Third argument DOC is the face documentation.
103
104 If FACE has been set with `custom-set-face', set the face attributes
105 as specified by that function, otherwise set the face attributes
106 according to SPEC.
107
108 The remaining arguments should have the form
109
110 [KEYWORD VALUE]...
111
112 The following KEYWORD's are defined:
113
114 :group VALUE should be a customization group.
115 Add FACE to that group.
116
117 SPEC should be an alist of the form ((DISPLAY ATTS)...).
118
119 ATTS is a list of face attributes and their values. The possible
120 attributes are defined in the variable `custom-face-attributes'.
121 Alternatively, ATTS can be a face in which case the attributes of that
122 face is used.
123
124 The ATTS of the first entry in SPEC where the DISPLAY matches the
125 frame should take effect in that frame. DISPLAY can either be the
126 symbol t, which will match all frames, or an alist of the form
127 \((REQ ITEM...)...)
128
129 For the DISPLAY to match a FRAME, the REQ property of the frame must
130 match one of the ITEM. The following REQ are defined:
131
132 `type' (the value of `window-system')
133 Should be one of `x' or `tty'.
134
135 `class' (the frame's color support)
136 Should be one of `color', `grayscale', or `mono'.
137
138 `background' (what color is used for the background text)
139 Should be one of `light' or `dark'.
140
141 Read the section about customization in the Emacs Lisp manual for more
142 information."
143 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
144
145 ;;; The `defgroup' Macro.
146
147 (defun custom-declare-group (symbol members doc &rest args)
148 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
149 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
150 (when doc
151 (put symbol 'group-documentation doc))
152 (while args
153 (let ((arg (car args)))
154 (setq args (cdr args))
155 (unless (symbolp arg)
156 (error "Junk in args %S" args))
157 (let ((keyword arg)
158 (value (car args)))
159 (unless args
160 (error "Keyword %s is missing an argument" keyword))
161 (setq args (cdr args))
162 (cond ((eq keyword :prefix)
163 (put symbol 'custom-prefix value))
164 (t
165 (custom-handle-keyword symbol keyword value
166 'custom-group))))))
167 (run-hooks 'custom-define-hook)
168 symbol)
169
170 (defmacro defgroup (symbol members doc &rest args)
171 "Declare SYMBOL as a customization group containing MEMBERS.
172 SYMBOL does not need to be quoted.
173
174 Third arg DOC is the group documentation.
175
176 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
177 NAME is a symbol and WIDGET is a widget is a widget for editing that
178 symbol. Useful widgets are `custom-variable' for editing variables,
179 `custom-face' for edit faces, and `custom-group' for editing groups.
180
181 The remaining arguments should have the form
182
183 [KEYWORD VALUE]...
184
185 The following KEYWORD's are defined:
186
187 :group VALUE should be a customization group.
188 Add SYMBOL to that group.
189
190 Read the section about customization in the Emacs Lisp manual for more
191 information."
192 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
193
194 (defun custom-add-to-group (group option widget)
195 "To existing GROUP add a new OPTION of type WIDGET.
196 If there already is an entry for that option, overwrite it."
197 (let* ((members (get group 'custom-group))
198 (old (assq option members)))
199 (if old
200 (setcar (cdr old) widget)
201 (put group 'custom-group (nconc members (list (list option widget)))))))
202
203 ;;; Properties.
204
205 (defun custom-handle-all-keywords (symbol args type)
206 "For customization option SYMBOL, handle keyword arguments ARGS.
207 Third argument TYPE is the custom option type."
208 (while args
209 (let ((arg (car args)))
210 (setq args (cdr args))
211 (unless (symbolp arg)
212 (error "Junk in args %S" args))
213 (let ((keyword arg)
214 (value (car args)))
215 (unless args
216 (error "Keyword %s is missing an argument" keyword))
217 (setq args (cdr args))
218 (custom-handle-keyword symbol keyword value type)))))
219
220 (defun custom-handle-keyword (symbol keyword value type)
221 "For customization option SYMBOL, handle KEYWORD with VALUE.
222 Fourth argument TYPE is the custom option type."
223 (cond ((eq keyword :group)
224 (custom-add-to-group value symbol type))
225 ((eq keyword :link)
226 (custom-add-link symbol value))
227 ((eq keyword :load)
228 (custom-add-load symbol value))
229 ((eq keyword :tag)
230 (put symbol 'custom-tag value))
231 (t
232 (error "Unknown keyword %s" symbol))))
233
234 (defun custom-add-option (symbol option)
235 "To the variable SYMBOL add OPTION.
236
237 If SYMBOL is a hook variable, OPTION should be a hook member.
238 For other types variables, the effect is undefined."
239 (let ((options (get symbol 'custom-options)))
240 (unless (member option options)
241 (put symbol 'custom-options (cons option options)))))
242
243 (defun custom-add-link (symbol widget)
244 "To the custom option SYMBOL add the link WIDGET."
245 (let ((links (get symbol 'custom-links)))
246 (unless (member widget links)
247 (put symbol 'custom-links (cons widget links)))))
248
249 (defun custom-add-load (symbol load)
250 "To the custom option SYMBOL add the dependency LOAD.
251 LOAD should be either a library file name, or a feature name."
252 (let ((loads (get symbol 'custom-loads)))
253 (unless (member load loads)
254 (put symbol 'custom-loads (cons load loads)))))
255
256 ;;; Initializing.
257
258 (defun custom-set-variables (&rest args)
259 "Initialize variables according to user preferences.
260
261 The arguments should be a list where each entry has the form:
262
263 (SYMBOL VALUE [NOW])
264
265 The unevaluated VALUE is stored as the saved value for SYMBOL.
266 If NOW is present and non-nil, VALUE is also evaluated and bound as
267 the default value for the SYMBOL."
268 (while args
269 (let ((entry (car args)))
270 (if (listp entry)
271 (let ((symbol (nth 0 entry))
272 (value (nth 1 entry))
273 (now (nth 2 entry)))
274 (put symbol 'saved-value (list value))
275 (cond (now
276 ;; Rogue variable, set it now.
277 (put symbol 'force-value t)
278 (set-default symbol (eval value)))
279 ((default-boundp symbol)
280 ;; Something already set this, overwrite it.
281 (set-default symbol (eval value))))
282 (setq args (cdr args)))
283 ;; Old format, a plist of SYMBOL VALUE pairs.
284 (message "Warning: old format `custom-set-variables'")
285 (ding)
286 (sit-for 2)
287 (let ((symbol (nth 0 args))
288 (value (nth 1 args)))
289 (put symbol 'saved-value (list value)))
290 (setq args (cdr (cdr args)))))))
291
292 ;;; The End.
293
294 (provide 'custom)
295
296 ;; custom.el ends here