Commit | Line | Data |
---|---|---|
55535639 PJ |
1 | ;;; custom.el --- tools for declaring and initializing options |
2 | ;; | |
00644b82 | 3 | ;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc. |
55535639 PJ |
4 | ;; |
5 | ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: help, faces | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ;; | |
e6bb31f3 | 28 | ;; This file only contains the code needed to declare and initialize |
55535639 PJ |
29 | ;; user options. The code to customize options is autoloaded from |
30 | ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual. | |
31 | ||
e6bb31f3 | 32 | ;; The code implementing face declarations is in `cus-face.el'. |
55535639 PJ |
33 | |
34 | ;;; Code: | |
35 | ||
36 | (require 'widget) | |
37 | ||
38 | (defvar custom-define-hook nil | |
39 | ;; Customize information for this option is in `cus-edit.el'. | |
40 | "Hook called after defining each customize option.") | |
41 | ||
6da43544 RS |
42 | (defvar custom-dont-initialize nil |
43 | "Non-nil means `defcustom' should not initialize the variable. | |
44 | That is used for the sake of `custom-make-dependencies'. | |
45 | Users should not set it.") | |
46 | ||
d3b80e9b SM |
47 | (defvar custom-current-group-alist nil |
48 | "Alist of (FILE . GROUP) indicating the current group to use for FILE.") | |
49 | ||
55535639 PJ |
50 | ;;; The `defcustom' Macro. |
51 | ||
52 | (defun custom-initialize-default (symbol value) | |
53 | "Initialize SYMBOL with VALUE. | |
54 | This will do nothing if symbol already has a default binding. | |
55 | Otherwise, if symbol has a `saved-value' property, it will evaluate | |
56 | the car of that and used as the default binding for symbol. | |
57 | Otherwise, VALUE will be evaluated and used as the default binding for | |
58 | symbol." | |
59 | (unless (default-boundp symbol) | |
60 | ;; Use the saved value if it exists, otherwise the standard setting. | |
61 | (set-default symbol (if (get symbol 'saved-value) | |
62 | (eval (car (get symbol 'saved-value))) | |
63 | (eval value))))) | |
64 | ||
65 | (defun custom-initialize-set (symbol value) | |
66 | "Initialize SYMBOL based on VALUE. | |
67 | If the symbol doesn't have a default binding already, | |
68 | then set it using its `:set' function (or `set-default' if it has none). | |
69 | The value is either the value in the symbol's `saved-value' property, | |
70 | if any, or VALUE." | |
71 | (unless (default-boundp symbol) | |
72 | (funcall (or (get symbol 'custom-set) 'set-default) | |
73 | symbol | |
74 | (if (get symbol 'saved-value) | |
75 | (eval (car (get symbol 'saved-value))) | |
76 | (eval value))))) | |
77 | ||
78 | (defun custom-initialize-reset (symbol value) | |
79 | "Initialize SYMBOL based on VALUE. | |
80 | Set the symbol, using its `:set' function (or `set-default' if it has none). | |
81 | The value is either the symbol's current value | |
82 | \(as obtained using the `:get' function), if any, | |
83 | or the value in the symbol's `saved-value' property if any, | |
84 | or (last of all) VALUE." | |
85 | (funcall (or (get symbol 'custom-set) 'set-default) | |
86 | symbol | |
87 | (cond ((default-boundp symbol) | |
88 | (funcall (or (get symbol 'custom-get) 'default-value) | |
89 | symbol)) | |
90 | ((get symbol 'saved-value) | |
91 | (eval (car (get symbol 'saved-value)))) | |
92 | (t | |
93 | (eval value))))) | |
94 | ||
95 | (defun custom-initialize-changed (symbol value) | |
96 | "Initialize SYMBOL with VALUE. | |
97 | Like `custom-initialize-reset', but only use the `:set' function if | |
98 | not using the standard setting. | |
99 | For the standard setting, use `set-default'." | |
100 | (cond ((default-boundp symbol) | |
101 | (funcall (or (get symbol 'custom-set) 'set-default) | |
102 | symbol | |
103 | (funcall (or (get symbol 'custom-get) 'default-value) | |
104 | symbol))) | |
105 | ((get symbol 'saved-value) | |
106 | (funcall (or (get symbol 'custom-set) 'set-default) | |
107 | symbol | |
108 | (eval (car (get symbol 'saved-value))))) | |
109 | (t | |
110 | (set-default symbol (eval value))))) | |
111 | ||
112 | (defun custom-declare-variable (symbol default doc &rest args) | |
113 | "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments. | |
114 | DEFAULT should be an expression to evaluate to compute the default value, | |
115 | not the default value itself." | |
116 | ;; Remember the standard setting. | |
117 | (put symbol 'standard-value (list default)) | |
118 | ;; Maybe this option was rogue in an earlier version. It no longer is. | |
119 | (when (get symbol 'force-value) | |
120 | (put symbol 'force-value nil)) | |
121 | (when doc | |
122 | (put symbol 'variable-documentation doc)) | |
123 | (let ((initialize 'custom-initialize-reset) | |
124 | (requests nil)) | |
d3b80e9b SM |
125 | (unless (memq :group args) |
126 | (custom-add-to-group (custom-current-group) symbol 'custom-variable)) | |
55535639 PJ |
127 | (while args |
128 | (let ((arg (car args))) | |
129 | (setq args (cdr args)) | |
130 | (unless (symbolp arg) | |
131 | (error "Junk in args %S" args)) | |
132 | (let ((keyword arg) | |
133 | (value (car args))) | |
134 | (unless args | |
135 | (error "Keyword %s is missing an argument" keyword)) | |
136 | (setq args (cdr args)) | |
137 | (cond ((eq keyword :initialize) | |
138 | (setq initialize value)) | |
139 | ((eq keyword :set) | |
140 | (put symbol 'custom-set value)) | |
141 | ((eq keyword :get) | |
142 | (put symbol 'custom-get value)) | |
143 | ((eq keyword :require) | |
144 | (setq requests (cons value requests))) | |
145 | ((eq keyword :type) | |
146 | (put symbol 'custom-type (purecopy value))) | |
147 | ((eq keyword :options) | |
148 | (if (get symbol 'custom-options) | |
149 | ;; Slow safe code to avoid duplicates. | |
150 | (mapc (lambda (option) | |
151 | (custom-add-option symbol option)) | |
152 | value) | |
153 | ;; Fast code for the common case. | |
154 | (put symbol 'custom-options (copy-sequence value)))) | |
155 | (t | |
156 | (custom-handle-keyword symbol keyword value | |
157 | 'custom-variable)))))) | |
158 | (put symbol 'custom-requests requests) | |
159 | ;; Do the actual initialization. | |
6da43544 RS |
160 | (unless custom-dont-initialize |
161 | (funcall initialize symbol default))) | |
55535639 PJ |
162 | (setq current-load-list (cons symbol current-load-list)) |
163 | (run-hooks 'custom-define-hook) | |
164 | symbol) | |
165 | ||
166 | (defmacro defcustom (symbol value doc &rest args) | |
167 | "Declare SYMBOL as a customizable variable that defaults to VALUE. | |
168 | DOC is the variable documentation. | |
169 | ||
170 | Neither SYMBOL nor VALUE needs to be quoted. | |
171 | If SYMBOL is not already bound, initialize it to VALUE. | |
172 | The remaining arguments should have the form | |
173 | ||
174 | [KEYWORD VALUE]... | |
175 | ||
176 | The following keywords are meaningful: | |
177 | ||
693d0bd3 | 178 | :type VALUE should be a widget type for editing the symbol's value. |
55535639 PJ |
179 | :options VALUE should be a list of valid members of the widget type. |
180 | :group VALUE should be a customization group. | |
181 | Add SYMBOL to that group. | |
3dc5f18e | 182 | :link LINK-DATA |
00644b82 EZ |
183 | Include an external link after the documentation string for this |
184 | item. This is a sentence containing an active field which | |
185 | references some other documentation. | |
186 | ||
187 | There are three alternatives you can use for LINK-DATA: | |
188 | ||
3dc5f18e | 189 | (custom-manual INFO-NODE) |
00644b82 | 190 | Link to an Info node; INFO-NODE is a string which specifies |
3dc5f18e | 191 | the node name, as in \"(emacs)Top\". The link appears as |
00644b82 EZ |
192 | `[manual]' in the customization buffer. |
193 | ||
3dc5f18e | 194 | (info-link INFO-NODE) |
00644b82 EZ |
195 | Like `custom-manual' except that the link appears in the |
196 | customization buffer with the Info node name. | |
197 | ||
3dc5f18e | 198 | (url-link URL) |
00644b82 EZ |
199 | Link to a web page; URL is a string which specifies the URL. |
200 | The link appears in the customization buffer as URL. | |
201 | ||
202 | You can specify the text to use in the customization buffer by | |
203 | adding `:tag NAME' after the first element of the LINK-DATA; for | |
3dc5f18e | 204 | example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the |
00644b82 EZ |
205 | Emacs manual which appears in the buffer as `foo'. |
206 | ||
207 | An item can have more than one external link; however, most items | |
208 | have none at all. | |
55535639 PJ |
209 | :initialize |
210 | VALUE should be a function used to initialize the | |
211 | variable. It takes two arguments, the symbol and value | |
212 | given in the `defcustom' call. The default is | |
a01639a3 | 213 | `custom-initialize-reset'. |
55535639 PJ |
214 | :set VALUE should be a function to set the value of the symbol. |
215 | It takes two arguments, the symbol to set and the value to | |
216 | give it. The default choice of function is `custom-set-default'. | |
217 | :get VALUE should be a function to extract the value of symbol. | |
218 | The function takes one argument, a symbol, and should return | |
219 | the current value for that symbol. The default choice of function | |
220 | is `custom-default-value'. | |
221 | :require | |
222 | VALUE should be a feature symbol. If you save a value | |
223 | for this option, then when your `.emacs' file loads the value, | |
224 | it does (require VALUE) first. | |
225 | :version | |
226 | VALUE should be a string specifying that the variable was | |
227 | first introduced, or its default value was changed, in Emacs | |
228 | version VERSION. | |
00644b82 EZ |
229 | :tag LABEL |
230 | Use LABEL, a string, instead of the item's name, to label the item | |
1c1da418 | 231 | in customization menus and buffers. |
00644b82 EZ |
232 | :load FILE |
233 | Load file FILE (a string) before displaying this customization | |
29512a0f | 234 | item. Loading is done with `load', and only if the file is |
00644b82 | 235 | not already loaded. |
a2b38e51 GM |
236 | :set-after VARIABLE |
237 | Specifies that SYMBOL should be set after VARIABLE when | |
238 | both have been customized. | |
55535639 PJ |
239 | |
240 | Read the section about customization in the Emacs Lisp manual for more | |
241 | information." | |
242 | ;; It is better not to use backquote in this file, | |
243 | ;; because that makes a bootstrapping problem | |
244 | ;; if you need to recompile all the Lisp files using interpreted code. | |
245 | (nconc (list 'custom-declare-variable | |
246 | (list 'quote symbol) | |
247 | (list 'quote value) | |
248 | doc) | |
249 | args)) | |
250 | ||
251 | ;;; The `defface' Macro. | |
252 | ||
253 | (defmacro defface (face spec doc &rest args) | |
254 | "Declare FACE as a customizable face that defaults to SPEC. | |
255 | FACE does not need to be quoted. | |
256 | ||
257 | Third argument DOC is the face documentation. | |
258 | ||
259 | If FACE has been set with `custom-set-face', set the face attributes | |
260 | as specified by that function, otherwise set the face attributes | |
261 | according to SPEC. | |
262 | ||
263 | The remaining arguments should have the form | |
264 | ||
265 | [KEYWORD VALUE]... | |
266 | ||
267 | The following KEYWORDs are defined: | |
268 | ||
269 | :group VALUE should be a customization group. | |
270 | Add FACE to that group. | |
271 | ||
272 | SPEC should be an alist of the form ((DISPLAY ATTS)...). | |
273 | ||
274 | The first element of SPEC where the DISPLAY matches the frame | |
275 | is the one that takes effect in that frame. The ATTRs in this | |
276 | element take effect; the other elements are ignored, on that frame. | |
277 | ||
278 | ATTS is a list of face attributes followed by their values: | |
279 | (ATTR VALUE ATTR VALUE...) | |
280 | ||
281 | The possible attributes are `:family', `:width', `:height', `:weight', | |
282 | `:slant', `:underline', `:overline', `:strike-through', `:box', | |
3d58b15e | 283 | `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'. |
55535639 PJ |
284 | |
285 | DISPLAY can either be the symbol t, which will match all frames, or an | |
286 | alist of the form \((REQ ITEM...)...). For the DISPLAY to match a | |
287 | FRAME, the REQ property of the frame must match one of the ITEM. The | |
288 | following REQ are defined: | |
289 | ||
290 | `type' (the value of `window-system') | |
291 | Under X, in addition to the values `window-system' can take, | |
292 | `motif', `lucid' and `x-toolkit' are allowed, and match when | |
293 | the Motif toolkit, Lucid toolkit, or any X toolkit is in use. | |
294 | ||
295 | `class' (the frame's color support) | |
296 | Should be one of `color', `grayscale', or `mono'. | |
297 | ||
298 | `background' (what color is used for the background text) | |
299 | Should be one of `light' or `dark'. | |
300 | ||
301 | Read the section about customization in the Emacs Lisp manual for more | |
302 | information." | |
303 | ;; It is better not to use backquote in this file, | |
304 | ;; because that makes a bootstrapping problem | |
305 | ;; if you need to recompile all the Lisp files using interpreted code. | |
306 | (nconc (list 'custom-declare-face (list 'quote face) spec doc) args)) | |
307 | ||
308 | ;;; The `defgroup' Macro. | |
309 | ||
d3b80e9b SM |
310 | (defun custom-current-group () |
311 | (cdr (assoc load-file-name custom-current-group-alist))) | |
312 | ||
55535639 PJ |
313 | (defun custom-declare-group (symbol members doc &rest args) |
314 | "Like `defgroup', but SYMBOL is evaluated as a normal argument." | |
315 | (while members | |
316 | (apply 'custom-add-to-group symbol (car members)) | |
317 | (setq members (cdr members))) | |
55535639 PJ |
318 | (when doc |
319 | ;; This text doesn't get into DOC. | |
320 | (put symbol 'group-documentation (purecopy doc))) | |
321 | (while args | |
322 | (let ((arg (car args))) | |
323 | (setq args (cdr args)) | |
324 | (unless (symbolp arg) | |
325 | (error "Junk in args %S" args)) | |
326 | (let ((keyword arg) | |
327 | (value (car args))) | |
328 | (unless args | |
329 | (error "Keyword %s is missing an argument" keyword)) | |
330 | (setq args (cdr args)) | |
331 | (cond ((eq keyword :prefix) | |
332 | (put symbol 'custom-prefix value)) | |
333 | (t | |
334 | (custom-handle-keyword symbol keyword value | |
335 | 'custom-group)))))) | |
d3b80e9b SM |
336 | ;; Record the group on the `current' list. |
337 | (let ((elt (assoc load-file-name custom-current-group-alist))) | |
338 | (if elt (setcdr elt symbol) | |
339 | (push (cons load-file-name symbol) custom-current-group-alist))) | |
55535639 PJ |
340 | (run-hooks 'custom-define-hook) |
341 | symbol) | |
342 | ||
343 | (defmacro defgroup (symbol members doc &rest args) | |
344 | "Declare SYMBOL as a customization group containing MEMBERS. | |
345 | SYMBOL does not need to be quoted. | |
346 | ||
347 | Third arg DOC is the group documentation. | |
348 | ||
349 | MEMBERS should be an alist of the form ((NAME WIDGET)...) where | |
350 | NAME is a symbol and WIDGET is a widget for editing that symbol. | |
351 | Useful widgets are `custom-variable' for editing variables, | |
352 | `custom-face' for edit faces, and `custom-group' for editing groups. | |
353 | ||
354 | The remaining arguments should have the form | |
355 | ||
356 | [KEYWORD VALUE]... | |
357 | ||
358 | The following KEYWORDs are defined: | |
359 | ||
360 | :group VALUE should be a customization group. | |
361 | Add SYMBOL to that group. | |
362 | ||
363 | :version VALUE should be a string specifying that the group was introduced | |
364 | in Emacs version VERSION. | |
365 | ||
366 | Read the section about customization in the Emacs Lisp manual for more | |
367 | information." | |
368 | ;; It is better not to use backquote in this file, | |
369 | ;; because that makes a bootstrapping problem | |
370 | ;; if you need to recompile all the Lisp files using interpreted code. | |
371 | (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args)) | |
372 | ||
373 | (defun custom-add-to-group (group option widget) | |
374 | "To existing GROUP add a new OPTION of type WIDGET. | |
375 | If there already is an entry for OPTION and WIDGET, nothing is done." | |
376 | (let ((members (get group 'custom-group)) | |
377 | (entry (list option widget))) | |
378 | (unless (member entry members) | |
379 | (put group 'custom-group (nconc members (list entry)))))) | |
380 | ||
29512a0f SM |
381 | (defun custom-group-of-mode (mode) |
382 | "Return the custom group corresponding to the major or minor MODE. | |
383 | If no such group is found, return nil." | |
384 | (or (get mode 'custom-mode-group) | |
385 | (if (or (get mode 'custom-group) | |
386 | (and (string-match "-mode\\'" (symbol-name mode)) | |
387 | (get (setq mode (intern (substring (symbol-name mode) | |
388 | 0 (match-beginning 0)))) | |
389 | 'custom-group))) | |
390 | mode))) | |
391 | ||
55535639 PJ |
392 | ;;; Properties. |
393 | ||
394 | (defun custom-handle-all-keywords (symbol args type) | |
395 | "For customization option SYMBOL, handle keyword arguments ARGS. | |
396 | Third argument TYPE is the custom option type." | |
d3b80e9b | 397 | (unless (memq :group args) |
9b6098b9 | 398 | (custom-add-to-group (custom-current-group) symbol type)) |
55535639 PJ |
399 | (while args |
400 | (let ((arg (car args))) | |
401 | (setq args (cdr args)) | |
402 | (unless (symbolp arg) | |
403 | (error "Junk in args %S" args)) | |
404 | (let ((keyword arg) | |
405 | (value (car args))) | |
406 | (unless args | |
407 | (error "Keyword %s is missing an argument" keyword)) | |
408 | (setq args (cdr args)) | |
409 | (custom-handle-keyword symbol keyword value type))))) | |
410 | ||
411 | (defun custom-handle-keyword (symbol keyword value type) | |
412 | "For customization option SYMBOL, handle KEYWORD with VALUE. | |
413 | Fourth argument TYPE is the custom option type." | |
414 | (if purify-flag | |
415 | (setq value (purecopy value))) | |
416 | (cond ((eq keyword :group) | |
417 | (custom-add-to-group value symbol type)) | |
418 | ((eq keyword :version) | |
419 | (custom-add-version symbol value)) | |
420 | ((eq keyword :link) | |
421 | (custom-add-link symbol value)) | |
422 | ((eq keyword :load) | |
423 | (custom-add-load symbol value)) | |
424 | ((eq keyword :tag) | |
425 | (put symbol 'custom-tag value)) | |
426 | ((eq keyword :set-after) | |
427 | (custom-add-dependencies symbol value)) | |
428 | (t | |
429 | (error "Unknown keyword %s" keyword)))) | |
430 | ||
431 | (defun custom-add-dependencies (symbol value) | |
432 | "To the custom option SYMBOL, add dependencies specified by VALUE. | |
433 | VALUE should be a list of symbols. For each symbol in that list, | |
434 | this specifies that SYMBOL should be set after the specified symbol, if | |
435 | both appear in constructs like `custom-set-variables'." | |
436 | (unless (listp value) | |
437 | (error "Invalid custom dependency `%s'" value)) | |
438 | (let* ((deps (get symbol 'custom-dependencies)) | |
439 | (new-deps deps)) | |
440 | (while value | |
441 | (let ((dep (car value))) | |
442 | (unless (symbolp dep) | |
443 | (error "Invalid custom dependency `%s'" dep)) | |
444 | (unless (memq dep new-deps) | |
445 | (setq new-deps (cons dep new-deps))) | |
446 | (setq value (cdr value)))) | |
447 | (unless (eq deps new-deps) | |
448 | (put symbol 'custom-dependencies new-deps)))) | |
449 | ||
450 | (defun custom-add-option (symbol option) | |
451 | "To the variable SYMBOL add OPTION. | |
452 | ||
453 | If SYMBOL is a hook variable, OPTION should be a hook member. | |
454 | For other types variables, the effect is undefined." | |
455 | (let ((options (get symbol 'custom-options))) | |
456 | (unless (member option options) | |
457 | (put symbol 'custom-options (cons option options))))) | |
458 | ||
459 | (defun custom-add-link (symbol widget) | |
460 | "To the custom option SYMBOL add the link WIDGET." | |
461 | (let ((links (get symbol 'custom-links))) | |
462 | (unless (member widget links) | |
463 | (put symbol 'custom-links (cons (purecopy widget) links))))) | |
464 | ||
465 | (defun custom-add-version (symbol version) | |
466 | "To the custom option SYMBOL add the version VERSION." | |
467 | (put symbol 'custom-version (purecopy version))) | |
468 | ||
469 | (defun custom-add-load (symbol load) | |
470 | "To the custom option SYMBOL add the dependency LOAD. | |
471 | LOAD should be either a library file name, or a feature name." | |
472 | (let ((loads (get symbol 'custom-loads))) | |
473 | (unless (member load loads) | |
474 | (put symbol 'custom-loads (cons (purecopy load) loads))))) | |
475 | ||
8f772dfd RS |
476 | ;;; Loading files needed to customize a symbol. |
477 | ;;; This is in custom.el because menu-bar.el needs it for toggle cmds. | |
478 | ||
479 | (defvar custom-load-recursion nil | |
480 | "Hack to avoid recursive dependencies.") | |
481 | ||
482 | (defun custom-load-symbol (symbol) | |
483 | "Load all dependencies for SYMBOL." | |
484 | (unless custom-load-recursion | |
29512a0f SM |
485 | (let ((custom-load-recursion t)) |
486 | (dolist (load (get symbol 'custom-loads)) | |
487 | (cond ((symbolp load) (condition-case nil (require load) (error nil))) | |
488 | ;; This is subsumed by the test below, but it's much faster. | |
8f772dfd RS |
489 | ((assoc load load-history)) |
490 | ;; This was just (assoc (locate-library load) load-history) | |
491 | ;; but has been optimized not to load locate-library | |
492 | ;; if not necessary. | |
29512a0f SM |
493 | ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load) |
494 | "\\(\\'\\|\\.\\)")) | |
495 | (found nil)) | |
8f772dfd | 496 | (dolist (loaded load-history) |
c3891447 RS |
497 | (and (stringp (car loaded)) |
498 | (string-match regexp (car loaded)) | |
8f772dfd RS |
499 | (setq found t))) |
500 | found)) | |
501 | ;; Without this, we would load cus-edit recursively. | |
502 | ;; We are still loading it when we call this, | |
503 | ;; and it is not in load-history yet. | |
504 | ((equal load "cus-edit")) | |
29512a0f | 505 | (t (condition-case nil (load load) (error nil)))))))) |
8f772dfd | 506 | |
55535639 PJ |
507 | ;;; Initializing. |
508 | ||
509 | (defvar custom-local-buffer nil | |
510 | "Non-nil, in a Customization buffer, means customize a specific buffer. | |
511 | If this variable is non-nil, it should be a buffer, | |
512 | and it means customize the local bindings of that buffer. | |
513 | This variable is a permanent local, and it normally has a local binding | |
514 | in every Customization buffer.") | |
515 | (put 'custom-local-buffer 'permanent-local t) | |
516 | ||
517 | (defun custom-set-variables (&rest args) | |
518 | "Initialize variables according to user preferences. | |
519 | ||
520 | The arguments should be a list where each entry has the form: | |
521 | ||
522 | (SYMBOL VALUE [NOW [REQUEST [COMMENT]]]) | |
523 | ||
524 | The unevaluated VALUE is stored as the saved value for SYMBOL. | |
525 | If NOW is present and non-nil, VALUE is also evaluated and bound as | |
526 | the default value for the SYMBOL. | |
527 | REQUEST is a list of features we must require for SYMBOL. | |
528 | COMMENT is a comment string about SYMBOL." | |
529 | (setq args | |
530 | (sort args | |
531 | (lambda (a1 a2) | |
532 | (let* ((sym1 (car a1)) | |
533 | (sym2 (car a2)) | |
534 | (1-then-2 (memq sym1 (get sym2 'custom-dependencies))) | |
535 | (2-then-1 (memq sym2 (get sym1 'custom-dependencies)))) | |
536 | (cond ((and 1-then-2 2-then-1) | |
537 | (error "Circular custom dependency between `%s' and `%s'" | |
538 | sym1 sym2)) | |
539 | (1-then-2 t) | |
cb3f945f GM |
540 | (2-then-1 nil) |
541 | ;; Put symbols with :require last. The macro | |
542 | ;; define-minor-mode generates a defcustom | |
543 | ;; with a :require and a :set, where the | |
544 | ;; setter function calls the mode function. | |
545 | ;; Putting symbols with :require last ensures | |
546 | ;; that the mode function will see other | |
547 | ;; customized values rather than default | |
548 | ;; values. | |
be48584d | 549 | (t (nth 3 a2))))))) |
55535639 PJ |
550 | (while args |
551 | (let ((entry (car args))) | |
552 | (if (listp entry) | |
553 | (let* ((symbol (nth 0 entry)) | |
554 | (value (nth 1 entry)) | |
555 | (now (nth 2 entry)) | |
556 | (requests (nth 3 entry)) | |
557 | (comment (nth 4 entry)) | |
558 | set) | |
559 | (when requests | |
560 | (put symbol 'custom-requests requests) | |
561 | (mapc 'require requests)) | |
562 | (setq set (or (get symbol 'custom-set) 'custom-set-default)) | |
563 | (put symbol 'saved-value (list value)) | |
564 | (put symbol 'saved-variable-comment comment) | |
565 | ;; Allow for errors in the case where the setter has | |
566 | ;; changed between versions, say, but let the user know. | |
567 | (condition-case data | |
568 | (cond (now | |
569 | ;; Rogue variable, set it now. | |
570 | (put symbol 'force-value t) | |
571 | (funcall set symbol (eval value))) | |
572 | ((default-boundp symbol) | |
573 | ;; Something already set this, overwrite it. | |
574 | (funcall set symbol (eval value)))) | |
575 | (error | |
576 | (message "Error setting %s: %s" symbol data))) | |
577 | (setq args (cdr args)) | |
578 | (and (or now (default-boundp symbol)) | |
579 | (put symbol 'variable-comment comment))) | |
580 | ;; Old format, a plist of SYMBOL VALUE pairs. | |
581 | (message "Warning: old format `custom-set-variables'") | |
582 | (ding) | |
583 | (sit-for 2) | |
584 | (let ((symbol (nth 0 args)) | |
585 | (value (nth 1 args))) | |
586 | (put symbol 'saved-value (list value))) | |
587 | (setq args (cdr (cdr args))))))) | |
588 | ||
589 | (defun custom-set-default (variable value) | |
590 | "Default :set function for a customizable variable. | |
591 | Normally, this sets the default value of VARIABLE to VALUE, | |
592 | but if `custom-local-buffer' is non-nil, | |
593 | this sets the local binding in that buffer instead." | |
594 | (if custom-local-buffer | |
595 | (with-current-buffer custom-local-buffer | |
596 | (set variable value)) | |
597 | (set-default variable value))) | |
598 | ||
6d912ee1 MB |
599 | (defun custom-quote (sexp) |
600 | "Quote SEXP iff it is not self quoting." | |
601 | (if (or (memq sexp '(t nil)) | |
602 | (keywordp sexp) | |
603 | (and (listp sexp) | |
604 | (memq (car sexp) '(lambda))) | |
605 | (stringp sexp) | |
606 | (numberp sexp) | |
607 | (vectorp sexp) | |
608 | ;;; (and (fboundp 'characterp) | |
609 | ;;; (characterp sexp)) | |
610 | ) | |
611 | sexp | |
612 | (list 'quote sexp))) | |
613 | ||
614 | (defun customize-mark-to-save (symbol) | |
615 | "Mark SYMBOL for later saving. | |
616 | ||
617 | If the default value of SYMBOL is different from the standard value, | |
618 | set the `saved-value' property to a list whose car evaluates to the | |
619 | default value. Otherwise, set it til nil. | |
620 | ||
621 | To actually save the value, call `custom-save-all'. | |
622 | ||
623 | Return non-nil iff the `saved-value' property actually changed." | |
624 | (let* ((get (or (get symbol 'custom-get) 'default-value)) | |
625 | (value (funcall get symbol)) | |
626 | (saved (get symbol 'saved-value)) | |
627 | (standard (get symbol 'standard-value)) | |
628 | (comment (get symbol 'customized-variable-comment))) | |
629 | ;; Save default value iff different from standard value. | |
630 | (if (or (null standard) | |
631 | (not (equal value (condition-case nil | |
632 | (eval (car standard)) | |
633 | (error nil))))) | |
634 | (put symbol 'saved-value (list (custom-quote value))) | |
635 | (put symbol 'saved-value nil)) | |
636 | ;; Clear customized information (set, but not saved). | |
637 | (put symbol 'customized-value nil) | |
638 | ;; Save any comment that might have been set. | |
639 | (when comment | |
640 | (put symbol 'saved-variable-comment comment)) | |
641 | (not (equal saved (get symbol 'saved-value))))) | |
642 | ||
643 | (defun customize-mark-as-set (symbol) | |
644 | "Mark current value of SYMBOL as being set from customize. | |
645 | ||
646 | If the default value of SYMBOL is different from the saved value if any, | |
647 | or else if it is different from the standard value, set the | |
648 | `customized-value' property to a list whose car evaluates to the | |
649 | default value. Otherwise, set it til nil. | |
650 | ||
651 | Return non-nil iff the `customized-value' property actually changed." | |
652 | (let* ((get (or (get symbol 'custom-get) 'default-value)) | |
653 | (value (funcall get symbol)) | |
654 | (customized (get symbol 'customized-value)) | |
655 | (old (or (get symbol 'saved-value) (get symbol 'standard-value)))) | |
656 | ;; Mark default value as set iff different from old value. | |
657 | (if (or (null old) | |
658 | (not (equal value (condition-case nil | |
659 | (eval (car old)) | |
660 | (error nil))))) | |
661 | (put symbol 'customized-value (list (custom-quote value))) | |
662 | (put symbol 'customized-value nil)) | |
663 | ;; Changed? | |
664 | (not (equal customized (get symbol 'customized-value))))) | |
665 | ||
55535639 PJ |
666 | ;;; The End. |
667 | ||
668 | ;; Process the defcustoms for variables loaded before this file. | |
669 | (while custom-declare-variable-list | |
670 | (apply 'custom-declare-variable (car custom-declare-variable-list)) | |
671 | (setq custom-declare-variable-list (cdr custom-declare-variable-list))) | |
672 | ||
673 | (provide 'custom) | |
674 | ||
675 | ;;; custom.el ends here |