Mercurial > emacs
annotate lispref/strings.texi @ 13245:2e0c4159e94b
(describe_map_tree): New arg always_title. Callers changed.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 19 Oct 1995 00:17:11 +0000 |
parents | a4a1d7df2e7f |
children | 66d807bdc5b4 |
rev | line source |
---|---|
6550 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | |
4 @c See the file elisp.texi for copying conditions. | |
5 @setfilename ../info/strings | |
6 @node Strings and Characters, Lists, Numbers, Top | |
7 @comment node-name, next, previous, up | |
8 @chapter Strings and Characters | |
9 @cindex strings | |
10 @cindex character arrays | |
11 @cindex characters | |
12 @cindex bytes | |
13 | |
14 A string in Emacs Lisp is an array that contains an ordered sequence | |
15 of characters. Strings are used as names of symbols, buffers, and | |
16 files, to send messages to users, to hold text being copied between | |
17 buffers, and for many other purposes. Because strings are so important, | |
18 Emacs Lisp has many functions expressly for manipulating them. Emacs | |
19 Lisp programs use strings more often than individual characters. | |
20 | |
21 @xref{Strings of Events}, for special considerations for strings of | |
22 keyboard character events. | |
23 | |
24 @menu | |
25 * Basics: String Basics. Basic properties of strings and characters. | |
26 * Predicates for Strings:: Testing whether an object is a string or char. | |
27 * Creating Strings:: Functions to allocate new strings. | |
28 * Text Comparison:: Comparing characters or strings. | |
29 * String Conversion:: Converting characters or strings and vice versa. | |
30 * Formatting Strings:: @code{format}: Emacs's analog of @code{printf}. | |
31 * Character Case:: Case conversion functions. | |
32 * Case Table:: Customizing case conversion. | |
33 @end menu | |
34 | |
35 @node String Basics | |
36 @section String and Character Basics | |
37 | |
38 Strings in Emacs Lisp are arrays that contain an ordered sequence of | |
39 characters. Characters are represented in Emacs Lisp as integers; | |
40 whether an integer was intended as a character or not is determined only | |
41 by how it is used. Thus, strings really contain integers. | |
42 | |
43 The length of a string (like any array) is fixed and independent of | |
44 the string contents, and cannot be altered. Strings in Lisp are | |
45 @emph{not} terminated by a distinguished character code. (By contrast, | |
46 strings in C are terminated by a character with @sc{ASCII} code 0.) | |
47 This means that any character, including the null character (@sc{ASCII} | |
48 code 0), is a valid element of a string.@refill | |
49 | |
50 Since strings are considered arrays, you can operate on them with the | |
51 general array functions. (@xref{Sequences Arrays Vectors}.) For | |
52 example, you can access or change individual characters in a string | |
53 using the functions @code{aref} and @code{aset} (@pxref{Array | |
54 Functions}). | |
55 | |
56 Each character in a string is stored in a single byte. Therefore, | |
57 numbers not in the range 0 to 255 are truncated when stored into a | |
58 string. This means that a string takes up much less memory than a | |
59 vector of the same length. | |
60 | |
61 Sometimes key sequences are represented as strings. When a string is | |
62 a key sequence, string elements in the range 128 to 255 represent meta | |
63 characters (which are extremely large integers) rather than keyboard | |
64 events in the range 128 to 255. | |
65 | |
66 Strings cannot hold characters that have the hyper, super or alt | |
67 modifiers; they can hold @sc{ASCII} control characters, but no other | |
68 control characters. They do not distinguish case in @sc{ASCII} control | |
69 characters. @xref{Character Type}, for more information about | |
70 representation of meta and other modifiers for keyboard input | |
71 characters. | |
72 | |
12098 | 73 Strings are useful for holding regular expressions. You can also |
74 match regular expressions against strings (@pxref{Regexp Search}). The | |
75 functions @code{match-string} (@pxref{Simple Match Data}) and | |
76 @code{replace-match} (@pxref{Replacing Match}) are useful for | |
77 decomposing and modifying strings based on regular expression matching. | |
78 | |
6550 | 79 Like a buffer, a string can contain text properties for the characters |
80 in it, as well as the characters themselves. @xref{Text Properties}. | |
12098 | 81 All the Lisp primitives that copy text from strings to buffers or other |
82 strings also copy the properties of the characters being copied. | |
6550 | 83 |
84 @xref{Text}, for information about functions that display strings or | |
85 copy them into buffers. @xref{Character Type}, and @ref{String Type}, | |
86 for information about the syntax of characters and strings. | |
87 | |
88 @node Predicates for Strings | |
89 @section The Predicates for Strings | |
90 | |
91 For more information about general sequence and array predicates, | |
92 see @ref{Sequences Arrays Vectors}, and @ref{Arrays}. | |
93 | |
94 @defun stringp object | |
95 This function returns @code{t} if @var{object} is a string, @code{nil} | |
96 otherwise. | |
97 @end defun | |
98 | |
99 @defun char-or-string-p object | |
100 This function returns @code{t} if @var{object} is a string or a | |
101 character (i.e., an integer), @code{nil} otherwise. | |
102 @end defun | |
103 | |
104 @node Creating Strings | |
105 @section Creating Strings | |
106 | |
107 The following functions create strings, either from scratch, or by | |
108 putting strings together, or by taking them apart. | |
109 | |
110 @defun make-string count character | |
111 This function returns a string made up of @var{count} repetitions of | |
112 @var{character}. If @var{count} is negative, an error is signaled. | |
113 | |
114 @example | |
115 (make-string 5 ?x) | |
116 @result{} "xxxxx" | |
117 (make-string 0 ?x) | |
118 @result{} "" | |
119 @end example | |
120 | |
121 Other functions to compare with this one include @code{char-to-string} | |
122 (@pxref{String Conversion}), @code{make-vector} (@pxref{Vectors}), and | |
123 @code{make-list} (@pxref{Building Lists}). | |
124 @end defun | |
125 | |
126 @defun substring string start &optional end | |
12098 | 127 This function returns a new string which consists of those characters |
6550 | 128 from @var{string} in the range from (and including) the character at the |
129 index @var{start} up to (but excluding) the character at the index | |
130 @var{end}. The first character is at index zero. | |
131 | |
132 @example | |
133 @group | |
134 (substring "abcdefg" 0 3) | |
135 @result{} "abc" | |
136 @end group | |
137 @end example | |
138 | |
139 @noindent | |
140 Here the index for @samp{a} is 0, the index for @samp{b} is 1, and the | |
141 index for @samp{c} is 2. Thus, three letters, @samp{abc}, are copied | |
142 from the string @code{"abcdefg"}. The index 3 marks the character | |
143 position up to which the substring is copied. The character whose index | |
144 is 3 is actually the fourth character in the string. | |
145 | |
146 A negative number counts from the end of the string, so that @minus{}1 | |
147 signifies the index of the last character of the string. For example: | |
148 | |
149 @example | |
150 @group | |
151 (substring "abcdefg" -3 -1) | |
152 @result{} "ef" | |
153 @end group | |
154 @end example | |
155 | |
156 @noindent | |
157 In this example, the index for @samp{e} is @minus{}3, the index for | |
158 @samp{f} is @minus{}2, and the index for @samp{g} is @minus{}1. | |
159 Therefore, @samp{e} and @samp{f} are included, and @samp{g} is excluded. | |
160 | |
161 When @code{nil} is used as an index, it stands for the length of the | |
162 string. Thus, | |
163 | |
164 @example | |
165 @group | |
166 (substring "abcdefg" -3 nil) | |
167 @result{} "efg" | |
168 @end group | |
169 @end example | |
170 | |
171 Omitting the argument @var{end} is equivalent to specifying @code{nil}. | |
172 It follows that @code{(substring @var{string} 0)} returns a copy of all | |
173 of @var{string}. | |
174 | |
175 @example | |
176 @group | |
177 (substring "abcdefg" 0) | |
178 @result{} "abcdefg" | |
179 @end group | |
180 @end example | |
181 | |
182 @noindent | |
183 But we recommend @code{copy-sequence} for this purpose (@pxref{Sequence | |
184 Functions}). | |
185 | |
12098 | 186 If the characters copied from @var{string} have text properties, the |
187 properties are copied into the new string also. @xref{Text Properties}. | |
188 | |
6550 | 189 A @code{wrong-type-argument} error is signaled if either @var{start} or |
190 @var{end} is not an integer or @code{nil}. An @code{args-out-of-range} | |
191 error is signaled if @var{start} indicates a character following | |
192 @var{end}, or if either integer is out of range for @var{string}. | |
193 | |
194 Contrast this function with @code{buffer-substring} (@pxref{Buffer | |
195 Contents}), which returns a string containing a portion of the text in | |
196 the current buffer. The beginning of a string is at index 0, but the | |
197 beginning of a buffer is at index 1. | |
198 @end defun | |
199 | |
200 @defun concat &rest sequences | |
201 @cindex copying strings | |
202 @cindex concatenating strings | |
203 This function returns a new string consisting of the characters in the | |
12098 | 204 arguments passed to it (along with their text properties, if any). The |
205 arguments may be strings, lists of numbers, or vectors of numbers; they | |
206 are not themselves changed. If @code{concat} receives no arguments, it | |
207 returns an empty string. | |
6550 | 208 |
209 @example | |
210 (concat "abc" "-def") | |
211 @result{} "abc-def" | |
212 (concat "abc" (list 120 (+ 256 121)) [122]) | |
213 @result{} "abcxyz" | |
214 ;; @r{@code{nil} is an empty sequence.} | |
215 (concat "abc" nil "-def") | |
216 @result{} "abc-def" | |
217 (concat "The " "quick brown " "fox.") | |
218 @result{} "The quick brown fox." | |
219 (concat) | |
220 @result{} "" | |
221 @end example | |
222 | |
223 @noindent | |
224 The second example above shows how characters stored in strings are | |
225 taken modulo 256. In other words, each character in the string is | |
226 stored in one byte. | |
227 | |
228 The @code{concat} function always constructs a new string that is | |
229 not @code{eq} to any existing string. | |
230 | |
231 When an argument is an integer (not a sequence of integers), it is | |
232 converted to a string of digits making up the decimal printed | |
12067 | 233 representation of the integer. @strong{Don't use this feature; we plan |
234 to eliminate it. If you already use this feature, change your programs | |
235 now!} The proper way to convert an integer to a decimal number in this | |
236 way is with @code{format} (@pxref{Formatting Strings}) or | |
11141
6f6c571ad0c0
Say not to use concat for integers.
Richard M. Stallman <rms@gnu.org>
parents:
8590
diff
changeset
|
237 @code{number-to-string} (@pxref{String Conversion}). |
6550 | 238 |
239 @example | |
240 @group | |
241 (concat 137) | |
242 @result{} "137" | |
243 (concat 54 321) | |
244 @result{} "54321" | |
245 @end group | |
246 @end example | |
247 | |
248 For information about other concatenation functions, see the | |
249 description of @code{mapconcat} in @ref{Mapping Functions}, | |
250 @code{vconcat} in @ref{Vectors}, and @code{append} in @ref{Building | |
251 Lists}. | |
252 @end defun | |
253 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12098
diff
changeset
|
254 @need 2000 |
6550 | 255 @node Text Comparison |
256 @section Comparison of Characters and Strings | |
257 @cindex string equality | |
258 | |
259 @defun char-equal character1 character2 | |
260 This function returns @code{t} if the arguments represent the same | |
261 character, @code{nil} otherwise. This function ignores differences | |
262 in case if @code{case-fold-search} is non-@code{nil}. | |
263 | |
264 @example | |
265 (char-equal ?x ?x) | |
266 @result{} t | |
267 (char-to-string (+ 256 ?x)) | |
268 @result{} "x" | |
269 (char-equal ?x (+ 256 ?x)) | |
270 @result{} t | |
271 @end example | |
272 @end defun | |
273 | |
274 @defun string= string1 string2 | |
275 This function returns @code{t} if the characters of the two strings | |
276 match exactly; case is significant. | |
277 | |
278 @example | |
279 (string= "abc" "abc") | |
280 @result{} t | |
281 (string= "abc" "ABC") | |
282 @result{} nil | |
283 (string= "ab" "ABC") | |
284 @result{} nil | |
285 @end example | |
12067 | 286 |
287 The function @code{string=} ignores the text properties of the | |
288 two strings. To compare strings in a way that compares their text | |
289 properties also, use @code{equal} (@pxref{Equality Predicates}). | |
6550 | 290 @end defun |
291 | |
292 @defun string-equal string1 string2 | |
293 @code{string-equal} is another name for @code{string=}. | |
294 @end defun | |
295 | |
296 @cindex lexical comparison | |
297 @defun string< string1 string2 | |
298 @c (findex string< causes problems for permuted index!!) | |
299 This function compares two strings a character at a time. First it | |
300 scans both the strings at once to find the first pair of corresponding | |
301 characters that do not match. If the lesser character of those two is | |
302 the character from @var{string1}, then @var{string1} is less, and this | |
303 function returns @code{t}. If the lesser character is the one from | |
304 @var{string2}, then @var{string1} is greater, and this function returns | |
305 @code{nil}. If the two strings match entirely, the value is @code{nil}. | |
306 | |
307 Pairs of characters are compared by their @sc{ASCII} codes. Keep in | |
308 mind that lower case letters have higher numeric values in the | |
309 @sc{ASCII} character set than their upper case counterparts; numbers and | |
310 many punctuation characters have a lower numeric value than upper case | |
311 letters. | |
312 | |
313 @example | |
314 @group | |
315 (string< "abc" "abd") | |
316 @result{} t | |
317 (string< "abd" "abc") | |
318 @result{} nil | |
319 (string< "123" "abc") | |
320 @result{} t | |
321 @end group | |
322 @end example | |
323 | |
324 When the strings have different lengths, and they match up to the | |
325 length of @var{string1}, then the result is @code{t}. If they match up | |
326 to the length of @var{string2}, the result is @code{nil}. A string of | |
327 no characters is less than any other string. | |
328 | |
329 @example | |
330 @group | |
331 (string< "" "abc") | |
332 @result{} t | |
333 (string< "ab" "abc") | |
334 @result{} t | |
335 (string< "abc" "") | |
336 @result{} nil | |
337 (string< "abc" "ab") | |
338 @result{} nil | |
339 (string< "" "") | |
340 @result{} nil | |
341 @end group | |
342 @end example | |
343 @end defun | |
344 | |
345 @defun string-lessp string1 string2 | |
346 @code{string-lessp} is another name for @code{string<}. | |
347 @end defun | |
348 | |
349 See also @code{compare-buffer-substrings} in @ref{Comparing Text}, for | |
350 a way to compare text in buffers. The function @code{string-match}, | |
351 which matches a regular expression against a string, can be used | |
352 for a kind of string comparison; see @ref{Regexp Search}. | |
353 | |
354 @node String Conversion | |
355 @comment node-name, next, previous, up | |
356 @section Conversion of Characters and Strings | |
357 @cindex conversion of strings | |
358 | |
359 This section describes functions for conversions between characters, | |
360 strings and integers. @code{format} and @code{prin1-to-string} | |
361 (@pxref{Output Functions}) can also convert Lisp objects into strings. | |
362 @code{read-from-string} (@pxref{Input Functions}) can ``convert'' a | |
363 string representation of a Lisp object into an object. | |
364 | |
365 @xref{Documentation}, for functions that produce textual descriptions | |
366 of text characters and general input events | |
367 (@code{single-key-description} and @code{text-char-description}). These | |
368 functions are used primarily for making help messages. | |
369 | |
370 @defun char-to-string character | |
371 @cindex character to string | |
372 This function returns a new string with a length of one character. | |
373 The value of @var{character}, modulo 256, is used to initialize the | |
374 element of the string. | |
375 | |
376 This function is similar to @code{make-string} with an integer argument | |
377 of 1. (@xref{Creating Strings}.) This conversion can also be done with | |
378 @code{format} using the @samp{%c} format specification. | |
379 (@xref{Formatting Strings}.) | |
380 | |
381 @example | |
382 (char-to-string ?x) | |
383 @result{} "x" | |
384 (char-to-string (+ 256 ?x)) | |
385 @result{} "x" | |
386 (make-string 1 ?x) | |
387 @result{} "x" | |
388 @end example | |
389 @end defun | |
390 | |
391 @defun string-to-char string | |
392 @cindex string to character | |
393 This function returns the first character in @var{string}. If the | |
394 string is empty, the function returns 0. The value is also 0 when the | |
395 first character of @var{string} is the null character, @sc{ASCII} code | |
396 0. | |
397 | |
398 @example | |
399 (string-to-char "ABC") | |
400 @result{} 65 | |
401 (string-to-char "xyz") | |
402 @result{} 120 | |
403 (string-to-char "") | |
404 @result{} 0 | |
405 (string-to-char "\000") | |
406 @result{} 0 | |
407 @end example | |
408 | |
409 This function may be eliminated in the future if it does not seem useful | |
410 enough to retain. | |
411 @end defun | |
412 | |
413 @defun number-to-string number | |
414 @cindex integer to string | |
415 @cindex integer to decimal | |
416 This function returns a string consisting of the printed | |
417 representation of @var{number}, which may be an integer or a floating | |
418 point number. The value starts with a sign if the argument is | |
419 negative. | |
420 | |
421 @example | |
422 (number-to-string 256) | |
423 @result{} "256" | |
424 (number-to-string -23) | |
425 @result{} "-23" | |
426 (number-to-string -23.5) | |
427 @result{} "-23.5" | |
428 @end example | |
429 | |
430 @cindex int-to-string | |
431 @code{int-to-string} is a semi-obsolete alias for this function. | |
432 | |
433 See also the function @code{format} in @ref{Formatting Strings}. | |
434 @end defun | |
435 | |
436 @defun string-to-number string | |
437 @cindex string to number | |
438 This function returns the numeric value of the characters in | |
439 @var{string}, read in base ten. It skips spaces and tabs at the | |
440 beginning of @var{string}, then reads as much of @var{string} as it can | |
441 interpret as a number. (On some systems it ignores other whitespace at | |
442 the beginning, not just spaces and tabs.) If the first character after | |
443 the ignored whitespace is not a digit or a minus sign, this function | |
444 returns 0. | |
445 | |
446 @example | |
447 (string-to-number "256") | |
448 @result{} 256 | |
449 (string-to-number "25 is a perfect square.") | |
450 @result{} 25 | |
451 (string-to-number "X256") | |
452 @result{} 0 | |
453 (string-to-number "-4.5") | |
454 @result{} -4.5 | |
455 @end example | |
456 | |
457 @findex string-to-int | |
458 @code{string-to-int} is an obsolete alias for this function. | |
459 @end defun | |
460 | |
461 @node Formatting Strings | |
462 @comment node-name, next, previous, up | |
463 @section Formatting Strings | |
464 @cindex formatting strings | |
465 @cindex strings, formatting them | |
466 | |
467 @dfn{Formatting} means constructing a string by substitution of | |
468 computed values at various places in a constant string. This string | |
469 controls how the other values are printed as well as where they appear; | |
470 it is called a @dfn{format string}. | |
471 | |
472 Formatting is often useful for computing messages to be displayed. In | |
473 fact, the functions @code{message} and @code{error} provide the same | |
474 formatting feature described here; they differ from @code{format} only | |
475 in how they use the result of formatting. | |
476 | |
477 @defun format string &rest objects | |
478 This function returns a new string that is made by copying | |
479 @var{string} and then replacing any format specification | |
480 in the copy with encodings of the corresponding @var{objects}. The | |
481 arguments @var{objects} are the computed values to be formatted. | |
482 @end defun | |
483 | |
484 @cindex @samp{%} in format | |
485 @cindex format specification | |
486 A format specification is a sequence of characters beginning with a | |
487 @samp{%}. Thus, if there is a @samp{%d} in @var{string}, the | |
488 @code{format} function replaces it with the printed representation of | |
489 one of the values to be formatted (one of the arguments @var{objects}). | |
490 For example: | |
491 | |
492 @example | |
493 @group | |
494 (format "The value of fill-column is %d." fill-column) | |
495 @result{} "The value of fill-column is 72." | |
496 @end group | |
497 @end example | |
498 | |
499 If @var{string} contains more than one format specification, the | |
500 format specifications correspond with successive values from | |
501 @var{objects}. Thus, the first format specification in @var{string} | |
502 uses the first such value, the second format specification uses the | |
503 second such value, and so on. Any extra format specifications (those | |
504 for which there are no corresponding values) cause unpredictable | |
505 behavior. Any extra values to be formatted are ignored. | |
506 | |
507 Certain format specifications require values of particular types. | |
508 However, no error is signaled if the value actually supplied fails to | |
509 have the expected type. Instead, the output is likely to be | |
510 meaningless. | |
511 | |
512 Here is a table of valid format specifications: | |
513 | |
514 @table @samp | |
515 @item %s | |
516 Replace the specification with the printed representation of the object, | |
517 made without quoting. Thus, strings are represented by their contents | |
518 alone, with no @samp{"} characters, and symbols appear without @samp{\} | |
519 characters. | |
520 | |
521 If there is no corresponding object, the empty string is used. | |
522 | |
523 @item %S | |
524 Replace the specification with the printed representation of the object, | |
525 made with quoting. Thus, strings are enclosed in @samp{"} characters, | |
526 and @samp{\} characters appear where necessary before special characters. | |
527 | |
528 If there is no corresponding object, the empty string is used. | |
529 | |
530 @item %o | |
531 @cindex integer to octal | |
532 Replace the specification with the base-eight representation of an | |
533 integer. | |
534 | |
535 @item %d | |
536 Replace the specification with the base-ten representation of an | |
537 integer. | |
538 | |
539 @item %x | |
540 @cindex integer to hexadecimal | |
541 Replace the specification with the base-sixteen representation of an | |
542 integer. | |
543 | |
544 @item %c | |
545 Replace the specification with the character which is the value given. | |
546 | |
547 @item %e | |
548 Replace the specification with the exponential notation for a floating | |
549 point number. | |
550 | |
551 @item %f | |
552 Replace the specification with the decimal-point notation for a floating | |
553 point number. | |
554 | |
555 @item %g | |
556 Replace the specification with notation for a floating point number, | |
557 using either exponential notation or decimal-point notation whichever | |
558 is shorter. | |
559 | |
560 @item %% | |
561 A single @samp{%} is placed in the string. This format specification is | |
562 unusual in that it does not use a value. For example, @code{(format "%% | |
563 %d" 30)} returns @code{"% 30"}. | |
564 @end table | |
565 | |
566 Any other format character results in an @samp{Invalid format | |
567 operation} error. | |
568 | |
569 Here are several examples: | |
570 | |
571 @example | |
572 @group | |
573 (format "The name of this buffer is %s." (buffer-name)) | |
574 @result{} "The name of this buffer is strings.texi." | |
575 | |
576 (format "The buffer object prints as %s." (current-buffer)) | |
13228 | 577 @result{} "The buffer object prints as strings.texi." |
6550 | 578 |
579 (format "The octal value of %d is %o, | |
580 and the hex value is %x." 18 18 18) | |
581 @result{} "The octal value of 18 is 22, | |
582 and the hex value is 12." | |
583 @end group | |
584 @end example | |
585 | |
586 @cindex numeric prefix | |
587 @cindex field width | |
588 @cindex padding | |
589 All the specification characters allow an optional numeric prefix | |
590 between the @samp{%} and the character. The optional numeric prefix | |
591 defines the minimum width for the object. If the printed representation | |
592 of the object contains fewer characters than this, then it is padded. | |
593 The padding is on the left if the prefix is positive (or starts with | |
594 zero) and on the right if the prefix is negative. The padding character | |
595 is normally a space, but if the numeric prefix starts with a zero, zeros | |
596 are used for padding. | |
597 | |
598 @example | |
599 (format "%06d is padded on the left with zeros" 123) | |
600 @result{} "000123 is padded on the left with zeros" | |
601 | |
602 (format "%-6d is padded on the right" 123) | |
603 @result{} "123 is padded on the right" | |
604 @end example | |
605 | |
606 @code{format} never truncates an object's printed representation, no | |
607 matter what width you specify. Thus, you can use a numeric prefix to | |
608 specify a minimum spacing between columns with no risk of losing | |
609 information. | |
610 | |
611 In the following three examples, @samp{%7s} specifies a minimum width | |
612 of 7. In the first case, the string inserted in place of @samp{%7s} has | |
613 only 3 letters, so 4 blank spaces are inserted for padding. In the | |
614 second case, the string @code{"specification"} is 13 letters wide but is | |
615 not truncated. In the third case, the padding is on the right. | |
616 | |
617 @smallexample | |
618 @group | |
619 (format "The word `%7s' actually has %d letters in it." | |
620 "foo" (length "foo")) | |
621 @result{} "The word ` foo' actually has 3 letters in it." | |
622 @end group | |
623 | |
624 @group | |
625 (format "The word `%7s' actually has %d letters in it." | |
626 "specification" (length "specification")) | |
627 @result{} "The word `specification' actually has 13 letters in it." | |
628 @end group | |
629 | |
630 @group | |
631 (format "The word `%-7s' actually has %d letters in it." | |
632 "foo" (length "foo")) | |
633 @result{} "The word `foo ' actually has 3 letters in it." | |
634 @end group | |
635 @end smallexample | |
636 | |
637 @node Character Case | |
638 @comment node-name, next, previous, up | |
639 @section Character Case | |
640 @cindex upper case | |
641 @cindex lower case | |
642 @cindex character case | |
643 | |
644 The character case functions change the case of single characters or | |
645 of the contents of strings. The functions convert only alphabetic | |
646 characters (the letters @samp{A} through @samp{Z} and @samp{a} through | |
647 @samp{z}); other characters are not altered. The functions do not | |
648 modify the strings that are passed to them as arguments. | |
649 | |
650 The examples below use the characters @samp{X} and @samp{x} which have | |
651 @sc{ASCII} codes 88 and 120 respectively. | |
652 | |
653 @defun downcase string-or-char | |
654 This function converts a character or a string to lower case. | |
655 | |
656 When the argument to @code{downcase} is a string, the function creates | |
657 and returns a new string in which each letter in the argument that is | |
658 upper case is converted to lower case. When the argument to | |
659 @code{downcase} is a character, @code{downcase} returns the | |
660 corresponding lower case character. This value is an integer. If the | |
661 original character is lower case, or is not a letter, then the value | |
662 equals the original character. | |
663 | |
664 @example | |
665 (downcase "The cat in the hat") | |
666 @result{} "the cat in the hat" | |
667 | |
668 (downcase ?X) | |
669 @result{} 120 | |
670 @end example | |
671 @end defun | |
672 | |
673 @defun upcase string-or-char | |
674 This function converts a character or a string to upper case. | |
675 | |
676 When the argument to @code{upcase} is a string, the function creates | |
677 and returns a new string in which each letter in the argument that is | |
678 lower case is converted to upper case. | |
679 | |
680 When the argument to @code{upcase} is a character, @code{upcase} | |
681 returns the corresponding upper case character. This value is an integer. | |
682 If the original character is upper case, or is not a letter, then the | |
683 value equals the original character. | |
684 | |
685 @example | |
686 (upcase "The cat in the hat") | |
687 @result{} "THE CAT IN THE HAT" | |
688 | |
689 (upcase ?x) | |
690 @result{} 88 | |
691 @end example | |
692 @end defun | |
693 | |
694 @defun capitalize string-or-char | |
695 @cindex capitalization | |
696 This function capitalizes strings or characters. If | |
697 @var{string-or-char} is a string, the function creates and returns a new | |
698 string, whose contents are a copy of @var{string-or-char} in which each | |
699 word has been capitalized. This means that the first character of each | |
700 word is converted to upper case, and the rest are converted to lower | |
701 case. | |
702 | |
703 The definition of a word is any sequence of consecutive characters that | |
704 are assigned to the word constituent syntax class in the current syntax | |
705 table (@xref{Syntax Class Table}). | |
706 | |
707 When the argument to @code{capitalize} is a character, @code{capitalize} | |
708 has the same result as @code{upcase}. | |
709 | |
710 @example | |
711 (capitalize "The cat in the hat") | |
712 @result{} "The Cat In The Hat" | |
713 | |
714 (capitalize "THE 77TH-HATTED CAT") | |
715 @result{} "The 77th-Hatted Cat" | |
716 | |
717 @group | |
718 (capitalize ?x) | |
719 @result{} 88 | |
720 @end group | |
721 @end example | |
722 @end defun | |
723 | |
724 @node Case Table | |
725 @section The Case Table | |
726 | |
727 You can customize case conversion by installing a special @dfn{case | |
728 table}. A case table specifies the mapping between upper case and lower | |
729 case letters. It affects both the string and character case conversion | |
730 functions (see the previous section) and those that apply to text in the | |
731 buffer (@pxref{Case Changes}). You need a case table if you are using a | |
732 language which has letters other than the standard @sc{ASCII} letters. | |
733 | |
734 A case table is a list of this form: | |
735 | |
736 @example | |
737 (@var{downcase} @var{upcase} @var{canonicalize} @var{equivalences}) | |
738 @end example | |
739 | |
740 @noindent | |
741 where each element is either @code{nil} or a string of length 256. The | |
742 element @var{downcase} says how to map each character to its lower-case | |
743 equivalent. The element @var{upcase} maps each character to its | |
744 upper-case equivalent. If lower and upper case characters are in | |
745 one-to-one correspondence, use @code{nil} for @var{upcase}; then Emacs | |
746 deduces the upcase table from @var{downcase}. | |
747 | |
748 For some languages, upper and lower case letters are not in one-to-one | |
749 correspondence. There may be two different lower case letters with the | |
750 same upper case equivalent. In these cases, you need to specify the | |
751 maps for both directions. | |
752 | |
753 The element @var{canonicalize} maps each character to a canonical | |
754 equivalent; any two characters that are related by case-conversion have | |
755 the same canonical equivalent character. | |
756 | |
757 The element @var{equivalences} is a map that cyclicly permutes each | |
758 equivalence class (of characters with the same canonical equivalent). | |
759 (For ordinary @sc{ASCII}, this would map @samp{a} into @samp{A} and | |
760 @samp{A} into @samp{a}, and likewise for each set of equivalent | |
761 characters.) | |
762 | |
6938
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
763 When you construct a case table, you can provide @code{nil} for |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
764 @var{canonicalize}; then Emacs fills in this string from @var{upcase} |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
765 and @var{downcase}. You can also provide @code{nil} for |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
766 @var{equivalences}; then Emacs fills in this string from |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
767 @var{canonicalize}. In a case table that is actually in use, those |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
768 components are non-@code{nil}. Do not try to specify @var{equivalences} |
782646fc7505
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6550
diff
changeset
|
769 without also specifying @var{canonicalize}. |
6550 | 770 |
771 Each buffer has a case table. Emacs also has a @dfn{standard case | |
772 table} which is copied into each buffer when you create the buffer. | |
773 Changing the standard case table doesn't affect any existing buffers. | |
774 | |
775 Here are the functions for working with case tables: | |
776 | |
777 @defun case-table-p object | |
778 This predicate returns non-@code{nil} if @var{object} is a valid case | |
779 table. | |
780 @end defun | |
781 | |
782 @defun set-standard-case-table table | |
783 This function makes @var{table} the standard case table, so that it will | |
784 apply to any buffers created subsequently. | |
785 @end defun | |
786 | |
787 @defun standard-case-table | |
788 This returns the standard case table. | |
789 @end defun | |
790 | |
791 @defun current-case-table | |
792 This function returns the current buffer's case table. | |
793 @end defun | |
794 | |
795 @defun set-case-table table | |
796 This sets the current buffer's case table to @var{table}. | |
797 @end defun | |
798 | |
799 The following three functions are convenient subroutines for packages | |
800 that define non-@sc{ASCII} character sets. They modify a string | |
801 @var{downcase-table} provided as an argument; this should be a string to | |
802 be used as the @var{downcase} part of a case table. They also modify | |
803 the standard syntax table. @xref{Syntax Tables}. | |
804 | |
805 @defun set-case-syntax-pair uc lc downcase-table | |
806 This function specifies a pair of corresponding letters, one upper case | |
807 and one lower case. | |
808 @end defun | |
809 | |
810 @defun set-case-syntax-delims l r downcase-table | |
811 This function makes characters @var{l} and @var{r} a matching pair of | |
812 case-invariant delimiters. | |
813 @end defun | |
814 | |
815 @defun set-case-syntax char syntax downcase-table | |
816 This function makes @var{char} case-invariant, with syntax | |
817 @var{syntax}. | |
818 @end defun | |
819 | |
820 @deffn Command describe-buffer-case-table | |
821 This command displays a description of the contents of the current | |
822 buffer's case table. | |
823 @end deffn | |
824 | |
825 @cindex ISO Latin 1 | |
826 @pindex iso-syntax | |
827 You can load the library @file{iso-syntax} to set up the standard syntax | |
8590 | 828 table and define a case table for the 8-bit ISO Latin 1 character set. |