dfdc88ae36f1d88a2f0c6b2f822890c68db55e30
[chise/xemacs-chise.git.1] / man / lispref / specifiers.texi
1 @c -*-texinfo-*-
2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1995, 1996 Ben Wing.
4 @c Copyright (C) 2002, 2004 Free Software Foundation, Inc.
5 @c See the file lispref.texi for copying conditions.
6 @setfilename ../../info/specifiers.info
7 @node Specifiers, Faces and Window-System Objects, Extents, top
8 @chapter Specifiers
9 @cindex specifier
10
11 A specifier is an object used to keep track of a property whose value
12 should vary according to @emph{display context}, a window, a frame, or
13 device.  The value of many built-in properties, such as the font,
14 foreground, background, and such properties of a face and variables
15 such as @code{modeline-shadow-thickness} and
16 @code{top-toolbar-height}, is actually a specifier object.  The
17 specifier object, in turn, is ``instanced'' in a particular situation
18 to yield the real value of the property in the current context.
19
20 @defun specifierp object
21 This function returns non-@code{nil} if @var{object} is a specifier.
22 @end defun
23
24 @menu
25 * Introduction to Specifiers::  Specifiers provide a clean way for
26                                 display and other properties to vary
27                                 (under user control) in a wide variety
28                                 of contexts.
29 * Simple Specifier Usage::      Getting started with specifiers.
30 * Specifiers In-Depth::         Gory details about specifier innards.
31 * Specifier Instancing::        Instancing means obtaining the ``value'' of
32                                 a specifier in a particular context.
33 * Specifier Types::             Specifiers come in different flavors.
34 * Adding Specifications::       Specifications control a specifier's ``value''
35                                 by giving conditions under which a
36                                 particular value is valid.
37 * Retrieving Specifications::   Querying a specifier's specifications.
38 * Specifier Tag Functions::     Working with specifier tags.
39 * Specifier Instancing Functions::
40                                 Functions to instance a specifier.
41 * Specifier Examples::          Making all this stuff clearer.
42 * Creating Specifiers::         Creating specifiers for your own use.
43 * Specifier Validation Functions::
44                                 Validating the components of a specifier.
45 * Other Specification Functions::
46                                 Other ways of working with specifications.
47 @end menu
48
49 @node Introduction to Specifiers
50 @section Introduction to Specifiers
51
52 Perhaps the most useful way to explain specifiers is via an analogy.
53 Emacs Lisp programmers are used to @emph{buffer-local variables}
54 @ref{Buffer-Local Variables}. For example, the variable
55 @code{modeline-format}, which controls the format of the modeline, can
56 have different values depending on the particular buffer being edited.
57 The variable has a default value which most modes will use, but a
58 specialized package such as Calendar might change the variable so as to
59 tailor the modeline to its own purposes. Other variables are perhaps
60 best thought of as ``mode local,'' such as font-lock keywords, but they
61 are implemented as buffer locals.
62
63 Other properties (such as those that can be changed by the
64 @code{modify-frame-parameters} function, for example the color of the
65 text cursor) can have frame-local values, although it might also make
66 sense for them to have buffer-local values.  In other cases, you might
67 want the property to vary depending on the particular window within the
68 frame that applies (e.g. the top or bottom window in a split frame), the
69 device type that that frame appears on (X or tty), etc.  Perhaps you can
70 envision some more complicated scenario where you want a particular
71 value in a specified buffer, another value in all other buffers
72 displayed on a particular frame, another value in all other buffers
73 displayed in all other frames on any mono (two-color, e.g. black and
74 white only) displays, and a default value in all other circumstances.
75
76 Specifiers generalize both buffer- and frame-local properties.
77 Specifiers vary according to the @emph{display} context.  Font-lock
78 keywords in a buffer will be the same no matter which window the
79 buffer is displayed in, but windows on TTY devices will simply not be
80 capable of the flexibility that windows on modern GUI devices are.
81 Specifiers provide a way for the programmer to @emph{declare} that an
82 emphasized text should be italic on GUI devices and inverse video on
83 TTYs.  They also provide a way for the programmer to declare
84 fallbacks, so that a color specified as ``chartreuse'' where possible
85 can fall back to ``yellow'' on devices where only ANSI (4-bit) color
86 is available.  The complex calculations and device querying are
87 transparent to both user and programmer.  You ask for what you want;
88 it's up to XEmacs to provide it, or a reasonable approximation.
89
90 We call such a declaration a @dfn{specification}.  A @dfn{specification}
91 applies in a particular @dfn{locale}, which is a window, buffer, frame,
92 device, or the global locale.  The value part of the specification is
93 called an @dfn{instantiator}.  The process of determining the value in a
94 particular context, or @dfn{domain}, is called @dfn{instantiation} or
95 @dfn{instancing}.  A domain is a window, frame, or device.
96
97 The difference between @dfn{locale} and @dfn{domain} is somewhat subtle.
98 You may think of a locale as a class of domains, which may span
99 different devices.  Since the specification is abstract (a Lisp form),
100 you can state it without reference to a device.  On the other hand, when
101 you instantiate a specification, you must know the type of the device.
102 It is useless to specify that ``blue means emphasis'' on a monochrome
103 device.  Thus instantiation requires specification of the device on
104 which it will be rendered.
105
106 Thus a @dfn{specifier} allows a great deal of flexibility in
107 controlling exactly what value a property has in which circumstances.
108 Specifiers are most commonly used for display properties, such as an image or
109 the foreground color of a face.  As a simple example, you can specify
110 that the foreground of the default face be
111
112 @itemize @bullet
113 @item
114 blue for a particular buffer
115 @item
116 green for all other buffers
117 @end itemize
118
119 As a more complicated example, you could specify that the foreground of
120 the default face be
121
122 @itemize @bullet
123 @item
124 forest green for all buffers displayed in a particular Emacs window, or
125 green if the X server doesn't recognize the color @samp{forest green}
126 @item
127 blue for all buffers displayed in a particular frame
128 @item
129 red for all other buffers displayed on a color device
130 @item
131 white for all other buffers
132 @end itemize
133
134 @node Simple Specifier Usage
135 @section Simple Specifier Usage
136 @cindex specifier examples
137 @cindex examples, specifier
138 @cindex adding a button to a toolbar
139 @cindex toolbar button, adding
140
141 A useful specifier application is adding a button to a toolbar.  XEmacs
142 provides several toolbars, one along each edge of the frame.  Normally
143 only one is used at a time, the default.  The default toolbar is
144 actually a specifier object which is the value of
145 @code{default-toolbar}.  @xref{Toolbar Intro}.
146
147 The specification of a toolbar is simple:  it is a list of buttons.
148 Each button is a vector with four elements:  an icon, a command, the
149 enabled flag, and a help string.  Let's retrieve the instance of the
150 toolbar you see in the selected frame.
151
152 @example
153 (specifier-instance default-toolbar)
154 @end example
155
156 The value returned is, as promised, a list of vectors.  Now let's build
157 up a button, and add it to the toolbar.  Our button will invoke the last
158 defined keyboard macro.  This is an alternative to
159 @code{name-last-kbd-macro} for creating a persistent macro, rather than
160 an alias for @kbd{C-x e}.
161
162 A toolbar button icon can be quite sophisticated, with different images
163 for button up, button down, and disabled states, and a similar set with
164 captions.  We'll use a very simple icon, but we have to jump through a
165 few non-obvious hoops designed to support the sophisticated applications.
166 The rest of the button descriptor is straightforward.
167
168 @example
169 (setq toolbar-my-kbd-macro-button
170   `[ (list (make-glyph "MyKbdMac"))
171      (lambda () (interactive) (execute-kbd-macro ,last-kbd-macro))
172      t
173      "Execute a previously defined keyboard macro." ])
174
175 (set-specifier default-toolbar
176                (cons toolbar-my-kbd-macro-button
177                      (specifier-specs default-toolbar 'global))
178                'global)
179 @end example
180
181 To remove the button, just substitute the function @code{delete} for the
182 @code{cons} above.
183
184 What is the difference between @code{specifier-instance}, which we used
185 in the example of retrieving the toolbar descriptor, and
186 @code{specifier-specs}, which was used in the toolbar manipulating code?
187 @code{specifier-specs} retrieves a copy of the instantiator, which is
188 abstract and does not depend on context.  @code{specifier-instance}, on
189 the other hand, actually instantiates the specification, and returns the
190 result for the given context.  Another way to express this is:
191 @code{specifier-specs} takes a @emph{locale} as an argument, while
192 @code{specifier-instance} takes a @emph{domain}.  The reason for
193 providing @code{specifier-instance} is that sometimes you wish to see
194 the object that XEmacs will actually use.  @code{specifier-specs}, on
195 the other hand, shows you what the programmer (or user) requested.  When
196 a program manipulates specifications, clearly it's the latter that is
197 desirable.
198
199 In the case of the toolbar descriptor, it turns out that these are the
200 same:  the instancing process is trivial.  However, many specifications
201 have non-trivial instancing.  Compare the results of the following forms
202 on my system.  (The @samp{(cdr (first ...))} form is due to my use of
203 Mule.  On non-Mule XEmacsen, just use @code{specifier-specs}.)
204
205 @example
206 (cdr (first (specifier-specs (face-font 'default) 'global)))
207 => "-*--14-*jisx0208*-0"
208
209 (specifier-instance (face-font 'default))
210 #<font-instance "-*--14-*jisx0208*-0" on #<x-device on ":0.0" 0x970> 0xe0028b 0x176b>
211 @end example
212
213 In this case, @code{specifier-instance} returns an opaque object;
214 programs can't work on it, they can only pass it around.  Worse, in some
215 environments the instantiation will fail, resulting in a different value
216 (when another instantiation succeeds), or worse yet, an error, if all
217 attempts to instance the specifier fail.  @code{specifier-instance} is
218 context-dependent, even for the exact same specification.
219 @code{specifier-specs} is deterministic, and only depends on the
220 specifications.
221
222 Note that in the toolbar-changing code we operate in the global locale.
223 This means that narrower locales, if they have specifications, will
224 shadow our changes.  (Specifier instancing does not merge
225 specifications.  It selects the "highest-priority successful
226 specification" and instances that.)
227
228 In fact, in our example, it seems pretty likely that different buffers
229 @emph{should} have different buttons.  (The icon can be the same, but
230 the keyboard macro you create in a Dired buffer is highly unlikely to be
231 useful in a LaTeX buffer!)  Here's one way to implement this:
232
233 @example
234 (setq toolbar-my-kbd-macro-button
235   `[ (list (make-glyph "MyKbdMac"))
236      (lambda () (interactive) (execute-kbd-macro ,last-kbd-macro))
237      t
238      "Execute a previously defined keyboard macro." ])
239
240 (set-specifier default-toolbar
241                (cons toolbar-my-kbd-macro-button
242                      (cond ((specifier-specs default-toolbar
243                                              (current-buffer)))
244                            ((specifier-specs default-toolbar
245                                              'global)))
246                (current-buffer))
247 @end example
248
249 Finally, a cautionary note: the use of @code{specifier-specs} in the
250 code above is for expository purposes.  Don't use it in production code.
251 In fact, the @code{set-specifier} form above is likely to fail
252 occasionally, because you can add many specifications for the same
253 locale.
254
255 In these cases, @code{specifier-specs} will return a list.  A further
256 refinement is that a specification may be associated with a set of
257 @dfn{specifier tags}.  If the list of specifier tags is non-nil, then
258 @code{specifier-specs} will return a cons of the tag set and the
259 instantiator.  Evidently @code{specifier-specs} is a bit unreliable.
260 (For toolbars, the code above should work 99% of the time, because
261 toolbars are rarely changed.  Since instantiation is trivial, multiple
262 specs are not useful---the first one always succeeds.)
263
264 In fact, @code{specifier-specs} is intended to be used to display specs
265 to humans with a minimum of clutter. The robust way to access
266 specifications is via @code{specifier-spec-list}. @xref{Adding
267 Specifications}, for the definition of @dfn{spec-list}. @xref{Retrieving
268 Specifications}, for documentation of @code{specifier-specs} and
269 @code{specifier-spec-list}. To get the desired effect, replace the form
270 @code{(specifier-spec default-toolbar 'global)} with
271
272 @example
273 (cdr (second (first (specifier-spec-list default-toolbar 'global))))
274 @end example
275
276 (It should be obvious why the example uses the lazy unreliable method!)
277
278 @node Specifiers In-Depth
279 @section In-Depth Overview of a Specifier
280 @cindex specification (in a specifier)
281 @cindex domain (in a specifier)
282 @cindex locale (in a specifier)
283 @cindex instantiator (in a specifier)
284 @cindex instancing (in a specifier)
285 @cindex instance (in a specifier)
286 @cindex inst-list (in a specifier)
287 @cindex inst-pair (in a specifier)
288 @cindex tag (in a specifier)
289 @cindex tag set (in a specifier)
290 @cindex specifier, specification
291 @cindex specifier, domain
292 @cindex specifier, locale
293 @cindex specifier, instantiator
294 @cindex specifier, instancing
295 @cindex specifier, instance
296 @cindex specifier, inst-list
297 @cindex specifier, inst-pair
298 @cindex specifier, tag
299 @cindex specifier, tag set
300
301 Having variables vary according the editing context is very useful, and
302 the buffer is the natural ``atomic'' unit of editing context.  In a GUI
303 environment, it can be similarly useful to have variables whose values
304 vary according to display context.  The atomic unit of display context
305 is the Emacs window.  Buffers are cleanly grouped by modes, but windows
306 are not so easily pigeonholed.  On the one hand, a window displays a
307 buffer, and thus one possible hierarchy is window, buffer, mode.  On the
308 other, a window is a component of a frame.  This generates the window,
309 frame, device hierarchy.  Finally, there are objects such as toolbars
310 whose properties are described by specifiers.  These do not fit
311 naturally into either hierarchy.  This problem is as yet not cleanly
312 solved.
313
314 Another potential source of conceptual confusion is the instantiation
315 process.  Instantiating a buffer-local variable is simple: at any given
316 point in time there is a current buffer, and its local values are used
317 and set whenever the variable is accessed, unless the programmer goes to
318 some special effort (uses @code{default-value} and @code{set-default}.
319 However, a specifier object encapsulates a set of @dfn{specifications},
320 each of which says what its value should be if a particular condition
321 applies.  Several such conditions might apply simultaneously in a given
322 window.
323
324 For example, one specification might be ``The value should be
325 darkseagreen2 on X devices'' another might be ``The value should be blue
326 in the *Help* buffer''.  So what do we do for "the *Help* buffer on an X
327 device"?  The answer is simple: give each type of locale a priority and
328 check them in priority order, returning the first instantiator that
329 successfully instantiates a value.
330
331 Given a specifier, a logical question is ``What is its value in a
332 particular situation?'' This involves looking through the specifications
333 to see which ones apply to this particular situation, and perhaps
334 preferring one over another if more than one applies.  In specifier
335 terminology, a ``particular situation'' is called a @dfn{domain}, and
336 determining its value in a particular domain is called @dfn{instancing}.
337 Most of the time, a domain is identified by a particular window.  For
338 example, if the redisplay engine is drawing text in the default face in
339 a particular window, it retrieves the specifier for the foreground color
340 of the default face and @dfn{instances} it in the domain given by that
341 window; in other words, it asks the specifier, ``What is your value in
342 this window?''.
343
344 Note that the redisplay example is in a sense canonical.  That is,
345 specifiers are designed to present a uniform and @emph{efficient} API
346 to redisplay.  It is the efficiency constraint that motivates the
347 introduction of specifier tags, and many restrictions on access (for
348 example, a buffer is not a domain, and you cannot instantiate a
349 specifier over a buffer).
350
351 More specifically, a specifier contains a set of @dfn{specifications},
352 each of which associates a @dfn{locale} (a window object, a buffer
353 object, a frame object, a device object, or the symbol @code{global})
354 with an @dfn{inst-list}, which is a list of one or more
355 @dfn{inst-pairs}. (For each possible locale, there can be at most one
356 specification containing that locale.) Each inst-pair is a cons of a
357 @dfn{tag set} (an unordered list of zero or more symbols, or @dfn{tags})
358 and an @dfn{instantiator} (the allowed form of this varies depending on
359 the type of specifier).  In a given specification, there may be more
360 than one inst-pair with the same tag set; this is unlike for locales.
361
362 The tag set is used to restrict the sorts of devices over which the
363 instantiator is valid and to uniquely identify instantiators added by a
364 particular application, so that different applications can work on the
365 same specifier and not interfere with each other.  Each tag can have a
366 @dfn{predicate} associated with it, which is a function of one argument
367 (a device) that specifies whether the tag matches that particular
368 device. (If a tag does not have a predicate, it matches all devices.)
369 All tags in a tag set must match a device for the associated inst-pair
370 to be instantiable over that device.  (A null tag set is perfectly
371 valid, and trivially matches all devices.)
372
373 @c #### don't we have more device types now, gtk, ms-windows, mac-carbon?
374 The valid device types (normally @code{x}, @code{tty}, and
375 @code{stream}) and device classes (normally @code{color},
376 @code{grayscale}, and @code{mono}) can always be used as tags, and match
377 devices of the associated type or class (@pxref{Consoles and Devices}).
378 User-defined tags may be defined, with an optional predicate specified.
379 An application can create its own tag, use it to mark all its
380 instantiators, and be fairly confident that it will not interfere with
381 other applications that modify the same specifier---Functions that add
382 a specification to a specifier usually only overwrite existing
383 inst-pairs with the same tag set as was given, and a particular tag or
384 tag set can be specified when removing instantiators.
385
386 When a specifier is instanced in a domain, both the locale and the tag
387 set can be viewed as specifying necessary conditions that must apply in
388 that domain for an instantiator to be considered as a possible result of
389 the instancing.  More specific locales always override more general
390 locales (thus, there is no particular ordering of the specifications in
391 a specifier); however, the tag sets are simply considered in the order
392 that the inst-pairs occur in the specification's inst-list.
393
394 Note also that the actual object that results from the instancing
395 (called an @dfn{instance object}) may not be the same as the instantiator
396 from which it was derived.  For some specifier types (such as integer
397 specifiers and boolean specifiers), the instantiator will be returned
398 directly as the instance object.  For other types, however, this
399 is not the case.  For example, for font specifiers, the instantiator
400 is a font-description string and the instance object is a font-instance
401 object, which describes how the font is displayed on a particular device.
402 A font-instance object encapsulates such things as the actual font name
403 used to display the font on that device (a font-description string
404 under X is usually a wildcard specification that may resolve to
405 different font names, with possibly different foundries, widths, etc.,
406 on different devices), the extra properties of that font on that
407 device, etc.  Furthermore, this conversion (called @dfn{instantiation})
408 might fail---a font or color might not exist on a particular device,
409 for example.
410
411 @node Specifier Instancing
412 @section How a Specifier Is Instanced
413 @cindex fallback (in a specifier)
414 @cindex specifier, fallback
415
416 Instancing of a specifier in a particular window domain proceeds as
417 follows:
418
419 @itemize @bullet
420 @item
421 First, XEmacs searches for a specification whose locale is the same as
422 the window.  If that fails, the search is repeated, looking for a locale
423 that is the same as the window's buffer.  If that fails, the search is
424 repeated using the window's frame, then using the device that frame is
425 on.  Finally, the specification whose locale is the symbol @code{global}
426 (if there is such a specification) is considered.
427 @item
428 The inst-pairs contained in the specification that was found are
429 considered in their order in the inst-list, looking for one whose tag
430 set matches the device that is derived from the window domain.  (The
431 tag set is an unordered list of zero or more tag symbols.  For all
432 tags that have predicates associated with them, the predicate must
433 match the device.)
434 @item
435 If a matching tag set is found, the corresponding instantiator is passed
436 to the specifier's instantiation method, which is specific to the type
437 of the specifier.  If it succeeds, the resulting instance object is
438 returned as the result of the instancing and the instancing is done.
439 Otherwise, the operation continues, looking for another matching
440 inst-pair in the current specification.
441 @item
442 When there are no more inst-pairs to be considered in the current
443 specification, the search starts over, looking for another specification
444 as in the first step above.
445 @item
446 If all specifications are exhausted and no instance object can be
447 derived, the instancing fails. (Actually, this is not completely true.
448 Some specifier objects for built-in properties have a @dfn{fallback}
449 value, which is either an inst-list or another specifier object, that is
450 consulted if the instancing is about to fail.  If it is an inst-list,
451 the searching proceeds using the inst-pairs in that list.  If it is a
452 specifier, the entire instancing starts over using that specifier
453 instead of the given one.  Fallback values are set by the C code and
454 cannot be modified, except perhaps indirectly, using any Lisp functions.
455 The purpose of them is to supply some values to make sure that
456 instancing of built-in properties can't fail and to implement some basic
457 specifier inheritance, such as the fact that faces inherit their
458 properties from the @code{default} face.)
459 @end itemize
460
461 It is also possible to instance a specifier over a frame domain or
462 device domain instead of over a window domain.  The C code, for example,
463 instances the @code{top-toolbar-height} variable over a frame domain in
464 order to determine the height of a frame's top toolbar.  Instancing over
465 a frame or device is similar to instancing over a window except that
466 specifications for locales that cannot be derived from the domain are
467 ignored.  Specifically, instancing over a frame looks first for frame
468 locales, then device locales, then the @code{global} locale.  Instancing
469 over a device domain looks only for device locales and the @code{global}
470 locale.
471
472 @node Specifier Types
473 @section Specifier Types
474
475 There are various different types of specifiers.  The type of a
476 specifier controls what sorts of instantiators are valid, how an
477 instantiator is instantiated, etc.  Here is a list of built-in specifier
478 types:
479
480 @table @code
481 @item boolean
482 The valid instantiators are the symbols @code{t} and @code{nil}.
483 Instance objects are the same as instantiators so no special
484 instantiation function is needed.
485
486 @item integer
487 The valid instantiators are integers.  Instance objects are the same as
488 instantiators so no special instantiation function is needed.
489 @code{modeline-shadow-thickness} is an example of an integer specifier
490 (negative thicknesses indicate that the shadow is drawn recessed instead
491 of raised).
492
493 @item natnum
494 The valid instantiators are natnums (non-negative integers).  Instance
495 objects are the same as instantiators so no special instantiation
496 function is needed.  Natnum specifiers are used for dimension variables
497 such as @code{top-toolbar-height}.
498
499 @item generic
500 All Lisp objects are valid instantiators.  Instance objects are the same
501 as instantiators so no special instantiation function is needed.
502
503 @item font
504 The valid instantiators are strings describing fonts or vectors
505 indicating inheritance from the font of some face.  Instance objects are
506 font-instance objects, which are specific to a particular device.  The
507 instantiation method for font specifiers can fail, unlike for integer,
508 natnum, boolean, and generic specifiers.
509
510 @item color
511 The valid instantiators are strings describing colors or vectors
512 indicating inheritance from the foreground or background of some face.
513 Instance objects are color-instance objects, which are specific to a
514 particular device.  The instantiation method for color specifiers can fail,
515 as for font specifiers.
516
517 @item image
518 Images are perhaps the most complicated type of built-in specifier.  The
519 valid instantiators are strings (a filename, inline data for a pixmap,
520 or text to be displayed in a text glyph) or vectors describing inline
521 data of various sorts or indicating inheritance from the
522 background-pixmap property of some face.  Instance objects are either
523 strings (for text images), image-instance objects (for pixmap images),
524 or subwindow objects (for subwindow images).  The instantiation method
525 for image specifiers can fail, as for font and color specifiers.
526
527 @item face-boolean
528 The valid instantiators are the symbols @code{t} and @code{nil} and
529 vectors indicating inheritance from a boolean property of some face.
530 Specifiers of this sort are used for all of the built-in boolean
531 properties of faces.  Instance objects are either the symbol @code{t}
532 or the symbol @code{nil}.
533
534 @item toolbar
535 The valid instantiators are toolbar descriptors, which are lists
536 of toolbar-button descriptors (each of which is a vector of two
537 or four elements).  @xref{Toolbar}, for more information.
538 @end table
539
540 Color and font instance objects can also be used in turn as
541 instantiators for a new color or font instance object.  Since these
542 instance objects are device-specific, the instantiator can be used
543 directly as the new instance object, but only if they are of the same
544 device.  If the devices differ, the base color or font of the
545 instantiating object is effectively used instead as the instantiator.
546
547 @xref{Faces and Window-System Objects}, for more information on fonts,
548 colors, and face-boolean specifiers.  @xref{Glyphs}, for more information
549 about image specifiers.  @xref{Toolbar}, for more information on toolbar
550 specifiers.
551
552 @defun specifier-type specifier
553 This function returns the type of @var{specifier}.  The returned value
554 will be a symbol: one of @code{integer}, @code{boolean}, etc., as
555 listed in the above table.
556 @end defun
557
558 Functions are also provided to query whether an object is a particular
559 kind of specifier:
560
561 @defun boolean-specifier-p object
562 This function returns non-@code{nil} if @var{object} is a boolean
563 specifier.
564 @end defun
565
566 @defun integer-specifier-p object
567 This function returns non-@code{nil} if @var{object} is an integer
568 specifier.
569 @end defun
570
571 @defun natnum-specifier-p object
572 This function returns non-@code{nil} if @var{object} is a natnum
573 specifier.
574 @end defun
575
576 @defun generic-specifier-p object
577 This function returns non-@code{nil} if @var{object} is a generic
578 specifier.
579 @end defun
580
581 @defun face-boolean-specifier-p object
582 This function returns non-@code{nil} if @var{object} is a face-boolean
583 specifier.
584 @end defun
585
586 @defun toolbar-specifier-p object
587 This function returns non-@code{nil} if @var{object} is a toolbar
588 specifier.
589 @end defun
590
591 @defun font-specifier-p object
592 This function returns non-@code{nil} if @var{object} is a font
593 specifier.
594 @end defun
595
596 @defun color-specifier-p object
597 This function returns non-@code{nil} if @var{object} is a color
598 specifier.
599 @end defun
600
601 @defun image-specifier-p object
602 This function returns non-@code{nil} if @var{object} is an image
603 specifier.
604 @end defun
605
606 @node Adding Specifications
607 @section Adding specifications to a Specifier
608
609 @defun add-spec-to-specifier specifier instantiator &optional locale tag-set how-to-add
610 This function adds a specification to @var{specifier}.  The
611 specification maps from @var{locale} (which should be a window, buffer,
612 frame, device, or the symbol @code{global}, and defaults to
613 @code{global}) to @var{instantiator}, whose allowed values depend on the
614 type of the specifier.  Optional argument @var{tag-set} limits the
615 instantiator to apply only to the specified tag set, which should be a
616 list of tags all of which must match the device being instantiated over
617 (tags are a device type, a device class, or tags defined with
618 @code{define-specifier-tag}).  Specifying a single symbol for
619 @var{tag-set} is equivalent to specifying a one-element list containing
620 that symbol.  Optional argument @var{how-to-add} specifies what to do if
621 there are already specifications in the specifier.  It should be one of
622
623 @table @code
624 @item prepend
625 Put at the beginning of the current list of instantiators for @var{locale}.
626 @item append
627 Add to the end of the current list of instantiators for @var{locale}.
628 @item remove-tag-set-prepend
629 This is the default.  Remove any existing instantiators whose tag set is
630 the same as @var{tag-set}; then put the new instantiator at the
631 beginning of the current list.
632 @item remove-tag-set-append
633 Remove any existing instantiators whose tag set is the same as
634 @var{tag-set}; then put the new instantiator at the end of the current
635 list.
636 @item remove-locale
637 Remove all previous instantiators for this locale before adding the new
638 spec.
639 @item remove-locale-type
640 Remove all specifications for all locales of the same type as
641 @var{locale} (this includes @var{locale} itself) before adding the new
642 spec.
643 @item remove-all
644 Remove all specifications from the specifier before adding the new spec.
645 @end table
646
647 @code{remove-tag-set-prepend} is the default.
648
649 You can retrieve the specifications for a particular locale or locale type
650 with the function @code{specifier-spec-list} or @code{specifier-specs}.
651 @end defun
652
653 @defun add-spec-list-to-specifier specifier spec-list &optional how-to-add
654 This function adds a @dfn{spec-list} (a list of specifications) to
655 @var{specifier}.  The format of a spec-list is
656
657 @example
658   @code{((@var{locale} (@var{tag-set} . @var{instantiator}) ...) ...)}
659 @end example
660
661 where
662
663 @itemize @bullet
664 @item
665 @var{locale} := a window, a buffer, a frame, a device, or @code{global}
666 @item
667 @var{tag-set} := an unordered list of zero or more @var{tags}, each of
668 which is a symbol
669 @item
670 @var{tag} := a device class (@pxref{Consoles and Devices}), a device type,
671 or a tag defined with @code{define-specifier-tag}
672 @item
673 @var{instantiator} := format determined by the type of specifier
674 @end itemize
675
676 The pair @code{(@var{tag-set} . @var{instantiator})} is called an
677 @dfn{inst-pair}.  A list of inst-pairs is called an @dfn{inst-list}.
678 The pair @code{(@var{locale} . @var{inst-list})} is called a
679 @dfn{specification}.  A spec-list, then, can be viewed as a list of
680 specifications.
681
682 @var{how-to-add} specifies how to combine the new specifications with
683 the existing ones, and has the same semantics as for
684 @code{add-spec-to-specifier}.
685
686 The higher-level function @code{set-specifier} is often
687 more convenient because it allows abbreviations of spec-lists to be used
688 instead of the heavily nested canonical syntax.  However, one should
689 take great care in using them with specifiers types which can have lists
690 as instantiators, such as toolbar specifiers and generic specifiers.  In
691 those cases it's probably best to use @code{add-spec-to-specifier} or
692 @code{add-spec-list-to-specifier}.
693 @end defun
694
695 @defspec let-specifier specifier-list &rest body
696 This special form temporarily adds specifications to specifiers,
697 evaluates forms in @var{body} and restores the specifiers to their
698 previous states.  The specifiers and their temporary specifications are
699 listed in @var{specifier-list}.
700
701 The format of @var{specifier-list} is
702
703 @example
704 ((@var{specifier} @var{value} &optional @var{locale} @var{tag-set} @var{how-to-add}) ...)
705 @end example
706
707 @var{specifier} is the specifier to be temporarily modified.
708 @var{value} is the instantiator to be temporarily added to specifier in
709 @var{locale}.  @var{locale}, @var{tag-set} and @var{how-to-add} have the
710 same meaning as in @code{add-spec-to-specifier}.
711
712 This special form is implemented as a macro; the code resulting from
713 macro expansion will add specifications to specifiers using
714 @code{add-spec-to-specifier}.  After forms in @var{body} are evaluated,
715 the temporary specifications are removed and old specifier spec-lists
716 are restored.
717
718 @var{locale}, @var{tag-set} and @var{how-to-add} may be omitted, and
719 default to @code{nil}.  The value of the last form in @var{body} is
720 returned.
721
722 NOTE: If you want the specifier's instance to change in all
723 circumstances, use @code{(selected-window)} as the @var{locale}.  If
724 @var{locale} is @code{nil} or omitted, it defaults to @code{global}.
725
726 The following example removes the 3D modeline effect in the currently
727 selected window for the duration of a second:
728
729 @example
730 (let-specifier ((modeline-shadow-thickness 0 (selected-window)))
731   (sit-for 1))
732 @end example
733 @end defspec
734
735 @defun set-specifier specifier value &optional locale tag-set how-to-add
736 This function adds some specifications to @var{specifier}.  @var{value}
737 can be a single instantiator or tagged instantiator (added as a global
738 specification), a list of tagged and/or untagged instantiators (added as
739 a global specification), a cons of a locale and instantiator or locale
740 and instantiator list, a list of such conses, or nearly any other
741 reasonable form.  More specifically, @var{value} can be anything
742 accepted by @code{canonicalize-spec-list} (described below).
743
744 @var{locale}, @var{tag-set}, and @var{how-to-add} are the same as in
745 @code{add-spec-to-specifier}.
746
747 Note that @code{set-specifier} is exactly complementary to
748 @code{specifier-specs} except in the case where @var{specifier} has no
749 specs at all in it but @code{nil} is a valid instantiator (in that case,
750 @code{specifier-specs} will return @code{nil} (meaning no specs) and
751 @code{set-specifier} will interpret the @code{nil} as meaning ``I'm
752 adding a global instantiator and its value is @code{nil}''), or in
753 strange cases where there is an ambiguity between a spec-list and an
754 inst-list, etc. (The built-in specifier types are designed in such a way
755 as to avoid any such ambiguities.)  For robust code,
756 @code{set-specifier} should probably be avoided for specifier types
757 which accept lists as instantiators (currently toolbar specifiers and
758 generic specifiers).
759
760 If you want to work with spec-lists, you should probably not use these
761 functions, but should use the lower-level functions
762 @code{specifier-spec-list} and @code{add-spec-list-to-specifier}.  These
763 functions always work with fully-qualified spec-lists; thus, there is no
764 ambiguity.
765 @end defun
766
767 @defun canonicalize-inst-pair inst-pair specifier-type &optional noerror
768 This function canonicalizes the given @var{inst-pair}.
769
770 @var{specifier-type} specifies the type of specifier that this
771 @var{spec-list} will be used for.
772
773 Canonicalizing means converting to the full form for an inst-pair, i.e.
774 @code{(@var{tag-set} . @var{instantiator})}.  A single, untagged
775 instantiator is given a tag set of @code{nil} (the empty set), and a
776 single tag is converted into a tag set consisting only of that tag.
777
778 If @var{noerror} is non-@code{nil}, signal an error if the inst-pair is
779 invalid; otherwise return @code{t}.
780 @end defun
781
782 @defun canonicalize-inst-list inst-list specifier-type &optional noerror
783 This function canonicalizes the given @var{inst-list} (a list of
784 inst-pairs).
785
786 @var{specifier-type} specifies the type of specifier that this @var{inst-list}
787 will be used for.
788
789 Canonicalizing means converting to the full form for an inst-list, i.e.
790 @code{((@var{tag-set} . @var{instantiator}) ...)}.  This function
791 accepts a single inst-pair or any abbreviation thereof or a list of
792 (possibly abbreviated) inst-pairs. (See @code{canonicalize-inst-pair}.)
793
794 If @var{noerror} is non-@code{nil}, signal an error if the inst-list is
795 invalid; otherwise return @code{t}.
796 @end defun
797
798 @defun canonicalize-spec spec specifier-type &optional noerror
799 This function canonicalizes the given @var{spec} (a specification).
800
801 @var{specifier-type} specifies the type of specifier that this
802 @var{spec-list} will be used for.
803
804 Canonicalizing means converting to the full form for a spec, i.e.
805 @code{(@var{locale} (@var{tag-set} . @var{instantiator}) ...)}.  This
806 function accepts a possibly abbreviated inst-list or a cons of a locale
807 and a possibly abbreviated inst-list. (See
808 @code{canonicalize-inst-list}.)
809
810 If @var{noerror} is @code{nil}, signal an error if the specification is
811 invalid; otherwise return @code{t}.
812 @end defun
813
814 @defun canonicalize-spec-list spec-list specifier-type &optional noerror
815 This function canonicalizes the given @var{spec-list} (a list of
816 specifications).
817
818 @var{specifier-type} specifies the type of specifier that this
819 @var{spec-list} will be used for.
820
821 If @var{noerror} is @code{nil}, signal an error if the spec-list is
822 invalid; otherwise return @code{t} for an invalid spec-list.  (Note that
823 this cannot be confused with a canonical spec-list.)
824
825 Canonicalizing means converting to the full form for a spec-list, i.e.
826 @code{((@var{locale} (@var{tag-set} . @var{instantiator}) ...) ...)}.
827 This function accepts a possibly abbreviated specification or a list of
828 such things. (See @code{canonicalize-spec}.) This is the function used
829 to convert spec-lists accepted by @code{set-specifier} and such into a
830 form suitable for @code{add-spec-list-to-specifier}.
831
832 This function tries extremely hard to resolve any ambiguities,
833 and the built-in specifier types (font, image, toolbar, etc.) are
834 designed so that there won't be any ambiguities.
835
836 The canonicalization algorithm is as follows:
837
838 @enumerate
839 @item
840 Attempt to parse @var{spec-list} as a single, possibly abbreviated,
841 specification.
842 @item
843 If that fails, attempt to parse @var{spec-list} as a list of (abbreviated)
844 specifications.
845 @item
846 If that fails, @var{spec-list} is invalid.
847 @end enumerate
848
849 A possibly abbreviated specification @var{spec} is parsed by
850
851 @enumerate
852 @item
853 Attempt to parse @var{spec} as a possibly abbreviated inst-list.
854 @item
855 If that fails, attempt to parse @var{spec} as a cons of a locale and an
856 (abbreviated) inst-list.
857 @item
858 If that fails, @var{spec} is invalid.
859 @end enumerate
860
861 A possibly abbreviated inst-list @var{inst-list} is parsed by
862
863 @enumerate
864 @item
865 Attempt to parse @var{inst-list} as a possibly abbreviated inst-pair.
866 @item
867 If that fails, attempt to parse @var{inst-list} as a list of (abbreviated)
868 inst-pairs.
869 @item
870 If that fails, @var{inst-list} is invalid.
871 @end enumerate
872
873 A possibly abbreviated inst-pair @var{inst-pair} is parsed by
874
875 @enumerate
876 @item
877 Check if @var{inst-pair} is @code{valid-instantiator-p}.
878 @item
879 If not, check if @var{inst-pair} is a cons of something that is a tag, ie,
880 @code{valid-specifier-tag-p}, and something that is @code{valid-instantiator-p}.
881 @item
882 If not, check if @var{inst-pair} is a cons of a list of tags and something that
883 is @code{valid-instantiator-p}.
884 @item
885 Otherwise, @var{inst-pair} is invalid.
886 @end enumerate
887
888 In summary, this function generally prefers more abbreviated forms.
889 @end defun
890
891 @node Retrieving Specifications
892 @section Retrieving the Specifications from a Specifier
893
894 @defun specifier-spec-list specifier &optional locale tag-set exact-p
895 This function returns the spec-list of specifications for
896 @var{specifier} in @var{locale}.
897
898 If @var{locale} is a particular locale (a window, buffer, frame, device,
899 or the symbol @code{global}), a spec-list consisting of the
900 specification for that locale will be returned.
901
902 If @var{locale} is a locale type (i.e. one of the symbols @code{window},
903 @code{buffer}, @code{frame}, or @code{device}), a spec-list of the
904 specifications for all locales of that type will be returned.
905
906 If @var{locale} is @code{nil} or the symbol @code{all}, a spec-list of
907 all specifications in @var{specifier} will be returned.
908
909 @var{locale} can also be a list of locales, locale types, and/or
910 @code{all}; the result is as if @code{specifier-spec-list} were called
911 on each element of the list and the results concatenated together.
912
913 Only instantiators where @var{tag-set} (a list of zero or more tags) is
914 a subset of (or possibly equal to) the instantiator's tag set are
915 returned.  (The default value of @code{nil} is a subset of all tag sets,
916 so in this case no instantiators will be screened out.) If @var{exact-p}
917 is non-@code{nil}, however, @var{tag-set} must be equal to an
918 instantiator's tag set for the instantiator to be returned.
919 @end defun
920
921 @defun specifier-specs specifier &optional locale tag-set exact-p
922 This function returns the specification(s) for @var{specifier} in
923 @var{locale}.
924
925 If @var{locale} is a single locale or is a list of one element
926 containing a single locale, then a ``short form'' of the instantiators
927 for that locale will be returned.  Otherwise, this function is identical
928 to @code{specifier-spec-list}.
929
930 The ``short form'' is designed for readability and not for ease of use
931 in Lisp programs, and is as follows:
932
933 @enumerate
934 @item
935 If there is only one instantiator, then an inst-pair (i.e. cons of tag
936 and instantiator) will be returned; otherwise a list of inst-pairs will
937 be returned.
938 @item
939 For each inst-pair returned, if the instantiator's tag is @code{any},
940 the tag will be removed and the instantiator itself will be returned
941 instead of the inst-pair.
942 @item
943 If there is only one instantiator, its value is @code{nil}, and its tag
944 is @code{any}, a one-element list containing @code{nil} will be returned
945 rather than just @code{nil}, to distinguish this case from there being
946 no instantiators at all.
947 @end enumerate
948
949 @end defun
950
951 @defun specifier-fallback specifier
952 This function returns the fallback value for @var{specifier}.  Fallback
953 values are provided by the C code for certain built-in specifiers to
954 make sure that instancing won't fail even if all specs are removed from
955 the specifier, or to implement simple inheritance behavior (e.g. this
956 method is used to ensure that faces other than @code{default} inherit
957 their attributes from @code{default}).  By design, you cannot change the
958 fallback value, and specifiers created with @code{make-specifier} will
959 never have a fallback (although a similar, Lisp-accessible capability
960 may be provided in the future to allow for inheritance).
961
962 The fallback value will be an inst-list that is instanced like
963 any other inst-list, a specifier of the same type as @var{specifier}
964 (results in inheritance), or @code{nil} for no fallback.
965
966 When you instance a specifier, you can explicitly request that the
967 fallback not be consulted. (The C code does this, for example, when
968 merging faces.) See @code{specifier-instance}.
969 @end defun
970
971 @node Specifier Tag Functions
972 @section Working With Specifier Tags
973
974 A specifier tag set is an entity that is attached to an instantiator
975 and can be used to restrict the scope of that instantiator to a
976 particular device class or device type and/or to mark instantiators
977 added by a particular package so that they can be later removed.
978
979 A specifier tag set consists of a list of zero or more specifier tags,
980 each of which is a symbol that is recognized by XEmacs as a tag.  (The
981 valid device types and device classes are always tags, as are any tags
982 defined by @code{define-specifier-tag}.) It is called a ``tag set'' (as
983 opposed to a list) because the order of the tags or the number of times
984 a particular tag occurs does not matter.
985
986 Each tag has a predicate associated with it, which specifies whether
987 that tag applies to a particular device.  The tags which are device
988 types and classes match devices of that type or class.  User-defined
989 tags can have any predicate, or none (meaning that all devices match).
990 When attempting to instance a specifier, a particular instantiator is
991 only considered if the device of the domain being instanced over matches
992 all tags in the tag set attached to that instantiator.
993
994 Most of the time, a tag set is not specified, and the instantiator gets
995 a null tag set, which matches all devices.
996
997 @defun valid-specifier-tag-p tag
998 This function returns non-@code{nil} if @var{tag} is a valid specifier
999 tag.
1000 @end defun
1001
1002 @defun valid-specifier-tag-set-p tag-set
1003 This function returns non-@code{nil} if @var{tag-set} is a valid
1004 specifier tag set.
1005 @end defun
1006
1007 @defun canonicalize-tag-set tag-set
1008 This function canonicalizes the given tag set.  Two canonicalized tag
1009 sets can be compared with @code{equal} to see if they represent the same
1010 tag set. (Specifically, canonicalizing involves sorting by symbol name
1011 and removing duplicates.)
1012 @end defun
1013
1014 @defun device-matches-specifier-tag-set-p device tag-set
1015 This function returns non-@code{nil} if @var{device} matches specifier
1016 tag set @var{tag-set}.  This means that @var{device} matches each tag in
1017 the tag set.
1018 @end defun
1019
1020 @defun define-specifier-tag tag &optional predicate
1021 This function defines a new specifier tag.  If @var{predicate} is
1022 specified, it should be a function of one argument (a device) that
1023 specifies whether the tag matches that particular device.  If
1024 @var{predicate} is omitted, the tag matches all devices.
1025
1026 You can redefine an existing user-defined specifier tag.  However, you
1027 cannot redefine the built-in specifier tags (the device types and
1028 classes) or the symbols @code{nil}, @code{t}, @code{all}, or
1029 @code{global}.
1030 @end defun
1031
1032 @defun device-matching-specifier-tag-list &optional device
1033 This function returns a list of all specifier tags matching
1034 @var{device}.  @var{device} defaults to the selected device if omitted.
1035 @end defun
1036
1037 @defun specifier-tag-list
1038 This function returns a list of all currently-defined specifier tags.
1039 This includes the built-in ones (the device types and classes).
1040 @end defun
1041
1042 @defun specifier-tag-predicate tag
1043 This function returns the predicate for the given specifier tag.
1044 @end defun
1045
1046 @node Specifier Instancing Functions
1047 @section Functions for Instancing a Specifier
1048
1049 @defun specifier-instance specifier &optional domain default no-fallback
1050 This function instantiates @var{specifier} (returns its value) in
1051 @var{domain}.  If no instance can be generated for this domain, return
1052 @var{default}.
1053
1054 @var{domain} should be a window, frame, or device.  Other values that
1055 are legal as a locale (e.g. a buffer) are not valid as a domain because
1056 they do not provide enough information to identify a particular device
1057 (see @code{valid-specifier-domain-p}).  @var{domain} defaults to the
1058 selected window if omitted.
1059
1060 @dfn{Instantiating} a specifier in a particular domain means determining
1061 the specifier's ``value'' in that domain.  This is accomplished by
1062 searching through the specifications in the specifier that correspond to
1063 all locales that can be derived from the given domain, from specific to
1064 general.  In most cases, the domain is an Emacs window.  In that case
1065 specifications are searched for as follows:
1066
1067 @enumerate
1068 @item
1069 A specification whose locale is the window itself;
1070 @item
1071 A specification whose locale is the window's buffer;
1072 @item
1073 A specification whose locale is the window's frame;
1074 @item
1075 A specification whose locale is the window's frame's device;
1076 @item
1077 A specification whose locale is the symbol @code{global}.
1078 @end enumerate
1079
1080 If all of those fail, then the C-code-provided fallback value for this
1081 specifier is consulted (see @code{specifier-fallback}).  If it is an
1082 inst-list, then this function attempts to instantiate that list just as
1083 when a specification is located in the first five steps above.  If the
1084 fallback is a specifier, @code{specifier-instance} is called recursively
1085 on this specifier and the return value used.  Note, however, that if the
1086 optional argument @var{no-fallback} is non-@code{nil}, the fallback
1087 value will not be consulted.
1088
1089 Note that there may be more than one specification matching a particular
1090 locale; all such specifications are considered before looking for any
1091 specifications for more general locales.  Any particular specification
1092 that is found may be rejected because it is tagged to a particular
1093 device class (e.g. @code{color}) or device type (e.g. @code{x}) or both
1094 and the device for the given domain does not match this, or because the
1095 specification is not valid for the device of the given domain (e.g.  the
1096 font or color name does not exist for this particular X server).
1097
1098 The returned value is dependent on the type of specifier.  For example,
1099 for a font specifier (as returned by the @code{face-font} function), the
1100 returned value will be a font-instance object.  For images, the returned
1101 value will be a string, pixmap, or subwindow.
1102 @end defun
1103
1104 @defun specifier-matching-instance specifier matchspec &optional domain default no-fallback
1105 This function returns an instance for @var{specifier} in @var{domain}
1106 that matches @var{matchspec}.  If no instance can be generated for
1107 @var{domain}, return @var{default}.
1108
1109 This function is identical to @code{specifier-instance} except that a
1110 specification will only be considered if it matches @var{matchspec}.
1111 The definition of ``match,'' and allowed values for @var{matchspec}, are
1112 dependent on the particular type of specifier.  Here are some examples:
1113
1114 @itemize
1115 @item
1116 For chartable (e.g. display table) specifiers, @var{matchspec} should be a
1117 character, and the specification (a chartable) must give a value for
1118 that character in order to be considered.  This allows you to specify,
1119 @emph{e.g.}, a buffer-local display table that only gives values for particular
1120 characters.  All other characters are handled as if the buffer-local
1121 display table is not there. (Chartable specifiers are not yet
1122 implemented.)
1123 @item
1124 For font specifiers, @var{matchspec} should be a charset,
1125 and the specification (a font string) must have
1126 a registry that matches the charset's registry.  (This only makes sense
1127 with Mule support.)  This makes it easy to choose a font that can
1128 display a particular character.  (This is what redisplay does, in fact.)
1129 @end itemize
1130 @end defun
1131
1132 @defun specifier-instance-from-inst-list specifier domain inst-list &optional default
1133 This function attempts to convert a particular inst-list into an
1134 instance.  This attempts to instantiate @var{inst-list} in the given
1135 @var{domain}, as if @var{inst-list} existed in a specification in
1136 @var{specifier}.  If the instantiation fails, @var{default} is returned.
1137 In most circumstances, you should not use this function; use
1138 @code{specifier-instance} instead.
1139 @end defun
1140
1141 @node Specifier Examples
1142 @section Examples of Specifier Usage
1143
1144 Now let us present an example to clarify the theoretical discussions we
1145 have been through.  In this example, we will use the general specifier
1146 functions for clarity.  Keep in mind that many types of specifiers, and
1147 some other types of objects that are associated with specifiers
1148 (e.g. faces), provide convenience functions making it easier to work
1149 with objects of that type.
1150
1151 Let us consider the background color of the default face.  A specifier
1152 is used to specify how that color will appear in different domains.
1153 First, let's retrieve the specifier:
1154
1155 @example
1156 (setq sp (face-property 'default 'background))
1157     @result{}   #<color-specifier 0x3da>
1158 @end example
1159
1160 @example
1161 (specifier-specs sp)
1162     @result{}   ((#<buffer "device.c"> (nil . "forest green"))
1163                  (#<window on "Makefile" 0x8a2b> (nil . "hot pink"))
1164                  (#<x-frame "emacs" 0x4ac> (nil . "puke orange")
1165                                            (nil . "moccasin"))
1166                  (#<x-frame "VM" 0x4ac> (nil . "magenta"))
1167                  (global ((tty) . "cyan") (nil . "white"))
1168                 )
1169 @end example
1170
1171 Then, say we want to determine what the background color of the default
1172 face is for the window currently displaying the buffer @samp{*scratch*}.
1173 We call
1174
1175 @example
1176 (get-buffer-window "*scratch*")
1177     @result{} #<window on "*scratch*" 0x4ad>
1178 (window-frame (get-buffer-window "*scratch*"))
1179     @result{} #<x-frame "emacs" 0x4ac>
1180 (specifier-instance sp (get-buffer-window "*scratch*"))
1181     @result{} #<color-instance moccasin 47=(FFFF,E4E4,B5B5) 0x6309>
1182 @end example
1183
1184 Note that we passed a window to @code{specifier-instance}, not a buffer.
1185 We cannot pass a buffer because a buffer by itself does not provide enough
1186 information.  The buffer might not be displayed anywhere at all, or
1187 could be displayed in many different frames on different devices.
1188
1189 The result is arrived at like this:
1190
1191 @enumerate
1192 @item
1193 First, we look for a specification matching the buffer displayed in the
1194 window, i.e. @samp{*scratch*}.  There are none, so we proceed.
1195 @item
1196 Then, we look for a specification matching the window itself.  Again, there
1197 are none.
1198 @item
1199 Then, we look for a specification matching the window's frame.  The
1200 specification @code{(#<x-frame "emacs" 0x4ac> . "puke orange")} is
1201 found.  We call the instantiation method for colors, passing it the
1202 locale we were searching over (i.e. the window, in this case) and the
1203 instantiator (@samp{"puke orange"}).  However, the particular device
1204 which this window is on (let's say it's an X connection) doesn't
1205 recognize the color @samp{"puke orange"}, so the specification is
1206 rejected.
1207 @item
1208 So we continue looking for a specification matching the window's frame.
1209 We find @samp{(#<x-frame "emacs" 0x4ac> . "moccasin")}.  Again, we
1210 call the instantiation method for colors.  This time, the X server
1211 our window is on recognizes the color @samp{moccasin}, and so the
1212 instantiation method succeeds and returns a color instance.
1213 @end enumerate
1214
1215 Here's another example, which implements something like GNU Emacs's
1216 ``frame-local'' variables.
1217
1218 @example
1219 ;; Implementation
1220
1221 ;; There are probably better ways to write this macro
1222 ;; Heaven help you if VAR is a buffer-local; you will become very
1223 ;; confused.  Probably should error on that.
1224 (defmacro define-frame-local-variable (var)
1225   "Make the unbound symbol VAR become a frame-local variable."
1226   (let ((val (if (boundp var) (symbol-value var) nil)))
1227     `(progn
1228       (setq ,var (make-specifier 'generic))
1229       (add-spec-to-specifier ,var ',val 'global))))
1230
1231 ;; I'm not real happy about this terminology, how can `setq' be a defun?
1232 ;; But `frame-set' would have people writing "(frame-set 'foo value)".
1233 (defun frame-setq (var value &optional frame)
1234   "Set the local value of VAR to VALUE in FRAME.
1235
1236 FRAME defaults to the selected frame."
1237   (and frame (not (framep frame))
1238        (error 'invalid-argument "FRAME must be a frame", frame))
1239   (add-spec-to-specifier var value (or frame (selected-frame))))
1240
1241 (defun frame-value (var &optional frame)
1242   "Get the local value of VAR in FRAME.
1243
1244 FRAME defaults to the selected frame."
1245   (and frame (not (framep frame))
1246        (error 'invalid-argument "FRAME must be a frame", frame))
1247   ;; this is not just a map from frames to values; it also falls back
1248   ;; to the global value
1249   (specifier-instance var (or frame (selected-frame))))
1250
1251 ;; for completeness
1252 (defun frame-set-default (var value)
1253   "Set the default value of frame-local variable VAR to VALUE."
1254   (add-spec-to-specifier var value 'global))
1255
1256 (defun frame-get-default (var)
1257   "Get the default value of frame-local variable VAR."
1258   (car (specifier-specs var 'global)))
1259 @end example
1260
1261 Now you can execute the above definitions (eg, with @code{eval-last-sexp})
1262 and switch to @file{*scratch*} to play.  Things will work differently if
1263 you already have a variable named @code{foo}.
1264
1265 @example
1266 ;; Usage
1267
1268 foo
1269 @error{} Symbol's value as variable is void: foo
1270
1271 (define-frame-local-variable foo)
1272 @result{} nil
1273
1274 ;; the value of foo is a specifier, which is an opaque object;
1275 ;; you must use accessor functions to get values
1276
1277 foo
1278 @result{} #<generic-specifier global=(nil) 0x4f5cb>
1279
1280 ;; since no frame-local value is set, the global value (which is the
1281 ;; constant `nil') is returned
1282 (frame-value foo)
1283 @result{} nil
1284
1285 ;; get the default explicitly
1286 (frame-get-default foo)
1287 @result{} nil
1288
1289 ;; get the whole specification list
1290 (specifier-specs foo 'global)
1291 @result{} (nil)
1292
1293 ;; give foo a frame-local value
1294
1295 (frame-setq foo 'bar)
1296 @result{} nil
1297
1298 ;; access foo in several ways
1299
1300 ;; Note that the print function for this kind of specifier only
1301 ;; gives you the global setting.  To get the full list of specs for
1302 ;; debugging or study purposes, you must use specifier-specs or
1303 ;; specifier-spec-list.
1304 foo
1305 @result{} #<generic-specifier global=(nil) 0x4f5cb>
1306
1307 ;; get the whole specification list
1308 (specifier-specs foo)
1309 @result{} ((#<x-frame "Message" 0x1bd66> (nil . bar)) (global (nil)))
1310
1311 ;; get the frame-local value
1312 (frame-value foo)
1313 @result{} bar
1314
1315 ;; get the default explicitly
1316 (frame-get-default foo)
1317 @result{} nil
1318
1319 ;; Switch to another frame and evaluate:
1320 ;; C-x 5 o M-: (frame-setq foo 'baz) RET M-: (frame-value foo) RET
1321 @result{} baz
1322
1323 ;; Switch back.
1324 ;; C-x 5 o
1325 (specifier-specs foo)
1326 @result{} ((#<x-frame "emacs" 0x28ec> (nil . baz))
1327     (#<x-frame "Message" 0x1bd66> (nil . bar))
1328     (global (nil)))
1329
1330 (frame-value foo)
1331 @result{} bar
1332
1333 (frame-get-default foo)
1334 @result{} nil
1335 @end example
1336
1337 Note that since specifiers generalize both frame-local and buffer-local
1338 variables in a sensible way, XEmacs is not likely to put a high priority
1339 on implementing frame-local variables.
1340 @c Thanks to Jerry James for the following explanation.  He is not
1341 @c responsible for its use here, Stephen Turnbull is.
1342 In fact, some developers think that frame-local variables are evil for
1343 the same reason that buffer-local variables are evil: the declaration is
1344 both global and invisible.  That is, you cannot tell whether a variable
1345 is ``normal,'' buffer-local, or frame-local just by looking at it.  So
1346 if you have namespace management problems, and some other Lisp package
1347 happens to use a variable name that you already declared frame- or
1348 buffer-local, weird stuff happens, and it is extremely hard to track
1349 down.
1350
1351
1352 @node Creating Specifiers
1353 @section Creating New Specifier Objects
1354
1355 @defun make-specifier type
1356 This function creates a new specifier.
1357
1358 A specifier is an object that can be used to keep track of a property
1359 whose value can be per-buffer, per-window, per-frame, or per-device,
1360 and can further be restricted to a particular device-type or device-class.
1361 Specifiers are used, for example, for the various built-in properties of a
1362 face; this allows a face to have different values in different frames,
1363 buffers, etc.  For more information, see @code{specifier-instance},
1364 @code{specifier-specs}, and @code{add-spec-to-specifier}; or, for a detailed
1365 description of specifiers, including how they are instantiated over a
1366 particular domain (i.e. how their value in that domain is determined),
1367 see the chapter on specifiers in the XEmacs Lisp Reference Manual.
1368
1369 @var{type} specifies the particular type of specifier, and should be one
1370 of the symbols @code{generic}, @code{integer}, @code{natnum},
1371 @code{boolean}, @code{color}, @code{font}, @code{image},
1372 @code{face-boolean}, or @code{toolbar}.
1373
1374 For more information on particular types of specifiers, see the
1375 functions @code{make-generic-specifier}, @code{make-integer-specifier},
1376 @code{make-natnum-specifier}, @code{make-boolean-specifier},
1377 @code{make-color-specifier}, @code{make-font-specifier},
1378 @code{make-image-specifier}, @code{make-face-boolean-specifier}, and
1379 @code{make-toolbar-specifier}.
1380 @end defun
1381
1382 @defun make-specifier-and-init type spec-list &optional dont-canonicalize
1383 This function creates and initializes a new specifier.
1384
1385 This is a convenience API combining @code{make-specifier} and
1386 @code{set-specifier} that allows you to create
1387 a specifier and add specs to it at the same time.  @var{type} specifies
1388 the specifier type.  Allowed types are as for @code{make-specifier}.
1389
1390 @var{spec-list} supplies the specification(s) to be
1391 added to the specifier.  Any abbreviation of
1392 the full spec-list form accepted by @code{canonicalize-spec-list} may
1393 be used.
1394 However, if the optional argument @var{dont-canonicalize} is non-@code{nil},
1395 canonicalization is not performed, and the @var{spec-list} must already
1396 be in full form.
1397 @end defun
1398
1399 @defun make-integer-specifier spec-list
1400
1401 Return a new @code{integer} specifier object with the given
1402 specification list.  @var{spec-list} can be a list of specifications
1403 (each of which is a cons of a locale and a list of instantiators), a
1404 single instantiator, or a list of instantiators.
1405
1406 Valid instantiators for integer specifiers are integers.
1407 @end defun
1408
1409 @defun make-boolean-specifier spec-list
1410
1411 Return a new @code{boolean} specifier object with the given
1412 specification list.  @var{spec-list} can be a list of specifications
1413 (each of which is a cons of a locale and a list of instantiators), a
1414 single instantiator, or a list of instantiators.
1415
1416 Valid instantiators for boolean specifiers are @code{t} and @code{nil}.
1417 @end defun
1418
1419 @defun make-natnum-specifier spec-list
1420
1421 Return a new @code{natnum} specifier object with the given specification
1422 list.  @var{spec-list} can be a list of specifications (each of which is
1423 a cons of a locale and a list of instantiators), a single instantiator,
1424 or a list of instantiators.
1425
1426 Valid instantiators for natnum specifiers are non-negative integers.
1427 @end defun
1428
1429 @defun make-generic-specifier spec-list
1430
1431 Return a new @code{generic} specifier object with the given
1432 specification list.  @var{spec-list} can be a list of specifications
1433 (each of which is a cons of a locale and a list of instantiators), a
1434 single instantiator, or a list of instantiators.
1435
1436 Valid instantiators for generic specifiers are all Lisp values.  They
1437 are returned back unchanged when a specifier is instantiated.
1438 @end defun
1439
1440 @defun make-display-table-specifier spec-list
1441
1442 Return a new @code{display-table} specifier object with the given spec
1443 list.  @var{spec-list} can be a list of specifications (each of which is
1444 a cons of a locale and a list of instantiators), a single instantiator,
1445 or a list of instantiators.
1446
1447 Valid instantiators for display-table specifiers are described in detail
1448 in the doc string for @code{current-display-table} (@pxref{Active
1449 Display Table}).
1450 @end defun
1451
1452 @node Specifier Validation Functions
1453 @section Functions for Checking the Validity of Specifier Components
1454
1455 @defun valid-specifier-domain-p domain
1456 This function returns non-@code{nil} if @var{domain} is a valid
1457 specifier domain.  A domain is used to instance a specifier
1458 (i.e. determine the specifier's value in that domain).  Valid domains
1459 are a window, frame, or device.  (@code{nil} is not valid.)
1460 @end defun
1461
1462 @defun valid-specifier-locale-p locale
1463 This function returns non-@code{nil} if @var{locale} is a valid
1464 specifier locale.  Valid locales are a device, a frame, a window, a
1465 buffer, and @code{global}.  (@code{nil} is not valid.)
1466 @end defun
1467
1468 @defun valid-specifier-locale-type-p locale-type
1469 Given a specifier @var{locale-type}, this function returns non-@code{nil} if it
1470 is valid.  Valid locale types are the symbols @code{global},
1471 @code{device}, @code{frame}, @code{window}, and @code{buffer}. (Note,
1472 however, that in functions that accept either a locale or a locale type,
1473 @code{global} is considered an individual locale.)
1474 @end defun
1475
1476 @defun valid-specifier-type-p specifier-type
1477 Given a @var{specifier-type}, this function returns non-@code{nil} if it
1478 is valid.  Valid types are @code{generic}, @code{integer},
1479 @code{boolean}, @code{color}, @code{font}, @code{image},
1480 @code{face-boolean}, and @code{toolbar}.
1481 @end defun
1482
1483 @defun valid-specifier-tag-p tag
1484 This function returns non-@code{nil} if @var{tag} is a valid specifier
1485 tag.
1486 @end defun
1487
1488 @defun valid-instantiator-p instantiator specifier-type
1489 This function returns non-@code{nil} if @var{instantiator} is valid for
1490 @var{specifier-type}.
1491 @end defun
1492
1493 @defun valid-inst-list-p inst-list type
1494 This function returns non-@code{nil} if @var{inst-list} is valid for
1495 specifier type @var{type}.
1496 @end defun
1497
1498 @defun valid-spec-list-p spec-list type
1499 This function returns non-@code{nil} if @var{spec-list} is valid for
1500 specifier type @var{type}.
1501 @end defun
1502
1503 @defun check-valid-instantiator instantiator specifier-type
1504 This function signals an error if @var{instantiator} is invalid for
1505 @var{specifier-type}.
1506 @end defun
1507
1508 @defun check-valid-inst-list inst-list type
1509 This function signals an error if @var{inst-list} is invalid for
1510 specifier type @var{type}.
1511 @end defun
1512
1513 @defun check-valid-spec-list spec-list type
1514 This function signals an error if @var{spec-list} is invalid for
1515 specifier type @var{type}.
1516 @end defun
1517
1518 @node Other Specification Functions
1519 @section Other Functions for Working with Specifications in a Specifier
1520
1521 @defun copy-specifier specifier &optional dest locale tag-set exact-p how-to-add
1522 This function copies @var{specifier} to @var{dest}, or creates a new one
1523 if @var{dest} is @code{nil}.
1524
1525 If @var{dest} is @code{nil} or omitted, a new specifier will be created
1526 and the specifications copied into it.  Otherwise, the specifications
1527 will be copied into the existing specifier in @var{dest}.
1528
1529 If @var{locale} is @code{nil} or the symbol @code{all}, all
1530 specifications will be copied.  If @var{locale} is a particular locale,
1531 the specification for that particular locale will be copied.  If
1532 @var{locale} is a locale type, the specifications for all locales of
1533 that type will be copied.  @var{locale} can also be a list of locales,
1534 locale types, and/or @code{all}; this is equivalent to calling
1535 @code{copy-specifier} for each of the elements of the list.  See
1536 @code{specifier-spec-list} for more information about @var{locale}.
1537
1538 Only instantiators where @var{tag-set} (a list of zero or more tags) is
1539 a subset of (or possibly equal to) the instantiator's tag set are
1540 copied.  (The default value of @code{nil} is a subset of all tag sets,
1541 so in this case no instantiators will be screened out.) If @var{exact-p}
1542 is non-@code{nil}, however, @var{tag-set} must be equal to an
1543 instantiator's tag set for the instantiator to be copied.
1544
1545 Optional argument @var{how-to-add} specifies what to do with existing
1546 specifications in @var{dest}.  If @code{nil}, then whichever locales or locale
1547 types are copied will first be completely erased in @var{dest}.
1548 Otherwise, it is the same as in @code{add-spec-to-specifier}.
1549 @end defun
1550
1551 @defun remove-specifier specifier &optional locale tag-set exact-p
1552 This function removes specification(s) for @var{specifier}.
1553
1554 If @var{locale} is a particular locale (a buffer, window, frame, device,
1555 or the symbol @code{global}), the specification for that locale will be
1556 removed.
1557
1558 If instead, @var{locale} is a locale type (i.e. a symbol @code{buffer},
1559 @code{window}, @code{frame}, or @code{device}), the specifications for
1560 all locales of that type will be removed.
1561
1562 If @var{locale} is @code{nil} or the symbol @code{all}, all
1563 specifications will be removed.
1564
1565 @var{locale} can also be a list of locales, locale types, and/or
1566 @code{all}; this is equivalent to calling @code{remove-specifier} for
1567 each of the elements in the list.
1568
1569 Only instantiators where @var{tag-set} (a list of zero or more tags) is
1570 a subset of (or possibly equal to) the instantiator's tag set are
1571 removed.  (The default value of @code{nil} is a subset of all tag sets,
1572 so in this case no instantiators will be screened out.) If @var{exact-p}
1573 is non-@code{nil}, however, @var{tag-set} must be equal to an
1574 instantiator's tag set for the instantiator to be removed.
1575 @end defun
1576
1577 @defun map-specifier specifier func &optional locale maparg
1578 This function applies @var{func} to the specification(s) for
1579 @var{locale} in @var{specifier}.
1580
1581 If optional @var{locale} is a locale, @var{func} will be called for that
1582 locale.
1583 If @var{locale} is a locale type, @var{func} will be mapped over all
1584 locales of that type.  If @var{locale} is @code{nil} or the symbol
1585 @code{all}, @var{func} will be mapped over all locales in
1586 @var{specifier}.
1587
1588 Optional @var{ms-maparg} will be passed to @var{ms-func}.
1589
1590 @var{func} is called with four arguments: the @var{specifier}, the
1591 locale being mapped over, the inst-list for that locale, and the
1592 optional @var{maparg}.  If any invocation of @var{func} returns
1593 non-@code{nil}, the mapping will stop and the returned value becomes the
1594 value returned from @code{map-specifier}.  Otherwise,
1595 @code{map-specifier} returns @code{nil}.
1596 @end defun
1597
1598 @defun specifier-locale-type-from-locale locale
1599 Given a specifier @var{locale}, this function returns its type.
1600 @end defun