Commit | Line | Data |
---|---|---|
35692fe0 | 1 | /* Lisp functions pertaining to editing. |
26fad6e5 | 2 | Copyright (C) 1985,86,87,89,93,94,95,96,97,98, 1999, 2000, 2001 |
acb7cc89 | 3 | Free Software Foundation, Inc. |
35692fe0 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
7c938215 | 9 | the Free Software Foundation; either version 2, or (at your option) |
35692fe0 JB |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
5a7670bf RS |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
35692fe0 JB |
21 | |
22 | ||
18160b98 | 23 | #include <config.h> |
68c45bf0 | 24 | #include <sys/types.h> |
bfb61299 JB |
25 | |
26 | #ifdef VMS | |
956ace37 | 27 | #include "vms-pwd.h" |
bfb61299 | 28 | #else |
35692fe0 | 29 | #include <pwd.h> |
bfb61299 JB |
30 | #endif |
31 | ||
dfcf069d AS |
32 | #ifdef HAVE_UNISTD_H |
33 | #include <unistd.h> | |
34 | #endif | |
35 | ||
409847a1 RS |
36 | #include <ctype.h> |
37 | ||
35692fe0 | 38 | #include "lisp.h" |
74d6d8c5 | 39 | #include "intervals.h" |
35692fe0 | 40 | #include "buffer.h" |
fb8106e8 | 41 | #include "charset.h" |
68c45bf0 | 42 | #include "coding.h" |
0ae83348 | 43 | #include "frame.h" |
35692fe0 JB |
44 | #include "window.h" |
45 | ||
956ace37 | 46 | #include "systime.h" |
35692fe0 | 47 | |
ea229bec GM |
48 | #ifdef STDC_HEADERS |
49 | #include <float.h> | |
50 | #define MAX_10_EXP DBL_MAX_10_EXP | |
51 | #else | |
52 | #define MAX_10_EXP 310 | |
53 | #endif | |
54 | ||
a03eaf1c RS |
55 | #ifndef NULL |
56 | #define NULL 0 | |
57 | #endif | |
58 | ||
f12ef5eb | 59 | #ifndef USE_CRT_DLL |
c59b5089 | 60 | extern char **environ; |
f12ef5eb AI |
61 | #endif |
62 | ||
acb7cc89 GM |
63 | extern Lisp_Object make_time P_ ((time_t)); |
64 | extern size_t emacs_strftimeu P_ ((char *, size_t, const char *, | |
65 | const struct tm *, int)); | |
66 | static int tm_diff P_ ((struct tm *, struct tm *)); | |
9ac741c5 | 67 | static void find_field P_ ((Lisp_Object, Lisp_Object, Lisp_Object, int *, Lisp_Object, int *)); |
acb7cc89 GM |
68 | static void update_buffer_properties P_ ((int, int)); |
69 | static Lisp_Object region_limit P_ ((int)); | |
70 | static int lisp_time_argument P_ ((Lisp_Object, time_t *, int *)); | |
71 | static size_t emacs_memftimeu P_ ((char *, size_t, const char *, | |
72 | size_t, const struct tm *, int)); | |
73 | static void general_insert_function P_ ((void (*) (unsigned char *, int), | |
74 | void (*) (Lisp_Object, int, int, int, | |
75 | int, int), | |
76 | int, int, Lisp_Object *)); | |
77 | static Lisp_Object subst_char_in_region_unwind P_ ((Lisp_Object)); | |
78 | static Lisp_Object subst_char_in_region_unwind_1 P_ ((Lisp_Object)); | |
79 | static void transpose_markers P_ ((int, int, int, int, int, int, int, int)); | |
260e2e2a | 80 | |
8892f40b GM |
81 | #ifdef HAVE_INDEX |
82 | extern char *index P_ ((const char *, int)); | |
83 | #endif | |
84 | ||
260e2e2a KH |
85 | Lisp_Object Vbuffer_access_fontify_functions; |
86 | Lisp_Object Qbuffer_access_fontify_functions; | |
87 | Lisp_Object Vbuffer_access_fontified_property; | |
b1b0ee5a | 88 | |
acb7cc89 | 89 | Lisp_Object Fuser_full_name P_ ((Lisp_Object)); |
e3ed8469 | 90 | |
9a74e7e5 GM |
91 | /* Non-nil means don't stop at field boundary in text motion commands. */ |
92 | ||
93 | Lisp_Object Vinhibit_field_text_motion; | |
94 | ||
35692fe0 JB |
95 | /* Some static data, and a function to initialize it for each run */ |
96 | ||
97 | Lisp_Object Vsystem_name; | |
35b34f72 KH |
98 | Lisp_Object Vuser_real_login_name; /* login name of current user ID */ |
99 | Lisp_Object Vuser_full_name; /* full name of current user */ | |
100 | Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */ | |
35692fe0 | 101 | |
acb7cc89 GM |
102 | /* Symbol for the text property used to mark fields. */ |
103 | ||
104 | Lisp_Object Qfield; | |
105 | ||
106 | /* A special value for Qfield properties. */ | |
107 | ||
108 | Lisp_Object Qboundary; | |
109 | ||
110 | ||
35692fe0 JB |
111 | void |
112 | init_editfns () | |
113 | { | |
52b14ac0 | 114 | char *user_name; |
2ea0266e | 115 | register unsigned char *p; |
35692fe0 | 116 | struct passwd *pw; /* password entry for the current user */ |
35692fe0 JB |
117 | Lisp_Object tem; |
118 | ||
119 | /* Set up system_name even when dumping. */ | |
ac988277 | 120 | init_system_name (); |
35692fe0 JB |
121 | |
122 | #ifndef CANNOT_DUMP | |
123 | /* Don't bother with this on initial start when just dumping out */ | |
124 | if (!initialized) | |
125 | return; | |
126 | #endif /* not CANNOT_DUMP */ | |
127 | ||
128 | pw = (struct passwd *) getpwuid (getuid ()); | |
87485d6f MW |
129 | #ifdef MSDOS |
130 | /* We let the real user name default to "root" because that's quite | |
131 | accurate on MSDOG and because it lets Emacs find the init file. | |
132 | (The DVX libraries override the Djgpp libraries here.) */ | |
35b34f72 | 133 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "root"); |
87485d6f | 134 | #else |
35b34f72 | 135 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown"); |
87485d6f | 136 | #endif |
35692fe0 | 137 | |
52b14ac0 JB |
138 | /* Get the effective user name, by consulting environment variables, |
139 | or the effective uid if those are unset. */ | |
2c9ae24e | 140 | user_name = (char *) getenv ("LOGNAME"); |
35692fe0 | 141 | if (!user_name) |
4691c06d RS |
142 | #ifdef WINDOWSNT |
143 | user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */ | |
144 | #else /* WINDOWSNT */ | |
2c9ae24e | 145 | user_name = (char *) getenv ("USER"); |
4691c06d | 146 | #endif /* WINDOWSNT */ |
52b14ac0 JB |
147 | if (!user_name) |
148 | { | |
149 | pw = (struct passwd *) getpwuid (geteuid ()); | |
150 | user_name = (char *) (pw ? pw->pw_name : "unknown"); | |
151 | } | |
35b34f72 | 152 | Vuser_login_name = build_string (user_name); |
35692fe0 | 153 | |
52b14ac0 JB |
154 | /* If the user name claimed in the environment vars differs from |
155 | the real uid, use the claimed name to find the full name. */ | |
35b34f72 | 156 | tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name); |
3415b0e9 RS |
157 | Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid()) |
158 | : Vuser_login_name); | |
34a7a267 | 159 | |
8f1e2d16 | 160 | p = (unsigned char *) getenv ("NAME"); |
9d36d071 RS |
161 | if (p) |
162 | Vuser_full_name = build_string (p); | |
3347526c RS |
163 | else if (NILP (Vuser_full_name)) |
164 | Vuser_full_name = build_string ("unknown"); | |
35692fe0 JB |
165 | } |
166 | \f | |
167 | DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0, | |
06283081 PJ |
168 | doc: /* Convert arg CHAR to a string containing that character. |
169 | usage: (char-to-string CHAR) */) | |
7ee72033 | 170 | (character) |
2591ec64 | 171 | Lisp_Object character; |
35692fe0 | 172 | { |
fb8106e8 | 173 | int len; |
d5c2c403 | 174 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
fb8106e8 | 175 | |
b7826503 | 176 | CHECK_NUMBER (character); |
35692fe0 | 177 | |
603a8d59 KH |
178 | len = (SINGLE_BYTE_CHAR_P (XFASTINT (character)) |
179 | ? (*str = (unsigned char)(XFASTINT (character)), 1) | |
180 | : char_to_string (XFASTINT (character), str)); | |
5f75e666 | 181 | return make_string_from_bytes (str, 1, len); |
35692fe0 JB |
182 | } |
183 | ||
184 | DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0, | |
7ee72033 MB |
185 | doc: /* Convert arg STRING to a character, the first character of that string. |
186 | A multibyte character is handled correctly. */) | |
187 | (string) | |
2591ec64 | 188 | register Lisp_Object string; |
35692fe0 JB |
189 | { |
190 | register Lisp_Object val; | |
191 | register struct Lisp_String *p; | |
b7826503 | 192 | CHECK_STRING (string); |
2591ec64 | 193 | p = XSTRING (string); |
35692fe0 | 194 | if (p->size) |
d9d851ea KH |
195 | { |
196 | if (STRING_MULTIBYTE (string)) | |
197 | XSETFASTINT (val, STRING_CHAR (p->data, STRING_BYTES (p))); | |
198 | else | |
199 | XSETFASTINT (val, p->data[0]); | |
200 | } | |
35692fe0 | 201 | else |
55561c63 | 202 | XSETFASTINT (val, 0); |
35692fe0 JB |
203 | return val; |
204 | } | |
205 | \f | |
206 | static Lisp_Object | |
ec1c14f6 RS |
207 | buildmark (charpos, bytepos) |
208 | int charpos, bytepos; | |
35692fe0 JB |
209 | { |
210 | register Lisp_Object mark; | |
211 | mark = Fmake_marker (); | |
ec1c14f6 | 212 | set_marker_both (mark, Qnil, charpos, bytepos); |
35692fe0 JB |
213 | return mark; |
214 | } | |
215 | ||
216 | DEFUN ("point", Fpoint, Spoint, 0, 0, 0, | |
7ee72033 MB |
217 | doc: /* Return value of point, as an integer. |
218 | Beginning of buffer is position (point-min). */) | |
219 | () | |
35692fe0 JB |
220 | { |
221 | Lisp_Object temp; | |
6ec8bbd2 | 222 | XSETFASTINT (temp, PT); |
35692fe0 JB |
223 | return temp; |
224 | } | |
225 | ||
226 | DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0, | |
7ee72033 MB |
227 | doc: /* Return value of point, as a marker object. */) |
228 | () | |
35692fe0 | 229 | { |
ec1c14f6 | 230 | return buildmark (PT, PT_BYTE); |
35692fe0 JB |
231 | } |
232 | ||
233 | int | |
234 | clip_to_bounds (lower, num, upper) | |
235 | int lower, num, upper; | |
236 | { | |
237 | if (num < lower) | |
238 | return lower; | |
239 | else if (num > upper) | |
240 | return upper; | |
241 | else | |
242 | return num; | |
243 | } | |
244 | ||
245 | DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ", | |
7ee72033 | 246 | doc: /* Set point to POSITION, a number or marker. |
a1f17501 PJ |
247 | Beginning of buffer is position (point-min), end is (point-max). |
248 | If the position is in the middle of a multibyte form, | |
249 | the actual point is set at the head of the multibyte form | |
7ee72033 MB |
250 | except in the case that `enable-multibyte-characters' is nil. */) |
251 | (position) | |
2591ec64 | 252 | register Lisp_Object position; |
35692fe0 | 253 | { |
fb8106e8 | 254 | int pos; |
fb8106e8 | 255 | |
72ef82ec RS |
256 | if (MARKERP (position) |
257 | && current_buffer == XMARKER (position)->buffer) | |
ec1c14f6 RS |
258 | { |
259 | pos = marker_position (position); | |
260 | if (pos < BEGV) | |
261 | SET_PT_BOTH (BEGV, BEGV_BYTE); | |
262 | else if (pos > ZV) | |
263 | SET_PT_BOTH (ZV, ZV_BYTE); | |
264 | else | |
265 | SET_PT_BOTH (pos, marker_byte_position (position)); | |
266 | ||
267 | return position; | |
268 | } | |
269 | ||
b7826503 | 270 | CHECK_NUMBER_COERCE_MARKER (position); |
35692fe0 | 271 | |
fb8106e8 | 272 | pos = clip_to_bounds (BEGV, XINT (position), ZV); |
fb8106e8 | 273 | SET_PT (pos); |
2591ec64 | 274 | return position; |
35692fe0 JB |
275 | } |
276 | ||
acb7cc89 GM |
277 | |
278 | /* Return the start or end position of the region. | |
279 | BEGINNINGP non-zero means return the start. | |
280 | If there is no region active, signal an error. */ | |
281 | ||
35692fe0 JB |
282 | static Lisp_Object |
283 | region_limit (beginningp) | |
284 | int beginningp; | |
285 | { | |
646d9d18 | 286 | extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */ |
acb7cc89 GM |
287 | Lisp_Object m; |
288 | ||
289 | if (!NILP (Vtransient_mark_mode) | |
290 | && NILP (Vmark_even_if_inactive) | |
c9dd14e1 RM |
291 | && NILP (current_buffer->mark_active)) |
292 | Fsignal (Qmark_inactive, Qnil); | |
acb7cc89 | 293 | |
35692fe0 | 294 | m = Fmarker_position (current_buffer->mark); |
acb7cc89 GM |
295 | if (NILP (m)) |
296 | error ("There is no region now"); | |
297 | ||
6ec8bbd2 | 298 | if ((PT < XFASTINT (m)) == beginningp) |
acb7cc89 GM |
299 | m = make_number (PT); |
300 | return m; | |
35692fe0 JB |
301 | } |
302 | ||
303 | DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0, | |
7ee72033 MB |
304 | doc: /* Return position of beginning of region, as an integer. */) |
305 | () | |
35692fe0 | 306 | { |
acb7cc89 | 307 | return region_limit (1); |
35692fe0 JB |
308 | } |
309 | ||
310 | DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0, | |
7ee72033 MB |
311 | doc: /* Return position of end of region, as an integer. */) |
312 | () | |
35692fe0 | 313 | { |
acb7cc89 | 314 | return region_limit (0); |
35692fe0 JB |
315 | } |
316 | ||
35692fe0 | 317 | DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0, |
7ee72033 | 318 | doc: /* Return this buffer's mark, as a marker object. |
a1f17501 | 319 | Watch out! Moving this marker changes the mark position. |
7ee72033 MB |
320 | If you set the marker not to point anywhere, the buffer will have no mark. */) |
321 | () | |
35692fe0 JB |
322 | { |
323 | return current_buffer->mark; | |
324 | } | |
acb7cc89 | 325 | |
c9ed721d | 326 | \f |
04f250d4 GM |
327 | #if 0 /* Not used. */ |
328 | ||
a3caef99 RS |
329 | /* Return nonzero if POS1 and POS2 have the same value |
330 | for the text property PROP. */ | |
0daf6e8d | 331 | |
0daf6e8d | 332 | static int |
f4d708d0 | 333 | char_property_eq (prop, pos1, pos2) |
0daf6e8d GM |
334 | Lisp_Object prop; |
335 | Lisp_Object pos1, pos2; | |
336 | { | |
337 | Lisp_Object pval1, pval2; | |
338 | ||
f4d708d0 MB |
339 | pval1 = Fget_char_property (pos1, prop, Qnil); |
340 | pval2 = Fget_char_property (pos2, prop, Qnil); | |
0daf6e8d GM |
341 | |
342 | return EQ (pval1, pval2); | |
343 | } | |
344 | ||
04f250d4 GM |
345 | #endif /* 0 */ |
346 | ||
7ae1c032 | 347 | /* Return the direction from which the text-property PROP would be |
a3caef99 RS |
348 | inherited by any new text inserted at POS: 1 if it would be |
349 | inherited from the char after POS, -1 if it would be inherited from | |
350 | the char before POS, and 0 if from neither. */ | |
351 | ||
0daf6e8d | 352 | static int |
7ae1c032 | 353 | text_property_stickiness (prop, pos) |
0daf6e8d GM |
354 | Lisp_Object prop; |
355 | Lisp_Object pos; | |
356 | { | |
f8d397ee MB |
357 | Lisp_Object prev_pos, front_sticky; |
358 | int is_rear_sticky = 1, is_front_sticky = 0; /* defaults */ | |
0daf6e8d | 359 | |
a3caef99 RS |
360 | if (XINT (pos) > BEGV) |
361 | /* Consider previous character. */ | |
0daf6e8d | 362 | { |
f8d397ee MB |
363 | Lisp_Object rear_non_sticky; |
364 | ||
365 | prev_pos = make_number (XINT (pos) - 1); | |
366 | rear_non_sticky = Fget_text_property (prev_pos, Qrear_nonsticky, Qnil); | |
367 | ||
e1010ebe SM |
368 | if (!NILP (CONSP (rear_non_sticky) |
369 | ? Fmemq (prop, rear_non_sticky) | |
370 | : rear_non_sticky)) | |
f8d397ee MB |
371 | /* PROP is rear-non-sticky. */ |
372 | is_rear_sticky = 0; | |
0daf6e8d GM |
373 | } |
374 | ||
a3caef99 | 375 | /* Consider following character. */ |
7ae1c032 | 376 | front_sticky = Fget_text_property (pos, Qfront_sticky, Qnil); |
0daf6e8d GM |
377 | |
378 | if (EQ (front_sticky, Qt) | |
379 | || (CONSP (front_sticky) | |
c01fbf95 | 380 | && !NILP (Fmemq (prop, front_sticky)))) |
a3caef99 | 381 | /* PROP is inherited from after. */ |
f8d397ee | 382 | is_front_sticky = 1; |
0daf6e8d | 383 | |
f8d397ee MB |
384 | /* Simple cases, where the properties are consistent. */ |
385 | if (is_rear_sticky && !is_front_sticky) | |
386 | return -1; | |
387 | else if (!is_rear_sticky && is_front_sticky) | |
388 | return 1; | |
389 | else if (!is_rear_sticky && !is_front_sticky) | |
390 | return 0; | |
391 | ||
392 | /* The stickiness properties are inconsistent, so we have to | |
393 | disambiguate. Basically, rear-sticky wins, _except_ if the | |
394 | property that would be inherited has a value of nil, in which case | |
395 | front-sticky wins. */ | |
396 | if (XINT (pos) == BEGV || NILP (Fget_text_property (prev_pos, prop, Qnil))) | |
397 | return 1; | |
398 | else | |
399 | return -1; | |
0daf6e8d | 400 | } |
ee547125 | 401 | |
acb7cc89 | 402 | \f |
a3caef99 | 403 | /* Find the field surrounding POS in *BEG and *END. If POS is nil, |
acb7cc89 GM |
404 | the value of point is used instead. If BEG or END null, |
405 | means don't store the beginning or end of the field. | |
a3caef99 | 406 | |
9ac741c5 MB |
407 | BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned |
408 | results; they do not effect boundary behavior. | |
409 | ||
a3caef99 | 410 | If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first |
ee547125 MB |
411 | position of a field, then the beginning of the previous field is |
412 | returned instead of the beginning of POS's field (since the end of a | |
413 | field is actually also the beginning of the next input field, this | |
414 | behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY | |
415 | true case, if two fields are separated by a field with the special | |
416 | value `boundary', and POS lies within it, then the two separated | |
417 | fields are considered to be adjacent, and POS between them, when | |
418 | finding the beginning and ending of the "merged" field. | |
a3caef99 RS |
419 | |
420 | Either BEG or END may be 0, in which case the corresponding value | |
421 | is not stored. */ | |
422 | ||
acb7cc89 | 423 | static void |
9ac741c5 | 424 | find_field (pos, merge_at_boundary, beg_limit, beg, end_limit, end) |
0daf6e8d GM |
425 | Lisp_Object pos; |
426 | Lisp_Object merge_at_boundary; | |
9ac741c5 | 427 | Lisp_Object beg_limit, end_limit; |
0daf6e8d GM |
428 | int *beg, *end; |
429 | { | |
ee547125 MB |
430 | /* Fields right before and after the point. */ |
431 | Lisp_Object before_field, after_field; | |
7ae1c032 MB |
432 | /* If the fields came from overlays, the associated overlays. |
433 | Qnil means they came from text-properties. */ | |
434 | Lisp_Object before_overlay = Qnil, after_overlay = Qnil; | |
a3caef99 RS |
435 | /* 1 if POS counts as the start of a field. */ |
436 | int at_field_start = 0; | |
437 | /* 1 if POS counts as the end of a field. */ | |
438 | int at_field_end = 0; | |
ee547125 | 439 | |
0daf6e8d GM |
440 | if (NILP (pos)) |
441 | XSETFASTINT (pos, PT); | |
442 | else | |
b7826503 | 443 | CHECK_NUMBER_COERCE_MARKER (pos); |
0daf6e8d | 444 | |
acb7cc89 | 445 | after_field |
7ae1c032 | 446 | = get_char_property_and_overlay (pos, Qfield, Qnil, &after_overlay); |
acb7cc89 GM |
447 | before_field |
448 | = (XFASTINT (pos) > BEGV | |
7ae1c032 MB |
449 | ? get_char_property_and_overlay (make_number (XINT (pos) - 1), |
450 | Qfield, Qnil, | |
451 | &before_overlay) | |
acb7cc89 | 452 | : Qnil); |
ee547125 MB |
453 | |
454 | /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil | |
455 | and POS is at beginning of a field, which can also be interpreted | |
456 | as the end of the previous field. Note that the case where if | |
457 | MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the | |
458 | more natural one; then we avoid treating the beginning of a field | |
459 | specially. */ | |
460 | if (NILP (merge_at_boundary) && !EQ (after_field, before_field)) | |
461 | /* We are at a boundary, see which direction is inclusive. We | |
462 | decide by seeing which field the `field' property sticks to. */ | |
463 | { | |
7ae1c032 MB |
464 | /* -1 means insertions go into before_field, 1 means they go |
465 | into after_field, 0 means neither. */ | |
466 | int stickiness; | |
467 | /* Whether the before/after_field come from overlays. */ | |
468 | int bop = !NILP (before_overlay); | |
469 | int aop = !NILP (after_overlay); | |
470 | ||
471 | if (bop && XMARKER (OVERLAY_END (before_overlay))->insertion_type == 1) | |
472 | /* before_field is from an overlay, which expands upon | |
473 | end-insertions. Note that it's possible for after_overlay to | |
474 | also eat insertions here, but then they will overlap, and | |
475 | there's not much we can do. */ | |
476 | stickiness = -1; | |
2480a59b MB |
477 | else if (aop |
478 | && XMARKER (OVERLAY_START (after_overlay))->insertion_type == 0) | |
7ae1c032 MB |
479 | /* after_field is from an overlay, which expand to contain |
480 | start-insertions. */ | |
481 | stickiness = 1; | |
482 | else if (bop && aop) | |
483 | /* Both fields come from overlays, but neither will contain any | |
484 | insertion here. */ | |
485 | stickiness = 0; | |
486 | else if (bop) | |
487 | /* before_field is an overlay that won't eat any insertion, but | |
488 | after_field is from a text-property. Assume that the | |
489 | text-property continues underneath the overlay, and so will | |
490 | be inherited by any insertion, regardless of any stickiness | |
491 | settings. */ | |
492 | stickiness = 1; | |
493 | else if (aop) | |
494 | /* Similarly, when after_field is the overlay. */ | |
495 | stickiness = -1; | |
496 | else | |
497 | /* Both fields come from text-properties. Look for explicit | |
498 | stickiness properties. */ | |
499 | stickiness = text_property_stickiness (Qfield, pos); | |
ee547125 MB |
500 | |
501 | if (stickiness > 0) | |
502 | at_field_start = 1; | |
503 | else if (stickiness < 0) | |
504 | at_field_end = 1; | |
505 | else | |
506 | /* STICKINESS == 0 means that any inserted text will get a | |
507 | `field' char-property of nil, so check to see if that | |
508 | matches either of the adjacent characters (this being a | |
509 | kind of "stickiness by default"). */ | |
0daf6e8d | 510 | { |
ee547125 MB |
511 | if (NILP (before_field)) |
512 | at_field_end = 1; /* Sticks to the left. */ | |
513 | else if (NILP (after_field)) | |
514 | at_field_start = 1; /* Sticks to the right. */ | |
0daf6e8d GM |
515 | } |
516 | } | |
517 | ||
ee547125 MB |
518 | /* Note about special `boundary' fields: |
519 | ||
520 | Consider the case where the point (`.') is between the fields `x' and `y': | |
521 | ||
522 | xxxx.yyyy | |
523 | ||
524 | In this situation, if merge_at_boundary is true, we consider the | |
525 | `x' and `y' fields as forming one big merged field, and so the end | |
526 | of the field is the end of `y'. | |
527 | ||
528 | However, if `x' and `y' are separated by a special `boundary' field | |
529 | (a field with a `field' char-property of 'boundary), then we ignore | |
530 | this special field when merging adjacent fields. Here's the same | |
531 | situation, but with a `boundary' field between the `x' and `y' fields: | |
532 | ||
533 | xxx.BBBByyyy | |
534 | ||
535 | Here, if point is at the end of `x', the beginning of `y', or | |
536 | anywhere in-between (within the `boundary' field), we merge all | |
537 | three fields and consider the beginning as being the beginning of | |
538 | the `x' field, and the end as being the end of the `y' field. */ | |
539 | ||
0daf6e8d | 540 | if (beg) |
acb7cc89 GM |
541 | { |
542 | if (at_field_start) | |
543 | /* POS is at the edge of a field, and we should consider it as | |
544 | the beginning of the following field. */ | |
545 | *beg = XFASTINT (pos); | |
546 | else | |
547 | /* Find the previous field boundary. */ | |
548 | { | |
549 | if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary)) | |
550 | /* Skip a `boundary' field. */ | |
9ac741c5 MB |
551 | pos = Fprevious_single_char_property_change (pos, Qfield, Qnil, |
552 | beg_limit); | |
ee547125 | 553 | |
9ac741c5 MB |
554 | pos = Fprevious_single_char_property_change (pos, Qfield, Qnil, |
555 | beg_limit); | |
acb7cc89 GM |
556 | *beg = NILP (pos) ? BEGV : XFASTINT (pos); |
557 | } | |
558 | } | |
0daf6e8d GM |
559 | |
560 | if (end) | |
acb7cc89 GM |
561 | { |
562 | if (at_field_end) | |
563 | /* POS is at the edge of a field, and we should consider it as | |
564 | the end of the previous field. */ | |
565 | *end = XFASTINT (pos); | |
566 | else | |
567 | /* Find the next field boundary. */ | |
568 | { | |
569 | if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary)) | |
570 | /* Skip a `boundary' field. */ | |
9ac741c5 MB |
571 | pos = Fnext_single_char_property_change (pos, Qfield, Qnil, |
572 | end_limit); | |
ee547125 | 573 | |
9ac741c5 MB |
574 | pos = Fnext_single_char_property_change (pos, Qfield, Qnil, |
575 | end_limit); | |
acb7cc89 GM |
576 | *end = NILP (pos) ? ZV : XFASTINT (pos); |
577 | } | |
578 | } | |
0daf6e8d | 579 | } |
acb7cc89 | 580 | |
0daf6e8d | 581 | \f |
d01f3570 | 582 | DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0, |
7ee72033 | 583 | doc: /* Delete the field surrounding POS. |
a1f17501 | 584 | A field is a region of text with the same `field' property. |
7ee72033 MB |
585 | If POS is nil, the value of point is used for POS. */) |
586 | (pos) | |
0daf6e8d GM |
587 | Lisp_Object pos; |
588 | { | |
589 | int beg, end; | |
9ac741c5 | 590 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
591 | if (beg != end) |
592 | del_range (beg, end); | |
d01f3570 | 593 | return Qnil; |
0daf6e8d GM |
594 | } |
595 | ||
596 | DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0, | |
7ee72033 | 597 | doc: /* Return the contents of the field surrounding POS as a string. |
a1f17501 | 598 | A field is a region of text with the same `field' property. |
7ee72033 MB |
599 | If POS is nil, the value of point is used for POS. */) |
600 | (pos) | |
0daf6e8d GM |
601 | Lisp_Object pos; |
602 | { | |
603 | int beg, end; | |
9ac741c5 | 604 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
605 | return make_buffer_string (beg, end, 1); |
606 | } | |
607 | ||
608 | DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0, | |
7ee72033 | 609 | doc: /* Return the contents of the field around POS, without text-properties. |
a1f17501 | 610 | A field is a region of text with the same `field' property. |
7ee72033 MB |
611 | If POS is nil, the value of point is used for POS. */) |
612 | (pos) | |
0daf6e8d GM |
613 | Lisp_Object pos; |
614 | { | |
615 | int beg, end; | |
9ac741c5 | 616 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
617 | return make_buffer_string (beg, end, 0); |
618 | } | |
619 | ||
9ac741c5 | 620 | DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0, |
7ee72033 | 621 | doc: /* Return the beginning of the field surrounding POS. |
a1f17501 PJ |
622 | A field is a region of text with the same `field' property. |
623 | If POS is nil, the value of point is used for POS. | |
624 | If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its | |
9ac741c5 MB |
625 | field, then the beginning of the *previous* field is returned. |
626 | If LIMIT is non-nil, it is a buffer position; if the beginning of the field | |
627 | is before LIMIT, then LIMIT will be returned instead. */) | |
628 | (pos, escape_from_edge, limit) | |
629 | Lisp_Object pos, escape_from_edge, limit; | |
0daf6e8d GM |
630 | { |
631 | int beg; | |
9ac741c5 | 632 | find_field (pos, escape_from_edge, limit, &beg, Qnil, 0); |
0daf6e8d GM |
633 | return make_number (beg); |
634 | } | |
635 | ||
9ac741c5 | 636 | DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0, |
7ee72033 | 637 | doc: /* Return the end of the field surrounding POS. |
a1f17501 PJ |
638 | A field is a region of text with the same `field' property. |
639 | If POS is nil, the value of point is used for POS. | |
640 | If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field, | |
9ac741c5 MB |
641 | then the end of the *following* field is returned. |
642 | If LIMIT is non-nil, it is a buffer position; if the end of the field | |
643 | is after LIMIT, then LIMIT will be returned instead. */) | |
644 | (pos, escape_from_edge, limit) | |
645 | Lisp_Object pos, escape_from_edge, limit; | |
0daf6e8d GM |
646 | { |
647 | int end; | |
9ac741c5 | 648 | find_field (pos, escape_from_edge, Qnil, 0, limit, &end); |
0daf6e8d GM |
649 | return make_number (end); |
650 | } | |
651 | ||
ee547125 | 652 | DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0, |
7ee72033 | 653 | doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS. |
a1f17501 PJ |
654 | |
655 | A field is a region of text with the same `field' property. | |
656 | If NEW-POS is nil, then the current point is used instead, and set to the | |
657 | constrained position if that is different. | |
658 | ||
659 | If OLD-POS is at the boundary of two fields, then the allowable | |
660 | positions for NEW-POS depends on the value of the optional argument | |
661 | ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is | |
662 | constrained to the field that has the same `field' char-property | |
663 | as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE | |
664 | is non-nil, NEW-POS is constrained to the union of the two adjacent | |
665 | fields. Additionally, if two fields are separated by another field with | |
666 | the special value `boundary', then any point within this special field is | |
667 | also considered to be `on the boundary'. | |
668 | ||
669 | If the optional argument ONLY-IN-LINE is non-nil and constraining | |
670 | NEW-POS would move it to a different line, NEW-POS is returned | |
671 | unconstrained. This useful for commands that move by line, like | |
672 | \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries | |
673 | only in the case where they can still move to the right line. | |
674 | ||
675 | If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has | |
676 | a non-nil property of that name, then any field boundaries are ignored. | |
677 | ||
7ee72033 MB |
678 | Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */) |
679 | (new_pos, old_pos, escape_from_edge, only_in_line, inhibit_capture_property) | |
ee547125 MB |
680 | Lisp_Object new_pos, old_pos; |
681 | Lisp_Object escape_from_edge, only_in_line, inhibit_capture_property; | |
0daf6e8d GM |
682 | { |
683 | /* If non-zero, then the original point, before re-positioning. */ | |
684 | int orig_point = 0; | |
685 | ||
686 | if (NILP (new_pos)) | |
687 | /* Use the current point, and afterwards, set it. */ | |
688 | { | |
689 | orig_point = PT; | |
690 | XSETFASTINT (new_pos, PT); | |
691 | } | |
692 | ||
ee5cd4db GM |
693 | if (NILP (Vinhibit_field_text_motion) |
694 | && !EQ (new_pos, old_pos) | |
f657bbf6 MB |
695 | && (!NILP (Fget_char_property (new_pos, Qfield, Qnil)) |
696 | || !NILP (Fget_char_property (old_pos, Qfield, Qnil))) | |
ee547125 MB |
697 | && (NILP (inhibit_capture_property) |
698 | || NILP (Fget_char_property(old_pos, inhibit_capture_property, Qnil)))) | |
0daf6e8d GM |
699 | /* NEW_POS is not within the same field as OLD_POS; try to |
700 | move NEW_POS so that it is. */ | |
701 | { | |
ee547125 | 702 | int fwd, shortage; |
0daf6e8d GM |
703 | Lisp_Object field_bound; |
704 | ||
b7826503 PJ |
705 | CHECK_NUMBER_COERCE_MARKER (new_pos); |
706 | CHECK_NUMBER_COERCE_MARKER (old_pos); | |
0daf6e8d GM |
707 | |
708 | fwd = (XFASTINT (new_pos) > XFASTINT (old_pos)); | |
709 | ||
710 | if (fwd) | |
9ac741c5 | 711 | field_bound = Ffield_end (old_pos, escape_from_edge, new_pos); |
0daf6e8d | 712 | else |
9ac741c5 | 713 | field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos); |
0daf6e8d | 714 | |
10b0f752 MB |
715 | if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the |
716 | other side of NEW_POS, which would mean that NEW_POS is | |
717 | already acceptable, and it's not necessary to constrain it | |
718 | to FIELD_BOUND. */ | |
719 | ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd) | |
720 | /* NEW_POS should be constrained, but only if either | |
721 | ONLY_IN_LINE is nil (in which case any constraint is OK), | |
722 | or NEW_POS and FIELD_BOUND are on the same line (in which | |
723 | case the constraint is OK even if ONLY_IN_LINE is non-nil). */ | |
724 | && (NILP (only_in_line) | |
725 | /* This is the ONLY_IN_LINE case, check that NEW_POS and | |
726 | FIELD_BOUND are on the same line by seeing whether | |
727 | there's an intervening newline or not. */ | |
728 | || (scan_buffer ('\n', | |
729 | XFASTINT (new_pos), XFASTINT (field_bound), | |
730 | fwd ? -1 : 1, &shortage, 1), | |
731 | shortage != 0))) | |
0daf6e8d GM |
732 | /* Constrain NEW_POS to FIELD_BOUND. */ |
733 | new_pos = field_bound; | |
734 | ||
735 | if (orig_point && XFASTINT (new_pos) != orig_point) | |
736 | /* The NEW_POS argument was originally nil, so automatically set PT. */ | |
737 | SET_PT (XFASTINT (new_pos)); | |
738 | } | |
739 | ||
740 | return new_pos; | |
741 | } | |
acb7cc89 | 742 | |
0daf6e8d | 743 | \f |
6d57c318 MB |
744 | DEFUN ("line-beginning-position", |
745 | Fline_beginning_position, Sline_beginning_position, 0, 1, 0, | |
7ee72033 | 746 | doc: /* Return the character position of the first character on the current line. |
a1f17501 PJ |
747 | With argument N not nil or 1, move forward N - 1 lines first. |
748 | If scan reaches end of buffer, return that position. | |
6d57c318 MB |
749 | |
750 | The scan does not cross a field boundary unless doing so would move | |
751 | beyond there to a different line; if N is nil or 1, and scan starts at a | |
752 | field boundary, the scan stops as soon as it starts. To ignore field | |
753 | boundaries bind `inhibit-field-text-motion' to t. | |
a1f17501 | 754 | |
7ee72033 MB |
755 | This function does not move point. */) |
756 | (n) | |
c9ed721d RS |
757 | Lisp_Object n; |
758 | { | |
acb7cc89 | 759 | int orig, orig_byte, end; |
c9ed721d RS |
760 | |
761 | if (NILP (n)) | |
762 | XSETFASTINT (n, 1); | |
763 | else | |
b7826503 | 764 | CHECK_NUMBER (n); |
c9ed721d RS |
765 | |
766 | orig = PT; | |
ec1c14f6 | 767 | orig_byte = PT_BYTE; |
c9ed721d RS |
768 | Fforward_line (make_number (XINT (n) - 1)); |
769 | end = PT; | |
e2dae3f2 | 770 | |
ec1c14f6 | 771 | SET_PT_BOTH (orig, orig_byte); |
35692fe0 | 772 | |
0daf6e8d | 773 | /* Return END constrained to the current input field. */ |
ee5cd4db GM |
774 | return Fconstrain_to_field (make_number (end), make_number (orig), |
775 | XINT (n) != 1 ? Qt : Qnil, | |
ee547125 | 776 | Qt, Qnil); |
c9ed721d RS |
777 | } |
778 | ||
6d57c318 | 779 | DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0, |
7ee72033 | 780 | doc: /* Return the character position of the last character on the current line. |
a1f17501 PJ |
781 | With argument N not nil or 1, move forward N - 1 lines first. |
782 | If scan reaches end of buffer, return that position. | |
6d57c318 MB |
783 | |
784 | The scan does not cross a field boundary unless doing so would move | |
785 | beyond there to a different line; if N is nil or 1, and scan starts at a | |
786 | field boundary, the scan stops as soon as it starts. To ignore field | |
787 | boundaries bind `inhibit-field-text-motion' to t. | |
788 | ||
7ee72033 MB |
789 | This function does not move point. */) |
790 | (n) | |
c9ed721d RS |
791 | Lisp_Object n; |
792 | { | |
0daf6e8d | 793 | int end_pos; |
acb7cc89 | 794 | int orig = PT; |
0daf6e8d | 795 | |
c9ed721d RS |
796 | if (NILP (n)) |
797 | XSETFASTINT (n, 1); | |
798 | else | |
b7826503 | 799 | CHECK_NUMBER (n); |
c9ed721d | 800 | |
0daf6e8d GM |
801 | end_pos = find_before_next_newline (orig, 0, XINT (n) - (XINT (n) <= 0)); |
802 | ||
803 | /* Return END_POS constrained to the current input field. */ | |
ee5cd4db | 804 | return Fconstrain_to_field (make_number (end_pos), make_number (orig), |
ee547125 | 805 | Qnil, Qt, Qnil); |
c9ed721d | 806 | } |
6d57c318 | 807 | |
c9ed721d | 808 | \f |
35692fe0 JB |
809 | Lisp_Object |
810 | save_excursion_save () | |
811 | { | |
acb7cc89 GM |
812 | int visible = (XBUFFER (XWINDOW (selected_window)->buffer) |
813 | == current_buffer); | |
35692fe0 JB |
814 | |
815 | return Fcons (Fpoint_marker (), | |
aea4a109 | 816 | Fcons (Fcopy_marker (current_buffer->mark, Qnil), |
9772455e | 817 | Fcons (visible ? Qt : Qnil, |
2483cf58 GM |
818 | Fcons (current_buffer->mark_active, |
819 | selected_window)))); | |
35692fe0 JB |
820 | } |
821 | ||
822 | Lisp_Object | |
823 | save_excursion_restore (info) | |
4ad8681a | 824 | Lisp_Object info; |
35692fe0 | 825 | { |
4ad8681a RS |
826 | Lisp_Object tem, tem1, omark, nmark; |
827 | struct gcpro gcpro1, gcpro2, gcpro3; | |
2483cf58 | 828 | int visible_p; |
35692fe0 | 829 | |
2483cf58 | 830 | tem = Fmarker_buffer (XCAR (info)); |
35692fe0 JB |
831 | /* If buffer being returned to is now deleted, avoid error */ |
832 | /* Otherwise could get error here while unwinding to top level | |
833 | and crash */ | |
834 | /* In that case, Fmarker_buffer returns nil now. */ | |
56a98455 | 835 | if (NILP (tem)) |
35692fe0 | 836 | return Qnil; |
4ad8681a RS |
837 | |
838 | omark = nmark = Qnil; | |
839 | GCPRO3 (info, omark, nmark); | |
840 | ||
35692fe0 | 841 | Fset_buffer (tem); |
2483cf58 GM |
842 | |
843 | /* Point marker. */ | |
844 | tem = XCAR (info); | |
35692fe0 JB |
845 | Fgoto_char (tem); |
846 | unchain_marker (tem); | |
2483cf58 GM |
847 | |
848 | /* Mark marker. */ | |
849 | info = XCDR (info); | |
850 | tem = XCAR (info); | |
03d18690 | 851 | omark = Fmarker_position (current_buffer->mark); |
35692fe0 | 852 | Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ()); |
03d18690 | 853 | nmark = Fmarker_position (tem); |
35692fe0 | 854 | unchain_marker (tem); |
2483cf58 GM |
855 | |
856 | /* visible */ | |
857 | info = XCDR (info); | |
858 | visible_p = !NILP (XCAR (info)); | |
859 | ||
ef580991 RS |
860 | #if 0 /* We used to make the current buffer visible in the selected window |
861 | if that was true previously. That avoids some anomalies. | |
862 | But it creates others, and it wasn't documented, and it is simpler | |
863 | and cleaner never to alter the window/buffer connections. */ | |
9772455e RS |
864 | tem1 = Fcar (tem); |
865 | if (!NILP (tem1) | |
0e2c9c70 | 866 | && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)) |
35692fe0 | 867 | Fswitch_to_buffer (Fcurrent_buffer (), Qnil); |
ef580991 | 868 | #endif /* 0 */ |
9772455e | 869 | |
2483cf58 GM |
870 | /* Mark active */ |
871 | info = XCDR (info); | |
872 | tem = XCAR (info); | |
9772455e | 873 | tem1 = current_buffer->mark_active; |
2483cf58 GM |
874 | current_buffer->mark_active = tem; |
875 | ||
9fed2b18 RS |
876 | if (!NILP (Vrun_hooks)) |
877 | { | |
03d18690 RS |
878 | /* If mark is active now, and either was not active |
879 | or was at a different place, run the activate hook. */ | |
9fed2b18 | 880 | if (! NILP (current_buffer->mark_active)) |
03d18690 RS |
881 | { |
882 | if (! EQ (omark, nmark)) | |
883 | call1 (Vrun_hooks, intern ("activate-mark-hook")); | |
884 | } | |
885 | /* If mark has ceased to be active, run deactivate hook. */ | |
9fed2b18 RS |
886 | else if (! NILP (tem1)) |
887 | call1 (Vrun_hooks, intern ("deactivate-mark-hook")); | |
888 | } | |
2483cf58 GM |
889 | |
890 | /* If buffer was visible in a window, and a different window was | |
793cd2c8 GM |
891 | selected, and the old selected window is still showing this |
892 | buffer, restore point in that window. */ | |
2483cf58 GM |
893 | tem = XCDR (info); |
894 | if (visible_p | |
895 | && !EQ (tem, selected_window) | |
ba973f7a GM |
896 | && (tem1 = XWINDOW (tem)->buffer, |
897 | (/* Window is live... */ | |
898 | BUFFERP (tem1) | |
899 | /* ...and it shows the current buffer. */ | |
900 | && XBUFFER (tem1) == current_buffer))) | |
2483cf58 GM |
901 | Fset_window_point (tem, make_number (PT)); |
902 | ||
4ad8681a | 903 | UNGCPRO; |
35692fe0 JB |
904 | return Qnil; |
905 | } | |
906 | ||
907 | DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0, | |
7ee72033 | 908 | doc: /* Save point, mark, and current buffer; execute BODY; restore those things. |
a1f17501 PJ |
909 | Executes BODY just like `progn'. |
910 | The values of point, mark and the current buffer are restored | |
911 | even in case of abnormal exit (throw or error). | |
912 | The state of activation of the mark is also restored. | |
913 | ||
914 | This construct does not save `deactivate-mark', and therefore | |
915 | functions that change the buffer will still cause deactivation | |
916 | of the mark at the end of the command. To prevent that, bind | |
33c2d29f MB |
917 | `deactivate-mark' with `let'. |
918 | ||
919 | usage: (save-excursion &rest BODY) */) | |
7ee72033 | 920 | (args) |
35692fe0 JB |
921 | Lisp_Object args; |
922 | { | |
923 | register Lisp_Object val; | |
924 | int count = specpdl_ptr - specpdl; | |
925 | ||
926 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
4bc8c7d2 RS |
927 | |
928 | val = Fprogn (args); | |
929 | return unbind_to (count, val); | |
930 | } | |
931 | ||
932 | DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0, | |
7ee72033 | 933 | doc: /* Save the current buffer; execute BODY; restore the current buffer. |
33c2d29f MB |
934 | Executes BODY just like `progn'. |
935 | usage: (save-current-buffer &rest BODY) */) | |
7ee72033 | 936 | (args) |
4bc8c7d2 RS |
937 | Lisp_Object args; |
938 | { | |
acb7cc89 | 939 | Lisp_Object val; |
4bc8c7d2 RS |
940 | int count = specpdl_ptr - specpdl; |
941 | ||
cb5e5f74 | 942 | record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ()); |
4bc8c7d2 | 943 | |
35692fe0 JB |
944 | val = Fprogn (args); |
945 | return unbind_to (count, val); | |
946 | } | |
947 | \f | |
95dccf75 | 948 | DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0, |
7ee72033 MB |
949 | doc: /* Return the number of characters in the current buffer. |
950 | If BUFFER, return the number of characters in that buffer instead. */) | |
951 | (buffer) | |
95dccf75 | 952 | Lisp_Object buffer; |
35692fe0 | 953 | { |
95dccf75 RS |
954 | if (NILP (buffer)) |
955 | return make_number (Z - BEG); | |
02050596 RS |
956 | else |
957 | { | |
b7826503 | 958 | CHECK_BUFFER (buffer); |
02050596 RS |
959 | return make_number (BUF_Z (XBUFFER (buffer)) |
960 | - BUF_BEG (XBUFFER (buffer))); | |
961 | } | |
35692fe0 JB |
962 | } |
963 | ||
964 | DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0, | |
7ee72033 MB |
965 | doc: /* Return the minimum permissible value of point in the current buffer. |
966 | This is 1, unless narrowing (a buffer restriction) is in effect. */) | |
967 | () | |
35692fe0 JB |
968 | { |
969 | Lisp_Object temp; | |
55561c63 | 970 | XSETFASTINT (temp, BEGV); |
35692fe0 JB |
971 | return temp; |
972 | } | |
973 | ||
974 | DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0, | |
7ee72033 MB |
975 | doc: /* Return a marker to the minimum permissible value of point in this buffer. |
976 | This is the beginning, unless narrowing (a buffer restriction) is in effect. */) | |
977 | () | |
35692fe0 | 978 | { |
ec1c14f6 | 979 | return buildmark (BEGV, BEGV_BYTE); |
35692fe0 JB |
980 | } |
981 | ||
982 | DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0, | |
7ee72033 | 983 | doc: /* Return the maximum permissible value of point in the current buffer. |
a1f17501 | 984 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction) |
7ee72033 MB |
985 | is in effect, in which case it is less. */) |
986 | () | |
35692fe0 JB |
987 | { |
988 | Lisp_Object temp; | |
55561c63 | 989 | XSETFASTINT (temp, ZV); |
35692fe0 JB |
990 | return temp; |
991 | } | |
992 | ||
993 | DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0, | |
7ee72033 | 994 | doc: /* Return a marker to the maximum permissible value of point in this buffer. |
a1f17501 | 995 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction) |
7ee72033 MB |
996 | is in effect, in which case it is less. */) |
997 | () | |
35692fe0 | 998 | { |
ec1c14f6 | 999 | return buildmark (ZV, ZV_BYTE); |
35692fe0 JB |
1000 | } |
1001 | ||
c86212b9 | 1002 | DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0, |
7ee72033 MB |
1003 | doc: /* Return the position of the gap, in the current buffer. |
1004 | See also `gap-size'. */) | |
1005 | () | |
c86212b9 RS |
1006 | { |
1007 | Lisp_Object temp; | |
1008 | XSETFASTINT (temp, GPT); | |
1009 | return temp; | |
1010 | } | |
1011 | ||
1012 | DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0, | |
7ee72033 MB |
1013 | doc: /* Return the size of the current buffer's gap. |
1014 | See also `gap-position'. */) | |
1015 | () | |
c86212b9 RS |
1016 | { |
1017 | Lisp_Object temp; | |
1018 | XSETFASTINT (temp, GAP_SIZE); | |
1019 | return temp; | |
1020 | } | |
1021 | ||
7df74da6 | 1022 | DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0, |
7ee72033 MB |
1023 | doc: /* Return the byte position for character position POSITION. |
1024 | If POSITION is out of range, the value is nil. */) | |
1025 | (position) | |
80e01f8d | 1026 | Lisp_Object position; |
7df74da6 | 1027 | { |
b7826503 | 1028 | CHECK_NUMBER_COERCE_MARKER (position); |
fcf9683e KH |
1029 | if (XINT (position) < BEG || XINT (position) > Z) |
1030 | return Qnil; | |
fa8a5a15 | 1031 | return make_number (CHAR_TO_BYTE (XINT (position))); |
7df74da6 | 1032 | } |
3ab0732d RS |
1033 | |
1034 | DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0, | |
7ee72033 MB |
1035 | doc: /* Return the character position for byte position BYTEPOS. |
1036 | If BYTEPOS is out of range, the value is nil. */) | |
1037 | (bytepos) | |
3ab0732d RS |
1038 | Lisp_Object bytepos; |
1039 | { | |
b7826503 | 1040 | CHECK_NUMBER (bytepos); |
fcf9683e KH |
1041 | if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE) |
1042 | return Qnil; | |
3ab0732d RS |
1043 | return make_number (BYTE_TO_CHAR (XINT (bytepos))); |
1044 | } | |
7df74da6 | 1045 | \f |
850a8179 | 1046 | DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0, |
7ee72033 MB |
1047 | doc: /* Return the character following point, as a number. |
1048 | At the end of the buffer or accessible region, return 0. */) | |
1049 | () | |
35692fe0 JB |
1050 | { |
1051 | Lisp_Object temp; | |
6ec8bbd2 | 1052 | if (PT >= ZV) |
55561c63 | 1053 | XSETFASTINT (temp, 0); |
850a8179 | 1054 | else |
ec1c14f6 | 1055 | XSETFASTINT (temp, FETCH_CHAR (PT_BYTE)); |
35692fe0 JB |
1056 | return temp; |
1057 | } | |
1058 | ||
850a8179 | 1059 | DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0, |
7ee72033 MB |
1060 | doc: /* Return the character preceding point, as a number. |
1061 | At the beginning of the buffer or accessible region, return 0. */) | |
1062 | () | |
35692fe0 JB |
1063 | { |
1064 | Lisp_Object temp; | |
6ec8bbd2 | 1065 | if (PT <= BEGV) |
55561c63 | 1066 | XSETFASTINT (temp, 0); |
fb8106e8 KH |
1067 | else if (!NILP (current_buffer->enable_multibyte_characters)) |
1068 | { | |
ec1c14f6 | 1069 | int pos = PT_BYTE; |
fb8106e8 KH |
1070 | DEC_POS (pos); |
1071 | XSETFASTINT (temp, FETCH_CHAR (pos)); | |
1072 | } | |
35692fe0 | 1073 | else |
ec1c14f6 | 1074 | XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1)); |
35692fe0 JB |
1075 | return temp; |
1076 | } | |
1077 | ||
1078 | DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0, | |
7ee72033 MB |
1079 | doc: /* Return t if point is at the beginning of the buffer. |
1080 | If the buffer is narrowed, this means the beginning of the narrowed part. */) | |
1081 | () | |
35692fe0 | 1082 | { |
6ec8bbd2 | 1083 | if (PT == BEGV) |
35692fe0 JB |
1084 | return Qt; |
1085 | return Qnil; | |
1086 | } | |
1087 | ||
1088 | DEFUN ("eobp", Feobp, Seobp, 0, 0, 0, | |
7ee72033 MB |
1089 | doc: /* Return t if point is at the end of the buffer. |
1090 | If the buffer is narrowed, this means the end of the narrowed part. */) | |
1091 | () | |
35692fe0 | 1092 | { |
6ec8bbd2 | 1093 | if (PT == ZV) |
35692fe0 JB |
1094 | return Qt; |
1095 | return Qnil; | |
1096 | } | |
1097 | ||
1098 | DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0, | |
7ee72033 MB |
1099 | doc: /* Return t if point is at the beginning of a line. */) |
1100 | () | |
35692fe0 | 1101 | { |
ec1c14f6 | 1102 | if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n') |
35692fe0 JB |
1103 | return Qt; |
1104 | return Qnil; | |
1105 | } | |
1106 | ||
1107 | DEFUN ("eolp", Feolp, Seolp, 0, 0, 0, | |
7ee72033 MB |
1108 | doc: /* Return t if point is at the end of a line. |
1109 | `End of a line' includes point being at the end of the buffer. */) | |
1110 | () | |
35692fe0 | 1111 | { |
ec1c14f6 | 1112 | if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n') |
35692fe0 JB |
1113 | return Qt; |
1114 | return Qnil; | |
1115 | } | |
1116 | ||
fa1d3816 | 1117 | DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0, |
7ee72033 | 1118 | doc: /* Return character in current buffer at position POS. |
a1f17501 | 1119 | POS is an integer or a marker. |
7ee72033 MB |
1120 | If POS is out of range, the value is nil. */) |
1121 | (pos) | |
35692fe0 JB |
1122 | Lisp_Object pos; |
1123 | { | |
ec1c14f6 | 1124 | register int pos_byte; |
35692fe0 | 1125 | |
fa1d3816 | 1126 | if (NILP (pos)) |
39a4c932 RS |
1127 | { |
1128 | pos_byte = PT_BYTE; | |
3c52e568 | 1129 | XSETFASTINT (pos, PT); |
39a4c932 RS |
1130 | } |
1131 | ||
1132 | if (MARKERP (pos)) | |
85cac557 RS |
1133 | { |
1134 | pos_byte = marker_byte_position (pos); | |
1135 | if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE) | |
1136 | return Qnil; | |
1137 | } | |
fa1d3816 RS |
1138 | else |
1139 | { | |
b7826503 | 1140 | CHECK_NUMBER_COERCE_MARKER (pos); |
b98ef0dc | 1141 | if (XINT (pos) < BEGV || XINT (pos) >= ZV) |
85cac557 | 1142 | return Qnil; |
34a7a267 | 1143 | |
ec1c14f6 | 1144 | pos_byte = CHAR_TO_BYTE (XINT (pos)); |
fa1d3816 | 1145 | } |
35692fe0 | 1146 | |
ec1c14f6 | 1147 | return make_number (FETCH_CHAR (pos_byte)); |
35692fe0 | 1148 | } |
fb8106e8 | 1149 | |
fa1d3816 | 1150 | DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0, |
7ee72033 | 1151 | doc: /* Return character in current buffer preceding position POS. |
a1f17501 | 1152 | POS is an integer or a marker. |
7ee72033 MB |
1153 | If POS is out of range, the value is nil. */) |
1154 | (pos) | |
fb8106e8 KH |
1155 | Lisp_Object pos; |
1156 | { | |
1157 | register Lisp_Object val; | |
ec1c14f6 | 1158 | register int pos_byte; |
fb8106e8 | 1159 | |
fa1d3816 | 1160 | if (NILP (pos)) |
39a4c932 RS |
1161 | { |
1162 | pos_byte = PT_BYTE; | |
3c52e568 | 1163 | XSETFASTINT (pos, PT); |
39a4c932 RS |
1164 | } |
1165 | ||
1166 | if (MARKERP (pos)) | |
85cac557 RS |
1167 | { |
1168 | pos_byte = marker_byte_position (pos); | |
1169 | ||
1170 | if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE) | |
1171 | return Qnil; | |
1172 | } | |
fa1d3816 RS |
1173 | else |
1174 | { | |
b7826503 | 1175 | CHECK_NUMBER_COERCE_MARKER (pos); |
fb8106e8 | 1176 | |
b98ef0dc | 1177 | if (XINT (pos) <= BEGV || XINT (pos) > ZV) |
85cac557 RS |
1178 | return Qnil; |
1179 | ||
ec1c14f6 | 1180 | pos_byte = CHAR_TO_BYTE (XINT (pos)); |
fa1d3816 | 1181 | } |
fb8106e8 KH |
1182 | |
1183 | if (!NILP (current_buffer->enable_multibyte_characters)) | |
1184 | { | |
ec1c14f6 RS |
1185 | DEC_POS (pos_byte); |
1186 | XSETFASTINT (val, FETCH_CHAR (pos_byte)); | |
fb8106e8 KH |
1187 | } |
1188 | else | |
1189 | { | |
ec1c14f6 RS |
1190 | pos_byte--; |
1191 | XSETFASTINT (val, FETCH_BYTE (pos_byte)); | |
fb8106e8 KH |
1192 | } |
1193 | return val; | |
1194 | } | |
35692fe0 | 1195 | \f |
87485d6f | 1196 | DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0, |
7ee72033 | 1197 | doc: /* Return the name under which the user logged in, as a string. |
a1f17501 PJ |
1198 | This is based on the effective uid, not the real uid. |
1199 | Also, if the environment variable LOGNAME or USER is set, | |
1200 | that determines the value of this function. | |
1201 | ||
1202 | If optional argument UID is an integer, return the login name of the user | |
7ee72033 MB |
1203 | with that uid, or nil if there is no such user. */) |
1204 | (uid) | |
87485d6f | 1205 | Lisp_Object uid; |
35692fe0 | 1206 | { |
87485d6f MW |
1207 | struct passwd *pw; |
1208 | ||
f8a0e364 RS |
1209 | /* Set up the user name info if we didn't do it before. |
1210 | (That can happen if Emacs is dumpable | |
1211 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 1212 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 1213 | init_editfns (); |
87485d6f MW |
1214 | |
1215 | if (NILP (uid)) | |
35b34f72 | 1216 | return Vuser_login_name; |
87485d6f | 1217 | |
b7826503 | 1218 | CHECK_NUMBER (uid); |
87485d6f MW |
1219 | pw = (struct passwd *) getpwuid (XINT (uid)); |
1220 | return (pw ? build_string (pw->pw_name) : Qnil); | |
35692fe0 JB |
1221 | } |
1222 | ||
1223 | DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name, | |
deb8e082 | 1224 | 0, 0, 0, |
7ee72033 | 1225 | doc: /* Return the name of the user's real uid, as a string. |
a1f17501 | 1226 | This ignores the environment variables LOGNAME and USER, so it differs from |
7ee72033 MB |
1227 | `user-login-name' when running under `su'. */) |
1228 | () | |
35692fe0 | 1229 | { |
f8a0e364 RS |
1230 | /* Set up the user name info if we didn't do it before. |
1231 | (That can happen if Emacs is dumpable | |
1232 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 1233 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 1234 | init_editfns (); |
35b34f72 | 1235 | return Vuser_real_login_name; |
35692fe0 JB |
1236 | } |
1237 | ||
1238 | DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0, | |
7ee72033 MB |
1239 | doc: /* Return the effective uid of Emacs. |
1240 | Value is an integer or float, depending on the value. */) | |
1241 | () | |
35692fe0 | 1242 | { |
1615d97a | 1243 | return make_fixnum_or_float (geteuid ()); |
35692fe0 JB |
1244 | } |
1245 | ||
1246 | DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0, | |
7ee72033 MB |
1247 | doc: /* Return the real uid of Emacs. |
1248 | Value is an integer or float, depending on the value. */) | |
1249 | () | |
35692fe0 | 1250 | { |
1615d97a | 1251 | return make_fixnum_or_float (getuid ()); |
35692fe0 JB |
1252 | } |
1253 | ||
c9ed721d | 1254 | DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0, |
7ee72033 | 1255 | doc: /* Return the full name of the user logged in, as a string. |
a1f17501 PJ |
1256 | If the full name corresponding to Emacs's userid is not known, |
1257 | return "unknown". | |
1258 | ||
1259 | If optional argument UID is an integer or float, return the full name | |
1260 | of the user with that uid, or nil if there is no such user. | |
1261 | If UID is a string, return the full name of the user with that login | |
7ee72033 MB |
1262 | name, or nil if there is no such user. */) |
1263 | (uid) | |
c9ed721d | 1264 | Lisp_Object uid; |
35692fe0 | 1265 | { |
c9ed721d | 1266 | struct passwd *pw; |
b0e92acd | 1267 | register unsigned char *p, *q; |
3415b0e9 | 1268 | Lisp_Object full; |
c9ed721d RS |
1269 | |
1270 | if (NILP (uid)) | |
34a7a267 | 1271 | return Vuser_full_name; |
3415b0e9 | 1272 | else if (NUMBERP (uid)) |
1615d97a | 1273 | pw = (struct passwd *) getpwuid ((uid_t) XFLOATINT (uid)); |
34a7a267 | 1274 | else if (STRINGP (uid)) |
3415b0e9 RS |
1275 | pw = (struct passwd *) getpwnam (XSTRING (uid)->data); |
1276 | else | |
1277 | error ("Invalid UID specification"); | |
c9ed721d | 1278 | |
3415b0e9 | 1279 | if (!pw) |
3347526c | 1280 | return Qnil; |
34a7a267 | 1281 | |
3415b0e9 RS |
1282 | p = (unsigned char *) USER_FULL_NAME; |
1283 | /* Chop off everything after the first comma. */ | |
1284 | q = (unsigned char *) index (p, ','); | |
1285 | full = make_string (p, q ? q - p : strlen (p)); | |
34a7a267 | 1286 | |
3415b0e9 RS |
1287 | #ifdef AMPERSAND_FULL_NAME |
1288 | p = XSTRING (full)->data; | |
1289 | q = (unsigned char *) index (p, '&'); | |
1290 | /* Substitute the login name for the &, upcasing the first character. */ | |
1291 | if (q) | |
1292 | { | |
b0e92acd | 1293 | register unsigned char *r; |
3415b0e9 RS |
1294 | Lisp_Object login; |
1295 | ||
1296 | login = Fuser_login_name (make_number (pw->pw_uid)); | |
1297 | r = (unsigned char *) alloca (strlen (p) + XSTRING (login)->size + 1); | |
1298 | bcopy (p, r, q - p); | |
1299 | r[q - p] = 0; | |
1300 | strcat (r, XSTRING (login)->data); | |
1301 | r[q - p] = UPCASE (r[q - p]); | |
1302 | strcat (r, q + 1); | |
1303 | full = build_string (r); | |
1304 | } | |
1305 | #endif /* AMPERSAND_FULL_NAME */ | |
1306 | ||
1307 | return full; | |
35692fe0 JB |
1308 | } |
1309 | ||
1310 | DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0, | |
7ee72033 MB |
1311 | doc: /* Return the name of the machine you are running on, as a string. */) |
1312 | () | |
35692fe0 JB |
1313 | { |
1314 | return Vsystem_name; | |
1315 | } | |
1316 | ||
ac988277 | 1317 | /* For the benefit of callers who don't want to include lisp.h */ |
acb7cc89 | 1318 | |
ac988277 KH |
1319 | char * |
1320 | get_system_name () | |
1321 | { | |
3d976a9a RS |
1322 | if (STRINGP (Vsystem_name)) |
1323 | return (char *) XSTRING (Vsystem_name)->data; | |
1324 | else | |
1325 | return ""; | |
ac988277 KH |
1326 | } |
1327 | ||
7fd233b3 | 1328 | DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0, |
7ee72033 MB |
1329 | doc: /* Return the process ID of Emacs, as an integer. */) |
1330 | () | |
7fd233b3 RS |
1331 | { |
1332 | return make_number (getpid ()); | |
1333 | } | |
1334 | ||
d940e0e4 | 1335 | DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0, |
7ee72033 | 1336 | doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00. |
a1f17501 PJ |
1337 | The time is returned as a list of three integers. The first has the |
1338 | most significant 16 bits of the seconds, while the second has the | |
1339 | least significant 16 bits. The third integer gives the microsecond | |
1340 | count. | |
1341 | ||
1342 | The microsecond count is zero on systems that do not provide | |
7ee72033 MB |
1343 | resolution finer than a second. */) |
1344 | () | |
d940e0e4 | 1345 | { |
956ace37 JB |
1346 | EMACS_TIME t; |
1347 | Lisp_Object result[3]; | |
1348 | ||
1349 | EMACS_GET_TIME (t); | |
d2fd0445 KH |
1350 | XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff); |
1351 | XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff); | |
1352 | XSETINT (result[2], EMACS_USECS (t)); | |
956ace37 JB |
1353 | |
1354 | return Flist (3, result); | |
d940e0e4 JB |
1355 | } |
1356 | \f | |
1357 | ||
e3120ab5 | 1358 | static int |
34a7a267 | 1359 | lisp_time_argument (specified_time, result, usec) |
e3120ab5 JB |
1360 | Lisp_Object specified_time; |
1361 | time_t *result; | |
34a7a267 | 1362 | int *usec; |
e3120ab5 JB |
1363 | { |
1364 | if (NILP (specified_time)) | |
34a7a267 SS |
1365 | { |
1366 | if (usec) | |
1367 | { | |
1368 | EMACS_TIME t; | |
1369 | ||
c0261b5e | 1370 | EMACS_GET_TIME (t); |
34a7a267 SS |
1371 | *usec = EMACS_USECS (t); |
1372 | *result = EMACS_SECS (t); | |
1373 | return 1; | |
1374 | } | |
1375 | else | |
1376 | return time (result) != -1; | |
1377 | } | |
e3120ab5 JB |
1378 | else |
1379 | { | |
1380 | Lisp_Object high, low; | |
1381 | high = Fcar (specified_time); | |
b7826503 | 1382 | CHECK_NUMBER (high); |
e3120ab5 | 1383 | low = Fcdr (specified_time); |
ae683129 | 1384 | if (CONSP (low)) |
34a7a267 SS |
1385 | { |
1386 | if (usec) | |
1387 | { | |
1388 | Lisp_Object usec_l = Fcdr (low); | |
1389 | if (CONSP (usec_l)) | |
1390 | usec_l = Fcar (usec_l); | |
1391 | if (NILP (usec_l)) | |
1392 | *usec = 0; | |
1393 | else | |
1394 | { | |
b7826503 | 1395 | CHECK_NUMBER (usec_l); |
34a7a267 SS |
1396 | *usec = XINT (usec_l); |
1397 | } | |
1398 | } | |
1399 | low = Fcar (low); | |
1400 | } | |
1401 | else if (usec) | |
1402 | *usec = 0; | |
b7826503 | 1403 | CHECK_NUMBER (low); |
e3120ab5 JB |
1404 | *result = (XINT (high) << 16) + (XINT (low) & 0xffff); |
1405 | return *result >> 16 == XINT (high); | |
1406 | } | |
1407 | } | |
1408 | ||
34a7a267 | 1409 | DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0, |
7ee72033 | 1410 | doc: /* Return the current time, as a float number of seconds since the epoch. |
a1f17501 PJ |
1411 | If an argument is given, it specifies a time to convert to float |
1412 | instead of the current time. The argument should have the forms: | |
1413 | (HIGH . LOW) or (HIGH LOW USEC) or (HIGH LOW . USEC). | |
1414 | Thus, you can use times obtained from `current-time' | |
1415 | and from `file-attributes'. | |
1416 | ||
1417 | WARNING: Since the result is floating point, it may not be exact. | |
7ee72033 MB |
1418 | Do not use this function if precise time stamps are required. */) |
1419 | (specified_time) | |
34a7a267 SS |
1420 | Lisp_Object specified_time; |
1421 | { | |
1422 | time_t sec; | |
1423 | int usec; | |
1424 | ||
1425 | if (! lisp_time_argument (specified_time, &sec, &usec)) | |
1426 | error ("Invalid time specification"); | |
1427 | ||
26fad6e5 | 1428 | return make_float ((sec * 1e6 + usec) / 1e6); |
34a7a267 SS |
1429 | } |
1430 | ||
70ebbe5f PE |
1431 | /* Write information into buffer S of size MAXSIZE, according to the |
1432 | FORMAT of length FORMAT_LEN, using time information taken from *TP. | |
68c45bf0 | 1433 | Default to Universal Time if UT is nonzero, local time otherwise. |
70ebbe5f PE |
1434 | Return the number of bytes written, not including the terminating |
1435 | '\0'. If S is NULL, nothing will be written anywhere; so to | |
1436 | determine how many bytes would be written, use NULL for S and | |
1437 | ((size_t) -1) for MAXSIZE. | |
1438 | ||
68c45bf0 | 1439 | This function behaves like emacs_strftimeu, except it allows null |
70ebbe5f PE |
1440 | bytes in FORMAT. */ |
1441 | static size_t | |
68c45bf0 | 1442 | emacs_memftimeu (s, maxsize, format, format_len, tp, ut) |
70ebbe5f PE |
1443 | char *s; |
1444 | size_t maxsize; | |
1445 | const char *format; | |
1446 | size_t format_len; | |
1447 | const struct tm *tp; | |
68c45bf0 | 1448 | int ut; |
70ebbe5f PE |
1449 | { |
1450 | size_t total = 0; | |
1451 | ||
be09e6e6 PE |
1452 | /* Loop through all the null-terminated strings in the format |
1453 | argument. Normally there's just one null-terminated string, but | |
1454 | there can be arbitrarily many, concatenated together, if the | |
68c45bf0 | 1455 | format contains '\0' bytes. emacs_strftimeu stops at the first |
be09e6e6 | 1456 | '\0' byte so we must invoke it separately for each such string. */ |
70ebbe5f PE |
1457 | for (;;) |
1458 | { | |
1459 | size_t len; | |
1460 | size_t result; | |
1461 | ||
1462 | if (s) | |
1463 | s[0] = '\1'; | |
1464 | ||
68c45bf0 | 1465 | result = emacs_strftimeu (s, maxsize, format, tp, ut); |
70ebbe5f PE |
1466 | |
1467 | if (s) | |
1468 | { | |
1469 | if (result == 0 && s[0] != '\0') | |
1470 | return 0; | |
1471 | s += result + 1; | |
1472 | } | |
1473 | ||
1474 | maxsize -= result + 1; | |
1475 | total += result; | |
1476 | len = strlen (format); | |
1477 | if (len == format_len) | |
1478 | return total; | |
1479 | total++; | |
1480 | format += len + 1; | |
1481 | format_len -= len + 1; | |
1482 | } | |
1483 | } | |
1484 | ||
3efcc98a | 1485 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, |
7ee72033 | 1486 | doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted. |
a1f17501 PJ |
1487 | TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by |
1488 | `current-time' or `file-attributes'. | |
1489 | The third, optional, argument UNIVERSAL, if non-nil, means describe TIME | |
1490 | as Universal Time; nil means describe TIME in the local time zone. | |
1491 | The value is a copy of FORMAT-STRING, but with certain constructs replaced | |
1492 | by text that describes the specified date and time in TIME: | |
1493 | ||
1494 | %Y is the year, %y within the century, %C the century. | |
1495 | %G is the year corresponding to the ISO week, %g within the century. | |
1496 | %m is the numeric month. | |
1497 | %b and %h are the locale's abbreviated month name, %B the full name. | |
1498 | %d is the day of the month, zero-padded, %e is blank-padded. | |
1499 | %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6. | |
1500 | %a is the locale's abbreviated name of the day of week, %A the full name. | |
1501 | %U is the week number starting on Sunday, %W starting on Monday, | |
1502 | %V according to ISO 8601. | |
1503 | %j is the day of the year. | |
1504 | ||
1505 | %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H | |
1506 | only blank-padded, %l is like %I blank-padded. | |
1507 | %p is the locale's equivalent of either AM or PM. | |
1508 | %M is the minute. | |
1509 | %S is the second. | |
1510 | %Z is the time zone name, %z is the numeric form. | |
1511 | %s is the number of seconds since 1970-01-01 00:00:00 +0000. | |
1512 | ||
1513 | %c is the locale's date and time format. | |
1514 | %x is the locale's "preferred" date format. | |
1515 | %D is like "%m/%d/%y". | |
1516 | ||
1517 | %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p". | |
1518 | %X is the locale's "preferred" time format. | |
1519 | ||
1520 | Finally, %n is a newline, %t is a tab, %% is a literal %. | |
1521 | ||
1522 | Certain flags and modifiers are available with some format controls. | |
1523 | The flags are `_', `-', `^' and `#'. For certain characters X, | |
1524 | %_X is like %X, but padded with blanks; %-X is like %X, | |
1525 | ut without padding. %^X is like %X but with all textual | |
1526 | characters up-cased; %#X is like %X but with letter-case of | |
1527 | all textual characters reversed. | |
1528 | %NX (where N stands for an integer) is like %X, | |
1529 | but takes up at least N (a number) positions. | |
1530 | The modifiers are `E' and `O'. For certain characters X, | |
1531 | %EX is a locale's alternative version of %X; | |
1532 | %OX is like %X, but uses the locale's number symbols. | |
1533 | ||
7ee72033 MB |
1534 | For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z". */) |
1535 | (format_string, time, universal) | |
b48382a0 | 1536 | Lisp_Object format_string, time, universal; |
a82d387c RS |
1537 | { |
1538 | time_t value; | |
1539 | int size; | |
177ea5f1 | 1540 | struct tm *tm; |
68c45bf0 | 1541 | int ut = ! NILP (universal); |
a82d387c | 1542 | |
b7826503 | 1543 | CHECK_STRING (format_string); |
a82d387c | 1544 | |
34a7a267 | 1545 | if (! lisp_time_argument (time, &value, NULL)) |
a82d387c RS |
1546 | error ("Invalid time specification"); |
1547 | ||
68c45bf0 PE |
1548 | format_string = code_convert_string_norecord (format_string, |
1549 | Vlocale_coding_system, 1); | |
1550 | ||
a82d387c | 1551 | /* This is probably enough. */ |
dc3620af | 1552 | size = STRING_BYTES (XSTRING (format_string)) * 6 + 50; |
a82d387c | 1553 | |
68c45bf0 | 1554 | tm = ut ? gmtime (&value) : localtime (&value); |
177ea5f1 PE |
1555 | if (! tm) |
1556 | error ("Specified time is not representable"); | |
1557 | ||
ca9c0567 | 1558 | synchronize_system_time_locale (); |
68c45bf0 | 1559 | |
a82d387c RS |
1560 | while (1) |
1561 | { | |
b48382a0 RS |
1562 | char *buf = (char *) alloca (size + 1); |
1563 | int result; | |
1564 | ||
bfbcc5ee | 1565 | buf[0] = '\1'; |
68c45bf0 PE |
1566 | result = emacs_memftimeu (buf, size, XSTRING (format_string)->data, |
1567 | STRING_BYTES (XSTRING (format_string)), | |
1568 | tm, ut); | |
bfbcc5ee | 1569 | if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0')) |
68c45bf0 PE |
1570 | return code_convert_string_norecord (make_string (buf, result), |
1571 | Vlocale_coding_system, 0); | |
b48382a0 RS |
1572 | |
1573 | /* If buffer was too small, make it bigger and try again. */ | |
68c45bf0 PE |
1574 | result = emacs_memftimeu (NULL, (size_t) -1, |
1575 | XSTRING (format_string)->data, | |
1576 | STRING_BYTES (XSTRING (format_string)), | |
1577 | tm, ut); | |
b48382a0 | 1578 | size = result + 1; |
a82d387c RS |
1579 | } |
1580 | } | |
1581 | ||
4691c06d | 1582 | DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0, |
7ee72033 | 1583 | doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE). |
a1f17501 PJ |
1584 | The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED) |
1585 | or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil' | |
1586 | to use the current time. The list has the following nine members: | |
1587 | SEC is an integer between 0 and 60; SEC is 60 for a leap second, which | |
1588 | only some operating systems support. MINUTE is an integer between 0 and 59. | |
1589 | HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31. | |
1590 | MONTH is an integer between 1 and 12. YEAR is an integer indicating the | |
1591 | four-digit year. DOW is the day of week, an integer between 0 and 6, where | |
1592 | 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil. | |
1593 | ZONE is an integer indicating the number of seconds east of Greenwich. | |
7ee72033 MB |
1594 | (Note that Common Lisp has different meanings for DOW and ZONE.) */) |
1595 | (specified_time) | |
4691c06d RS |
1596 | Lisp_Object specified_time; |
1597 | { | |
1598 | time_t time_spec; | |
3c887943 | 1599 | struct tm save_tm; |
4691c06d RS |
1600 | struct tm *decoded_time; |
1601 | Lisp_Object list_args[9]; | |
34a7a267 SS |
1602 | |
1603 | if (! lisp_time_argument (specified_time, &time_spec, NULL)) | |
4691c06d RS |
1604 | error ("Invalid time specification"); |
1605 | ||
1606 | decoded_time = localtime (&time_spec); | |
177ea5f1 PE |
1607 | if (! decoded_time) |
1608 | error ("Specified time is not representable"); | |
3c887943 KH |
1609 | XSETFASTINT (list_args[0], decoded_time->tm_sec); |
1610 | XSETFASTINT (list_args[1], decoded_time->tm_min); | |
1611 | XSETFASTINT (list_args[2], decoded_time->tm_hour); | |
1612 | XSETFASTINT (list_args[3], decoded_time->tm_mday); | |
1613 | XSETFASTINT (list_args[4], decoded_time->tm_mon + 1); | |
236ebf35 | 1614 | XSETINT (list_args[5], decoded_time->tm_year + 1900); |
3c887943 | 1615 | XSETFASTINT (list_args[6], decoded_time->tm_wday); |
4691c06d | 1616 | list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil; |
3c887943 KH |
1617 | |
1618 | /* Make a copy, in case gmtime modifies the struct. */ | |
1619 | save_tm = *decoded_time; | |
1620 | decoded_time = gmtime (&time_spec); | |
1621 | if (decoded_time == 0) | |
1622 | list_args[8] = Qnil; | |
1623 | else | |
94751666 | 1624 | XSETINT (list_args[8], tm_diff (&save_tm, decoded_time)); |
4691c06d RS |
1625 | return Flist (9, list_args); |
1626 | } | |
1627 | ||
6ee9061c | 1628 | DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, |
7ee72033 | 1629 | doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time. |
a1f17501 PJ |
1630 | This is the reverse operation of `decode-time', which see. |
1631 | ZONE defaults to the current time zone rule. This can | |
1632 | be a string or t (as from `set-time-zone-rule'), or it can be a list | |
b57c2708 | 1633 | \(as from `current-time-zone') or an integer (as from `decode-time') |
a1f17501 PJ |
1634 | applied without consideration for daylight savings time. |
1635 | ||
1636 | You can pass more than 7 arguments; then the first six arguments | |
1637 | are used as SECOND through YEAR, and the *last* argument is used as ZONE. | |
1638 | The intervening arguments are ignored. | |
1639 | This feature lets (apply 'encode-time (decode-time ...)) work. | |
1640 | ||
1641 | Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed; | |
1642 | for example, a DAY of 0 means the day preceding the given month. | |
1643 | Year numbers less than 100 are treated just like other year numbers. | |
4bfbe194 MB |
1644 | If you want them to stand for years in this century, you must do that yourself. |
1645 | ||
1646 | usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */) | |
7ee72033 | 1647 | (nargs, args) |
6ee9061c RS |
1648 | int nargs; |
1649 | register Lisp_Object *args; | |
cce7b8a0 | 1650 | { |
1b8fa736 | 1651 | time_t time; |
c59b5089 | 1652 | struct tm tm; |
60653898 | 1653 | Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil); |
6ee9061c | 1654 | |
b7826503 PJ |
1655 | CHECK_NUMBER (args[0]); /* second */ |
1656 | CHECK_NUMBER (args[1]); /* minute */ | |
1657 | CHECK_NUMBER (args[2]); /* hour */ | |
1658 | CHECK_NUMBER (args[3]); /* day */ | |
1659 | CHECK_NUMBER (args[4]); /* month */ | |
1660 | CHECK_NUMBER (args[5]); /* year */ | |
6ee9061c RS |
1661 | |
1662 | tm.tm_sec = XINT (args[0]); | |
1663 | tm.tm_min = XINT (args[1]); | |
1664 | tm.tm_hour = XINT (args[2]); | |
1665 | tm.tm_mday = XINT (args[3]); | |
1666 | tm.tm_mon = XINT (args[4]) - 1; | |
1667 | tm.tm_year = XINT (args[5]) - 1900; | |
c59b5089 PE |
1668 | tm.tm_isdst = -1; |
1669 | ||
1670 | if (CONSP (zone)) | |
1671 | zone = Fcar (zone); | |
1b8fa736 | 1672 | if (NILP (zone)) |
c59b5089 PE |
1673 | time = mktime (&tm); |
1674 | else | |
1b8fa736 | 1675 | { |
c59b5089 PE |
1676 | char tzbuf[100]; |
1677 | char *tzstring; | |
1678 | char **oldenv = environ, **newenv; | |
34a7a267 | 1679 | |
2e34157c | 1680 | if (EQ (zone, Qt)) |
085e9fcb EN |
1681 | tzstring = "UTC0"; |
1682 | else if (STRINGP (zone)) | |
4d4c1514 | 1683 | tzstring = (char *) XSTRING (zone)->data; |
c59b5089 | 1684 | else if (INTEGERP (zone)) |
1b8fa736 | 1685 | { |
c59b5089 PE |
1686 | int abszone = abs (XINT (zone)); |
1687 | sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0), | |
1688 | abszone / (60*60), (abszone/60) % 60, abszone % 60); | |
1689 | tzstring = tzbuf; | |
1b8fa736 | 1690 | } |
c59b5089 PE |
1691 | else |
1692 | error ("Invalid time zone specification"); | |
1693 | ||
34a7a267 | 1694 | /* Set TZ before calling mktime; merely adjusting mktime's returned |
c59b5089 PE |
1695 | value doesn't suffice, since that would mishandle leap seconds. */ |
1696 | set_time_zone_rule (tzstring); | |
1697 | ||
1698 | time = mktime (&tm); | |
1699 | ||
1700 | /* Restore TZ to previous value. */ | |
1701 | newenv = environ; | |
1702 | environ = oldenv; | |
c0efcacf | 1703 | xfree (newenv); |
c59b5089 PE |
1704 | #ifdef LOCALTIME_CACHE |
1705 | tzset (); | |
1706 | #endif | |
1b8fa736 | 1707 | } |
1b8fa736 | 1708 | |
c59b5089 PE |
1709 | if (time == (time_t) -1) |
1710 | error ("Specified time is not representable"); | |
1711 | ||
1712 | return make_time (time); | |
cce7b8a0 RS |
1713 | } |
1714 | ||
2148f2b4 | 1715 | DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0, |
7ee72033 | 1716 | doc: /* Return the current time, as a human-readable string. |
a1f17501 PJ |
1717 | Programs can use this function to decode a time, |
1718 | since the number of columns in each field is fixed. | |
1719 | The format is `Sun Sep 16 01:03:52 1973'. | |
1720 | However, see also the functions `decode-time' and `format-time-string' | |
1721 | which provide a much more powerful and general facility. | |
1722 | ||
1723 | If an argument is given, it specifies a time to format | |
1724 | instead of the current time. The argument should have the form: | |
1725 | (HIGH . LOW) | |
1726 | or the form: | |
1727 | (HIGH LOW . IGNORED). | |
1728 | Thus, you can use times obtained from `current-time' | |
7ee72033 MB |
1729 | and from `file-attributes'. */) |
1730 | (specified_time) | |
2148f2b4 RS |
1731 | Lisp_Object specified_time; |
1732 | { | |
e3120ab5 | 1733 | time_t value; |
35692fe0 | 1734 | char buf[30]; |
2148f2b4 RS |
1735 | register char *tem; |
1736 | ||
34a7a267 | 1737 | if (! lisp_time_argument (specified_time, &value, NULL)) |
e3120ab5 | 1738 | value = -1; |
2148f2b4 | 1739 | tem = (char *) ctime (&value); |
35692fe0 JB |
1740 | |
1741 | strncpy (buf, tem, 24); | |
1742 | buf[24] = 0; | |
1743 | ||
1744 | return build_string (buf); | |
1745 | } | |
c2662aea | 1746 | |
94751666 | 1747 | #define TM_YEAR_BASE 1900 |
e3120ab5 | 1748 | |
94751666 PE |
1749 | /* Yield A - B, measured in seconds. |
1750 | This function is copied from the GNU C Library. */ | |
1751 | static int | |
1752 | tm_diff (a, b) | |
e3120ab5 JB |
1753 | struct tm *a, *b; |
1754 | { | |
94751666 PE |
1755 | /* Compute intervening leap days correctly even if year is negative. |
1756 | Take care to avoid int overflow in leap day calculations, | |
1757 | but it's OK to assume that A and B are close to each other. */ | |
1758 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); | |
1759 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); | |
1760 | int a100 = a4 / 25 - (a4 % 25 < 0); | |
1761 | int b100 = b4 / 25 - (b4 % 25 < 0); | |
1762 | int a400 = a100 >> 2; | |
1763 | int b400 = b100 >> 2; | |
1764 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); | |
1765 | int years = a->tm_year - b->tm_year; | |
1766 | int days = (365 * years + intervening_leap_days | |
1767 | + (a->tm_yday - b->tm_yday)); | |
1768 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) | |
1769 | + (a->tm_min - b->tm_min)) | |
8e718b4e | 1770 | + (a->tm_sec - b->tm_sec)); |
e3120ab5 JB |
1771 | } |
1772 | ||
1773 | DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0, | |
7ee72033 | 1774 | doc: /* Return the offset and name for the local time zone. |
a1f17501 PJ |
1775 | This returns a list of the form (OFFSET NAME). |
1776 | OFFSET is an integer number of seconds ahead of UTC (east of Greenwich). | |
1777 | A negative value means west of Greenwich. | |
1778 | NAME is a string giving the name of the time zone. | |
1779 | If an argument is given, it specifies when the time zone offset is determined | |
1780 | instead of using the current time. The argument should have the form: | |
1781 | (HIGH . LOW) | |
1782 | or the form: | |
1783 | (HIGH LOW . IGNORED). | |
1784 | Thus, you can use times obtained from `current-time' | |
1785 | and from `file-attributes'. | |
1786 | ||
1787 | Some operating systems cannot provide all this information to Emacs; | |
1788 | in this case, `current-time-zone' returns a list containing nil for | |
7ee72033 MB |
1789 | the data it can't find. */) |
1790 | (specified_time) | |
e3120ab5 | 1791 | Lisp_Object specified_time; |
c2662aea | 1792 | { |
e3120ab5 JB |
1793 | time_t value; |
1794 | struct tm *t; | |
177ea5f1 | 1795 | struct tm gmt; |
c2662aea | 1796 | |
34a7a267 | 1797 | if (lisp_time_argument (specified_time, &value, NULL) |
177ea5f1 PE |
1798 | && (t = gmtime (&value)) != 0 |
1799 | && (gmt = *t, t = localtime (&value)) != 0) | |
e3120ab5 | 1800 | { |
177ea5f1 PE |
1801 | int offset = tm_diff (t, &gmt); |
1802 | char *s = 0; | |
1803 | char buf[6]; | |
e3120ab5 JB |
1804 | #ifdef HAVE_TM_ZONE |
1805 | if (t->tm_zone) | |
5fd4de15 | 1806 | s = (char *)t->tm_zone; |
a7971c39 RS |
1807 | #else /* not HAVE_TM_ZONE */ |
1808 | #ifdef HAVE_TZNAME | |
1809 | if (t->tm_isdst == 0 || t->tm_isdst == 1) | |
1810 | s = tzname[t->tm_isdst]; | |
c2662aea | 1811 | #endif |
a7971c39 | 1812 | #endif /* not HAVE_TM_ZONE */ |
cda0f4da GM |
1813 | |
1814 | #if defined HAVE_TM_ZONE || defined HAVE_TZNAME | |
1815 | if (s) | |
1816 | { | |
1817 | /* On Japanese w32, we can get a Japanese string as time | |
1818 | zone name. Don't accept that. */ | |
1819 | char *p; | |
9fac18bf | 1820 | for (p = s; *p && (isalnum ((unsigned char)*p) || *p == ' '); ++p) |
cda0f4da GM |
1821 | ; |
1822 | if (p == s || *p) | |
1823 | s = NULL; | |
1824 | } | |
1825 | #endif | |
1826 | ||
e3120ab5 JB |
1827 | if (!s) |
1828 | { | |
1829 | /* No local time zone name is available; use "+-NNNN" instead. */ | |
00fc94d0 | 1830 | int am = (offset < 0 ? -offset : offset) / 60; |
e3120ab5 JB |
1831 | sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60); |
1832 | s = buf; | |
1833 | } | |
1834 | return Fcons (make_number (offset), Fcons (build_string (s), Qnil)); | |
1835 | } | |
1836 | else | |
09dbcf71 | 1837 | return Fmake_list (make_number (2), Qnil); |
c2662aea JB |
1838 | } |
1839 | ||
260e2e2a KH |
1840 | /* This holds the value of `environ' produced by the previous |
1841 | call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule | |
1842 | has never been called. */ | |
1843 | static char **environbuf; | |
1844 | ||
143cb9a9 | 1845 | DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0, |
7ee72033 | 1846 | doc: /* Set the local time zone using TZ, a string specifying a time zone rule. |
a1f17501 | 1847 | If TZ is nil, use implementation-defined default time zone information. |
7ee72033 MB |
1848 | If TZ is t, use Universal Time. */) |
1849 | (tz) | |
143cb9a9 RS |
1850 | Lisp_Object tz; |
1851 | { | |
143cb9a9 RS |
1852 | char *tzstring; |
1853 | ||
1854 | if (NILP (tz)) | |
1855 | tzstring = 0; | |
2e34157c | 1856 | else if (EQ (tz, Qt)) |
085e9fcb | 1857 | tzstring = "UTC0"; |
143cb9a9 RS |
1858 | else |
1859 | { | |
b7826503 | 1860 | CHECK_STRING (tz); |
4d4c1514 | 1861 | tzstring = (char *) XSTRING (tz)->data; |
143cb9a9 RS |
1862 | } |
1863 | ||
c59b5089 PE |
1864 | set_time_zone_rule (tzstring); |
1865 | if (environbuf) | |
1866 | free (environbuf); | |
1867 | environbuf = environ; | |
1868 | ||
1869 | return Qnil; | |
1870 | } | |
1871 | ||
e0bf9faf PE |
1872 | #ifdef LOCALTIME_CACHE |
1873 | ||
1874 | /* These two values are known to load tz files in buggy implementations, | |
1875 | i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2. | |
1155c453 | 1876 | Their values shouldn't matter in non-buggy implementations. |
34a7a267 | 1877 | We don't use string literals for these strings, |
1155c453 RS |
1878 | since if a string in the environment is in readonly |
1879 | storage, it runs afoul of bugs in SVR4 and Solaris 2.3. | |
1880 | See Sun bugs 1113095 and 1114114, ``Timezone routines | |
1881 | improperly modify environment''. */ | |
1882 | ||
e0bf9faf PE |
1883 | static char set_time_zone_rule_tz1[] = "TZ=GMT+0"; |
1884 | static char set_time_zone_rule_tz2[] = "TZ=GMT+1"; | |
1885 | ||
1886 | #endif | |
1155c453 | 1887 | |
c59b5089 PE |
1888 | /* Set the local time zone rule to TZSTRING. |
1889 | This allocates memory into `environ', which it is the caller's | |
1890 | responsibility to free. */ | |
acb7cc89 | 1891 | |
a92ae0ce | 1892 | void |
c59b5089 PE |
1893 | set_time_zone_rule (tzstring) |
1894 | char *tzstring; | |
1895 | { | |
1896 | int envptrs; | |
1897 | char **from, **to, **newenv; | |
1898 | ||
aafe5147 | 1899 | /* Make the ENVIRON vector longer with room for TZSTRING. */ |
143cb9a9 RS |
1900 | for (from = environ; *from; from++) |
1901 | continue; | |
1902 | envptrs = from - environ + 2; | |
1903 | newenv = to = (char **) xmalloc (envptrs * sizeof (char *) | |
1904 | + (tzstring ? strlen (tzstring) + 4 : 0)); | |
aafe5147 RS |
1905 | |
1906 | /* Add TZSTRING to the end of environ, as a value for TZ. */ | |
143cb9a9 RS |
1907 | if (tzstring) |
1908 | { | |
1909 | char *t = (char *) (to + envptrs); | |
1910 | strcpy (t, "TZ="); | |
1911 | strcat (t, tzstring); | |
1912 | *to++ = t; | |
1913 | } | |
1914 | ||
aafe5147 RS |
1915 | /* Copy the old environ vector elements into NEWENV, |
1916 | but don't copy the TZ variable. | |
1917 | So we have only one definition of TZ, which came from TZSTRING. */ | |
143cb9a9 RS |
1918 | for (from = environ; *from; from++) |
1919 | if (strncmp (*from, "TZ=", 3) != 0) | |
1920 | *to++ = *from; | |
1921 | *to = 0; | |
1922 | ||
1923 | environ = newenv; | |
143cb9a9 | 1924 | |
aafe5147 RS |
1925 | /* If we do have a TZSTRING, NEWENV points to the vector slot where |
1926 | the TZ variable is stored. If we do not have a TZSTRING, | |
1927 | TO points to the vector slot which has the terminating null. */ | |
1928 | ||
143cb9a9 | 1929 | #ifdef LOCALTIME_CACHE |
aafe5147 RS |
1930 | { |
1931 | /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like | |
1932 | "US/Pacific" that loads a tz file, then changes to a value like | |
1933 | "XXX0" that does not load a tz file, and then changes back to | |
1934 | its original value, the last change is (incorrectly) ignored. | |
1935 | Also, if TZ changes twice in succession to values that do | |
1936 | not load a tz file, tzset can dump core (see Sun bug#1225179). | |
1937 | The following code works around these bugs. */ | |
1938 | ||
aafe5147 RS |
1939 | if (tzstring) |
1940 | { | |
1941 | /* Temporarily set TZ to a value that loads a tz file | |
1942 | and that differs from tzstring. */ | |
1943 | char *tz = *newenv; | |
1155c453 RS |
1944 | *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0 |
1945 | ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1); | |
aafe5147 RS |
1946 | tzset (); |
1947 | *newenv = tz; | |
1948 | } | |
1949 | else | |
1950 | { | |
1951 | /* The implied tzstring is unknown, so temporarily set TZ to | |
1952 | two different values that each load a tz file. */ | |
1155c453 | 1953 | *to = set_time_zone_rule_tz1; |
aafe5147 RS |
1954 | to[1] = 0; |
1955 | tzset (); | |
1155c453 | 1956 | *to = set_time_zone_rule_tz2; |
aafe5147 RS |
1957 | tzset (); |
1958 | *to = 0; | |
1959 | } | |
1960 | ||
1961 | /* Now TZ has the desired value, and tzset can be invoked safely. */ | |
1962 | } | |
1963 | ||
143cb9a9 RS |
1964 | tzset (); |
1965 | #endif | |
143cb9a9 | 1966 | } |
35692fe0 | 1967 | \f |
fb8106e8 KH |
1968 | /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC |
1969 | (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a | |
1970 | type of object is Lisp_String). INHERIT is passed to | |
1971 | INSERT_FROM_STRING_FUNC as the last argument. */ | |
1972 | ||
acb7cc89 | 1973 | static void |
fb8106e8 KH |
1974 | general_insert_function (insert_func, insert_from_string_func, |
1975 | inherit, nargs, args) | |
d11ba98c | 1976 | void (*insert_func) P_ ((unsigned char *, int)); |
1f24f4fd | 1977 | void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int)); |
fb8106e8 KH |
1978 | int inherit, nargs; |
1979 | register Lisp_Object *args; | |
1980 | { | |
1981 | register int argnum; | |
1982 | register Lisp_Object val; | |
1983 | ||
1984 | for (argnum = 0; argnum < nargs; argnum++) | |
1985 | { | |
1986 | val = args[argnum]; | |
1987 | retry: | |
1988 | if (INTEGERP (val)) | |
1989 | { | |
d5c2c403 | 1990 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
fb8106e8 KH |
1991 | int len; |
1992 | ||
1993 | if (!NILP (current_buffer->enable_multibyte_characters)) | |
d5c2c403 | 1994 | len = CHAR_STRING (XFASTINT (val), str); |
fb8106e8 | 1995 | else |
13c148b8 | 1996 | { |
d5c2c403 KH |
1997 | str[0] = (SINGLE_BYTE_CHAR_P (XINT (val)) |
1998 | ? XINT (val) | |
1999 | : multibyte_char_to_unibyte (XINT (val), Qnil)); | |
13c148b8 KH |
2000 | len = 1; |
2001 | } | |
fb8106e8 KH |
2002 | (*insert_func) (str, len); |
2003 | } | |
2004 | else if (STRINGP (val)) | |
2005 | { | |
1f24f4fd RS |
2006 | (*insert_from_string_func) (val, 0, 0, |
2007 | XSTRING (val)->size, | |
dc3620af | 2008 | STRING_BYTES (XSTRING (val)), |
1f24f4fd | 2009 | inherit); |
fb8106e8 KH |
2010 | } |
2011 | else | |
2012 | { | |
2013 | val = wrong_type_argument (Qchar_or_string_p, val); | |
2014 | goto retry; | |
2015 | } | |
2016 | } | |
2017 | } | |
2018 | ||
35692fe0 JB |
2019 | void |
2020 | insert1 (arg) | |
2021 | Lisp_Object arg; | |
2022 | { | |
2023 | Finsert (1, &arg); | |
2024 | } | |
2025 | ||
52b14ac0 JB |
2026 | |
2027 | /* Callers passing one argument to Finsert need not gcpro the | |
2028 | argument "array", since the only element of the array will | |
2029 | not be used after calling insert or insert_from_string, so | |
2030 | we don't care if it gets trashed. */ | |
2031 | ||
35692fe0 | 2032 | DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0, |
7ee72033 | 2033 | doc: /* Insert the arguments, either strings or characters, at point. |
a1f17501 PJ |
2034 | Point and before-insertion markers move forward to end up |
2035 | after the inserted text. | |
2036 | Any other markers at the point of insertion remain before the text. | |
2037 | ||
2038 | If the current buffer is multibyte, unibyte strings are converted | |
2039 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2040 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2041 | to unibyte for insertion. |
2042 | ||
2043 | usage: (insert &rest ARGS) */) | |
7ee72033 | 2044 | (nargs, args) |
35692fe0 JB |
2045 | int nargs; |
2046 | register Lisp_Object *args; | |
2047 | { | |
fb8106e8 | 2048 | general_insert_function (insert, insert_from_string, 0, nargs, args); |
be91036a RS |
2049 | return Qnil; |
2050 | } | |
2051 | ||
2052 | DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit, | |
2053 | 0, MANY, 0, | |
7ee72033 | 2054 | doc: /* Insert the arguments at point, inheriting properties from adjoining text. |
a1f17501 PJ |
2055 | Point and before-insertion markers move forward to end up |
2056 | after the inserted text. | |
2057 | Any other markers at the point of insertion remain before the text. | |
2058 | ||
2059 | If the current buffer is multibyte, unibyte strings are converted | |
2060 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2061 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2062 | to unibyte for insertion. |
2063 | ||
2064 | usage: (insert-and-inherit &rest ARGS) */) | |
7ee72033 | 2065 | (nargs, args) |
be91036a RS |
2066 | int nargs; |
2067 | register Lisp_Object *args; | |
2068 | { | |
fb8106e8 KH |
2069 | general_insert_function (insert_and_inherit, insert_from_string, 1, |
2070 | nargs, args); | |
35692fe0 JB |
2071 | return Qnil; |
2072 | } | |
2073 | ||
2074 | DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0, | |
7ee72033 | 2075 | doc: /* Insert strings or characters at point, relocating markers after the text. |
a1f17501 PJ |
2076 | Point and markers move forward to end up after the inserted text. |
2077 | ||
2078 | If the current buffer is multibyte, unibyte strings are converted | |
2079 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2080 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2081 | to unibyte for insertion. |
2082 | ||
2083 | usage: (insert-before-markers &rest ARGS) */) | |
7ee72033 | 2084 | (nargs, args) |
35692fe0 JB |
2085 | int nargs; |
2086 | register Lisp_Object *args; | |
2087 | { | |
fb8106e8 KH |
2088 | general_insert_function (insert_before_markers, |
2089 | insert_from_string_before_markers, 0, | |
2090 | nargs, args); | |
be91036a RS |
2091 | return Qnil; |
2092 | } | |
2093 | ||
a0d76c27 EN |
2094 | DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers, |
2095 | Sinsert_and_inherit_before_markers, 0, MANY, 0, | |
7ee72033 | 2096 | doc: /* Insert text at point, relocating markers and inheriting properties. |
a1f17501 PJ |
2097 | Point and markers move forward to end up after the inserted text. |
2098 | ||
2099 | If the current buffer is multibyte, unibyte strings are converted | |
2100 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2101 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2102 | to unibyte for insertion. |
2103 | ||
2104 | usage: (insert-before-markers-and-inherit &rest ARGS) */) | |
7ee72033 | 2105 | (nargs, args) |
be91036a RS |
2106 | int nargs; |
2107 | register Lisp_Object *args; | |
2108 | { | |
fb8106e8 KH |
2109 | general_insert_function (insert_before_markers_and_inherit, |
2110 | insert_from_string_before_markers, 1, | |
2111 | nargs, args); | |
35692fe0 JB |
2112 | return Qnil; |
2113 | } | |
2114 | \f | |
e2eeabbb | 2115 | DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0, |
7ee72033 | 2116 | doc: /* Insert COUNT (second arg) copies of CHARACTER (first arg). |
a1f17501 PJ |
2117 | Both arguments are required. |
2118 | Point, and before-insertion markers, are relocated as in the function `insert'. | |
2119 | The optional third arg INHERIT, if non-nil, says to inherit text properties | |
7ee72033 MB |
2120 | from adjoining text, if those properties are sticky. */) |
2121 | (character, count, inherit) | |
2591ec64 | 2122 | Lisp_Object character, count, inherit; |
35692fe0 JB |
2123 | { |
2124 | register unsigned char *string; | |
2125 | register int strlen; | |
2126 | register int i, n; | |
fb8106e8 | 2127 | int len; |
d5c2c403 | 2128 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
35692fe0 | 2129 | |
b7826503 PJ |
2130 | CHECK_NUMBER (character); |
2131 | CHECK_NUMBER (count); | |
35692fe0 | 2132 | |
fb8106e8 | 2133 | if (!NILP (current_buffer->enable_multibyte_characters)) |
d5c2c403 | 2134 | len = CHAR_STRING (XFASTINT (character), str); |
fb8106e8 | 2135 | else |
d5c2c403 | 2136 | str[0] = XFASTINT (character), len = 1; |
fb8106e8 | 2137 | n = XINT (count) * len; |
35692fe0 JB |
2138 | if (n <= 0) |
2139 | return Qnil; | |
fb8106e8 | 2140 | strlen = min (n, 256 * len); |
35692fe0 JB |
2141 | string = (unsigned char *) alloca (strlen); |
2142 | for (i = 0; i < strlen; i++) | |
fb8106e8 | 2143 | string[i] = str[i % len]; |
35692fe0 JB |
2144 | while (n >= strlen) |
2145 | { | |
54e42e2d | 2146 | QUIT; |
e2eeabbb RS |
2147 | if (!NILP (inherit)) |
2148 | insert_and_inherit (string, strlen); | |
2149 | else | |
2150 | insert (string, strlen); | |
35692fe0 JB |
2151 | n -= strlen; |
2152 | } | |
2153 | if (n > 0) | |
83951f1e KH |
2154 | { |
2155 | if (!NILP (inherit)) | |
2156 | insert_and_inherit (string, n); | |
2157 | else | |
2158 | insert (string, n); | |
2159 | } | |
35692fe0 JB |
2160 | return Qnil; |
2161 | } | |
2162 | ||
2163 | \f | |
ffd56f97 JB |
2164 | /* Making strings from buffer contents. */ |
2165 | ||
2166 | /* Return a Lisp_String containing the text of the current buffer from | |
74d6d8c5 | 2167 | START to END. If text properties are in use and the current buffer |
eb8c3be9 | 2168 | has properties in the range specified, the resulting string will also |
260e2e2a | 2169 | have them, if PROPS is nonzero. |
ffd56f97 JB |
2170 | |
2171 | We don't want to use plain old make_string here, because it calls | |
2172 | make_uninit_string, which can cause the buffer arena to be | |
2173 | compacted. make_string has no way of knowing that the data has | |
2174 | been moved, and thus copies the wrong data into the string. This | |
2175 | doesn't effect most of the other users of make_string, so it should | |
2176 | be left as is. But we should use this function when conjuring | |
2177 | buffer substrings. */ | |
74d6d8c5 | 2178 | |
ffd56f97 | 2179 | Lisp_Object |
260e2e2a | 2180 | make_buffer_string (start, end, props) |
ffd56f97 | 2181 | int start, end; |
260e2e2a | 2182 | int props; |
ffd56f97 | 2183 | { |
ec1c14f6 RS |
2184 | int start_byte = CHAR_TO_BYTE (start); |
2185 | int end_byte = CHAR_TO_BYTE (end); | |
ffd56f97 | 2186 | |
88441c8e RS |
2187 | return make_buffer_string_both (start, start_byte, end, end_byte, props); |
2188 | } | |
2189 | ||
2190 | /* Return a Lisp_String containing the text of the current buffer from | |
2191 | START / START_BYTE to END / END_BYTE. | |
2192 | ||
2193 | If text properties are in use and the current buffer | |
2194 | has properties in the range specified, the resulting string will also | |
2195 | have them, if PROPS is nonzero. | |
2196 | ||
2197 | We don't want to use plain old make_string here, because it calls | |
2198 | make_uninit_string, which can cause the buffer arena to be | |
2199 | compacted. make_string has no way of knowing that the data has | |
2200 | been moved, and thus copies the wrong data into the string. This | |
2201 | doesn't effect most of the other users of make_string, so it should | |
2202 | be left as is. But we should use this function when conjuring | |
2203 | buffer substrings. */ | |
2204 | ||
2205 | Lisp_Object | |
2206 | make_buffer_string_both (start, start_byte, end, end_byte, props) | |
2207 | int start, start_byte, end, end_byte; | |
2208 | int props; | |
2209 | { | |
2210 | Lisp_Object result, tem, tem1; | |
2211 | ||
ffd56f97 JB |
2212 | if (start < GPT && GPT < end) |
2213 | move_gap (start); | |
2214 | ||
5f75e666 RS |
2215 | if (! NILP (current_buffer->enable_multibyte_characters)) |
2216 | result = make_uninit_multibyte_string (end - start, end_byte - start_byte); | |
2217 | else | |
2218 | result = make_uninit_string (end - start); | |
ec1c14f6 RS |
2219 | bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data, |
2220 | end_byte - start_byte); | |
ffd56f97 | 2221 | |
260e2e2a | 2222 | /* If desired, update and copy the text properties. */ |
260e2e2a KH |
2223 | if (props) |
2224 | { | |
2225 | update_buffer_properties (start, end); | |
2226 | ||
2227 | tem = Fnext_property_change (make_number (start), Qnil, make_number (end)); | |
2228 | tem1 = Ftext_properties_at (make_number (start), Qnil); | |
2229 | ||
2230 | if (XINT (tem) != end || !NILP (tem1)) | |
ec1c14f6 RS |
2231 | copy_intervals_to_string (result, current_buffer, start, |
2232 | end - start); | |
260e2e2a | 2233 | } |
74d6d8c5 | 2234 | |
ffd56f97 JB |
2235 | return result; |
2236 | } | |
35692fe0 | 2237 | |
260e2e2a KH |
2238 | /* Call Vbuffer_access_fontify_functions for the range START ... END |
2239 | in the current buffer, if necessary. */ | |
2240 | ||
2241 | static void | |
2242 | update_buffer_properties (start, end) | |
2243 | int start, end; | |
2244 | { | |
260e2e2a KH |
2245 | /* If this buffer has some access functions, |
2246 | call them, specifying the range of the buffer being accessed. */ | |
2247 | if (!NILP (Vbuffer_access_fontify_functions)) | |
2248 | { | |
2249 | Lisp_Object args[3]; | |
2250 | Lisp_Object tem; | |
2251 | ||
2252 | args[0] = Qbuffer_access_fontify_functions; | |
2253 | XSETINT (args[1], start); | |
2254 | XSETINT (args[2], end); | |
2255 | ||
2256 | /* But don't call them if we can tell that the work | |
2257 | has already been done. */ | |
2258 | if (!NILP (Vbuffer_access_fontified_property)) | |
2259 | { | |
2260 | tem = Ftext_property_any (args[1], args[2], | |
2261 | Vbuffer_access_fontified_property, | |
2262 | Qnil, Qnil); | |
2263 | if (! NILP (tem)) | |
ced1d19a | 2264 | Frun_hook_with_args (3, args); |
260e2e2a KH |
2265 | } |
2266 | else | |
ced1d19a | 2267 | Frun_hook_with_args (3, args); |
260e2e2a | 2268 | } |
260e2e2a KH |
2269 | } |
2270 | ||
35692fe0 | 2271 | DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0, |
7ee72033 | 2272 | doc: /* Return the contents of part of the current buffer as a string. |
a1f17501 PJ |
2273 | The two arguments START and END are character positions; |
2274 | they can be in either order. | |
2275 | The string returned is multibyte if the buffer is multibyte. | |
2276 | ||
2277 | This function copies the text properties of that part of the buffer | |
2278 | into the result string; if you don't want the text properties, | |
7ee72033 MB |
2279 | use `buffer-substring-no-properties' instead. */) |
2280 | (start, end) | |
2591ec64 | 2281 | Lisp_Object start, end; |
35692fe0 | 2282 | { |
2591ec64 | 2283 | register int b, e; |
35692fe0 | 2284 | |
2591ec64 EN |
2285 | validate_region (&start, &end); |
2286 | b = XINT (start); | |
2287 | e = XINT (end); | |
35692fe0 | 2288 | |
2591ec64 | 2289 | return make_buffer_string (b, e, 1); |
260e2e2a KH |
2290 | } |
2291 | ||
2292 | DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties, | |
2293 | Sbuffer_substring_no_properties, 2, 2, 0, | |
7ee72033 | 2294 | doc: /* Return the characters of part of the buffer, without the text properties. |
a1f17501 | 2295 | The two arguments START and END are character positions; |
7ee72033 MB |
2296 | they can be in either order. */) |
2297 | (start, end) | |
2591ec64 | 2298 | Lisp_Object start, end; |
260e2e2a | 2299 | { |
2591ec64 | 2300 | register int b, e; |
260e2e2a | 2301 | |
2591ec64 EN |
2302 | validate_region (&start, &end); |
2303 | b = XINT (start); | |
2304 | e = XINT (end); | |
260e2e2a | 2305 | |
2591ec64 | 2306 | return make_buffer_string (b, e, 0); |
35692fe0 JB |
2307 | } |
2308 | ||
2309 | DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0, | |
7ee72033 | 2310 | doc: /* Return the contents of the current buffer as a string. |
a1f17501 | 2311 | If narrowing is in effect, this function returns only the visible part |
7ee72033 MB |
2312 | of the buffer. */) |
2313 | () | |
35692fe0 | 2314 | { |
0daf6e8d | 2315 | return make_buffer_string (BEGV, ZV, 1); |
35692fe0 JB |
2316 | } |
2317 | ||
2318 | DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring, | |
deb8e082 | 2319 | 1, 3, 0, |
7ee72033 | 2320 | doc: /* Insert before point a substring of the contents of buffer BUFFER. |
a1f17501 PJ |
2321 | BUFFER may be a buffer or a buffer name. |
2322 | Arguments START and END are character numbers specifying the substring. | |
7ee72033 MB |
2323 | They default to the beginning and the end of BUFFER. */) |
2324 | (buf, start, end) | |
2591ec64 | 2325 | Lisp_Object buf, start, end; |
35692fe0 | 2326 | { |
2591ec64 | 2327 | register int b, e, temp; |
260e2e2a | 2328 | register struct buffer *bp, *obuf; |
3fff2dfa | 2329 | Lisp_Object buffer; |
35692fe0 | 2330 | |
3fff2dfa RS |
2331 | buffer = Fget_buffer (buf); |
2332 | if (NILP (buffer)) | |
2333 | nsberror (buf); | |
2334 | bp = XBUFFER (buffer); | |
93b62e82 KH |
2335 | if (NILP (bp->name)) |
2336 | error ("Selecting deleted buffer"); | |
35692fe0 | 2337 | |
2591ec64 EN |
2338 | if (NILP (start)) |
2339 | b = BUF_BEGV (bp); | |
35692fe0 JB |
2340 | else |
2341 | { | |
b7826503 | 2342 | CHECK_NUMBER_COERCE_MARKER (start); |
2591ec64 | 2343 | b = XINT (start); |
35692fe0 | 2344 | } |
2591ec64 EN |
2345 | if (NILP (end)) |
2346 | e = BUF_ZV (bp); | |
35692fe0 JB |
2347 | else |
2348 | { | |
b7826503 | 2349 | CHECK_NUMBER_COERCE_MARKER (end); |
2591ec64 | 2350 | e = XINT (end); |
35692fe0 JB |
2351 | } |
2352 | ||
2591ec64 EN |
2353 | if (b > e) |
2354 | temp = b, b = e, e = temp; | |
35692fe0 | 2355 | |
2591ec64 EN |
2356 | if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp))) |
2357 | args_out_of_range (start, end); | |
35692fe0 | 2358 | |
260e2e2a KH |
2359 | obuf = current_buffer; |
2360 | set_buffer_internal_1 (bp); | |
2591ec64 | 2361 | update_buffer_properties (b, e); |
260e2e2a KH |
2362 | set_buffer_internal_1 (obuf); |
2363 | ||
2591ec64 | 2364 | insert_from_buffer (bp, b, e - b, 0); |
35692fe0 JB |
2365 | return Qnil; |
2366 | } | |
e9cf2084 RS |
2367 | |
2368 | DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings, | |
deb8e082 | 2369 | 6, 6, 0, |
7ee72033 | 2370 | doc: /* Compare two substrings of two buffers; return result as number. |
a1f17501 PJ |
2371 | the value is -N if first string is less after N-1 chars, |
2372 | +N if first string is greater after N-1 chars, or 0 if strings match. | |
2373 | Each substring is represented as three arguments: BUFFER, START and END. | |
2374 | That makes six args in all, three for each substring. | |
2375 | ||
2376 | The value of `case-fold-search' in the current buffer | |
7ee72033 MB |
2377 | determines whether case is significant or ignored. */) |
2378 | (buffer1, start1, end1, buffer2, start2, end2) | |
e9cf2084 RS |
2379 | Lisp_Object buffer1, start1, end1, buffer2, start2, end2; |
2380 | { | |
07422a12 | 2381 | register int begp1, endp1, begp2, endp2, temp; |
e9cf2084 | 2382 | register struct buffer *bp1, *bp2; |
2a8b0ff0 | 2383 | register Lisp_Object *trt |
e9cf2084 | 2384 | = (!NILP (current_buffer->case_fold_search) |
2a8b0ff0 | 2385 | ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0); |
ec1c14f6 | 2386 | int chars = 0; |
07422a12 | 2387 | int i1, i2, i1_byte, i2_byte; |
e9cf2084 RS |
2388 | |
2389 | /* Find the first buffer and its substring. */ | |
2390 | ||
2391 | if (NILP (buffer1)) | |
2392 | bp1 = current_buffer; | |
2393 | else | |
2394 | { | |
3fff2dfa RS |
2395 | Lisp_Object buf1; |
2396 | buf1 = Fget_buffer (buffer1); | |
2397 | if (NILP (buf1)) | |
2398 | nsberror (buffer1); | |
2399 | bp1 = XBUFFER (buf1); | |
93b62e82 KH |
2400 | if (NILP (bp1->name)) |
2401 | error ("Selecting deleted buffer"); | |
e9cf2084 RS |
2402 | } |
2403 | ||
2404 | if (NILP (start1)) | |
2405 | begp1 = BUF_BEGV (bp1); | |
2406 | else | |
2407 | { | |
b7826503 | 2408 | CHECK_NUMBER_COERCE_MARKER (start1); |
e9cf2084 RS |
2409 | begp1 = XINT (start1); |
2410 | } | |
2411 | if (NILP (end1)) | |
2412 | endp1 = BUF_ZV (bp1); | |
2413 | else | |
2414 | { | |
b7826503 | 2415 | CHECK_NUMBER_COERCE_MARKER (end1); |
e9cf2084 RS |
2416 | endp1 = XINT (end1); |
2417 | } | |
2418 | ||
2419 | if (begp1 > endp1) | |
2420 | temp = begp1, begp1 = endp1, endp1 = temp; | |
2421 | ||
2422 | if (!(BUF_BEGV (bp1) <= begp1 | |
2423 | && begp1 <= endp1 | |
2424 | && endp1 <= BUF_ZV (bp1))) | |
2425 | args_out_of_range (start1, end1); | |
2426 | ||
2427 | /* Likewise for second substring. */ | |
2428 | ||
2429 | if (NILP (buffer2)) | |
2430 | bp2 = current_buffer; | |
2431 | else | |
2432 | { | |
3fff2dfa RS |
2433 | Lisp_Object buf2; |
2434 | buf2 = Fget_buffer (buffer2); | |
2435 | if (NILP (buf2)) | |
2436 | nsberror (buffer2); | |
3b1fdd85 | 2437 | bp2 = XBUFFER (buf2); |
93b62e82 KH |
2438 | if (NILP (bp2->name)) |
2439 | error ("Selecting deleted buffer"); | |
e9cf2084 RS |
2440 | } |
2441 | ||
2442 | if (NILP (start2)) | |
2443 | begp2 = BUF_BEGV (bp2); | |
2444 | else | |
2445 | { | |
b7826503 | 2446 | CHECK_NUMBER_COERCE_MARKER (start2); |
e9cf2084 RS |
2447 | begp2 = XINT (start2); |
2448 | } | |
2449 | if (NILP (end2)) | |
2450 | endp2 = BUF_ZV (bp2); | |
2451 | else | |
2452 | { | |
b7826503 | 2453 | CHECK_NUMBER_COERCE_MARKER (end2); |
e9cf2084 RS |
2454 | endp2 = XINT (end2); |
2455 | } | |
2456 | ||
2457 | if (begp2 > endp2) | |
2458 | temp = begp2, begp2 = endp2, endp2 = temp; | |
2459 | ||
2460 | if (!(BUF_BEGV (bp2) <= begp2 | |
2461 | && begp2 <= endp2 | |
2462 | && endp2 <= BUF_ZV (bp2))) | |
2463 | args_out_of_range (start2, end2); | |
2464 | ||
07422a12 RS |
2465 | i1 = begp1; |
2466 | i2 = begp2; | |
2467 | i1_byte = buf_charpos_to_bytepos (bp1, i1); | |
2468 | i2_byte = buf_charpos_to_bytepos (bp2, i2); | |
e9cf2084 | 2469 | |
07422a12 | 2470 | while (i1 < endp1 && i2 < endp2) |
e9cf2084 | 2471 | { |
07422a12 RS |
2472 | /* When we find a mismatch, we must compare the |
2473 | characters, not just the bytes. */ | |
2474 | int c1, c2; | |
ec1c14f6 | 2475 | |
2221451f RS |
2476 | QUIT; |
2477 | ||
07422a12 RS |
2478 | if (! NILP (bp1->enable_multibyte_characters)) |
2479 | { | |
2480 | c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte); | |
2481 | BUF_INC_POS (bp1, i1_byte); | |
2482 | i1++; | |
2483 | } | |
2484 | else | |
2485 | { | |
2486 | c1 = BUF_FETCH_BYTE (bp1, i1); | |
2487 | c1 = unibyte_char_to_multibyte (c1); | |
2488 | i1++; | |
2489 | } | |
2490 | ||
2491 | if (! NILP (bp2->enable_multibyte_characters)) | |
2492 | { | |
2493 | c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte); | |
2494 | BUF_INC_POS (bp2, i2_byte); | |
2495 | i2++; | |
2496 | } | |
2497 | else | |
2498 | { | |
2499 | c2 = BUF_FETCH_BYTE (bp2, i2); | |
2500 | c2 = unibyte_char_to_multibyte (c2); | |
2501 | i2++; | |
2502 | } | |
ec1c14f6 | 2503 | |
e9cf2084 RS |
2504 | if (trt) |
2505 | { | |
1b10fb77 RS |
2506 | c1 = XINT (trt[c1]); |
2507 | c2 = XINT (trt[c2]); | |
e9cf2084 RS |
2508 | } |
2509 | if (c1 < c2) | |
ec1c14f6 | 2510 | return make_number (- 1 - chars); |
e9cf2084 | 2511 | if (c1 > c2) |
ec1c14f6 | 2512 | return make_number (chars + 1); |
07422a12 RS |
2513 | |
2514 | chars++; | |
e9cf2084 RS |
2515 | } |
2516 | ||
2517 | /* The strings match as far as they go. | |
2518 | If one is shorter, that one is less. */ | |
07422a12 | 2519 | if (chars < endp1 - begp1) |
ec1c14f6 | 2520 | return make_number (chars + 1); |
07422a12 | 2521 | else if (chars < endp2 - begp2) |
ec1c14f6 | 2522 | return make_number (- chars - 1); |
e9cf2084 RS |
2523 | |
2524 | /* Same length too => they are equal. */ | |
2525 | return make_number (0); | |
2526 | } | |
35692fe0 | 2527 | \f |
d5a539cd RS |
2528 | static Lisp_Object |
2529 | subst_char_in_region_unwind (arg) | |
2530 | Lisp_Object arg; | |
2531 | { | |
2532 | return current_buffer->undo_list = arg; | |
2533 | } | |
2534 | ||
c8e76b47 RS |
2535 | static Lisp_Object |
2536 | subst_char_in_region_unwind_1 (arg) | |
2537 | Lisp_Object arg; | |
2538 | { | |
2539 | return current_buffer->filename = arg; | |
2540 | } | |
2541 | ||
35692fe0 | 2542 | DEFUN ("subst-char-in-region", Fsubst_char_in_region, |
deb8e082 | 2543 | Ssubst_char_in_region, 4, 5, 0, |
7ee72033 | 2544 | doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs. |
a1f17501 PJ |
2545 | If optional arg NOUNDO is non-nil, don't record this change for undo |
2546 | and don't mark the buffer as really changed. | |
7ee72033 MB |
2547 | Both characters must have the same length of multi-byte form. */) |
2548 | (start, end, fromchar, tochar, noundo) | |
35692fe0 JB |
2549 | Lisp_Object start, end, fromchar, tochar, noundo; |
2550 | { | |
84246b95 | 2551 | register int pos, pos_byte, stop, i, len, end_byte; |
60b96ee7 | 2552 | int changed = 0; |
d5c2c403 KH |
2553 | unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH]; |
2554 | unsigned char *p; | |
d5a539cd | 2555 | int count = specpdl_ptr - specpdl; |
aa801467 KH |
2556 | #define COMBINING_NO 0 |
2557 | #define COMBINING_BEFORE 1 | |
2558 | #define COMBINING_AFTER 2 | |
2559 | #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER) | |
2560 | int maybe_byte_combining = COMBINING_NO; | |
2483cf58 | 2561 | int last_changed = 0; |
7439e5b9 | 2562 | int multibyte_p = !NILP (current_buffer->enable_multibyte_characters); |
35692fe0 JB |
2563 | |
2564 | validate_region (&start, &end); | |
b7826503 PJ |
2565 | CHECK_NUMBER (fromchar); |
2566 | CHECK_NUMBER (tochar); | |
35692fe0 | 2567 | |
7439e5b9 | 2568 | if (multibyte_p) |
fb8106e8 | 2569 | { |
d5c2c403 KH |
2570 | len = CHAR_STRING (XFASTINT (fromchar), fromstr); |
2571 | if (CHAR_STRING (XFASTINT (tochar), tostr) != len) | |
fb8106e8 | 2572 | error ("Characters in subst-char-in-region have different byte-lengths"); |
aa801467 KH |
2573 | if (!ASCII_BYTE_P (*tostr)) |
2574 | { | |
2575 | /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a | |
2576 | complete multibyte character, it may be combined with the | |
2577 | after bytes. If it is in the range 0xA0..0xFF, it may be | |
2578 | combined with the before and after bytes. */ | |
2579 | if (!CHAR_HEAD_P (*tostr)) | |
2580 | maybe_byte_combining = COMBINING_BOTH; | |
2581 | else if (BYTES_BY_CHAR_HEAD (*tostr) > len) | |
2582 | maybe_byte_combining = COMBINING_AFTER; | |
2583 | } | |
fb8106e8 KH |
2584 | } |
2585 | else | |
2586 | { | |
2587 | len = 1; | |
d5c2c403 KH |
2588 | fromstr[0] = XFASTINT (fromchar); |
2589 | tostr[0] = XFASTINT (tochar); | |
fb8106e8 KH |
2590 | } |
2591 | ||
84246b95 KH |
2592 | pos = XINT (start); |
2593 | pos_byte = CHAR_TO_BYTE (pos); | |
ec1c14f6 RS |
2594 | stop = CHAR_TO_BYTE (XINT (end)); |
2595 | end_byte = stop; | |
35692fe0 | 2596 | |
d5a539cd RS |
2597 | /* If we don't want undo, turn off putting stuff on the list. |
2598 | That's faster than getting rid of things, | |
c8e76b47 RS |
2599 | and it prevents even the entry for a first change. |
2600 | Also inhibit locking the file. */ | |
d5a539cd RS |
2601 | if (!NILP (noundo)) |
2602 | { | |
2603 | record_unwind_protect (subst_char_in_region_unwind, | |
2604 | current_buffer->undo_list); | |
2605 | current_buffer->undo_list = Qt; | |
c8e76b47 RS |
2606 | /* Don't do file-locking. */ |
2607 | record_unwind_protect (subst_char_in_region_unwind_1, | |
2608 | current_buffer->filename); | |
2609 | current_buffer->filename = Qnil; | |
d5a539cd RS |
2610 | } |
2611 | ||
84246b95 | 2612 | if (pos_byte < GPT_BYTE) |
ec1c14f6 | 2613 | stop = min (stop, GPT_BYTE); |
fb8106e8 | 2614 | while (1) |
35692fe0 | 2615 | { |
a3360ff9 KH |
2616 | int pos_byte_next = pos_byte; |
2617 | ||
84246b95 | 2618 | if (pos_byte >= stop) |
fb8106e8 | 2619 | { |
84246b95 | 2620 | if (pos_byte >= end_byte) break; |
ec1c14f6 | 2621 | stop = end_byte; |
fb8106e8 | 2622 | } |
84246b95 | 2623 | p = BYTE_POS_ADDR (pos_byte); |
7439e5b9 GM |
2624 | if (multibyte_p) |
2625 | INC_POS (pos_byte_next); | |
2626 | else | |
2627 | ++pos_byte_next; | |
a3360ff9 KH |
2628 | if (pos_byte_next - pos_byte == len |
2629 | && p[0] == fromstr[0] | |
fb8106e8 KH |
2630 | && (len == 1 |
2631 | || (p[1] == fromstr[1] | |
2632 | && (len == 2 || (p[2] == fromstr[2] | |
2633 | && (len == 3 || p[3] == fromstr[3])))))) | |
35692fe0 | 2634 | { |
60b96ee7 RS |
2635 | if (! changed) |
2636 | { | |
d5c2c403 KH |
2637 | changed = pos; |
2638 | modify_region (current_buffer, changed, XINT (end)); | |
7653d030 RS |
2639 | |
2640 | if (! NILP (noundo)) | |
2641 | { | |
1e158d25 RS |
2642 | if (MODIFF - 1 == SAVE_MODIFF) |
2643 | SAVE_MODIFF++; | |
7653d030 RS |
2644 | if (MODIFF - 1 == current_buffer->auto_save_modified) |
2645 | current_buffer->auto_save_modified++; | |
2646 | } | |
60b96ee7 RS |
2647 | } |
2648 | ||
0c1e3b85 | 2649 | /* Take care of the case where the new character |
34a7a267 | 2650 | combines with neighboring bytes. */ |
a3360ff9 | 2651 | if (maybe_byte_combining |
aa801467 KH |
2652 | && (maybe_byte_combining == COMBINING_AFTER |
2653 | ? (pos_byte_next < Z_BYTE | |
2654 | && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next))) | |
2655 | : ((pos_byte_next < Z_BYTE | |
2656 | && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next))) | |
2657 | || (pos_byte > BEG_BYTE | |
2658 | && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))) | |
0c1e3b85 RS |
2659 | { |
2660 | Lisp_Object tem, string; | |
2661 | ||
2662 | struct gcpro gcpro1; | |
2663 | ||
2664 | tem = current_buffer->undo_list; | |
2665 | GCPRO1 (tem); | |
2666 | ||
aa801467 KH |
2667 | /* Make a multibyte string containing this single character. */ |
2668 | string = make_multibyte_string (tostr, 1, len); | |
0c1e3b85 RS |
2669 | /* replace_range is less efficient, because it moves the gap, |
2670 | but it handles combining correctly. */ | |
2671 | replace_range (pos, pos + 1, string, | |
9869520f | 2672 | 0, 0, 1); |
a3360ff9 KH |
2673 | pos_byte_next = CHAR_TO_BYTE (pos); |
2674 | if (pos_byte_next > pos_byte) | |
2675 | /* Before combining happened. We should not increment | |
3f5409d3 KH |
2676 | POS. So, to cancel the later increment of POS, |
2677 | decrease it now. */ | |
2678 | pos--; | |
a3360ff9 | 2679 | else |
3f5409d3 | 2680 | INC_POS (pos_byte_next); |
34a7a267 | 2681 | |
0c1e3b85 RS |
2682 | if (! NILP (noundo)) |
2683 | current_buffer->undo_list = tem; | |
2684 | ||
2685 | UNGCPRO; | |
2686 | } | |
2687 | else | |
2688 | { | |
2689 | if (NILP (noundo)) | |
2690 | record_change (pos, 1); | |
2691 | for (i = 0; i < len; i++) *p++ = tostr[i]; | |
2692 | } | |
d5c2c403 | 2693 | last_changed = pos + 1; |
35692fe0 | 2694 | } |
3f5409d3 KH |
2695 | pos_byte = pos_byte_next; |
2696 | pos++; | |
35692fe0 JB |
2697 | } |
2698 | ||
60b96ee7 | 2699 | if (changed) |
d5c2c403 KH |
2700 | { |
2701 | signal_after_change (changed, | |
2702 | last_changed - changed, last_changed - changed); | |
2703 | update_compositions (changed, last_changed, CHECK_ALL); | |
2704 | } | |
60b96ee7 | 2705 | |
d5a539cd | 2706 | unbind_to (count, Qnil); |
35692fe0 JB |
2707 | return Qnil; |
2708 | } | |
2709 | ||
2710 | DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0, | |
7ee72033 | 2711 | doc: /* From START to END, translate characters according to TABLE. |
a1f17501 PJ |
2712 | TABLE is a string; the Nth character in it is the mapping |
2713 | for the character with code N. | |
2714 | This function does not alter multibyte characters. | |
7ee72033 MB |
2715 | It returns the number of characters changed. */) |
2716 | (start, end, table) | |
35692fe0 JB |
2717 | Lisp_Object start; |
2718 | Lisp_Object end; | |
2719 | register Lisp_Object table; | |
2720 | { | |
ec1c14f6 | 2721 | register int pos_byte, stop; /* Limits of the region. */ |
35692fe0 | 2722 | register unsigned char *tt; /* Trans table. */ |
35692fe0 JB |
2723 | register int nc; /* New character. */ |
2724 | int cnt; /* Number of changes made. */ | |
35692fe0 | 2725 | int size; /* Size of translate table. */ |
1f24f4fd | 2726 | int pos; |
e8cce5af | 2727 | int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
35692fe0 JB |
2728 | |
2729 | validate_region (&start, &end); | |
b7826503 | 2730 | CHECK_STRING (table); |
35692fe0 | 2731 | |
dc3620af | 2732 | size = STRING_BYTES (XSTRING (table)); |
35692fe0 JB |
2733 | tt = XSTRING (table)->data; |
2734 | ||
ec1c14f6 RS |
2735 | pos_byte = CHAR_TO_BYTE (XINT (start)); |
2736 | stop = CHAR_TO_BYTE (XINT (end)); | |
2737 | modify_region (current_buffer, XINT (start), XINT (end)); | |
1f24f4fd | 2738 | pos = XINT (start); |
35692fe0 JB |
2739 | |
2740 | cnt = 0; | |
1f24f4fd | 2741 | for (; pos_byte < stop; ) |
35692fe0 | 2742 | { |
ec1c14f6 | 2743 | register unsigned char *p = BYTE_POS_ADDR (pos_byte); |
1f24f4fd RS |
2744 | int len; |
2745 | int oc; | |
a3360ff9 | 2746 | int pos_byte_next; |
ec1c14f6 | 2747 | |
e8cce5af KH |
2748 | if (multibyte) |
2749 | oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len); | |
2750 | else | |
2751 | oc = *p, len = 1; | |
a3360ff9 | 2752 | pos_byte_next = pos_byte + len; |
1f24f4fd | 2753 | if (oc < size && len == 1) |
35692fe0 JB |
2754 | { |
2755 | nc = tt[oc]; | |
2756 | if (nc != oc) | |
2757 | { | |
0c1e3b85 | 2758 | /* Take care of the case where the new character |
34a7a267 | 2759 | combines with neighboring bytes. */ |
a3360ff9 KH |
2760 | if (!ASCII_BYTE_P (nc) |
2761 | && (CHAR_HEAD_P (nc) | |
2762 | ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1)) | |
48839d2e | 2763 | : (pos_byte > BEG_BYTE |
a3360ff9 | 2764 | && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))) |
0c1e3b85 RS |
2765 | { |
2766 | Lisp_Object string; | |
2767 | ||
a3360ff9 | 2768 | string = make_multibyte_string (tt + oc, 1, 1); |
0c1e3b85 RS |
2769 | /* This is less efficient, because it moves the gap, |
2770 | but it handles combining correctly. */ | |
2771 | replace_range (pos, pos + 1, string, | |
a3360ff9 KH |
2772 | 1, 0, 1); |
2773 | pos_byte_next = CHAR_TO_BYTE (pos); | |
2774 | if (pos_byte_next > pos_byte) | |
2775 | /* Before combining happened. We should not | |
3f5409d3 KH |
2776 | increment POS. So, to cancel the later |
2777 | increment of POS, we decrease it now. */ | |
2778 | pos--; | |
a3360ff9 | 2779 | else |
3f5409d3 | 2780 | INC_POS (pos_byte_next); |
0c1e3b85 RS |
2781 | } |
2782 | else | |
2783 | { | |
2784 | record_change (pos, 1); | |
2785 | *p = nc; | |
2786 | signal_after_change (pos, 1, 1); | |
d5c2c403 | 2787 | update_compositions (pos, pos + 1, CHECK_BORDER); |
0c1e3b85 | 2788 | } |
35692fe0 JB |
2789 | ++cnt; |
2790 | } | |
2791 | } | |
3f5409d3 KH |
2792 | pos_byte = pos_byte_next; |
2793 | pos++; | |
35692fe0 JB |
2794 | } |
2795 | ||
ec1c14f6 | 2796 | return make_number (cnt); |
35692fe0 JB |
2797 | } |
2798 | ||
2799 | DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r", | |
7ee72033 | 2800 | doc: /* Delete the text between point and mark. |
a1f17501 | 2801 | When called from a program, expects two arguments, |
7ee72033 MB |
2802 | positions (integers or markers) specifying the stretch to be deleted. */) |
2803 | (start, end) | |
2591ec64 | 2804 | Lisp_Object start, end; |
35692fe0 | 2805 | { |
2591ec64 EN |
2806 | validate_region (&start, &end); |
2807 | del_range (XINT (start), XINT (end)); | |
35692fe0 JB |
2808 | return Qnil; |
2809 | } | |
7dae4502 SM |
2810 | |
2811 | DEFUN ("delete-and-extract-region", Fdelete_and_extract_region, | |
2812 | Sdelete_and_extract_region, 2, 2, 0, | |
7ee72033 MB |
2813 | doc: /* Delete the text between START and END and return it. */) |
2814 | (start, end) | |
7dae4502 SM |
2815 | Lisp_Object start, end; |
2816 | { | |
2817 | validate_region (&start, &end); | |
2818 | return del_range_1 (XINT (start), XINT (end), 1, 1); | |
2819 | } | |
35692fe0 JB |
2820 | \f |
2821 | DEFUN ("widen", Fwiden, Swiden, 0, 0, "", | |
7ee72033 MB |
2822 | doc: /* Remove restrictions (narrowing) from current buffer. |
2823 | This allows the buffer's full text to be seen and edited. */) | |
2824 | () | |
35692fe0 | 2825 | { |
2cad2e34 RS |
2826 | if (BEG != BEGV || Z != ZV) |
2827 | current_buffer->clip_changed = 1; | |
35692fe0 | 2828 | BEGV = BEG; |
ec1c14f6 RS |
2829 | BEGV_BYTE = BEG_BYTE; |
2830 | SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE); | |
52b14ac0 JB |
2831 | /* Changing the buffer bounds invalidates any recorded current column. */ |
2832 | invalidate_current_column (); | |
35692fe0 JB |
2833 | return Qnil; |
2834 | } | |
2835 | ||
2836 | DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r", | |
7ee72033 | 2837 | doc: /* Restrict editing in this buffer to the current region. |
a1f17501 PJ |
2838 | The rest of the text becomes temporarily invisible and untouchable |
2839 | but is not deleted; if you save the buffer in a file, the invisible | |
2840 | text is included in the file. \\[widen] makes all visible again. | |
2841 | See also `save-restriction'. | |
2842 | ||
2843 | When calling from a program, pass two arguments; positions (integers | |
7ee72033 MB |
2844 | or markers) bounding the text that should remain visible. */) |
2845 | (start, end) | |
2591ec64 | 2846 | register Lisp_Object start, end; |
35692fe0 | 2847 | { |
b7826503 PJ |
2848 | CHECK_NUMBER_COERCE_MARKER (start); |
2849 | CHECK_NUMBER_COERCE_MARKER (end); | |
35692fe0 | 2850 | |
2591ec64 | 2851 | if (XINT (start) > XINT (end)) |
35692fe0 | 2852 | { |
b5a6948e | 2853 | Lisp_Object tem; |
2591ec64 | 2854 | tem = start; start = end; end = tem; |
35692fe0 JB |
2855 | } |
2856 | ||
2591ec64 EN |
2857 | if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z)) |
2858 | args_out_of_range (start, end); | |
35692fe0 | 2859 | |
2cad2e34 RS |
2860 | if (BEGV != XFASTINT (start) || ZV != XFASTINT (end)) |
2861 | current_buffer->clip_changed = 1; | |
2862 | ||
ec1c14f6 | 2863 | SET_BUF_BEGV (current_buffer, XFASTINT (start)); |
2591ec64 | 2864 | SET_BUF_ZV (current_buffer, XFASTINT (end)); |
6ec8bbd2 | 2865 | if (PT < XFASTINT (start)) |
2591ec64 | 2866 | SET_PT (XFASTINT (start)); |
6ec8bbd2 | 2867 | if (PT > XFASTINT (end)) |
2591ec64 | 2868 | SET_PT (XFASTINT (end)); |
52b14ac0 JB |
2869 | /* Changing the buffer bounds invalidates any recorded current column. */ |
2870 | invalidate_current_column (); | |
35692fe0 JB |
2871 | return Qnil; |
2872 | } | |
2873 | ||
2874 | Lisp_Object | |
2875 | save_restriction_save () | |
2876 | { | |
d6abb4c7 MB |
2877 | if (BEGV == BEG && ZV == Z) |
2878 | /* The common case that the buffer isn't narrowed. | |
2879 | We return just the buffer object, which save_restriction_restore | |
2880 | recognizes as meaning `no restriction'. */ | |
2881 | return Fcurrent_buffer (); | |
2882 | else | |
2883 | /* We have to save a restriction, so return a pair of markers, one | |
2884 | for the beginning and one for the end. */ | |
2885 | { | |
2886 | Lisp_Object beg, end; | |
2887 | ||
2888 | beg = buildmark (BEGV, BEGV_BYTE); | |
2889 | end = buildmark (ZV, ZV_BYTE); | |
35692fe0 | 2890 | |
d6abb4c7 MB |
2891 | /* END must move forward if text is inserted at its exact location. */ |
2892 | XMARKER(end)->insertion_type = 1; | |
2893 | ||
2894 | return Fcons (beg, end); | |
2895 | } | |
35692fe0 JB |
2896 | } |
2897 | ||
2898 | Lisp_Object | |
2899 | save_restriction_restore (data) | |
2900 | Lisp_Object data; | |
2901 | { | |
d6abb4c7 MB |
2902 | if (CONSP (data)) |
2903 | /* A pair of marks bounding a saved restriction. */ | |
35692fe0 | 2904 | { |
d6abb4c7 MB |
2905 | struct Lisp_Marker *beg = XMARKER (XCAR (data)); |
2906 | struct Lisp_Marker *end = XMARKER (XCDR (data)); | |
2907 | struct buffer *buf = beg->buffer; /* END should have the same buffer. */ | |
2cad2e34 | 2908 | |
d6abb4c7 MB |
2909 | if (beg->charpos != BUF_BEGV(buf) || end->charpos != BUF_ZV(buf)) |
2910 | /* The restriction has changed from the saved one, so restore | |
2911 | the saved restriction. */ | |
2912 | { | |
2913 | int pt = BUF_PT (buf); | |
2914 | ||
2915 | SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos); | |
2916 | SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos); | |
2917 | ||
2918 | if (pt < beg->charpos || pt > end->charpos) | |
2919 | /* The point is outside the new visible range, move it inside. */ | |
2920 | SET_BUF_PT_BOTH (buf, | |
2921 | clip_to_bounds (beg->charpos, pt, end->charpos), | |
2922 | clip_to_bounds (beg->bytepos, BUF_PT_BYTE(buf), | |
2923 | end->bytepos)); | |
2924 | ||
2925 | buf->clip_changed = 1; /* Remember that the narrowing changed. */ | |
2926 | } | |
2927 | } | |
2928 | else | |
2929 | /* A buffer, which means that there was no old restriction. */ | |
2930 | { | |
2931 | struct buffer *buf = XBUFFER (data); | |
2cad2e34 | 2932 | |
d6abb4c7 MB |
2933 | if (BUF_BEGV(buf) != BUF_BEG(buf) || BUF_ZV(buf) != BUF_Z(buf)) |
2934 | /* The buffer has been narrowed, get rid of the narrowing. */ | |
2935 | { | |
2936 | SET_BUF_BEGV_BOTH (buf, BUF_BEG(buf), BUF_BEG_BYTE(buf)); | |
2937 | SET_BUF_ZV_BOTH (buf, BUF_Z(buf), BUF_Z_BYTE(buf)); | |
35692fe0 | 2938 | |
d6abb4c7 MB |
2939 | buf->clip_changed = 1; /* Remember that the narrowing changed. */ |
2940 | } | |
2941 | } | |
35692fe0 JB |
2942 | |
2943 | return Qnil; | |
2944 | } | |
2945 | ||
2946 | DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0, | |
7ee72033 | 2947 | doc: /* Execute BODY, saving and restoring current buffer's restrictions. |
a1f17501 PJ |
2948 | The buffer's restrictions make parts of the beginning and end invisible. |
2949 | (They are set up with `narrow-to-region' and eliminated with `widen'.) | |
2950 | This special form, `save-restriction', saves the current buffer's restrictions | |
2951 | when it is entered, and restores them when it is exited. | |
2952 | So any `narrow-to-region' within BODY lasts only until the end of the form. | |
2953 | The old restrictions settings are restored | |
2954 | even in case of abnormal exit (throw or error). | |
2955 | ||
2956 | The value returned is the value of the last form in BODY. | |
2957 | ||
2958 | Note: if you are using both `save-excursion' and `save-restriction', | |
2959 | use `save-excursion' outermost: | |
33c2d29f MB |
2960 | (save-excursion (save-restriction ...)) |
2961 | ||
2962 | usage: (save-restriction &rest BODY) */) | |
7ee72033 | 2963 | (body) |
35692fe0 JB |
2964 | Lisp_Object body; |
2965 | { | |
2966 | register Lisp_Object val; | |
2967 | int count = specpdl_ptr - specpdl; | |
2968 | ||
2969 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
2970 | val = Fprogn (body); | |
2971 | return unbind_to (count, val); | |
2972 | } | |
2973 | \f | |
0ae83348 | 2974 | /* Buffer for the most recent text displayed by Fmessage_box. */ |
671fbc4d KH |
2975 | static char *message_text; |
2976 | ||
2977 | /* Allocated length of that buffer. */ | |
2978 | static int message_length; | |
2979 | ||
35692fe0 | 2980 | DEFUN ("message", Fmessage, Smessage, 1, MANY, 0, |
7ee72033 | 2981 | doc: /* Print a one-line message at the bottom of the screen. |
a1f17501 PJ |
2982 | The first argument is a format control string, and the rest are data |
2983 | to be formatted under control of the string. See `format' for details. | |
2984 | ||
2985 | If the first argument is nil, clear any existing message; let the | |
4bfbe194 MB |
2986 | minibuffer contents show. |
2987 | ||
2988 | usage: (message STRING &rest ARGS) */) | |
7ee72033 | 2989 | (nargs, args) |
35692fe0 JB |
2990 | int nargs; |
2991 | Lisp_Object *args; | |
2992 | { | |
ccdac5be | 2993 | if (NILP (args[0])) |
f0250249 JB |
2994 | { |
2995 | message (0); | |
2996 | return Qnil; | |
2997 | } | |
ccdac5be JB |
2998 | else |
2999 | { | |
3000 | register Lisp_Object val; | |
3001 | val = Fformat (nargs, args); | |
5e6d5493 | 3002 | message3 (val, STRING_BYTES (XSTRING (val)), STRING_MULTIBYTE (val)); |
ccdac5be JB |
3003 | return val; |
3004 | } | |
35692fe0 JB |
3005 | } |
3006 | ||
cacc3e2c | 3007 | DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0, |
7ee72033 | 3008 | doc: /* Display a message, in a dialog box if possible. |
a1f17501 PJ |
3009 | If a dialog box is not available, use the echo area. |
3010 | The first argument is a format control string, and the rest are data | |
3011 | to be formatted under control of the string. See `format' for details. | |
3012 | ||
3013 | If the first argument is nil, clear any existing message; let the | |
4bfbe194 MB |
3014 | minibuffer contents show. |
3015 | ||
3016 | usage: (message-box STRING &rest ARGS) */) | |
7ee72033 | 3017 | (nargs, args) |
cacc3e2c RS |
3018 | int nargs; |
3019 | Lisp_Object *args; | |
3020 | { | |
3021 | if (NILP (args[0])) | |
3022 | { | |
3023 | message (0); | |
3024 | return Qnil; | |
3025 | } | |
3026 | else | |
3027 | { | |
3028 | register Lisp_Object val; | |
3029 | val = Fformat (nargs, args); | |
f8250f01 | 3030 | #ifdef HAVE_MENUS |
0ae83348 EZ |
3031 | /* The MS-DOS frames support popup menus even though they are |
3032 | not FRAME_WINDOW_P. */ | |
3033 | if (FRAME_WINDOW_P (XFRAME (selected_frame)) | |
3034 | || FRAME_MSDOS_P (XFRAME (selected_frame))) | |
cacc3e2c RS |
3035 | { |
3036 | Lisp_Object pane, menu, obj; | |
3037 | struct gcpro gcpro1; | |
3038 | pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil); | |
3039 | GCPRO1 (pane); | |
3040 | menu = Fcons (val, pane); | |
3041 | obj = Fx_popup_dialog (Qt, menu); | |
3042 | UNGCPRO; | |
3043 | return val; | |
3044 | } | |
0ae83348 | 3045 | #endif /* HAVE_MENUS */ |
cacc3e2c RS |
3046 | /* Copy the data so that it won't move when we GC. */ |
3047 | if (! message_text) | |
3048 | { | |
3049 | message_text = (char *)xmalloc (80); | |
3050 | message_length = 80; | |
3051 | } | |
dc3620af | 3052 | if (STRING_BYTES (XSTRING (val)) > message_length) |
cacc3e2c | 3053 | { |
dc3620af | 3054 | message_length = STRING_BYTES (XSTRING (val)); |
cacc3e2c RS |
3055 | message_text = (char *)xrealloc (message_text, message_length); |
3056 | } | |
dc3620af | 3057 | bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val))); |
d13a8480 RS |
3058 | message2 (message_text, STRING_BYTES (XSTRING (val)), |
3059 | STRING_MULTIBYTE (val)); | |
cacc3e2c | 3060 | return val; |
cacc3e2c RS |
3061 | } |
3062 | } | |
f8250f01 | 3063 | #ifdef HAVE_MENUS |
cacc3e2c RS |
3064 | extern Lisp_Object last_nonmenu_event; |
3065 | #endif | |
f8250f01 | 3066 | |
cacc3e2c | 3067 | DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0, |
7ee72033 | 3068 | doc: /* Display a message in a dialog box or in the echo area. |
a1f17501 PJ |
3069 | If this command was invoked with the mouse, use a dialog box if |
3070 | `use-dialog-box' is non-nil. | |
3071 | Otherwise, use the echo area. | |
3072 | The first argument is a format control string, and the rest are data | |
3073 | to be formatted under control of the string. See `format' for details. | |
3074 | ||
3075 | If the first argument is nil, clear any existing message; let the | |
4bfbe194 MB |
3076 | minibuffer contents show. |
3077 | ||
3078 | usage: (message-or-box STRING &rest ARGS) */) | |
7ee72033 | 3079 | (nargs, args) |
cacc3e2c RS |
3080 | int nargs; |
3081 | Lisp_Object *args; | |
3082 | { | |
f8250f01 | 3083 | #ifdef HAVE_MENUS |
5920df33 | 3084 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
c01fbf95 | 3085 | && use_dialog_box) |
0a56ee6b | 3086 | return Fmessage_box (nargs, args); |
cacc3e2c RS |
3087 | #endif |
3088 | return Fmessage (nargs, args); | |
3089 | } | |
3090 | ||
b14dda8a | 3091 | DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0, |
7ee72033 MB |
3092 | doc: /* Return the string currently displayed in the echo area, or nil if none. */) |
3093 | () | |
b14dda8a | 3094 | { |
0634a78e | 3095 | return current_message (); |
b14dda8a RS |
3096 | } |
3097 | ||
2d9811c4 | 3098 | |
d2936d21 | 3099 | DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0, |
7ee72033 | 3100 | doc: /* Return a copy of STRING with text properties added. |
a1f17501 PJ |
3101 | First argument is the string to copy. |
3102 | Remaining arguments form a sequence of PROPERTY VALUE pairs for text | |
4bfbe194 MB |
3103 | properties to add to the result. |
3104 | usage: (propertize STRING &rest PROPERTIES) */) | |
7ee72033 | 3105 | (nargs, args) |
2d9811c4 GM |
3106 | int nargs; |
3107 | Lisp_Object *args; | |
3108 | { | |
3109 | Lisp_Object properties, string; | |
3110 | struct gcpro gcpro1, gcpro2; | |
3111 | int i; | |
3112 | ||
3113 | /* Number of args must be odd. */ | |
d2936d21 | 3114 | if ((nargs & 1) == 0 || nargs < 1) |
2d9811c4 GM |
3115 | error ("Wrong number of arguments"); |
3116 | ||
3117 | properties = string = Qnil; | |
3118 | GCPRO2 (properties, string); | |
34a7a267 | 3119 | |
2d9811c4 | 3120 | /* First argument must be a string. */ |
b7826503 | 3121 | CHECK_STRING (args[0]); |
2d9811c4 GM |
3122 | string = Fcopy_sequence (args[0]); |
3123 | ||
3124 | for (i = 1; i < nargs; i += 2) | |
3125 | { | |
b7826503 | 3126 | CHECK_SYMBOL (args[i]); |
2d9811c4 GM |
3127 | properties = Fcons (args[i], Fcons (args[i + 1], properties)); |
3128 | } | |
3129 | ||
3130 | Fadd_text_properties (make_number (0), | |
3131 | make_number (XSTRING (string)->size), | |
3132 | properties, string); | |
3133 | RETURN_UNGCPRO (string); | |
3134 | } | |
3135 | ||
3136 | ||
1f24f4fd RS |
3137 | /* Number of bytes that STRING will occupy when put into the result. |
3138 | MULTIBYTE is nonzero if the result should be multibyte. */ | |
3139 | ||
3140 | #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \ | |
3141 | (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \ | |
8d6179dc | 3142 | ? count_size_as_multibyte (XSTRING (STRING)->data, \ |
dc3620af RS |
3143 | STRING_BYTES (XSTRING (STRING))) \ |
3144 | : STRING_BYTES (XSTRING (STRING))) | |
1f24f4fd | 3145 | |
35692fe0 | 3146 | DEFUN ("format", Fformat, Sformat, 1, MANY, 0, |
7ee72033 | 3147 | doc: /* Format a string out of a control-string and arguments. |
a1f17501 PJ |
3148 | The first argument is a control string. |
3149 | The other arguments are substituted into it to make the result, a string. | |
3150 | It may contain %-sequences meaning to substitute the next argument. | |
3151 | %s means print a string argument. Actually, prints any object, with `princ'. | |
3152 | %d means print as number in decimal (%o octal, %x hex). | |
3153 | %X is like %x, but uses upper case. | |
3154 | %e means print a number in exponential notation. | |
3155 | %f means print a number in decimal-point notation. | |
3156 | %g means print a number in exponential notation | |
3157 | or decimal-point notation, whichever uses fewer characters. | |
3158 | %c means print a number as a single character. | |
3159 | %S means print any object as an s-expression (using `prin1'). | |
3160 | The argument used for %d, %o, %x, %e, %f, %g or %c must be a number. | |
4bfbe194 MB |
3161 | Use %% to put a single % into the output. |
3162 | ||
3163 | usage: (format STRING &rest OBJECTS) */) | |
7ee72033 | 3164 | (nargs, args) |
35692fe0 JB |
3165 | int nargs; |
3166 | register Lisp_Object *args; | |
3167 | { | |
3168 | register int n; /* The number of the next arg to substitute */ | |
e781c49e | 3169 | register int total; /* An estimate of the final length */ |
1f24f4fd | 3170 | char *buf, *p; |
35692fe0 | 3171 | register unsigned char *format, *end; |
2ea0266e | 3172 | int nchars; |
1f24f4fd RS |
3173 | /* Nonzero if the output should be a multibyte string, |
3174 | which is true if any of the inputs is one. */ | |
3175 | int multibyte = 0; | |
8f2917e4 KH |
3176 | /* When we make a multibyte string, we must pay attention to the |
3177 | byte combining problem, i.e., a byte may be combined with a | |
3178 | multibyte charcter of the previous string. This flag tells if we | |
3179 | must consider such a situation or not. */ | |
3180 | int maybe_combine_byte; | |
1f24f4fd | 3181 | unsigned char *this_format; |
e781c49e | 3182 | int longest_format; |
8d6179dc | 3183 | Lisp_Object val; |
5e6d5493 GM |
3184 | struct info |
3185 | { | |
3186 | int start, end; | |
3187 | } *info = 0; | |
1f24f4fd | 3188 | |
35692fe0 JB |
3189 | /* It should not be necessary to GCPRO ARGS, because |
3190 | the caller in the interpreter should take care of that. */ | |
3191 | ||
e781c49e RS |
3192 | /* Try to determine whether the result should be multibyte. |
3193 | This is not always right; sometimes the result needs to be multibyte | |
3194 | because of an object that we will pass through prin1, | |
3195 | and in that case, we won't know it here. */ | |
1f24f4fd RS |
3196 | for (n = 0; n < nargs; n++) |
3197 | if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n])) | |
3198 | multibyte = 1; | |
3199 | ||
b7826503 | 3200 | CHECK_STRING (args[0]); |
e781c49e RS |
3201 | |
3202 | /* If we start out planning a unibyte result, | |
3203 | and later find it has to be multibyte, we jump back to retry. */ | |
3204 | retry: | |
3205 | ||
35692fe0 | 3206 | format = XSTRING (args[0])->data; |
dc3620af | 3207 | end = format + STRING_BYTES (XSTRING (args[0])); |
e781c49e | 3208 | longest_format = 0; |
1f24f4fd RS |
3209 | |
3210 | /* Make room in result for all the non-%-codes in the control string. */ | |
e781c49e | 3211 | total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]); |
1f24f4fd RS |
3212 | |
3213 | /* Add to TOTAL enough space to hold the converted arguments. */ | |
35692fe0 JB |
3214 | |
3215 | n = 0; | |
3216 | while (format != end) | |
3217 | if (*format++ == '%') | |
3218 | { | |
a432bfe5 | 3219 | int thissize = 0; |
1f24f4fd | 3220 | unsigned char *this_format_start = format - 1; |
a432bfe5 | 3221 | int field_width, precision; |
35692fe0 | 3222 | |
a432bfe5 | 3223 | /* General format specifications look like |
537dfb13 | 3224 | |
a432bfe5 GM |
3225 | '%' [flags] [field-width] [precision] format |
3226 | ||
3227 | where | |
3228 | ||
3229 | flags ::= [#-* 0]+ | |
3230 | field-width ::= [0-9]+ | |
3231 | precision ::= '.' [0-9]* | |
3232 | ||
3233 | If a field-width is specified, it specifies to which width | |
3234 | the output should be padded with blanks, iff the output | |
3235 | string is shorter than field-width. | |
3236 | ||
3237 | if precision is specified, it specifies the number of | |
3238 | digits to print after the '.' for floats, or the max. | |
3239 | number of chars to print from a string. */ | |
3240 | ||
3241 | precision = field_width = 0; | |
3242 | ||
3243 | while (index ("-*# 0", *format)) | |
3244 | ++format; | |
3245 | ||
3246 | if (*format >= '0' && *format <= '9') | |
3247 | { | |
3248 | for (field_width = 0; *format >= '0' && *format <= '9'; ++format) | |
3249 | field_width = 10 * field_width + *format - '0'; | |
3250 | } | |
3251 | ||
3252 | if (*format == '.') | |
3253 | { | |
3254 | ++format; | |
3255 | for (precision = 0; *format >= '0' && *format <= '9'; ++format) | |
3256 | precision = 10 * precision + *format - '0'; | |
3257 | } | |
35692fe0 | 3258 | |
1f24f4fd RS |
3259 | if (format - this_format_start + 1 > longest_format) |
3260 | longest_format = format - this_format_start + 1; | |
3261 | ||
bf6ab66c KH |
3262 | if (format == end) |
3263 | error ("Format string ends in middle of format specifier"); | |
35692fe0 JB |
3264 | if (*format == '%') |
3265 | format++; | |
3266 | else if (++n >= nargs) | |
537dfb13 | 3267 | error ("Not enough arguments for format string"); |
35692fe0 JB |
3268 | else if (*format == 'S') |
3269 | { | |
3270 | /* For `S', prin1 the argument and then treat like a string. */ | |
3271 | register Lisp_Object tem; | |
3272 | tem = Fprin1_to_string (args[n], Qnil); | |
e781c49e RS |
3273 | if (STRING_MULTIBYTE (tem) && ! multibyte) |
3274 | { | |
3275 | multibyte = 1; | |
3276 | goto retry; | |
3277 | } | |
35692fe0 JB |
3278 | args[n] = tem; |
3279 | goto string; | |
3280 | } | |
ae683129 | 3281 | else if (SYMBOLP (args[n])) |
35692fe0 | 3282 | { |
c01fbf95 KR |
3283 | /* Use a temp var to avoid problems when ENABLE_CHECKING |
3284 | is turned on. */ | |
3285 | struct Lisp_String *t = XSYMBOL (args[n])->name; | |
3286 | XSETSTRING (args[n], t); | |
7df74da6 RS |
3287 | if (STRING_MULTIBYTE (args[n]) && ! multibyte) |
3288 | { | |
3289 | multibyte = 1; | |
3290 | goto retry; | |
3291 | } | |
35692fe0 JB |
3292 | goto string; |
3293 | } | |
ae683129 | 3294 | else if (STRINGP (args[n])) |
35692fe0 JB |
3295 | { |
3296 | string: | |
b22e7ecc | 3297 | if (*format != 's' && *format != 'S') |
bf6ab66c | 3298 | error ("Format specifier doesn't match argument type"); |
1f24f4fd | 3299 | thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]); |
35692fe0 JB |
3300 | } |
3301 | /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */ | |
ae683129 | 3302 | else if (INTEGERP (args[n]) && *format != 's') |
35692fe0 | 3303 | { |
eb8c3be9 | 3304 | /* The following loop assumes the Lisp type indicates |
35692fe0 JB |
3305 | the proper way to pass the argument. |
3306 | So make sure we have a flonum if the argument should | |
3307 | be a double. */ | |
3308 | if (*format == 'e' || *format == 'f' || *format == 'g') | |
3309 | args[n] = Ffloat (args[n]); | |
4224cb62 | 3310 | else |
4224cb62 | 3311 | if (*format != 'd' && *format != 'o' && *format != 'x' |
00d65216 | 3312 | && *format != 'i' && *format != 'X' && *format != 'c') |
4224cb62 KH |
3313 | error ("Invalid format operation %%%c", *format); |
3314 | ||
34a7a267 | 3315 | thissize = 30; |
25c9e7fb RS |
3316 | if (*format == 'c' |
3317 | && (! SINGLE_BYTE_CHAR_P (XINT (args[n])) | |
3318 | || XINT (args[n]) == 0)) | |
f49a2d74 KH |
3319 | { |
3320 | if (! multibyte) | |
3321 | { | |
3322 | multibyte = 1; | |
3323 | goto retry; | |
3324 | } | |
3325 | args[n] = Fchar_to_string (args[n]); | |
dc3620af | 3326 | thissize = STRING_BYTES (XSTRING (args[n])); |
f49a2d74 | 3327 | } |
35692fe0 | 3328 | } |
ae683129 | 3329 | else if (FLOATP (args[n]) && *format != 's') |
35692fe0 JB |
3330 | { |
3331 | if (! (*format == 'e' || *format == 'f' || *format == 'g')) | |
247422ce | 3332 | args[n] = Ftruncate (args[n], Qnil); |
a432bfe5 GM |
3333 | |
3334 | /* Note that we're using sprintf to print floats, | |
3335 | so we have to take into account what that function | |
3336 | prints. */ | |
ea229bec | 3337 | thissize = MAX_10_EXP + 100 + precision; |
35692fe0 JB |
3338 | } |
3339 | else | |
3340 | { | |
3341 | /* Anything but a string, convert to a string using princ. */ | |
3342 | register Lisp_Object tem; | |
3343 | tem = Fprin1_to_string (args[n], Qt); | |
a4e91443 | 3344 | if (STRING_MULTIBYTE (tem) & ! multibyte) |
e781c49e RS |
3345 | { |
3346 | multibyte = 1; | |
3347 | goto retry; | |
3348 | } | |
35692fe0 JB |
3349 | args[n] = tem; |
3350 | goto string; | |
3351 | } | |
34a7a267 | 3352 | |
a432bfe5 | 3353 | thissize = max (field_width, thissize); |
1f24f4fd | 3354 | total += thissize + 4; |
35692fe0 JB |
3355 | } |
3356 | ||
e781c49e RS |
3357 | /* Now we can no longer jump to retry. |
3358 | TOTAL and LONGEST_FORMAT are known for certain. */ | |
3359 | ||
1f24f4fd | 3360 | this_format = (unsigned char *) alloca (longest_format + 1); |
50aa2f90 | 3361 | |
1f24f4fd RS |
3362 | /* Allocate the space for the result. |
3363 | Note that TOTAL is an overestimate. */ | |
3364 | if (total < 1000) | |
3c6db9d5 | 3365 | buf = (char *) alloca (total + 1); |
1f24f4fd | 3366 | else |
3c6db9d5 | 3367 | buf = (char *) xmalloc (total + 1); |
35692fe0 | 3368 | |
1f24f4fd RS |
3369 | p = buf; |
3370 | nchars = 0; | |
3371 | n = 0; | |
35692fe0 | 3372 | |
1f24f4fd RS |
3373 | /* Scan the format and store result in BUF. */ |
3374 | format = XSTRING (args[0])->data; | |
8f2917e4 | 3375 | maybe_combine_byte = 0; |
1f24f4fd RS |
3376 | while (format != end) |
3377 | { | |
3378 | if (*format == '%') | |
3379 | { | |
3380 | int minlen; | |
25c9e7fb | 3381 | int negative = 0; |
1f24f4fd | 3382 | unsigned char *this_format_start = format; |
35692fe0 | 3383 | |
1f24f4fd | 3384 | format++; |
fb893977 | 3385 | |
1f24f4fd RS |
3386 | /* Process a numeric arg and skip it. */ |
3387 | minlen = atoi (format); | |
3388 | if (minlen < 0) | |
25c9e7fb | 3389 | minlen = - minlen, negative = 1; |
35692fe0 | 3390 | |
1f24f4fd RS |
3391 | while ((*format >= '0' && *format <= '9') |
3392 | || *format == '-' || *format == ' ' || *format == '.') | |
3393 | format++; | |
35692fe0 | 3394 | |
1f24f4fd RS |
3395 | if (*format++ == '%') |
3396 | { | |
3397 | *p++ = '%'; | |
3398 | nchars++; | |
3399 | continue; | |
3400 | } | |
3401 | ||
3402 | ++n; | |
3403 | ||
3404 | if (STRINGP (args[n])) | |
3405 | { | |
50606b4c | 3406 | int padding, nbytes, start, end; |
e1e40b38 | 3407 | int width = lisp_string_width (args[n], -1, NULL, NULL); |
25c9e7fb RS |
3408 | |
3409 | /* If spec requires it, pad on right with spaces. */ | |
3410 | padding = minlen - width; | |
3411 | if (! negative) | |
3412 | while (padding-- > 0) | |
3413 | { | |
3414 | *p++ = ' '; | |
50606b4c | 3415 | ++nchars; |
25c9e7fb | 3416 | } |
1f24f4fd | 3417 | |
50606b4c GM |
3418 | start = nchars; |
3419 | ||
8f2917e4 KH |
3420 | if (p > buf |
3421 | && multibyte | |
25aa5d64 | 3422 | && !ASCII_BYTE_P (*((unsigned char *) p - 1)) |
8f2917e4 | 3423 | && STRING_MULTIBYTE (args[n]) |
25aa5d64 | 3424 | && !CHAR_HEAD_P (XSTRING (args[n])->data[0])) |
8f2917e4 | 3425 | maybe_combine_byte = 1; |
1f24f4fd | 3426 | nbytes = copy_text (XSTRING (args[n])->data, p, |
dc3620af | 3427 | STRING_BYTES (XSTRING (args[n])), |
1f24f4fd RS |
3428 | STRING_MULTIBYTE (args[n]), multibyte); |
3429 | p += nbytes; | |
3430 | nchars += XSTRING (args[n])->size; | |
50606b4c | 3431 | end = nchars; |
1f24f4fd | 3432 | |
25c9e7fb RS |
3433 | if (negative) |
3434 | while (padding-- > 0) | |
3435 | { | |
3436 | *p++ = ' '; | |
3437 | nchars++; | |
3438 | } | |
5e6d5493 GM |
3439 | |
3440 | /* If this argument has text properties, record where | |
3441 | in the result string it appears. */ | |
3442 | if (XSTRING (args[n])->intervals) | |
3443 | { | |
3444 | if (!info) | |
3445 | { | |
3446 | int nbytes = nargs * sizeof *info; | |
3447 | info = (struct info *) alloca (nbytes); | |
3448 | bzero (info, nbytes); | |
3449 | } | |
34a7a267 | 3450 | |
5e6d5493 | 3451 | info[n].start = start; |
50606b4c | 3452 | info[n].end = end; |
5e6d5493 | 3453 | } |
1f24f4fd RS |
3454 | } |
3455 | else if (INTEGERP (args[n]) || FLOATP (args[n])) | |
3456 | { | |
3457 | int this_nchars; | |
3458 | ||
3459 | bcopy (this_format_start, this_format, | |
3460 | format - this_format_start); | |
3461 | this_format[format - this_format_start] = 0; | |
3462 | ||
d0183d38 RS |
3463 | if (INTEGERP (args[n])) |
3464 | sprintf (p, this_format, XINT (args[n])); | |
3465 | else | |
03699b14 | 3466 | sprintf (p, this_format, XFLOAT_DATA (args[n])); |
1f24f4fd | 3467 | |
8f2917e4 KH |
3468 | if (p > buf |
3469 | && multibyte | |
25aa5d64 KH |
3470 | && !ASCII_BYTE_P (*((unsigned char *) p - 1)) |
3471 | && !CHAR_HEAD_P (*((unsigned char *) p))) | |
8f2917e4 | 3472 | maybe_combine_byte = 1; |
1f24f4fd | 3473 | this_nchars = strlen (p); |
9a599130 KH |
3474 | if (multibyte) |
3475 | p += str_to_multibyte (p, buf + total - p, this_nchars); | |
3476 | else | |
3477 | p += this_nchars; | |
1f24f4fd RS |
3478 | nchars += this_nchars; |
3479 | } | |
3480 | } | |
7df74da6 RS |
3481 | else if (STRING_MULTIBYTE (args[0])) |
3482 | { | |
3483 | /* Copy a whole multibyte character. */ | |
8f2917e4 KH |
3484 | if (p > buf |
3485 | && multibyte | |
25aa5d64 KH |
3486 | && !ASCII_BYTE_P (*((unsigned char *) p - 1)) |
3487 | && !CHAR_HEAD_P (*format)) | |
8f2917e4 | 3488 | maybe_combine_byte = 1; |
7df74da6 RS |
3489 | *p++ = *format++; |
3490 | while (! CHAR_HEAD_P (*format)) *p++ = *format++; | |
3491 | nchars++; | |
3492 | } | |
3493 | else if (multibyte) | |
1f24f4fd RS |
3494 | { |
3495 | /* Convert a single-byte character to multibyte. */ | |
3496 | int len = copy_text (format, p, 1, 0, 1); | |
3497 | ||
3498 | p += len; | |
3499 | format++; | |
3500 | nchars++; | |
3501 | } | |
3502 | else | |
3503 | *p++ = *format++, nchars++; | |
3504 | } | |
3505 | ||
a432bfe5 GM |
3506 | if (p > buf + total + 1) |
3507 | abort (); | |
3508 | ||
8f2917e4 KH |
3509 | if (maybe_combine_byte) |
3510 | nchars = multibyte_chars_in_text (buf, p - buf); | |
5f75e666 | 3511 | val = make_specified_string (buf, nchars, p - buf, multibyte); |
8d6179dc | 3512 | |
1f24f4fd RS |
3513 | /* If we allocated BUF with malloc, free it too. */ |
3514 | if (total >= 1000) | |
3515 | xfree (buf); | |
35692fe0 | 3516 | |
5e6d5493 GM |
3517 | /* If the format string has text properties, or any of the string |
3518 | arguments has text properties, set up text properties of the | |
3519 | result string. */ | |
34a7a267 | 3520 | |
5e6d5493 GM |
3521 | if (XSTRING (args[0])->intervals || info) |
3522 | { | |
3523 | Lisp_Object len, new_len, props; | |
3524 | struct gcpro gcpro1; | |
34a7a267 | 3525 | |
5e6d5493 GM |
3526 | /* Add text properties from the format string. */ |
3527 | len = make_number (XSTRING (args[0])->size); | |
3528 | props = text_property_list (args[0], make_number (0), len, Qnil); | |
3529 | GCPRO1 (props); | |
34a7a267 | 3530 | |
5e6d5493 GM |
3531 | if (CONSP (props)) |
3532 | { | |
3533 | new_len = make_number (XSTRING (val)->size); | |
3534 | extend_property_ranges (props, len, new_len); | |
3535 | add_text_properties_from_list (val, props, make_number (0)); | |
3536 | } | |
3537 | ||
3538 | /* Add text properties from arguments. */ | |
3539 | if (info) | |
3540 | for (n = 1; n < nargs; ++n) | |
3541 | if (info[n].end) | |
3542 | { | |
3543 | len = make_number (XSTRING (args[n])->size); | |
3544 | new_len = make_number (info[n].end - info[n].start); | |
3545 | props = text_property_list (args[n], make_number (0), len, Qnil); | |
3546 | extend_property_ranges (props, len, new_len); | |
be17069b KH |
3547 | /* If successive arguments have properites, be sure that |
3548 | the value of `composition' property be the copy. */ | |
3549 | if (n > 1 && info[n - 1].end) | |
3550 | make_composition_value_copy (props); | |
5e6d5493 GM |
3551 | add_text_properties_from_list (val, props, |
3552 | make_number (info[n].start)); | |
3553 | } | |
3554 | ||
3555 | UNGCPRO; | |
3556 | } | |
3557 | ||
8d6179dc | 3558 | return val; |
35692fe0 JB |
3559 | } |
3560 | ||
2d9811c4 | 3561 | |
35692fe0 JB |
3562 | /* VARARGS 1 */ |
3563 | Lisp_Object | |
3564 | #ifdef NO_ARG_ARRAY | |
3565 | format1 (string1, arg0, arg1, arg2, arg3, arg4) | |
679e18b1 | 3566 | EMACS_INT arg0, arg1, arg2, arg3, arg4; |
35692fe0 JB |
3567 | #else |
3568 | format1 (string1) | |
3569 | #endif | |
3570 | char *string1; | |
3571 | { | |
3572 | char buf[100]; | |
3573 | #ifdef NO_ARG_ARRAY | |
679e18b1 | 3574 | EMACS_INT args[5]; |
35692fe0 JB |
3575 | args[0] = arg0; |
3576 | args[1] = arg1; | |
3577 | args[2] = arg2; | |
3578 | args[3] = arg3; | |
3579 | args[4] = arg4; | |
e3670faa | 3580 | doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args); |
35692fe0 | 3581 | #else |
ea4d2909 | 3582 | doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1); |
35692fe0 JB |
3583 | #endif |
3584 | return build_string (buf); | |
3585 | } | |
3586 | \f | |
3587 | DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0, | |
7ee72033 | 3588 | doc: /* Return t if two characters match, optionally ignoring case. |
a1f17501 | 3589 | Both arguments must be characters (i.e. integers). |
7ee72033 MB |
3590 | Case is ignored if `case-fold-search' is non-nil in the current buffer. */) |
3591 | (c1, c2) | |
35692fe0 JB |
3592 | register Lisp_Object c1, c2; |
3593 | { | |
1b5d98bb | 3594 | int i1, i2; |
b7826503 PJ |
3595 | CHECK_NUMBER (c1); |
3596 | CHECK_NUMBER (c2); | |
35692fe0 | 3597 | |
1b5d98bb | 3598 | if (XINT (c1) == XINT (c2)) |
35692fe0 | 3599 | return Qt; |
1b5d98bb RS |
3600 | if (NILP (current_buffer->case_fold_search)) |
3601 | return Qnil; | |
3602 | ||
3603 | /* Do these in separate statements, | |
3604 | then compare the variables. | |
3605 | because of the way DOWNCASE uses temp variables. */ | |
3606 | i1 = DOWNCASE (XFASTINT (c1)); | |
3607 | i2 = DOWNCASE (XFASTINT (c2)); | |
3608 | return (i1 == i2 ? Qt : Qnil); | |
35692fe0 | 3609 | } |
b229b8d1 RS |
3610 | \f |
3611 | /* Transpose the markers in two regions of the current buffer, and | |
3612 | adjust the ones between them if necessary (i.e.: if the regions | |
3613 | differ in size). | |
3614 | ||
ec1c14f6 RS |
3615 | START1, END1 are the character positions of the first region. |
3616 | START1_BYTE, END1_BYTE are the byte positions. | |
3617 | START2, END2 are the character positions of the second region. | |
3618 | START2_BYTE, END2_BYTE are the byte positions. | |
3619 | ||
b229b8d1 RS |
3620 | Traverses the entire marker list of the buffer to do so, adding an |
3621 | appropriate amount to some, subtracting from some, and leaving the | |
3622 | rest untouched. Most of this is copied from adjust_markers in insdel.c. | |
34a7a267 | 3623 | |
ec1c14f6 | 3624 | It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */ |
b229b8d1 | 3625 | |
acb7cc89 | 3626 | static void |
ec1c14f6 RS |
3627 | transpose_markers (start1, end1, start2, end2, |
3628 | start1_byte, end1_byte, start2_byte, end2_byte) | |
b229b8d1 | 3629 | register int start1, end1, start2, end2; |
ec1c14f6 | 3630 | register int start1_byte, end1_byte, start2_byte, end2_byte; |
b229b8d1 | 3631 | { |
ec1c14f6 | 3632 | register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos; |
b229b8d1 | 3633 | register Lisp_Object marker; |
b229b8d1 | 3634 | |
03240d11 | 3635 | /* Update point as if it were a marker. */ |
8de1d5f0 KH |
3636 | if (PT < start1) |
3637 | ; | |
3638 | else if (PT < end1) | |
ec1c14f6 RS |
3639 | TEMP_SET_PT_BOTH (PT + (end2 - end1), |
3640 | PT_BYTE + (end2_byte - end1_byte)); | |
8de1d5f0 | 3641 | else if (PT < start2) |
ec1c14f6 RS |
3642 | TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1), |
3643 | (PT_BYTE + (end2_byte - start2_byte) | |
3644 | - (end1_byte - start1_byte))); | |
8de1d5f0 | 3645 | else if (PT < end2) |
ec1c14f6 RS |
3646 | TEMP_SET_PT_BOTH (PT - (start2 - start1), |
3647 | PT_BYTE - (start2_byte - start1_byte)); | |
8de1d5f0 | 3648 | |
03240d11 KH |
3649 | /* We used to adjust the endpoints here to account for the gap, but that |
3650 | isn't good enough. Even if we assume the caller has tried to move the | |
3651 | gap out of our way, it might still be at start1 exactly, for example; | |
3652 | and that places it `inside' the interval, for our purposes. The amount | |
3653 | of adjustment is nontrivial if there's a `denormalized' marker whose | |
3654 | position is between GPT and GPT + GAP_SIZE, so it's simpler to leave | |
3655 | the dirty work to Fmarker_position, below. */ | |
b229b8d1 RS |
3656 | |
3657 | /* The difference between the region's lengths */ | |
3658 | diff = (end2 - start2) - (end1 - start1); | |
ec1c14f6 | 3659 | diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte); |
34a7a267 | 3660 | |
b229b8d1 | 3661 | /* For shifting each marker in a region by the length of the other |
ec1c14f6 | 3662 | region plus the distance between the regions. */ |
b229b8d1 RS |
3663 | amt1 = (end2 - start2) + (start2 - end1); |
3664 | amt2 = (end1 - start1) + (start2 - end1); | |
ec1c14f6 RS |
3665 | amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte); |
3666 | amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte); | |
b229b8d1 | 3667 | |
1e158d25 | 3668 | for (marker = BUF_MARKERS (current_buffer); !NILP (marker); |
03240d11 | 3669 | marker = XMARKER (marker)->chain) |
b229b8d1 | 3670 | { |
ec1c14f6 RS |
3671 | mpos = marker_byte_position (marker); |
3672 | if (mpos >= start1_byte && mpos < end2_byte) | |
3673 | { | |
3674 | if (mpos < end1_byte) | |
3675 | mpos += amt1_byte; | |
3676 | else if (mpos < start2_byte) | |
3677 | mpos += diff_byte; | |
3678 | else | |
3679 | mpos -= amt2_byte; | |
f3e1f752 | 3680 | XMARKER (marker)->bytepos = mpos; |
ec1c14f6 RS |
3681 | } |
3682 | mpos = XMARKER (marker)->charpos; | |
03240d11 KH |
3683 | if (mpos >= start1 && mpos < end2) |
3684 | { | |
3685 | if (mpos < end1) | |
3686 | mpos += amt1; | |
3687 | else if (mpos < start2) | |
3688 | mpos += diff; | |
3689 | else | |
3690 | mpos -= amt2; | |
03240d11 | 3691 | } |
ec1c14f6 | 3692 | XMARKER (marker)->charpos = mpos; |
b229b8d1 RS |
3693 | } |
3694 | } | |
3695 | ||
3696 | DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0, | |
7ee72033 | 3697 | doc: /* Transpose region START1 to END1 with START2 to END2. |
a1f17501 PJ |
3698 | The regions may not be overlapping, because the size of the buffer is |
3699 | never changed in a transposition. | |
3700 | ||
3701 | Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update | |
3702 | any markers that happen to be located in the regions. | |
3703 | ||
7ee72033 MB |
3704 | Transposing beyond buffer boundaries is an error. */) |
3705 | (startr1, endr1, startr2, endr2, leave_markers) | |
b229b8d1 RS |
3706 | Lisp_Object startr1, endr1, startr2, endr2, leave_markers; |
3707 | { | |
ec1c14f6 RS |
3708 | register int start1, end1, start2, end2; |
3709 | int start1_byte, start2_byte, len1_byte, len2_byte; | |
3710 | int gap, len1, len_mid, len2; | |
3c6bc7d0 | 3711 | unsigned char *start1_addr, *start2_addr, *temp; |
b229b8d1 | 3712 | |
b229b8d1 | 3713 | INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2; |
1e158d25 | 3714 | cur_intv = BUF_INTERVALS (current_buffer); |
b229b8d1 RS |
3715 | |
3716 | validate_region (&startr1, &endr1); | |
3717 | validate_region (&startr2, &endr2); | |
3718 | ||
3719 | start1 = XFASTINT (startr1); | |
3720 | end1 = XFASTINT (endr1); | |
3721 | start2 = XFASTINT (startr2); | |
3722 | end2 = XFASTINT (endr2); | |
3723 | gap = GPT; | |
3724 | ||
3725 | /* Swap the regions if they're reversed. */ | |
3726 | if (start2 < end1) | |
3727 | { | |
3728 | register int glumph = start1; | |
3729 | start1 = start2; | |
3730 | start2 = glumph; | |
3731 | glumph = end1; | |
3732 | end1 = end2; | |
3733 | end2 = glumph; | |
3734 | } | |
3735 | ||
b229b8d1 RS |
3736 | len1 = end1 - start1; |
3737 | len2 = end2 - start2; | |
3738 | ||
3739 | if (start2 < end1) | |
dc3620af | 3740 | error ("Transposed regions overlap"); |
b229b8d1 | 3741 | else if (start1 == end1 || start2 == end2) |
dc3620af | 3742 | error ("Transposed region has length 0"); |
b229b8d1 RS |
3743 | |
3744 | /* The possibilities are: | |
3745 | 1. Adjacent (contiguous) regions, or separate but equal regions | |
3746 | (no, really equal, in this case!), or | |
3747 | 2. Separate regions of unequal size. | |
34a7a267 | 3748 | |
b229b8d1 RS |
3749 | The worst case is usually No. 2. It means that (aside from |
3750 | potential need for getting the gap out of the way), there also | |
3751 | needs to be a shifting of the text between the two regions. So | |
3752 | if they are spread far apart, we are that much slower... sigh. */ | |
3753 | ||
3754 | /* It must be pointed out that the really studly thing to do would | |
3755 | be not to move the gap at all, but to leave it in place and work | |
3756 | around it if necessary. This would be extremely efficient, | |
3757 | especially considering that people are likely to do | |
3758 | transpositions near where they are working interactively, which | |
3759 | is exactly where the gap would be found. However, such code | |
3760 | would be much harder to write and to read. So, if you are | |
3761 | reading this comment and are feeling squirrely, by all means have | |
3762 | a go! I just didn't feel like doing it, so I will simply move | |
3763 | the gap the minimum distance to get it out of the way, and then | |
3764 | deal with an unbroken array. */ | |
3c6bc7d0 RS |
3765 | |
3766 | /* Make sure the gap won't interfere, by moving it out of the text | |
3767 | we will operate on. */ | |
3768 | if (start1 < gap && gap < end2) | |
3769 | { | |
3770 | if (gap - start1 < end2 - gap) | |
3771 | move_gap (start1); | |
3772 | else | |
3773 | move_gap (end2); | |
3774 | } | |
ec1c14f6 RS |
3775 | |
3776 | start1_byte = CHAR_TO_BYTE (start1); | |
3777 | start2_byte = CHAR_TO_BYTE (start2); | |
3778 | len1_byte = CHAR_TO_BYTE (end1) - start1_byte; | |
3779 | len2_byte = CHAR_TO_BYTE (end2) - start2_byte; | |
dc3620af | 3780 | |
9a599130 | 3781 | #ifdef BYTE_COMBINING_DEBUG |
dc3620af RS |
3782 | if (end1 == start2) |
3783 | { | |
9a599130 KH |
3784 | if (count_combining_before (BYTE_POS_ADDR (start2_byte), |
3785 | len2_byte, start1, start1_byte) | |
3786 | || count_combining_before (BYTE_POS_ADDR (start1_byte), | |
3787 | len1_byte, end2, start2_byte + len2_byte) | |
3788 | || count_combining_after (BYTE_POS_ADDR (start1_byte), | |
3789 | len1_byte, end2, start2_byte + len2_byte)) | |
3790 | abort (); | |
dc3620af RS |
3791 | } |
3792 | else | |
3793 | { | |
9a599130 KH |
3794 | if (count_combining_before (BYTE_POS_ADDR (start2_byte), |
3795 | len2_byte, start1, start1_byte) | |
3796 | || count_combining_before (BYTE_POS_ADDR (start1_byte), | |
3797 | len1_byte, start2, start2_byte) | |
3798 | || count_combining_after (BYTE_POS_ADDR (start2_byte), | |
3799 | len2_byte, end1, start1_byte + len1_byte) | |
3800 | || count_combining_after (BYTE_POS_ADDR (start1_byte), | |
3801 | len1_byte, end2, start2_byte + len2_byte)) | |
3802 | abort (); | |
dc3620af | 3803 | } |
9a599130 | 3804 | #endif |
dc3620af | 3805 | |
b229b8d1 RS |
3806 | /* Hmmm... how about checking to see if the gap is large |
3807 | enough to use as the temporary storage? That would avoid an | |
3808 | allocation... interesting. Later, don't fool with it now. */ | |
3809 | ||
3810 | /* Working without memmove, for portability (sigh), so must be | |
3811 | careful of overlapping subsections of the array... */ | |
3812 | ||
3813 | if (end1 == start2) /* adjacent regions */ | |
3814 | { | |
b229b8d1 RS |
3815 | modify_region (current_buffer, start1, end2); |
3816 | record_change (start1, len1 + len2); | |
3817 | ||
b229b8d1 RS |
3818 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
3819 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
3820 | Fset_text_properties (make_number (start1), make_number (end2), |
3821 | Qnil, Qnil); | |
b229b8d1 RS |
3822 | |
3823 | /* First region smaller than second. */ | |
ec1c14f6 | 3824 | if (len1_byte < len2_byte) |
b229b8d1 | 3825 | { |
3c6bc7d0 RS |
3826 | /* We use alloca only if it is small, |
3827 | because we want to avoid stack overflow. */ | |
ec1c14f6 RS |
3828 | if (len2_byte > 20000) |
3829 | temp = (unsigned char *) xmalloc (len2_byte); | |
3c6bc7d0 | 3830 | else |
ec1c14f6 | 3831 | temp = (unsigned char *) alloca (len2_byte); |
03240d11 KH |
3832 | |
3833 | /* Don't precompute these addresses. We have to compute them | |
3834 | at the last minute, because the relocating allocator might | |
3835 | have moved the buffer around during the xmalloc. */ | |
23017390 KH |
3836 | start1_addr = BYTE_POS_ADDR (start1_byte); |
3837 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
03240d11 | 3838 | |
ec1c14f6 RS |
3839 | bcopy (start2_addr, temp, len2_byte); |
3840 | bcopy (start1_addr, start1_addr + len2_byte, len1_byte); | |
3841 | bcopy (temp, start1_addr, len2_byte); | |
3842 | if (len2_byte > 20000) | |
afd74e25 | 3843 | xfree (temp); |
b229b8d1 RS |
3844 | } |
3845 | else | |
3846 | /* First region not smaller than second. */ | |
3847 | { | |
ec1c14f6 RS |
3848 | if (len1_byte > 20000) |
3849 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 3850 | else |
ec1c14f6 | 3851 | temp = (unsigned char *) alloca (len1_byte); |
23017390 KH |
3852 | start1_addr = BYTE_POS_ADDR (start1_byte); |
3853 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
ec1c14f6 RS |
3854 | bcopy (start1_addr, temp, len1_byte); |
3855 | bcopy (start2_addr, start1_addr, len2_byte); | |
3856 | bcopy (temp, start1_addr + len2_byte, len1_byte); | |
3857 | if (len1_byte > 20000) | |
afd74e25 | 3858 | xfree (temp); |
b229b8d1 | 3859 | } |
b229b8d1 RS |
3860 | graft_intervals_into_buffer (tmp_interval1, start1 + len2, |
3861 | len1, current_buffer, 0); | |
3862 | graft_intervals_into_buffer (tmp_interval2, start1, | |
3863 | len2, current_buffer, 0); | |
d5c2c403 KH |
3864 | update_compositions (start1, start1 + len2, CHECK_BORDER); |
3865 | update_compositions (start1 + len2, end2, CHECK_TAIL); | |
b229b8d1 RS |
3866 | } |
3867 | /* Non-adjacent regions, because end1 != start2, bleagh... */ | |
3868 | else | |
3869 | { | |
ec1c14f6 RS |
3870 | len_mid = start2_byte - (start1_byte + len1_byte); |
3871 | ||
3872 | if (len1_byte == len2_byte) | |
b229b8d1 RS |
3873 | /* Regions are same size, though, how nice. */ |
3874 | { | |
3875 | modify_region (current_buffer, start1, end1); | |
3876 | modify_region (current_buffer, start2, end2); | |
3877 | record_change (start1, len1); | |
3878 | record_change (start2, len2); | |
b229b8d1 RS |
3879 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
3880 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
3881 | Fset_text_properties (make_number (start1), make_number (end1), |
3882 | Qnil, Qnil); | |
3883 | Fset_text_properties (make_number (start2), make_number (end2), | |
3884 | Qnil, Qnil); | |
b229b8d1 | 3885 | |
ec1c14f6 RS |
3886 | if (len1_byte > 20000) |
3887 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 3888 | else |
ec1c14f6 | 3889 | temp = (unsigned char *) alloca (len1_byte); |
23017390 KH |
3890 | start1_addr = BYTE_POS_ADDR (start1_byte); |
3891 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
ec1c14f6 RS |
3892 | bcopy (start1_addr, temp, len1_byte); |
3893 | bcopy (start2_addr, start1_addr, len2_byte); | |
3894 | bcopy (temp, start2_addr, len1_byte); | |
3895 | if (len1_byte > 20000) | |
afd74e25 | 3896 | xfree (temp); |
b229b8d1 RS |
3897 | graft_intervals_into_buffer (tmp_interval1, start2, |
3898 | len1, current_buffer, 0); | |
3899 | graft_intervals_into_buffer (tmp_interval2, start1, | |
3900 | len2, current_buffer, 0); | |
b229b8d1 RS |
3901 | } |
3902 | ||
ec1c14f6 | 3903 | else if (len1_byte < len2_byte) /* Second region larger than first */ |
b229b8d1 RS |
3904 | /* Non-adjacent & unequal size, area between must also be shifted. */ |
3905 | { | |
b229b8d1 RS |
3906 | modify_region (current_buffer, start1, end2); |
3907 | record_change (start1, (end2 - start1)); | |
b229b8d1 RS |
3908 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
3909 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
3910 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
3911 | Fset_text_properties (make_number (start1), make_number (end2), |
3912 | Qnil, Qnil); | |
b229b8d1 | 3913 | |
3c6bc7d0 | 3914 | /* holds region 2 */ |
ec1c14f6 RS |
3915 | if (len2_byte > 20000) |
3916 | temp = (unsigned char *) xmalloc (len2_byte); | |
3c6bc7d0 | 3917 | else |
ec1c14f6 | 3918 | temp = (unsigned char *) alloca (len2_byte); |
23017390 KH |
3919 | start1_addr = BYTE_POS_ADDR (start1_byte); |
3920 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
ec1c14f6 RS |
3921 | bcopy (start2_addr, temp, len2_byte); |
3922 | bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte); | |
3923 | safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | |
3924 | bcopy (temp, start1_addr, len2_byte); | |
3925 | if (len2_byte > 20000) | |
afd74e25 | 3926 | xfree (temp); |
b229b8d1 RS |
3927 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
3928 | len1, current_buffer, 0); | |
3929 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
3930 | len_mid, current_buffer, 0); | |
3931 | graft_intervals_into_buffer (tmp_interval2, start1, | |
3932 | len2, current_buffer, 0); | |
b229b8d1 RS |
3933 | } |
3934 | else | |
3935 | /* Second region smaller than first. */ | |
3936 | { | |
b229b8d1 RS |
3937 | record_change (start1, (end2 - start1)); |
3938 | modify_region (current_buffer, start1, end2); | |
3939 | ||
b229b8d1 RS |
3940 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
3941 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
3942 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
3943 | Fset_text_properties (make_number (start1), make_number (end2), |
3944 | Qnil, Qnil); | |
b229b8d1 | 3945 | |
3c6bc7d0 | 3946 | /* holds region 1 */ |
ec1c14f6 RS |
3947 | if (len1_byte > 20000) |
3948 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 3949 | else |
ec1c14f6 | 3950 | temp = (unsigned char *) alloca (len1_byte); |
23017390 KH |
3951 | start1_addr = BYTE_POS_ADDR (start1_byte); |
3952 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
ec1c14f6 RS |
3953 | bcopy (start1_addr, temp, len1_byte); |
3954 | bcopy (start2_addr, start1_addr, len2_byte); | |
3955 | bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | |
3956 | bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte); | |
3957 | if (len1_byte > 20000) | |
afd74e25 | 3958 | xfree (temp); |
b229b8d1 RS |
3959 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
3960 | len1, current_buffer, 0); | |
3961 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
3962 | len_mid, current_buffer, 0); | |
3963 | graft_intervals_into_buffer (tmp_interval2, start1, | |
3964 | len2, current_buffer, 0); | |
b229b8d1 | 3965 | } |
d5c2c403 KH |
3966 | |
3967 | update_compositions (start1, start1 + len2, CHECK_BORDER); | |
3968 | update_compositions (end2 - len1, end2, CHECK_BORDER); | |
b229b8d1 RS |
3969 | } |
3970 | ||
ec1c14f6 RS |
3971 | /* When doing multiple transpositions, it might be nice |
3972 | to optimize this. Perhaps the markers in any one buffer | |
3973 | should be organized in some sorted data tree. */ | |
b229b8d1 | 3974 | if (NILP (leave_markers)) |
8de1d5f0 | 3975 | { |
ec1c14f6 RS |
3976 | transpose_markers (start1, end1, start2, end2, |
3977 | start1_byte, start1_byte + len1_byte, | |
3978 | start2_byte, start2_byte + len2_byte); | |
8de1d5f0 KH |
3979 | fix_overlays_in_range (start1, end2); |
3980 | } | |
b229b8d1 RS |
3981 | |
3982 | return Qnil; | |
3983 | } | |
35692fe0 | 3984 | |
35692fe0 JB |
3985 | \f |
3986 | void | |
3987 | syms_of_editfns () | |
3988 | { | |
260e2e2a KH |
3989 | environbuf = 0; |
3990 | ||
3991 | Qbuffer_access_fontify_functions | |
3992 | = intern ("buffer-access-fontify-functions"); | |
3993 | staticpro (&Qbuffer_access_fontify_functions); | |
3994 | ||
7ee72033 MB |
3995 | DEFVAR_LISP ("inhibit-field-text-motion", &Vinhibit_field_text_motion, |
3996 | doc: /* Non-nil means.text motion commands don't notice fields. */); | |
9a74e7e5 GM |
3997 | Vinhibit_field_text_motion = Qnil; |
3998 | ||
260e2e2a | 3999 | DEFVAR_LISP ("buffer-access-fontify-functions", |
7ee72033 MB |
4000 | &Vbuffer_access_fontify_functions, |
4001 | doc: /* List of functions called by `buffer-substring' to fontify if necessary. | |
a1f17501 PJ |
4002 | Each function is called with two arguments which specify the range |
4003 | of the buffer being accessed. */); | |
260e2e2a KH |
4004 | Vbuffer_access_fontify_functions = Qnil; |
4005 | ||
af209db8 RS |
4006 | { |
4007 | Lisp_Object obuf; | |
4008 | extern Lisp_Object Vprin1_to_string_buffer; | |
4009 | obuf = Fcurrent_buffer (); | |
4010 | /* Do this here, because init_buffer_once is too early--it won't work. */ | |
4011 | Fset_buffer (Vprin1_to_string_buffer); | |
4012 | /* Make sure buffer-access-fontify-functions is nil in this buffer. */ | |
4013 | Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")), | |
4014 | Qnil); | |
4015 | Fset_buffer (obuf); | |
4016 | } | |
4017 | ||
0b6fd023 | 4018 | DEFVAR_LISP ("buffer-access-fontified-property", |
7ee72033 MB |
4019 | &Vbuffer_access_fontified_property, |
4020 | doc: /* Property which (if non-nil) indicates text has been fontified. | |
a1f17501 PJ |
4021 | `buffer-substring' need not call the `buffer-access-fontify-functions' |
4022 | functions if all the text being accessed has this property. */); | |
260e2e2a KH |
4023 | Vbuffer_access_fontified_property = Qnil; |
4024 | ||
7ee72033 MB |
4025 | DEFVAR_LISP ("system-name", &Vsystem_name, |
4026 | doc: /* The name of the machine Emacs is running on. */); | |
34a7a267 | 4027 | |
7ee72033 MB |
4028 | DEFVAR_LISP ("user-full-name", &Vuser_full_name, |
4029 | doc: /* The full name of the user logged in. */); | |
f43754f6 | 4030 | |
7ee72033 MB |
4031 | DEFVAR_LISP ("user-login-name", &Vuser_login_name, |
4032 | doc: /* The user's name, taken from environment variables if possible. */); | |
f43754f6 | 4033 | |
7ee72033 MB |
4034 | DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name, |
4035 | doc: /* The user's name, based upon the real uid only. */); | |
35692fe0 | 4036 | |
0963334d | 4037 | defsubr (&Spropertize); |
35692fe0 JB |
4038 | defsubr (&Schar_equal); |
4039 | defsubr (&Sgoto_char); | |
4040 | defsubr (&Sstring_to_char); | |
4041 | defsubr (&Schar_to_string); | |
4042 | defsubr (&Sbuffer_substring); | |
260e2e2a | 4043 | defsubr (&Sbuffer_substring_no_properties); |
35692fe0 JB |
4044 | defsubr (&Sbuffer_string); |
4045 | ||
4046 | defsubr (&Spoint_marker); | |
4047 | defsubr (&Smark_marker); | |
4048 | defsubr (&Spoint); | |
4049 | defsubr (&Sregion_beginning); | |
4050 | defsubr (&Sregion_end); | |
7df74da6 | 4051 | |
0daf6e8d GM |
4052 | staticpro (&Qfield); |
4053 | Qfield = intern ("field"); | |
ee547125 MB |
4054 | staticpro (&Qboundary); |
4055 | Qboundary = intern ("boundary"); | |
0daf6e8d GM |
4056 | defsubr (&Sfield_beginning); |
4057 | defsubr (&Sfield_end); | |
4058 | defsubr (&Sfield_string); | |
4059 | defsubr (&Sfield_string_no_properties); | |
8bf64fe8 | 4060 | defsubr (&Sdelete_field); |
0daf6e8d GM |
4061 | defsubr (&Sconstrain_to_field); |
4062 | ||
7df74da6 RS |
4063 | defsubr (&Sline_beginning_position); |
4064 | defsubr (&Sline_end_position); | |
4065 | ||
35692fe0 JB |
4066 | /* defsubr (&Smark); */ |
4067 | /* defsubr (&Sset_mark); */ | |
4068 | defsubr (&Ssave_excursion); | |
4bc8c7d2 | 4069 | defsubr (&Ssave_current_buffer); |
35692fe0 JB |
4070 | |
4071 | defsubr (&Sbufsize); | |
4072 | defsubr (&Spoint_max); | |
4073 | defsubr (&Spoint_min); | |
4074 | defsubr (&Spoint_min_marker); | |
4075 | defsubr (&Spoint_max_marker); | |
c86212b9 RS |
4076 | defsubr (&Sgap_position); |
4077 | defsubr (&Sgap_size); | |
7df74da6 | 4078 | defsubr (&Sposition_bytes); |
3ab0732d | 4079 | defsubr (&Sbyte_to_position); |
c9ed721d | 4080 | |
35692fe0 JB |
4081 | defsubr (&Sbobp); |
4082 | defsubr (&Seobp); | |
4083 | defsubr (&Sbolp); | |
4084 | defsubr (&Seolp); | |
850a8179 JB |
4085 | defsubr (&Sfollowing_char); |
4086 | defsubr (&Sprevious_char); | |
35692fe0 | 4087 | defsubr (&Schar_after); |
fb8106e8 | 4088 | defsubr (&Schar_before); |
35692fe0 JB |
4089 | defsubr (&Sinsert); |
4090 | defsubr (&Sinsert_before_markers); | |
be91036a RS |
4091 | defsubr (&Sinsert_and_inherit); |
4092 | defsubr (&Sinsert_and_inherit_before_markers); | |
35692fe0 JB |
4093 | defsubr (&Sinsert_char); |
4094 | ||
4095 | defsubr (&Suser_login_name); | |
4096 | defsubr (&Suser_real_login_name); | |
4097 | defsubr (&Suser_uid); | |
4098 | defsubr (&Suser_real_uid); | |
4099 | defsubr (&Suser_full_name); | |
7fd233b3 | 4100 | defsubr (&Semacs_pid); |
d940e0e4 | 4101 | defsubr (&Scurrent_time); |
a82d387c | 4102 | defsubr (&Sformat_time_string); |
34a7a267 | 4103 | defsubr (&Sfloat_time); |
4691c06d | 4104 | defsubr (&Sdecode_time); |
cce7b8a0 | 4105 | defsubr (&Sencode_time); |
35692fe0 | 4106 | defsubr (&Scurrent_time_string); |
c2662aea | 4107 | defsubr (&Scurrent_time_zone); |
143cb9a9 | 4108 | defsubr (&Sset_time_zone_rule); |
35692fe0 | 4109 | defsubr (&Ssystem_name); |
35692fe0 | 4110 | defsubr (&Smessage); |
cacc3e2c RS |
4111 | defsubr (&Smessage_box); |
4112 | defsubr (&Smessage_or_box); | |
b14dda8a | 4113 | defsubr (&Scurrent_message); |
35692fe0 | 4114 | defsubr (&Sformat); |
35692fe0 JB |
4115 | |
4116 | defsubr (&Sinsert_buffer_substring); | |
e9cf2084 | 4117 | defsubr (&Scompare_buffer_substrings); |
35692fe0 JB |
4118 | defsubr (&Ssubst_char_in_region); |
4119 | defsubr (&Stranslate_region); | |
4120 | defsubr (&Sdelete_region); | |
7dae4502 | 4121 | defsubr (&Sdelete_and_extract_region); |
35692fe0 JB |
4122 | defsubr (&Swiden); |
4123 | defsubr (&Snarrow_to_region); | |
4124 | defsubr (&Ssave_restriction); | |
b229b8d1 | 4125 | defsubr (&Stranspose_regions); |
35692fe0 | 4126 | } |