In scheme-mode symbol regexp, disallow backquote and
[bpt/emacs.git] / lisp / custom.el
CommitLineData
55535639
PJ
1;;; custom.el --- tools for declaring and initializing options
2;;
00644b82 3;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc.
55535639
PJ
4;;
5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6;; Maintainer: FSF
7;; Keywords: help, faces
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27;;
e6bb31f3 28;; This file only contains the code needed to declare and initialize
55535639
PJ
29;; user options. The code to customize options is autoloaded from
30;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
31
e6bb31f3 32;; The code implementing face declarations is in `cus-face.el'.
55535639
PJ
33
34;;; Code:
35
36(require 'widget)
37
38(defvar custom-define-hook nil
39 ;; Customize information for this option is in `cus-edit.el'.
40 "Hook called after defining each customize option.")
41
6da43544
RS
42(defvar custom-dont-initialize nil
43 "Non-nil means `defcustom' should not initialize the variable.
44That is used for the sake of `custom-make-dependencies'.
45Users should not set it.")
46
d3b80e9b
SM
47(defvar custom-current-group-alist nil
48 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
49
55535639
PJ
50;;; The `defcustom' Macro.
51
52(defun custom-initialize-default (symbol value)
53 "Initialize SYMBOL with VALUE.
54This will do nothing if symbol already has a default binding.
55Otherwise, if symbol has a `saved-value' property, it will evaluate
56the car of that and used as the default binding for symbol.
57Otherwise, VALUE will be evaluated and used as the default binding for
58symbol."
59 (unless (default-boundp symbol)
60 ;; Use the saved value if it exists, otherwise the standard setting.
61 (set-default symbol (if (get symbol 'saved-value)
62 (eval (car (get symbol 'saved-value)))
63 (eval value)))))
64
65(defun custom-initialize-set (symbol value)
66 "Initialize SYMBOL based on VALUE.
67If the symbol doesn't have a default binding already,
68then set it using its `:set' function (or `set-default' if it has none).
69The value is either the value in the symbol's `saved-value' property,
70if any, or VALUE."
71 (unless (default-boundp symbol)
72 (funcall (or (get symbol 'custom-set) 'set-default)
73 symbol
74 (if (get symbol 'saved-value)
75 (eval (car (get symbol 'saved-value)))
76 (eval value)))))
77
78(defun custom-initialize-reset (symbol value)
79 "Initialize SYMBOL based on VALUE.
80Set the symbol, using its `:set' function (or `set-default' if it has none).
81The value is either the symbol's current value
82 \(as obtained using the `:get' function), if any,
83or the value in the symbol's `saved-value' property if any,
84or (last of all) VALUE."
85 (funcall (or (get symbol 'custom-set) 'set-default)
86 symbol
87 (cond ((default-boundp symbol)
88 (funcall (or (get symbol 'custom-get) 'default-value)
89 symbol))
90 ((get symbol 'saved-value)
91 (eval (car (get symbol 'saved-value))))
92 (t
93 (eval value)))))
94
95(defun custom-initialize-changed (symbol value)
96 "Initialize SYMBOL with VALUE.
97Like `custom-initialize-reset', but only use the `:set' function if
98not using the standard setting.
99For the standard setting, use `set-default'."
100 (cond ((default-boundp symbol)
101 (funcall (or (get symbol 'custom-set) 'set-default)
102 symbol
103 (funcall (or (get symbol 'custom-get) 'default-value)
104 symbol)))
105 ((get symbol 'saved-value)
106 (funcall (or (get symbol 'custom-set) 'set-default)
107 symbol
108 (eval (car (get symbol 'saved-value)))))
109 (t
110 (set-default symbol (eval value)))))
111
112(defun custom-declare-variable (symbol default doc &rest args)
113 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
114DEFAULT should be an expression to evaluate to compute the default value,
e1bab181
RS
115not the default value itself.
116
117DEFAULT is stored as SYMBOL's value in the standard theme. See
118`custom-known-themes' for a list of known themes. For backwards
119compatibility, DEFAULT is also stored in SYMBOL's property
120`standard-value'. At the same time, SYMBOL's property `force-value' is
121set to nil, as the value is no longer rogue."
122 ;; Remember the standard setting. The value should be in the standard
123 ;; theme, not in this property. However, his would require changeing
124 ;; the C source of defvar and others as well...
55535639
PJ
125 (put symbol 'standard-value (list default))
126 ;; Maybe this option was rogue in an earlier version. It no longer is.
127 (when (get symbol 'force-value)
128 (put symbol 'force-value nil))
129 (when doc
130 (put symbol 'variable-documentation doc))
131 (let ((initialize 'custom-initialize-reset)
132 (requests nil))
d3b80e9b
SM
133 (unless (memq :group args)
134 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
55535639
PJ
135 (while args
136 (let ((arg (car args)))
137 (setq args (cdr args))
138 (unless (symbolp arg)
139 (error "Junk in args %S" args))
140 (let ((keyword arg)
141 (value (car args)))
142 (unless args
143 (error "Keyword %s is missing an argument" keyword))
144 (setq args (cdr args))
145 (cond ((eq keyword :initialize)
146 (setq initialize value))
147 ((eq keyword :set)
148 (put symbol 'custom-set value))
149 ((eq keyword :get)
150 (put symbol 'custom-get value))
151 ((eq keyword :require)
6f0d61bf 152 (push value requests))
55535639
PJ
153 ((eq keyword :type)
154 (put symbol 'custom-type (purecopy value)))
155 ((eq keyword :options)
156 (if (get symbol 'custom-options)
157 ;; Slow safe code to avoid duplicates.
158 (mapc (lambda (option)
159 (custom-add-option symbol option))
6f0d61bf 160 value)
55535639
PJ
161 ;; Fast code for the common case.
162 (put symbol 'custom-options (copy-sequence value))))
163 (t
164 (custom-handle-keyword symbol keyword value
165 'custom-variable))))))
166 (put symbol 'custom-requests requests)
167 ;; Do the actual initialization.
6da43544
RS
168 (unless custom-dont-initialize
169 (funcall initialize symbol default)))
6f0d61bf 170 (push (cons 'defvar symbol) current-load-list)
55535639
PJ
171 (run-hooks 'custom-define-hook)
172 symbol)
173
174(defmacro defcustom (symbol value doc &rest args)
175 "Declare SYMBOL as a customizable variable that defaults to VALUE.
176DOC is the variable documentation.
177
178Neither SYMBOL nor VALUE needs to be quoted.
179If SYMBOL is not already bound, initialize it to VALUE.
180The remaining arguments should have the form
181
182 [KEYWORD VALUE]...
183
184The following keywords are meaningful:
185
693d0bd3 186:type VALUE should be a widget type for editing the symbol's value.
55535639
PJ
187:options VALUE should be a list of valid members of the widget type.
188:group VALUE should be a customization group.
189 Add SYMBOL to that group.
3dc5f18e 190:link LINK-DATA
00644b82
EZ
191 Include an external link after the documentation string for this
192 item. This is a sentence containing an active field which
193 references some other documentation.
71296446 194
00644b82 195 There are three alternatives you can use for LINK-DATA:
71296446 196
3dc5f18e 197 (custom-manual INFO-NODE)
00644b82 198 Link to an Info node; INFO-NODE is a string which specifies
3dc5f18e 199 the node name, as in \"(emacs)Top\". The link appears as
00644b82 200 `[manual]' in the customization buffer.
71296446 201
3dc5f18e 202 (info-link INFO-NODE)
00644b82
EZ
203 Like `custom-manual' except that the link appears in the
204 customization buffer with the Info node name.
71296446 205
3dc5f18e 206 (url-link URL)
00644b82
EZ
207 Link to a web page; URL is a string which specifies the URL.
208 The link appears in the customization buffer as URL.
71296446 209
00644b82
EZ
210 You can specify the text to use in the customization buffer by
211 adding `:tag NAME' after the first element of the LINK-DATA; for
3dc5f18e 212 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
00644b82 213 Emacs manual which appears in the buffer as `foo'.
71296446 214
00644b82
EZ
215 An item can have more than one external link; however, most items
216 have none at all.
55535639
PJ
217:initialize
218 VALUE should be a function used to initialize the
219 variable. It takes two arguments, the symbol and value
220 given in the `defcustom' call. The default is
a01639a3 221 `custom-initialize-reset'.
55535639
PJ
222:set VALUE should be a function to set the value of the symbol.
223 It takes two arguments, the symbol to set and the value to
224 give it. The default choice of function is `custom-set-default'.
225:get VALUE should be a function to extract the value of symbol.
226 The function takes one argument, a symbol, and should return
227 the current value for that symbol. The default choice of function
228 is `custom-default-value'.
229:require
230 VALUE should be a feature symbol. If you save a value
231 for this option, then when your `.emacs' file loads the value,
232 it does (require VALUE) first.
233:version
234 VALUE should be a string specifying that the variable was
235 first introduced, or its default value was changed, in Emacs
236 version VERSION.
00644b82
EZ
237:tag LABEL
238 Use LABEL, a string, instead of the item's name, to label the item
1c1da418 239 in customization menus and buffers.
00644b82
EZ
240:load FILE
241 Load file FILE (a string) before displaying this customization
29512a0f 242 item. Loading is done with `load', and only if the file is
00644b82 243 not already loaded.
64e9f9d9
DL
244:set-after VARIABLES
245 Specifies that SYMBOL should be set after the list of variables
246 VARIABLES when both have been customized.
55535639
PJ
247
248Read the section about customization in the Emacs Lisp manual for more
249information."
250 ;; It is better not to use backquote in this file,
251 ;; because that makes a bootstrapping problem
252 ;; if you need to recompile all the Lisp files using interpreted code.
253 (nconc (list 'custom-declare-variable
254 (list 'quote symbol)
255 (list 'quote value)
256 doc)
257 args))
258
259;;; The `defface' Macro.
260
261(defmacro defface (face spec doc &rest args)
262 "Declare FACE as a customizable face that defaults to SPEC.
263FACE does not need to be quoted.
264
265Third argument DOC is the face documentation.
266
2a69edce 267If FACE has been set with `custom-set-faces', set the face attributes
55535639
PJ
268as specified by that function, otherwise set the face attributes
269according to SPEC.
270
271The remaining arguments should have the form
272
273 [KEYWORD VALUE]...
274
275The following KEYWORDs are defined:
276
277:group VALUE should be a customization group.
278 Add FACE to that group.
279
280SPEC should be an alist of the form ((DISPLAY ATTS)...).
281
282The first element of SPEC where the DISPLAY matches the frame
283is the one that takes effect in that frame. The ATTRs in this
284element take effect; the other elements are ignored, on that frame.
285
286ATTS is a list of face attributes followed by their values:
287 (ATTR VALUE ATTR VALUE...)
288
289The possible attributes are `:family', `:width', `:height', `:weight',
290`:slant', `:underline', `:overline', `:strike-through', `:box',
3d58b15e 291`:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
55535639
PJ
292
293DISPLAY can either be the symbol t, which will match all frames, or an
294alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
295FRAME, the REQ property of the frame must match one of the ITEM. The
296following REQ are defined:
297
298`type' (the value of `window-system')
299 Under X, in addition to the values `window-system' can take,
300 `motif', `lucid' and `x-toolkit' are allowed, and match when
301 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
302
303`class' (the frame's color support)
304 Should be one of `color', `grayscale', or `mono'.
305
306`background' (what color is used for the background text)
307 Should be one of `light' or `dark'.
308
6e166990
EZ
309`min-colors' (the minimum number of colors the frame should support)
310 Should be an integer, it is compared with the result of
311 `display-color-cells'.
312
55535639
PJ
313Read the section about customization in the Emacs Lisp manual for more
314information."
315 ;; It is better not to use backquote in this file,
316 ;; because that makes a bootstrapping problem
317 ;; if you need to recompile all the Lisp files using interpreted code.
318 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
319
320;;; The `defgroup' Macro.
321
d3b80e9b
SM
322(defun custom-current-group ()
323 (cdr (assoc load-file-name custom-current-group-alist)))
324
55535639
PJ
325(defun custom-declare-group (symbol members doc &rest args)
326 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
327 (while members
328 (apply 'custom-add-to-group symbol (car members))
329 (setq members (cdr members)))
55535639
PJ
330 (when doc
331 ;; This text doesn't get into DOC.
332 (put symbol 'group-documentation (purecopy doc)))
333 (while args
334 (let ((arg (car args)))
335 (setq args (cdr args))
336 (unless (symbolp arg)
337 (error "Junk in args %S" args))
338 (let ((keyword arg)
339 (value (car args)))
340 (unless args
341 (error "Keyword %s is missing an argument" keyword))
342 (setq args (cdr args))
343 (cond ((eq keyword :prefix)
344 (put symbol 'custom-prefix value))
345 (t
346 (custom-handle-keyword symbol keyword value
347 'custom-group))))))
d3b80e9b
SM
348 ;; Record the group on the `current' list.
349 (let ((elt (assoc load-file-name custom-current-group-alist)))
350 (if elt (setcdr elt symbol)
351 (push (cons load-file-name symbol) custom-current-group-alist)))
55535639
PJ
352 (run-hooks 'custom-define-hook)
353 symbol)
354
355(defmacro defgroup (symbol members doc &rest args)
356 "Declare SYMBOL as a customization group containing MEMBERS.
357SYMBOL does not need to be quoted.
358
359Third arg DOC is the group documentation.
360
361MEMBERS should be an alist of the form ((NAME WIDGET)...) where
362NAME is a symbol and WIDGET is a widget for editing that symbol.
363Useful widgets are `custom-variable' for editing variables,
364`custom-face' for edit faces, and `custom-group' for editing groups.
365
366The remaining arguments should have the form
367
368 [KEYWORD VALUE]...
369
370The following KEYWORDs are defined:
371
372:group VALUE should be a customization group.
373 Add SYMBOL to that group.
374
375:version VALUE should be a string specifying that the group was introduced
376 in Emacs version VERSION.
377
378Read the section about customization in the Emacs Lisp manual for more
379information."
380 ;; It is better not to use backquote in this file,
381 ;; because that makes a bootstrapping problem
382 ;; if you need to recompile all the Lisp files using interpreted code.
383 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
384
385(defun custom-add-to-group (group option widget)
386 "To existing GROUP add a new OPTION of type WIDGET.
387If there already is an entry for OPTION and WIDGET, nothing is done."
388 (let ((members (get group 'custom-group))
389 (entry (list option widget)))
390 (unless (member entry members)
391 (put group 'custom-group (nconc members (list entry))))))
392
29512a0f
SM
393(defun custom-group-of-mode (mode)
394 "Return the custom group corresponding to the major or minor MODE.
395If no such group is found, return nil."
396 (or (get mode 'custom-mode-group)
397 (if (or (get mode 'custom-group)
398 (and (string-match "-mode\\'" (symbol-name mode))
399 (get (setq mode (intern (substring (symbol-name mode)
400 0 (match-beginning 0))))
401 'custom-group)))
402 mode)))
403
55535639
PJ
404;;; Properties.
405
406(defun custom-handle-all-keywords (symbol args type)
407 "For customization option SYMBOL, handle keyword arguments ARGS.
408Third argument TYPE is the custom option type."
d3b80e9b 409 (unless (memq :group args)
9b6098b9 410 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
411 (while args
412 (let ((arg (car args)))
413 (setq args (cdr args))
414 (unless (symbolp arg)
415 (error "Junk in args %S" args))
416 (let ((keyword arg)
417 (value (car args)))
418 (unless args
419 (error "Keyword %s is missing an argument" keyword))
420 (setq args (cdr args))
421 (custom-handle-keyword symbol keyword value type)))))
422
423(defun custom-handle-keyword (symbol keyword value type)
424 "For customization option SYMBOL, handle KEYWORD with VALUE.
425Fourth argument TYPE is the custom option type."
426 (if purify-flag
427 (setq value (purecopy value)))
428 (cond ((eq keyword :group)
429 (custom-add-to-group value symbol type))
430 ((eq keyword :version)
431 (custom-add-version symbol value))
432 ((eq keyword :link)
433 (custom-add-link symbol value))
434 ((eq keyword :load)
435 (custom-add-load symbol value))
436 ((eq keyword :tag)
437 (put symbol 'custom-tag value))
438 ((eq keyword :set-after)
439 (custom-add-dependencies symbol value))
440 (t
441 (error "Unknown keyword %s" keyword))))
442
443(defun custom-add-dependencies (symbol value)
444 "To the custom option SYMBOL, add dependencies specified by VALUE.
445VALUE should be a list of symbols. For each symbol in that list,
446this specifies that SYMBOL should be set after the specified symbol, if
447both appear in constructs like `custom-set-variables'."
448 (unless (listp value)
449 (error "Invalid custom dependency `%s'" value))
450 (let* ((deps (get symbol 'custom-dependencies))
451 (new-deps deps))
452 (while value
453 (let ((dep (car value)))
454 (unless (symbolp dep)
455 (error "Invalid custom dependency `%s'" dep))
456 (unless (memq dep new-deps)
457 (setq new-deps (cons dep new-deps)))
458 (setq value (cdr value))))
459 (unless (eq deps new-deps)
460 (put symbol 'custom-dependencies new-deps))))
e1bab181 461
55535639
PJ
462(defun custom-add-option (symbol option)
463 "To the variable SYMBOL add OPTION.
464
465If SYMBOL is a hook variable, OPTION should be a hook member.
466For other types variables, the effect is undefined."
467 (let ((options (get symbol 'custom-options)))
468 (unless (member option options)
469 (put symbol 'custom-options (cons option options)))))
470
471(defun custom-add-link (symbol widget)
472 "To the custom option SYMBOL add the link WIDGET."
473 (let ((links (get symbol 'custom-links)))
474 (unless (member widget links)
475 (put symbol 'custom-links (cons (purecopy widget) links)))))
476
477(defun custom-add-version (symbol version)
478 "To the custom option SYMBOL add the version VERSION."
479 (put symbol 'custom-version (purecopy version)))
480
481(defun custom-add-load (symbol load)
482 "To the custom option SYMBOL add the dependency LOAD.
483LOAD should be either a library file name, or a feature name."
484 (let ((loads (get symbol 'custom-loads)))
485 (unless (member load loads)
486 (put symbol 'custom-loads (cons (purecopy load) loads)))))
487
1669290d
MR
488(defun custom-autoload (symbol load)
489 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
490 (put symbol 'custom-autoload t)
491 (custom-add-load symbol load))
492
493;; This test is also in the C code of `user-variable-p'.
494(defun custom-variable-p (variable)
495 "Return non-nil if VARIABLE is a custom variable."
496 (or (get variable 'standard-value)
497 (get variable 'custom-autoload)))
498
8f772dfd
RS
499;;; Loading files needed to customize a symbol.
500;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
501
502(defvar custom-load-recursion nil
503 "Hack to avoid recursive dependencies.")
504
505(defun custom-load-symbol (symbol)
506 "Load all dependencies for SYMBOL."
507 (unless custom-load-recursion
29512a0f 508 (let ((custom-load-recursion t))
a8c78057
RS
509 ;; Load these files if not already done,
510 ;; to make sure we know all the dependencies of SYMBOL.
511 (condition-case nil
512 (require 'cus-load)
513 (error nil))
514 (condition-case nil
515 (require 'cus-start)
516 (error nil))
29512a0f
SM
517 (dolist (load (get symbol 'custom-loads))
518 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
519 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
520 ((assoc load load-history))
521 ;; This was just (assoc (locate-library load) load-history)
522 ;; but has been optimized not to load locate-library
523 ;; if not necessary.
29512a0f
SM
524 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
525 "\\(\\'\\|\\.\\)"))
526 (found nil))
8f772dfd 527 (dolist (loaded load-history)
c3891447
RS
528 (and (stringp (car loaded))
529 (string-match regexp (car loaded))
8f772dfd
RS
530 (setq found t)))
531 found))
532 ;; Without this, we would load cus-edit recursively.
533 ;; We are still loading it when we call this,
534 ;; and it is not in load-history yet.
535 ((equal load "cus-edit"))
29512a0f 536 (t (condition-case nil (load load) (error nil))))))))
8f772dfd 537
e1bab181
RS
538(defvar custom-known-themes '(user standard)
539 "Themes that have been define with `deftheme'.
540The default value is the list (user standard). The theme `standard'
541contains the Emacs standard settings from the original Lisp files. The
542theme `user' contains all the the settings the user customized and saved.
543Additional themes declared with the `deftheme' macro will be added to
544the front of this list.")
545
546(defun custom-declare-theme (theme feature &optional doc &rest args)
547 "Like `deftheme', but THEME is evaluated as a normal argument.
548FEATURE is the feature this theme provides. This symbol is created
549from THEME by `custom-make-theme-feature'."
550 (add-to-list 'custom-known-themes theme)
551 (put theme 'theme-feature feature)
552 (when doc
553 (put theme 'theme-documentation doc))
554 (while args
555 (let ((arg (car args)))
556 (setq args (cdr args))
557 (unless (symbolp arg)
558 (error "Junk in args %S" args))
559 (let ((keyword arg)
560 (value (car args)))
561 (unless args
562 (error "Keyword %s is missing an argument" keyword))
563 (setq args (cdr args))
564 (cond ((eq keyword :short-description)
7f9b6322 565 (put theme 'theme-short-description value))
e1bab181 566 ((eq keyword :immediate)
7f9b6322 567 (put theme 'theme-immediate value))
e1bab181 568 ((eq keyword :variable-set-string)
7f9b6322 569 (put theme 'theme-variable-set-string value))
e1bab181 570 ((eq keyword :variable-reset-string)
7f9b6322 571 (put theme 'theme-variable-reset-string value))
e1bab181 572 ((eq keyword :face-set-string)
7f9b6322 573 (put theme 'theme-face-set-string value))
e1bab181 574 ((eq keyword :face-reset-string)
7f9b6322 575 (put theme 'theme-face-reset-string value)))))))
e1bab181
RS
576
577(defmacro deftheme (theme &optional doc &rest args)
578 "Declare custom theme THEME.
579The optional argument DOC is a doc string describing the theme.
580The remaining arguments should have the form
581
582 [KEYWORD VALUE]...
583
584The following KEYWORD's are defined:
585
586:short-description
587 VALUE is a short (one line) description of the theme. If not
588 given, DOC is used.
589:immediate
590 If VALUE is non-nil, variables specified in this theme are set
591 immediately when loading the theme.
592:variable-set-string
593 VALUE is a string used to indicate that a variable takes its
594 setting from this theme. It is passed to FORMAT with the name
595 of the theme as an additional argument. If not given, a
596 generic description is used.
597:variable-reset-string
598 VALUE is a string used in the case a variable has been forced
599 to its value in this theme. It is passed to FORMAT with the
600 name of the theme as an additional argument. If not given, a
601 generic description is used.
602:face-set-string
603 VALUE is a string used to indicate that a face takes its
604 setting from this theme. It is passed to FORMAT with the name
605 of the theme as an additional argument. If not given, a
606 generic description is used.
607:face-reset-string
608 VALUE is a string used in the case a face has been forced to
609 its value in this theme. It is passed to FORMAT with the name
610 of the theme as an additional argument. If not given, a
611 generic description is used.
612
613Any theme `foo' should be defined in a file called `foo-theme.el';
614see `custom-make-theme-feature' for more information."
615 (let ((feature (custom-make-theme-feature theme)))
616 ;; It is better not to use backquote in this file,
617 ;; because that makes a bootstrapping problem
618 ;; if you need to recompile all the Lisp files using interpreted code.
619 (nconc (list 'custom-declare-theme
620 (list 'quote theme)
621 (list 'quote feature)
622 doc) args)))
623
624(defun custom-make-theme-feature (theme)
625 "Given a symbol THEME, create a new symbol by appending \"-theme\".
626Store this symbol in the `theme-feature' property of THEME.
627Calling `provide-theme' to provide THEME actually puts `THEME-theme'
628into `features'.
629
630This allows for a file-name convention for autoloading themes:
631Every theme X has a property `provide-theme' whose value is \"X-theme\".
632\(require-theme X) then attempts to load the file `X-theme.el'."
633 (intern (concat (symbol-name theme) "-theme")))
634
635(defsubst custom-theme-p (theme)
636 "Non-nil when THEME has been defined."
637 (memq theme custom-known-themes))
638
639(defsubst custom-check-theme (theme)
640 "Check whether THEME is valid, and signal an error if it is not."
641 (unless (custom-theme-p theme)
642 (error "Unknown theme `%s'" theme)))
643
55535639
PJ
644;;; Initializing.
645
e1bab181
RS
646(defun custom-push-theme (prop symbol theme mode value)
647 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
648If the first element in that list is already (THEME ...),
649discard it first.
650
651MODE can be either the symbol `set' or the symbol `reset'. If it is the
652symbol `set', then VALUE is the value to use. If it is the symbol
653`reset', then VALUE is the mode to query instead.
654
655In the following example for the variable `goto-address-url-face', the
656theme `subtle-hacker' uses the same value for the variable as the theme
657`gnome2':
658
659 \((standard set bold)
660 \(gnome2 set info-xref)
661 \(jonadab set underline)
662 \(subtle-hacker reset gnome2))
663
664
665If a value has been stored for themes A B and C, and a new value
666is to be stored for theme C, then the old value of C is discarded.
667If a new value is to be stored for theme B, however, the old value
668of B is not discarded because B is not the car of the list.
669
670For variables, list property PROP is `theme-value'.
671For faces, list property PROP is `theme-face'.
672This is used in `custom-do-theme-reset', for example.
673
674The list looks the same in any case; the examples shows a possible
675value of the `theme-face' property for the face `region':
676
677 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
678 \(standard set ((((class color) (background dark))
679 \(:background \"blue\"))
680 \(t (:background \"gray\")))))
681
682This records values for the `standard' and the `gnome2' themes.
683The user has not customized the face; had he done that,
684the list would contain an entry for the `user' theme, too.
685See `custom-known-themes' for a list of known themes."
686 (let ((old (get symbol prop)))
687 (if (eq (car-safe (car-safe old)) theme)
688 (setq old (cdr old)))
689 (put symbol prop (cons (list theme mode value) old))))
690
55535639
PJ
691(defvar custom-local-buffer nil
692 "Non-nil, in a Customization buffer, means customize a specific buffer.
693If this variable is non-nil, it should be a buffer,
694and it means customize the local bindings of that buffer.
695This variable is a permanent local, and it normally has a local binding
696in every Customization buffer.")
697(put 'custom-local-buffer 'permanent-local t)
698
699(defun custom-set-variables (&rest args)
700 "Initialize variables according to user preferences.
e1bab181 701The settings are registered as theme `user'.
64e9f9d9 702The arguments should each be a list of the form:
55535639
PJ
703
704 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
705
706The unevaluated VALUE is stored as the saved value for SYMBOL.
707If NOW is present and non-nil, VALUE is also evaluated and bound as
708the default value for the SYMBOL.
e1bab181
RS
709
710REQUEST is a list of features we must 'require for SYMBOL.
55535639 711COMMENT is a comment string about SYMBOL."
e1bab181
RS
712 (apply 'custom-theme-set-variables 'user args))
713
714(defun custom-theme-set-variables (theme &rest args)
715 "Initialize variables according to settings specified by args.
716Records the settings as belonging to THEME.
717
718The arguments should be a list where each entry has the form:
719
720 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
721
722The unevaluated VALUE is stored as the saved value for SYMBOL.
723If NOW is present and non-nil, VALUE is also evaluated and bound as
724the default value for the SYMBOL.
725REQUEST is a list of features we must 'require for SYMBOL.
726COMMENT is a comment string about SYMBOL.
727
728Several properties of THEME and SYMBOL are used in the process:
729
730If THEME property `theme-immediate' is non-nil, this is equivalent of
731providing the NOW argument to all symbols in the argument list: SYMBOL
732is bound to the evaluated VALUE. The only difference is SYMBOL property
733`force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
734the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
735FACE's property `force-face' is set to the symbol `immediate'.
736
737VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
738in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
739 (custom-check-theme theme)
740 (let ((immediate (get theme 'theme-immediate)))
741 (setq args
742 (sort args
743 (lambda (a1 a2)
744 (let* ((sym1 (car a1))
745 (sym2 (car a2))
746 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
747 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
748 (cond ((and 1-then-2 2-then-1)
749 (error "Circular custom dependency between `%s' and `%s'"
750 sym1 sym2))
751 (2-then-1 nil)
752 ;; Put symbols with :require last. The macro
753 ;; define-minor-mode generates a defcustom
754 ;; with a :require and a :set, where the
755 ;; setter function calls the mode function.
756 ;; Putting symbols with :require last ensures
757 ;; that the mode function will see other
758 ;; customized values rather than default
759 ;; values.
760 (t (nth 3 a2)))))))
761 (while args
762 (let ((entry (car args)))
763 (if (listp entry)
764 (let* ((symbol (nth 0 entry))
765 (value (nth 1 entry))
766 (now (nth 2 entry))
767 (requests (nth 3 entry))
768 (comment (nth 4 entry))
769 set)
770 (when requests
771 (put symbol 'custom-requests requests)
772 (mapc 'require requests))
773 (setq set (or (get symbol 'custom-set) 'custom-set-default))
774 (put symbol 'saved-value (list value))
775 (put symbol 'saved-variable-comment comment)
776 (custom-push-theme 'theme-value symbol theme 'set value)
777 ;; Allow for errors in the case where the setter has
55535639
PJ
778 ;; changed between versions, say, but let the user know.
779 (condition-case data
780 (cond (now
781 ;; Rogue variable, set it now.
782 (put symbol 'force-value t)
783 (funcall set symbol (eval value)))
784 ((default-boundp symbol)
785 ;; Something already set this, overwrite it.
786 (funcall set symbol (eval value))))
71296446 787 (error
55535639 788 (message "Error setting %s: %s" symbol data)))
e1bab181
RS
789 (setq args (cdr args))
790 (and (or now (default-boundp symbol))
791 (put symbol 'variable-comment comment)))
792 ;; Old format, a plist of SYMBOL VALUE pairs.
793 (message "Warning: old format `custom-set-variables'")
794 (ding)
795 (sit-for 2)
796 (let ((symbol (nth 0 args))
797 (value (nth 1 args)))
798 (put symbol 'saved-value (list value))
799 (custom-push-theme 'theme-value symbol theme 'set value))
800 (setq args (cdr (cdr args))))))))
55535639
PJ
801
802(defun custom-set-default (variable value)
803 "Default :set function for a customizable variable.
804Normally, this sets the default value of VARIABLE to VALUE,
805but if `custom-local-buffer' is non-nil,
806this sets the local binding in that buffer instead."
807 (if custom-local-buffer
808 (with-current-buffer custom-local-buffer
809 (set variable value))
810 (set-default variable value)))
811
df380962
SM
812(defun custom-set-minor-mode (variable value)
813 ":set function for minor mode variables.
814Normally, this sets the default value of VARIABLE to nil if VALUE
815is nil and to t otherwise,
816but if `custom-local-buffer' is non-nil,
817this sets the local binding in that buffer instead."
818 (if custom-local-buffer
819 (with-current-buffer custom-local-buffer
820 (funcall variable (or value 0)))
821 (funcall variable (or value 0))))
822
6d912ee1
MB
823(defun custom-quote (sexp)
824 "Quote SEXP iff it is not self quoting."
825 (if (or (memq sexp '(t nil))
826 (keywordp sexp)
827 (and (listp sexp)
828 (memq (car sexp) '(lambda)))
829 (stringp sexp)
830 (numberp sexp)
831 (vectorp sexp)
832;;; (and (fboundp 'characterp)
833;;; (characterp sexp))
834 )
835 sexp
836 (list 'quote sexp)))
837
838(defun customize-mark-to-save (symbol)
839 "Mark SYMBOL for later saving.
840
71296446 841If the default value of SYMBOL is different from the standard value,
6d912ee1 842set the `saved-value' property to a list whose car evaluates to the
e2cd29bd 843default value. Otherwise, set it to nil.
6d912ee1
MB
844
845To actually save the value, call `custom-save-all'.
846
847Return non-nil iff the `saved-value' property actually changed."
848 (let* ((get (or (get symbol 'custom-get) 'default-value))
849 (value (funcall get symbol))
850 (saved (get symbol 'saved-value))
851 (standard (get symbol 'standard-value))
852 (comment (get symbol 'customized-variable-comment)))
853 ;; Save default value iff different from standard value.
854 (if (or (null standard)
855 (not (equal value (condition-case nil
856 (eval (car standard))
857 (error nil)))))
858 (put symbol 'saved-value (list (custom-quote value)))
859 (put symbol 'saved-value nil))
860 ;; Clear customized information (set, but not saved).
861 (put symbol 'customized-value nil)
862 ;; Save any comment that might have been set.
863 (when comment
864 (put symbol 'saved-variable-comment comment))
865 (not (equal saved (get symbol 'saved-value)))))
866
867(defun customize-mark-as-set (symbol)
868 "Mark current value of SYMBOL as being set from customize.
869
71296446 870If the default value of SYMBOL is different from the saved value if any,
6d912ee1 871or else if it is different from the standard value, set the
71296446 872`customized-value' property to a list whose car evaluates to the
e2cd29bd 873default value. Otherwise, set it to nil.
6d912ee1
MB
874
875Return non-nil iff the `customized-value' property actually changed."
876 (let* ((get (or (get symbol 'custom-get) 'default-value))
877 (value (funcall get symbol))
878 (customized (get symbol 'customized-value))
879 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
880 ;; Mark default value as set iff different from old value.
881 (if (or (null old)
71296446 882 (not (equal value (condition-case nil
6d912ee1
MB
883 (eval (car old))
884 (error nil)))))
885 (put symbol 'customized-value (list (custom-quote value)))
886 (put symbol 'customized-value nil))
887 ;; Changed?
888 (not (equal customized (get symbol 'customized-value)))))
889
e1bab181
RS
890;;; Theme Manipulation
891
892(defvar custom-loaded-themes nil
893 "Themes in the order they are loaded.")
894
895(defun custom-theme-loaded-p (theme)
896 "Return non-nil when THEME has been loaded."
897 (memq theme custom-loaded-themes))
898
899(defun provide-theme (theme)
900 "Indicate that this file provides THEME.
901Add THEME to `custom-loaded-themes' and `provide' whatever
902is stored in THEME's property `theme-feature'.
903
904Usually the theme-feature property contains a symbol created
905by `custom-make-theme-feature'."
906 (custom-check-theme theme)
907 (provide (get theme 'theme-feature))
908 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
909
910(defun require-theme (theme)
911 "Try to load a theme by requiring its feature.
912THEME's feature is stored in THEME's `theme-feature' property.
913
914Usually the `theme-feature' property contains a symbol created
915by `custom-make-theme-feature'."
916 ;; Note we do no check for validity of the theme here.
917 ;; This allows to pull in themes by a file-name convention
918 (require (or (get theme 'theme-feature)
919 (custom-make-theme-feature theme))))
920
921(defun custom-remove-theme (spec-alist theme)
e2cd29bd 922 "Delete all elements from SPEC-ALIST whose car is THEME."
e1bab181
RS
923 (let ((elt (assoc theme spec-alist)))
924 (while elt
925 (setq spec-alist (delete elt spec-alist)
926 elt (assoc theme spec-alist))))
927 spec-alist)
928
929(defun custom-do-theme-reset (theme)
930 "Undo all settings defined by THEME.
931
932A variable remains unchanged if its property `theme-value' does not
933contain a value for THEME. A face remains unchanged if its property
934`theme-face' does not contain a value for THEME. In either case, all
935settings for THEME are removed from the property and the variable or
936face is set to the `user' theme.
937
938See `custom-known-themes' for a list of known themes."
939 (let (spec-list)
940 (mapatoms (lambda (symbol)
941 ;; This works even if symbol is both a variable and a
942 ;; face.
943 (setq spec-list (get symbol 'theme-value))
944 (when spec-list
945 (put symbol 'theme-value (custom-remove-theme spec-list theme))
946 (custom-theme-reset-internal symbol 'user))
947 (setq spec-list (get symbol 'theme-face))
948 (when spec-list
949 (put symbol 'theme-face (custom-remove-theme spec-list theme))
950 (custom-theme-reset-internal-face symbol 'user))))))
951
952(defun custom-theme-load-themes (by-theme &rest body)
953 "Load the themes specified by BODY.
954Record them as required by theme BY-THEME. BODY is a sequence of either
955
956THEME
957 BY-THEME requires THEME
958\(reset THEME)
959 Undo all the settings made by THEME
960\(hidden THEME)
961 Require THEME but hide it from the user
962
963All the themes loaded for BY-THEME are recorded in BY-THEME's property
964`theme-loads-themes'. Any theme loaded with the hidden predicate will
965be given the property `theme-hidden' unless it has been loaded before.
966Whether a theme has been loaded before is determined by the function
967`custom-theme-loaded-p'."
968 (custom-check-theme by-theme)
969 (let ((theme)
970 (themes-loaded (get by-theme 'theme-loads-themes)))
971 (while theme
972 (setq theme (car body)
973 body (cdr body))
974 (cond ((and (consp theme) (eq (car theme) 'reset))
975 (custom-do-theme-reset (cadr theme)))
976 ((and (consp theme) (eq (car theme) 'hidden))
977 (require-theme (cadr theme))
978 (unless (custom-theme-loaded-p (cadr theme))
979 (put (cadr theme) 'theme-hidden t)))
980 (t
981 (require-theme theme)
982 (put theme 'theme-hidden nil)))
983 (setq themes-loaded (nconc (list theme) themes-loaded)))
984 (put by-theme 'theme-loads-themes themes-loaded)))
985
986(defun custom-load-themes (&rest body)
987 "Load themes for the USER theme as specified by BODY.
988
989See `custom-theme-load-themes' for more information on BODY."
990 (apply 'custom-theme-load-themes 'user body))
991
992; (defsubst copy-upto-last (elt list)
993; "Copy all the elements of the list upto the last occurence of elt"
994; ;; Is it faster to do more work in C than to do less in elisp?
995; (nreverse (cdr (member elt (reverse list)))))
996
997(defun custom-theme-value (theme theme-spec-list)
998 "Determine the value for THEME defined by THEME-SPEC-LIST.
999Returns a list with the original value if found; nil otherwise.
1000
1001THEME-SPEC-LIST is an alist with themes as its key. As new themes are
1002installed, these are added to the front of THEME-SPEC-LIST.
1003Each element has the form
1004
1005 \(THEME MODE VALUE)
1006
1007MODE is either the symbol `set' or the symbol `reset'. See
1008`custom-push-theme' for more information on the format of
1009THEME-SPEC-LIST."
1010 ;; Note we do _NOT_ signal an error if the theme is unknown
1011 ;; it might have gone away without the user knowing.
1012 (let ((value (cdr (assoc theme theme-spec-list))))
1013 (if value
1014 (if (eq (car value) 'set)
1015 (cdr value)
1016 (custom-theme-value (cadr value) theme-spec-list)))))
1017
1018(defun custom-theme-variable-value (variable theme)
1019 "Return (list value) indicating value of VARIABLE in THEME.
1020If THEME does not define a value for VARIABLE, return nil. The value
1021definitions per theme are stored in VARIABLE's property `theme-value'.
1022The actual work is done by function `custom-theme-value', which see.
1023See `custom-push-theme' for more information on how these definitions
1024are stored."
1025 (custom-theme-value theme (get variable 'theme-value)))
1026
1027(defun custom-theme-reset-internal (symbol to-theme)
1028 "Reset SYMBOL to the value defined by TO-THEME.
1029If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1030value. See `custom-theme-variable-value'. The standard value is
1031stored in SYMBOL's property `standard-value'."
1032 (let ((value (custom-theme-variable-value symbol to-theme))
1033 was-in-theme)
1034 (setq was-in-theme value)
1035 (setq value (or value (get symbol 'standard-value)))
1036 (when value
1037 (put symbol 'saved-value was-in-theme)
1038 (if (or (get 'force-value symbol) (default-boundp symbol))
1039 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1040 (eval (car value)))))
1041 value))
1042
1043(defun custom-theme-reset-variables (theme &rest args)
1044 "Reset the value of the variables to values previously defined.
1045Associate this setting with THEME.
1046
1047ARGS is a list of lists of the form
1048
1049 (VARIABLE TO-THEME)
1050
1051This means reset VARIABLE to its value in TO-THEME."
1052 (custom-check-theme theme)
1053 (mapcar '(lambda (arg)
1054 (apply 'custom-theme-reset-internal arg)
1055 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1056 args))
1057
1058(defun custom-reset-variables (&rest args)
1059 "Reset the value of the variables to values previously saved.
1060This is the setting associated the `user' theme.
1061
1062ARGS is a list of lists of the form
1063
1064 (VARIABLE TO-THEME)
1065
1066This means reset VARIABLE to its value in TO-THEME."
1067 (apply 'custom-theme-reset-variables 'user args))
1068
55535639
PJ
1069;;; The End.
1070
1071;; Process the defcustoms for variables loaded before this file.
1072(while custom-declare-variable-list
1073 (apply 'custom-declare-variable (car custom-declare-variable-list))
1074 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1075
1076(provide 'custom)
1077
ab5796a9 1078;;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
55535639 1079;;; custom.el ends here