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