]> code.delx.au - gnu-emacs/blob - src/editfns.c
(command-line-processed): Doc fix.
[gnu-emacs] / src / editfns.c
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98, 1999, 2000, 2001
3 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 2, or (at your option)
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
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <sys/types.h>
25
26 #ifdef VMS
27 #include "vms-pwd.h"
28 #else
29 #include <pwd.h>
30 #endif
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include <ctype.h>
37
38 #include "lisp.h"
39 #include "intervals.h"
40 #include "buffer.h"
41 #include "charset.h"
42 #include "coding.h"
43 #include "frame.h"
44 #include "window.h"
45
46 #include "systime.h"
47
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
55 #ifndef NULL
56 #define NULL 0
57 #endif
58
59 #ifndef USE_CRT_DLL
60 extern char **environ;
61 #endif
62
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 *));
67 static void find_field P_ ((Lisp_Object, Lisp_Object, Lisp_Object, int *, Lisp_Object, int *));
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));
80
81 #ifdef HAVE_INDEX
82 extern char *index P_ ((const char *, int));
83 #endif
84
85 Lisp_Object Vbuffer_access_fontify_functions;
86 Lisp_Object Qbuffer_access_fontify_functions;
87 Lisp_Object Vbuffer_access_fontified_property;
88
89 Lisp_Object Fuser_full_name P_ ((Lisp_Object));
90
91 /* Non-nil means don't stop at field boundary in text motion commands. */
92
93 Lisp_Object Vinhibit_field_text_motion;
94
95 /* Some static data, and a function to initialize it for each run */
96
97 Lisp_Object Vsystem_name;
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 */
101
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
111 void
112 init_editfns ()
113 {
114 char *user_name;
115 register unsigned char *p;
116 struct passwd *pw; /* password entry for the current user */
117 Lisp_Object tem;
118
119 /* Set up system_name even when dumping. */
120 init_system_name ();
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 ());
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.) */
133 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
134 #else
135 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
136 #endif
137
138 /* Get the effective user name, by consulting environment variables,
139 or the effective uid if those are unset. */
140 user_name = (char *) getenv ("LOGNAME");
141 if (!user_name)
142 #ifdef WINDOWSNT
143 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
144 #else /* WINDOWSNT */
145 user_name = (char *) getenv ("USER");
146 #endif /* WINDOWSNT */
147 if (!user_name)
148 {
149 pw = (struct passwd *) getpwuid (geteuid ());
150 user_name = (char *) (pw ? pw->pw_name : "unknown");
151 }
152 Vuser_login_name = build_string (user_name);
153
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. */
156 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
157 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
158 : Vuser_login_name);
159
160 p = (unsigned char *) getenv ("NAME");
161 if (p)
162 Vuser_full_name = build_string (p);
163 else if (NILP (Vuser_full_name))
164 Vuser_full_name = build_string ("unknown");
165 }
166 \f
167 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
168 doc: /* Convert arg CHAR to a string containing that character.
169 usage: (char-to-string CHAR) */)
170 (character)
171 Lisp_Object character;
172 {
173 int len;
174 unsigned char str[MAX_MULTIBYTE_LENGTH];
175
176 CHECK_NUMBER (character);
177
178 len = (SINGLE_BYTE_CHAR_P (XFASTINT (character))
179 ? (*str = (unsigned char)(XFASTINT (character)), 1)
180 : char_to_string (XFASTINT (character), str));
181 return make_string_from_bytes (str, 1, len);
182 }
183
184 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
185 doc: /* Convert arg STRING to a character, the first character of that string.
186 A multibyte character is handled correctly. */)
187 (string)
188 register Lisp_Object string;
189 {
190 register Lisp_Object val;
191 register struct Lisp_String *p;
192 CHECK_STRING (string);
193 p = XSTRING (string);
194 if (p->size)
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 }
201 else
202 XSETFASTINT (val, 0);
203 return val;
204 }
205 \f
206 static Lisp_Object
207 buildmark (charpos, bytepos)
208 int charpos, bytepos;
209 {
210 register Lisp_Object mark;
211 mark = Fmake_marker ();
212 set_marker_both (mark, Qnil, charpos, bytepos);
213 return mark;
214 }
215
216 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
217 doc: /* Return value of point, as an integer.
218 Beginning of buffer is position (point-min). */)
219 ()
220 {
221 Lisp_Object temp;
222 XSETFASTINT (temp, PT);
223 return temp;
224 }
225
226 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
227 doc: /* Return value of point, as a marker object. */)
228 ()
229 {
230 return buildmark (PT, PT_BYTE);
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: ",
246 doc: /* Set point to POSITION, a number or marker.
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
250 except in the case that `enable-multibyte-characters' is nil. */)
251 (position)
252 register Lisp_Object position;
253 {
254 int pos;
255
256 if (MARKERP (position)
257 && current_buffer == XMARKER (position)->buffer)
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
270 CHECK_NUMBER_COERCE_MARKER (position);
271
272 pos = clip_to_bounds (BEGV, XINT (position), ZV);
273 SET_PT (pos);
274 return position;
275 }
276
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
282 static Lisp_Object
283 region_limit (beginningp)
284 int beginningp;
285 {
286 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
287 Lisp_Object m;
288
289 if (!NILP (Vtransient_mark_mode)
290 && NILP (Vmark_even_if_inactive)
291 && NILP (current_buffer->mark_active))
292 Fsignal (Qmark_inactive, Qnil);
293
294 m = Fmarker_position (current_buffer->mark);
295 if (NILP (m))
296 error ("There is no region now");
297
298 if ((PT < XFASTINT (m)) == beginningp)
299 m = make_number (PT);
300 return m;
301 }
302
303 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
304 doc: /* Return position of beginning of region, as an integer. */)
305 ()
306 {
307 return region_limit (1);
308 }
309
310 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
311 doc: /* Return position of end of region, as an integer. */)
312 ()
313 {
314 return region_limit (0);
315 }
316
317 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
318 doc: /* Return this buffer's mark, as a marker object.
319 Watch out! Moving this marker changes the mark position.
320 If you set the marker not to point anywhere, the buffer will have no mark. */)
321 ()
322 {
323 return current_buffer->mark;
324 }
325
326 \f
327 #if 0 /* Not used. */
328
329 /* Return nonzero if POS1 and POS2 have the same value
330 for the text property PROP. */
331
332 static int
333 char_property_eq (prop, pos1, pos2)
334 Lisp_Object prop;
335 Lisp_Object pos1, pos2;
336 {
337 Lisp_Object pval1, pval2;
338
339 pval1 = Fget_char_property (pos1, prop, Qnil);
340 pval2 = Fget_char_property (pos2, prop, Qnil);
341
342 return EQ (pval1, pval2);
343 }
344
345 #endif /* 0 */
346
347 /* Return the direction from which the text-property PROP would be
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
352 static int
353 text_property_stickiness (prop, pos)
354 Lisp_Object prop;
355 Lisp_Object pos;
356 {
357 Lisp_Object prev_pos, front_sticky;
358 int is_rear_sticky = 1, is_front_sticky = 0; /* defaults */
359
360 if (XINT (pos) > BEGV)
361 /* Consider previous character. */
362 {
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
368 if (!NILP (CONSP (rear_non_sticky)
369 ? Fmemq (prop, rear_non_sticky)
370 : rear_non_sticky))
371 /* PROP is rear-non-sticky. */
372 is_rear_sticky = 0;
373 }
374
375 /* Consider following character. */
376 front_sticky = Fget_text_property (pos, Qfront_sticky, Qnil);
377
378 if (EQ (front_sticky, Qt)
379 || (CONSP (front_sticky)
380 && !NILP (Fmemq (prop, front_sticky))))
381 /* PROP is inherited from after. */
382 is_front_sticky = 1;
383
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;
400 }
401
402 \f
403 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
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.
406
407 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
408 results; they do not effect boundary behavior.
409
410 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
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.
419
420 Either BEG or END may be 0, in which case the corresponding value
421 is not stored. */
422
423 static void
424 find_field (pos, merge_at_boundary, beg_limit, beg, end_limit, end)
425 Lisp_Object pos;
426 Lisp_Object merge_at_boundary;
427 Lisp_Object beg_limit, end_limit;
428 int *beg, *end;
429 {
430 /* Fields right before and after the point. */
431 Lisp_Object before_field, after_field;
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;
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;
439
440 if (NILP (pos))
441 XSETFASTINT (pos, PT);
442 else
443 CHECK_NUMBER_COERCE_MARKER (pos);
444
445 after_field
446 = get_char_property_and_overlay (pos, Qfield, Qnil, &after_overlay);
447 before_field
448 = (XFASTINT (pos) > BEGV
449 ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
450 Qfield, Qnil,
451 &before_overlay)
452 : Qnil);
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 {
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;
477 else if (aop
478 && XMARKER (OVERLAY_START (after_overlay))->insertion_type == 0)
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);
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"). */
510 {
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. */
515 }
516 }
517
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
540 if (beg)
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. */
551 pos = Fprevious_single_char_property_change (pos, Qfield, Qnil,
552 beg_limit);
553
554 pos = Fprevious_single_char_property_change (pos, Qfield, Qnil,
555 beg_limit);
556 *beg = NILP (pos) ? BEGV : XFASTINT (pos);
557 }
558 }
559
560 if (end)
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. */
571 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
572 end_limit);
573
574 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
575 end_limit);
576 *end = NILP (pos) ? ZV : XFASTINT (pos);
577 }
578 }
579 }
580
581 \f
582 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
583 doc: /* Delete the field surrounding POS.
584 A field is a region of text with the same `field' property.
585 If POS is nil, the value of point is used for POS. */)
586 (pos)
587 Lisp_Object pos;
588 {
589 int beg, end;
590 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
591 if (beg != end)
592 del_range (beg, end);
593 return Qnil;
594 }
595
596 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
597 doc: /* Return the contents of the field surrounding POS as a string.
598 A field is a region of text with the same `field' property.
599 If POS is nil, the value of point is used for POS. */)
600 (pos)
601 Lisp_Object pos;
602 {
603 int beg, end;
604 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
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,
609 doc: /* Return the contents of the field around POS, without text-properties.
610 A field is a region of text with the same `field' property.
611 If POS is nil, the value of point is used for POS. */)
612 (pos)
613 Lisp_Object pos;
614 {
615 int beg, end;
616 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
617 return make_buffer_string (beg, end, 0);
618 }
619
620 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
621 doc: /* Return the beginning of the field surrounding POS.
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
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;
630 {
631 int beg;
632 find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
633 return make_number (beg);
634 }
635
636 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
637 doc: /* Return the end of the field surrounding POS.
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,
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;
646 {
647 int end;
648 find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
649 return make_number (end);
650 }
651
652 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
653 doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
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
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)
680 Lisp_Object new_pos, old_pos;
681 Lisp_Object escape_from_edge, only_in_line, inhibit_capture_property;
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
693 if (NILP (Vinhibit_field_text_motion)
694 && !EQ (new_pos, old_pos)
695 && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
696 || !NILP (Fget_char_property (old_pos, Qfield, Qnil)))
697 && (NILP (inhibit_capture_property)
698 || NILP (Fget_char_property(old_pos, inhibit_capture_property, Qnil))))
699 /* NEW_POS is not within the same field as OLD_POS; try to
700 move NEW_POS so that it is. */
701 {
702 int fwd, shortage;
703 Lisp_Object field_bound;
704
705 CHECK_NUMBER_COERCE_MARKER (new_pos);
706 CHECK_NUMBER_COERCE_MARKER (old_pos);
707
708 fwd = (XFASTINT (new_pos) > XFASTINT (old_pos));
709
710 if (fwd)
711 field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
712 else
713 field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
714
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)))
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 }
742
743 \f
744 DEFUN ("line-beginning-position",
745 Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
746 doc: /* Return the character position of the first character on the current line.
747 With argument N not nil or 1, move forward N - 1 lines first.
748 If scan reaches end of buffer, return that position.
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.
754
755 This function does not move point. */)
756 (n)
757 Lisp_Object n;
758 {
759 int orig, orig_byte, end;
760
761 if (NILP (n))
762 XSETFASTINT (n, 1);
763 else
764 CHECK_NUMBER (n);
765
766 orig = PT;
767 orig_byte = PT_BYTE;
768 Fforward_line (make_number (XINT (n) - 1));
769 end = PT;
770
771 SET_PT_BOTH (orig, orig_byte);
772
773 /* Return END constrained to the current input field. */
774 return Fconstrain_to_field (make_number (end), make_number (orig),
775 XINT (n) != 1 ? Qt : Qnil,
776 Qt, Qnil);
777 }
778
779 DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0,
780 doc: /* Return the character position of the last character on the current line.
781 With argument N not nil or 1, move forward N - 1 lines first.
782 If scan reaches end of buffer, return that position.
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
789 This function does not move point. */)
790 (n)
791 Lisp_Object n;
792 {
793 int end_pos;
794 int orig = PT;
795
796 if (NILP (n))
797 XSETFASTINT (n, 1);
798 else
799 CHECK_NUMBER (n);
800
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. */
804 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
805 Qnil, Qt, Qnil);
806 }
807
808 \f
809 Lisp_Object
810 save_excursion_save ()
811 {
812 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
813 == current_buffer);
814
815 return Fcons (Fpoint_marker (),
816 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
817 Fcons (visible ? Qt : Qnil,
818 Fcons (current_buffer->mark_active,
819 selected_window))));
820 }
821
822 Lisp_Object
823 save_excursion_restore (info)
824 Lisp_Object info;
825 {
826 Lisp_Object tem, tem1, omark, nmark;
827 struct gcpro gcpro1, gcpro2, gcpro3;
828 int visible_p;
829
830 tem = Fmarker_buffer (XCAR (info));
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. */
835 if (NILP (tem))
836 return Qnil;
837
838 omark = nmark = Qnil;
839 GCPRO3 (info, omark, nmark);
840
841 Fset_buffer (tem);
842
843 /* Point marker. */
844 tem = XCAR (info);
845 Fgoto_char (tem);
846 unchain_marker (tem);
847
848 /* Mark marker. */
849 info = XCDR (info);
850 tem = XCAR (info);
851 omark = Fmarker_position (current_buffer->mark);
852 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
853 nmark = Fmarker_position (tem);
854 unchain_marker (tem);
855
856 /* visible */
857 info = XCDR (info);
858 visible_p = !NILP (XCAR (info));
859
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. */
864 tem1 = Fcar (tem);
865 if (!NILP (tem1)
866 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
867 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
868 #endif /* 0 */
869
870 /* Mark active */
871 info = XCDR (info);
872 tem = XCAR (info);
873 tem1 = current_buffer->mark_active;
874 current_buffer->mark_active = tem;
875
876 if (!NILP (Vrun_hooks))
877 {
878 /* If mark is active now, and either was not active
879 or was at a different place, run the activate hook. */
880 if (! NILP (current_buffer->mark_active))
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. */
886 else if (! NILP (tem1))
887 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
888 }
889
890 /* If buffer was visible in a window, and a different window was
891 selected, and the old selected window is still showing this
892 buffer, restore point in that window. */
893 tem = XCDR (info);
894 if (visible_p
895 && !EQ (tem, selected_window)
896 && (tem1 = XWINDOW (tem)->buffer,
897 (/* Window is live... */
898 BUFFERP (tem1)
899 /* ...and it shows the current buffer. */
900 && XBUFFER (tem1) == current_buffer)))
901 Fset_window_point (tem, make_number (PT));
902
903 UNGCPRO;
904 return Qnil;
905 }
906
907 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
908 doc: /* Save point, mark, and current buffer; execute BODY; restore those things.
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
917 `deactivate-mark' with `let'.
918
919 usage: (save-excursion &rest BODY) */)
920 (args)
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 ());
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,
933 doc: /* Save the current buffer; execute BODY; restore the current buffer.
934 Executes BODY just like `progn'.
935 usage: (save-current-buffer &rest BODY) */)
936 (args)
937 Lisp_Object args;
938 {
939 Lisp_Object val;
940 int count = specpdl_ptr - specpdl;
941
942 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
943
944 val = Fprogn (args);
945 return unbind_to (count, val);
946 }
947 \f
948 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
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)
952 Lisp_Object buffer;
953 {
954 if (NILP (buffer))
955 return make_number (Z - BEG);
956 else
957 {
958 CHECK_BUFFER (buffer);
959 return make_number (BUF_Z (XBUFFER (buffer))
960 - BUF_BEG (XBUFFER (buffer)));
961 }
962 }
963
964 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
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 ()
968 {
969 Lisp_Object temp;
970 XSETFASTINT (temp, BEGV);
971 return temp;
972 }
973
974 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
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 ()
978 {
979 return buildmark (BEGV, BEGV_BYTE);
980 }
981
982 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
983 doc: /* Return the maximum permissible value of point in the current buffer.
984 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
985 is in effect, in which case it is less. */)
986 ()
987 {
988 Lisp_Object temp;
989 XSETFASTINT (temp, ZV);
990 return temp;
991 }
992
993 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
994 doc: /* Return a marker to the maximum permissible value of point in this buffer.
995 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
996 is in effect, in which case it is less. */)
997 ()
998 {
999 return buildmark (ZV, ZV_BYTE);
1000 }
1001
1002 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
1003 doc: /* Return the position of the gap, in the current buffer.
1004 See also `gap-size'. */)
1005 ()
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,
1013 doc: /* Return the size of the current buffer's gap.
1014 See also `gap-position'. */)
1015 ()
1016 {
1017 Lisp_Object temp;
1018 XSETFASTINT (temp, GAP_SIZE);
1019 return temp;
1020 }
1021
1022 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
1023 doc: /* Return the byte position for character position POSITION.
1024 If POSITION is out of range, the value is nil. */)
1025 (position)
1026 Lisp_Object position;
1027 {
1028 CHECK_NUMBER_COERCE_MARKER (position);
1029 if (XINT (position) < BEG || XINT (position) > Z)
1030 return Qnil;
1031 return make_number (CHAR_TO_BYTE (XINT (position)));
1032 }
1033
1034 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
1035 doc: /* Return the character position for byte position BYTEPOS.
1036 If BYTEPOS is out of range, the value is nil. */)
1037 (bytepos)
1038 Lisp_Object bytepos;
1039 {
1040 CHECK_NUMBER (bytepos);
1041 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
1042 return Qnil;
1043 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
1044 }
1045 \f
1046 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
1047 doc: /* Return the character following point, as a number.
1048 At the end of the buffer or accessible region, return 0. */)
1049 ()
1050 {
1051 Lisp_Object temp;
1052 if (PT >= ZV)
1053 XSETFASTINT (temp, 0);
1054 else
1055 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
1056 return temp;
1057 }
1058
1059 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
1060 doc: /* Return the character preceding point, as a number.
1061 At the beginning of the buffer or accessible region, return 0. */)
1062 ()
1063 {
1064 Lisp_Object temp;
1065 if (PT <= BEGV)
1066 XSETFASTINT (temp, 0);
1067 else if (!NILP (current_buffer->enable_multibyte_characters))
1068 {
1069 int pos = PT_BYTE;
1070 DEC_POS (pos);
1071 XSETFASTINT (temp, FETCH_CHAR (pos));
1072 }
1073 else
1074 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
1075 return temp;
1076 }
1077
1078 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
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 ()
1082 {
1083 if (PT == BEGV)
1084 return Qt;
1085 return Qnil;
1086 }
1087
1088 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
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 ()
1092 {
1093 if (PT == ZV)
1094 return Qt;
1095 return Qnil;
1096 }
1097
1098 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
1099 doc: /* Return t if point is at the beginning of a line. */)
1100 ()
1101 {
1102 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
1103 return Qt;
1104 return Qnil;
1105 }
1106
1107 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
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 ()
1111 {
1112 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
1113 return Qt;
1114 return Qnil;
1115 }
1116
1117 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
1118 doc: /* Return character in current buffer at position POS.
1119 POS is an integer or a marker.
1120 If POS is out of range, the value is nil. */)
1121 (pos)
1122 Lisp_Object pos;
1123 {
1124 register int pos_byte;
1125
1126 if (NILP (pos))
1127 {
1128 pos_byte = PT_BYTE;
1129 XSETFASTINT (pos, PT);
1130 }
1131
1132 if (MARKERP (pos))
1133 {
1134 pos_byte = marker_byte_position (pos);
1135 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1136 return Qnil;
1137 }
1138 else
1139 {
1140 CHECK_NUMBER_COERCE_MARKER (pos);
1141 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1142 return Qnil;
1143
1144 pos_byte = CHAR_TO_BYTE (XINT (pos));
1145 }
1146
1147 return make_number (FETCH_CHAR (pos_byte));
1148 }
1149
1150 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1151 doc: /* Return character in current buffer preceding position POS.
1152 POS is an integer or a marker.
1153 If POS is out of range, the value is nil. */)
1154 (pos)
1155 Lisp_Object pos;
1156 {
1157 register Lisp_Object val;
1158 register int pos_byte;
1159
1160 if (NILP (pos))
1161 {
1162 pos_byte = PT_BYTE;
1163 XSETFASTINT (pos, PT);
1164 }
1165
1166 if (MARKERP (pos))
1167 {
1168 pos_byte = marker_byte_position (pos);
1169
1170 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1171 return Qnil;
1172 }
1173 else
1174 {
1175 CHECK_NUMBER_COERCE_MARKER (pos);
1176
1177 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1178 return Qnil;
1179
1180 pos_byte = CHAR_TO_BYTE (XINT (pos));
1181 }
1182
1183 if (!NILP (current_buffer->enable_multibyte_characters))
1184 {
1185 DEC_POS (pos_byte);
1186 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1187 }
1188 else
1189 {
1190 pos_byte--;
1191 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1192 }
1193 return val;
1194 }
1195 \f
1196 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1197 doc: /* Return the name under which the user logged in, as a string.
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
1203 with that uid, or nil if there is no such user. */)
1204 (uid)
1205 Lisp_Object uid;
1206 {
1207 struct passwd *pw;
1208
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. */
1212 if (INTEGERP (Vuser_login_name))
1213 init_editfns ();
1214
1215 if (NILP (uid))
1216 return Vuser_login_name;
1217
1218 CHECK_NUMBER (uid);
1219 pw = (struct passwd *) getpwuid (XINT (uid));
1220 return (pw ? build_string (pw->pw_name) : Qnil);
1221 }
1222
1223 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1224 0, 0, 0,
1225 doc: /* Return the name of the user's real uid, as a string.
1226 This ignores the environment variables LOGNAME and USER, so it differs from
1227 `user-login-name' when running under `su'. */)
1228 ()
1229 {
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. */
1233 if (INTEGERP (Vuser_login_name))
1234 init_editfns ();
1235 return Vuser_real_login_name;
1236 }
1237
1238 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1239 doc: /* Return the effective uid of Emacs.
1240 Value is an integer or float, depending on the value. */)
1241 ()
1242 {
1243 return make_fixnum_or_float (geteuid ());
1244 }
1245
1246 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1247 doc: /* Return the real uid of Emacs.
1248 Value is an integer or float, depending on the value. */)
1249 ()
1250 {
1251 return make_fixnum_or_float (getuid ());
1252 }
1253
1254 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1255 doc: /* Return the full name of the user logged in, as a string.
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
1262 name, or nil if there is no such user. */)
1263 (uid)
1264 Lisp_Object uid;
1265 {
1266 struct passwd *pw;
1267 register unsigned char *p, *q;
1268 Lisp_Object full;
1269
1270 if (NILP (uid))
1271 return Vuser_full_name;
1272 else if (NUMBERP (uid))
1273 pw = (struct passwd *) getpwuid ((uid_t) XFLOATINT (uid));
1274 else if (STRINGP (uid))
1275 pw = (struct passwd *) getpwnam (XSTRING (uid)->data);
1276 else
1277 error ("Invalid UID specification");
1278
1279 if (!pw)
1280 return Qnil;
1281
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));
1286
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 {
1293 register unsigned char *r;
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;
1308 }
1309
1310 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1311 doc: /* Return the name of the machine you are running on, as a string. */)
1312 ()
1313 {
1314 return Vsystem_name;
1315 }
1316
1317 /* For the benefit of callers who don't want to include lisp.h */
1318
1319 char *
1320 get_system_name ()
1321 {
1322 if (STRINGP (Vsystem_name))
1323 return (char *) XSTRING (Vsystem_name)->data;
1324 else
1325 return "";
1326 }
1327
1328 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1329 doc: /* Return the process ID of Emacs, as an integer. */)
1330 ()
1331 {
1332 return make_number (getpid ());
1333 }
1334
1335 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1336 doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
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
1343 resolution finer than a second. */)
1344 ()
1345 {
1346 EMACS_TIME t;
1347 Lisp_Object result[3];
1348
1349 EMACS_GET_TIME (t);
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));
1353
1354 return Flist (3, result);
1355 }
1356 \f
1357
1358 static int
1359 lisp_time_argument (specified_time, result, usec)
1360 Lisp_Object specified_time;
1361 time_t *result;
1362 int *usec;
1363 {
1364 if (NILP (specified_time))
1365 {
1366 if (usec)
1367 {
1368 EMACS_TIME t;
1369
1370 EMACS_GET_TIME (t);
1371 *usec = EMACS_USECS (t);
1372 *result = EMACS_SECS (t);
1373 return 1;
1374 }
1375 else
1376 return time (result) != -1;
1377 }
1378 else
1379 {
1380 Lisp_Object high, low;
1381 high = Fcar (specified_time);
1382 CHECK_NUMBER (high);
1383 low = Fcdr (specified_time);
1384 if (CONSP (low))
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 {
1395 CHECK_NUMBER (usec_l);
1396 *usec = XINT (usec_l);
1397 }
1398 }
1399 low = Fcar (low);
1400 }
1401 else if (usec)
1402 *usec = 0;
1403 CHECK_NUMBER (low);
1404 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
1405 return *result >> 16 == XINT (high);
1406 }
1407 }
1408
1409 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1410 doc: /* Return the current time, as a float number of seconds since the epoch.
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.
1418 Do not use this function if precise time stamps are required. */)
1419 (specified_time)
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
1428 return make_float ((sec * 1e6 + usec) / 1e6);
1429 }
1430
1431 /* Write information into buffer S of size MAXSIZE, according to the
1432 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1433 Default to Universal Time if UT is nonzero, local time otherwise.
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
1439 This function behaves like emacs_strftimeu, except it allows null
1440 bytes in FORMAT. */
1441 static size_t
1442 emacs_memftimeu (s, maxsize, format, format_len, tp, ut)
1443 char *s;
1444 size_t maxsize;
1445 const char *format;
1446 size_t format_len;
1447 const struct tm *tp;
1448 int ut;
1449 {
1450 size_t total = 0;
1451
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
1455 format contains '\0' bytes. emacs_strftimeu stops at the first
1456 '\0' byte so we must invoke it separately for each such string. */
1457 for (;;)
1458 {
1459 size_t len;
1460 size_t result;
1461
1462 if (s)
1463 s[0] = '\1';
1464
1465 result = emacs_strftimeu (s, maxsize, format, tp, ut);
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
1485 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1486 doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted.
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
1534 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z". */)
1535 (format_string, time, universal)
1536 Lisp_Object format_string, time, universal;
1537 {
1538 time_t value;
1539 int size;
1540 struct tm *tm;
1541 int ut = ! NILP (universal);
1542
1543 CHECK_STRING (format_string);
1544
1545 if (! lisp_time_argument (time, &value, NULL))
1546 error ("Invalid time specification");
1547
1548 format_string = code_convert_string_norecord (format_string,
1549 Vlocale_coding_system, 1);
1550
1551 /* This is probably enough. */
1552 size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
1553
1554 tm = ut ? gmtime (&value) : localtime (&value);
1555 if (! tm)
1556 error ("Specified time is not representable");
1557
1558 synchronize_system_time_locale ();
1559
1560 while (1)
1561 {
1562 char *buf = (char *) alloca (size + 1);
1563 int result;
1564
1565 buf[0] = '\1';
1566 result = emacs_memftimeu (buf, size, XSTRING (format_string)->data,
1567 STRING_BYTES (XSTRING (format_string)),
1568 tm, ut);
1569 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
1570 return code_convert_string_norecord (make_string (buf, result),
1571 Vlocale_coding_system, 0);
1572
1573 /* If buffer was too small, make it bigger and try again. */
1574 result = emacs_memftimeu (NULL, (size_t) -1,
1575 XSTRING (format_string)->data,
1576 STRING_BYTES (XSTRING (format_string)),
1577 tm, ut);
1578 size = result + 1;
1579 }
1580 }
1581
1582 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1583 doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
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.
1594 (Note that Common Lisp has different meanings for DOW and ZONE.) */)
1595 (specified_time)
1596 Lisp_Object specified_time;
1597 {
1598 time_t time_spec;
1599 struct tm save_tm;
1600 struct tm *decoded_time;
1601 Lisp_Object list_args[9];
1602
1603 if (! lisp_time_argument (specified_time, &time_spec, NULL))
1604 error ("Invalid time specification");
1605
1606 decoded_time = localtime (&time_spec);
1607 if (! decoded_time)
1608 error ("Specified time is not representable");
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);
1614 XSETINT (list_args[5], decoded_time->tm_year + 1900);
1615 XSETFASTINT (list_args[6], decoded_time->tm_wday);
1616 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
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
1624 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1625 return Flist (9, list_args);
1626 }
1627
1628 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1629 doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
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
1633 \(as from `current-time-zone') or an integer (as from `decode-time')
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.
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) */)
1647 (nargs, args)
1648 int nargs;
1649 register Lisp_Object *args;
1650 {
1651 time_t time;
1652 struct tm tm;
1653 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1654
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 */
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;
1668 tm.tm_isdst = -1;
1669
1670 if (CONSP (zone))
1671 zone = Fcar (zone);
1672 if (NILP (zone))
1673 time = mktime (&tm);
1674 else
1675 {
1676 char tzbuf[100];
1677 char *tzstring;
1678 char **oldenv = environ, **newenv;
1679
1680 if (EQ (zone, Qt))
1681 tzstring = "UTC0";
1682 else if (STRINGP (zone))
1683 tzstring = (char *) XSTRING (zone)->data;
1684 else if (INTEGERP (zone))
1685 {
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;
1690 }
1691 else
1692 error ("Invalid time zone specification");
1693
1694 /* Set TZ before calling mktime; merely adjusting mktime's returned
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;
1703 xfree (newenv);
1704 #ifdef LOCALTIME_CACHE
1705 tzset ();
1706 #endif
1707 }
1708
1709 if (time == (time_t) -1)
1710 error ("Specified time is not representable");
1711
1712 return make_time (time);
1713 }
1714
1715 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1716 doc: /* Return the current time, as a human-readable string.
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'
1729 and from `file-attributes'. */)
1730 (specified_time)
1731 Lisp_Object specified_time;
1732 {
1733 time_t value;
1734 char buf[30];
1735 register char *tem;
1736
1737 if (! lisp_time_argument (specified_time, &value, NULL))
1738 value = -1;
1739 tem = (char *) ctime (&value);
1740
1741 strncpy (buf, tem, 24);
1742 buf[24] = 0;
1743
1744 return build_string (buf);
1745 }
1746
1747 #define TM_YEAR_BASE 1900
1748
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)
1753 struct tm *a, *b;
1754 {
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))
1770 + (a->tm_sec - b->tm_sec));
1771 }
1772
1773 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1774 doc: /* Return the offset and name for the local time zone.
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
1789 the data it can't find. */)
1790 (specified_time)
1791 Lisp_Object specified_time;
1792 {
1793 time_t value;
1794 struct tm *t;
1795 struct tm gmt;
1796
1797 if (lisp_time_argument (specified_time, &value, NULL)
1798 && (t = gmtime (&value)) != 0
1799 && (gmt = *t, t = localtime (&value)) != 0)
1800 {
1801 int offset = tm_diff (t, &gmt);
1802 char *s = 0;
1803 char buf[6];
1804 #ifdef HAVE_TM_ZONE
1805 if (t->tm_zone)
1806 s = (char *)t->tm_zone;
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];
1811 #endif
1812 #endif /* not HAVE_TM_ZONE */
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;
1820 for (p = s; *p && (isalnum ((unsigned char)*p) || *p == ' '); ++p)
1821 ;
1822 if (p == s || *p)
1823 s = NULL;
1824 }
1825 #endif
1826
1827 if (!s)
1828 {
1829 /* No local time zone name is available; use "+-NNNN" instead. */
1830 int am = (offset < 0 ? -offset : offset) / 60;
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
1837 return Fmake_list (make_number (2), Qnil);
1838 }
1839
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
1845 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1846 doc: /* Set the local time zone using TZ, a string specifying a time zone rule.
1847 If TZ is nil, use implementation-defined default time zone information.
1848 If TZ is t, use Universal Time. */)
1849 (tz)
1850 Lisp_Object tz;
1851 {
1852 char *tzstring;
1853
1854 if (NILP (tz))
1855 tzstring = 0;
1856 else if (EQ (tz, Qt))
1857 tzstring = "UTC0";
1858 else
1859 {
1860 CHECK_STRING (tz);
1861 tzstring = (char *) XSTRING (tz)->data;
1862 }
1863
1864 set_time_zone_rule (tzstring);
1865 if (environbuf)
1866 free (environbuf);
1867 environbuf = environ;
1868
1869 return Qnil;
1870 }
1871
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.
1876 Their values shouldn't matter in non-buggy implementations.
1877 We don't use string literals for these strings,
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
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
1887
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. */
1891
1892 void
1893 set_time_zone_rule (tzstring)
1894 char *tzstring;
1895 {
1896 int envptrs;
1897 char **from, **to, **newenv;
1898
1899 /* Make the ENVIRON vector longer with room for TZSTRING. */
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));
1905
1906 /* Add TZSTRING to the end of environ, as a value for TZ. */
1907 if (tzstring)
1908 {
1909 char *t = (char *) (to + envptrs);
1910 strcpy (t, "TZ=");
1911 strcat (t, tzstring);
1912 *to++ = t;
1913 }
1914
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. */
1918 for (from = environ; *from; from++)
1919 if (strncmp (*from, "TZ=", 3) != 0)
1920 *to++ = *from;
1921 *to = 0;
1922
1923 environ = newenv;
1924
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
1929 #ifdef LOCALTIME_CACHE
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
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;
1944 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1945 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
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. */
1953 *to = set_time_zone_rule_tz1;
1954 to[1] = 0;
1955 tzset ();
1956 *to = set_time_zone_rule_tz2;
1957 tzset ();
1958 *to = 0;
1959 }
1960
1961 /* Now TZ has the desired value, and tzset can be invoked safely. */
1962 }
1963
1964 tzset ();
1965 #endif
1966 }
1967 \f
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
1973 static void
1974 general_insert_function (insert_func, insert_from_string_func,
1975 inherit, nargs, args)
1976 void (*insert_func) P_ ((unsigned char *, int));
1977 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
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 {
1990 unsigned char str[MAX_MULTIBYTE_LENGTH];
1991 int len;
1992
1993 if (!NILP (current_buffer->enable_multibyte_characters))
1994 len = CHAR_STRING (XFASTINT (val), str);
1995 else
1996 {
1997 str[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
1998 ? XINT (val)
1999 : multibyte_char_to_unibyte (XINT (val), Qnil));
2000 len = 1;
2001 }
2002 (*insert_func) (str, len);
2003 }
2004 else if (STRINGP (val))
2005 {
2006 (*insert_from_string_func) (val, 0, 0,
2007 XSTRING (val)->size,
2008 STRING_BYTES (XSTRING (val)),
2009 inherit);
2010 }
2011 else
2012 {
2013 val = wrong_type_argument (Qchar_or_string_p, val);
2014 goto retry;
2015 }
2016 }
2017 }
2018
2019 void
2020 insert1 (arg)
2021 Lisp_Object arg;
2022 {
2023 Finsert (1, &arg);
2024 }
2025
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
2032 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
2033 doc: /* Insert the arguments, either strings or characters, at point.
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
2041 to unibyte for insertion.
2042
2043 usage: (insert &rest ARGS) */)
2044 (nargs, args)
2045 int nargs;
2046 register Lisp_Object *args;
2047 {
2048 general_insert_function (insert, insert_from_string, 0, nargs, args);
2049 return Qnil;
2050 }
2051
2052 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
2053 0, MANY, 0,
2054 doc: /* Insert the arguments at point, inheriting properties from adjoining text.
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
2062 to unibyte for insertion.
2063
2064 usage: (insert-and-inherit &rest ARGS) */)
2065 (nargs, args)
2066 int nargs;
2067 register Lisp_Object *args;
2068 {
2069 general_insert_function (insert_and_inherit, insert_from_string, 1,
2070 nargs, args);
2071 return Qnil;
2072 }
2073
2074 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
2075 doc: /* Insert strings or characters at point, relocating markers after the text.
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
2081 to unibyte for insertion.
2082
2083 usage: (insert-before-markers &rest ARGS) */)
2084 (nargs, args)
2085 int nargs;
2086 register Lisp_Object *args;
2087 {
2088 general_insert_function (insert_before_markers,
2089 insert_from_string_before_markers, 0,
2090 nargs, args);
2091 return Qnil;
2092 }
2093
2094 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
2095 Sinsert_and_inherit_before_markers, 0, MANY, 0,
2096 doc: /* Insert text at point, relocating markers and inheriting properties.
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
2102 to unibyte for insertion.
2103
2104 usage: (insert-before-markers-and-inherit &rest ARGS) */)
2105 (nargs, args)
2106 int nargs;
2107 register Lisp_Object *args;
2108 {
2109 general_insert_function (insert_before_markers_and_inherit,
2110 insert_from_string_before_markers, 1,
2111 nargs, args);
2112 return Qnil;
2113 }
2114 \f
2115 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2116 doc: /* Insert COUNT (second arg) copies of CHARACTER (first arg).
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
2120 from adjoining text, if those properties are sticky. */)
2121 (character, count, inherit)
2122 Lisp_Object character, count, inherit;
2123 {
2124 register unsigned char *string;
2125 register int strlen;
2126 register int i, n;
2127 int len;
2128 unsigned char str[MAX_MULTIBYTE_LENGTH];
2129
2130 CHECK_NUMBER (character);
2131 CHECK_NUMBER (count);
2132
2133 if (!NILP (current_buffer->enable_multibyte_characters))
2134 len = CHAR_STRING (XFASTINT (character), str);
2135 else
2136 str[0] = XFASTINT (character), len = 1;
2137 n = XINT (count) * len;
2138 if (n <= 0)
2139 return Qnil;
2140 strlen = min (n, 256 * len);
2141 string = (unsigned char *) alloca (strlen);
2142 for (i = 0; i < strlen; i++)
2143 string[i] = str[i % len];
2144 while (n >= strlen)
2145 {
2146 QUIT;
2147 if (!NILP (inherit))
2148 insert_and_inherit (string, strlen);
2149 else
2150 insert (string, strlen);
2151 n -= strlen;
2152 }
2153 if (n > 0)
2154 {
2155 if (!NILP (inherit))
2156 insert_and_inherit (string, n);
2157 else
2158 insert (string, n);
2159 }
2160 return Qnil;
2161 }
2162
2163 \f
2164 /* Making strings from buffer contents. */
2165
2166 /* Return a Lisp_String containing the text of the current buffer from
2167 START to END. If text properties are in use and the current buffer
2168 has properties in the range specified, the resulting string will also
2169 have them, if PROPS is nonzero.
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. */
2178
2179 Lisp_Object
2180 make_buffer_string (start, end, props)
2181 int start, end;
2182 int props;
2183 {
2184 int start_byte = CHAR_TO_BYTE (start);
2185 int end_byte = CHAR_TO_BYTE (end);
2186
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
2212 if (start < GPT && GPT < end)
2213 move_gap (start);
2214
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);
2219 bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data,
2220 end_byte - start_byte);
2221
2222 /* If desired, update and copy the text properties. */
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))
2231 copy_intervals_to_string (result, current_buffer, start,
2232 end - start);
2233 }
2234
2235 return result;
2236 }
2237
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 {
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))
2264 Frun_hook_with_args (3, args);
2265 }
2266 else
2267 Frun_hook_with_args (3, args);
2268 }
2269 }
2270
2271 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2272 doc: /* Return the contents of part of the current buffer as a string.
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,
2279 use `buffer-substring-no-properties' instead. */)
2280 (start, end)
2281 Lisp_Object start, end;
2282 {
2283 register int b, e;
2284
2285 validate_region (&start, &end);
2286 b = XINT (start);
2287 e = XINT (end);
2288
2289 return make_buffer_string (b, e, 1);
2290 }
2291
2292 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2293 Sbuffer_substring_no_properties, 2, 2, 0,
2294 doc: /* Return the characters of part of the buffer, without the text properties.
2295 The two arguments START and END are character positions;
2296 they can be in either order. */)
2297 (start, end)
2298 Lisp_Object start, end;
2299 {
2300 register int b, e;
2301
2302 validate_region (&start, &end);
2303 b = XINT (start);
2304 e = XINT (end);
2305
2306 return make_buffer_string (b, e, 0);
2307 }
2308
2309 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2310 doc: /* Return the contents of the current buffer as a string.
2311 If narrowing is in effect, this function returns only the visible part
2312 of the buffer. */)
2313 ()
2314 {
2315 return make_buffer_string (BEGV, ZV, 1);
2316 }
2317
2318 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2319 1, 3, 0,
2320 doc: /* Insert before point a substring of the contents of buffer BUFFER.
2321 BUFFER may be a buffer or a buffer name.
2322 Arguments START and END are character numbers specifying the substring.
2323 They default to the beginning and the end of BUFFER. */)
2324 (buf, start, end)
2325 Lisp_Object buf, start, end;
2326 {
2327 register int b, e, temp;
2328 register struct buffer *bp, *obuf;
2329 Lisp_Object buffer;
2330
2331 buffer = Fget_buffer (buf);
2332 if (NILP (buffer))
2333 nsberror (buf);
2334 bp = XBUFFER (buffer);
2335 if (NILP (bp->name))
2336 error ("Selecting deleted buffer");
2337
2338 if (NILP (start))
2339 b = BUF_BEGV (bp);
2340 else
2341 {
2342 CHECK_NUMBER_COERCE_MARKER (start);
2343 b = XINT (start);
2344 }
2345 if (NILP (end))
2346 e = BUF_ZV (bp);
2347 else
2348 {
2349 CHECK_NUMBER_COERCE_MARKER (end);
2350 e = XINT (end);
2351 }
2352
2353 if (b > e)
2354 temp = b, b = e, e = temp;
2355
2356 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2357 args_out_of_range (start, end);
2358
2359 obuf = current_buffer;
2360 set_buffer_internal_1 (bp);
2361 update_buffer_properties (b, e);
2362 set_buffer_internal_1 (obuf);
2363
2364 insert_from_buffer (bp, b, e - b, 0);
2365 return Qnil;
2366 }
2367
2368 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2369 6, 6, 0,
2370 doc: /* Compare two substrings of two buffers; return result as number.
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
2377 determines whether case is significant or ignored. */)
2378 (buffer1, start1, end1, buffer2, start2, end2)
2379 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
2380 {
2381 register int begp1, endp1, begp2, endp2, temp;
2382 register struct buffer *bp1, *bp2;
2383 register Lisp_Object *trt
2384 = (!NILP (current_buffer->case_fold_search)
2385 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
2386 int chars = 0;
2387 int i1, i2, i1_byte, i2_byte;
2388
2389 /* Find the first buffer and its substring. */
2390
2391 if (NILP (buffer1))
2392 bp1 = current_buffer;
2393 else
2394 {
2395 Lisp_Object buf1;
2396 buf1 = Fget_buffer (buffer1);
2397 if (NILP (buf1))
2398 nsberror (buffer1);
2399 bp1 = XBUFFER (buf1);
2400 if (NILP (bp1->name))
2401 error ("Selecting deleted buffer");
2402 }
2403
2404 if (NILP (start1))
2405 begp1 = BUF_BEGV (bp1);
2406 else
2407 {
2408 CHECK_NUMBER_COERCE_MARKER (start1);
2409 begp1 = XINT (start1);
2410 }
2411 if (NILP (end1))
2412 endp1 = BUF_ZV (bp1);
2413 else
2414 {
2415 CHECK_NUMBER_COERCE_MARKER (end1);
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 {
2433 Lisp_Object buf2;
2434 buf2 = Fget_buffer (buffer2);
2435 if (NILP (buf2))
2436 nsberror (buffer2);
2437 bp2 = XBUFFER (buf2);
2438 if (NILP (bp2->name))
2439 error ("Selecting deleted buffer");
2440 }
2441
2442 if (NILP (start2))
2443 begp2 = BUF_BEGV (bp2);
2444 else
2445 {
2446 CHECK_NUMBER_COERCE_MARKER (start2);
2447 begp2 = XINT (start2);
2448 }
2449 if (NILP (end2))
2450 endp2 = BUF_ZV (bp2);
2451 else
2452 {
2453 CHECK_NUMBER_COERCE_MARKER (end2);
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
2465 i1 = begp1;
2466 i2 = begp2;
2467 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2468 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2469
2470 while (i1 < endp1 && i2 < endp2)
2471 {
2472 /* When we find a mismatch, we must compare the
2473 characters, not just the bytes. */
2474 int c1, c2;
2475
2476 if (! NILP (bp1->enable_multibyte_characters))
2477 {
2478 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2479 BUF_INC_POS (bp1, i1_byte);
2480 i1++;
2481 }
2482 else
2483 {
2484 c1 = BUF_FETCH_BYTE (bp1, i1);
2485 c1 = unibyte_char_to_multibyte (c1);
2486 i1++;
2487 }
2488
2489 if (! NILP (bp2->enable_multibyte_characters))
2490 {
2491 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2492 BUF_INC_POS (bp2, i2_byte);
2493 i2++;
2494 }
2495 else
2496 {
2497 c2 = BUF_FETCH_BYTE (bp2, i2);
2498 c2 = unibyte_char_to_multibyte (c2);
2499 i2++;
2500 }
2501
2502 if (trt)
2503 {
2504 c1 = XINT (trt[c1]);
2505 c2 = XINT (trt[c2]);
2506 }
2507 if (c1 < c2)
2508 return make_number (- 1 - chars);
2509 if (c1 > c2)
2510 return make_number (chars + 1);
2511
2512 chars++;
2513 }
2514
2515 /* The strings match as far as they go.
2516 If one is shorter, that one is less. */
2517 if (chars < endp1 - begp1)
2518 return make_number (chars + 1);
2519 else if (chars < endp2 - begp2)
2520 return make_number (- chars - 1);
2521
2522 /* Same length too => they are equal. */
2523 return make_number (0);
2524 }
2525 \f
2526 static Lisp_Object
2527 subst_char_in_region_unwind (arg)
2528 Lisp_Object arg;
2529 {
2530 return current_buffer->undo_list = arg;
2531 }
2532
2533 static Lisp_Object
2534 subst_char_in_region_unwind_1 (arg)
2535 Lisp_Object arg;
2536 {
2537 return current_buffer->filename = arg;
2538 }
2539
2540 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2541 Ssubst_char_in_region, 4, 5, 0,
2542 doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs.
2543 If optional arg NOUNDO is non-nil, don't record this change for undo
2544 and don't mark the buffer as really changed.
2545 Both characters must have the same length of multi-byte form. */)
2546 (start, end, fromchar, tochar, noundo)
2547 Lisp_Object start, end, fromchar, tochar, noundo;
2548 {
2549 register int pos, pos_byte, stop, i, len, end_byte;
2550 int changed = 0;
2551 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2552 unsigned char *p;
2553 int count = specpdl_ptr - specpdl;
2554 #define COMBINING_NO 0
2555 #define COMBINING_BEFORE 1
2556 #define COMBINING_AFTER 2
2557 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2558 int maybe_byte_combining = COMBINING_NO;
2559 int last_changed = 0;
2560 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2561
2562 validate_region (&start, &end);
2563 CHECK_NUMBER (fromchar);
2564 CHECK_NUMBER (tochar);
2565
2566 if (multibyte_p)
2567 {
2568 len = CHAR_STRING (XFASTINT (fromchar), fromstr);
2569 if (CHAR_STRING (XFASTINT (tochar), tostr) != len)
2570 error ("Characters in subst-char-in-region have different byte-lengths");
2571 if (!ASCII_BYTE_P (*tostr))
2572 {
2573 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2574 complete multibyte character, it may be combined with the
2575 after bytes. If it is in the range 0xA0..0xFF, it may be
2576 combined with the before and after bytes. */
2577 if (!CHAR_HEAD_P (*tostr))
2578 maybe_byte_combining = COMBINING_BOTH;
2579 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2580 maybe_byte_combining = COMBINING_AFTER;
2581 }
2582 }
2583 else
2584 {
2585 len = 1;
2586 fromstr[0] = XFASTINT (fromchar);
2587 tostr[0] = XFASTINT (tochar);
2588 }
2589
2590 pos = XINT (start);
2591 pos_byte = CHAR_TO_BYTE (pos);
2592 stop = CHAR_TO_BYTE (XINT (end));
2593 end_byte = stop;
2594
2595 /* If we don't want undo, turn off putting stuff on the list.
2596 That's faster than getting rid of things,
2597 and it prevents even the entry for a first change.
2598 Also inhibit locking the file. */
2599 if (!NILP (noundo))
2600 {
2601 record_unwind_protect (subst_char_in_region_unwind,
2602 current_buffer->undo_list);
2603 current_buffer->undo_list = Qt;
2604 /* Don't do file-locking. */
2605 record_unwind_protect (subst_char_in_region_unwind_1,
2606 current_buffer->filename);
2607 current_buffer->filename = Qnil;
2608 }
2609
2610 if (pos_byte < GPT_BYTE)
2611 stop = min (stop, GPT_BYTE);
2612 while (1)
2613 {
2614 int pos_byte_next = pos_byte;
2615
2616 if (pos_byte >= stop)
2617 {
2618 if (pos_byte >= end_byte) break;
2619 stop = end_byte;
2620 }
2621 p = BYTE_POS_ADDR (pos_byte);
2622 if (multibyte_p)
2623 INC_POS (pos_byte_next);
2624 else
2625 ++pos_byte_next;
2626 if (pos_byte_next - pos_byte == len
2627 && p[0] == fromstr[0]
2628 && (len == 1
2629 || (p[1] == fromstr[1]
2630 && (len == 2 || (p[2] == fromstr[2]
2631 && (len == 3 || p[3] == fromstr[3]))))))
2632 {
2633 if (! changed)
2634 {
2635 changed = pos;
2636 modify_region (current_buffer, changed, XINT (end));
2637
2638 if (! NILP (noundo))
2639 {
2640 if (MODIFF - 1 == SAVE_MODIFF)
2641 SAVE_MODIFF++;
2642 if (MODIFF - 1 == current_buffer->auto_save_modified)
2643 current_buffer->auto_save_modified++;
2644 }
2645 }
2646
2647 /* Take care of the case where the new character
2648 combines with neighboring bytes. */
2649 if (maybe_byte_combining
2650 && (maybe_byte_combining == COMBINING_AFTER
2651 ? (pos_byte_next < Z_BYTE
2652 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2653 : ((pos_byte_next < Z_BYTE
2654 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2655 || (pos_byte > BEG_BYTE
2656 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2657 {
2658 Lisp_Object tem, string;
2659
2660 struct gcpro gcpro1;
2661
2662 tem = current_buffer->undo_list;
2663 GCPRO1 (tem);
2664
2665 /* Make a multibyte string containing this single character. */
2666 string = make_multibyte_string (tostr, 1, len);
2667 /* replace_range is less efficient, because it moves the gap,
2668 but it handles combining correctly. */
2669 replace_range (pos, pos + 1, string,
2670 0, 0, 1);
2671 pos_byte_next = CHAR_TO_BYTE (pos);
2672 if (pos_byte_next > pos_byte)
2673 /* Before combining happened. We should not increment
2674 POS. So, to cancel the later increment of POS,
2675 decrease it now. */
2676 pos--;
2677 else
2678 INC_POS (pos_byte_next);
2679
2680 if (! NILP (noundo))
2681 current_buffer->undo_list = tem;
2682
2683 UNGCPRO;
2684 }
2685 else
2686 {
2687 if (NILP (noundo))
2688 record_change (pos, 1);
2689 for (i = 0; i < len; i++) *p++ = tostr[i];
2690 }
2691 last_changed = pos + 1;
2692 }
2693 pos_byte = pos_byte_next;
2694 pos++;
2695 }
2696
2697 if (changed)
2698 {
2699 signal_after_change (changed,
2700 last_changed - changed, last_changed - changed);
2701 update_compositions (changed, last_changed, CHECK_ALL);
2702 }
2703
2704 unbind_to (count, Qnil);
2705 return Qnil;
2706 }
2707
2708 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
2709 doc: /* From START to END, translate characters according to TABLE.
2710 TABLE is a string; the Nth character in it is the mapping
2711 for the character with code N.
2712 This function does not alter multibyte characters.
2713 It returns the number of characters changed. */)
2714 (start, end, table)
2715 Lisp_Object start;
2716 Lisp_Object end;
2717 register Lisp_Object table;
2718 {
2719 register int pos_byte, stop; /* Limits of the region. */
2720 register unsigned char *tt; /* Trans table. */
2721 register int nc; /* New character. */
2722 int cnt; /* Number of changes made. */
2723 int size; /* Size of translate table. */
2724 int pos;
2725 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2726
2727 validate_region (&start, &end);
2728 CHECK_STRING (table);
2729
2730 size = STRING_BYTES (XSTRING (table));
2731 tt = XSTRING (table)->data;
2732
2733 pos_byte = CHAR_TO_BYTE (XINT (start));
2734 stop = CHAR_TO_BYTE (XINT (end));
2735 modify_region (current_buffer, XINT (start), XINT (end));
2736 pos = XINT (start);
2737
2738 cnt = 0;
2739 for (; pos_byte < stop; )
2740 {
2741 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
2742 int len;
2743 int oc;
2744 int pos_byte_next;
2745
2746 if (multibyte)
2747 oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len);
2748 else
2749 oc = *p, len = 1;
2750 pos_byte_next = pos_byte + len;
2751 if (oc < size && len == 1)
2752 {
2753 nc = tt[oc];
2754 if (nc != oc)
2755 {
2756 /* Take care of the case where the new character
2757 combines with neighboring bytes. */
2758 if (!ASCII_BYTE_P (nc)
2759 && (CHAR_HEAD_P (nc)
2760 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1))
2761 : (pos_byte > BEG_BYTE
2762 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))
2763 {
2764 Lisp_Object string;
2765
2766 string = make_multibyte_string (tt + oc, 1, 1);
2767 /* This is less efficient, because it moves the gap,
2768 but it handles combining correctly. */
2769 replace_range (pos, pos + 1, string,
2770 1, 0, 1);
2771 pos_byte_next = CHAR_TO_BYTE (pos);
2772 if (pos_byte_next > pos_byte)
2773 /* Before combining happened. We should not
2774 increment POS. So, to cancel the later
2775 increment of POS, we decrease it now. */
2776 pos--;
2777 else
2778 INC_POS (pos_byte_next);
2779 }
2780 else
2781 {
2782 record_change (pos, 1);
2783 *p = nc;
2784 signal_after_change (pos, 1, 1);
2785 update_compositions (pos, pos + 1, CHECK_BORDER);
2786 }
2787 ++cnt;
2788 }
2789 }
2790 pos_byte = pos_byte_next;
2791 pos++;
2792 }
2793
2794 return make_number (cnt);
2795 }
2796
2797 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
2798 doc: /* Delete the text between point and mark.
2799 When called from a program, expects two arguments,
2800 positions (integers or markers) specifying the stretch to be deleted. */)
2801 (start, end)
2802 Lisp_Object start, end;
2803 {
2804 validate_region (&start, &end);
2805 del_range (XINT (start), XINT (end));
2806 return Qnil;
2807 }
2808
2809 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
2810 Sdelete_and_extract_region, 2, 2, 0,
2811 doc: /* Delete the text between START and END and return it. */)
2812 (start, end)
2813 Lisp_Object start, end;
2814 {
2815 validate_region (&start, &end);
2816 return del_range_1 (XINT (start), XINT (end), 1, 1);
2817 }
2818 \f
2819 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
2820 doc: /* Remove restrictions (narrowing) from current buffer.
2821 This allows the buffer's full text to be seen and edited. */)
2822 ()
2823 {
2824 if (BEG != BEGV || Z != ZV)
2825 current_buffer->clip_changed = 1;
2826 BEGV = BEG;
2827 BEGV_BYTE = BEG_BYTE;
2828 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
2829 /* Changing the buffer bounds invalidates any recorded current column. */
2830 invalidate_current_column ();
2831 return Qnil;
2832 }
2833
2834 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
2835 doc: /* Restrict editing in this buffer to the current region.
2836 The rest of the text becomes temporarily invisible and untouchable
2837 but is not deleted; if you save the buffer in a file, the invisible
2838 text is included in the file. \\[widen] makes all visible again.
2839 See also `save-restriction'.
2840
2841 When calling from a program, pass two arguments; positions (integers
2842 or markers) bounding the text that should remain visible. */)
2843 (start, end)
2844 register Lisp_Object start, end;
2845 {
2846 CHECK_NUMBER_COERCE_MARKER (start);
2847 CHECK_NUMBER_COERCE_MARKER (end);
2848
2849 if (XINT (start) > XINT (end))
2850 {
2851 Lisp_Object tem;
2852 tem = start; start = end; end = tem;
2853 }
2854
2855 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
2856 args_out_of_range (start, end);
2857
2858 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
2859 current_buffer->clip_changed = 1;
2860
2861 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2862 SET_BUF_ZV (current_buffer, XFASTINT (end));
2863 if (PT < XFASTINT (start))
2864 SET_PT (XFASTINT (start));
2865 if (PT > XFASTINT (end))
2866 SET_PT (XFASTINT (end));
2867 /* Changing the buffer bounds invalidates any recorded current column. */
2868 invalidate_current_column ();
2869 return Qnil;
2870 }
2871
2872 Lisp_Object
2873 save_restriction_save ()
2874 {
2875 if (BEGV == BEG && ZV == Z)
2876 /* The common case that the buffer isn't narrowed.
2877 We return just the buffer object, which save_restriction_restore
2878 recognizes as meaning `no restriction'. */
2879 return Fcurrent_buffer ();
2880 else
2881 /* We have to save a restriction, so return a pair of markers, one
2882 for the beginning and one for the end. */
2883 {
2884 Lisp_Object beg, end;
2885
2886 beg = buildmark (BEGV, BEGV_BYTE);
2887 end = buildmark (ZV, ZV_BYTE);
2888
2889 /* END must move forward if text is inserted at its exact location. */
2890 XMARKER(end)->insertion_type = 1;
2891
2892 return Fcons (beg, end);
2893 }
2894 }
2895
2896 Lisp_Object
2897 save_restriction_restore (data)
2898 Lisp_Object data;
2899 {
2900 if (CONSP (data))
2901 /* A pair of marks bounding a saved restriction. */
2902 {
2903 struct Lisp_Marker *beg = XMARKER (XCAR (data));
2904 struct Lisp_Marker *end = XMARKER (XCDR (data));
2905 struct buffer *buf = beg->buffer; /* END should have the same buffer. */
2906
2907 if (beg->charpos != BUF_BEGV(buf) || end->charpos != BUF_ZV(buf))
2908 /* The restriction has changed from the saved one, so restore
2909 the saved restriction. */
2910 {
2911 int pt = BUF_PT (buf);
2912
2913 SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
2914 SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
2915
2916 if (pt < beg->charpos || pt > end->charpos)
2917 /* The point is outside the new visible range, move it inside. */
2918 SET_BUF_PT_BOTH (buf,
2919 clip_to_bounds (beg->charpos, pt, end->charpos),
2920 clip_to_bounds (beg->bytepos, BUF_PT_BYTE(buf),
2921 end->bytepos));
2922
2923 buf->clip_changed = 1; /* Remember that the narrowing changed. */
2924 }
2925 }
2926 else
2927 /* A buffer, which means that there was no old restriction. */
2928 {
2929 struct buffer *buf = XBUFFER (data);
2930
2931 if (BUF_BEGV(buf) != BUF_BEG(buf) || BUF_ZV(buf) != BUF_Z(buf))
2932 /* The buffer has been narrowed, get rid of the narrowing. */
2933 {
2934 SET_BUF_BEGV_BOTH (buf, BUF_BEG(buf), BUF_BEG_BYTE(buf));
2935 SET_BUF_ZV_BOTH (buf, BUF_Z(buf), BUF_Z_BYTE(buf));
2936
2937 buf->clip_changed = 1; /* Remember that the narrowing changed. */
2938 }
2939 }
2940
2941 return Qnil;
2942 }
2943
2944 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2945 doc: /* Execute BODY, saving and restoring current buffer's restrictions.
2946 The buffer's restrictions make parts of the beginning and end invisible.
2947 (They are set up with `narrow-to-region' and eliminated with `widen'.)
2948 This special form, `save-restriction', saves the current buffer's restrictions
2949 when it is entered, and restores them when it is exited.
2950 So any `narrow-to-region' within BODY lasts only until the end of the form.
2951 The old restrictions settings are restored
2952 even in case of abnormal exit (throw or error).
2953
2954 The value returned is the value of the last form in BODY.
2955
2956 Note: if you are using both `save-excursion' and `save-restriction',
2957 use `save-excursion' outermost:
2958 (save-excursion (save-restriction ...))
2959
2960 usage: (save-restriction &rest BODY) */)
2961 (body)
2962 Lisp_Object body;
2963 {
2964 register Lisp_Object val;
2965 int count = specpdl_ptr - specpdl;
2966
2967 record_unwind_protect (save_restriction_restore, save_restriction_save ());
2968 val = Fprogn (body);
2969 return unbind_to (count, val);
2970 }
2971 \f
2972 /* Buffer for the most recent text displayed by Fmessage_box. */
2973 static char *message_text;
2974
2975 /* Allocated length of that buffer. */
2976 static int message_length;
2977
2978 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2979 doc: /* Print a one-line message at the bottom of the screen.
2980 The first argument is a format control string, and the rest are data
2981 to be formatted under control of the string. See `format' for details.
2982
2983 If the first argument is nil, clear any existing message; let the
2984 minibuffer contents show.
2985
2986 usage: (message STRING &rest ARGS) */)
2987 (nargs, args)
2988 int nargs;
2989 Lisp_Object *args;
2990 {
2991 if (NILP (args[0]))
2992 {
2993 message (0);
2994 return Qnil;
2995 }
2996 else
2997 {
2998 register Lisp_Object val;
2999 val = Fformat (nargs, args);
3000 message3 (val, STRING_BYTES (XSTRING (val)), STRING_MULTIBYTE (val));
3001 return val;
3002 }
3003 }
3004
3005 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
3006 doc: /* Display a message, in a dialog box if possible.
3007 If a dialog box is not available, use the echo area.
3008 The first argument is a format control string, and the rest are data
3009 to be formatted under control of the string. See `format' for details.
3010
3011 If the first argument is nil, clear any existing message; let the
3012 minibuffer contents show.
3013
3014 usage: (message-box STRING &rest ARGS) */)
3015 (nargs, args)
3016 int nargs;
3017 Lisp_Object *args;
3018 {
3019 if (NILP (args[0]))
3020 {
3021 message (0);
3022 return Qnil;
3023 }
3024 else
3025 {
3026 register Lisp_Object val;
3027 val = Fformat (nargs, args);
3028 #ifdef HAVE_MENUS
3029 /* The MS-DOS frames support popup menus even though they are
3030 not FRAME_WINDOW_P. */
3031 if (FRAME_WINDOW_P (XFRAME (selected_frame))
3032 || FRAME_MSDOS_P (XFRAME (selected_frame)))
3033 {
3034 Lisp_Object pane, menu, obj;
3035 struct gcpro gcpro1;
3036 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
3037 GCPRO1 (pane);
3038 menu = Fcons (val, pane);
3039 obj = Fx_popup_dialog (Qt, menu);
3040 UNGCPRO;
3041 return val;
3042 }
3043 #endif /* HAVE_MENUS */
3044 /* Copy the data so that it won't move when we GC. */
3045 if (! message_text)
3046 {
3047 message_text = (char *)xmalloc (80);
3048 message_length = 80;
3049 }
3050 if (STRING_BYTES (XSTRING (val)) > message_length)
3051 {
3052 message_length = STRING_BYTES (XSTRING (val));
3053 message_text = (char *)xrealloc (message_text, message_length);
3054 }
3055 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
3056 message2 (message_text, STRING_BYTES (XSTRING (val)),
3057 STRING_MULTIBYTE (val));
3058 return val;
3059 }
3060 }
3061 #ifdef HAVE_MENUS
3062 extern Lisp_Object last_nonmenu_event;
3063 #endif
3064
3065 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
3066 doc: /* Display a message in a dialog box or in the echo area.
3067 If this command was invoked with the mouse, use a dialog box if
3068 `use-dialog-box' is non-nil.
3069 Otherwise, use the echo area.
3070 The first argument is a format control string, and the rest are data
3071 to be formatted under control of the string. See `format' for details.
3072
3073 If the first argument is nil, clear any existing message; let the
3074 minibuffer contents show.
3075
3076 usage: (message-or-box STRING &rest ARGS) */)
3077 (nargs, args)
3078 int nargs;
3079 Lisp_Object *args;
3080 {
3081 #ifdef HAVE_MENUS
3082 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3083 && use_dialog_box)
3084 return Fmessage_box (nargs, args);
3085 #endif
3086 return Fmessage (nargs, args);
3087 }
3088
3089 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
3090 doc: /* Return the string currently displayed in the echo area, or nil if none. */)
3091 ()
3092 {
3093 return current_message ();
3094 }
3095
3096
3097 DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0,
3098 doc: /* Return a copy of STRING with text properties added.
3099 First argument is the string to copy.
3100 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
3101 properties to add to the result.
3102 usage: (propertize STRING &rest PROPERTIES) */)
3103 (nargs, args)
3104 int nargs;
3105 Lisp_Object *args;
3106 {
3107 Lisp_Object properties, string;
3108 struct gcpro gcpro1, gcpro2;
3109 int i;
3110
3111 /* Number of args must be odd. */
3112 if ((nargs & 1) == 0 || nargs < 1)
3113 error ("Wrong number of arguments");
3114
3115 properties = string = Qnil;
3116 GCPRO2 (properties, string);
3117
3118 /* First argument must be a string. */
3119 CHECK_STRING (args[0]);
3120 string = Fcopy_sequence (args[0]);
3121
3122 for (i = 1; i < nargs; i += 2)
3123 {
3124 CHECK_SYMBOL (args[i]);
3125 properties = Fcons (args[i], Fcons (args[i + 1], properties));
3126 }
3127
3128 Fadd_text_properties (make_number (0),
3129 make_number (XSTRING (string)->size),
3130 properties, string);
3131 RETURN_UNGCPRO (string);
3132 }
3133
3134
3135 /* Number of bytes that STRING will occupy when put into the result.
3136 MULTIBYTE is nonzero if the result should be multibyte. */
3137
3138 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
3139 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
3140 ? count_size_as_multibyte (XSTRING (STRING)->data, \
3141 STRING_BYTES (XSTRING (STRING))) \
3142 : STRING_BYTES (XSTRING (STRING)))
3143
3144 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3145 doc: /* Format a string out of a control-string and arguments.
3146 The first argument is a control string.
3147 The other arguments are substituted into it to make the result, a string.
3148 It may contain %-sequences meaning to substitute the next argument.
3149 %s means print a string argument. Actually, prints any object, with `princ'.
3150 %d means print as number in decimal (%o octal, %x hex).
3151 %X is like %x, but uses upper case.
3152 %e means print a number in exponential notation.
3153 %f means print a number in decimal-point notation.
3154 %g means print a number in exponential notation
3155 or decimal-point notation, whichever uses fewer characters.
3156 %c means print a number as a single character.
3157 %S means print any object as an s-expression (using `prin1').
3158 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.
3159 Use %% to put a single % into the output.
3160
3161 usage: (format STRING &rest OBJECTS) */)
3162 (nargs, args)
3163 int nargs;
3164 register Lisp_Object *args;
3165 {
3166 register int n; /* The number of the next arg to substitute */
3167 register int total; /* An estimate of the final length */
3168 char *buf, *p;
3169 register unsigned char *format, *end;
3170 int nchars;
3171 /* Nonzero if the output should be a multibyte string,
3172 which is true if any of the inputs is one. */
3173 int multibyte = 0;
3174 /* When we make a multibyte string, we must pay attention to the
3175 byte combining problem, i.e., a byte may be combined with a
3176 multibyte charcter of the previous string. This flag tells if we
3177 must consider such a situation or not. */
3178 int maybe_combine_byte;
3179 unsigned char *this_format;
3180 int longest_format;
3181 Lisp_Object val;
3182 struct info
3183 {
3184 int start, end;
3185 } *info = 0;
3186
3187 /* It should not be necessary to GCPRO ARGS, because
3188 the caller in the interpreter should take care of that. */
3189
3190 /* Try to determine whether the result should be multibyte.
3191 This is not always right; sometimes the result needs to be multibyte
3192 because of an object that we will pass through prin1,
3193 and in that case, we won't know it here. */
3194 for (n = 0; n < nargs; n++)
3195 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3196 multibyte = 1;
3197
3198 CHECK_STRING (args[0]);
3199
3200 /* If we start out planning a unibyte result,
3201 and later find it has to be multibyte, we jump back to retry. */
3202 retry:
3203
3204 format = XSTRING (args[0])->data;
3205 end = format + STRING_BYTES (XSTRING (args[0]));
3206 longest_format = 0;
3207
3208 /* Make room in result for all the non-%-codes in the control string. */
3209 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]);
3210
3211 /* Add to TOTAL enough space to hold the converted arguments. */
3212
3213 n = 0;
3214 while (format != end)
3215 if (*format++ == '%')
3216 {
3217 int thissize = 0;
3218 unsigned char *this_format_start = format - 1;
3219 int field_width, precision;
3220
3221 /* General format specifications look like
3222
3223 '%' [flags] [field-width] [precision] format
3224
3225 where
3226
3227 flags ::= [#-* 0]+
3228 field-width ::= [0-9]+
3229 precision ::= '.' [0-9]*
3230
3231 If a field-width is specified, it specifies to which width
3232 the output should be padded with blanks, iff the output
3233 string is shorter than field-width.
3234
3235 if precision is specified, it specifies the number of
3236 digits to print after the '.' for floats, or the max.
3237 number of chars to print from a string. */
3238
3239 precision = field_width = 0;
3240
3241 while (index ("-*# 0", *format))
3242 ++format;
3243
3244 if (*format >= '0' && *format <= '9')
3245 {
3246 for (field_width = 0; *format >= '0' && *format <= '9'; ++format)
3247 field_width = 10 * field_width + *format - '0';
3248 }
3249
3250 if (*format == '.')
3251 {
3252 ++format;
3253 for (precision = 0; *format >= '0' && *format <= '9'; ++format)
3254 precision = 10 * precision + *format - '0';
3255 }
3256
3257 if (format - this_format_start + 1 > longest_format)
3258 longest_format = format - this_format_start + 1;
3259
3260 if (format == end)
3261 error ("Format string ends in middle of format specifier");
3262 if (*format == '%')
3263 format++;
3264 else if (++n >= nargs)
3265 error ("Not enough arguments for format string");
3266 else if (*format == 'S')
3267 {
3268 /* For `S', prin1 the argument and then treat like a string. */
3269 register Lisp_Object tem;
3270 tem = Fprin1_to_string (args[n], Qnil);
3271 if (STRING_MULTIBYTE (tem) && ! multibyte)
3272 {
3273 multibyte = 1;
3274 goto retry;
3275 }
3276 args[n] = tem;
3277 goto string;
3278 }
3279 else if (SYMBOLP (args[n]))
3280 {
3281 /* Use a temp var to avoid problems when ENABLE_CHECKING
3282 is turned on. */
3283 struct Lisp_String *t = XSYMBOL (args[n])->name;
3284 XSETSTRING (args[n], t);
3285 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3286 {
3287 multibyte = 1;
3288 goto retry;
3289 }
3290 goto string;
3291 }
3292 else if (STRINGP (args[n]))
3293 {
3294 string:
3295 if (*format != 's' && *format != 'S')
3296 error ("Format specifier doesn't match argument type");
3297 thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
3298 }
3299 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
3300 else if (INTEGERP (args[n]) && *format != 's')
3301 {
3302 /* The following loop assumes the Lisp type indicates
3303 the proper way to pass the argument.
3304 So make sure we have a flonum if the argument should
3305 be a double. */
3306 if (*format == 'e' || *format == 'f' || *format == 'g')
3307 args[n] = Ffloat (args[n]);
3308 else
3309 if (*format != 'd' && *format != 'o' && *format != 'x'
3310 && *format != 'i' && *format != 'X' && *format != 'c')
3311 error ("Invalid format operation %%%c", *format);
3312
3313 thissize = 30;
3314 if (*format == 'c'
3315 && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
3316 || XINT (args[n]) == 0))
3317 {
3318 if (! multibyte)
3319 {
3320 multibyte = 1;
3321 goto retry;
3322 }
3323 args[n] = Fchar_to_string (args[n]);
3324 thissize = STRING_BYTES (XSTRING (args[n]));
3325 }
3326 }
3327 else if (FLOATP (args[n]) && *format != 's')
3328 {
3329 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
3330 args[n] = Ftruncate (args[n], Qnil);
3331
3332 /* Note that we're using sprintf to print floats,
3333 so we have to take into account what that function
3334 prints. */
3335 thissize = MAX_10_EXP + 100 + precision;
3336 }
3337 else
3338 {
3339 /* Anything but a string, convert to a string using princ. */
3340 register Lisp_Object tem;
3341 tem = Fprin1_to_string (args[n], Qt);
3342 if (STRING_MULTIBYTE (tem) & ! multibyte)
3343 {
3344 multibyte = 1;
3345 goto retry;
3346 }
3347 args[n] = tem;
3348 goto string;
3349 }
3350
3351 thissize = max (field_width, thissize);
3352 total += thissize + 4;
3353 }
3354
3355 /* Now we can no longer jump to retry.
3356 TOTAL and LONGEST_FORMAT are known for certain. */
3357
3358 this_format = (unsigned char *) alloca (longest_format + 1);
3359
3360 /* Allocate the space for the result.
3361 Note that TOTAL is an overestimate. */
3362 if (total < 1000)
3363 buf = (char *) alloca (total + 1);
3364 else
3365 buf = (char *) xmalloc (total + 1);
3366
3367 p = buf;
3368 nchars = 0;
3369 n = 0;
3370
3371 /* Scan the format and store result in BUF. */
3372 format = XSTRING (args[0])->data;
3373 maybe_combine_byte = 0;
3374 while (format != end)
3375 {
3376 if (*format == '%')
3377 {
3378 int minlen;
3379 int negative = 0;
3380 unsigned char *this_format_start = format;
3381
3382 format++;
3383
3384 /* Process a numeric arg and skip it. */
3385 minlen = atoi (format);
3386 if (minlen < 0)
3387 minlen = - minlen, negative = 1;
3388
3389 while ((*format >= '0' && *format <= '9')
3390 || *format == '-' || *format == ' ' || *format == '.')
3391 format++;
3392
3393 if (*format++ == '%')
3394 {
3395 *p++ = '%';
3396 nchars++;
3397 continue;
3398 }
3399
3400 ++n;
3401
3402 if (STRINGP (args[n]))
3403 {
3404 int padding, nbytes, start, end;
3405 int width = lisp_string_width (args[n], -1, NULL, NULL);
3406
3407 /* If spec requires it, pad on right with spaces. */
3408 padding = minlen - width;
3409 if (! negative)
3410 while (padding-- > 0)
3411 {
3412 *p++ = ' ';
3413 ++nchars;
3414 }
3415
3416 start = nchars;
3417
3418 if (p > buf
3419 && multibyte
3420 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3421 && STRING_MULTIBYTE (args[n])
3422 && !CHAR_HEAD_P (XSTRING (args[n])->data[0]))
3423 maybe_combine_byte = 1;
3424 nbytes = copy_text (XSTRING (args[n])->data, p,
3425 STRING_BYTES (XSTRING (args[n])),
3426 STRING_MULTIBYTE (args[n]), multibyte);
3427 p += nbytes;
3428 nchars += XSTRING (args[n])->size;
3429 end = nchars;
3430
3431 if (negative)
3432 while (padding-- > 0)
3433 {
3434 *p++ = ' ';
3435 nchars++;
3436 }
3437
3438 /* If this argument has text properties, record where
3439 in the result string it appears. */
3440 if (XSTRING (args[n])->intervals)
3441 {
3442 if (!info)
3443 {
3444 int nbytes = nargs * sizeof *info;
3445 info = (struct info *) alloca (nbytes);
3446 bzero (info, nbytes);
3447 }
3448
3449 info[n].start = start;
3450 info[n].end = end;
3451 }
3452 }
3453 else if (INTEGERP (args[n]) || FLOATP (args[n]))
3454 {
3455 int this_nchars;
3456
3457 bcopy (this_format_start, this_format,
3458 format - this_format_start);
3459 this_format[format - this_format_start] = 0;
3460
3461 if (INTEGERP (args[n]))
3462 sprintf (p, this_format, XINT (args[n]));
3463 else
3464 sprintf (p, this_format, XFLOAT_DATA (args[n]));
3465
3466 if (p > buf
3467 && multibyte
3468 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3469 && !CHAR_HEAD_P (*((unsigned char *) p)))
3470 maybe_combine_byte = 1;
3471 this_nchars = strlen (p);
3472 if (multibyte)
3473 p += str_to_multibyte (p, buf + total - p, this_nchars);
3474 else
3475 p += this_nchars;
3476 nchars += this_nchars;
3477 }
3478 }
3479 else if (STRING_MULTIBYTE (args[0]))
3480 {
3481 /* Copy a whole multibyte character. */
3482 if (p > buf
3483 && multibyte
3484 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3485 && !CHAR_HEAD_P (*format))
3486 maybe_combine_byte = 1;
3487 *p++ = *format++;
3488 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
3489 nchars++;
3490 }
3491 else if (multibyte)
3492 {
3493 /* Convert a single-byte character to multibyte. */
3494 int len = copy_text (format, p, 1, 0, 1);
3495
3496 p += len;
3497 format++;
3498 nchars++;
3499 }
3500 else
3501 *p++ = *format++, nchars++;
3502 }
3503
3504 if (p > buf + total + 1)
3505 abort ();
3506
3507 if (maybe_combine_byte)
3508 nchars = multibyte_chars_in_text (buf, p - buf);
3509 val = make_specified_string (buf, nchars, p - buf, multibyte);
3510
3511 /* If we allocated BUF with malloc, free it too. */
3512 if (total >= 1000)
3513 xfree (buf);
3514
3515 /* If the format string has text properties, or any of the string
3516 arguments has text properties, set up text properties of the
3517 result string. */
3518
3519 if (XSTRING (args[0])->intervals || info)
3520 {
3521 Lisp_Object len, new_len, props;
3522 struct gcpro gcpro1;
3523
3524 /* Add text properties from the format string. */
3525 len = make_number (XSTRING (args[0])->size);
3526 props = text_property_list (args[0], make_number (0), len, Qnil);
3527 GCPRO1 (props);
3528
3529 if (CONSP (props))
3530 {
3531 new_len = make_number (XSTRING (val)->size);
3532 extend_property_ranges (props, len, new_len);
3533 add_text_properties_from_list (val, props, make_number (0));
3534 }
3535
3536 /* Add text properties from arguments. */
3537 if (info)
3538 for (n = 1; n < nargs; ++n)
3539 if (info[n].end)
3540 {
3541 len = make_number (XSTRING (args[n])->size);
3542 new_len = make_number (info[n].end - info[n].start);
3543 props = text_property_list (args[n], make_number (0), len, Qnil);
3544 extend_property_ranges (props, len, new_len);
3545 /* If successive arguments have properites, be sure that
3546 the value of `composition' property be the copy. */
3547 if (n > 1 && info[n - 1].end)
3548 make_composition_value_copy (props);
3549 add_text_properties_from_list (val, props,
3550 make_number (info[n].start));
3551 }
3552
3553 UNGCPRO;
3554 }
3555
3556 return val;
3557 }
3558
3559
3560 /* VARARGS 1 */
3561 Lisp_Object
3562 #ifdef NO_ARG_ARRAY
3563 format1 (string1, arg0, arg1, arg2, arg3, arg4)
3564 EMACS_INT arg0, arg1, arg2, arg3, arg4;
3565 #else
3566 format1 (string1)
3567 #endif
3568 char *string1;
3569 {
3570 char buf[100];
3571 #ifdef NO_ARG_ARRAY
3572 EMACS_INT args[5];
3573 args[0] = arg0;
3574 args[1] = arg1;
3575 args[2] = arg2;
3576 args[3] = arg3;
3577 args[4] = arg4;
3578 doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
3579 #else
3580 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
3581 #endif
3582 return build_string (buf);
3583 }
3584 \f
3585 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
3586 doc: /* Return t if two characters match, optionally ignoring case.
3587 Both arguments must be characters (i.e. integers).
3588 Case is ignored if `case-fold-search' is non-nil in the current buffer. */)
3589 (c1, c2)
3590 register Lisp_Object c1, c2;
3591 {
3592 int i1, i2;
3593 CHECK_NUMBER (c1);
3594 CHECK_NUMBER (c2);
3595
3596 if (XINT (c1) == XINT (c2))
3597 return Qt;
3598 if (NILP (current_buffer->case_fold_search))
3599 return Qnil;
3600
3601 /* Do these in separate statements,
3602 then compare the variables.
3603 because of the way DOWNCASE uses temp variables. */
3604 i1 = DOWNCASE (XFASTINT (c1));
3605 i2 = DOWNCASE (XFASTINT (c2));
3606 return (i1 == i2 ? Qt : Qnil);
3607 }
3608 \f
3609 /* Transpose the markers in two regions of the current buffer, and
3610 adjust the ones between them if necessary (i.e.: if the regions
3611 differ in size).
3612
3613 START1, END1 are the character positions of the first region.
3614 START1_BYTE, END1_BYTE are the byte positions.
3615 START2, END2 are the character positions of the second region.
3616 START2_BYTE, END2_BYTE are the byte positions.
3617
3618 Traverses the entire marker list of the buffer to do so, adding an
3619 appropriate amount to some, subtracting from some, and leaving the
3620 rest untouched. Most of this is copied from adjust_markers in insdel.c.
3621
3622 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
3623
3624 static void
3625 transpose_markers (start1, end1, start2, end2,
3626 start1_byte, end1_byte, start2_byte, end2_byte)
3627 register int start1, end1, start2, end2;
3628 register int start1_byte, end1_byte, start2_byte, end2_byte;
3629 {
3630 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
3631 register Lisp_Object marker;
3632
3633 /* Update point as if it were a marker. */
3634 if (PT < start1)
3635 ;
3636 else if (PT < end1)
3637 TEMP_SET_PT_BOTH (PT + (end2 - end1),
3638 PT_BYTE + (end2_byte - end1_byte));
3639 else if (PT < start2)
3640 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
3641 (PT_BYTE + (end2_byte - start2_byte)
3642 - (end1_byte - start1_byte)));
3643 else if (PT < end2)
3644 TEMP_SET_PT_BOTH (PT - (start2 - start1),
3645 PT_BYTE - (start2_byte - start1_byte));
3646
3647 /* We used to adjust the endpoints here to account for the gap, but that
3648 isn't good enough. Even if we assume the caller has tried to move the
3649 gap out of our way, it might still be at start1 exactly, for example;
3650 and that places it `inside' the interval, for our purposes. The amount
3651 of adjustment is nontrivial if there's a `denormalized' marker whose
3652 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
3653 the dirty work to Fmarker_position, below. */
3654
3655 /* The difference between the region's lengths */
3656 diff = (end2 - start2) - (end1 - start1);
3657 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
3658
3659 /* For shifting each marker in a region by the length of the other
3660 region plus the distance between the regions. */
3661 amt1 = (end2 - start2) + (start2 - end1);
3662 amt2 = (end1 - start1) + (start2 - end1);
3663 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
3664 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
3665
3666 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
3667 marker = XMARKER (marker)->chain)
3668 {
3669 mpos = marker_byte_position (marker);
3670 if (mpos >= start1_byte && mpos < end2_byte)
3671 {
3672 if (mpos < end1_byte)
3673 mpos += amt1_byte;
3674 else if (mpos < start2_byte)
3675 mpos += diff_byte;
3676 else
3677 mpos -= amt2_byte;
3678 XMARKER (marker)->bytepos = mpos;
3679 }
3680 mpos = XMARKER (marker)->charpos;
3681 if (mpos >= start1 && mpos < end2)
3682 {
3683 if (mpos < end1)
3684 mpos += amt1;
3685 else if (mpos < start2)
3686 mpos += diff;
3687 else
3688 mpos -= amt2;
3689 }
3690 XMARKER (marker)->charpos = mpos;
3691 }
3692 }
3693
3694 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
3695 doc: /* Transpose region START1 to END1 with START2 to END2.
3696 The regions may not be overlapping, because the size of the buffer is
3697 never changed in a transposition.
3698
3699 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update
3700 any markers that happen to be located in the regions.
3701
3702 Transposing beyond buffer boundaries is an error. */)
3703 (startr1, endr1, startr2, endr2, leave_markers)
3704 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
3705 {
3706 register int start1, end1, start2, end2;
3707 int start1_byte, start2_byte, len1_byte, len2_byte;
3708 int gap, len1, len_mid, len2;
3709 unsigned char *start1_addr, *start2_addr, *temp;
3710
3711 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
3712 cur_intv = BUF_INTERVALS (current_buffer);
3713
3714 validate_region (&startr1, &endr1);
3715 validate_region (&startr2, &endr2);
3716
3717 start1 = XFASTINT (startr1);
3718 end1 = XFASTINT (endr1);
3719 start2 = XFASTINT (startr2);
3720 end2 = XFASTINT (endr2);
3721 gap = GPT;
3722
3723 /* Swap the regions if they're reversed. */
3724 if (start2 < end1)
3725 {
3726 register int glumph = start1;
3727 start1 = start2;
3728 start2 = glumph;
3729 glumph = end1;
3730 end1 = end2;
3731 end2 = glumph;
3732 }
3733
3734 len1 = end1 - start1;
3735 len2 = end2 - start2;
3736
3737 if (start2 < end1)
3738 error ("Transposed regions overlap");
3739 else if (start1 == end1 || start2 == end2)
3740 error ("Transposed region has length 0");
3741
3742 /* The possibilities are:
3743 1. Adjacent (contiguous) regions, or separate but equal regions
3744 (no, really equal, in this case!), or
3745 2. Separate regions of unequal size.
3746
3747 The worst case is usually No. 2. It means that (aside from
3748 potential need for getting the gap out of the way), there also
3749 needs to be a shifting of the text between the two regions. So
3750 if they are spread far apart, we are that much slower... sigh. */
3751
3752 /* It must be pointed out that the really studly thing to do would
3753 be not to move the gap at all, but to leave it in place and work
3754 around it if necessary. This would be extremely efficient,
3755 especially considering that people are likely to do
3756 transpositions near where they are working interactively, which
3757 is exactly where the gap would be found. However, such code
3758 would be much harder to write and to read. So, if you are
3759 reading this comment and are feeling squirrely, by all means have
3760 a go! I just didn't feel like doing it, so I will simply move
3761 the gap the minimum distance to get it out of the way, and then
3762 deal with an unbroken array. */
3763
3764 /* Make sure the gap won't interfere, by moving it out of the text
3765 we will operate on. */
3766 if (start1 < gap && gap < end2)
3767 {
3768 if (gap - start1 < end2 - gap)
3769 move_gap (start1);
3770 else
3771 move_gap (end2);
3772 }
3773
3774 start1_byte = CHAR_TO_BYTE (start1);
3775 start2_byte = CHAR_TO_BYTE (start2);
3776 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
3777 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
3778
3779 #ifdef BYTE_COMBINING_DEBUG
3780 if (end1 == start2)
3781 {
3782 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
3783 len2_byte, start1, start1_byte)
3784 || count_combining_before (BYTE_POS_ADDR (start1_byte),
3785 len1_byte, end2, start2_byte + len2_byte)
3786 || count_combining_after (BYTE_POS_ADDR (start1_byte),
3787 len1_byte, end2, start2_byte + len2_byte))
3788 abort ();
3789 }
3790 else
3791 {
3792 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
3793 len2_byte, start1, start1_byte)
3794 || count_combining_before (BYTE_POS_ADDR (start1_byte),
3795 len1_byte, start2, start2_byte)
3796 || count_combining_after (BYTE_POS_ADDR (start2_byte),
3797 len2_byte, end1, start1_byte + len1_byte)
3798 || count_combining_after (BYTE_POS_ADDR (start1_byte),
3799 len1_byte, end2, start2_byte + len2_byte))
3800 abort ();
3801 }
3802 #endif
3803
3804 /* Hmmm... how about checking to see if the gap is large
3805 enough to use as the temporary storage? That would avoid an
3806 allocation... interesting. Later, don't fool with it now. */
3807
3808 /* Working without memmove, for portability (sigh), so must be
3809 careful of overlapping subsections of the array... */
3810
3811 if (end1 == start2) /* adjacent regions */
3812 {
3813 modify_region (current_buffer, start1, end2);
3814 record_change (start1, len1 + len2);
3815
3816 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3817 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3818 Fset_text_properties (make_number (start1), make_number (end2),
3819 Qnil, Qnil);
3820
3821 /* First region smaller than second. */
3822 if (len1_byte < len2_byte)
3823 {
3824 /* We use alloca only if it is small,
3825 because we want to avoid stack overflow. */
3826 if (len2_byte > 20000)
3827 temp = (unsigned char *) xmalloc (len2_byte);
3828 else
3829 temp = (unsigned char *) alloca (len2_byte);
3830
3831 /* Don't precompute these addresses. We have to compute them
3832 at the last minute, because the relocating allocator might
3833 have moved the buffer around during the xmalloc. */
3834 start1_addr = BYTE_POS_ADDR (start1_byte);
3835 start2_addr = BYTE_POS_ADDR (start2_byte);
3836
3837 bcopy (start2_addr, temp, len2_byte);
3838 bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
3839 bcopy (temp, start1_addr, len2_byte);
3840 if (len2_byte > 20000)
3841 xfree (temp);
3842 }
3843 else
3844 /* First region not smaller than second. */
3845 {
3846 if (len1_byte > 20000)
3847 temp = (unsigned char *) xmalloc (len1_byte);
3848 else
3849 temp = (unsigned char *) alloca (len1_byte);
3850 start1_addr = BYTE_POS_ADDR (start1_byte);
3851 start2_addr = BYTE_POS_ADDR (start2_byte);
3852 bcopy (start1_addr, temp, len1_byte);
3853 bcopy (start2_addr, start1_addr, len2_byte);
3854 bcopy (temp, start1_addr + len2_byte, len1_byte);
3855 if (len1_byte > 20000)
3856 xfree (temp);
3857 }
3858 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
3859 len1, current_buffer, 0);
3860 graft_intervals_into_buffer (tmp_interval2, start1,
3861 len2, current_buffer, 0);
3862 update_compositions (start1, start1 + len2, CHECK_BORDER);
3863 update_compositions (start1 + len2, end2, CHECK_TAIL);
3864 }
3865 /* Non-adjacent regions, because end1 != start2, bleagh... */
3866 else
3867 {
3868 len_mid = start2_byte - (start1_byte + len1_byte);
3869
3870 if (len1_byte == len2_byte)
3871 /* Regions are same size, though, how nice. */
3872 {
3873 modify_region (current_buffer, start1, end1);
3874 modify_region (current_buffer, start2, end2);
3875 record_change (start1, len1);
3876 record_change (start2, len2);
3877 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3878 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3879 Fset_text_properties (make_number (start1), make_number (end1),
3880 Qnil, Qnil);
3881 Fset_text_properties (make_number (start2), make_number (end2),
3882 Qnil, Qnil);
3883
3884 if (len1_byte > 20000)
3885 temp = (unsigned char *) xmalloc (len1_byte);
3886 else
3887 temp = (unsigned char *) alloca (len1_byte);
3888 start1_addr = BYTE_POS_ADDR (start1_byte);
3889 start2_addr = BYTE_POS_ADDR (start2_byte);
3890 bcopy (start1_addr, temp, len1_byte);
3891 bcopy (start2_addr, start1_addr, len2_byte);
3892 bcopy (temp, start2_addr, len1_byte);
3893 if (len1_byte > 20000)
3894 xfree (temp);
3895 graft_intervals_into_buffer (tmp_interval1, start2,
3896 len1, current_buffer, 0);
3897 graft_intervals_into_buffer (tmp_interval2, start1,
3898 len2, current_buffer, 0);
3899 }
3900
3901 else if (len1_byte < len2_byte) /* Second region larger than first */
3902 /* Non-adjacent & unequal size, area between must also be shifted. */
3903 {
3904 modify_region (current_buffer, start1, end2);
3905 record_change (start1, (end2 - start1));
3906 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3907 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3908 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3909 Fset_text_properties (make_number (start1), make_number (end2),
3910 Qnil, Qnil);
3911
3912 /* holds region 2 */
3913 if (len2_byte > 20000)
3914 temp = (unsigned char *) xmalloc (len2_byte);
3915 else
3916 temp = (unsigned char *) alloca (len2_byte);
3917 start1_addr = BYTE_POS_ADDR (start1_byte);
3918 start2_addr = BYTE_POS_ADDR (start2_byte);
3919 bcopy (start2_addr, temp, len2_byte);
3920 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
3921 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3922 bcopy (temp, start1_addr, len2_byte);
3923 if (len2_byte > 20000)
3924 xfree (temp);
3925 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3926 len1, current_buffer, 0);
3927 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3928 len_mid, current_buffer, 0);
3929 graft_intervals_into_buffer (tmp_interval2, start1,
3930 len2, current_buffer, 0);
3931 }
3932 else
3933 /* Second region smaller than first. */
3934 {
3935 record_change (start1, (end2 - start1));
3936 modify_region (current_buffer, start1, end2);
3937
3938 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3939 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3940 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3941 Fset_text_properties (make_number (start1), make_number (end2),
3942 Qnil, Qnil);
3943
3944 /* holds region 1 */
3945 if (len1_byte > 20000)
3946 temp = (unsigned char *) xmalloc (len1_byte);
3947 else
3948 temp = (unsigned char *) alloca (len1_byte);
3949 start1_addr = BYTE_POS_ADDR (start1_byte);
3950 start2_addr = BYTE_POS_ADDR (start2_byte);
3951 bcopy (start1_addr, temp, len1_byte);
3952 bcopy (start2_addr, start1_addr, len2_byte);
3953 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3954 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte);
3955 if (len1_byte > 20000)
3956 xfree (temp);
3957 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3958 len1, current_buffer, 0);
3959 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3960 len_mid, current_buffer, 0);
3961 graft_intervals_into_buffer (tmp_interval2, start1,
3962 len2, current_buffer, 0);
3963 }
3964
3965 update_compositions (start1, start1 + len2, CHECK_BORDER);
3966 update_compositions (end2 - len1, end2, CHECK_BORDER);
3967 }
3968
3969 /* When doing multiple transpositions, it might be nice
3970 to optimize this. Perhaps the markers in any one buffer
3971 should be organized in some sorted data tree. */
3972 if (NILP (leave_markers))
3973 {
3974 transpose_markers (start1, end1, start2, end2,
3975 start1_byte, start1_byte + len1_byte,
3976 start2_byte, start2_byte + len2_byte);
3977 fix_overlays_in_range (start1, end2);
3978 }
3979
3980 return Qnil;
3981 }
3982
3983 \f
3984 void
3985 syms_of_editfns ()
3986 {
3987 environbuf = 0;
3988
3989 Qbuffer_access_fontify_functions
3990 = intern ("buffer-access-fontify-functions");
3991 staticpro (&Qbuffer_access_fontify_functions);
3992
3993 DEFVAR_LISP ("inhibit-field-text-motion", &Vinhibit_field_text_motion,
3994 doc: /* Non-nil means.text motion commands don't notice fields. */);
3995 Vinhibit_field_text_motion = Qnil;
3996
3997 DEFVAR_LISP ("buffer-access-fontify-functions",
3998 &Vbuffer_access_fontify_functions,
3999 doc: /* List of functions called by `buffer-substring' to fontify if necessary.
4000 Each function is called with two arguments which specify the range
4001 of the buffer being accessed. */);
4002 Vbuffer_access_fontify_functions = Qnil;
4003
4004 {
4005 Lisp_Object obuf;
4006 extern Lisp_Object Vprin1_to_string_buffer;
4007 obuf = Fcurrent_buffer ();
4008 /* Do this here, because init_buffer_once is too early--it won't work. */
4009 Fset_buffer (Vprin1_to_string_buffer);
4010 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
4011 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
4012 Qnil);
4013 Fset_buffer (obuf);
4014 }
4015
4016 DEFVAR_LISP ("buffer-access-fontified-property",
4017 &Vbuffer_access_fontified_property,
4018 doc: /* Property which (if non-nil) indicates text has been fontified.
4019 `buffer-substring' need not call the `buffer-access-fontify-functions'
4020 functions if all the text being accessed has this property. */);
4021 Vbuffer_access_fontified_property = Qnil;
4022
4023 DEFVAR_LISP ("system-name", &Vsystem_name,
4024 doc: /* The name of the machine Emacs is running on. */);
4025
4026 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
4027 doc: /* The full name of the user logged in. */);
4028
4029 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
4030 doc: /* The user's name, taken from environment variables if possible. */);
4031
4032 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
4033 doc: /* The user's name, based upon the real uid only. */);
4034
4035 defsubr (&Spropertize);
4036 defsubr (&Schar_equal);
4037 defsubr (&Sgoto_char);
4038 defsubr (&Sstring_to_char);
4039 defsubr (&Schar_to_string);
4040 defsubr (&Sbuffer_substring);
4041 defsubr (&Sbuffer_substring_no_properties);
4042 defsubr (&Sbuffer_string);
4043
4044 defsubr (&Spoint_marker);
4045 defsubr (&Smark_marker);
4046 defsubr (&Spoint);
4047 defsubr (&Sregion_beginning);
4048 defsubr (&Sregion_end);
4049
4050 staticpro (&Qfield);
4051 Qfield = intern ("field");
4052 staticpro (&Qboundary);
4053 Qboundary = intern ("boundary");
4054 defsubr (&Sfield_beginning);
4055 defsubr (&Sfield_end);
4056 defsubr (&Sfield_string);
4057 defsubr (&Sfield_string_no_properties);
4058 defsubr (&Sdelete_field);
4059 defsubr (&Sconstrain_to_field);
4060
4061 defsubr (&Sline_beginning_position);
4062 defsubr (&Sline_end_position);
4063
4064 /* defsubr (&Smark); */
4065 /* defsubr (&Sset_mark); */
4066 defsubr (&Ssave_excursion);
4067 defsubr (&Ssave_current_buffer);
4068
4069 defsubr (&Sbufsize);
4070 defsubr (&Spoint_max);
4071 defsubr (&Spoint_min);
4072 defsubr (&Spoint_min_marker);
4073 defsubr (&Spoint_max_marker);
4074 defsubr (&Sgap_position);
4075 defsubr (&Sgap_size);
4076 defsubr (&Sposition_bytes);
4077 defsubr (&Sbyte_to_position);
4078
4079 defsubr (&Sbobp);
4080 defsubr (&Seobp);
4081 defsubr (&Sbolp);
4082 defsubr (&Seolp);
4083 defsubr (&Sfollowing_char);
4084 defsubr (&Sprevious_char);
4085 defsubr (&Schar_after);
4086 defsubr (&Schar_before);
4087 defsubr (&Sinsert);
4088 defsubr (&Sinsert_before_markers);
4089 defsubr (&Sinsert_and_inherit);
4090 defsubr (&Sinsert_and_inherit_before_markers);
4091 defsubr (&Sinsert_char);
4092
4093 defsubr (&Suser_login_name);
4094 defsubr (&Suser_real_login_name);
4095 defsubr (&Suser_uid);
4096 defsubr (&Suser_real_uid);
4097 defsubr (&Suser_full_name);
4098 defsubr (&Semacs_pid);
4099 defsubr (&Scurrent_time);
4100 defsubr (&Sformat_time_string);
4101 defsubr (&Sfloat_time);
4102 defsubr (&Sdecode_time);
4103 defsubr (&Sencode_time);
4104 defsubr (&Scurrent_time_string);
4105 defsubr (&Scurrent_time_zone);
4106 defsubr (&Sset_time_zone_rule);
4107 defsubr (&Ssystem_name);
4108 defsubr (&Smessage);
4109 defsubr (&Smessage_box);
4110 defsubr (&Smessage_or_box);
4111 defsubr (&Scurrent_message);
4112 defsubr (&Sformat);
4113
4114 defsubr (&Sinsert_buffer_substring);
4115 defsubr (&Scompare_buffer_substrings);
4116 defsubr (&Ssubst_char_in_region);
4117 defsubr (&Stranslate_region);
4118 defsubr (&Sdelete_region);
4119 defsubr (&Sdelete_and_extract_region);
4120 defsubr (&Swiden);
4121 defsubr (&Snarrow_to_region);
4122 defsubr (&Ssave_restriction);
4123 defsubr (&Stranspose_regions);
4124 }