All of SXEmacs' http URLs are now https. WooHoo!
[sxemacs] / info / lispref / strings.texi
1 @c -*-texinfo-*-
2 @c This is part of the SXEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c Copyright (C) 2005 Sebastian Freundt <hroptatyr@sxemacs.org>
5 @c See the file lispref.texi for copying conditions.
6 @setfilename ../../info/strings.info
7
8 @node Strings and Characters, Lists, Numbers, Top
9 @chapter Strings and Characters
10 @cindex strings
11 @cindex character arrays
12 @cindex characters
13 @cindex bytes
14
15   A string in SXEmacs Lisp is an array that contains an ordered sequence
16 of characters.  Strings are used as names of symbols, buffers, and
17 files, to send messages to users, to hold text being copied between
18 buffers, and for many other purposes.  Because strings are so important,
19 SXEmacs Lisp has many functions expressly for manipulating them.  SXEmacs
20 Lisp programs use strings more often than individual characters.
21
22 @menu
23 * String Basics::             Basic properties of strings and characters.
24 * Predicates for Strings::    Testing whether an object is a string or char.
25 * Creating Strings::          Functions to allocate new strings.
26 * Predicates for Characters:: Testing whether an object is a character.
27 * Character Codes::           Each character has an equivalent integer.
28 * Text Comparison::           Comparing characters or strings.
29 * String Conversion::         Converting characters or strings and vice versa.
30 * Modifying Strings::         Changing characters in a string.
31 * String Properties::         Additional information attached to strings.
32 * Formatting Strings::        @code{format}: SXEmacs's analog of @code{printf}.
33 * Character Case::            Case conversion functions.
34 * Case Tables::               Customizing case conversion.
35 * Char Tables::               Mapping from characters to Lisp objects.
36 @end menu
37
38
39 @node String Basics, Predicates for Strings, Strings and Characters, Strings and Characters
40 @section String and Character Basics
41
42   Strings in SXEmacs Lisp are arrays that contain an ordered sequence of
43 characters.  Characters are their own primitive object type in SXEmacs
44 since XEmacs 20.
45
46 However, in XEmacs 19, characters are represented in XEmacs Lisp as
47 integers; whether an integer was intended as a character or not is
48 determined only by how it is used.  @xref{Character Type}.
49
50   The length of a string (like any array) is fixed and independent of
51 the string contents, and cannot be altered.  Strings in Lisp are
52 @emph{not} terminated by a distinguished character code.  (By contrast,
53 strings in C are terminated by a character with @sc{ascii} code 0.)
54 This means that any character, including the null character (@sc{ascii}
55 code 0), is a valid element of a string.@refill
56
57   Since strings are considered arrays, you can operate on them with the
58 general array functions.  (@xref{Sequences Arrays Vectors}.)  For
59 example, you can access or change individual characters in a string
60 using the functions @code{aref} and @code{aset} (@pxref{Array
61 Functions}).
62
63   Strings use an efficient representation for storing the characters
64 in them, and thus take up much less memory than a vector of the same
65 length.
66
67   Sometimes you will see strings used to hold key sequences.  This
68 exists for backward compatibility with Emacs 18, but should @emph{not}
69 be used in new code, since many key chords can't be represented at
70 all and others (in particular meta key chords) are confused with
71 accented characters.
72
73 @ignore @c Not accurate any more
74   Each character in a string is stored in a single byte.  Therefore,
75 numbers not in the range 0 to 255 are truncated when stored into a
76 string.  This means that a string takes up much less memory than a
77 vector of the same length.
78
79   Sometimes key sequences are represented as strings.  When a string is
80 a key sequence, string elements in the range 128 to 255 represent meta
81 characters (which are extremely large integers) rather than keyboard
82 events in the range 128 to 255.
83
84   Strings cannot hold characters that have the hyper, super or alt
85 modifiers; they can hold @sc{ASCII} control characters, but no other
86 control characters.  They do not distinguish case in @sc{ASCII} control
87 characters.  @xref{Character Type}, for more information about
88 representation of meta and other modifiers for keyboard input
89 characters.
90 @end ignore
91
92   Strings are useful for holding regular expressions.  You can also
93 match regular expressions against strings (@pxref{Regexp Search}).  The
94 functions @code{match-string} (@pxref{Simple Match Data}) and
95 @code{replace-match} (@pxref{Replacing Match}) are useful for
96 decomposing and modifying strings based on regular expression matching.
97
98   Like a buffer, a string can contain extents in it.  These extents are
99 created when a function such as @code{buffer-substring} is called on a
100 region with duplicable extents in it.  When the string is inserted into
101 a buffer, the extents are inserted along with it.  @xref{Duplicable
102 Extents}.
103
104   @xref{Text}, for information about functions that display strings or
105 copy them into buffers.  @xref{Character Type}, and @ref{String Type},
106 for information about the syntax of characters and strings.
107
108
109 @node Predicates for Strings
110 @section The Predicates for Strings
111
112 For more information about general sequence and array predicates,
113 see @ref{Sequences Arrays Vectors}, and @ref{Arrays}.
114
115 @defun stringp object
116   This function returns @code{t} if @var{object} is a string, @code{nil}
117 otherwise.
118 @end defun
119
120 @defun char-or-string-p object
121   This function returns @code{t} if @var{object} is a string or a
122 character, @code{nil} otherwise.
123
124 In SXEmacs addition, this function also returns @code{t} if @var{object}
125 is an integer that can be represented as a character.  This is because
126 of compatibility with previous XEmacs and should not be depended on.
127 @end defun
128
129
130 @node Creating Strings
131 @section Creating Strings
132
133   The following functions create strings, either from scratch, or by
134 putting strings together, or by taking them apart.
135
136 @defun string &rest characters
137   This function returns a new string made up of @var{characters}.
138
139 @example
140 (string ?S ?X ?E ?m ?a ?c ?s)
141      @result{} "SXEmacs"
142 (string)
143      @result{} ""
144 @end example
145
146 Analogous functions operating on other data types include @code{list},
147 @code{cons} (@pxref{Building Lists}), @code{vector} (@pxref{Vectors})
148 and @code{bit-vector} (@pxref{Bit Vectors}).  This function has not been
149 available in XEmacs prior to 21.0 and FSF Emacs prior to 20.3.
150 @end defun
151
152 @defun make-string length character
153 This function returns a new string consisting entirely of @var{length}
154 successive copies of @var{character}.  @var{length} must be a
155 non-negative integer.
156
157 @example
158 (make-string 5 ?x)
159      @result{} "xxxxx"
160 (make-string 0 ?x)
161      @result{} ""
162 @end example
163
164   Other functions to compare with this one include @code{char-to-string}
165 (@pxref{String Conversion}), @code{make-vector} (@pxref{Vectors}), and
166 @code{make-list} (@pxref{Building Lists}).
167 @end defun
168
169 @defun substring string start &optional end
170 This function returns a new string which consists of those characters
171 from @var{string} in the range from (and including) the character at the
172 index @var{start} up to (but excluding) the character at the index
173 @var{end}.  The first character is at index zero.
174
175 @example
176 @group
177 (substring "abcdefg" 0 3)
178      @result{} "abc"
179 @end group
180 @end example
181
182 @noindent
183 Here the index for @samp{a} is 0, the index for @samp{b} is 1, and the
184 index for @samp{c} is 2.  Thus, three letters, @samp{abc}, are copied
185 from the string @code{"abcdefg"}.  The index 3 marks the character
186 position up to which the substring is copied.  The character whose index
187 is 3 is actually the fourth character in the string.
188
189 A negative number counts from the end of the string, so that @minus{}1
190 signifies the index of the last character of the string.  For example:
191
192 @example
193 @group
194 (substring "abcdefg" -3 -1)
195      @result{} "ef"
196 @end group
197 @end example
198
199 @noindent
200 In this example, the index for @samp{e} is @minus{}3, the index for
201 @samp{f} is @minus{}2, and the index for @samp{g} is @minus{}1.
202 Therefore, @samp{e} and @samp{f} are included, and @samp{g} is excluded.
203
204 When @code{nil} is used as an index, it stands for the length of the
205 string.  Thus,
206
207 @example
208 @group
209 (substring "abcdefg" -3 nil)
210      @result{} "efg"
211 @end group
212 @end example
213
214 Omitting the argument @var{end} is equivalent to specifying @code{nil}.
215 It follows that @code{(substring @var{string} 0)} returns a copy of all
216 of @var{string}.
217
218 @example
219 @group
220 (substring "abcdefg" 0)
221      @result{} "abcdefg"
222 @end group
223 @end example
224
225 @noindent
226 But we recommend @code{copy-sequence} for this purpose (@pxref{Sequence
227 Functions}).
228
229 If the characters copied from @var{string} have duplicable extents or
230 text properties, those are copied into the new string also.
231 @xref{Duplicable Extents}.
232
233 A @code{wrong-type-argument} error is signaled if either @var{start} or
234 @var{end} is not an integer or @code{nil}.  An @code{args-out-of-range}
235 error is signaled if @var{start} indicates a character following
236 @var{end}, or if either integer is out of range for @var{string}.
237
238 Contrast this function with @code{buffer-substring} (@pxref{Buffer
239 Contents}), which returns a string containing a portion of the text in
240 the current buffer.  The beginning of a string is at index 0, but the
241 beginning of a buffer is at index 1.
242 @end defun
243
244 @defun concat &rest sequences
245 @cindex copying strings
246 @cindex concatenating strings
247 This function returns a new string consisting of the characters in the
248 arguments passed to it (along with their text properties, if any).  The
249 arguments may be strings, lists of numbers, or vectors of numbers; they
250 are not themselves changed.  If @code{concat} receives no arguments, it
251 returns an empty string.
252
253 @example
254 (concat "abc" "-def")
255      @result{} "abc-def"
256 (concat "abc" (list 120 (+ 256 121)) [122])
257      @result{} "abcxyz"
258 ;; @r{@code{nil} is an empty sequence.}
259 (concat "abc" nil "-def")
260      @result{} "abc-def"
261 (concat "The " "quick brown " "fox.")
262      @result{} "The quick brown fox."
263 (concat)
264      @result{} ""
265 @end example
266
267 @noindent
268 The second example above shows how characters stored in strings are
269 taken modulo 256.  In other words, each character in the string is
270 stored in one byte.
271
272 The @code{concat} function always constructs a new string that is
273 not @code{eq} to any existing string.
274
275 When an argument is an integer (not a sequence of integers), it is
276 converted to a string of digits making up the decimal printed
277 representation of the integer.  @strong{Don't use this feature; we plan
278 to eliminate it.  If you already use this feature, change your programs
279 now!}  The proper way to convert an integer to a decimal number in this
280 way is with @code{format} (@pxref{Formatting Strings}) or
281 @code{number-to-string} (@pxref{String Conversion}).
282
283 @example
284 @group
285 (concat 137)
286      @result{} "137"
287 (concat 54 321)
288      @result{} "54321"
289 @end group
290 @end example
291
292 For information about other concatenation functions, see the description
293 of @code{mapconcat} in @ref{Mapping Functions}, @code{vconcat} in
294 @ref{Vectors}, @code{bvconcat} in @ref{Bit Vectors}, and @code{append}
295 in @ref{Building Lists}.
296 @end defun
297
298
299 @node Predicates for Characters
300 @section The Predicates for Characters
301
302 @defun characterp object
303 This function returns @code{t} if @var{object} is a character.
304
305 Some functions that work on integers (e.g. the comparison functions
306 <, <=, =, /=, etc. and the arithmetic functions +, -, *, etc.)
307 accept characters and implicitly convert them into integers.  In
308 general, functions that work on characters also accept char-ints and
309 implicitly convert them into characters.  WARNING: Neither of these
310 behaviors is very desirable, and they are maintained for backward
311 compatibility with old E-Lisp programs that confounded characters and
312 integers willy-nilly.  These behaviors may change in the future; therefore,
313 do not rely on them.  Instead, convert the characters explicitly
314 using @code{char-int}.
315 @end defun
316
317 @defun integer-or-char-p object
318 This function returns @code{t} if @var{object} is an integer or character.
319 @end defun
320
321
322 @node Character Codes
323 @section Character Codes
324
325 @defun char-int character
326 This function converts a character into an equivalent integer.
327 The resulting integer will always be non-negative.  The integers in
328 the range 0 - 255 map to characters as follows:
329
330 @table @asis
331 @item 0 - 31
332 Control set 0
333 @item 32 - 127
334 @sc{ascii}
335 @item 128 - 159
336 Control set 1
337 @item 160 - 255
338 Right half of ISO-8859-1
339 @end table
340
341 If support for @sc{mule} does not exist, these are the only valid
342 character values.  When @sc{mule} support exists, the values assigned to
343 other characters may vary depending on the particular version of SXEmacs,
344 the order in which character sets were loaded, etc., and you should not
345 depend on them.
346 @end defun
347
348 @defun int-char integer
349 This function converts an integer into the equivalent character.  Not
350 all integers correspond to valid characters; use @code{char-int-p} to
351 determine whether this is the case.  If the integer cannot be converted,
352 @code{nil} is returned.
353 @end defun
354
355 @defun char-int-p object
356 This function returns @code{t} if @var{object} is an integer that can be
357 converted into a character.
358 @end defun
359
360 @defun char-or-char-int-p object
361 This function returns @code{t} if @var{object} is a character or an
362 integer that can be converted into one.
363 @end defun
364
365 @need 2000
366 @node Text Comparison
367 @section Comparison of Characters and Strings
368 @cindex string equality
369
370 @defun char-equal character1 character2 &optional buffer
371 This function returns @code{t} if the arguments represent the same
372 character, @code{nil} otherwise.  This function ignores differences
373 in case if the value of @code{case-fold-search} is non-@code{nil} in
374 @var{buffer}, which defaults to the current buffer.
375
376 @example
377 (char-equal ?x ?x)
378      @result{} t
379 (let ((case-fold-search t))
380   (char-equal ?x ?X))
381      @result{} t
382 (let ((case-fold-search nil))
383   (char-equal ?x ?X))
384      @result{} nil
385 @end example
386 @end defun
387
388 @defun char= character1 character2
389 This function returns @code{t} if the arguments represent the same
390 character, @code{nil} otherwise.  Case is significant.
391
392 @example
393 (char= ?x ?x)
394      @result{} t
395 (char= ?x ?X)
396      @result{} nil
397 (let ((case-fold-search t))
398   (char-equal ?x ?X))
399      @result{} nil
400 (let ((case-fold-search nil))
401   (char-equal ?x ?X))
402      @result{} nil
403 @end example
404 @end defun
405
406 @defun string= string1 string2
407 This function returns @code{t} if the characters of the two strings
408 match exactly; case is significant.
409
410 @example
411 (string= "abc" "abc")
412      @result{} t
413 (string= "abc" "ABC")
414      @result{} nil
415 (string= "ab" "ABC")
416      @result{} nil
417 @end example
418
419 @ignore @c `equal' in SXEmacs does not compare text properties
420 The function @code{string=} ignores the text properties of the
421 two strings.  To compare strings in a way that compares their text
422 properties also, use @code{equal} (@pxref{Equality Predicates}).
423 @end ignore
424 @end defun
425
426 @defun string-equal string1 string2
427 @code{string-equal} is another name for @code{string=}.
428 @end defun
429
430 @cindex lexical comparison
431 @defun string< string1 string2
432 @c (findex string< causes problems for permuted index!!)
433 This function compares two strings a character at a time.  First it
434 scans both the strings at once to find the first pair of corresponding
435 characters that do not match.  If the lesser character of those two is
436 the character from @var{string1}, then @var{string1} is less, and this
437 function returns @code{t}.  If the lesser character is the one from
438 @var{string2}, then @var{string1} is greater, and this function returns
439 @code{nil}.  If the two strings match entirely, the value is @code{nil}.
440
441 Pairs of characters are compared by their @sc{ascii} codes.  Keep in
442 mind that lower case letters have higher numeric values in the
443 @sc{ascii} character set than their upper case counterparts; numbers and
444 many punctuation characters have a lower numeric value than upper case
445 letters.
446
447 @example
448 @group
449 (string< "abc" "abd")
450      @result{} t
451 (string< "abd" "abc")
452      @result{} nil
453 (string< "123" "abc")
454      @result{} t
455 @end group
456 @end example
457
458 When the strings have different lengths, and they match up to the
459 length of @var{string1}, then the result is @code{t}.  If they match up
460 to the length of @var{string2}, the result is @code{nil}.  A string of
461 no characters is less than any other string.
462
463 @example
464 @group
465 (string< "" "abc")
466      @result{} t
467 (string< "ab" "abc")
468      @result{} t
469 (string< "abc" "")
470      @result{} nil
471 (string< "abc" "ab")
472      @result{} nil
473 (string< "" "")
474      @result{} nil
475 @end group
476 @end example
477 @end defun
478
479 @defun string-lessp string1 string2
480 @code{string-lessp} is another name for @code{string<}.
481 @end defun
482
483   See also @code{compare-buffer-substrings} in @ref{Comparing Text}, for
484 a way to compare text in buffers.  The function @code{string-match},
485 which matches a regular expression against a string, can be used
486 for a kind of string comparison; see @ref{Regexp Search}.
487
488
489 @node String Conversion
490 @section Conversion of Characters and Strings
491 @cindex conversion of strings
492
493   This section describes functions for conversions between characters,
494 strings and integers.  @code{format} and @code{prin1-to-string}
495 (@pxref{Output Functions}) can also convert Lisp objects into strings.
496 @code{read-from-string} (@pxref{Input Functions}) can ``convert'' a
497 string representation of a Lisp object into an object.
498
499   @xref{Documentation}, for functions that produce textual descriptions
500 of text characters and general input events
501 (@code{single-key-description} and @code{text-char-description}).  These
502 functions are used primarily for making help messages.
503
504 @defun char-to-string character
505 @cindex character to string
506   This function returns a new string with a length of one character.
507 The value of @var{character}, modulo 256, is used to initialize the
508 element of the string.
509
510 This function is similar to @code{make-string} with an integer argument
511 of 1.  (@xref{Creating Strings}.)  This conversion can also be done with
512 @code{format} using the @samp{%c} format specification.
513 (@xref{Formatting Strings}.)
514
515 @example
516 (char-to-string ?x)
517      @result{} "x"
518 (char-to-string (+ 256 ?x))
519      @result{} "x"
520 (make-string 1 ?x)
521      @result{} "x"
522 @end example
523 @end defun
524
525 @defun string-to-char string
526 @cindex string to character
527   This function returns the first character in @var{string}.  If the
528 string is empty, the function returns 0. (Under XEmacs 19, the value is
529 also 0 when the first character of @var{string} is the null character,
530 @sc{ascii} code 0.)
531
532 @example
533 (string-to-char "ABC")
534      @result{} ?A   ;; @r{Under SXemacs.}
535      @result{} 65   ;; @r{Under XEmacs 19.}
536 (string-to-char "xyz")
537      @result{} ?x   ;; @r{Under SXEmacs.}
538      @result{} 120  ;; @r{Under XEmacs 19.}
539 (string-to-char "")
540      @result{} nil  ;; @r{Under SXEmacs.}
541      @result{} 0    ;; @r{Under XEmacs 20.}
542 (string-to-char "\000")
543      @result{} ?\^@ ;; @r{Under SXEmacs.}
544      @result{} 0    ;; @r{Under XEmacs 20.}
545 @end example
546
547 This function may be eliminated in the future if it does not seem useful
548 enough to retain.
549 @end defun
550
551 @defun number-to-string number
552 @cindex integer to string
553 @cindex integer to decimal
554 This function returns a string consisting of the printed
555 representation of @var{number}, which may be an integer or a floating
556 point number.  The value starts with a sign if the argument is
557 negative.
558
559 @example
560 (number-to-string 256)
561      @result{} "256"
562 (number-to-string -23)
563      @result{} "-23"
564 (number-to-string -23.5)
565      @result{} "-23.5"
566 @end example
567
568 @cindex int-to-string
569 @code{int-to-string} is a semi-obsolete alias for this function.
570
571 See also the function @code{format} in @ref{Formatting Strings}.
572 @end defun
573
574 @defun string-to-number string &optional base
575 @cindex string to number
576 This function returns the numeric value represented by @var{string},
577 read in @var{base}.  It skips spaces and tabs at the beginning of
578 @var{string}, then reads as much of @var{string} as it can interpret as
579 a number.  (On some systems it ignores other whitespace at the
580 beginning, not just spaces and tabs.)  If the first character after the
581 ignored whitespace is not a digit or a minus sign, this function returns
582 0.
583
584 If @var{base} is not specified, it defaults to ten.  With @var{base}
585 other than ten, only integers can be read.
586
587 @example
588 (string-to-number "256")
589      @result{} 256
590 (string-to-number "25 is a perfect square.")
591      @result{} 25
592 (string-to-number "X256")
593      @result{} 0
594 (string-to-number "-4.5")
595      @result{} -4.5
596 (string-to-number "ffff" 16)
597      @result{} 65535
598 @end example
599
600 @findex string-to-int
601 @code{string-to-int} is an obsolete alias for this function.
602 @end defun
603
604
605 @node Modifying Strings
606 @section Modifying Strings
607 @cindex strings, modifying
608
609 You can modify a string using the general array-modifying primitives.
610 @xref{Arrays}.  The function @code{aset} modifies a single character;
611 the function @code{fillarray} sets all characters in the string to
612 a specified character.
613
614 Each string has a tick counter that starts out at zero (when the string
615 is created) and is incremented each time a change is made to that
616 string.
617
618 @defun string-modified-tick string
619 This function returns the tick counter for @samp{string}.
620 @end defun
621
622 @node String Properties
623 @section String Properties
624 @cindex string properties
625 @cindex properties of strings
626
627 Just as with symbols, extents, faces, and glyphs, you can attach
628 additional information to strings in the form of @dfn{string
629 properties}.  These differ from text properties, which are logically
630 attached to particular characters in the string.
631
632 To attach a property to a string, use @code{put}.  To retrieve a property
633 from a string, use @code{get}.  You can also use @code{remprop} to remove
634 a property from a string and @code{object-plist} to retrieve a list of
635 all the properties in a string.
636
637
638 @node Formatting Strings
639 @section Formatting Strings
640 @cindex formatting strings
641 @cindex strings, formatting them
642
643   @dfn{Formatting} means constructing a string by substitution of
644 computed values at various places in a constant string.  This string
645 controls how the other values are printed as well as where they appear;
646 it is called a @dfn{format string}.
647
648   Formatting is often useful for computing messages to be displayed.  In
649 fact, the functions @code{message} and @code{error} provide the same
650 formatting feature described here; they differ from @code{format} only
651 in how they use the result of formatting.
652
653 @defun format string &rest objects
654   This function returns a new string that is made by copying
655 @var{string} and then replacing any format specification
656 in the copy with encodings of the corresponding @var{objects}.  The
657 arguments @var{objects} are the computed values to be formatted.
658 @end defun
659
660 @cindex @samp{%} in format
661 @cindex format specification
662   A format specification is a sequence of characters beginning with a
663 @samp{%}.  Thus, if there is a @samp{%d} in @var{string}, the
664 @code{format} function replaces it with the printed representation of
665 one of the values to be formatted (one of the arguments @var{objects}).
666 For example:
667
668 @example
669 @group
670 (format "The value of fill-column is %d." fill-column)
671      @result{} "The value of fill-column is 72."
672 @end group
673 @end example
674
675   If @var{string} contains more than one format specification, the
676 format specifications correspond with successive values from
677 @var{objects}.  Thus, the first format specification in @var{string}
678 uses the first such value, the second format specification uses the
679 second such value, and so on.  Any extra format specifications (those
680 for which there are no corresponding values) cause unpredictable
681 behavior.  Any extra values to be formatted are ignored.
682
683   Certain format specifications require values of particular types.
684 However, no error is signaled if the value actually supplied fails to
685 have the expected type.  Instead, the output is likely to be
686 meaningless.
687
688   Here is a table of valid format specifications:
689
690 @table @samp
691 @item %s
692 Replace the specification with the printed representation of the object,
693 made without quoting.  Thus, strings are represented by their contents
694 alone, with no @samp{"} characters, and symbols appear without @samp{\}
695 characters.  This is equivalent to printing the object with @code{princ}.
696
697 If there is no corresponding object, the empty string is used.
698
699 @item %S
700 Replace the specification with the printed representation of the object,
701 made with quoting.  Thus, strings are enclosed in @samp{"} characters,
702 and @samp{\} characters appear where necessary before special characters.
703 This is equivalent to printing the object with @code{prin1}.
704
705 If there is no corresponding object, the empty string is used.
706
707 @item %o
708 @cindex integer to octal
709 Replace the specification with the base-eight representation of an
710 integer.
711
712 @item %d
713 @itemx %i
714 Replace the specification with the base-ten representation of an
715 integer.
716
717 @item %x
718 @cindex integer to hexadecimal
719 Replace the specification with the base-sixteen representation of an
720 integer, using lowercase letters.
721
722 @item %X
723 @cindex integer to hexadecimal
724 Replace the specification with the base-sixteen representation of an
725 integer, using uppercase letters.
726
727 @item %b
728 @cindex integer to binary
729 Replace the specification with the base-two representation of an
730 integer.
731
732 @item %c
733 Replace the specification with the character which is the value given.
734
735 @item %e
736 Replace the specification with the exponential notation for a floating
737 point number (e.g. @samp{7.85200e+03}).
738
739 @item %f
740 Replace the specification with the decimal-point notation for a floating
741 point number.
742
743   Please bear in mind that floating point numbers have a limited and
744 fixed precision although the print output may suggest something else.
745 The precision varies (depending on the machine) between 12 and 38
746 digits.  This means if you use specifiers like @samp{%.60f} on
747 @samp{1.0} or @samp{1.5} only the first 12 to 38 digits are real.
748 Also note, that internally numbers are processed in a 2-adic
749 arithmetic, so you may experience strange rounding effects,
750 e.g. @samp{%.60f} on @samp{1.2} or @samp{%f} on @samp{1e+40}, this is
751 because you force the printer to be more precise than actually valid.
752 No error is thrown in these cases!
753
754 @item %g
755 Replace the specification with notation for a floating point number,
756 using a ``pretty format''.  Either exponential notation or decimal-point
757 notation will be used (usually whichever is shorter), and trailing
758 zeroes are removed from the fractional part.
759
760 @item %%
761 A single @samp{%} is placed in the string.  This format specification is
762 unusual in that it does not use a value.  For example, @code{(format "%%
763 %d" 30)} returns @code{"% 30"}.
764 @end table
765
766   If ENT support is compiled in, there are several additional
767 specifiers which may become available, depending on the provided
768 additional libraries.  @pxref{Enhanced Number Types}.
769
770   Any other format character results in an @samp{Invalid format
771 operation} error.
772
773   Here are several examples:
774
775 @example
776 @group
777 (format "The name of this buffer is %s." (buffer-name))
778      @result{} "The name of this buffer is strings.texi."
779
780 (format "The buffer object prints as %s." (current-buffer))
781      @result{} "The buffer object prints as #<buffer strings.texi>."
782
783 (format "The octal value of %d is %o,
784          and the hex value is %x." 18 18 18)
785      @result{} "The octal value of 18 is 22,
786          and the hex value is 12."
787 @end group
788 @end example
789
790   There are many additional flags and specifications that can occur
791 between the @samp{%} and the format character, in the following order:
792
793 @enumerate
794 @item
795 An optional repositioning specification, which is a positive
796 integer followed by a @samp{$}.
797
798 @item
799 Zero or more of the optional flag characters @samp{-}, @samp{+},
800 @samp{ }, @samp{0}, and @samp{#}.
801
802 @item
803 An asterisk (@samp{*}, meaning that the field width is now assumed to
804 have been specified as an argument.
805
806 @item
807 An optional minimum field width.
808
809 @item
810 An optional precision, preceded by a @samp{.} character.
811 @end enumerate
812
813 @cindex repositioning format arguments
814 @cindex multilingual string formatting
815   A @dfn{repositioning} specification changes which argument to
816 @code{format} is used by the current and all following format
817 specifications.  Normally the first specification uses the first
818 argument, the second specification uses the second argument, etc.  Using
819 a repositioning specification, you can change this.  By placing a number
820 @var{n} followed by a @samp{$} between the @samp{%} and the format
821 character, you cause the specification to use the @var{n}th argument.
822 The next specification will use the @var{n}+1'th argument, etc.
823
824 For example:
825
826 @example
827 @group
828 (format "Can't find file `%s' in directory `%s'."
829         "ignatius.c" "loyola/")
830      @result{} "Can't find file `ignatius.c' in directory `loyola/'."
831
832 (format "In directory `%2$s', the file `%1$s' was not found."
833         "ignatius.c" "loyola/")
834      @result{} "In directory `loyola/', the file `ignatius.c' was not found."
835
836 (format
837     "The numbers %d and %d are %1$x and %x in hex and %1$o and %o in octal."
838     37 12)
839 @result{} "The numbers 37 and 12 are 25 and c in hex and 45 and 14 in octal."
840 @end group
841 @end example
842
843 As you can see, this lets you reprocess arguments more than once or
844 reword a format specification (thereby moving the arguments around)
845 without having to actually reorder the arguments.  This is especially
846 useful in translating messages from one language to another: Different
847 languages use different word orders, and this sometimes entails changing
848 the order of the arguments.  By using repositioning specifications,
849 this can be accomplished without having to embed knowledge of particular
850 languages into the location in the program's code where the message is
851 displayed.
852
853 @cindex numeric prefix
854 @cindex field width
855 @cindex padding
856   All the specification characters allow an optional numeric prefix
857 between the @samp{%} and the character, and following any repositioning
858 specification or flag.  The optional numeric prefix defines the minimum
859 width for the object.  If the printed representation of the object
860 contains fewer characters than this, then it is padded.  The padding is
861 normally on the left, but will be on the right if the @samp{-} flag
862 character is given.  The padding character is normally a space, but if
863 the @samp{0} flag character is given, zeros are used for padding.
864
865 @example
866 (format "%06d is padded on the left with zeros" 123)
867      @result{} "000123 is padded on the left with zeros"
868
869 (format "%-6d is padded on the right" 123)
870      @result{} "123    is padded on the right"
871 @end example
872
873   @code{format} never truncates an object's printed representation, no
874 matter what width you specify.  Thus, you can use a numeric prefix to
875 specify a minimum spacing between columns with no risk of losing
876 information.
877
878   In the following three examples, @samp{%7s} specifies a minimum width
879 of 7.  In the first case, the string inserted in place of @samp{%7s} has
880 only 3 letters, so 4 blank spaces are inserted for padding.  In the
881 second case, the string @code{"specification"} is 13 letters wide but is
882 not truncated.  In the third case, the padding is on the right.
883
884 @smallexample
885 @group
886 (format "The word `%7s' actually has %d letters in it."
887         "foo" (length "foo"))
888      @result{} "The word `    foo' actually has 3 letters in it."
889 @end group
890
891 @group
892 (format "The word `%7s' actually has %d letters in it."
893         "specification" (length "specification"))
894      @result{} "The word `specification' actually has 13 letters in it."
895 @end group
896
897 @group
898 (format "The word `%-7s' actually has %d letters in it."
899         "foo" (length "foo"))
900      @result{} "The word `foo    ' actually has 3 letters in it."
901 @end group
902 @end smallexample
903
904 @cindex format precision
905 @cindex precision of formatted numbers
906   After any minimum field width, a precision may be specified by
907 preceding it with a @samp{.} character.  The precision specifies the
908 minimum number of digits to appear in @samp{%d}, @samp{%i}, @samp{%o},
909 @samp{%x}, and @samp{%X} conversions (the number is padded on the left
910 with zeroes as necessary); the number of digits printed after the
911 decimal point for @samp{%f}, @samp{%e}, and @samp{%E} conversions; the
912 number of significant digits printed in @samp{%g} and @samp{%G}
913 conversions; and the maximum number of non-padding characters printed in
914 @samp{%s} and @samp{%S} conversions.  The default precision for
915 floating-point conversions is six.
916
917 The other flag characters have the following meanings:
918
919 @itemize @bullet
920 @item
921 The @samp{ } flag means prefix non-negative numbers with a space.
922
923 @item
924 The @samp{+} flag means prefix non-negative numbers with a plus sign.
925
926 @item
927 The @samp{#} flag means print numbers in an alternate, more verbose
928 format: octal numbers begin with zero; hex numbers begin with a
929 @samp{0x} or @samp{0X}; a decimal point is printed in @samp{%f},
930 @samp{%e}, and @samp{%E} conversions even if no numbers are printed
931 after it; and trailing zeroes are not omitted in @samp{%g} and @samp{%G}
932 conversions.
933 @end itemize
934
935
936 @node Character Case
937 @section Character Case
938 @cindex upper case
939 @cindex lower case
940 @cindex character case
941
942   The character case functions change the case of single characters or
943 of the contents of strings.  The functions convert only alphabetic
944 characters (the letters @samp{A} through @samp{Z} and @samp{a} through
945 @samp{z}); other characters are not altered.  The functions do not
946 modify the strings that are passed to them as arguments.
947
948   The examples below use the characters @samp{X} and @samp{x} which have
949 @sc{ascii} codes 88 and 120 respectively.
950
951 @defun downcase string-or-char &optional buffer
952 This function converts a character or a string to lower case.
953
954 When the argument to @code{downcase} is a string, the function creates
955 and returns a new string in which each letter in the argument that is
956 upper case is converted to lower case.  When the argument to
957 @code{downcase} is a character, @code{downcase} returns the
958 corresponding lower case character. (This value is actually an integer
959 under XEmacs 19.) If the original character is lower case, or is not a
960 letter, then the value equals the original character.
961
962 Optional second arg @var{buffer} specifies which buffer's case tables to
963 use, and defaults to the current buffer.
964
965 @example
966 (downcase "The cat in the hat")
967      @result{} "the cat in the hat"
968
969 (downcase ?X)
970      @result{} ?x   ;; @r{Under SXEmacs.}
971      @result{} 120  ;; @r{Under XEmacs 19.}
972
973 @end example
974 @end defun
975
976 @defun upcase string-or-char &optional buffer
977 This function converts a character or a string to upper case.
978
979 When the argument to @code{upcase} is a string, the function creates
980 and returns a new string in which each letter in the argument that is
981 lower case is converted to upper case.
982
983 When the argument to @code{upcase} is a character, @code{upcase} returns
984 the corresponding upper case character. (This value is actually an
985 integer under XEmacs 19.)  If the original character is upper case, or
986 is not a letter, then the value equals the original character.
987
988 Optional second arg @var{buffer} specifies which buffer's case tables to
989 use, and defaults to the current buffer.
990
991 @example
992 (upcase "The cat in the hat")
993      @result{} "THE CAT IN THE HAT"
994
995 (upcase ?x)
996      @result{} ?X   ;; @r{Under SXEmacs.}
997      @result{} 88   ;; @r{Under XEmacs 19.}
998 @end example
999 @end defun
1000
1001 @defun capitalize string-or-char &optional buffer
1002 @cindex capitalization
1003 This function capitalizes strings or characters.  If
1004 @var{string-or-char} is a string, the function creates and returns a new
1005 string, whose contents are a copy of @var{string-or-char} in which each
1006 word has been capitalized.  This means that the first character of each
1007 word is converted to upper case, and the rest are converted to lower
1008 case.
1009
1010 The definition of a word is any sequence of consecutive characters that
1011 are assigned to the word constituent syntax class in the current syntax
1012 table (@pxref{Syntax Class Table}).
1013
1014 When the argument to @code{capitalize} is a character, @code{capitalize}
1015 has the same result as @code{upcase}.
1016
1017 Optional second arg @var{buffer} specifies which buffer's case tables to
1018 use, and defaults to the current buffer.
1019
1020 @example
1021 (capitalize "The cat in the hat")
1022      @result{} "The Cat In The Hat"
1023
1024 (capitalize "THE 77TH-HATTED CAT")
1025      @result{} "The 77th-Hatted Cat"
1026
1027 @group
1028 (capitalize ?x)
1029      @result{} ?X   ;; @r{Under SXEmacs.}
1030      @result{} 88   ;; @r{Under XEmacs 19.}
1031 @end group
1032 @end example
1033 @end defun
1034
1035
1036 @node Case Tables
1037 @section The Case Table
1038
1039   You can customize case conversion by installing a special @dfn{case
1040 table}.  A case table specifies the mapping between upper case and lower
1041 case letters.  It affects both the string and character case conversion
1042 functions (see the previous section) and those that apply to text in the
1043 buffer (@pxref{Case Changes}).  You need a case table if you are using a
1044 language which has letters other than the standard @sc{ascii} letters.
1045
1046   A case table is a list of this form:
1047
1048 @example
1049 (@var{downcase} @var{upcase} @var{canonicalize} @var{equivalences})
1050 @end example
1051
1052 @noindent
1053 where each element is either @code{nil} or a string of length 256.  The
1054 element @var{downcase} says how to map each character to its lower-case
1055 equivalent.  The element @var{upcase} maps each character to its
1056 upper-case equivalent.  If lower and upper case characters are in
1057 one-to-one correspondence, use @code{nil} for @var{upcase}; then SXEmacs
1058 deduces the upcase table from @var{downcase}.
1059
1060   For some languages, upper and lower case letters are not in one-to-one
1061 correspondence.  There may be two different lower case letters with the
1062 same upper case equivalent.  In these cases, you need to specify the
1063 maps for both directions.
1064
1065   The element @var{canonicalize} maps each character to a canonical
1066 equivalent; any two characters that are related by case-conversion have
1067 the same canonical equivalent character.
1068
1069   The element @var{equivalences} is a map that cyclicly permutes each
1070 equivalence class (of characters with the same canonical equivalent).
1071 (For ordinary @sc{ascii}, this would map @samp{a} into @samp{A} and
1072 @samp{A} into @samp{a}, and likewise for each set of equivalent
1073 characters.)
1074
1075   When you construct a case table, you can provide @code{nil} for
1076 @var{canonicalize}; then SXEmacs fills in this string from @var{upcase}
1077 and @var{downcase}.  You can also provide @code{nil} for
1078 @var{equivalences}; then SXEmacs fills in this string from
1079 @var{canonicalize}.  In a case table that is actually in use, those
1080 components are non-@code{nil}.  Do not try to specify @var{equivalences}
1081 without also specifying @var{canonicalize}.
1082
1083   Each buffer has a case table.  SXEmacs also has a @dfn{standard case
1084 table} which is copied into each buffer when you create the buffer.
1085 Changing the standard case table doesn't affect any existing buffers.
1086
1087   Here are the functions for working with case tables:
1088
1089 @defun case-table-p object
1090 This predicate returns non-@code{nil} if @var{object} is a valid case
1091 table.
1092 @end defun
1093
1094 @defun set-standard-case-table case-table
1095 This function makes @var{case-table} the standard case table, so that it
1096 will apply to any buffers created subsequently.
1097 @end defun
1098
1099 @defun standard-case-table
1100 This returns the standard case table.
1101 @end defun
1102
1103 @defun current-case-table &optional buffer
1104 This function returns the case table of @var{buffer}, which defaults to
1105 the current buffer.
1106 @end defun
1107
1108 @defun set-case-table case-table
1109 This sets the current buffer's case table to @var{case-table}.
1110 @end defun
1111
1112   The following three functions are convenient subroutines for packages
1113 that define non-@sc{ascii} character sets.  They modify a string
1114 @var{downcase-table} provided as an argument; this should be a string to
1115 be used as the @var{downcase} part of a case table.  They also modify
1116 the standard syntax table.  @xref{Syntax Tables}.
1117
1118 @defun set-case-syntax-pair uc lc downcase-table
1119 This function specifies a pair of corresponding letters, one upper case
1120 and one lower case.
1121 @end defun
1122
1123 @defun set-case-syntax-delims l r downcase-table
1124 This function makes characters @var{l} and @var{r} a matching pair of
1125 case-invariant delimiters.
1126 @end defun
1127
1128 @defun set-case-syntax char syntax downcase-table
1129 This function makes @var{char} case-invariant, with syntax
1130 @var{syntax}.
1131 @end defun
1132
1133 @deffn Command describe-buffer-case-table
1134 This command displays a description of the contents of the current
1135 buffer's case table.
1136 @end deffn
1137
1138 @cindex ISO Latin 1
1139 @pindex iso-syntax
1140 You can load the library @file{iso-syntax} to set up the standard syntax
1141 table and define a case table for the 8-bit ISO Latin 1 character set.
1142
1143
1144 @node Char Tables,  , Case Tables, Strings and Characters
1145 @section The Char Table
1146
1147 A char table is a table that maps characters (or ranges of characters)
1148 to values.  Char tables are specialized for characters, only allowing
1149 particular sorts of ranges to be assigned values.  Although this
1150 loses in generality, it makes for extremely fast (constant-time)
1151 lookups, and thus is feasible for applications that do an extremely
1152 large number of lookups (e.g. scanning a buffer for a character in
1153 a particular syntax, where a lookup in the syntax table must occur
1154 once per character).
1155
1156 Note that char tables as a primitive type, and all of the functions in
1157 this section, exist only in SXEmacs and XEmacs 20+.
1158 In XEmacs 19, char tables are generally implemented using a vector of
1159 256 elements.
1160
1161 When @sc{mule} support exists, the types of ranges that can be assigned
1162 values are
1163
1164 @itemize @bullet
1165 @item
1166 all characters
1167 @item
1168 an entire charset
1169 @item
1170 a single row in a two-octet charset
1171 @item
1172 a single character
1173 @end itemize
1174
1175 When @sc{mule} support is not present, the types of ranges that can be
1176 assigned values are
1177
1178 @itemize @bullet
1179 @item
1180 all characters
1181 @item
1182 a single character
1183 @end itemize
1184
1185 @defun char-table-p object
1186 This function returns non-@code{nil} if @var{object} is a char table.
1187 @end defun
1188
1189 @menu
1190 * Char Table Types::            Char tables have different uses.
1191 * Working With Char Tables::    Creating and working with char tables.
1192 @end menu
1193
1194
1195 @node Char Table Types
1196 @subsection Char Table Types
1197
1198 Each char table type is used for a different purpose and allows different
1199 sorts of values.  The different char table types are
1200
1201 @table @code
1202 @item category
1203 Used for category tables, which specify the regexp categories
1204 that a character is in.  The valid values are @code{nil} or a
1205 bit vector of 95 elements.  Higher-level Lisp functions are
1206 provided for working with category tables.  Currently categories
1207 and category tables only exist when @sc{mule} support is present.
1208 @item char
1209 A generalized char table, for mapping from one character to
1210 another.  Used for case tables, syntax matching tables,
1211 @code{keyboard-translate-table}, etc.  The valid values are characters.
1212 @item generic
1213 An even more generalized char table, for mapping from a
1214 character to anything.
1215 @item display
1216 Used for display tables, which specify how a particular character
1217 is to appear when displayed.  #### Not yet implemented.
1218 @item syntax
1219 Used for syntax tables, which specify the syntax of a particular
1220 character.  Higher-level Lisp functions are provided for
1221 working with syntax tables.  The valid values are integers.
1222 @end table
1223
1224 @defun char-table-type char-table
1225 This function returns the type of char table @var{char-table}.
1226 @end defun
1227
1228 @defun char-table-type-list
1229 This function returns a list of the recognized char table types.
1230 @end defun
1231
1232 @defun valid-char-table-type-p type
1233 This function returns @code{t} if @var{type} if a recognized char table type.
1234 @end defun
1235
1236 @node Working With Char Tables
1237 @subsection Working With Char Tables
1238
1239 @defun make-char-table type
1240 This function makes a new, empty char table of type @var{type}.
1241 @var{type} should be a symbol, one of @code{char}, @code{category},
1242 @code{display}, @code{generic}, or @code{syntax}.
1243 @end defun
1244
1245 @defun put-char-table range value char-table
1246 This function sets the value for chars in @var{range} to be @var{value} in
1247 @var{char-table}.
1248
1249 @var{range} specifies one or more characters to be affected and should be
1250 one of the following:
1251
1252 @itemize @bullet
1253 @item
1254 @code{t} (all characters are affected)
1255 @item
1256 A charset (only allowed when @sc{mule} support is present)
1257 @item
1258 A vector of two elements: a two-octet charset and a row number
1259 (only allowed when @sc{mule} support is present)
1260 @item
1261 A single character
1262 @end itemize
1263
1264 @var{value} must be a value appropriate for the type of @var{char-table}.
1265 @end defun
1266
1267 @defun get-char-table character char-table
1268 This function finds the value for @var{character} in @var{char-table}.
1269 @end defun
1270
1271 @defun get-range-char-table range char-table &optional multi
1272 This function finds the value for a range in @var{char-table}.  If there is
1273 more than one value, @var{multi} is returned (defaults to @code{nil}).
1274 @end defun
1275
1276 @defun reset-char-table char-table
1277 This function resets @var{char-table} to its default state.
1278 @end defun
1279
1280 @defun map-char-table function char-table &optional range
1281 This function maps @var{function} over entries in @var{char-table}, calling
1282 it with two args, each key and value in the table.
1283
1284 @var{range} specifies a subrange to map over and is in the same format
1285 as the @var{range} argument to @code{put-range-table}.  If omitted or
1286 @code{t}, it defaults to the entire table.
1287 @end defun
1288
1289 @defun valid-char-table-value-p value char-table-type
1290 This function returns non-@code{nil} if @var{value} is a valid value for
1291 @var{char-table-type}.
1292 @end defun
1293
1294 @defun check-valid-char-table-value value char-table-type
1295 This function signals an error if @var{value} is not a valid value for
1296 @var{char-table-type}.
1297 @end defun