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