comparison lisp/custom.el @ 17550:d6545cfb6c5a

Synched with custom 1.90.
author Per Abrahamsen <abraham@dina.kvl.dk>
date Thu, 24 Apr 1997 16:53:55 +0000
parents d74be66ca177
children 1cc3913deaf8
comparison
equal deleted inserted replaced
17549:f57de209f01b 17550:d6545cfb6c5a
2 ;; 2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc. 3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;; 4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> 5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces 6 ;; Keywords: help, faces
7 ;; Version: 1.84 7 ;; Version: 1.90
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ 8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9 9
10 ;; This file is part of GNU Emacs. 10 ;; This file is part of GNU Emacs.
11 11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify 12 ;; GNU Emacs is free software; you can redistribute it and/or modify
36 36
37 ;;; Code: 37 ;;; Code:
38 38
39 (require 'widget) 39 (require 'widget)
40 40
41 (define-widget-keywords :prefix :tag :load :link :options :type :group) 41 (define-widget-keywords :initialize :set :get :require :prefix :tag
42 :load :link :options :type :group)
43
42 44
43 (defvar custom-define-hook nil 45 (defvar custom-define-hook nil
44 ;; Customize information for this option is in `cus-edit.el'. 46 ;; Customize information for this option is in `cus-edit.el'.
45 "Hook called after defining each customize option.") 47 "Hook called after defining each customize option.")
46 48
47 ;;; The `defcustom' Macro. 49 ;;; The `defcustom' Macro.
48 50
49 (defun custom-declare-variable (symbol value doc &rest args) 51 (defun custom-initialize-default (symbol value)
50 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments." 52 "Initialize SYMBOL with VALUE.
51 ;; Bind this variable unless it already is bound. 53 This will do nothing if symbol already has a default binding.
54 Otherwise, if symbol has a `saved-value' property, it will evaluate
55 the car of that and used as the default binding for symbol.
56 Otherwise, VALUE will be evaluated and used as the default binding for
57 symbol."
52 (unless (default-boundp symbol) 58 (unless (default-boundp symbol)
53 ;; Use the saved value if it exists, otherwise the factory setting. 59 ;; Use the saved value if it exists, otherwise the factory setting.
54 (set-default symbol (if (get symbol 'saved-value) 60 (set-default symbol (if (get symbol 'saved-value)
55 (eval (car (get symbol 'saved-value))) 61 (eval (car (get symbol 'saved-value)))
56 (eval value)))) 62 (eval value)))))
63
64 (defun custom-initialize-set (symbol value)
65 "Initialize SYMBOL with VALUE.
66 Like `custom-initialize-default', but use the function specified by
67 `:set' to initialize SYMBOL."
68 (unless (default-boundp symbol)
69 (funcall (or (get symbol 'custom-set) 'set-default)
70 symbol
71 (if (get symbol 'saved-value)
72 (eval (car (get symbol 'saved-value)))
73 (eval value)))))
74
75 (defun custom-initialize-reset (symbol value)
76 "Initialize SYMBOL with VALUE.
77 Like `custom-initialize-set', but use the function specified by
78 `:get' to reinitialize SYMBOL if it is already bound."
79 (funcall (or (get symbol 'custom-set) 'set-default)
80 symbol
81 (cond ((default-boundp symbol)
82 (funcall (or (get symbol 'custom-get) 'default-value)
83 symbol))
84 ((get symbol 'saved-value)
85 (eval (car (get symbol 'saved-value))))
86 (t
87 (eval value)))))
88
89 (defun custom-initialize-changed (symbol value)
90 "Initialize SYMBOL with VALUE.
91 Like `custom-initialize-reset', but only use the `:set' function if the
92 not using the factory setting. Otherwise, use the `set-default'."
93 (cond ((default-boundp symbol)
94 (funcall (or (get symbol 'custom-set) 'set-default)
95 symbol
96 (funcall (or (get symbol 'custom-get) 'default-value)
97 symbol)))
98 ((get symbol 'saved-value)
99 (funcall (or (get symbol 'custom-set) 'set-default)
100 symbol
101 (eval (car (get symbol 'saved-value)))))
102 (t
103 (set-default symbol (eval value)))))
104
105 (defun custom-declare-variable (symbol value doc &rest args)
106 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
57 ;; Remember the factory setting. 107 ;; Remember the factory setting.
58 (put symbol 'factory-value (list value)) 108 (put symbol 'factory-value (list value))
59 ;; Maybe this option was rogue in an earlier version. It no longer is. 109 ;; Maybe this option was rogue in an earlier version. It no longer is.
60 (when (get symbol 'force-value) 110 (when (get symbol 'force-value)
61 ;; It no longer is. 111 ;; It no longer is.
62 (put symbol 'force-value nil)) 112 (put symbol 'force-value nil))
63 (when doc 113 (when doc
64 (put symbol 'variable-documentation doc)) 114 (put symbol 'variable-documentation doc))
65 (while args 115 (let ((initialize 'custom-initialize-set)
66 (let ((arg (car args))) 116 (requests nil))
67 (setq args (cdr args)) 117 (while args
68 (unless (symbolp arg) 118 (let ((arg (car args)))
69 (error "Junk in args %S" args))
70 (let ((keyword arg)
71 (value (car args)))
72 (unless args
73 (error "Keyword %s is missing an argument" keyword))
74 (setq args (cdr args)) 119 (setq args (cdr args))
75 (cond ((eq keyword :type) 120 (unless (symbolp arg)
76 (put symbol 'custom-type value)) 121 (error "Junk in args %S" args))
77 ((eq keyword :options) 122 (let ((keyword arg)
78 (if (get symbol 'custom-options) 123 (value (car args)))
79 ;; Slow safe code to avoid duplicates. 124 (unless args
80 (mapcar (lambda (option) 125 (error "Keyword %s is missing an argument" keyword))
81 (custom-add-option symbol option)) 126 (setq args (cdr args))
82 value) 127 (cond ((eq keyword :initialize)
83 ;; Fast code for the common case. 128 (setq initialize value))
84 (put symbol 'custom-options (copy-sequence value)))) 129 ((eq keyword :set)
85 (t 130 (put symbol 'custom-set value))
86 (custom-handle-keyword symbol keyword value 131 ((eq keyword :get)
87 'custom-variable)))))) 132 (put symbol 'custom-get value))
133 ((eq keyword :require)
134 (push value requests))
135 ((eq keyword :type)
136 (put symbol 'custom-type value))
137 ((eq keyword :options)
138 (if (get symbol 'custom-options)
139 ;; Slow safe code to avoid duplicates.
140 (mapcar (lambda (option)
141 (custom-add-option symbol option))
142 value)
143 ;; Fast code for the common case.
144 (put symbol 'custom-options (copy-sequence value))))
145 (t
146 (custom-handle-keyword symbol keyword value
147 'custom-variable))))))
148 (put symbol 'custom-requests requests)
149 ;; Do the actual initialization.
150 (funcall initialize symbol value))
88 (run-hooks 'custom-define-hook) 151 (run-hooks 'custom-define-hook)
89 symbol) 152 symbol)
90 153
91 (defmacro defcustom (symbol value doc &rest args) 154 (defmacro defcustom (symbol value doc &rest args)
92 "Declare SYMBOL as a customizable variable that defaults to VALUE. 155 "Declare SYMBOL as a customizable variable that defaults to VALUE.
98 161
99 [KEYWORD VALUE]... 162 [KEYWORD VALUE]...
100 163
101 The following KEYWORD's are defined: 164 The following KEYWORD's are defined:
102 165
103 :type VALUE should be a widget type. 166 :type VALUE should be a widget type for editing the symbols value.
167 The default is `sexp'.
104 :options VALUE should be a list of valid members of the widget type. 168 :options VALUE should be a list of valid members of the widget type.
105 :group VALUE should be a customization group. 169 :group VALUE should be a customization group.
106 Add SYMBOL to that group. 170 Add SYMBOL to that group.
171 :initialize VALUE should be a function used to initialize the
172 variable. It takes two arguments, the symbol and value
173 given in the `defcustom' call. The default is
174 `custom-initialize-default'
175 :set VALUE should be a function to set the value of the symbol.
176 It takes two arguments, the symbol to set and the value to
177 give it. The default is `set-default'.
178 :get VALUE should be a function to extract the value of symbol.
179 The function takes one argument, a symbol, and should return
180 the current value for that symbol. The default is
181 `default-value'.
182 :require VALUE should be a feature symbol. Each feature will be
183 required after initialization, of the the user have saved this
184 option.
107 185
108 Read the section about customization in the Emacs Lisp manual for more 186 Read the section about customization in the Emacs Lisp manual for more
109 information." 187 information."
110 `(eval-and-compile 188 `(eval-and-compile
111 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))) 189 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
161 239
162 ;;; The `defgroup' Macro. 240 ;;; The `defgroup' Macro.
163 241
164 (defun custom-declare-group (symbol members doc &rest args) 242 (defun custom-declare-group (symbol members doc &rest args)
165 "Like `defgroup', but SYMBOL is evaluated as a normal argument." 243 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
244 (while members
245 (apply 'custom-add-to-group symbol (car members))
246 (setq members (cdr members)))
166 (put symbol 'custom-group (nconc members (get symbol 'custom-group))) 247 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
167 (when doc 248 (when doc
168 (put symbol 'group-documentation doc)) 249 (put symbol 'group-documentation doc))
169 (while args 250 (while args
170 (let ((arg (car args))) 251 (let ((arg (car args)))
283 If NOW is present and non-nil, VALUE is also evaluated and bound as 364 If NOW is present and non-nil, VALUE is also evaluated and bound as
284 the default value for the SYMBOL." 365 the default value for the SYMBOL."
285 (while args 366 (while args
286 (let ((entry (car args))) 367 (let ((entry (car args)))
287 (if (listp entry) 368 (if (listp entry)
288 (let ((symbol (nth 0 entry)) 369 (let* ((symbol (nth 0 entry))
289 (value (nth 1 entry)) 370 (value (nth 1 entry))
290 (now (nth 2 entry))) 371 (now (nth 2 entry))
372 (requests (nth 3 entry))
373 (set (or (get symbol 'custom-set) 'set-default)))
291 (put symbol 'saved-value (list value)) 374 (put symbol 'saved-value (list value))
292 (cond (now 375 (cond (now
293 ;; Rogue variable, set it now. 376 ;; Rogue variable, set it now.
294 (put symbol 'force-value t) 377 (put symbol 'force-value t)
295 (set-default symbol (eval value))) 378 (funcall set symbol (eval value)))
296 ((default-boundp symbol) 379 ((default-boundp symbol)
297 ;; Something already set this, overwrite it. 380 ;; Something already set this, overwrite it.
298 (set-default symbol (eval value)))) 381 (funcall set symbol (eval value))))
382 (when requests
383 (put symbol 'custom-requests requests)
384 (mapcar 'require requests))
299 (setq args (cdr args))) 385 (setq args (cdr args)))
300 ;; Old format, a plist of SYMBOL VALUE pairs. 386 ;; Old format, a plist of SYMBOL VALUE pairs.
301 (message "Warning: old format `custom-set-variables'") 387 (message "Warning: old format `custom-set-variables'")
302 (ding) 388 (ding)
303 (sit-for 2) 389 (sit-for 2)