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, |
ae940284 | 4 | ;; 2005, 2006, 2007, 2008, 2009 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) | |
417 | (push (cons load-file-name symbol) custom-current-group-alist))) | |
55535639 PJ |
418 | (run-hooks 'custom-define-hook) |
419 | symbol) | |
420 | ||
421 | (defmacro defgroup (symbol members doc &rest args) | |
422 | "Declare SYMBOL as a customization group containing MEMBERS. | |
423 | SYMBOL does not need to be quoted. | |
424 | ||
425 | Third arg DOC is the group documentation. | |
426 | ||
427 | MEMBERS should be an alist of the form ((NAME WIDGET)...) where | |
428 | NAME is a symbol and WIDGET is a widget for editing that symbol. | |
429 | Useful widgets are `custom-variable' for editing variables, | |
430 | `custom-face' for edit faces, and `custom-group' for editing groups. | |
431 | ||
432 | The remaining arguments should have the form | |
433 | ||
434 | [KEYWORD VALUE]... | |
435 | ||
81117bdd BW |
436 | For a list of valid keywords, see the common keywords listed in |
437 | `defcustom'. | |
55535639 | 438 | |
81117bdd BW |
439 | See Info node `(elisp) Customization' in the Emacs Lisp manual |
440 | for more information." | |
45aacac6 | 441 | (declare (doc-string 3)) |
55535639 PJ |
442 | ;; It is better not to use backquote in this file, |
443 | ;; because that makes a bootstrapping problem | |
444 | ;; if you need to recompile all the Lisp files using interpreted code. | |
445 | (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args)) | |
446 | ||
447 | (defun custom-add-to-group (group option widget) | |
448 | "To existing GROUP add a new OPTION of type WIDGET. | |
449 | If there already is an entry for OPTION and WIDGET, nothing is done." | |
450 | (let ((members (get group 'custom-group)) | |
451 | (entry (list option widget))) | |
452 | (unless (member entry members) | |
453 | (put group 'custom-group (nconc members (list entry)))))) | |
454 | ||
29512a0f SM |
455 | (defun custom-group-of-mode (mode) |
456 | "Return the custom group corresponding to the major or minor MODE. | |
457 | If no such group is found, return nil." | |
458 | (or (get mode 'custom-mode-group) | |
459 | (if (or (get mode 'custom-group) | |
460 | (and (string-match "-mode\\'" (symbol-name mode)) | |
461 | (get (setq mode (intern (substring (symbol-name mode) | |
462 | 0 (match-beginning 0)))) | |
463 | 'custom-group))) | |
464 | mode))) | |
465 | ||
55535639 PJ |
466 | ;;; Properties. |
467 | ||
468 | (defun custom-handle-all-keywords (symbol args type) | |
469 | "For customization option SYMBOL, handle keyword arguments ARGS. | |
470 | Third argument TYPE is the custom option type." | |
d3b80e9b | 471 | (unless (memq :group args) |
9b6098b9 | 472 | (custom-add-to-group (custom-current-group) symbol type)) |
55535639 PJ |
473 | (while args |
474 | (let ((arg (car args))) | |
475 | (setq args (cdr args)) | |
476 | (unless (symbolp arg) | |
477 | (error "Junk in args %S" args)) | |
478 | (let ((keyword arg) | |
479 | (value (car args))) | |
480 | (unless args | |
481 | (error "Keyword %s is missing an argument" keyword)) | |
482 | (setq args (cdr args)) | |
483 | (custom-handle-keyword symbol keyword value type))))) | |
484 | ||
485 | (defun custom-handle-keyword (symbol keyword value type) | |
486 | "For customization option SYMBOL, handle KEYWORD with VALUE. | |
487 | Fourth argument TYPE is the custom option type." | |
488 | (if purify-flag | |
489 | (setq value (purecopy value))) | |
490 | (cond ((eq keyword :group) | |
491 | (custom-add-to-group value symbol type)) | |
492 | ((eq keyword :version) | |
493 | (custom-add-version symbol value)) | |
4e9c705e BW |
494 | ((eq keyword :package-version) |
495 | (custom-add-package-version symbol value)) | |
55535639 PJ |
496 | ((eq keyword :link) |
497 | (custom-add-link symbol value)) | |
498 | ((eq keyword :load) | |
499 | (custom-add-load symbol value)) | |
500 | ((eq keyword :tag) | |
501 | (put symbol 'custom-tag value)) | |
502 | ((eq keyword :set-after) | |
503 | (custom-add-dependencies symbol value)) | |
504 | (t | |
505 | (error "Unknown keyword %s" keyword)))) | |
506 | ||
507 | (defun custom-add-dependencies (symbol value) | |
508 | "To the custom option SYMBOL, add dependencies specified by VALUE. | |
509 | VALUE should be a list of symbols. For each symbol in that list, | |
510 | this specifies that SYMBOL should be set after the specified symbol, if | |
511 | both appear in constructs like `custom-set-variables'." | |
512 | (unless (listp value) | |
513 | (error "Invalid custom dependency `%s'" value)) | |
514 | (let* ((deps (get symbol 'custom-dependencies)) | |
515 | (new-deps deps)) | |
516 | (while value | |
517 | (let ((dep (car value))) | |
518 | (unless (symbolp dep) | |
519 | (error "Invalid custom dependency `%s'" dep)) | |
520 | (unless (memq dep new-deps) | |
521 | (setq new-deps (cons dep new-deps))) | |
522 | (setq value (cdr value)))) | |
523 | (unless (eq deps new-deps) | |
524 | (put symbol 'custom-dependencies new-deps)))) | |
e1bab181 | 525 | |
55535639 PJ |
526 | (defun custom-add-option (symbol option) |
527 | "To the variable SYMBOL add OPTION. | |
528 | ||
a4842ffa RS |
529 | If SYMBOL's custom type is a hook, OPTION should be a hook member. |
530 | If SYMBOL's custom type is an alist, OPTION specifies a symbol | |
531 | to offer to the user as a possible key in the alist. | |
532 | For other custom types, this has no effect." | |
55535639 PJ |
533 | (let ((options (get symbol 'custom-options))) |
534 | (unless (member option options) | |
535 | (put symbol 'custom-options (cons option options))))) | |
467064a4 | 536 | (defalias 'custom-add-frequent-value 'custom-add-option) |
55535639 PJ |
537 | |
538 | (defun custom-add-link (symbol widget) | |
539 | "To the custom option SYMBOL add the link WIDGET." | |
540 | (let ((links (get symbol 'custom-links))) | |
541 | (unless (member widget links) | |
542 | (put symbol 'custom-links (cons (purecopy widget) links))))) | |
543 | ||
544 | (defun custom-add-version (symbol version) | |
545 | "To the custom option SYMBOL add the version VERSION." | |
546 | (put symbol 'custom-version (purecopy version))) | |
547 | ||
4e9c705e BW |
548 | (defun custom-add-package-version (symbol version) |
549 | "To the custom option SYMBOL add the package version VERSION." | |
550 | (put symbol 'custom-package-version (purecopy version))) | |
551 | ||
55535639 PJ |
552 | (defun custom-add-load (symbol load) |
553 | "To the custom option SYMBOL add the dependency LOAD. | |
554 | LOAD should be either a library file name, or a feature name." | |
555 | (let ((loads (get symbol 'custom-loads))) | |
556 | (unless (member load loads) | |
557 | (put symbol 'custom-loads (cons (purecopy load) loads))))) | |
558 | ||
d54fbdfd SM |
559 | (defun custom-autoload (symbol load &optional noset) |
560 | "Mark SYMBOL as autoloaded custom variable and add dependency LOAD. | |
561 | If NOSET is non-nil, don't bother autoloading LOAD when setting the variable." | |
562 | (put symbol 'custom-autoload (if noset 'noset t)) | |
1669290d MR |
563 | (custom-add-load symbol load)) |
564 | ||
565 | ;; This test is also in the C code of `user-variable-p'. | |
566 | (defun custom-variable-p (variable) | |
289e1999 LT |
567 | "Return non-nil if VARIABLE is a custom variable. |
568 | This recursively follows aliases." | |
569 | (setq variable (indirect-variable variable)) | |
1669290d MR |
570 | (or (get variable 'standard-value) |
571 | (get variable 'custom-autoload))) | |
572 | ||
400b960e RS |
573 | (defun custom-note-var-changed (variable) |
574 | "Inform Custom that VARIABLE has been set (changed). | |
575 | VARIABLE is a symbol that names a user option. | |
576 | The result is that the change is treated as having been made through Custom." | |
400b960e | 577 | (put variable 'customized-value (list (custom-quote (eval variable))))) |
06f5c483 JL |
578 | |
579 | \f | |
580 | ;;; Custom Themes | |
400b960e | 581 | |
8f772dfd RS |
582 | ;;; Loading files needed to customize a symbol. |
583 | ;;; This is in custom.el because menu-bar.el needs it for toggle cmds. | |
584 | ||
585 | (defvar custom-load-recursion nil | |
586 | "Hack to avoid recursive dependencies.") | |
587 | ||
588 | (defun custom-load-symbol (symbol) | |
589 | "Load all dependencies for SYMBOL." | |
590 | (unless custom-load-recursion | |
29512a0f | 591 | (let ((custom-load-recursion t)) |
a8c78057 RS |
592 | ;; Load these files if not already done, |
593 | ;; to make sure we know all the dependencies of SYMBOL. | |
594 | (condition-case nil | |
595 | (require 'cus-load) | |
596 | (error nil)) | |
597 | (condition-case nil | |
598 | (require 'cus-start) | |
599 | (error nil)) | |
29512a0f SM |
600 | (dolist (load (get symbol 'custom-loads)) |
601 | (cond ((symbolp load) (condition-case nil (require load) (error nil))) | |
602 | ;; This is subsumed by the test below, but it's much faster. | |
8f772dfd RS |
603 | ((assoc load load-history)) |
604 | ;; This was just (assoc (locate-library load) load-history) | |
605 | ;; but has been optimized not to load locate-library | |
606 | ;; if not necessary. | |
29512a0f SM |
607 | ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load) |
608 | "\\(\\'\\|\\.\\)")) | |
609 | (found nil)) | |
8f772dfd | 610 | (dolist (loaded load-history) |
c3891447 RS |
611 | (and (stringp (car loaded)) |
612 | (string-match regexp (car loaded)) | |
8f772dfd RS |
613 | (setq found t))) |
614 | found)) | |
615 | ;; Without this, we would load cus-edit recursively. | |
616 | ;; We are still loading it when we call this, | |
617 | ;; and it is not in load-history yet. | |
618 | ((equal load "cus-edit")) | |
29512a0f | 619 | (t (condition-case nil (load load) (error nil)))))))) |
46ce5feb | 620 | \f |
c2e2f9be CY |
621 | (defvar custom-local-buffer nil |
622 | "Non-nil, in a Customization buffer, means customize a specific buffer. | |
623 | If this variable is non-nil, it should be a buffer, | |
624 | and it means customize the local bindings of that buffer. | |
625 | This variable is a permanent local, and it normally has a local binding | |
626 | in every Customization buffer.") | |
627 | (put 'custom-local-buffer 'permanent-local t) | |
628 | ||
629 | (defun custom-set-default (variable value) | |
630 | "Default :set function for a customizable variable. | |
631 | Normally, this sets the default value of VARIABLE to VALUE, | |
632 | but if `custom-local-buffer' is non-nil, | |
633 | this sets the local binding in that buffer instead." | |
634 | (if custom-local-buffer | |
635 | (with-current-buffer custom-local-buffer | |
636 | (set variable value)) | |
637 | (set-default variable value))) | |
638 | ||
639 | (defun custom-set-minor-mode (variable value) | |
640 | ":set function for minor mode variables. | |
641 | Normally, this sets the default value of VARIABLE to nil if VALUE | |
642 | is nil and to t otherwise, | |
643 | but if `custom-local-buffer' is non-nil, | |
644 | this sets the local binding in that buffer instead." | |
645 | (if custom-local-buffer | |
646 | (with-current-buffer custom-local-buffer | |
647 | (funcall variable (if value 1 0))) | |
648 | (funcall variable (if value 1 0)))) | |
649 | ||
650 | (defun custom-quote (sexp) | |
4837b516 | 651 | "Quote SEXP if it is not self quoting." |
c2e2f9be CY |
652 | (if (or (memq sexp '(t nil)) |
653 | (keywordp sexp) | |
654 | (and (listp sexp) | |
655 | (memq (car sexp) '(lambda))) | |
656 | (stringp sexp) | |
657 | (numberp sexp) | |
658 | (vectorp sexp) | |
659 | ;;; (and (fboundp 'characterp) | |
660 | ;;; (characterp sexp)) | |
661 | ) | |
662 | sexp | |
663 | (list 'quote sexp))) | |
664 | ||
665 | (defun customize-mark-to-save (symbol) | |
666 | "Mark SYMBOL for later saving. | |
667 | ||
668 | If the default value of SYMBOL is different from the standard value, | |
669 | set the `saved-value' property to a list whose car evaluates to the | |
670 | default value. Otherwise, set it to nil. | |
671 | ||
672 | To actually save the value, call `custom-save-all'. | |
673 | ||
4837b516 | 674 | Return non-nil if the `saved-value' property actually changed." |
ec9f0a62 | 675 | (custom-load-symbol symbol) |
c2e2f9be CY |
676 | (let* ((get (or (get symbol 'custom-get) 'default-value)) |
677 | (value (funcall get symbol)) | |
678 | (saved (get symbol 'saved-value)) | |
679 | (standard (get symbol 'standard-value)) | |
680 | (comment (get symbol 'customized-variable-comment))) | |
4837b516 | 681 | ;; Save default value if different from standard value. |
c2e2f9be CY |
682 | (if (or (null standard) |
683 | (not (equal value (condition-case nil | |
684 | (eval (car standard)) | |
685 | (error nil))))) | |
686 | (put symbol 'saved-value (list (custom-quote value))) | |
687 | (put symbol 'saved-value nil)) | |
688 | ;; Clear customized information (set, but not saved). | |
689 | (put symbol 'customized-value nil) | |
690 | ;; Save any comment that might have been set. | |
691 | (when comment | |
692 | (put symbol 'saved-variable-comment comment)) | |
693 | (not (equal saved (get symbol 'saved-value))))) | |
694 | ||
695 | (defun customize-mark-as-set (symbol) | |
696 | "Mark current value of SYMBOL as being set from customize. | |
697 | ||
698 | If the default value of SYMBOL is different from the saved value if any, | |
699 | or else if it is different from the standard value, set the | |
700 | `customized-value' property to a list whose car evaluates to the | |
701 | default value. Otherwise, set it to nil. | |
702 | ||
4837b516 | 703 | Return non-nil if the `customized-value' property actually changed." |
ec9f0a62 | 704 | (custom-load-symbol symbol) |
c2e2f9be CY |
705 | (let* ((get (or (get symbol 'custom-get) 'default-value)) |
706 | (value (funcall get symbol)) | |
707 | (customized (get symbol 'customized-value)) | |
708 | (old (or (get symbol 'saved-value) (get symbol 'standard-value)))) | |
4837b516 | 709 | ;; Mark default value as set if different from old value. |
d54fbdfd SM |
710 | (if (not (and old |
711 | (equal value (condition-case nil | |
712 | (eval (car old)) | |
713 | (error nil))))) | |
fccf2784 CY |
714 | (progn (put symbol 'customized-value (list (custom-quote value))) |
715 | (custom-push-theme 'theme-value symbol 'user 'set | |
716 | (custom-quote value))) | |
c2e2f9be CY |
717 | (put symbol 'customized-value nil)) |
718 | ;; Changed? | |
719 | (not (equal customized (get symbol 'customized-value))))) | |
720 | ||
721 | (defun custom-reevaluate-setting (symbol) | |
722 | "Reset the value of SYMBOL by re-evaluating its saved or standard value. | |
723 | Use the :set function to do so. This is useful for customizable options | |
724 | that are defined before their standard value can really be computed. | |
725 | E.g. dumped variables whose default depends on run-time information." | |
726 | (funcall (or (get symbol 'custom-set) 'set-default) | |
727 | symbol | |
728 | (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value)))))) | |
729 | ||
730 | \f | |
d358aa10 CY |
731 | ;;; Custom Themes |
732 | ||
733 | ;; Custom themes are collections of settings that can be enabled or | |
734 | ;; disabled as a unit. | |
735 | ||
736 | ;; Each Custom theme is defined by a symbol, called the theme name. | |
737 | ;; The `theme-settings' property of the theme name records the | |
738 | ;; variable and face settings of the theme. This property is a list | |
739 | ;; of elements, each of the form | |
740 | ;; | |
741 | ;; (PROP SYMBOL THEME VALUE) | |
742 | ;; | |
743 | ;; - PROP is either `theme-value' or `theme-face' | |
744 | ;; - SYMBOL is the face or variable name | |
745 | ;; - THEME is the theme name (redundant, but simplifies the code) | |
746 | ;; - VALUE is an expression that gives the theme's setting for SYMBOL. | |
747 | ;; | |
748 | ;; The theme name also has a `theme-feature' property, whose value is | |
749 | ;; specified when the theme is defined (see `custom-declare-theme'). | |
750 | ;; Usually, this is just a symbol named THEME-theme. This lets | |
751 | ;; external libraries call (require 'foo-theme). | |
752 | ||
753 | ;; In addition, each symbol (either a variable or a face) affected by | |
754 | ;; an *enabled* theme has a `theme-value' or `theme-face' property, | |
755 | ;; which is a list of elements each of the form | |
756 | ;; | |
757 | ;; (THEME VALUE) | |
758 | ;; | |
759 | ;; which have the same meanings as in `theme-settings'. | |
760 | ;; | |
761 | ;; The `theme-value' and `theme-face' lists are ordered by decreasing | |
762 | ;; theme precedence. Thus, the first element is always the one that | |
763 | ;; is in effect. | |
764 | ||
765 | ;; Each theme is stored in a theme file, with filename THEME-theme.el. | |
766 | ;; Loading a theme basically involves calling (load "THEME-theme") | |
767 | ;; This is done by the function `load-theme'. Loading a theme | |
768 | ;; automatically enables it. | |
769 | ;; | |
770 | ;; When a theme is enabled, the `theme-value' and `theme-face' | |
771 | ;; properties for the affected symbols are set. When a theme is | |
772 | ;; disabled, its settings are removed from the `theme-value' and | |
773 | ;; `theme-face' properties, but the theme's own `theme-settings' | |
774 | ;; property remains unchanged. | |
775 | ||
d358aa10 | 776 | (defvar custom-known-themes '(user changed) |
9c4b6e94 | 777 | "Themes that have been defined with `deftheme'. |
d358aa10 | 778 | The default value is the list (user changed). The theme `changed' |
79a0aa11 | 779 | contains the settings before custom themes are applied. The |
9b05fb22 | 780 | theme `user' contains all the settings the user customized and saved. |
e1bab181 RS |
781 | Additional themes declared with the `deftheme' macro will be added to |
782 | the front of this list.") | |
783 | ||
e1bab181 RS |
784 | (defsubst custom-theme-p (theme) |
785 | "Non-nil when THEME has been defined." | |
786 | (memq theme custom-known-themes)) | |
787 | ||
788 | (defsubst custom-check-theme (theme) | |
789 | "Check whether THEME is valid, and signal an error if it is not." | |
790 | (unless (custom-theme-p theme) | |
791 | (error "Unknown theme `%s'" theme))) | |
792 | ||
d358aa10 CY |
793 | (defun custom-push-theme (prop symbol theme mode &optional value) |
794 | "Record VALUE for face or variable SYMBOL in custom theme THEME. | |
795 | PROP is `theme-face' for a face, `theme-value' for a variable. | |
e1bab181 RS |
796 | |
797 | MODE can be either the symbol `set' or the symbol `reset'. If it is the | |
798 | symbol `set', then VALUE is the value to use. If it is the symbol | |
d358aa10 | 799 | `reset', then SYMBOL will be removed from THEME (VALUE is ignored). |
e1bab181 | 800 | |
e1bab181 | 801 | See `custom-known-themes' for a list of known themes." |
b2a41d12 | 802 | (unless (memq prop '(theme-value theme-face)) |
d820f1fb | 803 | (error "Unknown theme property")) |
46ce5feb | 804 | (let* ((old (get symbol prop)) |
d358aa10 CY |
805 | (setting (assq theme old)) ; '(theme value) |
806 | (theme-settings ; '(prop symbol theme value) | |
807 | (get theme 'theme-settings))) | |
808 | (if (eq mode 'reset) | |
d820f1fb CY |
809 | ;; Remove a setting. |
810 | (when setting | |
811 | (let (res) | |
812 | (dolist (theme-setting theme-settings) | |
813 | (if (and (eq (car theme-setting) prop) | |
814 | (eq (cadr theme-setting) symbol)) | |
815 | (setq res theme-setting))) | |
816 | (put theme 'theme-settings (delq res theme-settings))) | |
817 | (put symbol prop (delq setting old))) | |
818 | (if setting | |
819 | ;; Alter an existing setting. | |
820 | (let (res) | |
821 | (dolist (theme-setting theme-settings) | |
822 | (if (and (eq (car theme-setting) prop) | |
823 | (eq (cadr theme-setting) symbol)) | |
824 | (setq res theme-setting))) | |
825 | (put theme 'theme-settings | |
d358aa10 | 826 | (cons (list prop symbol theme value) |
d820f1fb | 827 | (delq res theme-settings))) |
d358aa10 | 828 | (setcar (cdr setting) value)) |
d820f1fb CY |
829 | ;; Add a new setting. |
830 | ;; If the user changed the value outside of Customize, we | |
d358aa10 | 831 | ;; first save the current value to a fake theme, `changed'. |
d820f1fb CY |
832 | ;; This ensures that the user-set value comes back if the |
833 | ;; theme is later disabled. | |
834 | (if (null old) | |
835 | (if (and (eq prop 'theme-value) | |
e58d8ef5 CY |
836 | (boundp symbol)) |
837 | (let ((sv (get symbol 'standard-value))) | |
d54fbdfd SM |
838 | (unless (and sv |
839 | (equal (eval (car sv)) (symbol-value symbol))) | |
840 | (setq old (list (list 'changed (symbol-value symbol)))))) | |
e02577b7 CY |
841 | (if (and (facep symbol) |
842 | (not (face-spec-match-p symbol (get symbol 'face-defface-spec)))) | |
d358aa10 | 843 | (setq old (list (list 'changed (list |
d820f1fb | 844 | (append '(t) (custom-face-attributes-get symbol nil))))))))) |
d358aa10 | 845 | (put symbol prop (cons (list theme value) old)) |
d820f1fb | 846 | (put theme 'theme-settings |
d358aa10 | 847 | (cons (list prop symbol theme value) |
d820f1fb | 848 | theme-settings)))))) |
55535639 | 849 | |
c2e2f9be | 850 | \f |
55535639 | 851 | (defun custom-set-variables (&rest args) |
f1a262ed RS |
852 | "Install user customizations of variable values specified in ARGS. |
853 | These settings are registered as theme `user'. | |
64e9f9d9 | 854 | The arguments should each be a list of the form: |
55535639 | 855 | |
f1a262ed | 856 | (SYMBOL EXP [NOW [REQUEST [COMMENT]]]) |
55535639 | 857 | |
f1a262ed RS |
858 | This stores EXP (without evaluating it) as the saved value for SYMBOL. |
859 | If NOW is present and non-nil, then also evaluate EXP and set | |
860 | the default value for the SYMBOL to the value of EXP. | |
e1bab181 | 861 | |
f1a262ed RS |
862 | REQUEST is a list of features we must require in order to |
863 | handle SYMBOL properly. | |
55535639 | 864 | COMMENT is a comment string about SYMBOL." |
e1bab181 RS |
865 | (apply 'custom-theme-set-variables 'user args)) |
866 | ||
867 | (defun custom-theme-set-variables (theme &rest args) | |
f1a262ed RS |
868 | "Initialize variables for theme THEME according to settings in ARGS. |
869 | Each of the arguments in ARGS should be a list of this form: | |
e1bab181 | 870 | |
f1a262ed | 871 | (SYMBOL EXP [NOW [REQUEST [COMMENT]]]) |
e1bab181 | 872 | |
f1a262ed RS |
873 | This stores EXP (without evaluating it) as the saved value for SYMBOL. |
874 | If NOW is present and non-nil, then also evaluate EXP and set | |
875 | the default value for the SYMBOL to the value of EXP. | |
e1bab181 | 876 | |
f1a262ed RS |
877 | REQUEST is a list of features we must require in order to |
878 | handle SYMBOL properly. | |
e1bab181 RS |
879 | COMMENT is a comment string about SYMBOL. |
880 | ||
f1a262ed | 881 | EXP itself is saved unevaluated as SYMBOL property `saved-value' and |
e1bab181 RS |
882 | in SYMBOL's list property `theme-value' \(using `custom-push-theme')." |
883 | (custom-check-theme theme) | |
9277ee6c SM |
884 | |
885 | ;; Process all the needed autoloads before anything else, so that the | |
886 | ;; subsequent code has all the info it needs (e.g. which var corresponds | |
887 | ;; to a minor mode), regardless of the ordering of the variables. | |
888 | (dolist (entry args) | |
889 | (let* ((symbol (indirect-variable (nth 0 entry)))) | |
890 | (unless (or (get symbol 'standard-value) | |
891 | (memq (get symbol 'custom-autoload) '(nil noset))) | |
892 | ;; This symbol needs to be autoloaded, even just for a `set'. | |
893 | (custom-load-symbol symbol)))) | |
1e281896 | 894 | |
9277ee6c | 895 | ;; Move minor modes and variables with explicit requires to the end. |
059290d6 SM |
896 | (setq args |
897 | (sort args | |
898 | (lambda (a1 a2) | |
899 | (let* ((sym1 (car a1)) | |
900 | (sym2 (car a2)) | |
901 | (1-then-2 (memq sym1 (get sym2 'custom-dependencies))) | |
902 | (2-then-1 (memq sym2 (get sym1 'custom-dependencies)))) | |
903 | (cond ((and 1-then-2 2-then-1) | |
904 | (error "Circular custom dependency between `%s' and `%s'" | |
905 | sym1 sym2)) | |
906 | (2-then-1 nil) | |
3d0c2a29 GM |
907 | ;; 1 is a dependency of 2, so needs to be set first. |
908 | (1-then-2) | |
b6790c3e SM |
909 | ;; Put minor modes and symbols with :require last. |
910 | ;; Putting minor modes last ensures that the mode | |
911 | ;; function will see other customized values rather | |
912 | ;; than default values. | |
913 | (t (or (nth 3 a2) | |
914 | (eq (get sym2 'custom-set) | |
915 | 'custom-set-minor-mode)))))))) | |
059290d6 SM |
916 | (while args |
917 | (let ((entry (car args))) | |
918 | (if (listp entry) | |
919 | (let* ((symbol (indirect-variable (nth 0 entry))) | |
920 | (value (nth 1 entry)) | |
921 | (now (nth 2 entry)) | |
922 | (requests (nth 3 entry)) | |
923 | (comment (nth 4 entry)) | |
924 | set) | |
925 | (when requests | |
926 | (put symbol 'custom-requests requests) | |
927 | (mapc 'require requests)) | |
928 | (setq set (or (get symbol 'custom-set) 'custom-set-default)) | |
929 | (put symbol 'saved-value (list value)) | |
930 | (put symbol 'saved-variable-comment comment) | |
931 | (custom-push-theme 'theme-value symbol theme 'set value) | |
932 | ;; Allow for errors in the case where the setter has | |
55535639 PJ |
933 | ;; changed between versions, say, but let the user know. |
934 | (condition-case data | |
935 | (cond (now | |
936 | ;; Rogue variable, set it now. | |
937 | (put symbol 'force-value t) | |
938 | (funcall set symbol (eval value))) | |
939 | ((default-boundp symbol) | |
940 | ;; Something already set this, overwrite it. | |
941 | (funcall set symbol (eval value)))) | |
71296446 | 942 | (error |
55535639 | 943 | (message "Error setting %s: %s" symbol data))) |
059290d6 SM |
944 | (setq args (cdr args)) |
945 | (and (or now (default-boundp symbol)) | |
946 | (put symbol 'variable-comment comment))) | |
d54fbdfd SM |
947 | ;; I believe this is dead-code, because the `sort' code above would |
948 | ;; have burped before we could get here. --Stef | |
059290d6 SM |
949 | ;; Old format, a plist of SYMBOL VALUE pairs. |
950 | (message "Warning: old format `custom-set-variables'") | |
951 | (ding) | |
952 | (sit-for 2) | |
953 | (let ((symbol (indirect-variable (nth 0 args))) | |
954 | (value (nth 1 args))) | |
955 | (put symbol 'saved-value (list value)) | |
956 | (custom-push-theme 'theme-value symbol theme 'set value)) | |
957 | (setq args (cdr (cdr args))))))) | |
55535639 | 958 | |
46ce5feb RS |
959 | \f |
960 | ;;; Defining themes. | |
961 | ||
d358aa10 CY |
962 | ;; A theme file should be named `THEME-theme.el' (where THEME is the theme |
963 | ;; name), and found in either `custom-theme-directory' or the load path. | |
964 | ;; It has the following format: | |
965 | ;; | |
966 | ;; (deftheme THEME | |
967 | ;; DOCSTRING) | |
968 | ;; | |
969 | ;; (custom-theme-set-variables | |
970 | ;; 'THEME | |
971 | ;; [THEME-VARIABLES]) | |
972 | ;; | |
973 | ;; (custom-theme-set-faces | |
974 | ;; 'THEME | |
975 | ;; [THEME-FACES]) | |
976 | ;; | |
977 | ;; (provide-theme 'THEME) | |
46ce5feb | 978 | |
46ce5feb | 979 | |
d358aa10 CY |
980 | ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where |
981 | ;; they were used to supply keyword-value pairs like `:immediate', | |
982 | ;; `:variable-reset-string', etc. We don't use any of these, so ignore them. | |
6d912ee1 | 983 | |
d358aa10 CY |
984 | (defmacro deftheme (theme &optional doc &rest ignored) |
985 | "Declare THEME to be a Custom theme. | |
986 | The optional argument DOC is a doc string describing the theme. | |
46ce5feb RS |
987 | |
988 | Any theme `foo' should be defined in a file called `foo-theme.el'; | |
989 | see `custom-make-theme-feature' for more information." | |
990 | (let ((feature (custom-make-theme-feature theme))) | |
991 | ;; It is better not to use backquote in this file, | |
992 | ;; because that makes a bootstrapping problem | |
993 | ;; if you need to recompile all the Lisp files using interpreted code. | |
d358aa10 | 994 | (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc))) |
46ce5feb | 995 | |
d358aa10 | 996 | (defun custom-declare-theme (theme feature &optional doc &rest ignored) |
46ce5feb | 997 | "Like `deftheme', but THEME is evaluated as a normal argument. |
d358aa10 CY |
998 | FEATURE is the feature this theme provides. Normally, this is a symbol |
999 | created from THEME by `custom-make-theme-feature'." | |
1000 | (if (memq theme '(user changed)) | |
1001 | (error "Custom theme cannot be named %S" theme)) | |
46ce5feb RS |
1002 | (add-to-list 'custom-known-themes theme) |
1003 | (put theme 'theme-feature feature) | |
d358aa10 | 1004 | (when doc (put theme 'theme-documentation doc))) |
46ce5feb RS |
1005 | |
1006 | (defun custom-make-theme-feature (theme) | |
1007 | "Given a symbol THEME, create a new symbol by appending \"-theme\". | |
1008 | Store this symbol in the `theme-feature' property of THEME. | |
1009 | Calling `provide-theme' to provide THEME actually puts `THEME-theme' | |
1010 | into `features'. | |
1011 | ||
1012 | This allows for a file-name convention for autoloading themes: | |
1013 | Every theme X has a property `provide-theme' whose value is \"X-theme\". | |
87d737ae | 1014 | \(load-theme X) then attempts to load the file `X-theme.el'." |
46ce5feb RS |
1015 | (intern (concat (symbol-name theme) "-theme"))) |
1016 | \f | |
1017 | ;;; Loading themes. | |
1018 | ||
9c4b6e94 | 1019 | (defcustom custom-theme-directory |
63212d45 | 1020 | user-emacs-directory |
9c4b6e94 | 1021 | "Directory in which Custom theme files should be written. |
87d737ae | 1022 | `load-theme' searches this directory in addition to load-path. |
9c4b6e94 LT |
1023 | The command `customize-create-theme' writes the files it produces |
1024 | into this directory." | |
1025 | :type 'string | |
1026 | :group 'customize | |
1027 | :version "22.1") | |
1028 | ||
e1bab181 | 1029 | (defun provide-theme (theme) |
d358aa10 CY |
1030 | "Indicate that this file provides THEME. |
1031 | This calls `provide' to provide the feature name stored in THEME's | |
1032 | property `theme-feature' (which is usually a symbol created by | |
1033 | `custom-make-theme-feature')." | |
1034 | (if (memq theme '(user changed)) | |
1035 | (error "Custom theme cannot be named %S" theme)) | |
e1bab181 RS |
1036 | (custom-check-theme theme) |
1037 | (provide (get theme 'theme-feature)) | |
d358aa10 | 1038 | ;; Loading a theme also enables it. |
46ce5feb RS |
1039 | (push theme custom-enabled-themes) |
1040 | ;; `user' must always be the highest-precedence enabled theme. | |
1041 | ;; Make that remain true. (This has the effect of making user settings | |
1042 | ;; override the ones just loaded, too.) | |
d358aa10 CY |
1043 | (let ((custom-enabling-themes t)) |
1044 | (enable-theme 'user))) | |
e1bab181 | 1045 | |
87d737ae | 1046 | (defun load-theme (theme) |
73e60f53 | 1047 | "Load a theme's settings from its file. |
87d737ae | 1048 | This also enables the theme; use `disable-theme' to disable it." |
e1bab181 RS |
1049 | ;; Note we do no check for validity of the theme here. |
1050 | ;; This allows to pull in themes by a file-name convention | |
87d737ae | 1051 | (interactive "SCustom theme name: ") |
73e60f53 CY |
1052 | ;; If reloading, clear out the old theme settings. |
1053 | (when (custom-theme-p theme) | |
1054 | (disable-theme theme) | |
1055 | (put theme 'theme-settings nil) | |
1056 | (put theme 'theme-feature nil) | |
1057 | (put theme 'theme-documentation nil)) | |
9c4b6e94 LT |
1058 | (let ((load-path (if (file-directory-p custom-theme-directory) |
1059 | (cons custom-theme-directory load-path) | |
1060 | load-path))) | |
d358aa10 | 1061 | (load (symbol-name (custom-make-theme-feature theme))))) |
46ce5feb RS |
1062 | \f |
1063 | ;;; Enabling and disabling loaded themes. | |
1064 | ||
b2a41d12 CY |
1065 | (defvar custom-enabling-themes nil) |
1066 | ||
87d737ae | 1067 | (defun enable-theme (theme) |
46ce5feb RS |
1068 | "Reenable all variable and face settings defined by THEME. |
1069 | The newly enabled theme gets the highest precedence (after `user'). | |
e335f09e CY |
1070 | If it is already enabled, just give it highest precedence (after `user'). |
1071 | ||
d358aa10 CY |
1072 | If THEME does not specify any theme settings, this tries to load |
1073 | the theme from its theme file, by calling `load-theme'." | |
87d737ae | 1074 | (interactive "SEnable Custom theme: ") |
d358aa10 CY |
1075 | (if (not (custom-theme-p theme)) |
1076 | (load-theme theme) | |
1077 | ;; This could use a bit of optimization -- cyd | |
1078 | (let ((settings (get theme 'theme-settings))) | |
1079 | (dolist (s settings) | |
1080 | (let* ((prop (car s)) | |
1081 | (symbol (cadr s)) | |
1082 | (spec-list (get symbol prop))) | |
1083 | (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list))) | |
1084 | (if (eq prop 'theme-value) | |
1085 | (custom-theme-recalc-variable symbol) | |
1086 | (custom-theme-recalc-face symbol))))) | |
1087 | (unless (eq theme 'user) | |
1088 | (setq custom-enabled-themes | |
1089 | (cons theme (delq theme custom-enabled-themes))) | |
1090 | (unless custom-enabling-themes | |
1091 | (enable-theme 'user))))) | |
b2a41d12 CY |
1092 | |
1093 | (defcustom custom-enabled-themes nil | |
1094 | "List of enabled Custom Themes, highest precedence first. | |
1095 | ||
1096 | This does not include the `user' theme, which is set by Customize, | |
1097 | and always takes precedence over other Custom Themes." | |
1098 | :group 'customize | |
1099 | :type '(repeat symbol) | |
3d0c2a29 | 1100 | :set-after '(custom-theme-directory) ; so we can find the themes |
b2a41d12 CY |
1101 | :set (lambda (symbol themes) |
1102 | ;; Avoid an infinite loop when custom-enabled-themes is | |
1103 | ;; defined in a theme (e.g. `user'). Enabling the theme sets | |
1104 | ;; custom-enabled-themes, which enables the theme... | |
1105 | (unless custom-enabling-themes | |
d358aa10 | 1106 | (let ((custom-enabling-themes t) failures) |
b2a41d12 CY |
1107 | (setq themes (delq 'user (delete-dups themes))) |
1108 | (if (boundp symbol) | |
1109 | (dolist (theme (symbol-value symbol)) | |
1110 | (if (not (memq theme themes)) | |
1111 | (disable-theme theme)))) | |
1112 | (dolist (theme (reverse themes)) | |
d358aa10 | 1113 | (condition-case nil |
b2a41d12 | 1114 | (enable-theme theme) |
d358aa10 CY |
1115 | (error (progn (push theme failures) |
1116 | (setq themes (delq theme themes)))))) | |
b2a41d12 | 1117 | (enable-theme 'user) |
d358aa10 CY |
1118 | (custom-set-default symbol themes) |
1119 | (if failures | |
1120 | (message "Failed to enable themes: %s" | |
1121 | (mapconcat 'symbol-name failures " "))))))) | |
b2a41d12 | 1122 | |
d358aa10 | 1123 | (defsubst custom-theme-enabled-p (theme) |
b2a41d12 CY |
1124 | "Return non-nil if THEME is enabled." |
1125 | (memq theme custom-enabled-themes)) | |
46ce5feb | 1126 | |
87d737ae | 1127 | (defun disable-theme (theme) |
46ce5feb | 1128 | "Disable all variable and face settings defined by THEME. |
b2a41d12 | 1129 | See `custom-enabled-themes' for a list of enabled themes." |
d358aa10 CY |
1130 | (interactive (list (intern |
1131 | (completing-read | |
1132 | "Disable Custom theme: " | |
1133 | (mapcar 'symbol-name custom-enabled-themes) | |
1134 | nil t)))) | |
1135 | (when (custom-theme-enabled-p theme) | |
8913f945 RS |
1136 | (let ((settings (get theme 'theme-settings))) |
1137 | (dolist (s settings) | |
1138 | (let* ((prop (car s)) | |
1139 | (symbol (cadr s)) | |
1140 | (spec-list (get symbol prop))) | |
1141 | (put symbol prop (assq-delete-all theme spec-list)) | |
1142 | (if (eq prop 'theme-value) | |
1143 | (custom-theme-recalc-variable symbol) | |
b2a41d12 CY |
1144 | (custom-theme-recalc-face symbol))))) |
1145 | (setq custom-enabled-themes | |
1146 | (delq theme custom-enabled-themes)))) | |
46ce5feb | 1147 | |
46ce5feb RS |
1148 | (defun custom-variable-theme-value (variable) |
1149 | "Return (list VALUE) indicating the custom theme value of VARIABLE. | |
1150 | That is to say, it specifies what the value should be according to | |
1151 | currently enabled custom themes. | |
1152 | ||
1153 | This function returns nil if no custom theme specifies a value for VARIABLE." | |
1154 | (let* ((theme-value (get variable 'theme-value))) | |
1155 | (if theme-value | |
d358aa10 | 1156 | (cdr (car theme-value))))) |
46ce5feb | 1157 | |
46ce5feb RS |
1158 | (defun custom-theme-recalc-variable (variable) |
1159 | "Set VARIABLE according to currently enabled custom themes." | |
1160 | (let ((valspec (custom-variable-theme-value variable))) | |
d358aa10 CY |
1161 | (if valspec |
1162 | (put variable 'saved-value valspec) | |
46ce5feb | 1163 | (setq valspec (get variable 'standard-value))) |
d358aa10 CY |
1164 | (if (and valspec |
1165 | (or (get variable 'force-value) | |
1166 | (default-boundp variable))) | |
1167 | (funcall (or (get variable 'custom-set) 'set-default) variable | |
1168 | (eval (car valspec)))))) | |
46ce5feb RS |
1169 | |
1170 | (defun custom-theme-recalc-face (face) | |
1171 | "Set FACE according to currently enabled custom themes." | |
b2a41d12 | 1172 | (if (facep face) |
279c4408 GM |
1173 | (face-spec-set face |
1174 | (get (or (get face 'face-alias) face) | |
1175 | 'face-override-spec)))) | |
46ce5feb | 1176 | \f |
d358aa10 CY |
1177 | ;;; XEmacs compability functions |
1178 | ||
1179 | ;; In XEmacs, when you reset a Custom Theme, you have to specify the | |
1180 | ;; theme to reset it to. We just apply the next available theme, so | |
1181 | ;; just ignore the IGNORED arguments. | |
1182 | ||
e1bab181 | 1183 | (defun custom-theme-reset-variables (theme &rest args) |
d358aa10 | 1184 | "Reset some variable settings in THEME to their values in other themes. |
46ce5feb | 1185 | Each of the arguments ARGS has this form: |
e1bab181 | 1186 | |
d358aa10 | 1187 | (VARIABLE IGNORED) |
e1bab181 | 1188 | |
7e8b9dc3 | 1189 | This means reset VARIABLE. (The argument IGNORED is ignored)." |
e1bab181 | 1190 | (custom-check-theme theme) |
46ce5feb | 1191 | (dolist (arg args) |
d358aa10 | 1192 | (custom-push-theme 'theme-value (car arg) theme 'reset))) |
e1bab181 RS |
1193 | |
1194 | (defun custom-reset-variables (&rest args) | |
d358aa10 | 1195 | "Reset the specs of some variables to their values in other themes. |
46ce5feb | 1196 | This creates settings in the `user' theme. |
e1bab181 | 1197 | |
46ce5feb | 1198 | Each of the arguments ARGS has this form: |
e1bab181 | 1199 | |
d358aa10 | 1200 | (VARIABLE IGNORED) |
e1bab181 | 1201 | |
7e8b9dc3 | 1202 | This means reset VARIABLE. (The argument IGNORED is ignored)." |
e1bab181 RS |
1203 | (apply 'custom-theme-reset-variables 'user args)) |
1204 | ||
55535639 PJ |
1205 | ;;; The End. |
1206 | ||
1207 | ;; Process the defcustoms for variables loaded before this file. | |
1208 | (while custom-declare-variable-list | |
1209 | (apply 'custom-declare-variable (car custom-declare-variable-list)) | |
1210 | (setq custom-declare-variable-list (cdr custom-declare-variable-list))) | |
1211 | ||
1212 | (provide 'custom) | |
1213 | ||
059290d6 | 1214 | ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2 |
55535639 | 1215 | ;;; custom.el ends here |