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