1 ;;; specifier.el --- Lisp interface to specifiers
3 ;; Copyright (C) 1997, 2004 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 2000, 2002 Ben Wing.
6 ;; Author: Ben Wing <ben@xemacs.org>
7 ;; Keywords: internal, dumped
9 ;;; Synched up with: Not in FSF.
11 ;; This file is part of SXEmacs.
13 ;; SXEmacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; SXEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is dumped with SXEmacs.
32 (defun make-specifier-and-init (type spec-list &optional dont-canonicalize)
33 "Create and initialize a specifier of type TYPE with spec(s) SPEC-LIST.
35 A convenience API combining `make-specifier' and `set-specifier', allowing you
36 to create a specifier and add specs to it at the same time.
37 TYPE specifies the specifier type. See `make-specifier' for known types.
38 SPEC-LIST supplies the specification(s) to be added to the specifier, in any
39 form acceptable to `canonicalize-spec-list'.
40 Optional DONT-CANONICALIZE, if non-nil, inhibits the conversion, and the
41 SPEC-LIST must already be in full form."
42 (let ((sp (make-specifier type)))
43 (if (not dont-canonicalize)
44 (setq spec-list (canonicalize-spec-list spec-list type)))
45 (add-spec-list-to-specifier sp spec-list)
48 ;; God damn, do I hate dynamic scoping.
50 (defun map-specifier (ms-specifier ms-func &optional ms-locale ms-maparg)
51 "Apply MS-FUNC to the specification(s) for MS-LOCALE in MS-SPECIFIER.
53 If optional MS-LOCALE is a locale, MS-FUNC will be called for that locale.
54 If MS-LOCALE is a locale type, MS-FUNC will be mapped over all locales of that
55 type. If MS-LOCALE is 'all or nil, MS-FUNC will be mapped over all locales in
58 Optional MS-MAPARG will be passed to MS-FUNC.
60 MS-FUNC is called with four arguments: the MS-SPECIFIER, the locale
61 being mapped over, the inst-list for that locale, and the
62 optional MS-MAPARG. If any invocation of MS-FUNC returns non-nil,
63 the mapping will stop and the returned value becomes the
64 value returned from `map-specifier'. Otherwise, `map-specifier'
66 (let ((ms-specs (specifier-spec-list ms-specifier ms-locale))
68 (while (and ms-specs (not ms-result))
69 (let ((ms-this-spec (car ms-specs)))
70 (setq ms-result (funcall ms-func ms-specifier (car ms-this-spec)
71 (cdr ms-this-spec) ms-maparg))
72 (setq ms-specs (cdr ms-specs))))
75 (defun canonicalize-inst-pair (inst-pair specifier-type &optional noerror)
76 "Canonicalize the given INST-PAIR.
78 SPECIFIER-TYPE specifies the type of specifier that this INST-PAIR
81 Canonicalizing means converting to the full form for an inst-pair, i.e.
82 `(TAG-SET . INSTANTIATOR)'. A single, untagged instantiator is given
83 a tag set of nil (the empty set), and a single tag is converted into
84 a tag set consisting only of that tag.
86 If NOERROR is non-nil, signal an error if the inst-pair is invalid;
88 ;; OK, the possibilities are:
90 ;; a) a single instantiator
91 ;; b) a cons of a tag and an instantiator
92 ;; c) a cons of a tag set and an instantiator
93 (cond ((valid-instantiator-p inst-pair specifier-type)
97 ((not (consp inst-pair))
100 ;; this will signal an appropriate error.
101 (check-valid-instantiator inst-pair specifier-type)))
103 ((and (valid-specifier-tag-p (car inst-pair))
104 (valid-instantiator-p (cdr inst-pair) specifier-type))
106 (cons (list (car inst-pair)) (cdr inst-pair)))
108 ((and (valid-specifier-tag-set-p (car inst-pair))
109 (valid-instantiator-p (cdr inst-pair) specifier-type))
115 (signal 'error (list "Invalid specifier tag set"
116 (car inst-pair)))))))
118 (defun canonicalize-inst-list (inst-list specifier-type &optional noerror)
119 "Canonicalize the given INST-LIST (a list of inst-pairs).
121 SPECIFIER-TYPE specifies the type of specifier that this INST-LIST
124 Canonicalizing means converting to the full form for an inst-list, i.e.
125 `((TAG-SET . INSTANTIATOR) ...)'. This function accepts a single
126 inst-pair or any abbreviation thereof or a list of (possibly
127 abbreviated) inst-pairs. (See `canonicalize-inst-pair'.)
129 If NOERROR is non-nil, signal an error if the inst-list is invalid;
132 ;; OK, the possibilities are:
134 ;; a) an inst-pair or various abbreviations thereof
136 (let ((result (canonicalize-inst-pair inst-list specifier-type t)))
137 (if (not (eq result t))
141 (if (not (consp inst-list))
144 ;; this will signal an appropriate error.
145 (check-valid-instantiator inst-list specifier-type))
148 (catch 'cann-inst-list
149 ;; don't use mapcar here; we need to catch the case of
151 (let ((rest inst-list)
154 (if (not (consp rest))
155 (if noerror (throw 'cann-inst-list t)
156 (signal 'error (list "Invalid list format" inst-list)))
157 (let ((res2 (canonicalize-inst-pair (car rest) specifier-type
160 ;; at this point, we know we're noerror because
161 ;; otherwise canonicalize-inst-pair would have
162 ;; signalled an error.
163 (throw 'cann-inst-list t)
164 (setq result (cons res2 result)))))
165 (setq rest (cdr rest)))
166 (nreverse result)))))))
168 (defun canonicalize-spec (spec specifier-type &optional noerror)
169 "Canonicalize the given SPEC (a specification).
171 SPECIFIER-TYPE is the type of specifier that this SPEC will be used for.
173 Canonicalizing means converting to the full form for a spec, i.e.
174 `(LOCALE (TAG-SET . INSTANTIATOR) ...)'. This function accepts a
175 possibly abbreviated inst-list or a cons of a locale and a possibly
176 abbreviated inst-list. (See `canonicalize-inst-list'.)
178 If NOERROR is nil, signal an error if the specification is invalid;
180 ;; OK, the possibilities are:
182 ;; a) an inst-list or some abbreviation thereof
183 ;; b) a cons of a locale and an inst-list
184 (let ((result (canonicalize-inst-list spec specifier-type t)))
185 (if (not (eq result t))
187 (cons 'global result)
189 (if (not (consp spec))
192 ;; this will signal an appropriate error.
193 (check-valid-instantiator spec specifier-type))
195 (if (not (valid-specifier-locale-p (car spec)))
198 (signal 'error (list "Invalid specifier locale" (car spec))))
201 (let ((result (canonicalize-inst-list (cdr spec) specifier-type
204 ;; at this point, we know we're noerror because
205 ;; otherwise canonicalize-inst-list would have
206 ;; signalled an error.
208 (cons (car spec) result))))))))
210 (defun canonicalize-spec-list (spec-list specifier-type &optional noerror)
211 "Canonicalize the given SPEC-LIST (a list of specifications).
213 SPECIFIER-TYPE specifies the type of specifier that this SPEC-LIST
216 Canonicalizing means converting to the full form for a spec-list, i.e.
217 `((LOCALE (TAG-SET . INSTANTIATOR) ...) ...)'. This function accepts
218 a possibly abbreviated specification or a list of such things. (See
219 `canonicalize-spec'.) This is the function used to convert spec-lists
220 accepted by `set-specifier' and such into a form suitable for
221 `add-spec-list-to-specifier'.
223 The canonicalization algorithm is as follows:
225 1. Attempt to parse SPEC-LIST as a single, possibly abbreviated, specification.
226 2. If (1) fails, attempt to parse SPEC-LIST as a list of (abbreviated)
228 3. If (2) fails, SPEC-LIST is invalid.
230 A possibly abbreviated specification SPEC is parsed by
232 1. Attempt to parse SPEC as a possibly abbreviated inst-list.
233 2. If (1) fails, attempt to parse SPEC as a cons of a locale and an
234 (abbreviated) inst-list.
235 3. If (2) fails, SPEC is invalid.
237 A possibly abbreviated inst-list INST-LIST is parsed by
239 1. Attempt to parse INST-LIST as a possibly abbreviated inst-pair.
240 2. If (1) fails, attempt to parse INST-LIST as a list of (abbreviated)
242 3. If (2) fails, INST-LIST is invalid.
244 A possibly abbreviated inst-pair INST-PAIR is parsed by
246 1. Check if INST-PAIR is `valid-instantiator-p'.
247 2. If not, check if INST-PAIR is a cons of something that is a tag, ie,
248 `valid-specifier-tag-p', and something that is `valid-instantiator-p'.
249 3. If not, check if INST-PAIR is a cons of a list of tags and something that
250 is `valid-instantiator-p'.
252 In summary, this function generally prefers more abbreviated forms.
254 This function tries extremely hard to resolve any ambiguities, and the
255 built-in specifier types (font, image, toolbar, etc.) are designed so that
256 there won't be any ambiguities. (#### Unfortunately there are bugs in the
257 treatment of toolbar spec-lists and generic spec-lists; avoid depending on
258 canonicalization for these types.)
260 If NOERROR is nil, signal an error if the spec-list is invalid;
262 ;; OK, the possibilities are:
264 ;; a) a spec or various abbreviations thereof
266 (let ((result (canonicalize-spec spec-list specifier-type t)))
267 (if (not (eq result t))
271 (if (not (consp spec-list))
274 ;; this will signal an appropriate error.
275 (check-valid-instantiator spec-list specifier-type))
278 (catch 'cann-spec-list
279 ;; don't use mapcar here; we need to catch the case of
281 (let ((rest spec-list)
284 (if (not (consp rest))
285 (if noerror (throw 'cann-spec-list t)
286 (signal 'error (list "Invalid list format" spec-list)))
287 (let ((res2 (canonicalize-spec (car rest) specifier-type
290 ;; at this point, we know we're noerror because
291 ;; otherwise canonicalize-spec would have
292 ;; signalled an error.
293 (throw 'cann-spec-list t)
294 (setq result (cons res2 result)))))
295 (setq rest (cdr rest)))
296 (nreverse result)))))))
298 (defun set-specifier (specifier value &optional locale tag-set how-to-add)
299 "Add the specification(s) given by VALUE to SPECIFIER in LOCALE.
301 VALUE may be any of the values accepted by `canonicalize-spec-list', including
303 -- an instantiator (either a Lisp object which will be returned when the
304 specifier is instanced, or a Lisp object that can be instantiated to
305 produce an opaque value: eg, a font name (string) can be used for a font
306 specifier, but an instance will be a font object)
307 -- a list of instantiators
308 -- a cons of a locale and an instantiator, or of a locale and a list of
310 -- a cons of a tag or tag-set and an instantiator (or list of instantiators)
311 -- a cons of a locale and the previous type of item
312 -- a list of one or more of any of the previous types of items
313 -- a canonical spec-list.
315 See `canonicalize-spec-list' for details. If you need to know the details,
316 though, strongly consider using the unambiguous APIs `add-spec-to-specifier'
317 and `add-spec-list-to-specifier' instead.
319 Finally, VALUE can itself be a specifier (of the same type as
320 SPECIFIER), if you want to copy specifications from one specifier
321 to another; this is equivalent to calling `copy-specifier', and
322 LOCALE, TAG-SET, and HOW-TO-ADD have the same semantics as with
325 Note that a VALUE of `nil' is either illegal or will be treated as a value of
326 `nil'; it does not remove existing specifications. Use `remove-specifier' for
327 that. N.B. `remove-specifier' defaults to removing all specifications, not
328 just the 'global one!
330 Warning: this function is inherently heuristic, and should not be relied on to
331 properly resolve ambiguities, when specifier instantiators can be lists
332 \(currently, for toolbar specifiers and generic specifiers). In those cases
333 use either `add-spec-to-specifier' or `add-spec-list-to-specifier'.
335 LOCALE indicates where this specification is active, and should be
336 a buffer, a window, a frame, a device, or the symbol `global' to
337 indicate that it applies everywhere. LOCALE defaults to
338 `global' if omitted, and is overridden by locales provided by VALUE (in the
339 cases where value is a full specification or a spec-list).
341 Optional argument TAG-SET is a tag or a list of tags, to be associated
342 with the VALUE. Tags are symbols (usually naming device types, such
343 as `x' and `tty', or device classes, such as `color', `mono', and
344 `grayscale'); specifying a TAG-SET restricts the scope of VALUE to
345 devices that match all specified tags. (You can also create your
346 own tags using `define-specifier-tag', and use them to identify
347 specifications added by you, so you can remove them later.)
349 Optional argument HOW-TO-ADD should be either nil or one of the
350 symbols `prepend', `append', `remove-tag-set-prepend',
351 `remove-tag-set-append', `remove-locale', `remove-locale-type',
352 or `remove-all'. This specifies what to do with existing
353 specifications in LOCALE (and possibly elsewhere in the specifier).
354 Most of the time, you do not need to worry about this argument;
355 the default behavior of `remove-tag-set-prepend' is usually fine.
356 See `copy-specifier' and `add-spec-to-specifier' for a full
357 description of what each of these means.
359 Note that `set-specifier' is exactly complementary to `specifier-specs'
360 except in the case where SPECIFIER has no specs at all in it but nil
361 is a valid instantiator (in that case, `specifier-specs' will return
362 nil (meaning no specs) and `set-specifier' will interpret the `nil'
363 as meaning \"I'm adding a global instantiator and its value is `nil'\"),
364 or in strange cases where there is an ambiguity between a spec-list
365 and an inst-list, etc. (The built-in specifier types are designed
366 in such a way as to avoid any such ambiguities.)"
368 ;; backward compatibility: the old function had HOW-TO-ADD as the
369 ;; third argument and no arguments after that.
370 ;; #### this should disappear at some point.
371 (if (and (null how-to-add)
372 (memq locale '(prepend append remove-tag-set-prepend
373 remove-tag-set-append remove-locale
374 remove-locale-type remove-all)))
376 (setq how-to-add locale)
379 ;; proper beginning of the function.
380 (let ((is-valid (valid-instantiator-p value (specifier-type specifier)))
382 (cond ((and (not is-valid) (specifierp nval))
383 (copy-specifier nval specifier locale tag-set nil how-to-add))
387 (if (not (listp tag-set))
388 (setq tag-set (list tag-set)))
389 ;; You tend to get more accurate errors
390 ;; for a variety of cases if you call
391 ;; canonicalize-tag-set here.
392 (setq tag-set (canonicalize-tag-set tag-set))
393 (if (and (not is-valid) (consp nval))
395 (mapcar #'(lambda (x)
396 (check-valid-instantiator
397 x (specifier-type specifier))
400 (setq nval (cons tag-set nval)))))
402 (setq nval (cons locale nval)))
403 (add-spec-list-to-specifier
405 (canonicalize-spec-list nval (specifier-type specifier))
409 (defun modify-specifier-instances (specifier func &optional args force default
411 "Modify all specifications that match LOCALE and TAG-SET by FUNC.
413 For each specification that exists for SPECIFIER, in locale LOCALE
414 that matches TAG-SET, call the function FUNC with the instance as its
415 first argument and with optional arguments ARGS. The result is then
416 used as the new value of the instantiator.
418 If there is no specification in the domain LOCALE matching TAG-SET and
419 FORCE is non-nil, an explicit one is created from the matching
420 specifier instance if that exists or DEFAULT otherwise. If LOCALE is
421 not a domain (i.e. a buffer), DEFAULT is always used. FUNC is then
422 applied like above and the resulting specification is added."
424 (let ((spec-list (specifier-spec-list specifier locale tag-set)))
427 ;; Destructively edit the spec-list
428 (mapc #'(lambda (spec)
429 (mapc #'(lambda (inst-pair)
431 (apply func (cdr inst-pair) args)))
434 (add-spec-list-to-specifier specifier spec-list))
436 (set-specifier specifier
438 (or (and (valid-specifier-domain-p locale)
439 (specifier-instance specifier))
443 (defmacro let-specifier (specifier-list &rest body)
444 "Add specifier specs, evaluate forms in BODY and restore the specifiers.
445 \(let-specifier SPECIFIER-LIST BODY...)
447 Each element of SPECIFIER-LIST should look like this:
448 \(SPECIFIER VALUE &optional LOCALE TAG-SET HOW-TO-ADD).
450 SPECIFIER is the specifier to be temporarily modified. VALUE is the
451 instantiator to be temporarily added to SPECIFIER in LOCALE. LOCALE,
452 TAG-SET and HOW-TO-ADD have the same meaning as in
453 `add-spec-to-specifier'.
455 The code resulting from macro expansion will add specifications to
456 specifiers using `add-spec-to-specifier'. After BODY is finished, the
457 temporary specifications are removed and old spec-lists are restored.
459 LOCALE, TAG-SET and HOW-TO-ADD may be omitted, and default to nil.
460 The value of the last form in BODY is returned.
462 NOTE: If you want the specifier's instance to change in all
463 circumstances, use (selected-window) as the LOCALE. If LOCALE is nil
464 or omitted, it defaults to `global'.
467 (let-specifier ((modeline-shadow-thickness 0 (selected-window)))
469 (check-argument-type 'listp specifier-list)
470 (flet ((gensym-frob (x name)
471 (if (or (atom x) (eq (car x) 'quote))
473 (list (gensym name) x))))
474 ;; VARLIST is a list of
475 ;; ((SPECIFIERSYM SPECIFIER) (VALUE) (LOCALESYM LOCALE)
476 ;; (TAG-SET) (HOW-TO-ADD))
477 ;; If any of these is an atom, then a separate symbol is
478 ;; unnecessary, the CAR will contain the atom and CDR will be nil.
479 (let* ((varlist (mapcar #'(lambda (listel)
480 (or (and (consp listel)
481 (<= (length listel) 5)
482 (> (length listel) 1))
485 "should be a list of 2-5 elements"
487 ;; VALUE, TAG-SET and HOW-TO-ADD are
488 ;; referenced only once, so we needn't
489 ;; frob them with gensym.
490 (list (gensym-frob (nth 0 listel) "specifier-")
491 (list (nth 1 listel))
492 (gensym-frob (nth 2 listel) "locale-")
493 (list (nth 3 listel))
494 (list (nth 4 listel))))
496 ;; OLDVALLIST is a list of (OLDVALSYM OLDVALFORM)
497 (oldvallist (mapcar #'(lambda (varel)
498 (list (gensym "old-")
499 `(specifier-spec-list
501 ,(car (nth 2 varel)))))
503 ;; Bind the appropriate variables.
504 `(let* (,@(mapcan #'(lambda (varel)
507 (and (cdr varcons) varcons))
513 ,@(mapcar #'(lambda (varel)
514 `(add-spec-to-specifier
515 ,(car (nth 0 varel)) ,(car (nth 1 varel))
516 ,(car (nth 2 varel)) ,(car (nth 3 varel))
517 ,(car (nth 4 varel))))
520 ;; Reverse the unwinding order, so that using the same
521 ;; specifier multiple times works.
522 ,@(apply #'nconc (nreverse (mapcar*
523 #'(lambda (oldval varel)
526 ,(car (nth 2 varel)))
527 (add-spec-list-to-specifier
530 oldvallist varlist))))))))
532 (defun make-integer-specifier (spec-list)
533 "Return a new `integer' specifier object with the given specification list.
534 SPEC-LIST can be a list of specifications (each of which is a cons of a
535 locale and a list of instantiators), a single instantiator, or a list
536 of instantiators. See `make-specifier' for more information about
539 Valid instantiators for integer specifiers are integers."
540 (make-specifier-and-init 'integer spec-list))
542 (defun make-boolean-specifier (spec-list)
543 "Return a new `boolean' specifier object with the given specification list.
544 SPEC-LIST can be a list of specifications (each of which is a cons of a
545 locale and a list of instantiators), a single instantiator, or a list
546 of instantiators. See `make-specifier' for more information about
549 Valid instantiators for boolean specifiers are t and nil."
550 (make-specifier-and-init 'boolean spec-list))
552 (defun make-natnum-specifier (spec-list)
553 "Return a new `natnum' specifier object with the given specification list.
554 SPEC-LIST can be a list of specifications (each of which is a cons of a
555 locale and a list of instantiators), a single instantiator, or a list
556 of instantiators. See `make-specifier' for more information about
559 Valid instantiators for natnum specifiers are non-negative integers."
560 (make-specifier-and-init 'natnum spec-list))
562 (defun make-generic-specifier (spec-list)
563 "Return a new `generic' specifier object with the given specification list.
564 SPEC-LIST can be a list of specifications (each of which is a cons of a
565 locale and a list of instantiators), a single instantiator, or a list
566 of instantiators. See `make-specifier' for more information about
569 Valid instantiators for generic specifiers are all Lisp values.
570 They are returned back unchanged when a specifier is instantiated."
571 (make-specifier-and-init 'generic spec-list))
573 (defun make-display-table-specifier (spec-list)
574 "Return a new `display-table' specifier object with the given spec list.
575 SPEC-LIST can be a list of specifications (each of which is a cons of a
576 locale and a list of instantiators), a single instantiator, or a list
577 of instantiators. See `make-specifier' for more information about
580 Valid instantiators for display-table specifiers are described in
581 detail in the doc string for `current-display-table'."
582 (make-specifier-and-init 'display-table spec-list))
584 ;; Evaluate this for testing:
585 ; (cl-prettyexpand '(let-specifier ((modeline-shadow-thickness 0 (selected-window) 'x) (fubar (value) baz)) (sit-for 1)))
587 (define-specifier-tag 'win 'device-on-window-system-p)
589 ;; Add tags for device types that don't have support compiled
590 ;; into the binary that we're about to dump. This will prevent
593 ;; (set-face-foreground 'default "black" nil '(x color))
595 ;; from producing an error if no X support was compiled in.
597 (or (valid-specifier-tag-p 'x)
598 (define-specifier-tag 'x (lambda (dev) (eq (device-type dev) 'x))))
599 (or (valid-specifier-tag-p 'tty)
600 (define-specifier-tag 'tty (lambda (dev) (eq (device-type dev) 'tty))))
601 (or (valid-specifier-tag-p 'mswindows)
602 (define-specifier-tag 'mswindows (lambda (dev)
603 (eq (device-type dev) 'mswindows))))
605 ;; Add special tag for use by initialization code. Code that
606 ;; sets up default specs should use this tag. Code that needs to
607 ;; override default specs (e.g. the X resource initialization
608 ;; code) can safely clear specs with this tag without worrying
609 ;; about clobbering user settings.
611 (define-specifier-tag 'default)
613 ;;; specifier.el ends here