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