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