(dabbrev--safe-replace-match): Use with-no-warnings.
[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
267If FACE has been set with `custom-set-face', set the face attributes
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
309Read the section about customization in the Emacs Lisp manual for more
310information."
311 ;; It is better not to use backquote in this file,
312 ;; because that makes a bootstrapping problem
313 ;; if you need to recompile all the Lisp files using interpreted code.
314 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
315
316;;; The `defgroup' Macro.
317
d3b80e9b
SM
318(defun custom-current-group ()
319 (cdr (assoc load-file-name custom-current-group-alist)))
320
55535639
PJ
321(defun custom-declare-group (symbol members doc &rest args)
322 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
323 (while members
324 (apply 'custom-add-to-group symbol (car members))
325 (setq members (cdr members)))
55535639
PJ
326 (when doc
327 ;; This text doesn't get into DOC.
328 (put symbol 'group-documentation (purecopy doc)))
329 (while args
330 (let ((arg (car args)))
331 (setq args (cdr args))
332 (unless (symbolp arg)
333 (error "Junk in args %S" args))
334 (let ((keyword arg)
335 (value (car args)))
336 (unless args
337 (error "Keyword %s is missing an argument" keyword))
338 (setq args (cdr args))
339 (cond ((eq keyword :prefix)
340 (put symbol 'custom-prefix value))
341 (t
342 (custom-handle-keyword symbol keyword value
343 'custom-group))))))
d3b80e9b
SM
344 ;; Record the group on the `current' list.
345 (let ((elt (assoc load-file-name custom-current-group-alist)))
346 (if elt (setcdr elt symbol)
347 (push (cons load-file-name symbol) custom-current-group-alist)))
55535639
PJ
348 (run-hooks 'custom-define-hook)
349 symbol)
350
351(defmacro defgroup (symbol members doc &rest args)
352 "Declare SYMBOL as a customization group containing MEMBERS.
353SYMBOL does not need to be quoted.
354
355Third arg DOC is the group documentation.
356
357MEMBERS should be an alist of the form ((NAME WIDGET)...) where
358NAME is a symbol and WIDGET is a widget for editing that symbol.
359Useful widgets are `custom-variable' for editing variables,
360`custom-face' for edit faces, and `custom-group' for editing groups.
361
362The remaining arguments should have the form
363
364 [KEYWORD VALUE]...
365
366The following KEYWORDs are defined:
367
368:group VALUE should be a customization group.
369 Add SYMBOL to that group.
370
371:version VALUE should be a string specifying that the group was introduced
372 in Emacs version VERSION.
373
374Read the section about customization in the Emacs Lisp manual for more
375information."
376 ;; It is better not to use backquote in this file,
377 ;; because that makes a bootstrapping problem
378 ;; if you need to recompile all the Lisp files using interpreted code.
379 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
380
381(defun custom-add-to-group (group option widget)
382 "To existing GROUP add a new OPTION of type WIDGET.
383If there already is an entry for OPTION and WIDGET, nothing is done."
384 (let ((members (get group 'custom-group))
385 (entry (list option widget)))
386 (unless (member entry members)
387 (put group 'custom-group (nconc members (list entry))))))
388
29512a0f
SM
389(defun custom-group-of-mode (mode)
390 "Return the custom group corresponding to the major or minor MODE.
391If no such group is found, return nil."
392 (or (get mode 'custom-mode-group)
393 (if (or (get mode 'custom-group)
394 (and (string-match "-mode\\'" (symbol-name mode))
395 (get (setq mode (intern (substring (symbol-name mode)
396 0 (match-beginning 0))))
397 'custom-group)))
398 mode)))
399
55535639
PJ
400;;; Properties.
401
402(defun custom-handle-all-keywords (symbol args type)
403 "For customization option SYMBOL, handle keyword arguments ARGS.
404Third argument TYPE is the custom option type."
d3b80e9b 405 (unless (memq :group args)
9b6098b9 406 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
407 (while args
408 (let ((arg (car args)))
409 (setq args (cdr args))
410 (unless (symbolp arg)
411 (error "Junk in args %S" args))
412 (let ((keyword arg)
413 (value (car args)))
414 (unless args
415 (error "Keyword %s is missing an argument" keyword))
416 (setq args (cdr args))
417 (custom-handle-keyword symbol keyword value type)))))
418
419(defun custom-handle-keyword (symbol keyword value type)
420 "For customization option SYMBOL, handle KEYWORD with VALUE.
421Fourth argument TYPE is the custom option type."
422 (if purify-flag
423 (setq value (purecopy value)))
424 (cond ((eq keyword :group)
425 (custom-add-to-group value symbol type))
426 ((eq keyword :version)
427 (custom-add-version symbol value))
428 ((eq keyword :link)
429 (custom-add-link symbol value))
430 ((eq keyword :load)
431 (custom-add-load symbol value))
432 ((eq keyword :tag)
433 (put symbol 'custom-tag value))
434 ((eq keyword :set-after)
435 (custom-add-dependencies symbol value))
436 (t
437 (error "Unknown keyword %s" keyword))))
438
439(defun custom-add-dependencies (symbol value)
440 "To the custom option SYMBOL, add dependencies specified by VALUE.
441VALUE should be a list of symbols. For each symbol in that list,
442this specifies that SYMBOL should be set after the specified symbol, if
443both appear in constructs like `custom-set-variables'."
444 (unless (listp value)
445 (error "Invalid custom dependency `%s'" value))
446 (let* ((deps (get symbol 'custom-dependencies))
447 (new-deps deps))
448 (while value
449 (let ((dep (car value)))
450 (unless (symbolp dep)
451 (error "Invalid custom dependency `%s'" dep))
452 (unless (memq dep new-deps)
453 (setq new-deps (cons dep new-deps)))
454 (setq value (cdr value))))
455 (unless (eq deps new-deps)
456 (put symbol 'custom-dependencies new-deps))))
e1bab181 457
55535639
PJ
458(defun custom-add-option (symbol option)
459 "To the variable SYMBOL add OPTION.
460
461If SYMBOL is a hook variable, OPTION should be a hook member.
462For other types variables, the effect is undefined."
463 (let ((options (get symbol 'custom-options)))
464 (unless (member option options)
465 (put symbol 'custom-options (cons option options)))))
466
467(defun custom-add-link (symbol widget)
468 "To the custom option SYMBOL add the link WIDGET."
469 (let ((links (get symbol 'custom-links)))
470 (unless (member widget links)
471 (put symbol 'custom-links (cons (purecopy widget) links)))))
472
473(defun custom-add-version (symbol version)
474 "To the custom option SYMBOL add the version VERSION."
475 (put symbol 'custom-version (purecopy version)))
476
477(defun custom-add-load (symbol load)
478 "To the custom option SYMBOL add the dependency LOAD.
479LOAD should be either a library file name, or a feature name."
480 (let ((loads (get symbol 'custom-loads)))
481 (unless (member load loads)
482 (put symbol 'custom-loads (cons (purecopy load) loads)))))
483
1669290d
MR
484(defun custom-autoload (symbol load)
485 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
486 (put symbol 'custom-autoload t)
487 (custom-add-load symbol load))
488
489;; This test is also in the C code of `user-variable-p'.
490(defun custom-variable-p (variable)
491 "Return non-nil if VARIABLE is a custom variable."
492 (or (get variable 'standard-value)
493 (get variable 'custom-autoload)))
494
8f772dfd
RS
495;;; Loading files needed to customize a symbol.
496;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
497
498(defvar custom-load-recursion nil
499 "Hack to avoid recursive dependencies.")
500
501(defun custom-load-symbol (symbol)
502 "Load all dependencies for SYMBOL."
503 (unless custom-load-recursion
29512a0f
SM
504 (let ((custom-load-recursion t))
505 (dolist (load (get symbol 'custom-loads))
506 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
507 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
508 ((assoc load load-history))
509 ;; This was just (assoc (locate-library load) load-history)
510 ;; but has been optimized not to load locate-library
511 ;; if not necessary.
29512a0f
SM
512 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
513 "\\(\\'\\|\\.\\)"))
514 (found nil))
8f772dfd 515 (dolist (loaded load-history)
c3891447
RS
516 (and (stringp (car loaded))
517 (string-match regexp (car loaded))
8f772dfd
RS
518 (setq found t)))
519 found))
520 ;; Without this, we would load cus-edit recursively.
521 ;; We are still loading it when we call this,
522 ;; and it is not in load-history yet.
523 ((equal load "cus-edit"))
29512a0f 524 (t (condition-case nil (load load) (error nil))))))))
8f772dfd 525
e1bab181
RS
526(defvar custom-known-themes '(user standard)
527 "Themes that have been define with `deftheme'.
528The default value is the list (user standard). The theme `standard'
529contains the Emacs standard settings from the original Lisp files. The
530theme `user' contains all the the settings the user customized and saved.
531Additional themes declared with the `deftheme' macro will be added to
532the front of this list.")
533
534(defun custom-declare-theme (theme feature &optional doc &rest args)
535 "Like `deftheme', but THEME is evaluated as a normal argument.
536FEATURE is the feature this theme provides. This symbol is created
537from THEME by `custom-make-theme-feature'."
538 (add-to-list 'custom-known-themes theme)
539 (put theme 'theme-feature feature)
540 (when doc
541 (put theme 'theme-documentation doc))
542 (while args
543 (let ((arg (car args)))
544 (setq args (cdr args))
545 (unless (symbolp arg)
546 (error "Junk in args %S" args))
547 (let ((keyword arg)
548 (value (car args)))
549 (unless args
550 (error "Keyword %s is missing an argument" keyword))
551 (setq args (cdr args))
552 (cond ((eq keyword :short-description)
553 (put theme 'theme-short-description short-description))
554 ((eq keyword :immediate)
555 (put theme 'theme-immediate immediate))
556 ((eq keyword :variable-set-string)
557 (put theme 'theme-variable-set-string variable-set-string))
558 ((eq keyword :variable-reset-string)
559 (put theme 'theme-variable-reset-string variable-reset-string))
560 ((eq keyword :face-set-string)
561 (put theme 'theme-face-set-string face-set-string))
562 ((eq keyword :face-reset-string)
563 (put theme 'theme-face-reset-string face-reset-string)))))))
564
565(defmacro deftheme (theme &optional doc &rest args)
566 "Declare custom theme THEME.
567The optional argument DOC is a doc string describing the theme.
568The remaining arguments should have the form
569
570 [KEYWORD VALUE]...
571
572The following KEYWORD's are defined:
573
574:short-description
575 VALUE is a short (one line) description of the theme. If not
576 given, DOC is used.
577:immediate
578 If VALUE is non-nil, variables specified in this theme are set
579 immediately when loading the theme.
580:variable-set-string
581 VALUE is a string used to indicate that a variable takes its
582 setting from this theme. It is passed to FORMAT with the name
583 of the theme as an additional argument. If not given, a
584 generic description is used.
585:variable-reset-string
586 VALUE is a string used in the case a variable has been forced
587 to its value in this theme. It is passed to FORMAT with the
588 name of the theme as an additional argument. If not given, a
589 generic description is used.
590:face-set-string
591 VALUE is a string used to indicate that a face takes its
592 setting from this theme. It is passed to FORMAT with the name
593 of the theme as an additional argument. If not given, a
594 generic description is used.
595:face-reset-string
596 VALUE is a string used in the case a face has been forced to
597 its value in this theme. It is passed to FORMAT with the name
598 of the theme as an additional argument. If not given, a
599 generic description is used.
600
601Any theme `foo' should be defined in a file called `foo-theme.el';
602see `custom-make-theme-feature' for more information."
603 (let ((feature (custom-make-theme-feature theme)))
604 ;; It is better not to use backquote in this file,
605 ;; because that makes a bootstrapping problem
606 ;; if you need to recompile all the Lisp files using interpreted code.
607 (nconc (list 'custom-declare-theme
608 (list 'quote theme)
609 (list 'quote feature)
610 doc) args)))
611
612(defun custom-make-theme-feature (theme)
613 "Given a symbol THEME, create a new symbol by appending \"-theme\".
614Store this symbol in the `theme-feature' property of THEME.
615Calling `provide-theme' to provide THEME actually puts `THEME-theme'
616into `features'.
617
618This allows for a file-name convention for autoloading themes:
619Every theme X has a property `provide-theme' whose value is \"X-theme\".
620\(require-theme X) then attempts to load the file `X-theme.el'."
621 (intern (concat (symbol-name theme) "-theme")))
622
623(defsubst custom-theme-p (theme)
624 "Non-nil when THEME has been defined."
625 (memq theme custom-known-themes))
626
627(defsubst custom-check-theme (theme)
628 "Check whether THEME is valid, and signal an error if it is not."
629 (unless (custom-theme-p theme)
630 (error "Unknown theme `%s'" theme)))
631
55535639
PJ
632;;; Initializing.
633
e1bab181
RS
634(defun custom-push-theme (prop symbol theme mode value)
635 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
636If the first element in that list is already (THEME ...),
637discard it first.
638
639MODE can be either the symbol `set' or the symbol `reset'. If it is the
640symbol `set', then VALUE is the value to use. If it is the symbol
641`reset', then VALUE is the mode to query instead.
642
643In the following example for the variable `goto-address-url-face', the
644theme `subtle-hacker' uses the same value for the variable as the theme
645`gnome2':
646
647 \((standard set bold)
648 \(gnome2 set info-xref)
649 \(jonadab set underline)
650 \(subtle-hacker reset gnome2))
651
652
653If a value has been stored for themes A B and C, and a new value
654is to be stored for theme C, then the old value of C is discarded.
655If a new value is to be stored for theme B, however, the old value
656of B is not discarded because B is not the car of the list.
657
658For variables, list property PROP is `theme-value'.
659For faces, list property PROP is `theme-face'.
660This is used in `custom-do-theme-reset', for example.
661
662The list looks the same in any case; the examples shows a possible
663value of the `theme-face' property for the face `region':
664
665 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
666 \(standard set ((((class color) (background dark))
667 \(:background \"blue\"))
668 \(t (:background \"gray\")))))
669
670This records values for the `standard' and the `gnome2' themes.
671The user has not customized the face; had he done that,
672the list would contain an entry for the `user' theme, too.
673See `custom-known-themes' for a list of known themes."
674 (let ((old (get symbol prop)))
675 (if (eq (car-safe (car-safe old)) theme)
676 (setq old (cdr old)))
677 (put symbol prop (cons (list theme mode value) old))))
678
55535639
PJ
679(defvar custom-local-buffer nil
680 "Non-nil, in a Customization buffer, means customize a specific buffer.
681If this variable is non-nil, it should be a buffer,
682and it means customize the local bindings of that buffer.
683This variable is a permanent local, and it normally has a local binding
684in every Customization buffer.")
685(put 'custom-local-buffer 'permanent-local t)
686
687(defun custom-set-variables (&rest args)
688 "Initialize variables according to user preferences.
e1bab181 689The settings are registered as theme `user'.
64e9f9d9 690The arguments should each be a list of the form:
55535639
PJ
691
692 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
693
694The unevaluated VALUE is stored as the saved value for SYMBOL.
695If NOW is present and non-nil, VALUE is also evaluated and bound as
696the default value for the SYMBOL.
e1bab181
RS
697
698REQUEST is a list of features we must 'require for SYMBOL.
55535639 699COMMENT is a comment string about SYMBOL."
e1bab181
RS
700 (apply 'custom-theme-set-variables 'user args))
701
702(defun custom-theme-set-variables (theme &rest args)
703 "Initialize variables according to settings specified by args.
704Records the settings as belonging to THEME.
705
706The arguments should be a list where each entry has the form:
707
708 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
709
710The unevaluated VALUE is stored as the saved value for SYMBOL.
711If NOW is present and non-nil, VALUE is also evaluated and bound as
712the default value for the SYMBOL.
713REQUEST is a list of features we must 'require for SYMBOL.
714COMMENT is a comment string about SYMBOL.
715
716Several properties of THEME and SYMBOL are used in the process:
717
718If THEME property `theme-immediate' is non-nil, this is equivalent of
719providing the NOW argument to all symbols in the argument list: SYMBOL
720is bound to the evaluated VALUE. The only difference is SYMBOL property
721`force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
722the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
723FACE's property `force-face' is set to the symbol `immediate'.
724
725VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
726in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
727 (custom-check-theme theme)
728 (let ((immediate (get theme 'theme-immediate)))
729 (setq args
730 (sort args
731 (lambda (a1 a2)
732 (let* ((sym1 (car a1))
733 (sym2 (car a2))
734 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
735 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
736 (cond ((and 1-then-2 2-then-1)
737 (error "Circular custom dependency between `%s' and `%s'"
738 sym1 sym2))
739 (2-then-1 nil)
740 ;; Put symbols with :require last. The macro
741 ;; define-minor-mode generates a defcustom
742 ;; with a :require and a :set, where the
743 ;; setter function calls the mode function.
744 ;; Putting symbols with :require last ensures
745 ;; that the mode function will see other
746 ;; customized values rather than default
747 ;; values.
748 (t (nth 3 a2)))))))
749 (while args
750 (let ((entry (car args)))
751 (if (listp entry)
752 (let* ((symbol (nth 0 entry))
753 (value (nth 1 entry))
754 (now (nth 2 entry))
755 (requests (nth 3 entry))
756 (comment (nth 4 entry))
757 set)
758 (when requests
759 (put symbol 'custom-requests requests)
760 (mapc 'require requests))
761 (setq set (or (get symbol 'custom-set) 'custom-set-default))
762 (put symbol 'saved-value (list value))
763 (put symbol 'saved-variable-comment comment)
764 (custom-push-theme 'theme-value symbol theme 'set value)
765 ;; Allow for errors in the case where the setter has
55535639
PJ
766 ;; changed between versions, say, but let the user know.
767 (condition-case data
768 (cond (now
769 ;; Rogue variable, set it now.
770 (put symbol 'force-value t)
771 (funcall set symbol (eval value)))
772 ((default-boundp symbol)
773 ;; Something already set this, overwrite it.
774 (funcall set symbol (eval value))))
71296446 775 (error
55535639 776 (message "Error setting %s: %s" symbol data)))
e1bab181
RS
777 (setq args (cdr args))
778 (and (or now (default-boundp symbol))
779 (put symbol 'variable-comment comment)))
780 ;; Old format, a plist of SYMBOL VALUE pairs.
781 (message "Warning: old format `custom-set-variables'")
782 (ding)
783 (sit-for 2)
784 (let ((symbol (nth 0 args))
785 (value (nth 1 args)))
786 (put symbol 'saved-value (list value))
787 (custom-push-theme 'theme-value symbol theme 'set value))
788 (setq args (cdr (cdr args))))))))
55535639
PJ
789
790(defun custom-set-default (variable value)
791 "Default :set function for a customizable variable.
792Normally, this sets the default value of VARIABLE to VALUE,
793but if `custom-local-buffer' is non-nil,
794this sets the local binding in that buffer instead."
795 (if custom-local-buffer
796 (with-current-buffer custom-local-buffer
797 (set variable value))
798 (set-default variable value)))
799
df380962
SM
800(defun custom-set-minor-mode (variable value)
801 ":set function for minor mode variables.
802Normally, this sets the default value of VARIABLE to nil if VALUE
803is nil and to t otherwise,
804but if `custom-local-buffer' is non-nil,
805this sets the local binding in that buffer instead."
806 (if custom-local-buffer
807 (with-current-buffer custom-local-buffer
808 (funcall variable (or value 0)))
809 (funcall variable (or value 0))))
810
6d912ee1
MB
811(defun custom-quote (sexp)
812 "Quote SEXP iff it is not self quoting."
813 (if (or (memq sexp '(t nil))
814 (keywordp sexp)
815 (and (listp sexp)
816 (memq (car sexp) '(lambda)))
817 (stringp sexp)
818 (numberp sexp)
819 (vectorp sexp)
820;;; (and (fboundp 'characterp)
821;;; (characterp sexp))
822 )
823 sexp
824 (list 'quote sexp)))
825
826(defun customize-mark-to-save (symbol)
827 "Mark SYMBOL for later saving.
828
71296446 829If the default value of SYMBOL is different from the standard value,
6d912ee1 830set the `saved-value' property to a list whose car evaluates to the
e2cd29bd 831default value. Otherwise, set it to nil.
6d912ee1
MB
832
833To actually save the value, call `custom-save-all'.
834
835Return non-nil iff the `saved-value' property actually changed."
836 (let* ((get (or (get symbol 'custom-get) 'default-value))
837 (value (funcall get symbol))
838 (saved (get symbol 'saved-value))
839 (standard (get symbol 'standard-value))
840 (comment (get symbol 'customized-variable-comment)))
841 ;; Save default value iff different from standard value.
842 (if (or (null standard)
843 (not (equal value (condition-case nil
844 (eval (car standard))
845 (error nil)))))
846 (put symbol 'saved-value (list (custom-quote value)))
847 (put symbol 'saved-value nil))
848 ;; Clear customized information (set, but not saved).
849 (put symbol 'customized-value nil)
850 ;; Save any comment that might have been set.
851 (when comment
852 (put symbol 'saved-variable-comment comment))
853 (not (equal saved (get symbol 'saved-value)))))
854
855(defun customize-mark-as-set (symbol)
856 "Mark current value of SYMBOL as being set from customize.
857
71296446 858If the default value of SYMBOL is different from the saved value if any,
6d912ee1 859or else if it is different from the standard value, set the
71296446 860`customized-value' property to a list whose car evaluates to the
e2cd29bd 861default value. Otherwise, set it to nil.
6d912ee1
MB
862
863Return non-nil iff the `customized-value' property actually changed."
864 (let* ((get (or (get symbol 'custom-get) 'default-value))
865 (value (funcall get symbol))
866 (customized (get symbol 'customized-value))
867 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
868 ;; Mark default value as set iff different from old value.
869 (if (or (null old)
71296446 870 (not (equal value (condition-case nil
6d912ee1
MB
871 (eval (car old))
872 (error nil)))))
873 (put symbol 'customized-value (list (custom-quote value)))
874 (put symbol 'customized-value nil))
875 ;; Changed?
876 (not (equal customized (get symbol 'customized-value)))))
877
e1bab181
RS
878;;; Theme Manipulation
879
880(defvar custom-loaded-themes nil
881 "Themes in the order they are loaded.")
882
883(defun custom-theme-loaded-p (theme)
884 "Return non-nil when THEME has been loaded."
885 (memq theme custom-loaded-themes))
886
887(defun provide-theme (theme)
888 "Indicate that this file provides THEME.
889Add THEME to `custom-loaded-themes' and `provide' whatever
890is stored in THEME's property `theme-feature'.
891
892Usually the theme-feature property contains a symbol created
893by `custom-make-theme-feature'."
894 (custom-check-theme theme)
895 (provide (get theme 'theme-feature))
896 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
897
898(defun require-theme (theme)
899 "Try to load a theme by requiring its feature.
900THEME's feature is stored in THEME's `theme-feature' property.
901
902Usually the `theme-feature' property contains a symbol created
903by `custom-make-theme-feature'."
904 ;; Note we do no check for validity of the theme here.
905 ;; This allows to pull in themes by a file-name convention
906 (require (or (get theme 'theme-feature)
907 (custom-make-theme-feature theme))))
908
909(defun custom-remove-theme (spec-alist theme)
e2cd29bd 910 "Delete all elements from SPEC-ALIST whose car is THEME."
e1bab181
RS
911 (let ((elt (assoc theme spec-alist)))
912 (while elt
913 (setq spec-alist (delete elt spec-alist)
914 elt (assoc theme spec-alist))))
915 spec-alist)
916
917(defun custom-do-theme-reset (theme)
918 "Undo all settings defined by THEME.
919
920A variable remains unchanged if its property `theme-value' does not
921contain a value for THEME. A face remains unchanged if its property
922`theme-face' does not contain a value for THEME. In either case, all
923settings for THEME are removed from the property and the variable or
924face is set to the `user' theme.
925
926See `custom-known-themes' for a list of known themes."
927 (let (spec-list)
928 (mapatoms (lambda (symbol)
929 ;; This works even if symbol is both a variable and a
930 ;; face.
931 (setq spec-list (get symbol 'theme-value))
932 (when spec-list
933 (put symbol 'theme-value (custom-remove-theme spec-list theme))
934 (custom-theme-reset-internal symbol 'user))
935 (setq spec-list (get symbol 'theme-face))
936 (when spec-list
937 (put symbol 'theme-face (custom-remove-theme spec-list theme))
938 (custom-theme-reset-internal-face symbol 'user))))))
939
940(defun custom-theme-load-themes (by-theme &rest body)
941 "Load the themes specified by BODY.
942Record them as required by theme BY-THEME. BODY is a sequence of either
943
944THEME
945 BY-THEME requires THEME
946\(reset THEME)
947 Undo all the settings made by THEME
948\(hidden THEME)
949 Require THEME but hide it from the user
950
951All the themes loaded for BY-THEME are recorded in BY-THEME's property
952`theme-loads-themes'. Any theme loaded with the hidden predicate will
953be given the property `theme-hidden' unless it has been loaded before.
954Whether a theme has been loaded before is determined by the function
955`custom-theme-loaded-p'."
956 (custom-check-theme by-theme)
957 (let ((theme)
958 (themes-loaded (get by-theme 'theme-loads-themes)))
959 (while theme
960 (setq theme (car body)
961 body (cdr body))
962 (cond ((and (consp theme) (eq (car theme) 'reset))
963 (custom-do-theme-reset (cadr theme)))
964 ((and (consp theme) (eq (car theme) 'hidden))
965 (require-theme (cadr theme))
966 (unless (custom-theme-loaded-p (cadr theme))
967 (put (cadr theme) 'theme-hidden t)))
968 (t
969 (require-theme theme)
970 (put theme 'theme-hidden nil)))
971 (setq themes-loaded (nconc (list theme) themes-loaded)))
972 (put by-theme 'theme-loads-themes themes-loaded)))
973
974(defun custom-load-themes (&rest body)
975 "Load themes for the USER theme as specified by BODY.
976
977See `custom-theme-load-themes' for more information on BODY."
978 (apply 'custom-theme-load-themes 'user body))
979
980; (defsubst copy-upto-last (elt list)
981; "Copy all the elements of the list upto the last occurence of elt"
982; ;; Is it faster to do more work in C than to do less in elisp?
983; (nreverse (cdr (member elt (reverse list)))))
984
985(defun custom-theme-value (theme theme-spec-list)
986 "Determine the value for THEME defined by THEME-SPEC-LIST.
987Returns a list with the original value if found; nil otherwise.
988
989THEME-SPEC-LIST is an alist with themes as its key. As new themes are
990installed, these are added to the front of THEME-SPEC-LIST.
991Each element has the form
992
993 \(THEME MODE VALUE)
994
995MODE is either the symbol `set' or the symbol `reset'. See
996`custom-push-theme' for more information on the format of
997THEME-SPEC-LIST."
998 ;; Note we do _NOT_ signal an error if the theme is unknown
999 ;; it might have gone away without the user knowing.
1000 (let ((value (cdr (assoc theme theme-spec-list))))
1001 (if value
1002 (if (eq (car value) 'set)
1003 (cdr value)
1004 (custom-theme-value (cadr value) theme-spec-list)))))
1005
1006(defun custom-theme-variable-value (variable theme)
1007 "Return (list value) indicating value of VARIABLE in THEME.
1008If THEME does not define a value for VARIABLE, return nil. The value
1009definitions per theme are stored in VARIABLE's property `theme-value'.
1010The actual work is done by function `custom-theme-value', which see.
1011See `custom-push-theme' for more information on how these definitions
1012are stored."
1013 (custom-theme-value theme (get variable 'theme-value)))
1014
1015(defun custom-theme-reset-internal (symbol to-theme)
1016 "Reset SYMBOL to the value defined by TO-THEME.
1017If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1018value. See `custom-theme-variable-value'. The standard value is
1019stored in SYMBOL's property `standard-value'."
1020 (let ((value (custom-theme-variable-value symbol to-theme))
1021 was-in-theme)
1022 (setq was-in-theme value)
1023 (setq value (or value (get symbol 'standard-value)))
1024 (when value
1025 (put symbol 'saved-value was-in-theme)
1026 (if (or (get 'force-value symbol) (default-boundp symbol))
1027 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1028 (eval (car value)))))
1029 value))
1030
1031(defun custom-theme-reset-variables (theme &rest args)
1032 "Reset the value of the variables to values previously defined.
1033Associate this setting with THEME.
1034
1035ARGS is a list of lists of the form
1036
1037 (VARIABLE TO-THEME)
1038
1039This means reset VARIABLE to its value in TO-THEME."
1040 (custom-check-theme theme)
1041 (mapcar '(lambda (arg)
1042 (apply 'custom-theme-reset-internal arg)
1043 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1044 args))
1045
1046(defun custom-reset-variables (&rest args)
1047 "Reset the value of the variables to values previously saved.
1048This is the setting associated the `user' theme.
1049
1050ARGS is a list of lists of the form
1051
1052 (VARIABLE TO-THEME)
1053
1054This means reset VARIABLE to its value in TO-THEME."
1055 (apply 'custom-theme-reset-variables 'user args))
1056
55535639
PJ
1057;;; The End.
1058
1059;; Process the defcustoms for variables loaded before this file.
1060(while custom-declare-variable-list
1061 (apply 'custom-declare-variable (car custom-declare-variable-list))
1062 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1063
1064(provide 'custom)
1065
1066;;; custom.el ends here