Commit | Line | Data |
---|---|---|
55535639 PJ |
1 | ;;; custom.el --- tools for declaring and initializing options |
2 | ;; | |
0d30b337 | 3 | ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2003, 2004, |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
55535639 PJ |
5 | ;; |
6 | ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> | |
7 | ;; Maintainer: FSF | |
8 | ;; Keywords: help, faces | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
55535639 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
55535639 PJ |
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 | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
55535639 PJ |
24 | |
25 | ;;; Commentary: | |
26 | ;; | |
e6bb31f3 | 27 | ;; This file only contains the code needed to declare and initialize |
55535639 PJ |
28 | ;; user options. The code to customize options is autoloaded from |
29 | ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual. | |
30 | ||
e6bb31f3 | 31 | ;; The code implementing face declarations is in `cus-face.el'. |
55535639 PJ |
32 | |
33 | ;;; Code: | |
34 | ||
35 | (require 'widget) | |
36 | ||
37 | (defvar custom-define-hook nil | |
38 | ;; Customize information for this option is in `cus-edit.el'. | |
39 | "Hook called after defining each customize option.") | |
40 | ||
6da43544 RS |
41 | (defvar custom-dont-initialize nil |
42 | "Non-nil means `defcustom' should not initialize the variable. | |
43 | That is used for the sake of `custom-make-dependencies'. | |
44 | Users should not set it.") | |
45 | ||
d3b80e9b SM |
46 | (defvar custom-current-group-alist nil |
47 | "Alist of (FILE . GROUP) indicating the current group to use for FILE.") | |
48 | ||
55535639 PJ |
49 | ;;; The `defcustom' Macro. |
50 | ||
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 | |
494bcd27 | 55 | the car of that and use it as the default binding for symbol. |
55535639 PJ |
56 | Otherwise, VALUE will be evaluated and used as the default binding for |
57 | symbol." | |
58 | (unless (default-boundp symbol) | |
59 | ;; Use the saved value if it exists, otherwise the standard setting. | |
b3c7c12d SM |
60 | (set-default symbol (eval (if (get symbol 'saved-value) |
61 | (car (get symbol 'saved-value)) | |
62 | value))))) | |
55535639 PJ |
63 | |
64 | (defun custom-initialize-set (symbol value) | |
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." | |
70 | (unless (default-boundp symbol) | |
71 | (funcall (or (get symbol 'custom-set) 'set-default) | |
72 | symbol | |
b3c7c12d SM |
73 | (eval (if (get symbol 'saved-value) |
74 | (car (get symbol 'saved-value)) | |
75 | value))))) | |
55535639 PJ |
76 | |
77 | (defun custom-initialize-reset (symbol value) | |
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." | |
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 | |
97 | not using the standard setting. | |
98 | For the standard setting, use `set-default'." | |
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 | ||
790d0270 SM |
111 | (defvar custom-delayed-init-variables nil |
112 | "List of variables whose initialization is pending.") | |
113 | ||
114 | (defun custom-initialize-delay (symbol value) | |
115 | "Delay initialization of SYMBOL to the next Emacs start. | |
116 | This is used in files that are preloaded, so that the initialization is | |
117 | done in the run-time context rather than the build-time context. | |
118 | This also has the side-effect that the (delayed) initialization is performed | |
119 | with the :setter." | |
adba8116 SM |
120 | ;; Until the var is actually initialized, it is kept unbound. |
121 | ;; This seemed to be at least as good as setting it to an arbitrary | |
122 | ;; value like nil (evaluating `value' is not an option because it | |
123 | ;; may have undesirable side-effects). | |
790d0270 SM |
124 | (push symbol custom-delayed-init-variables)) |
125 | ||
55535639 PJ |
126 | (defun custom-declare-variable (symbol default doc &rest args) |
127 | "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments. | |
128 | DEFAULT should be an expression to evaluate to compute the default value, | |
e1bab181 RS |
129 | not the default value itself. |
130 | ||
8989771d | 131 | DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property |
e1bab181 RS |
132 | `standard-value'. At the same time, SYMBOL's property `force-value' is |
133 | set to nil, as the value is no longer rogue." | |
b6f8ba09 | 134 | (put symbol 'standard-value (purecopy (list default))) |
55535639 PJ |
135 | ;; Maybe this option was rogue in an earlier version. It no longer is. |
136 | (when (get symbol 'force-value) | |
137 | (put symbol 'force-value nil)) | |
138 | (when doc | |
139 | (put symbol 'variable-documentation doc)) | |
140 | (let ((initialize 'custom-initialize-reset) | |
141 | (requests nil)) | |
d3b80e9b SM |
142 | (unless (memq :group args) |
143 | (custom-add-to-group (custom-current-group) symbol 'custom-variable)) | |
55535639 PJ |
144 | (while args |
145 | (let ((arg (car args))) | |
146 | (setq args (cdr args)) | |
147 | (unless (symbolp arg) | |
148 | (error "Junk in args %S" args)) | |
149 | (let ((keyword arg) | |
150 | (value (car args))) | |
151 | (unless args | |
152 | (error "Keyword %s is missing an argument" keyword)) | |
153 | (setq args (cdr args)) | |
154 | (cond ((eq keyword :initialize) | |
155 | (setq initialize value)) | |
156 | ((eq keyword :set) | |
157 | (put symbol 'custom-set value)) | |
158 | ((eq keyword :get) | |
159 | (put symbol 'custom-get value)) | |
160 | ((eq keyword :require) | |
6f0d61bf | 161 | (push value requests)) |
fd045a34 GM |
162 | ((eq keyword :risky) |
163 | (put symbol 'risky-local-variable value)) | |
164 | ((eq keyword :safe) | |
165 | (put symbol 'safe-local-variable value)) | |
55535639 PJ |
166 | ((eq keyword :type) |
167 | (put symbol 'custom-type (purecopy value))) | |
168 | ((eq keyword :options) | |
169 | (if (get symbol 'custom-options) | |
170 | ;; Slow safe code to avoid duplicates. | |
171 | (mapc (lambda (option) | |
172 | (custom-add-option symbol option)) | |
6f0d61bf | 173 | value) |
55535639 PJ |
174 | ;; Fast code for the common case. |
175 | (put symbol 'custom-options (copy-sequence value)))) | |
176 | (t | |
177 | (custom-handle-keyword symbol keyword value | |
178 | 'custom-variable)))))) | |
179 | (put symbol 'custom-requests requests) | |
180 | ;; Do the actual initialization. | |
6da43544 RS |
181 | (unless custom-dont-initialize |
182 | (funcall initialize symbol default))) | |
80888260 | 183 | (push symbol current-load-list) |
55535639 PJ |
184 | (run-hooks 'custom-define-hook) |
185 | symbol) | |
186 | ||
187 | (defmacro defcustom (symbol value doc &rest args) | |
188 | "Declare SYMBOL as a customizable variable that defaults to VALUE. | |
189 | DOC is the variable documentation. | |
190 | ||
494bcd27 | 191 | Neither SYMBOL nor VALUE need to be quoted. |
55535639 PJ |
192 | If SYMBOL is not already bound, initialize it to VALUE. |
193 | The remaining arguments should have the form | |
194 | ||
195 | [KEYWORD VALUE]... | |
196 | ||
197 | The following keywords are meaningful: | |
198 | ||
693d0bd3 | 199 | :type VALUE should be a widget type for editing the symbol's value. |
55535639 | 200 | :options VALUE should be a list of valid members of the widget type. |
81117bdd BW |
201 | :initialize |
202 | VALUE should be a function used to initialize the | |
203 | variable. It takes two arguments, the symbol and value | |
204 | given in the `defcustom' call. The default is | |
205 | `custom-initialize-reset'. | |
206 | :set VALUE should be a function to set the value of the symbol. | |
207 | It takes two arguments, the symbol to set and the value to | |
f2276b69 | 208 | give it. The default choice of function is `set-default'. |
81117bdd BW |
209 | :get VALUE should be a function to extract the value of symbol. |
210 | The function takes one argument, a symbol, and should return | |
211 | the current value for that symbol. The default choice of function | |
f2276b69 | 212 | is `default-value'. |
81117bdd BW |
213 | :require |
214 | VALUE should be a feature symbol. If you save a value | |
215 | for this option, then when your `.emacs' file loads the value, | |
216 | it does (require VALUE) first. | |
fd045a34 GM |
217 | :risky Set SYMBOL's `risky-local-variable' property to VALUE. |
218 | :safe Set SYMBOL's `safe-local-variable' property to VALUE. | |
81117bdd BW |
219 | |
220 | The following common keywords are also meaningful. | |
221 | ||
55535639 | 222 | :group VALUE should be a customization group. |
81117bdd | 223 | Add SYMBOL (or FACE with `defface') to that group. |
3dc5f18e | 224 | :link LINK-DATA |
00644b82 EZ |
225 | Include an external link after the documentation string for this |
226 | item. This is a sentence containing an active field which | |
227 | references some other documentation. | |
71296446 | 228 | |
11ee7d4e | 229 | There are several alternatives you can use for LINK-DATA: |
71296446 | 230 | |
3dc5f18e | 231 | (custom-manual INFO-NODE) |
00644b82 | 232 | Link to an Info node; INFO-NODE is a string which specifies |
11ee7d4e | 233 | the node name, as in \"(emacs)Top\". |
71296446 | 234 | |
3dc5f18e | 235 | (info-link INFO-NODE) |
00644b82 EZ |
236 | Like `custom-manual' except that the link appears in the |
237 | customization buffer with the Info node name. | |
71296446 | 238 | |
3dc5f18e | 239 | (url-link URL) |
00644b82 | 240 | Link to a web page; URL is a string which specifies the URL. |
11ee7d4e JL |
241 | |
242 | (emacs-commentary-link LIBRARY) | |
243 | Link to the commentary section of LIBRARY. | |
244 | ||
245 | (emacs-library-link LIBRARY) | |
246 | Link to an Emacs Lisp LIBRARY file. | |
247 | ||
248 | (file-link FILE) | |
249 | Link to FILE. | |
250 | ||
251 | (function-link FUNCTION) | |
252 | Link to the documentation of FUNCTION. | |
253 | ||
254 | (variable-link VARIABLE) | |
255 | Link to the documentation of VARIABLE. | |
256 | ||
257 | (custom-group-link GROUP) | |
258 | Link to another customization GROUP. | |
71296446 | 259 | |
00644b82 EZ |
260 | You can specify the text to use in the customization buffer by |
261 | adding `:tag NAME' after the first element of the LINK-DATA; for | |
3dc5f18e | 262 | example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the |
00644b82 | 263 | Emacs manual which appears in the buffer as `foo'. |
71296446 | 264 | |
00644b82 EZ |
265 | An item can have more than one external link; however, most items |
266 | have none at all. | |
55535639 PJ |
267 | :version |
268 | VALUE should be a string specifying that the variable was | |
269 | first introduced, or its default value was changed, in Emacs | |
270 | version VERSION. | |
4e9c705e | 271 | :package-version |
994019df | 272 | VALUE should be a list with the form (PACKAGE . VERSION) |
4e9c705e BW |
273 | specifying that the variable was first introduced, or its |
274 | default value was changed, in PACKAGE version VERSION. This | |
275 | keyword takes priority over :version. The PACKAGE and VERSION | |
276 | must appear in the alist `customize-package-emacs-version-alist'. | |
994019df BW |
277 | Since PACKAGE must be unique and the user might see it in an |
278 | error message, a good choice is the official name of the | |
279 | package, such as MH-E or Gnus. | |
00644b82 EZ |
280 | :tag LABEL |
281 | Use LABEL, a string, instead of the item's name, to label the item | |
1c1da418 | 282 | in customization menus and buffers. |
00644b82 EZ |
283 | :load FILE |
284 | Load file FILE (a string) before displaying this customization | |
29512a0f | 285 | item. Loading is done with `load', and only if the file is |
00644b82 | 286 | not already loaded. |
64e9f9d9 DL |
287 | :set-after VARIABLES |
288 | Specifies that SYMBOL should be set after the list of variables | |
289 | VARIABLES when both have been customized. | |
55535639 | 290 | |
19229e5e LT |
291 | If SYMBOL has a local binding, then this form affects the local |
292 | binding. This is normally not what you want. Thus, if you need | |
293 | to load a file defining variables with this form, or with | |
294 | `defvar' or `defconst', you should always load that file | |
295 | _outside_ any bindings for these variables. \(`defvar' and | |
296 | `defconst' behave similarly in this respect.) | |
297 | ||
81117bdd BW |
298 | See Info node `(elisp) Customization' in the Emacs Lisp manual |
299 | for more information." | |
67a60caa | 300 | (declare (doc-string 3)) |
55535639 PJ |
301 | ;; It is better not to use backquote in this file, |
302 | ;; because that makes a bootstrapping problem | |
303 | ;; if you need to recompile all the Lisp files using interpreted code. | |
304 | (nconc (list 'custom-declare-variable | |
305 | (list 'quote symbol) | |
306 | (list 'quote value) | |
307 | doc) | |
308 | args)) | |
309 | ||
310 | ;;; The `defface' Macro. | |
311 | ||
312 | (defmacro defface (face spec doc &rest args) | |
313 | "Declare FACE as a customizable face that defaults to SPEC. | |
314 | FACE does not need to be quoted. | |
315 | ||
316 | Third argument DOC is the face documentation. | |
317 | ||
2a69edce | 318 | If FACE has been set with `custom-set-faces', set the face attributes |
55535639 PJ |
319 | as specified by that function, otherwise set the face attributes |
320 | according to SPEC. | |
321 | ||
322 | The remaining arguments should have the form | |
323 | ||
324 | [KEYWORD VALUE]... | |
325 | ||
81117bdd BW |
326 | For a list of valid keywords, see the common keywords listed in |
327 | `defcustom'. | |
55535639 PJ |
328 | |
329 | SPEC should be an alist of the form ((DISPLAY ATTS)...). | |
330 | ||
ebdaed17 | 331 | In the first element, DISPLAY can be `default'. The ATTS in that |
9c27debd RS |
332 | element then act as defaults for all the following elements. |
333 | ||
334 | Aside from that, DISPLAY specifies conditions to match some or | |
335 | all frames. For each frame, the first element of SPEC where the | |
336 | DISPLAY conditions are satisfied is the one that applies to that | |
337 | frame. The ATTRs in this element take effect, and the following | |
338 | elements are ignored, on that frame. | |
339 | ||
340 | In the last element, DISPLAY can be t. That element applies to a | |
ebdaed17 | 341 | frame if none of the previous elements (except the `default' if |
9c27debd | 342 | any) did. |
55535639 PJ |
343 | |
344 | ATTS is a list of face attributes followed by their values: | |
345 | (ATTR VALUE ATTR VALUE...) | |
346 | ||
347 | The possible attributes are `:family', `:width', `:height', `:weight', | |
348 | `:slant', `:underline', `:overline', `:strike-through', `:box', | |
3d58b15e | 349 | `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'. |
55535639 | 350 | |
ebdaed17 | 351 | DISPLAY can be `default' (only in the first element), the symbol |
9c27debd RS |
352 | t (only in the last element) to match all frames, or an alist of |
353 | conditions of the form \(REQ ITEM...). For such an alist to | |
354 | match a frame, each of the conditions must be satisfied, meaning | |
355 | that the REQ property of the frame must match one of the | |
356 | corresponding ITEMs. These are the defined REQ values: | |
55535639 PJ |
357 | |
358 | `type' (the value of `window-system') | |
359 | Under X, in addition to the values `window-system' can take, | |
9b44aa8b JD |
360 | `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when |
361 | the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use. | |
55535639 PJ |
362 | |
363 | `class' (the frame's color support) | |
364 | Should be one of `color', `grayscale', or `mono'. | |
365 | ||
366 | `background' (what color is used for the background text) | |
367 | Should be one of `light' or `dark'. | |
368 | ||
6e166990 EZ |
369 | `min-colors' (the minimum number of colors the frame should support) |
370 | Should be an integer, it is compared with the result of | |
371 | `display-color-cells'. | |
372 | ||
ec85856c JL |
373 | `supports' (only match frames that support the specified face attributes) |
374 | Should be a list of face attributes. See the documentation for | |
375 | the function `display-supports-face-attributes-p' for more | |
376 | information on exactly how testing is done. | |
377 | ||
81117bdd BW |
378 | See Info node `(elisp) Customization' in the Emacs Lisp manual |
379 | for more information." | |
67a60caa | 380 | (declare (doc-string 3)) |
55535639 PJ |
381 | ;; It is better not to use backquote in this file, |
382 | ;; because that makes a bootstrapping problem | |
383 | ;; if you need to recompile all the Lisp files using interpreted code. | |
384 | (nconc (list 'custom-declare-face (list 'quote face) spec doc) args)) | |
385 | ||
386 | ;;; The `defgroup' Macro. | |
387 | ||
d3b80e9b SM |
388 | (defun custom-current-group () |
389 | (cdr (assoc load-file-name custom-current-group-alist))) | |
390 | ||
55535639 PJ |
391 | (defun custom-declare-group (symbol members doc &rest args) |
392 | "Like `defgroup', but SYMBOL is evaluated as a normal argument." | |
393 | (while members | |
394 | (apply 'custom-add-to-group symbol (car members)) | |
395 | (setq members (cdr members))) | |
55535639 PJ |
396 | (when doc |
397 | ;; This text doesn't get into DOC. | |
398 | (put symbol 'group-documentation (purecopy doc))) | |
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 | (cond ((eq keyword :prefix) | |
b6f8ba09 | 410 | (put symbol 'custom-prefix (purecopy value))) |
55535639 PJ |
411 | (t |
412 | (custom-handle-keyword symbol keyword value | |
413 | 'custom-group)))))) | |
d3b80e9b SM |
414 | ;; Record the group on the `current' list. |
415 | (let ((elt (assoc load-file-name custom-current-group-alist))) | |
416 | (if elt (setcdr elt symbol) | |
af89cf77 DN |
417 | (push (cons (purecopy load-file-name) symbol) |
418 | custom-current-group-alist))) | |
55535639 PJ |
419 | (run-hooks 'custom-define-hook) |
420 | symbol) | |
421 | ||
422 | (defmacro defgroup (symbol members doc &rest args) | |
423 | "Declare SYMBOL as a customization group containing MEMBERS. | |
424 | SYMBOL does not need to be quoted. | |
425 | ||
7a41cd7f GM |
426 | Third argument DOC is the group documentation. This should be a short |
427 | description of the group, beginning with a capital and ending with | |
428 | a period. Words other than the first should not be capitalized, if they | |
429 | are not usually written so. | |
55535639 PJ |
430 | |
431 | MEMBERS should be an alist of the form ((NAME WIDGET)...) where | |
432 | NAME is a symbol and WIDGET is a widget for editing that symbol. | |
433 | Useful widgets are `custom-variable' for editing variables, | |
434 | `custom-face' for edit faces, and `custom-group' for editing groups. | |
435 | ||
436 | The remaining arguments should have the form | |
437 | ||
438 | [KEYWORD VALUE]... | |
439 | ||
81117bdd BW |
440 | For a list of valid keywords, see the common keywords listed in |
441 | `defcustom'. | |
55535639 | 442 | |
81117bdd BW |
443 | See Info node `(elisp) Customization' in the Emacs Lisp manual |
444 | for more information." | |
45aacac6 | 445 | (declare (doc-string 3)) |
55535639 PJ |
446 | ;; It is better not to use backquote in this file, |
447 | ;; because that makes a bootstrapping problem | |
448 | ;; if you need to recompile all the Lisp files using interpreted code. | |
449 | (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args)) | |
450 | ||
451 | (defun custom-add-to-group (group option widget) | |
452 | "To existing GROUP add a new OPTION of type WIDGET. | |
453 | If there already is an entry for OPTION and WIDGET, nothing is done." | |
454 | (let ((members (get group 'custom-group)) | |
455 | (entry (list option widget))) | |
456 | (unless (member entry members) | |
457 | (put group 'custom-group (nconc members (list entry)))))) | |
458 | ||
29512a0f SM |
459 | (defun custom-group-of-mode (mode) |
460 | "Return the custom group corresponding to the major or minor MODE. | |
461 | If no such group is found, return nil." | |
462 | (or (get mode 'custom-mode-group) | |
463 | (if (or (get mode 'custom-group) | |
464 | (and (string-match "-mode\\'" (symbol-name mode)) | |
465 | (get (setq mode (intern (substring (symbol-name mode) | |
466 | 0 (match-beginning 0)))) | |
467 | 'custom-group))) | |
468 | mode))) | |
469 | ||
55535639 PJ |
470 | ;;; Properties. |
471 | ||
472 | (defun custom-handle-all-keywords (symbol args type) | |
473 | "For customization option SYMBOL, handle keyword arguments ARGS. | |
474 | Third argument TYPE is the custom option type." | |
d3b80e9b | 475 | (unless (memq :group args) |
9b6098b9 | 476 | (custom-add-to-group (custom-current-group) symbol type)) |
55535639 PJ |
477 | (while args |
478 | (let ((arg (car args))) | |
479 | (setq args (cdr args)) | |
480 | (unless (symbolp arg) | |
481 | (error "Junk in args %S" args)) | |
482 | (let ((keyword arg) | |
483 | (value (car args))) | |
484 | (unless args | |
485 | (error "Keyword %s is missing an argument" keyword)) | |
486 | (setq args (cdr args)) | |
487 | (custom-handle-keyword symbol keyword value type))))) | |
488 | ||
489 | (defun custom-handle-keyword (symbol keyword value type) | |
490 | "For customization option SYMBOL, handle KEYWORD with VALUE. | |
491 | Fourth argument TYPE is the custom option type." | |
492 | (if purify-flag | |
493 | (setq value (purecopy value))) | |
494 | (cond ((eq keyword :group) | |
495 | (custom-add-to-group value symbol type)) | |
496 | ((eq keyword :version) | |
497 | (custom-add-version symbol value)) | |
4e9c705e BW |
498 | ((eq keyword :package-version) |
499 | (custom-add-package-version symbol value)) | |
55535639 PJ |
500 | ((eq keyword :link) |
501 | (custom-add-link symbol value)) | |
502 | ((eq keyword :load) | |
503 | (custom-add-load symbol value)) | |
504 | ((eq keyword :tag) | |
505 | (put symbol 'custom-tag value)) | |
506 | ((eq keyword :set-after) | |
507 | (custom-add-dependencies symbol value)) | |
508 | (t | |
509 | (error "Unknown keyword %s" keyword)))) | |
510 | ||
511 | (defun custom-add-dependencies (symbol value) | |
512 | "To the custom option SYMBOL, add dependencies specified by VALUE. | |
513 | VALUE should be a list of symbols. For each symbol in that list, | |
514 | this specifies that SYMBOL should be set after the specified symbol, if | |
515 | both appear in constructs like `custom-set-variables'." | |
516 | (unless (listp value) | |
517 | (error "Invalid custom dependency `%s'" value)) | |
518 | (let* ((deps (get symbol 'custom-dependencies)) | |
519 | (new-deps deps)) | |
520 | (while value | |
521 | (let ((dep (car value))) | |
522 | (unless (symbolp dep) | |
523 | (error "Invalid custom dependency `%s'" dep)) | |
524 | (unless (memq dep new-deps) | |
525 | (setq new-deps (cons dep new-deps))) | |
526 | (setq value (cdr value)))) | |
527 | (unless (eq deps new-deps) | |
528 | (put symbol 'custom-dependencies new-deps)))) | |
e1bab181 | 529 | |
55535639 PJ |
530 | (defun custom-add-option (symbol option) |
531 | "To the variable SYMBOL add OPTION. | |
532 | ||
a4842ffa RS |
533 | If SYMBOL's custom type is a hook, OPTION should be a hook member. |
534 | If SYMBOL's custom type is an alist, OPTION specifies a symbol | |
535 | to offer to the user as a possible key in the alist. | |
536 | For other custom types, this has no effect." | |
55535639 PJ |
537 | (let ((options (get symbol 'custom-options))) |
538 | (unless (member option options) | |
539 | (put symbol 'custom-options (cons option options))))) | |
467064a4 | 540 | (defalias 'custom-add-frequent-value 'custom-add-option) |
55535639 PJ |
541 | |
542 | (defun custom-add-link (symbol widget) | |
543 | "To the custom option SYMBOL add the link WIDGET." | |
544 | (let ((links (get symbol 'custom-links))) | |
545 | (unless (member widget links) | |
546 | (put symbol 'custom-links (cons (purecopy widget) links))))) | |
547 | ||
548 | (defun custom-add-version (symbol version) | |
549 | "To the custom option SYMBOL add the version VERSION." | |
550 | (put symbol 'custom-version (purecopy version))) | |
551 | ||
4e9c705e BW |
552 | (defun custom-add-package-version (symbol version) |
553 | "To the custom option SYMBOL add the package version VERSION." | |
554 | (put symbol 'custom-package-version (purecopy version))) | |
555 | ||
55535639 PJ |
556 | (defun custom-add-load (symbol load) |
557 | "To the custom option SYMBOL add the dependency LOAD. | |
558 | LOAD should be either a library file name, or a feature name." | |
559 | (let ((loads (get symbol 'custom-loads))) | |
560 | (unless (member load loads) | |
561 | (put symbol 'custom-loads (cons (purecopy load) loads))))) | |
562 | ||
d54fbdfd SM |
563 | (defun custom-autoload (symbol load &optional noset) |
564 | "Mark SYMBOL as autoloaded custom variable and add dependency LOAD. | |
565 | If NOSET is non-nil, don't bother autoloading LOAD when setting the variable." | |
566 | (put symbol 'custom-autoload (if noset 'noset t)) | |
1669290d MR |
567 | (custom-add-load symbol load)) |
568 | ||
569 | ;; This test is also in the C code of `user-variable-p'. | |
570 | (defun custom-variable-p (variable) | |
289e1999 LT |
571 | "Return non-nil if VARIABLE is a custom variable. |
572 | This recursively follows aliases." | |
573 | (setq variable (indirect-variable variable)) | |
1669290d MR |
574 | (or (get variable 'standard-value) |
575 | (get variable 'custom-autoload))) | |
576 | ||
400b960e RS |
577 | (defun custom-note-var-changed (variable) |
578 | "Inform Custom that VARIABLE has been set (changed). | |
579 | VARIABLE is a symbol that names a user option. | |
580 | The result is that the change is treated as having been made through Custom." | |
400b960e | 581 | (put variable 'customized-value (list (custom-quote (eval variable))))) |
06f5c483 JL |
582 | |
583 | \f | |
584 | ;;; Custom Themes | |
400b960e | 585 | |
8f772dfd RS |
586 | ;;; Loading files needed to customize a symbol. |
587 | ;;; This is in custom.el because menu-bar.el needs it for toggle cmds. | |
588 | ||
589 | (defvar custom-load-recursion nil | |
590 | "Hack to avoid recursive dependencies.") | |
591 | ||
592 | (defun custom-load-symbol (symbol) | |
593 | "Load all dependencies for SYMBOL." | |
594 | (unless custom-load-recursion | |
29512a0f | 595 | (let ((custom-load-recursion t)) |
a8c78057 RS |
596 | ;; Load these files if not already done, |
597 | ;; to make sure we know all the dependencies of SYMBOL. | |
598 | (condition-case nil | |
599 | (require 'cus-load) | |
600 | (error nil)) | |
601 | (condition-case nil | |
602 | (require 'cus-start) | |
603 | (error nil)) | |
29512a0f SM |
604 | (dolist (load (get symbol 'custom-loads)) |
605 | (cond ((symbolp load) (condition-case nil (require load) (error nil))) | |
606 | ;; This is subsumed by the test below, but it's much faster. | |
8f772dfd RS |
607 | ((assoc load load-history)) |
608 | ;; This was just (assoc (locate-library load) load-history) | |
609 | ;; but has been optimized not to load locate-library | |
610 | ;; if not necessary. | |
29512a0f SM |
611 | ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load) |
612 | "\\(\\'\\|\\.\\)")) | |
613 | (found nil)) | |
8f772dfd | 614 | (dolist (loaded load-history) |
c3891447 RS |
615 | (and (stringp (car loaded)) |
616 | (string-match regexp (car loaded)) | |
8f772dfd RS |
617 | (setq found t))) |
618 | found)) | |
619 | ;; Without this, we would load cus-edit recursively. | |
620 | ;; We are still loading it when we call this, | |
621 | ;; and it is not in load-history yet. | |
622 | ((equal load "cus-edit")) | |
29512a0f | 623 | (t (condition-case nil (load load) (error nil)))))))) |
46ce5feb | 624 | \f |
c2e2f9be CY |
625 | (defvar custom-local-buffer nil |
626 | "Non-nil, in a Customization buffer, means customize a specific buffer. | |
627 | If this variable is non-nil, it should be a buffer, | |
628 | and it means customize the local bindings of that buffer. | |
629 | This variable is a permanent local, and it normally has a local binding | |
630 | in every Customization buffer.") | |
631 | (put 'custom-local-buffer 'permanent-local t) | |
632 | ||
633 | (defun custom-set-default (variable value) | |
634 | "Default :set function for a customizable variable. | |
635 | Normally, this sets the default value of VARIABLE to VALUE, | |
636 | but if `custom-local-buffer' is non-nil, | |
637 | this sets the local binding in that buffer instead." | |
638 | (if custom-local-buffer | |
639 | (with-current-buffer custom-local-buffer | |
640 | (set variable value)) | |
641 | (set-default variable value))) | |
642 | ||
643 | (defun custom-set-minor-mode (variable value) | |
644 | ":set function for minor mode variables. | |
645 | Normally, this sets the default value of VARIABLE to nil if VALUE | |
646 | is nil and to t otherwise, | |
647 | but if `custom-local-buffer' is non-nil, | |
648 | this sets the local binding in that buffer instead." | |
649 | (if custom-local-buffer | |
650 | (with-current-buffer custom-local-buffer | |
651 | (funcall variable (if value 1 0))) | |
652 | (funcall variable (if value 1 0)))) | |
653 | ||
654 | (defun custom-quote (sexp) | |
4837b516 | 655 | "Quote SEXP if it is not self quoting." |
c2e2f9be CY |
656 | (if (or (memq sexp '(t nil)) |
657 | (keywordp sexp) | |
658 | (and (listp sexp) | |
659 | (memq (car sexp) '(lambda))) | |
660 | (stringp sexp) | |
661 | (numberp sexp) | |
662 | (vectorp sexp) | |
663 | ;;; (and (fboundp 'characterp) | |
664 | ;;; (characterp sexp)) | |
665 | ) | |
666 | sexp | |
667 | (list 'quote sexp))) | |
668 | ||
669 | (defun customize-mark-to-save (symbol) | |
670 | "Mark SYMBOL for later saving. | |
671 | ||
672 | If the default value of SYMBOL is different from the standard value, | |
673 | set the `saved-value' property to a list whose car evaluates to the | |
674 | default value. Otherwise, set it to nil. | |
675 | ||
676 | To actually save the value, call `custom-save-all'. | |
677 | ||
4837b516 | 678 | Return non-nil if the `saved-value' property actually changed." |
ec9f0a62 | 679 | (custom-load-symbol symbol) |
c2e2f9be CY |
680 | (let* ((get (or (get symbol 'custom-get) 'default-value)) |
681 | (value (funcall get symbol)) | |
682 | (saved (get symbol 'saved-value)) | |
683 | (standard (get symbol 'standard-value)) | |
684 | (comment (get symbol 'customized-variable-comment))) | |
4837b516 | 685 | ;; Save default value if different from standard value. |
c2e2f9be CY |
686 | (if (or (null standard) |
687 | (not (equal value (condition-case nil | |
688 | (eval (car standard)) | |
689 | (error nil))))) | |
690 | (put symbol 'saved-value (list (custom-quote value))) | |
691 | (put symbol 'saved-value nil)) | |
692 | ;; Clear customized information (set, but not saved). | |
693 | (put symbol 'customized-value nil) | |
694 | ;; Save any comment that might have been set. | |
695 | (when comment | |
696 | (put symbol 'saved-variable-comment comment)) | |
697 | (not (equal saved (get symbol 'saved-value))))) | |
698 | ||
699 | (defun customize-mark-as-set (symbol) | |
700 | "Mark current value of SYMBOL as being set from customize. | |
701 | ||
702 | If the default value of SYMBOL is different from the saved value if any, | |
703 | or else if it is different from the standard value, set the | |
704 | `customized-value' property to a list whose car evaluates to the | |
705 | default value. Otherwise, set it to nil. | |
706 | ||
4837b516 | 707 | Return non-nil if the `customized-value' property actually changed." |
ec9f0a62 | 708 | (custom-load-symbol symbol) |
c2e2f9be CY |
709 | (let* ((get (or (get symbol 'custom-get) 'default-value)) |
710 | (value (funcall get symbol)) | |
711 | (customized (get symbol 'customized-value)) | |
712 | (old (or (get symbol 'saved-value) (get symbol 'standard-value)))) | |
4837b516 | 713 | ;; Mark default value as set if different from old value. |
d54fbdfd SM |
714 | (if (not (and old |
715 | (equal value (condition-case nil | |
716 | (eval (car old)) | |
717 | (error nil))))) | |
fccf2784 CY |
718 | (progn (put symbol 'customized-value (list (custom-quote value))) |
719 | (custom-push-theme 'theme-value symbol 'user 'set | |
720 | (custom-quote value))) | |
c2e2f9be CY |
721 | (put symbol 'customized-value nil)) |
722 | ;; Changed? | |
723 | (not (equal customized (get symbol 'customized-value))))) | |
724 | ||
725 | (defun custom-reevaluate-setting (symbol) | |
726 | "Reset the value of SYMBOL by re-evaluating its saved or standard value. | |
727 | Use the :set function to do so. This is useful for customizable options | |
728 | that are defined before their standard value can really be computed. | |
729 | E.g. dumped variables whose default depends on run-time information." | |
730 | (funcall (or (get symbol 'custom-set) 'set-default) | |
731 | symbol | |
732 | (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value)))))) | |
733 | ||
734 | \f | |
d358aa10 CY |
735 | ;;; Custom Themes |
736 | ||
737 | ;; Custom themes are collections of settings that can be enabled or | |
738 | ;; disabled as a unit. | |
739 | ||
740 | ;; Each Custom theme is defined by a symbol, called the theme name. | |
741 | ;; The `theme-settings' property of the theme name records the | |
742 | ;; variable and face settings of the theme. This property is a list | |
743 | ;; of elements, each of the form | |
744 | ;; | |
745 | ;; (PROP SYMBOL THEME VALUE) | |
746 | ;; | |
747 | ;; - PROP is either `theme-value' or `theme-face' | |
748 | ;; - SYMBOL is the face or variable name | |
749 | ;; - THEME is the theme name (redundant, but simplifies the code) | |
750 | ;; - VALUE is an expression that gives the theme's setting for SYMBOL. | |
751 | ;; | |
752 | ;; The theme name also has a `theme-feature' property, whose value is | |
753 | ;; specified when the theme is defined (see `custom-declare-theme'). | |
754 | ;; Usually, this is just a symbol named THEME-theme. This lets | |
755 | ;; external libraries call (require 'foo-theme). | |
756 | ||
757 | ;; In addition, each symbol (either a variable or a face) affected by | |
758 | ;; an *enabled* theme has a `theme-value' or `theme-face' property, | |
759 | ;; which is a list of elements each of the form | |
760 | ;; | |
761 | ;; (THEME VALUE) | |
762 | ;; | |
763 | ;; which have the same meanings as in `theme-settings'. | |
764 | ;; | |
765 | ;; The `theme-value' and `theme-face' lists are ordered by decreasing | |
766 | ;; theme precedence. Thus, the first element is always the one that | |
767 | ;; is in effect. | |
768 | ||
769 | ;; Each theme is stored in a theme file, with filename THEME-theme.el. | |
770 | ;; Loading a theme basically involves calling (load "THEME-theme") | |
771 | ;; This is done by the function `load-theme'. Loading a theme | |
772 | ;; automatically enables it. | |
773 | ;; | |
774 | ;; When a theme is enabled, the `theme-value' and `theme-face' | |
775 | ;; properties for the affected symbols are set. When a theme is | |
776 | ;; disabled, its settings are removed from the `theme-value' and | |
777 | ;; `theme-face' properties, but the theme's own `theme-settings' | |
778 | ;; property remains unchanged. | |
779 | ||
d358aa10 | 780 | (defvar custom-known-themes '(user changed) |
9c4b6e94 | 781 | "Themes that have been defined with `deftheme'. |
d358aa10 | 782 | The default value is the list (user changed). The theme `changed' |
79a0aa11 | 783 | contains the settings before custom themes are applied. The |
9b05fb22 | 784 | theme `user' contains all the settings the user customized and saved. |
e1bab181 RS |
785 | Additional themes declared with the `deftheme' macro will be added to |
786 | the front of this list.") | |
787 | ||
e1bab181 RS |
788 | (defsubst custom-theme-p (theme) |
789 | "Non-nil when THEME has been defined." | |
790 | (memq theme custom-known-themes)) | |
791 | ||
792 | (defsubst custom-check-theme (theme) | |
793 | "Check whether THEME is valid, and signal an error if it is not." | |
794 | (unless (custom-theme-p theme) | |
795 | (error "Unknown theme `%s'" theme))) | |
796 | ||
d358aa10 CY |
797 | (defun custom-push-theme (prop symbol theme mode &optional value) |
798 | "Record VALUE for face or variable SYMBOL in custom theme THEME. | |
799 | PROP is `theme-face' for a face, `theme-value' for a variable. | |
e1bab181 RS |
800 | |
801 | MODE can be either the symbol `set' or the symbol `reset'. If it is the | |
802 | symbol `set', then VALUE is the value to use. If it is the symbol | |
d358aa10 | 803 | `reset', then SYMBOL will be removed from THEME (VALUE is ignored). |
e1bab181 | 804 | |
e1bab181 | 805 | See `custom-known-themes' for a list of known themes." |
b2a41d12 | 806 | (unless (memq prop '(theme-value theme-face)) |
d820f1fb | 807 | (error "Unknown theme property")) |
46ce5feb | 808 | (let* ((old (get symbol prop)) |
d358aa10 CY |
809 | (setting (assq theme old)) ; '(theme value) |
810 | (theme-settings ; '(prop symbol theme value) | |
811 | (get theme 'theme-settings))) | |
812 | (if (eq mode 'reset) | |
d820f1fb CY |
813 | ;; Remove a setting. |
814 | (when setting | |
815 | (let (res) | |
816 | (dolist (theme-setting theme-settings) | |
817 | (if (and (eq (car theme-setting) prop) | |
818 | (eq (cadr theme-setting) symbol)) | |
819 | (setq res theme-setting))) | |
820 | (put theme 'theme-settings (delq res theme-settings))) | |
821 | (put symbol prop (delq setting old))) | |
822 | (if setting | |
823 | ;; Alter an existing setting. | |
824 | (let (res) | |
825 | (dolist (theme-setting theme-settings) | |
826 | (if (and (eq (car theme-setting) prop) | |
827 | (eq (cadr theme-setting) symbol)) | |
828 | (setq res theme-setting))) | |
829 | (put theme 'theme-settings | |
d358aa10 | 830 | (cons (list prop symbol theme value) |
d820f1fb | 831 | (delq res theme-settings))) |
d358aa10 | 832 | (setcar (cdr setting) value)) |
d820f1fb CY |
833 | ;; Add a new setting. |
834 | ;; If the user changed the value outside of Customize, we | |
d358aa10 | 835 | ;; first save the current value to a fake theme, `changed'. |
d820f1fb CY |
836 | ;; This ensures that the user-set value comes back if the |
837 | ;; theme is later disabled. | |
838 | (if (null old) | |
839 | (if (and (eq prop 'theme-value) | |
e58d8ef5 CY |
840 | (boundp symbol)) |
841 | (let ((sv (get symbol 'standard-value))) | |
d54fbdfd SM |
842 | (unless (and sv |
843 | (equal (eval (car sv)) (symbol-value symbol))) | |
844 | (setq old (list (list 'changed (symbol-value symbol)))))) | |
e02577b7 CY |
845 | (if (and (facep symbol) |
846 | (not (face-spec-match-p symbol (get symbol 'face-defface-spec)))) | |
d358aa10 | 847 | (setq old (list (list 'changed (list |
d820f1fb | 848 | (append '(t) (custom-face-attributes-get symbol nil))))))))) |
d358aa10 | 849 | (put symbol prop (cons (list theme value) old)) |
d820f1fb | 850 | (put theme 'theme-settings |
d358aa10 | 851 | (cons (list prop symbol theme value) |
d820f1fb | 852 | theme-settings)))))) |
55535639 | 853 | |
c2e2f9be | 854 | \f |
55535639 | 855 | (defun custom-set-variables (&rest args) |
f1a262ed RS |
856 | "Install user customizations of variable values specified in ARGS. |
857 | These settings are registered as theme `user'. | |
64e9f9d9 | 858 | The arguments should each be a list of the form: |
55535639 | 859 | |
f1a262ed | 860 | (SYMBOL EXP [NOW [REQUEST [COMMENT]]]) |
55535639 | 861 | |
f1a262ed RS |
862 | This stores EXP (without evaluating it) as the saved value for SYMBOL. |
863 | If NOW is present and non-nil, then also evaluate EXP and set | |
864 | the default value for the SYMBOL to the value of EXP. | |
e1bab181 | 865 | |
f1a262ed RS |
866 | REQUEST is a list of features we must require in order to |
867 | handle SYMBOL properly. | |
55535639 | 868 | COMMENT is a comment string about SYMBOL." |
e1bab181 RS |
869 | (apply 'custom-theme-set-variables 'user args)) |
870 | ||
871 | (defun custom-theme-set-variables (theme &rest args) | |
f1a262ed RS |
872 | "Initialize variables for theme THEME according to settings in ARGS. |
873 | Each of the arguments in ARGS should be a list of this form: | |
e1bab181 | 874 | |
f1a262ed | 875 | (SYMBOL EXP [NOW [REQUEST [COMMENT]]]) |
e1bab181 | 876 | |
f1a262ed RS |
877 | This stores EXP (without evaluating it) as the saved value for SYMBOL. |
878 | If NOW is present and non-nil, then also evaluate EXP and set | |
879 | the default value for the SYMBOL to the value of EXP. | |
e1bab181 | 880 | |
f1a262ed RS |
881 | REQUEST is a list of features we must require in order to |
882 | handle SYMBOL properly. | |
e1bab181 RS |
883 | COMMENT is a comment string about SYMBOL. |
884 | ||
f1a262ed | 885 | EXP itself is saved unevaluated as SYMBOL property `saved-value' and |
e1bab181 RS |
886 | in SYMBOL's list property `theme-value' \(using `custom-push-theme')." |
887 | (custom-check-theme theme) | |
9277ee6c SM |
888 | |
889 | ;; Process all the needed autoloads before anything else, so that the | |
890 | ;; subsequent code has all the info it needs (e.g. which var corresponds | |
891 | ;; to a minor mode), regardless of the ordering of the variables. | |
892 | (dolist (entry args) | |
893 | (let* ((symbol (indirect-variable (nth 0 entry)))) | |
894 | (unless (or (get symbol 'standard-value) | |
895 | (memq (get symbol 'custom-autoload) '(nil noset))) | |
896 | ;; This symbol needs to be autoloaded, even just for a `set'. | |
897 | (custom-load-symbol symbol)))) | |
1e281896 | 898 | |
9277ee6c | 899 | ;; Move minor modes and variables with explicit requires to the end. |
059290d6 SM |
900 | (setq args |
901 | (sort args | |
902 | (lambda (a1 a2) | |
903 | (let* ((sym1 (car a1)) | |
904 | (sym2 (car a2)) | |
905 | (1-then-2 (memq sym1 (get sym2 'custom-dependencies))) | |
906 | (2-then-1 (memq sym2 (get sym1 'custom-dependencies)))) | |
907 | (cond ((and 1-then-2 2-then-1) | |
908 | (error "Circular custom dependency between `%s' and `%s'" | |
909 | sym1 sym2)) | |
910 | (2-then-1 nil) | |
3d0c2a29 GM |
911 | ;; 1 is a dependency of 2, so needs to be set first. |
912 | (1-then-2) | |
b6790c3e SM |
913 | ;; Put minor modes and symbols with :require last. |
914 | ;; Putting minor modes last ensures that the mode | |
915 | ;; function will see other customized values rather | |
916 | ;; than default values. | |
917 | (t (or (nth 3 a2) | |
918 | (eq (get sym2 'custom-set) | |
919 | 'custom-set-minor-mode)))))))) | |
059290d6 SM |
920 | (while args |
921 | (let ((entry (car args))) | |
922 | (if (listp entry) | |
923 | (let* ((symbol (indirect-variable (nth 0 entry))) | |
924 | (value (nth 1 entry)) | |
925 | (now (nth 2 entry)) | |
926 | (requests (nth 3 entry)) | |
927 | (comment (nth 4 entry)) | |
928 | set) | |
929 | (when requests | |
930 | (put symbol 'custom-requests requests) | |
931 | (mapc 'require requests)) | |
932 | (setq set (or (get symbol 'custom-set) 'custom-set-default)) | |
933 | (put symbol 'saved-value (list value)) | |
934 | (put symbol 'saved-variable-comment comment) | |
935 | (custom-push-theme 'theme-value symbol theme 'set value) | |
936 | ;; Allow for errors in the case where the setter has | |
55535639 PJ |
937 | ;; changed between versions, say, but let the user know. |
938 | (condition-case data | |
939 | (cond (now | |
940 | ;; Rogue variable, set it now. | |
941 | (put symbol 'force-value t) | |
942 | (funcall set symbol (eval value))) | |
943 | ((default-boundp symbol) | |
944 | ;; Something already set this, overwrite it. | |
945 | (funcall set symbol (eval value)))) | |
71296446 | 946 | (error |
55535639 | 947 | (message "Error setting %s: %s" symbol data))) |
059290d6 SM |
948 | (setq args (cdr args)) |
949 | (and (or now (default-boundp symbol)) | |
950 | (put symbol 'variable-comment comment))) | |
d54fbdfd SM |
951 | ;; I believe this is dead-code, because the `sort' code above would |
952 | ;; have burped before we could get here. --Stef | |
059290d6 SM |
953 | ;; Old format, a plist of SYMBOL VALUE pairs. |
954 | (message "Warning: old format `custom-set-variables'") | |
955 | (ding) | |
956 | (sit-for 2) | |
957 | (let ((symbol (indirect-variable (nth 0 args))) | |
958 | (value (nth 1 args))) | |
959 | (put symbol 'saved-value (list value)) | |
960 | (custom-push-theme 'theme-value symbol theme 'set value)) | |
961 | (setq args (cdr (cdr args))))))) | |
55535639 | 962 | |
46ce5feb RS |
963 | \f |
964 | ;;; Defining themes. | |
965 | ||
d358aa10 CY |
966 | ;; A theme file should be named `THEME-theme.el' (where THEME is the theme |
967 | ;; name), and found in either `custom-theme-directory' or the load path. | |
968 | ;; It has the following format: | |
969 | ;; | |
970 | ;; (deftheme THEME | |
971 | ;; DOCSTRING) | |
972 | ;; | |
973 | ;; (custom-theme-set-variables | |
974 | ;; 'THEME | |
975 | ;; [THEME-VARIABLES]) | |
976 | ;; | |
977 | ;; (custom-theme-set-faces | |
978 | ;; 'THEME | |
979 | ;; [THEME-FACES]) | |
980 | ;; | |
981 | ;; (provide-theme 'THEME) | |
46ce5feb | 982 | |
46ce5feb | 983 | |
d358aa10 CY |
984 | ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where |
985 | ;; they were used to supply keyword-value pairs like `:immediate', | |
986 | ;; `:variable-reset-string', etc. We don't use any of these, so ignore them. | |
6d912ee1 | 987 | |
d358aa10 CY |
988 | (defmacro deftheme (theme &optional doc &rest ignored) |
989 | "Declare THEME to be a Custom theme. | |
990 | The optional argument DOC is a doc string describing the theme. | |
46ce5feb RS |
991 | |
992 | Any theme `foo' should be defined in a file called `foo-theme.el'; | |
993 | see `custom-make-theme-feature' for more information." | |
994 | (let ((feature (custom-make-theme-feature theme))) | |
995 | ;; It is better not to use backquote in this file, | |
996 | ;; because that makes a bootstrapping problem | |
997 | ;; if you need to recompile all the Lisp files using interpreted code. | |
d358aa10 | 998 | (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc))) |
46ce5feb | 999 | |
d358aa10 | 1000 | (defun custom-declare-theme (theme feature &optional doc &rest ignored) |
46ce5feb | 1001 | "Like `deftheme', but THEME is evaluated as a normal argument. |
d358aa10 CY |
1002 | FEATURE is the feature this theme provides. Normally, this is a symbol |
1003 | created from THEME by `custom-make-theme-feature'." | |
1004 | (if (memq theme '(user changed)) | |
1005 | (error "Custom theme cannot be named %S" theme)) | |
46ce5feb RS |
1006 | (add-to-list 'custom-known-themes theme) |
1007 | (put theme 'theme-feature feature) | |
d358aa10 | 1008 | (when doc (put theme 'theme-documentation doc))) |
46ce5feb RS |
1009 | |
1010 | (defun custom-make-theme-feature (theme) | |
1011 | "Given a symbol THEME, create a new symbol by appending \"-theme\". | |
1012 | Store this symbol in the `theme-feature' property of THEME. | |
1013 | Calling `provide-theme' to provide THEME actually puts `THEME-theme' | |
1014 | into `features'. | |
1015 | ||
1016 | This allows for a file-name convention for autoloading themes: | |
1017 | Every theme X has a property `provide-theme' whose value is \"X-theme\". | |
87d737ae | 1018 | \(load-theme X) then attempts to load the file `X-theme.el'." |
46ce5feb RS |
1019 | (intern (concat (symbol-name theme) "-theme"))) |
1020 | \f | |
1021 | ;;; Loading themes. | |
1022 | ||
9c4b6e94 | 1023 | (defcustom custom-theme-directory |
63212d45 | 1024 | user-emacs-directory |
9c4b6e94 | 1025 | "Directory in which Custom theme files should be written. |
87d737ae | 1026 | `load-theme' searches this directory in addition to load-path. |
9c4b6e94 LT |
1027 | The command `customize-create-theme' writes the files it produces |
1028 | into this directory." | |
1029 | :type 'string | |
1030 | :group 'customize | |
1031 | :version "22.1") | |
1032 | ||
e1bab181 | 1033 | (defun provide-theme (theme) |
d358aa10 CY |
1034 | "Indicate that this file provides THEME. |
1035 | This calls `provide' to provide the feature name stored in THEME's | |
1036 | property `theme-feature' (which is usually a symbol created by | |
1037 | `custom-make-theme-feature')." | |
1038 | (if (memq theme '(user changed)) | |
1039 | (error "Custom theme cannot be named %S" theme)) | |
e1bab181 RS |
1040 | (custom-check-theme theme) |
1041 | (provide (get theme 'theme-feature)) | |
d358aa10 | 1042 | ;; Loading a theme also enables it. |
46ce5feb RS |
1043 | (push theme custom-enabled-themes) |
1044 | ;; `user' must always be the highest-precedence enabled theme. | |
1045 | ;; Make that remain true. (This has the effect of making user settings | |
1046 | ;; override the ones just loaded, too.) | |
d358aa10 CY |
1047 | (let ((custom-enabling-themes t)) |
1048 | (enable-theme 'user))) | |
e1bab181 | 1049 | |
87d737ae | 1050 | (defun load-theme (theme) |
73e60f53 | 1051 | "Load a theme's settings from its file. |
87d737ae | 1052 | This also enables the theme; use `disable-theme' to disable it." |
e1bab181 RS |
1053 | ;; Note we do no check for validity of the theme here. |
1054 | ;; This allows to pull in themes by a file-name convention | |
87d737ae | 1055 | (interactive "SCustom theme name: ") |
73e60f53 CY |
1056 | ;; If reloading, clear out the old theme settings. |
1057 | (when (custom-theme-p theme) | |
1058 | (disable-theme theme) | |
1059 | (put theme 'theme-settings nil) | |
1060 | (put theme 'theme-feature nil) | |
1061 | (put theme 'theme-documentation nil)) | |
9c4b6e94 LT |
1062 | (let ((load-path (if (file-directory-p custom-theme-directory) |
1063 | (cons custom-theme-directory load-path) | |
1064 | load-path))) | |
d358aa10 | 1065 | (load (symbol-name (custom-make-theme-feature theme))))) |
46ce5feb RS |
1066 | \f |
1067 | ;;; Enabling and disabling loaded themes. | |
1068 | ||
b2a41d12 CY |
1069 | (defvar custom-enabling-themes nil) |
1070 | ||
87d737ae | 1071 | (defun enable-theme (theme) |
46ce5feb RS |
1072 | "Reenable all variable and face settings defined by THEME. |
1073 | The newly enabled theme gets the highest precedence (after `user'). | |
e335f09e CY |
1074 | If it is already enabled, just give it highest precedence (after `user'). |
1075 | ||
d358aa10 CY |
1076 | If THEME does not specify any theme settings, this tries to load |
1077 | the theme from its theme file, by calling `load-theme'." | |
87d737ae | 1078 | (interactive "SEnable Custom theme: ") |
d358aa10 CY |
1079 | (if (not (custom-theme-p theme)) |
1080 | (load-theme theme) | |
1081 | ;; This could use a bit of optimization -- cyd | |
1082 | (let ((settings (get theme 'theme-settings))) | |
1083 | (dolist (s settings) | |
1084 | (let* ((prop (car s)) | |
1085 | (symbol (cadr s)) | |
1086 | (spec-list (get symbol prop))) | |
1087 | (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list))) | |
1088 | (if (eq prop 'theme-value) | |
1089 | (custom-theme-recalc-variable symbol) | |
1090 | (custom-theme-recalc-face symbol))))) | |
1091 | (unless (eq theme 'user) | |
1092 | (setq custom-enabled-themes | |
1093 | (cons theme (delq theme custom-enabled-themes))) | |
1094 | (unless custom-enabling-themes | |
1095 | (enable-theme 'user))))) | |
b2a41d12 CY |
1096 | |
1097 | (defcustom custom-enabled-themes nil | |
1098 | "List of enabled Custom Themes, highest precedence first. | |
1099 | ||
1100 | This does not include the `user' theme, which is set by Customize, | |
1101 | and always takes precedence over other Custom Themes." | |
1102 | :group 'customize | |
1103 | :type '(repeat symbol) | |
3d0c2a29 | 1104 | :set-after '(custom-theme-directory) ; so we can find the themes |
b2a41d12 CY |
1105 | :set (lambda (symbol themes) |
1106 | ;; Avoid an infinite loop when custom-enabled-themes is | |
1107 | ;; defined in a theme (e.g. `user'). Enabling the theme sets | |
1108 | ;; custom-enabled-themes, which enables the theme... | |
1109 | (unless custom-enabling-themes | |
d358aa10 | 1110 | (let ((custom-enabling-themes t) failures) |
b2a41d12 CY |
1111 | (setq themes (delq 'user (delete-dups themes))) |
1112 | (if (boundp symbol) | |
1113 | (dolist (theme (symbol-value symbol)) | |
1114 | (if (not (memq theme themes)) | |
1115 | (disable-theme theme)))) | |
1116 | (dolist (theme (reverse themes)) | |
d358aa10 | 1117 | (condition-case nil |
b2a41d12 | 1118 | (enable-theme theme) |
d358aa10 CY |
1119 | (error (progn (push theme failures) |
1120 | (setq themes (delq theme themes)))))) | |
b2a41d12 | 1121 | (enable-theme 'user) |
d358aa10 CY |
1122 | (custom-set-default symbol themes) |
1123 | (if failures | |
1124 | (message "Failed to enable themes: %s" | |
1125 | (mapconcat 'symbol-name failures " "))))))) | |
b2a41d12 | 1126 | |
d358aa10 | 1127 | (defsubst custom-theme-enabled-p (theme) |
b2a41d12 CY |
1128 | "Return non-nil if THEME is enabled." |
1129 | (memq theme custom-enabled-themes)) | |
46ce5feb | 1130 | |
87d737ae | 1131 | (defun disable-theme (theme) |
46ce5feb | 1132 | "Disable all variable and face settings defined by THEME. |
b2a41d12 | 1133 | See `custom-enabled-themes' for a list of enabled themes." |
d358aa10 CY |
1134 | (interactive (list (intern |
1135 | (completing-read | |
1136 | "Disable Custom theme: " | |
1137 | (mapcar 'symbol-name custom-enabled-themes) | |
1138 | nil t)))) | |
1139 | (when (custom-theme-enabled-p theme) | |
8913f945 RS |
1140 | (let ((settings (get theme 'theme-settings))) |
1141 | (dolist (s settings) | |
1142 | (let* ((prop (car s)) | |
1143 | (symbol (cadr s)) | |
1144 | (spec-list (get symbol prop))) | |
1145 | (put symbol prop (assq-delete-all theme spec-list)) | |
1146 | (if (eq prop 'theme-value) | |
1147 | (custom-theme-recalc-variable symbol) | |
b2a41d12 CY |
1148 | (custom-theme-recalc-face symbol))))) |
1149 | (setq custom-enabled-themes | |
1150 | (delq theme custom-enabled-themes)))) | |
46ce5feb | 1151 | |
46ce5feb RS |
1152 | (defun custom-variable-theme-value (variable) |
1153 | "Return (list VALUE) indicating the custom theme value of VARIABLE. | |
1154 | That is to say, it specifies what the value should be according to | |
1155 | currently enabled custom themes. | |
1156 | ||
1157 | This function returns nil if no custom theme specifies a value for VARIABLE." | |
1158 | (let* ((theme-value (get variable 'theme-value))) | |
1159 | (if theme-value | |
d358aa10 | 1160 | (cdr (car theme-value))))) |
46ce5feb | 1161 | |
46ce5feb RS |
1162 | (defun custom-theme-recalc-variable (variable) |
1163 | "Set VARIABLE according to currently enabled custom themes." | |
1164 | (let ((valspec (custom-variable-theme-value variable))) | |
d358aa10 CY |
1165 | (if valspec |
1166 | (put variable 'saved-value valspec) | |
46ce5feb | 1167 | (setq valspec (get variable 'standard-value))) |
d358aa10 CY |
1168 | (if (and valspec |
1169 | (or (get variable 'force-value) | |
1170 | (default-boundp variable))) | |
1171 | (funcall (or (get variable 'custom-set) 'set-default) variable | |
1172 | (eval (car valspec)))))) | |
46ce5feb RS |
1173 | |
1174 | (defun custom-theme-recalc-face (face) | |
1175 | "Set FACE according to currently enabled custom themes." | |
b2a41d12 | 1176 | (if (facep face) |
279c4408 GM |
1177 | (face-spec-set face |
1178 | (get (or (get face 'face-alias) face) | |
1179 | 'face-override-spec)))) | |
46ce5feb | 1180 | \f |
d358aa10 CY |
1181 | ;;; XEmacs compability functions |
1182 | ||
1183 | ;; In XEmacs, when you reset a Custom Theme, you have to specify the | |
1184 | ;; theme to reset it to. We just apply the next available theme, so | |
1185 | ;; just ignore the IGNORED arguments. | |
1186 | ||
e1bab181 | 1187 | (defun custom-theme-reset-variables (theme &rest args) |
d358aa10 | 1188 | "Reset some variable settings in THEME to their values in other themes. |
46ce5feb | 1189 | Each of the arguments ARGS has this form: |
e1bab181 | 1190 | |
d358aa10 | 1191 | (VARIABLE IGNORED) |
e1bab181 | 1192 | |
7e8b9dc3 | 1193 | This means reset VARIABLE. (The argument IGNORED is ignored)." |
e1bab181 | 1194 | (custom-check-theme theme) |
46ce5feb | 1195 | (dolist (arg args) |
d358aa10 | 1196 | (custom-push-theme 'theme-value (car arg) theme 'reset))) |
e1bab181 RS |
1197 | |
1198 | (defun custom-reset-variables (&rest args) | |
d358aa10 | 1199 | "Reset the specs of some variables to their values in other themes. |
46ce5feb | 1200 | This creates settings in the `user' theme. |
e1bab181 | 1201 | |
46ce5feb | 1202 | Each of the arguments ARGS has this form: |
e1bab181 | 1203 | |
d358aa10 | 1204 | (VARIABLE IGNORED) |
e1bab181 | 1205 | |
7e8b9dc3 | 1206 | This means reset VARIABLE. (The argument IGNORED is ignored)." |
e1bab181 RS |
1207 | (apply 'custom-theme-reset-variables 'user args)) |
1208 | ||
55535639 PJ |
1209 | ;;; The End. |
1210 | ||
1211 | ;; Process the defcustoms for variables loaded before this file. | |
1212 | (while custom-declare-variable-list | |
1213 | (apply 'custom-declare-variable (car custom-declare-variable-list)) | |
1214 | (setq custom-declare-variable-list (cdr custom-declare-variable-list))) | |
1215 | ||
1216 | (provide 'custom) | |
1217 | ||
059290d6 | 1218 | ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2 |
55535639 | 1219 | ;;; custom.el ends here |