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