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