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