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