Merge from trunk.
[emacs.git] / src / editfns.c
blobafd4ed4833327a8c8d20bcea6bdabf127d313d2a
1 /* Lisp functions pertaining to editing.
3 Copyright (C) 1985-1987, 1989, 1993-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <setjmp.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
30 #include <unistd.h>
32 #ifdef HAVE_SYS_UTSNAME_H
33 #include <sys/utsname.h>
34 #endif
36 #include "lisp.h"
38 /* systime.h includes <sys/time.h> which, on some systems, is required
39 for <sys/resource.h>; thus systime.h must be included before
40 <sys/resource.h> */
41 #include "systime.h"
43 #if defined HAVE_SYS_RESOURCE_H
44 #include <sys/resource.h>
45 #endif
47 #include <ctype.h>
48 #include <float.h>
49 #include <limits.h>
50 #include <intprops.h>
51 #include <strftime.h>
52 #include <verify.h>
54 #include "intervals.h"
55 #include "buffer.h"
56 #include "character.h"
57 #include "coding.h"
58 #include "frame.h"
59 #include "window.h"
60 #include "blockinput.h"
62 #ifndef NULL
63 #define NULL 0
64 #endif
66 #ifndef USER_FULL_NAME
67 #define USER_FULL_NAME pw->pw_gecos
68 #endif
70 #ifndef USE_CRT_DLL
71 extern char **environ;
72 #endif
74 #define TM_YEAR_BASE 1900
76 /* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
77 asctime to have well-defined behavior. */
78 #ifndef TM_YEAR_IN_ASCTIME_RANGE
79 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
80 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
81 #endif
83 #ifdef WINDOWSNT
84 extern Lisp_Object w32_get_internal_run_time (void);
85 #endif
87 static void time_overflow (void) NO_RETURN;
88 static Lisp_Object format_time_string (char const *, ptrdiff_t, Lisp_Object,
89 int, time_t *, struct tm **);
90 static int tm_diff (struct tm *, struct tm *);
91 static void update_buffer_properties (ptrdiff_t, ptrdiff_t);
93 static Lisp_Object Qbuffer_access_fontify_functions;
94 static Lisp_Object Fuser_full_name (Lisp_Object);
96 /* Symbol for the text property used to mark fields. */
98 Lisp_Object Qfield;
100 /* A special value for Qfield properties. */
102 static Lisp_Object Qboundary;
105 void
106 init_editfns (void)
108 const char *user_name;
109 register char *p;
110 struct passwd *pw; /* password entry for the current user */
111 Lisp_Object tem;
113 /* Set up system_name even when dumping. */
114 init_system_name ();
116 #ifndef CANNOT_DUMP
117 /* Don't bother with this on initial start when just dumping out */
118 if (!initialized)
119 return;
120 #endif /* not CANNOT_DUMP */
122 pw = getpwuid (getuid ());
123 #ifdef MSDOS
124 /* We let the real user name default to "root" because that's quite
125 accurate on MSDOG and because it lets Emacs find the init file.
126 (The DVX libraries override the Djgpp libraries here.) */
127 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
128 #else
129 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
130 #endif
132 /* Get the effective user name, by consulting environment variables,
133 or the effective uid if those are unset. */
134 user_name = getenv ("LOGNAME");
135 if (!user_name)
136 #ifdef WINDOWSNT
137 user_name = getenv ("USERNAME"); /* it's USERNAME on NT */
138 #else /* WINDOWSNT */
139 user_name = getenv ("USER");
140 #endif /* WINDOWSNT */
141 if (!user_name)
143 pw = getpwuid (geteuid ());
144 user_name = pw ? pw->pw_name : "unknown";
146 Vuser_login_name = build_string (user_name);
148 /* If the user name claimed in the environment vars differs from
149 the real uid, use the claimed name to find the full name. */
150 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
151 if (! NILP (tem))
152 tem = Vuser_login_name;
153 else
155 uid_t euid = geteuid ();
156 tem = make_fixnum_or_float (euid);
158 Vuser_full_name = Fuser_full_name (tem);
160 p = getenv ("NAME");
161 if (p)
162 Vuser_full_name = build_string (p);
163 else if (NILP (Vuser_full_name))
164 Vuser_full_name = build_string ("unknown");
166 #ifdef HAVE_SYS_UTSNAME_H
168 struct utsname uts;
169 uname (&uts);
170 Voperating_system_release = build_string (uts.release);
172 #else
173 Voperating_system_release = Qnil;
174 #endif
177 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
178 doc: /* Convert arg CHAR to a string containing that character.
179 usage: (char-to-string CHAR) */)
180 (Lisp_Object character)
182 int c, len;
183 unsigned char str[MAX_MULTIBYTE_LENGTH];
185 CHECK_CHARACTER (character);
186 c = XFASTINT (character);
188 len = CHAR_STRING (c, str);
189 return make_string_from_bytes ((char *) str, 1, len);
192 DEFUN ("byte-to-string", Fbyte_to_string, Sbyte_to_string, 1, 1, 0,
193 doc: /* Convert arg BYTE to a unibyte string containing that byte. */)
194 (Lisp_Object byte)
196 unsigned char b;
197 CHECK_NUMBER (byte);
198 if (XINT (byte) < 0 || XINT (byte) > 255)
199 error ("Invalid byte");
200 b = XINT (byte);
201 return make_string_from_bytes ((char *) &b, 1, 1);
204 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
205 doc: /* Return the first character in STRING. */)
206 (register Lisp_Object string)
208 register Lisp_Object val;
209 CHECK_STRING (string);
210 if (SCHARS (string))
212 if (STRING_MULTIBYTE (string))
213 XSETFASTINT (val, STRING_CHAR (SDATA (string)));
214 else
215 XSETFASTINT (val, SREF (string, 0));
217 else
218 XSETFASTINT (val, 0);
219 return val;
222 static Lisp_Object
223 buildmark (ptrdiff_t charpos, ptrdiff_t bytepos)
225 register Lisp_Object mark;
226 mark = Fmake_marker ();
227 set_marker_both (mark, Qnil, charpos, bytepos);
228 return mark;
231 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
232 doc: /* Return value of point, as an integer.
233 Beginning of buffer is position (point-min). */)
234 (void)
236 Lisp_Object temp;
237 XSETFASTINT (temp, PT);
238 return temp;
241 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
242 doc: /* Return value of point, as a marker object. */)
243 (void)
245 return buildmark (PT, PT_BYTE);
248 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
249 doc: /* Set point to POSITION, a number or marker.
250 Beginning of buffer is position (point-min), end is (point-max).
252 The return value is POSITION. */)
253 (register Lisp_Object position)
255 ptrdiff_t pos;
257 if (MARKERP (position)
258 && current_buffer == XMARKER (position)->buffer)
260 pos = marker_position (position);
261 if (pos < BEGV)
262 SET_PT_BOTH (BEGV, BEGV_BYTE);
263 else if (pos > ZV)
264 SET_PT_BOTH (ZV, ZV_BYTE);
265 else
266 SET_PT_BOTH (pos, marker_byte_position (position));
268 return position;
271 CHECK_NUMBER_COERCE_MARKER (position);
273 pos = clip_to_bounds (BEGV, XINT (position), ZV);
274 SET_PT (pos);
275 return position;
279 /* Return the start or end position of the region.
280 BEGINNINGP non-zero means return the start.
281 If there is no region active, signal an error. */
283 static Lisp_Object
284 region_limit (int beginningp)
286 Lisp_Object m;
288 if (!NILP (Vtransient_mark_mode)
289 && NILP (Vmark_even_if_inactive)
290 && NILP (BVAR (current_buffer, mark_active)))
291 xsignal0 (Qmark_inactive);
293 m = Fmarker_position (BVAR (current_buffer, mark));
294 if (NILP (m))
295 error ("The mark is not set now, so there is no region");
297 if ((PT < XFASTINT (m)) == (beginningp != 0))
298 m = make_number (PT);
299 return m;
302 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
303 doc: /* Return the integer value of point or mark, whichever is smaller. */)
304 (void)
306 return region_limit (1);
309 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
310 doc: /* Return the integer value of point or mark, whichever is larger. */)
311 (void)
313 return region_limit (0);
316 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
317 doc: /* Return this buffer's mark, as a marker object.
318 Watch out! Moving this marker changes the mark position.
319 If you set the marker not to point anywhere, the buffer will have no mark. */)
320 (void)
322 return BVAR (current_buffer, mark);
326 /* Find all the overlays in the current buffer that touch position POS.
327 Return the number found, and store them in a vector in VEC
328 of length LEN. */
330 static ptrdiff_t
331 overlays_around (EMACS_INT pos, Lisp_Object *vec, ptrdiff_t len)
333 Lisp_Object overlay, start, end;
334 struct Lisp_Overlay *tail;
335 ptrdiff_t startpos, endpos;
336 ptrdiff_t idx = 0;
338 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
340 XSETMISC (overlay, tail);
342 end = OVERLAY_END (overlay);
343 endpos = OVERLAY_POSITION (end);
344 if (endpos < pos)
345 break;
346 start = OVERLAY_START (overlay);
347 startpos = OVERLAY_POSITION (start);
348 if (startpos <= pos)
350 if (idx < len)
351 vec[idx] = overlay;
352 /* Keep counting overlays even if we can't return them all. */
353 idx++;
357 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
359 XSETMISC (overlay, tail);
361 start = OVERLAY_START (overlay);
362 startpos = OVERLAY_POSITION (start);
363 if (pos < startpos)
364 break;
365 end = OVERLAY_END (overlay);
366 endpos = OVERLAY_POSITION (end);
367 if (pos <= endpos)
369 if (idx < len)
370 vec[idx] = overlay;
371 idx++;
375 return idx;
378 /* Return the value of property PROP, in OBJECT at POSITION.
379 It's the value of PROP that a char inserted at POSITION would get.
380 OBJECT is optional and defaults to the current buffer.
381 If OBJECT is a buffer, then overlay properties are considered as well as
382 text properties.
383 If OBJECT is a window, then that window's buffer is used, but
384 window-specific overlays are considered only if they are associated
385 with OBJECT. */
386 Lisp_Object
387 get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object object)
389 CHECK_NUMBER_COERCE_MARKER (position);
391 if (NILP (object))
392 XSETBUFFER (object, current_buffer);
393 else if (WINDOWP (object))
394 object = XWINDOW (object)->buffer;
396 if (!BUFFERP (object))
397 /* pos-property only makes sense in buffers right now, since strings
398 have no overlays and no notion of insertion for which stickiness
399 could be obeyed. */
400 return Fget_text_property (position, prop, object);
401 else
403 EMACS_INT posn = XINT (position);
404 ptrdiff_t noverlays;
405 Lisp_Object *overlay_vec, tem;
406 struct buffer *obuf = current_buffer;
408 set_buffer_temp (XBUFFER (object));
410 /* First try with room for 40 overlays. */
411 noverlays = 40;
412 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
413 noverlays = overlays_around (posn, overlay_vec, noverlays);
415 /* If there are more than 40,
416 make enough space for all, and try again. */
417 if (noverlays > 40)
419 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
420 noverlays = overlays_around (posn, overlay_vec, noverlays);
422 noverlays = sort_overlays (overlay_vec, noverlays, NULL);
424 set_buffer_temp (obuf);
426 /* Now check the overlays in order of decreasing priority. */
427 while (--noverlays >= 0)
429 Lisp_Object ol = overlay_vec[noverlays];
430 tem = Foverlay_get (ol, prop);
431 if (!NILP (tem))
433 /* Check the overlay is indeed active at point. */
434 Lisp_Object start = OVERLAY_START (ol), finish = OVERLAY_END (ol);
435 if ((OVERLAY_POSITION (start) == posn
436 && XMARKER (start)->insertion_type == 1)
437 || (OVERLAY_POSITION (finish) == posn
438 && XMARKER (finish)->insertion_type == 0))
439 ; /* The overlay will not cover a char inserted at point. */
440 else
442 return tem;
447 { /* Now check the text properties. */
448 int stickiness = text_property_stickiness (prop, position, object);
449 if (stickiness > 0)
450 return Fget_text_property (position, prop, object);
451 else if (stickiness < 0
452 && XINT (position) > BUF_BEGV (XBUFFER (object)))
453 return Fget_text_property (make_number (XINT (position) - 1),
454 prop, object);
455 else
456 return Qnil;
461 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
462 the value of point is used instead. If BEG or END is null,
463 means don't store the beginning or end of the field.
465 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
466 results; they do not effect boundary behavior.
468 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
469 position of a field, then the beginning of the previous field is
470 returned instead of the beginning of POS's field (since the end of a
471 field is actually also the beginning of the next input field, this
472 behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
473 true case, if two fields are separated by a field with the special
474 value `boundary', and POS lies within it, then the two separated
475 fields are considered to be adjacent, and POS between them, when
476 finding the beginning and ending of the "merged" field.
478 Either BEG or END may be 0, in which case the corresponding value
479 is not stored. */
481 static void
482 find_field (Lisp_Object pos, Lisp_Object merge_at_boundary,
483 Lisp_Object beg_limit,
484 ptrdiff_t *beg, Lisp_Object end_limit, ptrdiff_t *end)
486 /* Fields right before and after the point. */
487 Lisp_Object before_field, after_field;
488 /* 1 if POS counts as the start of a field. */
489 int at_field_start = 0;
490 /* 1 if POS counts as the end of a field. */
491 int at_field_end = 0;
493 if (NILP (pos))
494 XSETFASTINT (pos, PT);
495 else
496 CHECK_NUMBER_COERCE_MARKER (pos);
498 after_field
499 = get_char_property_and_overlay (pos, Qfield, Qnil, NULL);
500 before_field
501 = (XFASTINT (pos) > BEGV
502 ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
503 Qfield, Qnil, NULL)
504 /* Using nil here would be a more obvious choice, but it would
505 fail when the buffer starts with a non-sticky field. */
506 : after_field);
508 /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil
509 and POS is at beginning of a field, which can also be interpreted
510 as the end of the previous field. Note that the case where if
511 MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
512 more natural one; then we avoid treating the beginning of a field
513 specially. */
514 if (NILP (merge_at_boundary))
516 Lisp_Object field = get_pos_property (pos, Qfield, Qnil);
517 if (!EQ (field, after_field))
518 at_field_end = 1;
519 if (!EQ (field, before_field))
520 at_field_start = 1;
521 if (NILP (field) && at_field_start && at_field_end)
522 /* If an inserted char would have a nil field while the surrounding
523 text is non-nil, we're probably not looking at a
524 zero-length field, but instead at a non-nil field that's
525 not intended for editing (such as comint's prompts). */
526 at_field_end = at_field_start = 0;
529 /* Note about special `boundary' fields:
531 Consider the case where the point (`.') is between the fields `x' and `y':
533 xxxx.yyyy
535 In this situation, if merge_at_boundary is true, we consider the
536 `x' and `y' fields as forming one big merged field, and so the end
537 of the field is the end of `y'.
539 However, if `x' and `y' are separated by a special `boundary' field
540 (a field with a `field' char-property of 'boundary), then we ignore
541 this special field when merging adjacent fields. Here's the same
542 situation, but with a `boundary' field between the `x' and `y' fields:
544 xxx.BBBByyyy
546 Here, if point is at the end of `x', the beginning of `y', or
547 anywhere in-between (within the `boundary' field), we merge all
548 three fields and consider the beginning as being the beginning of
549 the `x' field, and the end as being the end of the `y' field. */
551 if (beg)
553 if (at_field_start)
554 /* POS is at the edge of a field, and we should consider it as
555 the beginning of the following field. */
556 *beg = XFASTINT (pos);
557 else
558 /* Find the previous field boundary. */
560 Lisp_Object p = pos;
561 if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary))
562 /* Skip a `boundary' field. */
563 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
564 beg_limit);
566 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
567 beg_limit);
568 *beg = NILP (p) ? BEGV : XFASTINT (p);
572 if (end)
574 if (at_field_end)
575 /* POS is at the edge of a field, and we should consider it as
576 the end of the previous field. */
577 *end = XFASTINT (pos);
578 else
579 /* Find the next field boundary. */
581 if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary))
582 /* Skip a `boundary' field. */
583 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
584 end_limit);
586 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
587 end_limit);
588 *end = NILP (pos) ? ZV : XFASTINT (pos);
594 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
595 doc: /* Delete the field surrounding POS.
596 A field is a region of text with the same `field' property.
597 If POS is nil, the value of point is used for POS. */)
598 (Lisp_Object pos)
600 ptrdiff_t beg, end;
601 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
602 if (beg != end)
603 del_range (beg, end);
604 return Qnil;
607 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
608 doc: /* Return the contents of the field surrounding POS as a string.
609 A field is a region of text with the same `field' property.
610 If POS is nil, the value of point is used for POS. */)
611 (Lisp_Object pos)
613 ptrdiff_t beg, end;
614 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
615 return make_buffer_string (beg, end, 1);
618 DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
619 doc: /* Return the contents of the field around POS, without text properties.
620 A field is a region of text with the same `field' property.
621 If POS is nil, the value of point is used for POS. */)
622 (Lisp_Object pos)
624 ptrdiff_t beg, end;
625 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
626 return make_buffer_string (beg, end, 0);
629 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
630 doc: /* Return the beginning of the field surrounding POS.
631 A field is a region of text with the same `field' property.
632 If POS is nil, the value of point is used for POS.
633 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
634 field, then the beginning of the *previous* field is returned.
635 If LIMIT is non-nil, it is a buffer position; if the beginning of the field
636 is before LIMIT, then LIMIT will be returned instead. */)
637 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
639 ptrdiff_t beg;
640 find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
641 return make_number (beg);
644 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
645 doc: /* Return the end of the field surrounding POS.
646 A field is a region of text with the same `field' property.
647 If POS is nil, the value of point is used for POS.
648 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
649 then the end of the *following* field is returned.
650 If LIMIT is non-nil, it is a buffer position; if the end of the field
651 is after LIMIT, then LIMIT will be returned instead. */)
652 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
654 ptrdiff_t end;
655 find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
656 return make_number (end);
659 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
660 doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
662 A field is a region of text with the same `field' property.
663 If NEW-POS is nil, then the current point is used instead, and set to the
664 constrained position if that is different.
666 If OLD-POS is at the boundary of two fields, then the allowable
667 positions for NEW-POS depends on the value of the optional argument
668 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
669 constrained to the field that has the same `field' char-property
670 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
671 is non-nil, NEW-POS is constrained to the union of the two adjacent
672 fields. Additionally, if two fields are separated by another field with
673 the special value `boundary', then any point within this special field is
674 also considered to be `on the boundary'.
676 If the optional argument ONLY-IN-LINE is non-nil and constraining
677 NEW-POS would move it to a different line, NEW-POS is returned
678 unconstrained. This useful for commands that move by line, like
679 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
680 only in the case where they can still move to the right line.
682 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
683 a non-nil property of that name, then any field boundaries are ignored.
685 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */)
686 (Lisp_Object new_pos, Lisp_Object old_pos, Lisp_Object escape_from_edge, Lisp_Object only_in_line, Lisp_Object inhibit_capture_property)
688 /* If non-zero, then the original point, before re-positioning. */
689 ptrdiff_t orig_point = 0;
690 int fwd;
691 Lisp_Object prev_old, prev_new;
693 if (NILP (new_pos))
694 /* Use the current point, and afterwards, set it. */
696 orig_point = PT;
697 XSETFASTINT (new_pos, PT);
700 CHECK_NUMBER_COERCE_MARKER (new_pos);
701 CHECK_NUMBER_COERCE_MARKER (old_pos);
703 fwd = (XINT (new_pos) > XINT (old_pos));
705 prev_old = make_number (XINT (old_pos) - 1);
706 prev_new = make_number (XINT (new_pos) - 1);
708 if (NILP (Vinhibit_field_text_motion)
709 && !EQ (new_pos, old_pos)
710 && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
711 || !NILP (Fget_char_property (old_pos, Qfield, Qnil))
712 /* To recognize field boundaries, we must also look at the
713 previous positions; we could use `get_pos_property'
714 instead, but in itself that would fail inside non-sticky
715 fields (like comint prompts). */
716 || (XFASTINT (new_pos) > BEGV
717 && !NILP (Fget_char_property (prev_new, Qfield, Qnil)))
718 || (XFASTINT (old_pos) > BEGV
719 && !NILP (Fget_char_property (prev_old, Qfield, Qnil))))
720 && (NILP (inhibit_capture_property)
721 /* Field boundaries are again a problem; but now we must
722 decide the case exactly, so we need to call
723 `get_pos_property' as well. */
724 || (NILP (get_pos_property (old_pos, inhibit_capture_property, Qnil))
725 && (XFASTINT (old_pos) <= BEGV
726 || NILP (Fget_char_property (old_pos, inhibit_capture_property, Qnil))
727 || NILP (Fget_char_property (prev_old, inhibit_capture_property, Qnil))))))
728 /* It is possible that NEW_POS is not within the same field as
729 OLD_POS; try to move NEW_POS so that it is. */
731 ptrdiff_t shortage;
732 Lisp_Object field_bound;
734 if (fwd)
735 field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
736 else
737 field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
739 if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
740 other side of NEW_POS, which would mean that NEW_POS is
741 already acceptable, and it's not necessary to constrain it
742 to FIELD_BOUND. */
743 ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd)
744 /* NEW_POS should be constrained, but only if either
745 ONLY_IN_LINE is nil (in which case any constraint is OK),
746 or NEW_POS and FIELD_BOUND are on the same line (in which
747 case the constraint is OK even if ONLY_IN_LINE is non-nil). */
748 && (NILP (only_in_line)
749 /* This is the ONLY_IN_LINE case, check that NEW_POS and
750 FIELD_BOUND are on the same line by seeing whether
751 there's an intervening newline or not. */
752 || (scan_buffer ('\n',
753 XFASTINT (new_pos), XFASTINT (field_bound),
754 fwd ? -1 : 1, &shortage, 1),
755 shortage != 0)))
756 /* Constrain NEW_POS to FIELD_BOUND. */
757 new_pos = field_bound;
759 if (orig_point && XFASTINT (new_pos) != orig_point)
760 /* The NEW_POS argument was originally nil, so automatically set PT. */
761 SET_PT (XFASTINT (new_pos));
764 return new_pos;
768 DEFUN ("line-beginning-position",
769 Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
770 doc: /* Return the character position of the first character on the current line.
771 With argument N not nil or 1, move forward N - 1 lines first.
772 If scan reaches end of buffer, return that position.
774 The returned position is of the first character in the logical order,
775 i.e. the one that has the smallest character position.
777 This function constrains the returned position to the current field
778 unless that would be on a different line than the original,
779 unconstrained result. If N is nil or 1, and a front-sticky field
780 starts at point, the scan stops as soon as it starts. To ignore field
781 boundaries bind `inhibit-field-text-motion' to t.
783 This function does not move point. */)
784 (Lisp_Object n)
786 ptrdiff_t orig, orig_byte, end;
787 ptrdiff_t count = SPECPDL_INDEX ();
788 specbind (Qinhibit_point_motion_hooks, Qt);
790 if (NILP (n))
791 XSETFASTINT (n, 1);
792 else
793 CHECK_NUMBER (n);
795 orig = PT;
796 orig_byte = PT_BYTE;
797 Fforward_line (make_number (XINT (n) - 1));
798 end = PT;
800 SET_PT_BOTH (orig, orig_byte);
802 unbind_to (count, Qnil);
804 /* Return END constrained to the current input field. */
805 return Fconstrain_to_field (make_number (end), make_number (orig),
806 XINT (n) != 1 ? Qt : Qnil,
807 Qt, Qnil);
810 DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0,
811 doc: /* Return the character position of the last character on the current line.
812 With argument N not nil or 1, move forward N - 1 lines first.
813 If scan reaches end of buffer, return that position.
815 The returned position is of the last character in the logical order,
816 i.e. the character whose buffer position is the largest one.
818 This function constrains the returned position to the current field
819 unless that would be on a different line than the original,
820 unconstrained result. If N is nil or 1, and a rear-sticky field ends
821 at point, the scan stops as soon as it starts. To ignore field
822 boundaries bind `inhibit-field-text-motion' to t.
824 This function does not move point. */)
825 (Lisp_Object n)
827 ptrdiff_t clipped_n;
828 ptrdiff_t end_pos;
829 ptrdiff_t orig = PT;
831 if (NILP (n))
832 XSETFASTINT (n, 1);
833 else
834 CHECK_NUMBER (n);
836 clipped_n = clip_to_bounds (PTRDIFF_MIN + 1, XINT (n), PTRDIFF_MAX);
837 end_pos = find_before_next_newline (orig, 0, clipped_n - (clipped_n <= 0));
839 /* Return END_POS constrained to the current input field. */
840 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
841 Qnil, Qt, Qnil);
845 Lisp_Object
846 save_excursion_save (void)
848 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
849 == current_buffer);
851 return Fcons (Fpoint_marker (),
852 Fcons (Fcopy_marker (BVAR (current_buffer, mark), Qnil),
853 Fcons (visible ? Qt : Qnil,
854 Fcons (BVAR (current_buffer, mark_active),
855 selected_window))));
858 Lisp_Object
859 save_excursion_restore (Lisp_Object info)
861 Lisp_Object tem, tem1, omark, nmark;
862 struct gcpro gcpro1, gcpro2, gcpro3;
863 int visible_p;
865 tem = Fmarker_buffer (XCAR (info));
866 /* If buffer being returned to is now deleted, avoid error */
867 /* Otherwise could get error here while unwinding to top level
868 and crash */
869 /* In that case, Fmarker_buffer returns nil now. */
870 if (NILP (tem))
871 return Qnil;
873 omark = nmark = Qnil;
874 GCPRO3 (info, omark, nmark);
876 Fset_buffer (tem);
878 /* Point marker. */
879 tem = XCAR (info);
880 Fgoto_char (tem);
881 unchain_marker (XMARKER (tem));
883 /* Mark marker. */
884 info = XCDR (info);
885 tem = XCAR (info);
886 omark = Fmarker_position (BVAR (current_buffer, mark));
887 Fset_marker (BVAR (current_buffer, mark), tem, Fcurrent_buffer ());
888 nmark = Fmarker_position (tem);
889 unchain_marker (XMARKER (tem));
891 /* visible */
892 info = XCDR (info);
893 visible_p = !NILP (XCAR (info));
895 #if 0 /* We used to make the current buffer visible in the selected window
896 if that was true previously. That avoids some anomalies.
897 But it creates others, and it wasn't documented, and it is simpler
898 and cleaner never to alter the window/buffer connections. */
899 tem1 = Fcar (tem);
900 if (!NILP (tem1)
901 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
902 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
903 #endif /* 0 */
905 /* Mark active */
906 info = XCDR (info);
907 tem = XCAR (info);
908 tem1 = BVAR (current_buffer, mark_active);
909 BVAR (current_buffer, mark_active) = tem;
911 /* If mark is active now, and either was not active
912 or was at a different place, run the activate hook. */
913 if (! NILP (tem))
915 if (! EQ (omark, nmark))
917 tem = intern ("activate-mark-hook");
918 Frun_hooks (1, &tem);
921 /* If mark has ceased to be active, run deactivate hook. */
922 else if (! NILP (tem1))
924 tem = intern ("deactivate-mark-hook");
925 Frun_hooks (1, &tem);
928 /* If buffer was visible in a window, and a different window was
929 selected, and the old selected window is still showing this
930 buffer, restore point in that window. */
931 tem = XCDR (info);
932 if (visible_p
933 && !EQ (tem, selected_window)
934 && (tem1 = XWINDOW (tem)->buffer,
935 (/* Window is live... */
936 BUFFERP (tem1)
937 /* ...and it shows the current buffer. */
938 && XBUFFER (tem1) == current_buffer)))
939 Fset_window_point (tem, make_number (PT));
941 UNGCPRO;
942 return Qnil;
945 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
946 doc: /* Save point, mark, and current buffer; execute BODY; restore those things.
947 Executes BODY just like `progn'.
948 The values of point, mark and the current buffer are restored
949 even in case of abnormal exit (throw or error).
950 The state of activation of the mark is also restored.
952 This construct does not save `deactivate-mark', and therefore
953 functions that change the buffer will still cause deactivation
954 of the mark at the end of the command. To prevent that, bind
955 `deactivate-mark' with `let'.
957 If you only want to save the current buffer but not point nor mark,
958 then just use `save-current-buffer', or even `with-current-buffer'.
960 usage: (save-excursion &rest BODY) */)
961 (Lisp_Object args)
963 register Lisp_Object val;
964 ptrdiff_t count = SPECPDL_INDEX ();
966 record_unwind_protect (save_excursion_restore, save_excursion_save ());
968 val = Fprogn (args);
969 return unbind_to (count, val);
972 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
973 doc: /* Save the current buffer; execute BODY; restore the current buffer.
974 Executes BODY just like `progn'.
975 usage: (save-current-buffer &rest BODY) */)
976 (Lisp_Object args)
978 Lisp_Object val;
979 ptrdiff_t count = SPECPDL_INDEX ();
981 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
983 val = Fprogn (args);
984 return unbind_to (count, val);
987 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
988 doc: /* Return the number of characters in the current buffer.
989 If BUFFER, return the number of characters in that buffer instead. */)
990 (Lisp_Object buffer)
992 if (NILP (buffer))
993 return make_number (Z - BEG);
994 else
996 CHECK_BUFFER (buffer);
997 return make_number (BUF_Z (XBUFFER (buffer))
998 - BUF_BEG (XBUFFER (buffer)));
1002 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
1003 doc: /* Return the minimum permissible value of point in the current buffer.
1004 This is 1, unless narrowing (a buffer restriction) is in effect. */)
1005 (void)
1007 Lisp_Object temp;
1008 XSETFASTINT (temp, BEGV);
1009 return temp;
1012 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
1013 doc: /* Return a marker to the minimum permissible value of point in this buffer.
1014 This is the beginning, unless narrowing (a buffer restriction) is in effect. */)
1015 (void)
1017 return buildmark (BEGV, BEGV_BYTE);
1020 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
1021 doc: /* Return the maximum permissible value of point in the current buffer.
1022 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1023 is in effect, in which case it is less. */)
1024 (void)
1026 Lisp_Object temp;
1027 XSETFASTINT (temp, ZV);
1028 return temp;
1031 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
1032 doc: /* Return a marker to the maximum permissible value of point in this buffer.
1033 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1034 is in effect, in which case it is less. */)
1035 (void)
1037 return buildmark (ZV, ZV_BYTE);
1040 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
1041 doc: /* Return the position of the gap, in the current buffer.
1042 See also `gap-size'. */)
1043 (void)
1045 Lisp_Object temp;
1046 XSETFASTINT (temp, GPT);
1047 return temp;
1050 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
1051 doc: /* Return the size of the current buffer's gap.
1052 See also `gap-position'. */)
1053 (void)
1055 Lisp_Object temp;
1056 XSETFASTINT (temp, GAP_SIZE);
1057 return temp;
1060 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
1061 doc: /* Return the byte position for character position POSITION.
1062 If POSITION is out of range, the value is nil. */)
1063 (Lisp_Object position)
1065 CHECK_NUMBER_COERCE_MARKER (position);
1066 if (XINT (position) < BEG || XINT (position) > Z)
1067 return Qnil;
1068 return make_number (CHAR_TO_BYTE (XINT (position)));
1071 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
1072 doc: /* Return the character position for byte position BYTEPOS.
1073 If BYTEPOS is out of range, the value is nil. */)
1074 (Lisp_Object bytepos)
1076 CHECK_NUMBER (bytepos);
1077 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
1078 return Qnil;
1079 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
1082 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
1083 doc: /* Return the character following point, as a number.
1084 At the end of the buffer or accessible region, return 0. */)
1085 (void)
1087 Lisp_Object temp;
1088 if (PT >= ZV)
1089 XSETFASTINT (temp, 0);
1090 else
1091 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
1092 return temp;
1095 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
1096 doc: /* Return the character preceding point, as a number.
1097 At the beginning of the buffer or accessible region, return 0. */)
1098 (void)
1100 Lisp_Object temp;
1101 if (PT <= BEGV)
1102 XSETFASTINT (temp, 0);
1103 else if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1105 ptrdiff_t pos = PT_BYTE;
1106 DEC_POS (pos);
1107 XSETFASTINT (temp, FETCH_CHAR (pos));
1109 else
1110 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
1111 return temp;
1114 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
1115 doc: /* Return t if point is at the beginning of the buffer.
1116 If the buffer is narrowed, this means the beginning of the narrowed part. */)
1117 (void)
1119 if (PT == BEGV)
1120 return Qt;
1121 return Qnil;
1124 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
1125 doc: /* Return t if point is at the end of the buffer.
1126 If the buffer is narrowed, this means the end of the narrowed part. */)
1127 (void)
1129 if (PT == ZV)
1130 return Qt;
1131 return Qnil;
1134 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
1135 doc: /* Return t if point is at the beginning of a line. */)
1136 (void)
1138 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
1139 return Qt;
1140 return Qnil;
1143 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
1144 doc: /* Return t if point is at the end of a line.
1145 `End of a line' includes point being at the end of the buffer. */)
1146 (void)
1148 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
1149 return Qt;
1150 return Qnil;
1153 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
1154 doc: /* Return character in current buffer at position POS.
1155 POS is an integer or a marker and defaults to point.
1156 If POS is out of range, the value is nil. */)
1157 (Lisp_Object pos)
1159 register ptrdiff_t pos_byte;
1161 if (NILP (pos))
1163 pos_byte = PT_BYTE;
1164 XSETFASTINT (pos, PT);
1167 if (MARKERP (pos))
1169 pos_byte = marker_byte_position (pos);
1170 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1171 return Qnil;
1173 else
1175 CHECK_NUMBER_COERCE_MARKER (pos);
1176 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1177 return Qnil;
1179 pos_byte = CHAR_TO_BYTE (XINT (pos));
1182 return make_number (FETCH_CHAR (pos_byte));
1185 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1186 doc: /* Return character in current buffer preceding position POS.
1187 POS is an integer or a marker and defaults to point.
1188 If POS is out of range, the value is nil. */)
1189 (Lisp_Object pos)
1191 register Lisp_Object val;
1192 register ptrdiff_t pos_byte;
1194 if (NILP (pos))
1196 pos_byte = PT_BYTE;
1197 XSETFASTINT (pos, PT);
1200 if (MARKERP (pos))
1202 pos_byte = marker_byte_position (pos);
1204 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1205 return Qnil;
1207 else
1209 CHECK_NUMBER_COERCE_MARKER (pos);
1211 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1212 return Qnil;
1214 pos_byte = CHAR_TO_BYTE (XINT (pos));
1217 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1219 DEC_POS (pos_byte);
1220 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1222 else
1224 pos_byte--;
1225 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1227 return val;
1230 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1231 doc: /* Return the name under which the user logged in, as a string.
1232 This is based on the effective uid, not the real uid.
1233 Also, if the environment variables LOGNAME or USER are set,
1234 that determines the value of this function.
1236 If optional argument UID is an integer or a float, return the login name
1237 of the user with that uid, or nil if there is no such user. */)
1238 (Lisp_Object uid)
1240 struct passwd *pw;
1241 uid_t id;
1243 /* Set up the user name info if we didn't do it before.
1244 (That can happen if Emacs is dumpable
1245 but you decide to run `temacs -l loadup' and not dump. */
1246 if (INTEGERP (Vuser_login_name))
1247 init_editfns ();
1249 if (NILP (uid))
1250 return Vuser_login_name;
1252 CONS_TO_INTEGER (uid, uid_t, id);
1253 BLOCK_INPUT;
1254 pw = getpwuid (id);
1255 UNBLOCK_INPUT;
1256 return (pw ? build_string (pw->pw_name) : Qnil);
1259 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1260 0, 0, 0,
1261 doc: /* Return the name of the user's real uid, as a string.
1262 This ignores the environment variables LOGNAME and USER, so it differs from
1263 `user-login-name' when running under `su'. */)
1264 (void)
1266 /* Set up the user name info if we didn't do it before.
1267 (That can happen if Emacs is dumpable
1268 but you decide to run `temacs -l loadup' and not dump. */
1269 if (INTEGERP (Vuser_login_name))
1270 init_editfns ();
1271 return Vuser_real_login_name;
1274 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1275 doc: /* Return the effective uid of Emacs.
1276 Value is an integer or a float, depending on the value. */)
1277 (void)
1279 uid_t euid = geteuid ();
1280 return make_fixnum_or_float (euid);
1283 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1284 doc: /* Return the real uid of Emacs.
1285 Value is an integer or a float, depending on the value. */)
1286 (void)
1288 uid_t uid = getuid ();
1289 return make_fixnum_or_float (uid);
1292 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1293 doc: /* Return the full name of the user logged in, as a string.
1294 If the full name corresponding to Emacs's userid is not known,
1295 return "unknown".
1297 If optional argument UID is an integer or float, return the full name
1298 of the user with that uid, or nil if there is no such user.
1299 If UID is a string, return the full name of the user with that login
1300 name, or nil if there is no such user. */)
1301 (Lisp_Object uid)
1303 struct passwd *pw;
1304 register char *p, *q;
1305 Lisp_Object full;
1307 if (NILP (uid))
1308 return Vuser_full_name;
1309 else if (NUMBERP (uid))
1311 uid_t u;
1312 CONS_TO_INTEGER (uid, uid_t, u);
1313 BLOCK_INPUT;
1314 pw = getpwuid (u);
1315 UNBLOCK_INPUT;
1317 else if (STRINGP (uid))
1319 BLOCK_INPUT;
1320 pw = getpwnam (SSDATA (uid));
1321 UNBLOCK_INPUT;
1323 else
1324 error ("Invalid UID specification");
1326 if (!pw)
1327 return Qnil;
1329 p = USER_FULL_NAME;
1330 /* Chop off everything after the first comma. */
1331 q = strchr (p, ',');
1332 full = make_string (p, q ? q - p : strlen (p));
1334 #ifdef AMPERSAND_FULL_NAME
1335 p = SSDATA (full);
1336 q = strchr (p, '&');
1337 /* Substitute the login name for the &, upcasing the first character. */
1338 if (q)
1340 register char *r;
1341 Lisp_Object login;
1343 login = Fuser_login_name (make_number (pw->pw_uid));
1344 r = (char *) alloca (strlen (p) + SCHARS (login) + 1);
1345 memcpy (r, p, q - p);
1346 r[q - p] = 0;
1347 strcat (r, SSDATA (login));
1348 r[q - p] = upcase ((unsigned char) r[q - p]);
1349 strcat (r, q + 1);
1350 full = build_string (r);
1352 #endif /* AMPERSAND_FULL_NAME */
1354 return full;
1357 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1358 doc: /* Return the host name of the machine you are running on, as a string. */)
1359 (void)
1361 return Vsystem_name;
1364 const char *
1365 get_system_name (void)
1367 if (STRINGP (Vsystem_name))
1368 return SSDATA (Vsystem_name);
1369 else
1370 return "";
1373 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1374 doc: /* Return the process ID of Emacs, as a number. */)
1375 (void)
1377 pid_t pid = getpid ();
1378 return make_fixnum_or_float (pid);
1383 #ifndef TIME_T_MIN
1384 # define TIME_T_MIN TYPE_MINIMUM (time_t)
1385 #endif
1386 #ifndef TIME_T_MAX
1387 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
1388 #endif
1390 /* Report that a time value is out of range for Emacs. */
1391 static void
1392 time_overflow (void)
1394 error ("Specified time is not representable");
1397 /* Return the upper part of the time T (everything but the bottom 16 bits),
1398 making sure that it is representable. */
1399 static EMACS_INT
1400 hi_time (time_t t)
1402 time_t hi = t >> 16;
1404 /* Check for overflow, helping the compiler for common cases where
1405 no runtime check is needed, and taking care not to convert
1406 negative numbers to unsigned before comparing them. */
1407 if (! ((! TYPE_SIGNED (time_t)
1408 || MOST_NEGATIVE_FIXNUM <= TIME_T_MIN >> 16
1409 || MOST_NEGATIVE_FIXNUM <= hi)
1410 && (TIME_T_MAX >> 16 <= MOST_POSITIVE_FIXNUM
1411 || hi <= MOST_POSITIVE_FIXNUM)))
1412 time_overflow ();
1414 return hi;
1417 /* Return the bottom 16 bits of the time T. */
1418 static int
1419 lo_time (time_t t)
1421 return t & ((1 << 16) - 1);
1424 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1425 doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
1426 The time is returned as a list of three integers. The first has the
1427 most significant 16 bits of the seconds, while the second has the
1428 least significant 16 bits. The third integer gives the microsecond
1429 count.
1431 The microsecond count is zero on systems that do not provide
1432 resolution finer than a second. */)
1433 (void)
1435 EMACS_TIME t;
1437 EMACS_GET_TIME (t);
1438 return list3 (make_number (hi_time (EMACS_SECS (t))),
1439 make_number (lo_time (EMACS_SECS (t))),
1440 make_number (EMACS_USECS (t)));
1443 DEFUN ("get-internal-run-time", Fget_internal_run_time, Sget_internal_run_time,
1444 0, 0, 0,
1445 doc: /* Return the current run time used by Emacs.
1446 The time is returned as a list of three integers. The first has the
1447 most significant 16 bits of the seconds, while the second has the
1448 least significant 16 bits. The third integer gives the microsecond
1449 count.
1451 On systems that can't determine the run time, `get-internal-run-time'
1452 does the same thing as `current-time'. The microsecond count is zero
1453 on systems that do not provide resolution finer than a second. */)
1454 (void)
1456 #ifdef HAVE_GETRUSAGE
1457 struct rusage usage;
1458 time_t secs;
1459 int usecs;
1461 if (getrusage (RUSAGE_SELF, &usage) < 0)
1462 /* This shouldn't happen. What action is appropriate? */
1463 xsignal0 (Qerror);
1465 /* Sum up user time and system time. */
1466 secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
1467 usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
1468 if (usecs >= 1000000)
1470 usecs -= 1000000;
1471 secs++;
1474 return list3 (make_number (hi_time (secs)),
1475 make_number (lo_time (secs)),
1476 make_number (usecs));
1477 #else /* ! HAVE_GETRUSAGE */
1478 #ifdef WINDOWSNT
1479 return w32_get_internal_run_time ();
1480 #else /* ! WINDOWSNT */
1481 return Fcurrent_time ();
1482 #endif /* WINDOWSNT */
1483 #endif /* HAVE_GETRUSAGE */
1487 /* Make a Lisp list that represents the time T. */
1488 Lisp_Object
1489 make_time (time_t t)
1491 return list2 (make_number (hi_time (t)),
1492 make_number (lo_time (t)));
1495 /* Decode a Lisp list SPECIFIED_TIME that represents a time.
1496 If SPECIFIED_TIME is nil, use the current time.
1497 Set *RESULT to seconds since the Epoch.
1498 If USEC is not null, set *USEC to the microseconds component.
1499 Return nonzero if successful. */
1501 lisp_time_argument (Lisp_Object specified_time, time_t *result, int *usec)
1503 if (NILP (specified_time))
1505 if (usec)
1507 EMACS_TIME t;
1509 EMACS_GET_TIME (t);
1510 *usec = EMACS_USECS (t);
1511 *result = EMACS_SECS (t);
1512 return 1;
1514 else
1515 return time (result) != -1;
1517 else
1519 Lisp_Object high, low;
1520 EMACS_INT hi;
1521 high = Fcar (specified_time);
1522 CHECK_NUMBER (high);
1523 low = Fcdr (specified_time);
1524 if (CONSP (low))
1526 if (usec)
1528 Lisp_Object usec_l = Fcdr (low);
1529 if (CONSP (usec_l))
1530 usec_l = Fcar (usec_l);
1531 if (NILP (usec_l))
1532 *usec = 0;
1533 else
1535 CHECK_NUMBER (usec_l);
1536 if (! (0 <= XINT (usec_l) && XINT (usec_l) < 1000000))
1537 return 0;
1538 *usec = XINT (usec_l);
1541 low = Fcar (low);
1543 else if (usec)
1544 *usec = 0;
1545 CHECK_NUMBER (low);
1546 hi = XINT (high);
1548 /* Check for overflow, helping the compiler for common cases
1549 where no runtime check is needed, and taking care not to
1550 convert negative numbers to unsigned before comparing them. */
1551 if (! ((TYPE_SIGNED (time_t)
1552 ? (TIME_T_MIN >> 16 <= MOST_NEGATIVE_FIXNUM
1553 || TIME_T_MIN >> 16 <= hi)
1554 : 0 <= hi)
1555 && (MOST_POSITIVE_FIXNUM <= TIME_T_MAX >> 16
1556 || hi <= TIME_T_MAX >> 16)))
1557 return 0;
1559 *result = (hi << 16) + (XINT (low) & 0xffff);
1560 return 1;
1564 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1565 doc: /* Return the current time, as a float number of seconds since the epoch.
1566 If SPECIFIED-TIME is given, it is the time to convert to float
1567 instead of the current time. The argument should have the form
1568 (HIGH LOW) or (HIGH LOW USEC). Thus, you can use times obtained from
1569 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1570 have the form (HIGH . LOW), but this is considered obsolete.
1572 WARNING: Since the result is floating point, it may not be exact.
1573 If precise time stamps are required, use either `current-time',
1574 or (if you need time as a string) `format-time-string'. */)
1575 (Lisp_Object specified_time)
1577 time_t sec;
1578 int usec;
1580 if (! lisp_time_argument (specified_time, &sec, &usec))
1581 error ("Invalid time specification");
1583 return make_float ((sec * 1e6 + usec) / 1e6);
1586 /* Write information into buffer S of size MAXSIZE, according to the
1587 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1588 Default to Universal Time if UT is nonzero, local time otherwise.
1589 Use NS as the number of nanoseconds in the %N directive.
1590 Return the number of bytes written, not including the terminating
1591 '\0'. If S is NULL, nothing will be written anywhere; so to
1592 determine how many bytes would be written, use NULL for S and
1593 ((size_t) -1) for MAXSIZE.
1595 This function behaves like nstrftime, except it allows null
1596 bytes in FORMAT and it does not support nanoseconds. */
1597 static size_t
1598 emacs_nmemftime (char *s, size_t maxsize, const char *format,
1599 size_t format_len, const struct tm *tp, int ut, int ns)
1601 size_t total = 0;
1603 /* Loop through all the null-terminated strings in the format
1604 argument. Normally there's just one null-terminated string, but
1605 there can be arbitrarily many, concatenated together, if the
1606 format contains '\0' bytes. nstrftime stops at the first
1607 '\0' byte so we must invoke it separately for each such string. */
1608 for (;;)
1610 size_t len;
1611 size_t result;
1613 if (s)
1614 s[0] = '\1';
1616 result = nstrftime (s, maxsize, format, tp, ut, ns);
1618 if (s)
1620 if (result == 0 && s[0] != '\0')
1621 return 0;
1622 s += result + 1;
1625 maxsize -= result + 1;
1626 total += result;
1627 len = strlen (format);
1628 if (len == format_len)
1629 return total;
1630 total++;
1631 format += len + 1;
1632 format_len -= len + 1;
1636 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1637 doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted.
1638 TIME is specified as (HIGH LOW . IGNORED), as returned by
1639 `current-time' or `file-attributes'. The obsolete form (HIGH . LOW)
1640 is also still accepted.
1641 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME
1642 as Universal Time; nil means describe TIME in the local time zone.
1643 The value is a copy of FORMAT-STRING, but with certain constructs replaced
1644 by text that describes the specified date and time in TIME:
1646 %Y is the year, %y within the century, %C the century.
1647 %G is the year corresponding to the ISO week, %g within the century.
1648 %m is the numeric month.
1649 %b and %h are the locale's abbreviated month name, %B the full name.
1650 %d is the day of the month, zero-padded, %e is blank-padded.
1651 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
1652 %a is the locale's abbreviated name of the day of week, %A the full name.
1653 %U is the week number starting on Sunday, %W starting on Monday,
1654 %V according to ISO 8601.
1655 %j is the day of the year.
1657 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
1658 only blank-padded, %l is like %I blank-padded.
1659 %p is the locale's equivalent of either AM or PM.
1660 %M is the minute.
1661 %S is the second.
1662 %N is the nanosecond, %6N the microsecond, %3N the millisecond, etc.
1663 %Z is the time zone name, %z is the numeric form.
1664 %s is the number of seconds since 1970-01-01 00:00:00 +0000.
1666 %c is the locale's date and time format.
1667 %x is the locale's "preferred" date format.
1668 %D is like "%m/%d/%y".
1670 %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p".
1671 %X is the locale's "preferred" time format.
1673 Finally, %n is a newline, %t is a tab, %% is a literal %.
1675 Certain flags and modifiers are available with some format controls.
1676 The flags are `_', `-', `^' and `#'. For certain characters X,
1677 %_X is like %X, but padded with blanks; %-X is like %X,
1678 but without padding. %^X is like %X, but with all textual
1679 characters up-cased; %#X is like %X, but with letter-case of
1680 all textual characters reversed.
1681 %NX (where N stands for an integer) is like %X,
1682 but takes up at least N (a number) positions.
1683 The modifiers are `E' and `O'. For certain characters X,
1684 %EX is a locale's alternative version of %X;
1685 %OX is like %X, but uses the locale's number symbols.
1687 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".
1689 usage: (format-time-string FORMAT-STRING &optional TIME UNIVERSAL) */)
1690 (Lisp_Object format_string, Lisp_Object timeval, Lisp_Object universal)
1692 time_t t;
1693 struct tm *tm;
1695 CHECK_STRING (format_string);
1696 format_string = code_convert_string_norecord (format_string,
1697 Vlocale_coding_system, 1);
1698 return format_time_string (SSDATA (format_string), SBYTES (format_string),
1699 timeval, ! NILP (universal), &t, &tm);
1702 static Lisp_Object
1703 format_time_string (char const *format, ptrdiff_t formatlen,
1704 Lisp_Object timeval, int ut, time_t *tval, struct tm **tmp)
1706 ptrdiff_t size;
1707 int usec;
1708 int ns;
1709 struct tm *tm;
1711 if (! lisp_time_argument (timeval, tval, &usec))
1712 error ("Invalid time specification");
1713 ns = usec * 1000;
1715 /* This is probably enough. */
1716 size = formatlen;
1717 if (size <= (STRING_BYTES_BOUND - 50) / 6)
1718 size = size * 6 + 50;
1720 BLOCK_INPUT;
1721 tm = ut ? gmtime (tval) : localtime (tval);
1722 UNBLOCK_INPUT;
1723 if (! tm)
1724 time_overflow ();
1725 *tmp = tm;
1727 synchronize_system_time_locale ();
1729 while (1)
1731 char *buf = (char *) alloca (size + 1);
1732 size_t result;
1734 buf[0] = '\1';
1735 BLOCK_INPUT;
1736 result = emacs_nmemftime (buf, size, format, formatlen, tm, ut, ns);
1737 UNBLOCK_INPUT;
1738 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
1739 return code_convert_string_norecord (make_unibyte_string (buf, result),
1740 Vlocale_coding_system, 0);
1742 /* If buffer was too small, make it bigger and try again. */
1743 BLOCK_INPUT;
1744 result = emacs_nmemftime (NULL, (size_t) -1, format, formatlen,
1745 tm, ut, ns);
1746 UNBLOCK_INPUT;
1747 if (STRING_BYTES_BOUND <= result)
1748 string_overflow ();
1749 size = result + 1;
1753 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1754 doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
1755 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED),
1756 as from `current-time' and `file-attributes', or nil to use the
1757 current time. The obsolete form (HIGH . LOW) is also still accepted.
1758 The list has the following nine members: SEC is an integer between 0
1759 and 60; SEC is 60 for a leap second, which only some operating systems
1760 support. MINUTE is an integer between 0 and 59. HOUR is an integer
1761 between 0 and 23. DAY is an integer between 1 and 31. MONTH is an
1762 integer between 1 and 12. YEAR is an integer indicating the
1763 four-digit year. DOW is the day of week, an integer between 0 and 6,
1764 where 0 is Sunday. DST is t if daylight saving time is in effect,
1765 otherwise nil. ZONE is an integer indicating the number of seconds
1766 east of Greenwich. (Note that Common Lisp has different meanings for
1767 DOW and ZONE.) */)
1768 (Lisp_Object specified_time)
1770 time_t time_spec;
1771 struct tm save_tm;
1772 struct tm *decoded_time;
1773 Lisp_Object list_args[9];
1775 if (! lisp_time_argument (specified_time, &time_spec, NULL))
1776 error ("Invalid time specification");
1778 BLOCK_INPUT;
1779 decoded_time = localtime (&time_spec);
1780 UNBLOCK_INPUT;
1781 if (! (decoded_time
1782 && MOST_NEGATIVE_FIXNUM - TM_YEAR_BASE <= decoded_time->tm_year
1783 && decoded_time->tm_year <= MOST_POSITIVE_FIXNUM - TM_YEAR_BASE))
1784 time_overflow ();
1785 XSETFASTINT (list_args[0], decoded_time->tm_sec);
1786 XSETFASTINT (list_args[1], decoded_time->tm_min);
1787 XSETFASTINT (list_args[2], decoded_time->tm_hour);
1788 XSETFASTINT (list_args[3], decoded_time->tm_mday);
1789 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
1790 /* On 64-bit machines an int is narrower than EMACS_INT, thus the
1791 cast below avoids overflow in int arithmetics. */
1792 XSETINT (list_args[5], TM_YEAR_BASE + (EMACS_INT) decoded_time->tm_year);
1793 XSETFASTINT (list_args[6], decoded_time->tm_wday);
1794 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
1796 /* Make a copy, in case gmtime modifies the struct. */
1797 save_tm = *decoded_time;
1798 BLOCK_INPUT;
1799 decoded_time = gmtime (&time_spec);
1800 UNBLOCK_INPUT;
1801 if (decoded_time == 0)
1802 list_args[8] = Qnil;
1803 else
1804 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1805 return Flist (9, list_args);
1808 /* Return OBJ - OFFSET, checking that OBJ is a valid fixnum and that
1809 the result is representable as an int. Assume OFFSET is small and
1810 nonnegative. */
1811 static int
1812 check_tm_member (Lisp_Object obj, int offset)
1814 EMACS_INT n;
1815 CHECK_NUMBER (obj);
1816 n = XINT (obj);
1817 if (! (INT_MIN + offset <= n && n - offset <= INT_MAX))
1818 time_overflow ();
1819 return n - offset;
1822 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1823 doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
1824 This is the reverse operation of `decode-time', which see.
1825 ZONE defaults to the current time zone rule. This can
1826 be a string or t (as from `set-time-zone-rule'), or it can be a list
1827 \(as from `current-time-zone') or an integer (as from `decode-time')
1828 applied without consideration for daylight saving time.
1830 You can pass more than 7 arguments; then the first six arguments
1831 are used as SECOND through YEAR, and the *last* argument is used as ZONE.
1832 The intervening arguments are ignored.
1833 This feature lets (apply 'encode-time (decode-time ...)) work.
1835 Out-of-range values for SECOND, MINUTE, HOUR, DAY, or MONTH are allowed;
1836 for example, a DAY of 0 means the day preceding the given month.
1837 Year numbers less than 100 are treated just like other year numbers.
1838 If you want them to stand for years in this century, you must do that yourself.
1840 Years before 1970 are not guaranteed to work. On some systems,
1841 year values as low as 1901 do work.
1843 usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */)
1844 (ptrdiff_t nargs, Lisp_Object *args)
1846 time_t value;
1847 struct tm tm;
1848 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1850 tm.tm_sec = check_tm_member (args[0], 0);
1851 tm.tm_min = check_tm_member (args[1], 0);
1852 tm.tm_hour = check_tm_member (args[2], 0);
1853 tm.tm_mday = check_tm_member (args[3], 0);
1854 tm.tm_mon = check_tm_member (args[4], 1);
1855 tm.tm_year = check_tm_member (args[5], TM_YEAR_BASE);
1856 tm.tm_isdst = -1;
1858 if (CONSP (zone))
1859 zone = Fcar (zone);
1860 if (NILP (zone))
1862 BLOCK_INPUT;
1863 value = mktime (&tm);
1864 UNBLOCK_INPUT;
1866 else
1868 char tzbuf[100];
1869 const char *tzstring;
1870 char **oldenv = environ, **newenv;
1872 if (EQ (zone, Qt))
1873 tzstring = "UTC0";
1874 else if (STRINGP (zone))
1875 tzstring = SSDATA (zone);
1876 else if (INTEGERP (zone))
1878 EMACS_INT abszone = eabs (XINT (zone));
1879 EMACS_INT zone_hr = abszone / (60*60);
1880 int zone_min = (abszone/60) % 60;
1881 int zone_sec = abszone % 60;
1882 sprintf (tzbuf, "XXX%s%"pI"d:%02d:%02d", "-" + (XINT (zone) < 0),
1883 zone_hr, zone_min, zone_sec);
1884 tzstring = tzbuf;
1886 else
1887 error ("Invalid time zone specification");
1889 /* Set TZ before calling mktime; merely adjusting mktime's returned
1890 value doesn't suffice, since that would mishandle leap seconds. */
1891 set_time_zone_rule (tzstring);
1893 BLOCK_INPUT;
1894 value = mktime (&tm);
1895 UNBLOCK_INPUT;
1897 /* Restore TZ to previous value. */
1898 newenv = environ;
1899 environ = oldenv;
1900 xfree (newenv);
1901 #ifdef LOCALTIME_CACHE
1902 tzset ();
1903 #endif
1906 if (value == (time_t) -1)
1907 time_overflow ();
1909 return make_time (value);
1912 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1913 doc: /* Return the current local time, as a human-readable string.
1914 Programs can use this function to decode a time,
1915 since the number of columns in each field is fixed
1916 if the year is in the range 1000-9999.
1917 The format is `Sun Sep 16 01:03:52 1973'.
1918 However, see also the functions `decode-time' and `format-time-string'
1919 which provide a much more powerful and general facility.
1921 If SPECIFIED-TIME is given, it is a time to format instead of the
1922 current time. The argument should have the form (HIGH LOW . IGNORED).
1923 Thus, you can use times obtained from `current-time' and from
1924 `file-attributes'. SPECIFIED-TIME can also have the form (HIGH . LOW),
1925 but this is considered obsolete. */)
1926 (Lisp_Object specified_time)
1928 time_t value;
1929 struct tm *tm;
1930 register char *tem;
1932 if (! lisp_time_argument (specified_time, &value, NULL))
1933 error ("Invalid time specification");
1935 /* Convert to a string, checking for out-of-range time stamps.
1936 Don't use 'ctime', as that might dump core if VALUE is out of
1937 range. */
1938 BLOCK_INPUT;
1939 tm = localtime (&value);
1940 UNBLOCK_INPUT;
1941 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) && (tem = asctime (tm))))
1942 time_overflow ();
1944 /* Remove the trailing newline. */
1945 tem[strlen (tem) - 1] = '\0';
1947 return build_string (tem);
1950 /* Yield A - B, measured in seconds.
1951 This function is copied from the GNU C Library. */
1952 static int
1953 tm_diff (struct tm *a, struct tm *b)
1955 /* Compute intervening leap days correctly even if year is negative.
1956 Take care to avoid int overflow in leap day calculations,
1957 but it's OK to assume that A and B are close to each other. */
1958 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1959 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1960 int a100 = a4 / 25 - (a4 % 25 < 0);
1961 int b100 = b4 / 25 - (b4 % 25 < 0);
1962 int a400 = a100 >> 2;
1963 int b400 = b100 >> 2;
1964 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1965 int years = a->tm_year - b->tm_year;
1966 int days = (365 * years + intervening_leap_days
1967 + (a->tm_yday - b->tm_yday));
1968 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1969 + (a->tm_min - b->tm_min))
1970 + (a->tm_sec - b->tm_sec));
1973 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1974 doc: /* Return the offset and name for the local time zone.
1975 This returns a list of the form (OFFSET NAME).
1976 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).
1977 A negative value means west of Greenwich.
1978 NAME is a string giving the name of the time zone.
1979 If SPECIFIED-TIME is given, the time zone offset is determined from it
1980 instead of using the current time. The argument should have the form
1981 (HIGH LOW . IGNORED). Thus, you can use times obtained from
1982 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1983 have the form (HIGH . LOW), but this is considered obsolete.
1985 Some operating systems cannot provide all this information to Emacs;
1986 in this case, `current-time-zone' returns a list containing nil for
1987 the data it can't find. */)
1988 (Lisp_Object specified_time)
1990 time_t value;
1991 struct tm *t;
1992 struct tm localtm;
1993 struct tm *localt;
1994 Lisp_Object zone_offset, zone_name;
1996 zone_offset = Qnil;
1997 zone_name = format_time_string ("%Z", sizeof "%Z" - 1, specified_time,
1998 0, &value, &localt);
1999 localtm = *localt;
2000 BLOCK_INPUT;
2001 t = gmtime (&value);
2002 UNBLOCK_INPUT;
2004 if (t)
2006 int offset = tm_diff (&localtm, t);
2007 zone_offset = make_number (offset);
2008 if (SCHARS (zone_name) == 0)
2010 /* No local time zone name is available; use "+-NNNN" instead. */
2011 int m = offset / 60;
2012 int am = offset < 0 ? - m : m;
2013 char buf[sizeof "+00" + INT_STRLEN_BOUND (int)];
2014 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
2015 zone_name = build_string (buf);
2019 return list2 (zone_offset, zone_name);
2022 /* This holds the value of `environ' produced by the previous
2023 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
2024 has never been called. */
2025 static char **environbuf;
2027 /* This holds the startup value of the TZ environment variable so it
2028 can be restored if the user calls set-time-zone-rule with a nil
2029 argument. */
2030 static char *initial_tz;
2032 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
2033 doc: /* Set the local time zone using TZ, a string specifying a time zone rule.
2034 If TZ is nil, use implementation-defined default time zone information.
2035 If TZ is t, use Universal Time.
2037 Instead of calling this function, you typically want (setenv "TZ" TZ).
2038 That changes both the environment of the Emacs process and the
2039 variable `process-environment', whereas `set-time-zone-rule' affects
2040 only the former. */)
2041 (Lisp_Object tz)
2043 const char *tzstring;
2045 /* When called for the first time, save the original TZ. */
2046 if (!environbuf)
2047 initial_tz = (char *) getenv ("TZ");
2049 if (NILP (tz))
2050 tzstring = initial_tz;
2051 else if (EQ (tz, Qt))
2052 tzstring = "UTC0";
2053 else
2055 CHECK_STRING (tz);
2056 tzstring = SSDATA (tz);
2059 set_time_zone_rule (tzstring);
2060 xfree (environbuf);
2061 environbuf = environ;
2063 return Qnil;
2066 #ifdef LOCALTIME_CACHE
2068 /* These two values are known to load tz files in buggy implementations,
2069 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
2070 Their values shouldn't matter in non-buggy implementations.
2071 We don't use string literals for these strings,
2072 since if a string in the environment is in readonly
2073 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
2074 See Sun bugs 1113095 and 1114114, ``Timezone routines
2075 improperly modify environment''. */
2077 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
2078 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
2080 #endif
2082 /* Set the local time zone rule to TZSTRING.
2083 This allocates memory into `environ', which it is the caller's
2084 responsibility to free. */
2086 void
2087 set_time_zone_rule (const char *tzstring)
2089 ptrdiff_t envptrs;
2090 char **from, **to, **newenv;
2092 /* Make the ENVIRON vector longer with room for TZSTRING. */
2093 for (from = environ; *from; from++)
2094 continue;
2095 envptrs = from - environ + 2;
2096 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
2097 + (tzstring ? strlen (tzstring) + 4 : 0));
2099 /* Add TZSTRING to the end of environ, as a value for TZ. */
2100 if (tzstring)
2102 char *t = (char *) (to + envptrs);
2103 strcpy (t, "TZ=");
2104 strcat (t, tzstring);
2105 *to++ = t;
2108 /* Copy the old environ vector elements into NEWENV,
2109 but don't copy the TZ variable.
2110 So we have only one definition of TZ, which came from TZSTRING. */
2111 for (from = environ; *from; from++)
2112 if (strncmp (*from, "TZ=", 3) != 0)
2113 *to++ = *from;
2114 *to = 0;
2116 environ = newenv;
2118 /* If we do have a TZSTRING, NEWENV points to the vector slot where
2119 the TZ variable is stored. If we do not have a TZSTRING,
2120 TO points to the vector slot which has the terminating null. */
2122 #ifdef LOCALTIME_CACHE
2124 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
2125 "US/Pacific" that loads a tz file, then changes to a value like
2126 "XXX0" that does not load a tz file, and then changes back to
2127 its original value, the last change is (incorrectly) ignored.
2128 Also, if TZ changes twice in succession to values that do
2129 not load a tz file, tzset can dump core (see Sun bug#1225179).
2130 The following code works around these bugs. */
2132 if (tzstring)
2134 /* Temporarily set TZ to a value that loads a tz file
2135 and that differs from tzstring. */
2136 char *tz = *newenv;
2137 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
2138 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
2139 tzset ();
2140 *newenv = tz;
2142 else
2144 /* The implied tzstring is unknown, so temporarily set TZ to
2145 two different values that each load a tz file. */
2146 *to = set_time_zone_rule_tz1;
2147 to[1] = 0;
2148 tzset ();
2149 *to = set_time_zone_rule_tz2;
2150 tzset ();
2151 *to = 0;
2154 /* Now TZ has the desired value, and tzset can be invoked safely. */
2157 tzset ();
2158 #endif
2161 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
2162 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
2163 type of object is Lisp_String). INHERIT is passed to
2164 INSERT_FROM_STRING_FUNC as the last argument. */
2166 static void
2167 general_insert_function (void (*insert_func)
2168 (const char *, ptrdiff_t),
2169 void (*insert_from_string_func)
2170 (Lisp_Object, ptrdiff_t, ptrdiff_t,
2171 ptrdiff_t, ptrdiff_t, int),
2172 int inherit, ptrdiff_t nargs, Lisp_Object *args)
2174 ptrdiff_t argnum;
2175 register Lisp_Object val;
2177 for (argnum = 0; argnum < nargs; argnum++)
2179 val = args[argnum];
2180 if (CHARACTERP (val))
2182 int c = XFASTINT (val);
2183 unsigned char str[MAX_MULTIBYTE_LENGTH];
2184 int len;
2186 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2187 len = CHAR_STRING (c, str);
2188 else
2190 str[0] = ASCII_CHAR_P (c) ? c : multibyte_char_to_unibyte (c);
2191 len = 1;
2193 (*insert_func) ((char *) str, len);
2195 else if (STRINGP (val))
2197 (*insert_from_string_func) (val, 0, 0,
2198 SCHARS (val),
2199 SBYTES (val),
2200 inherit);
2202 else
2203 wrong_type_argument (Qchar_or_string_p, val);
2207 void
2208 insert1 (Lisp_Object arg)
2210 Finsert (1, &arg);
2214 /* Callers passing one argument to Finsert need not gcpro the
2215 argument "array", since the only element of the array will
2216 not be used after calling insert or insert_from_string, so
2217 we don't care if it gets trashed. */
2219 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
2220 doc: /* Insert the arguments, either strings or characters, at point.
2221 Point and before-insertion markers move forward to end up
2222 after the inserted text.
2223 Any other markers at the point of insertion remain before the text.
2225 If the current buffer is multibyte, unibyte strings are converted
2226 to multibyte for insertion (see `string-make-multibyte').
2227 If the current buffer is unibyte, multibyte strings are converted
2228 to unibyte for insertion (see `string-make-unibyte').
2230 When operating on binary data, it may be necessary to preserve the
2231 original bytes of a unibyte string when inserting it into a multibyte
2232 buffer; to accomplish this, apply `string-as-multibyte' to the string
2233 and insert the result.
2235 usage: (insert &rest ARGS) */)
2236 (ptrdiff_t nargs, Lisp_Object *args)
2238 general_insert_function (insert, insert_from_string, 0, nargs, args);
2239 return Qnil;
2242 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
2243 0, MANY, 0,
2244 doc: /* Insert the arguments at point, inheriting properties from adjoining text.
2245 Point and before-insertion markers move forward to end up
2246 after the inserted text.
2247 Any other markers at the point of insertion remain before the text.
2249 If the current buffer is multibyte, unibyte strings are converted
2250 to multibyte for insertion (see `unibyte-char-to-multibyte').
2251 If the current buffer is unibyte, multibyte strings are converted
2252 to unibyte for insertion.
2254 usage: (insert-and-inherit &rest ARGS) */)
2255 (ptrdiff_t nargs, Lisp_Object *args)
2257 general_insert_function (insert_and_inherit, insert_from_string, 1,
2258 nargs, args);
2259 return Qnil;
2262 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
2263 doc: /* Insert strings or characters at point, relocating markers after the text.
2264 Point and markers move forward to end up after the inserted text.
2266 If the current buffer is multibyte, unibyte strings are converted
2267 to multibyte for insertion (see `unibyte-char-to-multibyte').
2268 If the current buffer is unibyte, multibyte strings are converted
2269 to unibyte for insertion.
2271 usage: (insert-before-markers &rest ARGS) */)
2272 (ptrdiff_t nargs, Lisp_Object *args)
2274 general_insert_function (insert_before_markers,
2275 insert_from_string_before_markers, 0,
2276 nargs, args);
2277 return Qnil;
2280 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
2281 Sinsert_and_inherit_before_markers, 0, MANY, 0,
2282 doc: /* Insert text at point, relocating markers and inheriting properties.
2283 Point and markers move forward to end up after the inserted text.
2285 If the current buffer is multibyte, unibyte strings are converted
2286 to multibyte for insertion (see `unibyte-char-to-multibyte').
2287 If the current buffer is unibyte, multibyte strings are converted
2288 to unibyte for insertion.
2290 usage: (insert-before-markers-and-inherit &rest ARGS) */)
2291 (ptrdiff_t nargs, Lisp_Object *args)
2293 general_insert_function (insert_before_markers_and_inherit,
2294 insert_from_string_before_markers, 1,
2295 nargs, args);
2296 return Qnil;
2299 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2300 doc: /* Insert COUNT copies of CHARACTER.
2301 Point, and before-insertion markers, are relocated as in the function `insert'.
2302 The optional third arg INHERIT, if non-nil, says to inherit text properties
2303 from adjoining text, if those properties are sticky. */)
2304 (Lisp_Object character, Lisp_Object count, Lisp_Object inherit)
2306 int i, stringlen;
2307 register ptrdiff_t n;
2308 int c, len;
2309 unsigned char str[MAX_MULTIBYTE_LENGTH];
2310 char string[4000];
2312 CHECK_CHARACTER (character);
2313 CHECK_NUMBER (count);
2314 c = XFASTINT (character);
2316 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2317 len = CHAR_STRING (c, str);
2318 else
2319 str[0] = c, len = 1;
2320 if (XINT (count) <= 0)
2321 return Qnil;
2322 if (BUF_BYTES_MAX / len < XINT (count))
2323 buffer_overflow ();
2324 n = XINT (count) * len;
2325 stringlen = min (n, sizeof string - sizeof string % len);
2326 for (i = 0; i < stringlen; i++)
2327 string[i] = str[i % len];
2328 while (n > stringlen)
2330 QUIT;
2331 if (!NILP (inherit))
2332 insert_and_inherit (string, stringlen);
2333 else
2334 insert (string, stringlen);
2335 n -= stringlen;
2337 if (!NILP (inherit))
2338 insert_and_inherit (string, n);
2339 else
2340 insert (string, n);
2341 return Qnil;
2344 DEFUN ("insert-byte", Finsert_byte, Sinsert_byte, 2, 3, 0,
2345 doc: /* Insert COUNT (second arg) copies of BYTE (first arg).
2346 Both arguments are required.
2347 BYTE is a number of the range 0..255.
2349 If BYTE is 128..255 and the current buffer is multibyte, the
2350 corresponding eight-bit character is inserted.
2352 Point, and before-insertion markers, are relocated as in the function `insert'.
2353 The optional third arg INHERIT, if non-nil, says to inherit text properties
2354 from adjoining text, if those properties are sticky. */)
2355 (Lisp_Object byte, Lisp_Object count, Lisp_Object inherit)
2357 CHECK_NUMBER (byte);
2358 if (XINT (byte) < 0 || XINT (byte) > 255)
2359 args_out_of_range_3 (byte, make_number (0), make_number (255));
2360 if (XINT (byte) >= 128
2361 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2362 XSETFASTINT (byte, BYTE8_TO_CHAR (XINT (byte)));
2363 return Finsert_char (byte, count, inherit);
2367 /* Making strings from buffer contents. */
2369 /* Return a Lisp_String containing the text of the current buffer from
2370 START to END. If text properties are in use and the current buffer
2371 has properties in the range specified, the resulting string will also
2372 have them, if PROPS is nonzero.
2374 We don't want to use plain old make_string here, because it calls
2375 make_uninit_string, which can cause the buffer arena to be
2376 compacted. make_string has no way of knowing that the data has
2377 been moved, and thus copies the wrong data into the string. This
2378 doesn't effect most of the other users of make_string, so it should
2379 be left as is. But we should use this function when conjuring
2380 buffer substrings. */
2382 Lisp_Object
2383 make_buffer_string (ptrdiff_t start, ptrdiff_t end, int props)
2385 ptrdiff_t start_byte = CHAR_TO_BYTE (start);
2386 ptrdiff_t end_byte = CHAR_TO_BYTE (end);
2388 return make_buffer_string_both (start, start_byte, end, end_byte, props);
2391 /* Return a Lisp_String containing the text of the current buffer from
2392 START / START_BYTE to END / END_BYTE.
2394 If text properties are in use and the current buffer
2395 has properties in the range specified, the resulting string will also
2396 have them, if PROPS is nonzero.
2398 We don't want to use plain old make_string here, because it calls
2399 make_uninit_string, which can cause the buffer arena to be
2400 compacted. make_string has no way of knowing that the data has
2401 been moved, and thus copies the wrong data into the string. This
2402 doesn't effect most of the other users of make_string, so it should
2403 be left as is. But we should use this function when conjuring
2404 buffer substrings. */
2406 Lisp_Object
2407 make_buffer_string_both (ptrdiff_t start, ptrdiff_t start_byte,
2408 ptrdiff_t end, ptrdiff_t end_byte, int props)
2410 Lisp_Object result, tem, tem1;
2412 if (start < GPT && GPT < end)
2413 move_gap (start);
2415 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2416 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
2417 else
2418 result = make_uninit_string (end - start);
2419 memcpy (SDATA (result), BYTE_POS_ADDR (start_byte), end_byte - start_byte);
2421 /* If desired, update and copy the text properties. */
2422 if (props)
2424 update_buffer_properties (start, end);
2426 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
2427 tem1 = Ftext_properties_at (make_number (start), Qnil);
2429 if (XINT (tem) != end || !NILP (tem1))
2430 copy_intervals_to_string (result, current_buffer, start,
2431 end - start);
2434 return result;
2437 /* Call Vbuffer_access_fontify_functions for the range START ... END
2438 in the current buffer, if necessary. */
2440 static void
2441 update_buffer_properties (ptrdiff_t start, ptrdiff_t end)
2443 /* If this buffer has some access functions,
2444 call them, specifying the range of the buffer being accessed. */
2445 if (!NILP (Vbuffer_access_fontify_functions))
2447 Lisp_Object args[3];
2448 Lisp_Object tem;
2450 args[0] = Qbuffer_access_fontify_functions;
2451 XSETINT (args[1], start);
2452 XSETINT (args[2], end);
2454 /* But don't call them if we can tell that the work
2455 has already been done. */
2456 if (!NILP (Vbuffer_access_fontified_property))
2458 tem = Ftext_property_any (args[1], args[2],
2459 Vbuffer_access_fontified_property,
2460 Qnil, Qnil);
2461 if (! NILP (tem))
2462 Frun_hook_with_args (3, args);
2464 else
2465 Frun_hook_with_args (3, args);
2469 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2470 doc: /* Return the contents of part of the current buffer as a string.
2471 The two arguments START and END are character positions;
2472 they can be in either order.
2473 The string returned is multibyte if the buffer is multibyte.
2475 This function copies the text properties of that part of the buffer
2476 into the result string; if you don't want the text properties,
2477 use `buffer-substring-no-properties' instead. */)
2478 (Lisp_Object start, Lisp_Object end)
2480 register ptrdiff_t b, e;
2482 validate_region (&start, &end);
2483 b = XINT (start);
2484 e = XINT (end);
2486 return make_buffer_string (b, e, 1);
2489 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2490 Sbuffer_substring_no_properties, 2, 2, 0,
2491 doc: /* Return the characters of part of the buffer, without the text properties.
2492 The two arguments START and END are character positions;
2493 they can be in either order. */)
2494 (Lisp_Object start, Lisp_Object end)
2496 register ptrdiff_t b, e;
2498 validate_region (&start, &end);
2499 b = XINT (start);
2500 e = XINT (end);
2502 return make_buffer_string (b, e, 0);
2505 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2506 doc: /* Return the contents of the current buffer as a string.
2507 If narrowing is in effect, this function returns only the visible part
2508 of the buffer. */)
2509 (void)
2511 return make_buffer_string (BEGV, ZV, 1);
2514 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2515 1, 3, 0,
2516 doc: /* Insert before point a substring of the contents of BUFFER.
2517 BUFFER may be a buffer or a buffer name.
2518 Arguments START and END are character positions specifying the substring.
2519 They default to the values of (point-min) and (point-max) in BUFFER. */)
2520 (Lisp_Object buffer, Lisp_Object start, Lisp_Object end)
2522 register EMACS_INT b, e, temp;
2523 register struct buffer *bp, *obuf;
2524 Lisp_Object buf;
2526 buf = Fget_buffer (buffer);
2527 if (NILP (buf))
2528 nsberror (buffer);
2529 bp = XBUFFER (buf);
2530 if (NILP (BVAR (bp, name)))
2531 error ("Selecting deleted buffer");
2533 if (NILP (start))
2534 b = BUF_BEGV (bp);
2535 else
2537 CHECK_NUMBER_COERCE_MARKER (start);
2538 b = XINT (start);
2540 if (NILP (end))
2541 e = BUF_ZV (bp);
2542 else
2544 CHECK_NUMBER_COERCE_MARKER (end);
2545 e = XINT (end);
2548 if (b > e)
2549 temp = b, b = e, e = temp;
2551 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2552 args_out_of_range (start, end);
2554 obuf = current_buffer;
2555 set_buffer_internal_1 (bp);
2556 update_buffer_properties (b, e);
2557 set_buffer_internal_1 (obuf);
2559 insert_from_buffer (bp, b, e - b, 0);
2560 return Qnil;
2563 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2564 6, 6, 0,
2565 doc: /* Compare two substrings of two buffers; return result as number.
2566 the value is -N if first string is less after N-1 chars,
2567 +N if first string is greater after N-1 chars, or 0 if strings match.
2568 Each substring is represented as three arguments: BUFFER, START and END.
2569 That makes six args in all, three for each substring.
2571 The value of `case-fold-search' in the current buffer
2572 determines whether case is significant or ignored. */)
2573 (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2)
2575 register EMACS_INT begp1, endp1, begp2, endp2, temp;
2576 register struct buffer *bp1, *bp2;
2577 register Lisp_Object trt
2578 = (!NILP (BVAR (current_buffer, case_fold_search))
2579 ? BVAR (current_buffer, case_canon_table) : Qnil);
2580 ptrdiff_t chars = 0;
2581 ptrdiff_t i1, i2, i1_byte, i2_byte;
2583 /* Find the first buffer and its substring. */
2585 if (NILP (buffer1))
2586 bp1 = current_buffer;
2587 else
2589 Lisp_Object buf1;
2590 buf1 = Fget_buffer (buffer1);
2591 if (NILP (buf1))
2592 nsberror (buffer1);
2593 bp1 = XBUFFER (buf1);
2594 if (NILP (BVAR (bp1, name)))
2595 error ("Selecting deleted buffer");
2598 if (NILP (start1))
2599 begp1 = BUF_BEGV (bp1);
2600 else
2602 CHECK_NUMBER_COERCE_MARKER (start1);
2603 begp1 = XINT (start1);
2605 if (NILP (end1))
2606 endp1 = BUF_ZV (bp1);
2607 else
2609 CHECK_NUMBER_COERCE_MARKER (end1);
2610 endp1 = XINT (end1);
2613 if (begp1 > endp1)
2614 temp = begp1, begp1 = endp1, endp1 = temp;
2616 if (!(BUF_BEGV (bp1) <= begp1
2617 && begp1 <= endp1
2618 && endp1 <= BUF_ZV (bp1)))
2619 args_out_of_range (start1, end1);
2621 /* Likewise for second substring. */
2623 if (NILP (buffer2))
2624 bp2 = current_buffer;
2625 else
2627 Lisp_Object buf2;
2628 buf2 = Fget_buffer (buffer2);
2629 if (NILP (buf2))
2630 nsberror (buffer2);
2631 bp2 = XBUFFER (buf2);
2632 if (NILP (BVAR (bp2, name)))
2633 error ("Selecting deleted buffer");
2636 if (NILP (start2))
2637 begp2 = BUF_BEGV (bp2);
2638 else
2640 CHECK_NUMBER_COERCE_MARKER (start2);
2641 begp2 = XINT (start2);
2643 if (NILP (end2))
2644 endp2 = BUF_ZV (bp2);
2645 else
2647 CHECK_NUMBER_COERCE_MARKER (end2);
2648 endp2 = XINT (end2);
2651 if (begp2 > endp2)
2652 temp = begp2, begp2 = endp2, endp2 = temp;
2654 if (!(BUF_BEGV (bp2) <= begp2
2655 && begp2 <= endp2
2656 && endp2 <= BUF_ZV (bp2)))
2657 args_out_of_range (start2, end2);
2659 i1 = begp1;
2660 i2 = begp2;
2661 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2662 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2664 while (i1 < endp1 && i2 < endp2)
2666 /* When we find a mismatch, we must compare the
2667 characters, not just the bytes. */
2668 int c1, c2;
2670 QUIT;
2672 if (! NILP (BVAR (bp1, enable_multibyte_characters)))
2674 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2675 BUF_INC_POS (bp1, i1_byte);
2676 i1++;
2678 else
2680 c1 = BUF_FETCH_BYTE (bp1, i1);
2681 MAKE_CHAR_MULTIBYTE (c1);
2682 i1++;
2685 if (! NILP (BVAR (bp2, enable_multibyte_characters)))
2687 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2688 BUF_INC_POS (bp2, i2_byte);
2689 i2++;
2691 else
2693 c2 = BUF_FETCH_BYTE (bp2, i2);
2694 MAKE_CHAR_MULTIBYTE (c2);
2695 i2++;
2698 if (!NILP (trt))
2700 c1 = CHAR_TABLE_TRANSLATE (trt, c1);
2701 c2 = CHAR_TABLE_TRANSLATE (trt, c2);
2703 if (c1 < c2)
2704 return make_number (- 1 - chars);
2705 if (c1 > c2)
2706 return make_number (chars + 1);
2708 chars++;
2711 /* The strings match as far as they go.
2712 If one is shorter, that one is less. */
2713 if (chars < endp1 - begp1)
2714 return make_number (chars + 1);
2715 else if (chars < endp2 - begp2)
2716 return make_number (- chars - 1);
2718 /* Same length too => they are equal. */
2719 return make_number (0);
2722 static Lisp_Object
2723 subst_char_in_region_unwind (Lisp_Object arg)
2725 return BVAR (current_buffer, undo_list) = arg;
2728 static Lisp_Object
2729 subst_char_in_region_unwind_1 (Lisp_Object arg)
2731 return BVAR (current_buffer, filename) = arg;
2734 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2735 Ssubst_char_in_region, 4, 5, 0,
2736 doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs.
2737 If optional arg NOUNDO is non-nil, don't record this change for undo
2738 and don't mark the buffer as really changed.
2739 Both characters must have the same length of multi-byte form. */)
2740 (Lisp_Object start, Lisp_Object end, Lisp_Object fromchar, Lisp_Object tochar, Lisp_Object noundo)
2742 register ptrdiff_t pos, pos_byte, stop, i, len, end_byte;
2743 /* Keep track of the first change in the buffer:
2744 if 0 we haven't found it yet.
2745 if < 0 we've found it and we've run the before-change-function.
2746 if > 0 we've actually performed it and the value is its position. */
2747 ptrdiff_t changed = 0;
2748 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2749 unsigned char *p;
2750 ptrdiff_t count = SPECPDL_INDEX ();
2751 #define COMBINING_NO 0
2752 #define COMBINING_BEFORE 1
2753 #define COMBINING_AFTER 2
2754 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2755 int maybe_byte_combining = COMBINING_NO;
2756 ptrdiff_t last_changed = 0;
2757 int multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2758 int fromc, toc;
2760 restart:
2762 validate_region (&start, &end);
2763 CHECK_CHARACTER (fromchar);
2764 CHECK_CHARACTER (tochar);
2765 fromc = XFASTINT (fromchar);
2766 toc = XFASTINT (tochar);
2768 if (multibyte_p)
2770 len = CHAR_STRING (fromc, fromstr);
2771 if (CHAR_STRING (toc, tostr) != len)
2772 error ("Characters in `subst-char-in-region' have different byte-lengths");
2773 if (!ASCII_BYTE_P (*tostr))
2775 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2776 complete multibyte character, it may be combined with the
2777 after bytes. If it is in the range 0xA0..0xFF, it may be
2778 combined with the before and after bytes. */
2779 if (!CHAR_HEAD_P (*tostr))
2780 maybe_byte_combining = COMBINING_BOTH;
2781 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2782 maybe_byte_combining = COMBINING_AFTER;
2785 else
2787 len = 1;
2788 fromstr[0] = fromc;
2789 tostr[0] = toc;
2792 pos = XINT (start);
2793 pos_byte = CHAR_TO_BYTE (pos);
2794 stop = CHAR_TO_BYTE (XINT (end));
2795 end_byte = stop;
2797 /* If we don't want undo, turn off putting stuff on the list.
2798 That's faster than getting rid of things,
2799 and it prevents even the entry for a first change.
2800 Also inhibit locking the file. */
2801 if (!changed && !NILP (noundo))
2803 record_unwind_protect (subst_char_in_region_unwind,
2804 BVAR (current_buffer, undo_list));
2805 BVAR (current_buffer, undo_list) = Qt;
2806 /* Don't do file-locking. */
2807 record_unwind_protect (subst_char_in_region_unwind_1,
2808 BVAR (current_buffer, filename));
2809 BVAR (current_buffer, filename) = Qnil;
2812 if (pos_byte < GPT_BYTE)
2813 stop = min (stop, GPT_BYTE);
2814 while (1)
2816 ptrdiff_t pos_byte_next = pos_byte;
2818 if (pos_byte >= stop)
2820 if (pos_byte >= end_byte) break;
2821 stop = end_byte;
2823 p = BYTE_POS_ADDR (pos_byte);
2824 if (multibyte_p)
2825 INC_POS (pos_byte_next);
2826 else
2827 ++pos_byte_next;
2828 if (pos_byte_next - pos_byte == len
2829 && p[0] == fromstr[0]
2830 && (len == 1
2831 || (p[1] == fromstr[1]
2832 && (len == 2 || (p[2] == fromstr[2]
2833 && (len == 3 || p[3] == fromstr[3]))))))
2835 if (changed < 0)
2836 /* We've already seen this and run the before-change-function;
2837 this time we only need to record the actual position. */
2838 changed = pos;
2839 else if (!changed)
2841 changed = -1;
2842 modify_region (current_buffer, pos, XINT (end), 0);
2844 if (! NILP (noundo))
2846 if (MODIFF - 1 == SAVE_MODIFF)
2847 SAVE_MODIFF++;
2848 if (MODIFF - 1 == BUF_AUTOSAVE_MODIFF (current_buffer))
2849 BUF_AUTOSAVE_MODIFF (current_buffer)++;
2852 /* The before-change-function may have moved the gap
2853 or even modified the buffer so we should start over. */
2854 goto restart;
2857 /* Take care of the case where the new character
2858 combines with neighboring bytes. */
2859 if (maybe_byte_combining
2860 && (maybe_byte_combining == COMBINING_AFTER
2861 ? (pos_byte_next < Z_BYTE
2862 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2863 : ((pos_byte_next < Z_BYTE
2864 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2865 || (pos_byte > BEG_BYTE
2866 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2868 Lisp_Object tem, string;
2870 struct gcpro gcpro1;
2872 tem = BVAR (current_buffer, undo_list);
2873 GCPRO1 (tem);
2875 /* Make a multibyte string containing this single character. */
2876 string = make_multibyte_string ((char *) tostr, 1, len);
2877 /* replace_range is less efficient, because it moves the gap,
2878 but it handles combining correctly. */
2879 replace_range (pos, pos + 1, string,
2880 0, 0, 1);
2881 pos_byte_next = CHAR_TO_BYTE (pos);
2882 if (pos_byte_next > pos_byte)
2883 /* Before combining happened. We should not increment
2884 POS. So, to cancel the later increment of POS,
2885 decrease it now. */
2886 pos--;
2887 else
2888 INC_POS (pos_byte_next);
2890 if (! NILP (noundo))
2891 BVAR (current_buffer, undo_list) = tem;
2893 UNGCPRO;
2895 else
2897 if (NILP (noundo))
2898 record_change (pos, 1);
2899 for (i = 0; i < len; i++) *p++ = tostr[i];
2901 last_changed = pos + 1;
2903 pos_byte = pos_byte_next;
2904 pos++;
2907 if (changed > 0)
2909 signal_after_change (changed,
2910 last_changed - changed, last_changed - changed);
2911 update_compositions (changed, last_changed, CHECK_ALL);
2914 unbind_to (count, Qnil);
2915 return Qnil;
2919 static Lisp_Object check_translation (ptrdiff_t, ptrdiff_t, ptrdiff_t,
2920 Lisp_Object);
2922 /* Helper function for Ftranslate_region_internal.
2924 Check if a character sequence at POS (POS_BYTE) matches an element
2925 of VAL. VAL is a list (([FROM-CHAR ...] . TO) ...). If a matching
2926 element is found, return it. Otherwise return Qnil. */
2928 static Lisp_Object
2929 check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end,
2930 Lisp_Object val)
2932 int buf_size = 16, buf_used = 0;
2933 int *buf = alloca (sizeof (int) * buf_size);
2935 for (; CONSP (val); val = XCDR (val))
2937 Lisp_Object elt;
2938 ptrdiff_t len, i;
2940 elt = XCAR (val);
2941 if (! CONSP (elt))
2942 continue;
2943 elt = XCAR (elt);
2944 if (! VECTORP (elt))
2945 continue;
2946 len = ASIZE (elt);
2947 if (len <= end - pos)
2949 for (i = 0; i < len; i++)
2951 if (buf_used <= i)
2953 unsigned char *p = BYTE_POS_ADDR (pos_byte);
2954 int len1;
2956 if (buf_used == buf_size)
2958 int *newbuf;
2960 buf_size += 16;
2961 newbuf = alloca (sizeof (int) * buf_size);
2962 memcpy (newbuf, buf, sizeof (int) * buf_used);
2963 buf = newbuf;
2965 buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1);
2966 pos_byte += len1;
2968 if (XINT (AREF (elt, i)) != buf[i])
2969 break;
2971 if (i == len)
2972 return XCAR (val);
2975 return Qnil;
2979 DEFUN ("translate-region-internal", Ftranslate_region_internal,
2980 Stranslate_region_internal, 3, 3, 0,
2981 doc: /* Internal use only.
2982 From START to END, translate characters according to TABLE.
2983 TABLE is a string or a char-table; the Nth character in it is the
2984 mapping for the character with code N.
2985 It returns the number of characters changed. */)
2986 (Lisp_Object start, Lisp_Object end, register Lisp_Object table)
2988 register unsigned char *tt; /* Trans table. */
2989 register int nc; /* New character. */
2990 int cnt; /* Number of changes made. */
2991 ptrdiff_t size; /* Size of translate table. */
2992 ptrdiff_t pos, pos_byte, end_pos;
2993 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2994 int string_multibyte IF_LINT (= 0);
2996 validate_region (&start, &end);
2997 if (CHAR_TABLE_P (table))
2999 if (! EQ (XCHAR_TABLE (table)->purpose, Qtranslation_table))
3000 error ("Not a translation table");
3001 size = MAX_CHAR;
3002 tt = NULL;
3004 else
3006 CHECK_STRING (table);
3008 if (! multibyte && (SCHARS (table) < SBYTES (table)))
3009 table = string_make_unibyte (table);
3010 string_multibyte = SCHARS (table) < SBYTES (table);
3011 size = SBYTES (table);
3012 tt = SDATA (table);
3015 pos = XINT (start);
3016 pos_byte = CHAR_TO_BYTE (pos);
3017 end_pos = XINT (end);
3018 modify_region (current_buffer, pos, end_pos, 0);
3020 cnt = 0;
3021 for (; pos < end_pos; )
3023 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
3024 unsigned char *str, buf[MAX_MULTIBYTE_LENGTH];
3025 int len, str_len;
3026 int oc;
3027 Lisp_Object val;
3029 if (multibyte)
3030 oc = STRING_CHAR_AND_LENGTH (p, len);
3031 else
3032 oc = *p, len = 1;
3033 if (oc < size)
3035 if (tt)
3037 /* Reload as signal_after_change in last iteration may GC. */
3038 tt = SDATA (table);
3039 if (string_multibyte)
3041 str = tt + string_char_to_byte (table, oc);
3042 nc = STRING_CHAR_AND_LENGTH (str, str_len);
3044 else
3046 nc = tt[oc];
3047 if (! ASCII_BYTE_P (nc) && multibyte)
3049 str_len = BYTE8_STRING (nc, buf);
3050 str = buf;
3052 else
3054 str_len = 1;
3055 str = tt + oc;
3059 else
3061 nc = oc;
3062 val = CHAR_TABLE_REF (table, oc);
3063 if (CHARACTERP (val))
3065 nc = XFASTINT (val);
3066 str_len = CHAR_STRING (nc, buf);
3067 str = buf;
3069 else if (VECTORP (val) || (CONSP (val)))
3071 /* VAL is [TO_CHAR ...] or (([FROM-CHAR ...] . TO) ...)
3072 where TO is TO-CHAR or [TO-CHAR ...]. */
3073 nc = -1;
3077 if (nc != oc && nc >= 0)
3079 /* Simple one char to one char translation. */
3080 if (len != str_len)
3082 Lisp_Object string;
3084 /* This is less efficient, because it moves the gap,
3085 but it should handle multibyte characters correctly. */
3086 string = make_multibyte_string ((char *) str, 1, str_len);
3087 replace_range (pos, pos + 1, string, 1, 0, 1);
3088 len = str_len;
3090 else
3092 record_change (pos, 1);
3093 while (str_len-- > 0)
3094 *p++ = *str++;
3095 signal_after_change (pos, 1, 1);
3096 update_compositions (pos, pos + 1, CHECK_BORDER);
3098 ++cnt;
3100 else if (nc < 0)
3102 Lisp_Object string;
3104 if (CONSP (val))
3106 val = check_translation (pos, pos_byte, end_pos, val);
3107 if (NILP (val))
3109 pos_byte += len;
3110 pos++;
3111 continue;
3113 /* VAL is ([FROM-CHAR ...] . TO). */
3114 len = ASIZE (XCAR (val));
3115 val = XCDR (val);
3117 else
3118 len = 1;
3120 if (VECTORP (val))
3122 string = Fconcat (1, &val);
3124 else
3126 string = Fmake_string (make_number (1), val);
3128 replace_range (pos, pos + len, string, 1, 0, 1);
3129 pos_byte += SBYTES (string);
3130 pos += SCHARS (string);
3131 cnt += SCHARS (string);
3132 end_pos += SCHARS (string) - len;
3133 continue;
3136 pos_byte += len;
3137 pos++;
3140 return make_number (cnt);
3143 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
3144 doc: /* Delete the text between START and END.
3145 If called interactively, delete the region between point and mark.
3146 This command deletes buffer text without modifying the kill ring. */)
3147 (Lisp_Object start, Lisp_Object end)
3149 validate_region (&start, &end);
3150 del_range (XINT (start), XINT (end));
3151 return Qnil;
3154 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
3155 Sdelete_and_extract_region, 2, 2, 0,
3156 doc: /* Delete the text between START and END and return it. */)
3157 (Lisp_Object start, Lisp_Object end)
3159 validate_region (&start, &end);
3160 if (XINT (start) == XINT (end))
3161 return empty_unibyte_string;
3162 return del_range_1 (XINT (start), XINT (end), 1, 1);
3165 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
3166 doc: /* Remove restrictions (narrowing) from current buffer.
3167 This allows the buffer's full text to be seen and edited. */)
3168 (void)
3170 if (BEG != BEGV || Z != ZV)
3171 current_buffer->clip_changed = 1;
3172 BEGV = BEG;
3173 BEGV_BYTE = BEG_BYTE;
3174 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
3175 /* Changing the buffer bounds invalidates any recorded current column. */
3176 invalidate_current_column ();
3177 return Qnil;
3180 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
3181 doc: /* Restrict editing in this buffer to the current region.
3182 The rest of the text becomes temporarily invisible and untouchable
3183 but is not deleted; if you save the buffer in a file, the invisible
3184 text is included in the file. \\[widen] makes all visible again.
3185 See also `save-restriction'.
3187 When calling from a program, pass two arguments; positions (integers
3188 or markers) bounding the text that should remain visible. */)
3189 (register Lisp_Object start, Lisp_Object end)
3191 CHECK_NUMBER_COERCE_MARKER (start);
3192 CHECK_NUMBER_COERCE_MARKER (end);
3194 if (XINT (start) > XINT (end))
3196 Lisp_Object tem;
3197 tem = start; start = end; end = tem;
3200 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
3201 args_out_of_range (start, end);
3203 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
3204 current_buffer->clip_changed = 1;
3206 SET_BUF_BEGV (current_buffer, XFASTINT (start));
3207 SET_BUF_ZV (current_buffer, XFASTINT (end));
3208 if (PT < XFASTINT (start))
3209 SET_PT (XFASTINT (start));
3210 if (PT > XFASTINT (end))
3211 SET_PT (XFASTINT (end));
3212 /* Changing the buffer bounds invalidates any recorded current column. */
3213 invalidate_current_column ();
3214 return Qnil;
3217 Lisp_Object
3218 save_restriction_save (void)
3220 if (BEGV == BEG && ZV == Z)
3221 /* The common case that the buffer isn't narrowed.
3222 We return just the buffer object, which save_restriction_restore
3223 recognizes as meaning `no restriction'. */
3224 return Fcurrent_buffer ();
3225 else
3226 /* We have to save a restriction, so return a pair of markers, one
3227 for the beginning and one for the end. */
3229 Lisp_Object beg, end;
3231 beg = buildmark (BEGV, BEGV_BYTE);
3232 end = buildmark (ZV, ZV_BYTE);
3234 /* END must move forward if text is inserted at its exact location. */
3235 XMARKER (end)->insertion_type = 1;
3237 return Fcons (beg, end);
3241 Lisp_Object
3242 save_restriction_restore (Lisp_Object data)
3244 struct buffer *cur = NULL;
3245 struct buffer *buf = (CONSP (data)
3246 ? XMARKER (XCAR (data))->buffer
3247 : XBUFFER (data));
3249 if (buf && buf != current_buffer && !NILP (BVAR (buf, pt_marker)))
3250 { /* If `buf' uses markers to keep track of PT, BEGV, and ZV (as
3251 is the case if it is or has an indirect buffer), then make
3252 sure it is current before we update BEGV, so
3253 set_buffer_internal takes care of managing those markers. */
3254 cur = current_buffer;
3255 set_buffer_internal (buf);
3258 if (CONSP (data))
3259 /* A pair of marks bounding a saved restriction. */
3261 struct Lisp_Marker *beg = XMARKER (XCAR (data));
3262 struct Lisp_Marker *end = XMARKER (XCDR (data));
3263 eassert (buf == end->buffer);
3265 if (buf /* Verify marker still points to a buffer. */
3266 && (beg->charpos != BUF_BEGV (buf) || end->charpos != BUF_ZV (buf)))
3267 /* The restriction has changed from the saved one, so restore
3268 the saved restriction. */
3270 ptrdiff_t pt = BUF_PT (buf);
3272 SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
3273 SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
3275 if (pt < beg->charpos || pt > end->charpos)
3276 /* The point is outside the new visible range, move it inside. */
3277 SET_BUF_PT_BOTH (buf,
3278 clip_to_bounds (beg->charpos, pt, end->charpos),
3279 clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf),
3280 end->bytepos));
3282 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3285 else
3286 /* A buffer, which means that there was no old restriction. */
3288 if (buf /* Verify marker still points to a buffer. */
3289 && (BUF_BEGV (buf) != BUF_BEG (buf) || BUF_ZV (buf) != BUF_Z (buf)))
3290 /* The buffer has been narrowed, get rid of the narrowing. */
3292 SET_BUF_BEGV_BOTH (buf, BUF_BEG (buf), BUF_BEG_BYTE (buf));
3293 SET_BUF_ZV_BOTH (buf, BUF_Z (buf), BUF_Z_BYTE (buf));
3295 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3299 /* Changing the buffer bounds invalidates any recorded current column. */
3300 invalidate_current_column ();
3302 if (cur)
3303 set_buffer_internal (cur);
3305 return Qnil;
3308 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
3309 doc: /* Execute BODY, saving and restoring current buffer's restrictions.
3310 The buffer's restrictions make parts of the beginning and end invisible.
3311 \(They are set up with `narrow-to-region' and eliminated with `widen'.)
3312 This special form, `save-restriction', saves the current buffer's restrictions
3313 when it is entered, and restores them when it is exited.
3314 So any `narrow-to-region' within BODY lasts only until the end of the form.
3315 The old restrictions settings are restored
3316 even in case of abnormal exit (throw or error).
3318 The value returned is the value of the last form in BODY.
3320 Note: if you are using both `save-excursion' and `save-restriction',
3321 use `save-excursion' outermost:
3322 (save-excursion (save-restriction ...))
3324 usage: (save-restriction &rest BODY) */)
3325 (Lisp_Object body)
3327 register Lisp_Object val;
3328 ptrdiff_t count = SPECPDL_INDEX ();
3330 record_unwind_protect (save_restriction_restore, save_restriction_save ());
3331 val = Fprogn (body);
3332 return unbind_to (count, val);
3335 /* Buffer for the most recent text displayed by Fmessage_box. */
3336 static char *message_text;
3338 /* Allocated length of that buffer. */
3339 static ptrdiff_t message_length;
3341 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
3342 doc: /* Display a message at the bottom of the screen.
3343 The message also goes into the `*Messages*' buffer.
3344 \(In keyboard macros, that's all it does.)
3345 Return the message.
3347 The first argument is a format control string, and the rest are data
3348 to be formatted under control of the string. See `format' for details.
3350 Note: Use (message "%s" VALUE) to print the value of expressions and
3351 variables to avoid accidentally interpreting `%' as format specifiers.
3353 If the first argument is nil or the empty string, the function clears
3354 any existing message; this lets the minibuffer contents show. See
3355 also `current-message'.
3357 usage: (message FORMAT-STRING &rest ARGS) */)
3358 (ptrdiff_t nargs, Lisp_Object *args)
3360 if (NILP (args[0])
3361 || (STRINGP (args[0])
3362 && SBYTES (args[0]) == 0))
3364 message (0);
3365 return args[0];
3367 else
3369 register Lisp_Object val;
3370 val = Fformat (nargs, args);
3371 message3 (val, SBYTES (val), STRING_MULTIBYTE (val));
3372 return val;
3376 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
3377 doc: /* Display a message, in a dialog box if possible.
3378 If a dialog box is not available, use the echo area.
3379 The first argument is a format control string, and the rest are data
3380 to be formatted under control of the string. See `format' for details.
3382 If the first argument is nil or the empty string, clear any existing
3383 message; let the minibuffer contents show.
3385 usage: (message-box FORMAT-STRING &rest ARGS) */)
3386 (ptrdiff_t nargs, Lisp_Object *args)
3388 if (NILP (args[0]))
3390 message (0);
3391 return Qnil;
3393 else
3395 register Lisp_Object val;
3396 val = Fformat (nargs, args);
3397 #ifdef HAVE_MENUS
3398 /* The MS-DOS frames support popup menus even though they are
3399 not FRAME_WINDOW_P. */
3400 if (FRAME_WINDOW_P (XFRAME (selected_frame))
3401 || FRAME_MSDOS_P (XFRAME (selected_frame)))
3403 Lisp_Object pane, menu;
3404 struct gcpro gcpro1;
3405 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
3406 GCPRO1 (pane);
3407 menu = Fcons (val, pane);
3408 Fx_popup_dialog (Qt, menu, Qt);
3409 UNGCPRO;
3410 return val;
3412 #endif /* HAVE_MENUS */
3413 /* Copy the data so that it won't move when we GC. */
3414 if (! message_text)
3416 message_text = (char *)xmalloc (80);
3417 message_length = 80;
3419 if (SBYTES (val) > message_length)
3421 message_text = (char *) xrealloc (message_text, SBYTES (val));
3422 message_length = SBYTES (val);
3424 memcpy (message_text, SDATA (val), SBYTES (val));
3425 message2 (message_text, SBYTES (val),
3426 STRING_MULTIBYTE (val));
3427 return val;
3431 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
3432 doc: /* Display a message in a dialog box or in the echo area.
3433 If this command was invoked with the mouse, use a dialog box if
3434 `use-dialog-box' is non-nil.
3435 Otherwise, use the echo area.
3436 The first argument is a format control string, and the rest are data
3437 to be formatted under control of the string. See `format' for details.
3439 If the first argument is nil or the empty string, clear any existing
3440 message; let the minibuffer contents show.
3442 usage: (message-or-box FORMAT-STRING &rest ARGS) */)
3443 (ptrdiff_t nargs, Lisp_Object *args)
3445 #ifdef HAVE_MENUS
3446 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3447 && use_dialog_box)
3448 return Fmessage_box (nargs, args);
3449 #endif
3450 return Fmessage (nargs, args);
3453 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
3454 doc: /* Return the string currently displayed in the echo area, or nil if none. */)
3455 (void)
3457 return current_message ();
3461 DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0,
3462 doc: /* Return a copy of STRING with text properties added.
3463 First argument is the string to copy.
3464 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
3465 properties to add to the result.
3466 usage: (propertize STRING &rest PROPERTIES) */)
3467 (ptrdiff_t nargs, Lisp_Object *args)
3469 Lisp_Object properties, string;
3470 struct gcpro gcpro1, gcpro2;
3471 ptrdiff_t i;
3473 /* Number of args must be odd. */
3474 if ((nargs & 1) == 0)
3475 error ("Wrong number of arguments");
3477 properties = string = Qnil;
3478 GCPRO2 (properties, string);
3480 /* First argument must be a string. */
3481 CHECK_STRING (args[0]);
3482 string = Fcopy_sequence (args[0]);
3484 for (i = 1; i < nargs; i += 2)
3485 properties = Fcons (args[i], Fcons (args[i + 1], properties));
3487 Fadd_text_properties (make_number (0),
3488 make_number (SCHARS (string)),
3489 properties, string);
3490 RETURN_UNGCPRO (string);
3493 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3494 doc: /* Format a string out of a format-string and arguments.
3495 The first argument is a format control string.
3496 The other arguments are substituted into it to make the result, a string.
3498 The format control string may contain %-sequences meaning to substitute
3499 the next available argument:
3501 %s means print a string argument. Actually, prints any object, with `princ'.
3502 %d means print as number in decimal (%o octal, %x hex).
3503 %X is like %x, but uses upper case.
3504 %e means print a number in exponential notation.
3505 %f means print a number in decimal-point notation.
3506 %g means print a number in exponential notation
3507 or decimal-point notation, whichever uses fewer characters.
3508 %c means print a number as a single character.
3509 %S means print any object as an s-expression (using `prin1').
3511 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.
3512 Use %% to put a single % into the output.
3514 A %-sequence may contain optional flag, width, and precision
3515 specifiers, as follows:
3517 %<flags><width><precision>character
3519 where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+
3521 The + flag character inserts a + before any positive number, while a
3522 space inserts a space before any positive number; these flags only
3523 affect %d, %e, %f, and %g sequences, and the + flag takes precedence.
3524 The # flag means to use an alternate display form for %o, %x, %X, %e,
3525 %f, and %g sequences. The - and 0 flags affect the width specifier,
3526 as described below.
3528 The width specifier supplies a lower limit for the length of the
3529 printed representation. The padding, if any, normally goes on the
3530 left, but it goes on the right if the - flag is present. The padding
3531 character is normally a space, but it is 0 if the 0 flag is present.
3532 The 0 flag is ignored if the - flag is present, or the format sequence
3533 is something other than %d, %e, %f, and %g.
3535 For %e, %f, and %g sequences, the number after the "." in the
3536 precision specifier says how many decimal places to show; if zero, the
3537 decimal point itself is omitted. For %s and %S, the precision
3538 specifier truncates the string to the given width.
3540 usage: (format STRING &rest OBJECTS) */)
3541 (ptrdiff_t nargs, Lisp_Object *args)
3543 ptrdiff_t n; /* The number of the next arg to substitute */
3544 char initial_buffer[4000];
3545 char *buf = initial_buffer;
3546 ptrdiff_t bufsize = sizeof initial_buffer;
3547 ptrdiff_t max_bufsize = STRING_BYTES_BOUND + 1;
3548 char *p;
3549 Lisp_Object buf_save_value IF_LINT (= {0});
3550 register char *format, *end, *format_start;
3551 ptrdiff_t formatlen, nchars;
3552 /* Nonzero if the format is multibyte. */
3553 int multibyte_format = 0;
3554 /* Nonzero if the output should be a multibyte string,
3555 which is true if any of the inputs is one. */
3556 int multibyte = 0;
3557 /* When we make a multibyte string, we must pay attention to the
3558 byte combining problem, i.e., a byte may be combined with a
3559 multibyte character of the previous string. This flag tells if we
3560 must consider such a situation or not. */
3561 int maybe_combine_byte;
3562 Lisp_Object val;
3563 int arg_intervals = 0;
3564 USE_SAFE_ALLOCA;
3566 /* discarded[I] is 1 if byte I of the format
3567 string was not copied into the output.
3568 It is 2 if byte I was not the first byte of its character. */
3569 char *discarded;
3571 /* Each element records, for one argument,
3572 the start and end bytepos in the output string,
3573 whether the argument has been converted to string (e.g., due to "%S"),
3574 and whether the argument is a string with intervals.
3575 info[0] is unused. Unused elements have -1 for start. */
3576 struct info
3578 ptrdiff_t start, end;
3579 int converted_to_string;
3580 int intervals;
3581 } *info = 0;
3583 /* It should not be necessary to GCPRO ARGS, because
3584 the caller in the interpreter should take care of that. */
3586 CHECK_STRING (args[0]);
3587 format_start = SSDATA (args[0]);
3588 formatlen = SBYTES (args[0]);
3590 /* Allocate the info and discarded tables. */
3592 ptrdiff_t i;
3593 if ((SIZE_MAX - formatlen) / sizeof (struct info) <= nargs)
3594 memory_full (SIZE_MAX);
3595 SAFE_ALLOCA (info, struct info *, (nargs + 1) * sizeof *info + formatlen);
3596 discarded = (char *) &info[nargs + 1];
3597 for (i = 0; i < nargs + 1; i++)
3599 info[i].start = -1;
3600 info[i].intervals = info[i].converted_to_string = 0;
3602 memset (discarded, 0, formatlen);
3605 /* Try to determine whether the result should be multibyte.
3606 This is not always right; sometimes the result needs to be multibyte
3607 because of an object that we will pass through prin1,
3608 and in that case, we won't know it here. */
3609 multibyte_format = STRING_MULTIBYTE (args[0]);
3610 multibyte = multibyte_format;
3611 for (n = 1; !multibyte && n < nargs; n++)
3612 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3613 multibyte = 1;
3615 /* If we start out planning a unibyte result,
3616 then discover it has to be multibyte, we jump back to retry. */
3617 retry:
3619 p = buf;
3620 nchars = 0;
3621 n = 0;
3623 /* Scan the format and store result in BUF. */
3624 format = format_start;
3625 end = format + formatlen;
3626 maybe_combine_byte = 0;
3628 while (format != end)
3630 /* The values of N and FORMAT when the loop body is entered. */
3631 ptrdiff_t n0 = n;
3632 char *format0 = format;
3634 /* Bytes needed to represent the output of this conversion. */
3635 ptrdiff_t convbytes;
3637 if (*format == '%')
3639 /* General format specifications look like
3641 '%' [flags] [field-width] [precision] format
3643 where
3645 flags ::= [-+0# ]+
3646 field-width ::= [0-9]+
3647 precision ::= '.' [0-9]*
3649 If a field-width is specified, it specifies to which width
3650 the output should be padded with blanks, if the output
3651 string is shorter than field-width.
3653 If precision is specified, it specifies the number of
3654 digits to print after the '.' for floats, or the max.
3655 number of chars to print from a string. */
3657 int minus_flag = 0;
3658 int plus_flag = 0;
3659 int space_flag = 0;
3660 int sharp_flag = 0;
3661 int zero_flag = 0;
3662 ptrdiff_t field_width;
3663 int precision_given;
3664 uintmax_t precision = UINTMAX_MAX;
3665 char *num_end;
3666 char conversion;
3668 while (1)
3670 switch (*++format)
3672 case '-': minus_flag = 1; continue;
3673 case '+': plus_flag = 1; continue;
3674 case ' ': space_flag = 1; continue;
3675 case '#': sharp_flag = 1; continue;
3676 case '0': zero_flag = 1; continue;
3678 break;
3681 /* Ignore flags when sprintf ignores them. */
3682 space_flag &= ~ plus_flag;
3683 zero_flag &= ~ minus_flag;
3686 uintmax_t w = strtoumax (format, &num_end, 10);
3687 if (max_bufsize <= w)
3688 string_overflow ();
3689 field_width = w;
3691 precision_given = *num_end == '.';
3692 if (precision_given)
3693 precision = strtoumax (num_end + 1, &num_end, 10);
3694 format = num_end;
3696 if (format == end)
3697 error ("Format string ends in middle of format specifier");
3699 memset (&discarded[format0 - format_start], 1, format - format0);
3700 conversion = *format;
3701 if (conversion == '%')
3702 goto copy_char;
3703 discarded[format - format_start] = 1;
3704 format++;
3706 ++n;
3707 if (! (n < nargs))
3708 error ("Not enough arguments for format string");
3710 /* For 'S', prin1 the argument, and then treat like 's'.
3711 For 's', princ any argument that is not a string or
3712 symbol. But don't do this conversion twice, which might
3713 happen after retrying. */
3714 if ((conversion == 'S'
3715 || (conversion == 's'
3716 && ! STRINGP (args[n]) && ! SYMBOLP (args[n]))))
3718 if (! info[n].converted_to_string)
3720 Lisp_Object noescape = conversion == 'S' ? Qnil : Qt;
3721 args[n] = Fprin1_to_string (args[n], noescape);
3722 info[n].converted_to_string = 1;
3723 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3725 multibyte = 1;
3726 goto retry;
3729 conversion = 's';
3731 else if (conversion == 'c')
3733 if (FLOATP (args[n]))
3735 double d = XFLOAT_DATA (args[n]);
3736 args[n] = make_number (FIXNUM_OVERFLOW_P (d) ? -1 : d);
3739 if (INTEGERP (args[n]) && ! ASCII_CHAR_P (XINT (args[n])))
3741 if (!multibyte)
3743 multibyte = 1;
3744 goto retry;
3746 args[n] = Fchar_to_string (args[n]);
3747 info[n].converted_to_string = 1;
3750 if (info[n].converted_to_string)
3751 conversion = 's';
3752 zero_flag = 0;
3755 if (SYMBOLP (args[n]))
3757 args[n] = SYMBOL_NAME (args[n]);
3758 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3760 multibyte = 1;
3761 goto retry;
3765 if (conversion == 's')
3767 /* handle case (precision[n] >= 0) */
3769 ptrdiff_t width, padding, nbytes;
3770 ptrdiff_t nchars_string;
3772 ptrdiff_t prec = -1;
3773 if (precision_given && precision <= TYPE_MAXIMUM (ptrdiff_t))
3774 prec = precision;
3776 /* lisp_string_width ignores a precision of 0, but GNU
3777 libc functions print 0 characters when the precision
3778 is 0. Imitate libc behavior here. Changing
3779 lisp_string_width is the right thing, and will be
3780 done, but meanwhile we work with it. */
3782 if (prec == 0)
3783 width = nchars_string = nbytes = 0;
3784 else
3786 ptrdiff_t nch, nby;
3787 width = lisp_string_width (args[n], prec, &nch, &nby);
3788 if (prec < 0)
3790 nchars_string = SCHARS (args[n]);
3791 nbytes = SBYTES (args[n]);
3793 else
3795 nchars_string = nch;
3796 nbytes = nby;
3800 convbytes = nbytes;
3801 if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n]))
3802 convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes);
3804 padding = width < field_width ? field_width - width : 0;
3806 if (max_bufsize - padding <= convbytes)
3807 string_overflow ();
3808 convbytes += padding;
3809 if (convbytes <= buf + bufsize - p)
3811 if (! minus_flag)
3813 memset (p, ' ', padding);
3814 p += padding;
3815 nchars += padding;
3818 if (p > buf
3819 && multibyte
3820 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3821 && STRING_MULTIBYTE (args[n])
3822 && !CHAR_HEAD_P (SREF (args[n], 0)))
3823 maybe_combine_byte = 1;
3825 p += copy_text (SDATA (args[n]), (unsigned char *) p,
3826 nbytes,
3827 STRING_MULTIBYTE (args[n]), multibyte);
3829 info[n].start = nchars;
3830 nchars += nchars_string;
3831 info[n].end = nchars;
3833 if (minus_flag)
3835 memset (p, ' ', padding);
3836 p += padding;
3837 nchars += padding;
3840 /* If this argument has text properties, record where
3841 in the result string it appears. */
3842 if (STRING_INTERVALS (args[n]))
3843 info[n].intervals = arg_intervals = 1;
3845 continue;
3848 else if (! (conversion == 'c' || conversion == 'd'
3849 || conversion == 'e' || conversion == 'f'
3850 || conversion == 'g' || conversion == 'i'
3851 || conversion == 'o' || conversion == 'x'
3852 || conversion == 'X'))
3853 error ("Invalid format operation %%%c",
3854 STRING_CHAR ((unsigned char *) format - 1));
3855 else if (! (INTEGERP (args[n]) || FLOATP (args[n])))
3856 error ("Format specifier doesn't match argument type");
3857 else
3859 enum
3861 /* Maximum precision for a %f conversion such that the
3862 trailing output digit might be nonzero. Any precision
3863 larger than this will not yield useful information. */
3864 USEFUL_PRECISION_MAX =
3865 ((1 - DBL_MIN_EXP)
3866 * (FLT_RADIX == 2 || FLT_RADIX == 10 ? 1
3867 : FLT_RADIX == 16 ? 4
3868 : -1)),
3870 /* Maximum number of bytes generated by any format, if
3871 precision is no more than USEFUL_PRECISION_MAX.
3872 On all practical hosts, %f is the worst case. */
3873 SPRINTF_BUFSIZE =
3874 sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX,
3876 /* Length of pM (that is, of pMd without the
3877 trailing "d"). */
3878 pMlen = sizeof pMd - 2
3880 verify (0 < USEFUL_PRECISION_MAX);
3882 int prec;
3883 ptrdiff_t padding, sprintf_bytes;
3884 uintmax_t excess_precision, numwidth;
3885 uintmax_t leading_zeros = 0, trailing_zeros = 0;
3887 char sprintf_buf[SPRINTF_BUFSIZE];
3889 /* Copy of conversion specification, modified somewhat.
3890 At most three flags F can be specified at once. */
3891 char convspec[sizeof "%FFF.*d" + pMlen];
3893 /* Avoid undefined behavior in underlying sprintf. */
3894 if (conversion == 'd' || conversion == 'i')
3895 sharp_flag = 0;
3897 /* Create the copy of the conversion specification, with
3898 any width and precision removed, with ".*" inserted,
3899 and with pM inserted for integer formats. */
3901 char *f = convspec;
3902 *f++ = '%';
3903 *f = '-'; f += minus_flag;
3904 *f = '+'; f += plus_flag;
3905 *f = ' '; f += space_flag;
3906 *f = '#'; f += sharp_flag;
3907 *f = '0'; f += zero_flag;
3908 *f++ = '.';
3909 *f++ = '*';
3910 if (conversion == 'd' || conversion == 'i'
3911 || conversion == 'o' || conversion == 'x'
3912 || conversion == 'X')
3914 memcpy (f, pMd, pMlen);
3915 f += pMlen;
3916 zero_flag &= ~ precision_given;
3918 *f++ = conversion;
3919 *f = '\0';
3922 prec = -1;
3923 if (precision_given)
3924 prec = min (precision, USEFUL_PRECISION_MAX);
3926 /* Use sprintf to format this number into sprintf_buf. Omit
3927 padding and excess precision, though, because sprintf limits
3928 output length to INT_MAX.
3930 There are four types of conversion: double, unsigned
3931 char (passed as int), wide signed int, and wide
3932 unsigned int. Treat them separately because the
3933 sprintf ABI is sensitive to which type is passed. Be
3934 careful about integer overflow, NaNs, infinities, and
3935 conversions; for example, the min and max macros are
3936 not suitable here. */
3937 if (conversion == 'e' || conversion == 'f' || conversion == 'g')
3939 double x = (INTEGERP (args[n])
3940 ? XINT (args[n])
3941 : XFLOAT_DATA (args[n]));
3942 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
3944 else if (conversion == 'c')
3946 /* Don't use sprintf here, as it might mishandle prec. */
3947 sprintf_buf[0] = XINT (args[n]);
3948 sprintf_bytes = prec != 0;
3950 else if (conversion == 'd')
3952 /* For float, maybe we should use "%1.0f"
3953 instead so it also works for values outside
3954 the integer range. */
3955 printmax_t x;
3956 if (INTEGERP (args[n]))
3957 x = XINT (args[n]);
3958 else
3960 double d = XFLOAT_DATA (args[n]);
3961 if (d < 0)
3963 x = TYPE_MINIMUM (printmax_t);
3964 if (x < d)
3965 x = d;
3967 else
3969 x = TYPE_MAXIMUM (printmax_t);
3970 if (d < x)
3971 x = d;
3974 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
3976 else
3978 /* Don't sign-extend for octal or hex printing. */
3979 uprintmax_t x;
3980 if (INTEGERP (args[n]))
3981 x = XUINT (args[n]);
3982 else
3984 double d = XFLOAT_DATA (args[n]);
3985 if (d < 0)
3986 x = 0;
3987 else
3989 x = TYPE_MAXIMUM (uprintmax_t);
3990 if (d < x)
3991 x = d;
3994 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
3997 /* Now the length of the formatted item is known, except it omits
3998 padding and excess precision. Deal with excess precision
3999 first. This happens only when the format specifies
4000 ridiculously large precision. */
4001 excess_precision = precision - prec;
4002 if (excess_precision)
4004 if (conversion == 'e' || conversion == 'f'
4005 || conversion == 'g')
4007 if ((conversion == 'g' && ! sharp_flag)
4008 || ! ('0' <= sprintf_buf[sprintf_bytes - 1]
4009 && sprintf_buf[sprintf_bytes - 1] <= '9'))
4010 excess_precision = 0;
4011 else
4013 if (conversion == 'g')
4015 char *dot = strchr (sprintf_buf, '.');
4016 if (!dot)
4017 excess_precision = 0;
4020 trailing_zeros = excess_precision;
4022 else
4023 leading_zeros = excess_precision;
4026 /* Compute the total bytes needed for this item, including
4027 excess precision and padding. */
4028 numwidth = sprintf_bytes + excess_precision;
4029 padding = numwidth < field_width ? field_width - numwidth : 0;
4030 if (max_bufsize - sprintf_bytes <= excess_precision
4031 || max_bufsize - padding <= numwidth)
4032 string_overflow ();
4033 convbytes = numwidth + padding;
4035 if (convbytes <= buf + bufsize - p)
4037 /* Copy the formatted item from sprintf_buf into buf,
4038 inserting padding and excess-precision zeros. */
4040 char *src = sprintf_buf;
4041 char src0 = src[0];
4042 int exponent_bytes = 0;
4043 int signedp = src0 == '-' || src0 == '+' || src0 == ' ';
4044 int significand_bytes;
4045 if (zero_flag
4046 && ((src[signedp] >= '0' && src[signedp] <= '9')
4047 || (src[signedp] >= 'a' && src[signedp] <= 'f')
4048 || (src[signedp] >= 'A' && src[signedp] <= 'F')))
4050 leading_zeros += padding;
4051 padding = 0;
4054 if (excess_precision
4055 && (conversion == 'e' || conversion == 'g'))
4057 char *e = strchr (src, 'e');
4058 if (e)
4059 exponent_bytes = src + sprintf_bytes - e;
4062 if (! minus_flag)
4064 memset (p, ' ', padding);
4065 p += padding;
4066 nchars += padding;
4069 *p = src0;
4070 src += signedp;
4071 p += signedp;
4072 memset (p, '0', leading_zeros);
4073 p += leading_zeros;
4074 significand_bytes = sprintf_bytes - signedp - exponent_bytes;
4075 memcpy (p, src, significand_bytes);
4076 p += significand_bytes;
4077 src += significand_bytes;
4078 memset (p, '0', trailing_zeros);
4079 p += trailing_zeros;
4080 memcpy (p, src, exponent_bytes);
4081 p += exponent_bytes;
4083 info[n].start = nchars;
4084 nchars += leading_zeros + sprintf_bytes + trailing_zeros;
4085 info[n].end = nchars;
4087 if (minus_flag)
4089 memset (p, ' ', padding);
4090 p += padding;
4091 nchars += padding;
4094 continue;
4098 else
4099 copy_char:
4101 /* Copy a single character from format to buf. */
4103 char *src = format;
4104 unsigned char str[MAX_MULTIBYTE_LENGTH];
4106 if (multibyte_format)
4108 /* Copy a whole multibyte character. */
4109 if (p > buf
4110 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
4111 && !CHAR_HEAD_P (*format))
4112 maybe_combine_byte = 1;
4115 format++;
4116 while (! CHAR_HEAD_P (*format));
4118 convbytes = format - src;
4119 memset (&discarded[src + 1 - format_start], 2, convbytes - 1);
4121 else
4123 unsigned char uc = *format++;
4124 if (! multibyte || ASCII_BYTE_P (uc))
4125 convbytes = 1;
4126 else
4128 int c = BYTE8_TO_CHAR (uc);
4129 convbytes = CHAR_STRING (c, str);
4130 src = (char *) str;
4134 if (convbytes <= buf + bufsize - p)
4136 memcpy (p, src, convbytes);
4137 p += convbytes;
4138 nchars++;
4139 continue;
4143 /* There wasn't enough room to store this conversion or single
4144 character. CONVBYTES says how much room is needed. Allocate
4145 enough room (and then some) and do it again. */
4147 ptrdiff_t used = p - buf;
4149 if (max_bufsize - used < convbytes)
4150 string_overflow ();
4151 bufsize = used + convbytes;
4152 bufsize = bufsize < max_bufsize / 2 ? bufsize * 2 : max_bufsize;
4154 if (buf == initial_buffer)
4156 buf = xmalloc (bufsize);
4157 sa_must_free = 1;
4158 buf_save_value = make_save_value (buf, 0);
4159 record_unwind_protect (safe_alloca_unwind, buf_save_value);
4160 memcpy (buf, initial_buffer, used);
4162 else
4163 XSAVE_VALUE (buf_save_value)->pointer = buf = xrealloc (buf, bufsize);
4165 p = buf + used;
4168 format = format0;
4169 n = n0;
4172 if (bufsize < p - buf)
4173 abort ();
4175 if (maybe_combine_byte)
4176 nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf);
4177 val = make_specified_string (buf, nchars, p - buf, multibyte);
4179 /* If we allocated BUF with malloc, free it too. */
4180 SAFE_FREE ();
4182 /* If the format string has text properties, or any of the string
4183 arguments has text properties, set up text properties of the
4184 result string. */
4186 if (STRING_INTERVALS (args[0]) || arg_intervals)
4188 Lisp_Object len, new_len, props;
4189 struct gcpro gcpro1;
4191 /* Add text properties from the format string. */
4192 len = make_number (SCHARS (args[0]));
4193 props = text_property_list (args[0], make_number (0), len, Qnil);
4194 GCPRO1 (props);
4196 if (CONSP (props))
4198 ptrdiff_t bytepos = 0, position = 0, translated = 0;
4199 ptrdiff_t argn = 1;
4200 Lisp_Object list;
4202 /* Adjust the bounds of each text property
4203 to the proper start and end in the output string. */
4205 /* Put the positions in PROPS in increasing order, so that
4206 we can do (effectively) one scan through the position
4207 space of the format string. */
4208 props = Fnreverse (props);
4210 /* BYTEPOS is the byte position in the format string,
4211 POSITION is the untranslated char position in it,
4212 TRANSLATED is the translated char position in BUF,
4213 and ARGN is the number of the next arg we will come to. */
4214 for (list = props; CONSP (list); list = XCDR (list))
4216 Lisp_Object item;
4217 ptrdiff_t pos;
4219 item = XCAR (list);
4221 /* First adjust the property start position. */
4222 pos = XINT (XCAR (item));
4224 /* Advance BYTEPOS, POSITION, TRANSLATED and ARGN
4225 up to this position. */
4226 for (; position < pos; bytepos++)
4228 if (! discarded[bytepos])
4229 position++, translated++;
4230 else if (discarded[bytepos] == 1)
4232 position++;
4233 if (translated == info[argn].start)
4235 translated += info[argn].end - info[argn].start;
4236 argn++;
4241 XSETCAR (item, make_number (translated));
4243 /* Likewise adjust the property end position. */
4244 pos = XINT (XCAR (XCDR (item)));
4246 for (; position < pos; bytepos++)
4248 if (! discarded[bytepos])
4249 position++, translated++;
4250 else if (discarded[bytepos] == 1)
4252 position++;
4253 if (translated == info[argn].start)
4255 translated += info[argn].end - info[argn].start;
4256 argn++;
4261 XSETCAR (XCDR (item), make_number (translated));
4264 add_text_properties_from_list (val, props, make_number (0));
4267 /* Add text properties from arguments. */
4268 if (arg_intervals)
4269 for (n = 1; n < nargs; ++n)
4270 if (info[n].intervals)
4272 len = make_number (SCHARS (args[n]));
4273 new_len = make_number (info[n].end - info[n].start);
4274 props = text_property_list (args[n], make_number (0), len, Qnil);
4275 props = extend_property_ranges (props, new_len);
4276 /* If successive arguments have properties, be sure that
4277 the value of `composition' property be the copy. */
4278 if (n > 1 && info[n - 1].end)
4279 make_composition_value_copy (props);
4280 add_text_properties_from_list (val, props,
4281 make_number (info[n].start));
4284 UNGCPRO;
4287 return val;
4290 Lisp_Object
4291 format2 (const char *string1, Lisp_Object arg0, Lisp_Object arg1)
4293 Lisp_Object args[3];
4294 args[0] = build_string (string1);
4295 args[1] = arg0;
4296 args[2] = arg1;
4297 return Fformat (3, args);
4300 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
4301 doc: /* Return t if two characters match, optionally ignoring case.
4302 Both arguments must be characters (i.e. integers).
4303 Case is ignored if `case-fold-search' is non-nil in the current buffer. */)
4304 (register Lisp_Object c1, Lisp_Object c2)
4306 int i1, i2;
4307 /* Check they're chars, not just integers, otherwise we could get array
4308 bounds violations in downcase. */
4309 CHECK_CHARACTER (c1);
4310 CHECK_CHARACTER (c2);
4312 if (XINT (c1) == XINT (c2))
4313 return Qt;
4314 if (NILP (BVAR (current_buffer, case_fold_search)))
4315 return Qnil;
4317 i1 = XFASTINT (c1);
4318 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4319 && ! ASCII_CHAR_P (i1))
4321 MAKE_CHAR_MULTIBYTE (i1);
4323 i2 = XFASTINT (c2);
4324 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4325 && ! ASCII_CHAR_P (i2))
4327 MAKE_CHAR_MULTIBYTE (i2);
4329 return (downcase (i1) == downcase (i2) ? Qt : Qnil);
4332 /* Transpose the markers in two regions of the current buffer, and
4333 adjust the ones between them if necessary (i.e.: if the regions
4334 differ in size).
4336 START1, END1 are the character positions of the first region.
4337 START1_BYTE, END1_BYTE are the byte positions.
4338 START2, END2 are the character positions of the second region.
4339 START2_BYTE, END2_BYTE are the byte positions.
4341 Traverses the entire marker list of the buffer to do so, adding an
4342 appropriate amount to some, subtracting from some, and leaving the
4343 rest untouched. Most of this is copied from adjust_markers in insdel.c.
4345 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
4347 static void
4348 transpose_markers (ptrdiff_t start1, ptrdiff_t end1,
4349 ptrdiff_t start2, ptrdiff_t end2,
4350 ptrdiff_t start1_byte, ptrdiff_t end1_byte,
4351 ptrdiff_t start2_byte, ptrdiff_t end2_byte)
4353 register ptrdiff_t amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
4354 register struct Lisp_Marker *marker;
4356 /* Update point as if it were a marker. */
4357 if (PT < start1)
4359 else if (PT < end1)
4360 TEMP_SET_PT_BOTH (PT + (end2 - end1),
4361 PT_BYTE + (end2_byte - end1_byte));
4362 else if (PT < start2)
4363 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
4364 (PT_BYTE + (end2_byte - start2_byte)
4365 - (end1_byte - start1_byte)));
4366 else if (PT < end2)
4367 TEMP_SET_PT_BOTH (PT - (start2 - start1),
4368 PT_BYTE - (start2_byte - start1_byte));
4370 /* We used to adjust the endpoints here to account for the gap, but that
4371 isn't good enough. Even if we assume the caller has tried to move the
4372 gap out of our way, it might still be at start1 exactly, for example;
4373 and that places it `inside' the interval, for our purposes. The amount
4374 of adjustment is nontrivial if there's a `denormalized' marker whose
4375 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
4376 the dirty work to Fmarker_position, below. */
4378 /* The difference between the region's lengths */
4379 diff = (end2 - start2) - (end1 - start1);
4380 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
4382 /* For shifting each marker in a region by the length of the other
4383 region plus the distance between the regions. */
4384 amt1 = (end2 - start2) + (start2 - end1);
4385 amt2 = (end1 - start1) + (start2 - end1);
4386 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
4387 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
4389 for (marker = BUF_MARKERS (current_buffer); marker; marker = marker->next)
4391 mpos = marker->bytepos;
4392 if (mpos >= start1_byte && mpos < end2_byte)
4394 if (mpos < end1_byte)
4395 mpos += amt1_byte;
4396 else if (mpos < start2_byte)
4397 mpos += diff_byte;
4398 else
4399 mpos -= amt2_byte;
4400 marker->bytepos = mpos;
4402 mpos = marker->charpos;
4403 if (mpos >= start1 && mpos < end2)
4405 if (mpos < end1)
4406 mpos += amt1;
4407 else if (mpos < start2)
4408 mpos += diff;
4409 else
4410 mpos -= amt2;
4412 marker->charpos = mpos;
4416 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
4417 doc: /* Transpose region STARTR1 to ENDR1 with STARTR2 to ENDR2.
4418 The regions should not be overlapping, because the size of the buffer is
4419 never changed in a transposition.
4421 Optional fifth arg LEAVE-MARKERS, if non-nil, means don't update
4422 any markers that happen to be located in the regions.
4424 Transposing beyond buffer boundaries is an error. */)
4425 (Lisp_Object startr1, Lisp_Object endr1, Lisp_Object startr2, Lisp_Object endr2, Lisp_Object leave_markers)
4427 register ptrdiff_t start1, end1, start2, end2;
4428 ptrdiff_t start1_byte, start2_byte, len1_byte, len2_byte;
4429 ptrdiff_t gap, len1, len_mid, len2;
4430 unsigned char *start1_addr, *start2_addr, *temp;
4432 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2, tmp_interval3;
4433 Lisp_Object buf;
4435 XSETBUFFER (buf, current_buffer);
4436 cur_intv = BUF_INTERVALS (current_buffer);
4438 validate_region (&startr1, &endr1);
4439 validate_region (&startr2, &endr2);
4441 start1 = XFASTINT (startr1);
4442 end1 = XFASTINT (endr1);
4443 start2 = XFASTINT (startr2);
4444 end2 = XFASTINT (endr2);
4445 gap = GPT;
4447 /* Swap the regions if they're reversed. */
4448 if (start2 < end1)
4450 register ptrdiff_t glumph = start1;
4451 start1 = start2;
4452 start2 = glumph;
4453 glumph = end1;
4454 end1 = end2;
4455 end2 = glumph;
4458 len1 = end1 - start1;
4459 len2 = end2 - start2;
4461 if (start2 < end1)
4462 error ("Transposed regions overlap");
4463 /* Nothing to change for adjacent regions with one being empty */
4464 else if ((start1 == end1 || start2 == end2) && end1 == start2)
4465 return Qnil;
4467 /* The possibilities are:
4468 1. Adjacent (contiguous) regions, or separate but equal regions
4469 (no, really equal, in this case!), or
4470 2. Separate regions of unequal size.
4472 The worst case is usually No. 2. It means that (aside from
4473 potential need for getting the gap out of the way), there also
4474 needs to be a shifting of the text between the two regions. So
4475 if they are spread far apart, we are that much slower... sigh. */
4477 /* It must be pointed out that the really studly thing to do would
4478 be not to move the gap at all, but to leave it in place and work
4479 around it if necessary. This would be extremely efficient,
4480 especially considering that people are likely to do
4481 transpositions near where they are working interactively, which
4482 is exactly where the gap would be found. However, such code
4483 would be much harder to write and to read. So, if you are
4484 reading this comment and are feeling squirrely, by all means have
4485 a go! I just didn't feel like doing it, so I will simply move
4486 the gap the minimum distance to get it out of the way, and then
4487 deal with an unbroken array. */
4489 /* Make sure the gap won't interfere, by moving it out of the text
4490 we will operate on. */
4491 if (start1 < gap && gap < end2)
4493 if (gap - start1 < end2 - gap)
4494 move_gap (start1);
4495 else
4496 move_gap (end2);
4499 start1_byte = CHAR_TO_BYTE (start1);
4500 start2_byte = CHAR_TO_BYTE (start2);
4501 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
4502 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
4504 #ifdef BYTE_COMBINING_DEBUG
4505 if (end1 == start2)
4507 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4508 len2_byte, start1, start1_byte)
4509 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4510 len1_byte, end2, start2_byte + len2_byte)
4511 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4512 len1_byte, end2, start2_byte + len2_byte))
4513 abort ();
4515 else
4517 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4518 len2_byte, start1, start1_byte)
4519 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4520 len1_byte, start2, start2_byte)
4521 || count_combining_after (BYTE_POS_ADDR (start2_byte),
4522 len2_byte, end1, start1_byte + len1_byte)
4523 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4524 len1_byte, end2, start2_byte + len2_byte))
4525 abort ();
4527 #endif
4529 /* Hmmm... how about checking to see if the gap is large
4530 enough to use as the temporary storage? That would avoid an
4531 allocation... interesting. Later, don't fool with it now. */
4533 /* Working without memmove, for portability (sigh), so must be
4534 careful of overlapping subsections of the array... */
4536 if (end1 == start2) /* adjacent regions */
4538 modify_region (current_buffer, start1, end2, 0);
4539 record_change (start1, len1 + len2);
4541 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4542 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4543 /* Don't use Fset_text_properties: that can cause GC, which can
4544 clobber objects stored in the tmp_intervals. */
4545 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4546 if (!NULL_INTERVAL_P (tmp_interval3))
4547 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4549 /* First region smaller than second. */
4550 if (len1_byte < len2_byte)
4552 USE_SAFE_ALLOCA;
4554 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4556 /* Don't precompute these addresses. We have to compute them
4557 at the last minute, because the relocating allocator might
4558 have moved the buffer around during the xmalloc. */
4559 start1_addr = BYTE_POS_ADDR (start1_byte);
4560 start2_addr = BYTE_POS_ADDR (start2_byte);
4562 memcpy (temp, start2_addr, len2_byte);
4563 memcpy (start1_addr + len2_byte, start1_addr, len1_byte);
4564 memcpy (start1_addr, temp, len2_byte);
4565 SAFE_FREE ();
4567 else
4568 /* First region not smaller than second. */
4570 USE_SAFE_ALLOCA;
4572 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4573 start1_addr = BYTE_POS_ADDR (start1_byte);
4574 start2_addr = BYTE_POS_ADDR (start2_byte);
4575 memcpy (temp, start1_addr, len1_byte);
4576 memcpy (start1_addr, start2_addr, len2_byte);
4577 memcpy (start1_addr + len2_byte, temp, len1_byte);
4578 SAFE_FREE ();
4580 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
4581 len1, current_buffer, 0);
4582 graft_intervals_into_buffer (tmp_interval2, start1,
4583 len2, current_buffer, 0);
4584 update_compositions (start1, start1 + len2, CHECK_BORDER);
4585 update_compositions (start1 + len2, end2, CHECK_TAIL);
4587 /* Non-adjacent regions, because end1 != start2, bleagh... */
4588 else
4590 len_mid = start2_byte - (start1_byte + len1_byte);
4592 if (len1_byte == len2_byte)
4593 /* Regions are same size, though, how nice. */
4595 USE_SAFE_ALLOCA;
4597 modify_region (current_buffer, start1, end1, 0);
4598 modify_region (current_buffer, start2, end2, 0);
4599 record_change (start1, len1);
4600 record_change (start2, len2);
4601 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4602 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4604 tmp_interval3 = validate_interval_range (buf, &startr1, &endr1, 0);
4605 if (!NULL_INTERVAL_P (tmp_interval3))
4606 set_text_properties_1 (startr1, endr1, Qnil, buf, tmp_interval3);
4608 tmp_interval3 = validate_interval_range (buf, &startr2, &endr2, 0);
4609 if (!NULL_INTERVAL_P (tmp_interval3))
4610 set_text_properties_1 (startr2, endr2, Qnil, buf, tmp_interval3);
4612 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4613 start1_addr = BYTE_POS_ADDR (start1_byte);
4614 start2_addr = BYTE_POS_ADDR (start2_byte);
4615 memcpy (temp, start1_addr, len1_byte);
4616 memcpy (start1_addr, start2_addr, len2_byte);
4617 memcpy (start2_addr, temp, len1_byte);
4618 SAFE_FREE ();
4620 graft_intervals_into_buffer (tmp_interval1, start2,
4621 len1, current_buffer, 0);
4622 graft_intervals_into_buffer (tmp_interval2, start1,
4623 len2, current_buffer, 0);
4626 else if (len1_byte < len2_byte) /* Second region larger than first */
4627 /* Non-adjacent & unequal size, area between must also be shifted. */
4629 USE_SAFE_ALLOCA;
4631 modify_region (current_buffer, start1, end2, 0);
4632 record_change (start1, (end2 - start1));
4633 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4634 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4635 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4637 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4638 if (!NULL_INTERVAL_P (tmp_interval3))
4639 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4641 /* holds region 2 */
4642 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4643 start1_addr = BYTE_POS_ADDR (start1_byte);
4644 start2_addr = BYTE_POS_ADDR (start2_byte);
4645 memcpy (temp, start2_addr, len2_byte);
4646 memcpy (start1_addr + len_mid + len2_byte, start1_addr, len1_byte);
4647 memmove (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4648 memcpy (start1_addr, temp, len2_byte);
4649 SAFE_FREE ();
4651 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4652 len1, current_buffer, 0);
4653 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4654 len_mid, current_buffer, 0);
4655 graft_intervals_into_buffer (tmp_interval2, start1,
4656 len2, current_buffer, 0);
4658 else
4659 /* Second region smaller than first. */
4661 USE_SAFE_ALLOCA;
4663 record_change (start1, (end2 - start1));
4664 modify_region (current_buffer, start1, end2, 0);
4666 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4667 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4668 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4670 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4671 if (!NULL_INTERVAL_P (tmp_interval3))
4672 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4674 /* holds region 1 */
4675 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4676 start1_addr = BYTE_POS_ADDR (start1_byte);
4677 start2_addr = BYTE_POS_ADDR (start2_byte);
4678 memcpy (temp, start1_addr, len1_byte);
4679 memcpy (start1_addr, start2_addr, len2_byte);
4680 memcpy (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4681 memcpy (start1_addr + len2_byte + len_mid, temp, len1_byte);
4682 SAFE_FREE ();
4684 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4685 len1, current_buffer, 0);
4686 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4687 len_mid, current_buffer, 0);
4688 graft_intervals_into_buffer (tmp_interval2, start1,
4689 len2, current_buffer, 0);
4692 update_compositions (start1, start1 + len2, CHECK_BORDER);
4693 update_compositions (end2 - len1, end2, CHECK_BORDER);
4696 /* When doing multiple transpositions, it might be nice
4697 to optimize this. Perhaps the markers in any one buffer
4698 should be organized in some sorted data tree. */
4699 if (NILP (leave_markers))
4701 transpose_markers (start1, end1, start2, end2,
4702 start1_byte, start1_byte + len1_byte,
4703 start2_byte, start2_byte + len2_byte);
4704 fix_start_end_in_overlays (start1, end2);
4707 signal_after_change (start1, end2 - start1, end2 - start1);
4708 return Qnil;
4712 void
4713 syms_of_editfns (void)
4715 environbuf = 0;
4716 initial_tz = 0;
4718 DEFSYM (Qbuffer_access_fontify_functions, "buffer-access-fontify-functions");
4720 DEFVAR_LISP ("inhibit-field-text-motion", Vinhibit_field_text_motion,
4721 doc: /* Non-nil means text motion commands don't notice fields. */);
4722 Vinhibit_field_text_motion = Qnil;
4724 DEFVAR_LISP ("buffer-access-fontify-functions",
4725 Vbuffer_access_fontify_functions,
4726 doc: /* List of functions called by `buffer-substring' to fontify if necessary.
4727 Each function is called with two arguments which specify the range
4728 of the buffer being accessed. */);
4729 Vbuffer_access_fontify_functions = Qnil;
4732 Lisp_Object obuf;
4733 obuf = Fcurrent_buffer ();
4734 /* Do this here, because init_buffer_once is too early--it won't work. */
4735 Fset_buffer (Vprin1_to_string_buffer);
4736 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
4737 Fset (Fmake_local_variable (intern_c_string ("buffer-access-fontify-functions")),
4738 Qnil);
4739 Fset_buffer (obuf);
4742 DEFVAR_LISP ("buffer-access-fontified-property",
4743 Vbuffer_access_fontified_property,
4744 doc: /* Property which (if non-nil) indicates text has been fontified.
4745 `buffer-substring' need not call the `buffer-access-fontify-functions'
4746 functions if all the text being accessed has this property. */);
4747 Vbuffer_access_fontified_property = Qnil;
4749 DEFVAR_LISP ("system-name", Vsystem_name,
4750 doc: /* The host name of the machine Emacs is running on. */);
4752 DEFVAR_LISP ("user-full-name", Vuser_full_name,
4753 doc: /* The full name of the user logged in. */);
4755 DEFVAR_LISP ("user-login-name", Vuser_login_name,
4756 doc: /* The user's name, taken from environment variables if possible. */);
4758 DEFVAR_LISP ("user-real-login-name", Vuser_real_login_name,
4759 doc: /* The user's name, based upon the real uid only. */);
4761 DEFVAR_LISP ("operating-system-release", Voperating_system_release,
4762 doc: /* The release of the operating system Emacs is running on. */);
4764 defsubr (&Spropertize);
4765 defsubr (&Schar_equal);
4766 defsubr (&Sgoto_char);
4767 defsubr (&Sstring_to_char);
4768 defsubr (&Schar_to_string);
4769 defsubr (&Sbyte_to_string);
4770 defsubr (&Sbuffer_substring);
4771 defsubr (&Sbuffer_substring_no_properties);
4772 defsubr (&Sbuffer_string);
4774 defsubr (&Spoint_marker);
4775 defsubr (&Smark_marker);
4776 defsubr (&Spoint);
4777 defsubr (&Sregion_beginning);
4778 defsubr (&Sregion_end);
4780 DEFSYM (Qfield, "field");
4781 DEFSYM (Qboundary, "boundary");
4782 defsubr (&Sfield_beginning);
4783 defsubr (&Sfield_end);
4784 defsubr (&Sfield_string);
4785 defsubr (&Sfield_string_no_properties);
4786 defsubr (&Sdelete_field);
4787 defsubr (&Sconstrain_to_field);
4789 defsubr (&Sline_beginning_position);
4790 defsubr (&Sline_end_position);
4792 /* defsubr (&Smark); */
4793 /* defsubr (&Sset_mark); */
4794 defsubr (&Ssave_excursion);
4795 defsubr (&Ssave_current_buffer);
4797 defsubr (&Sbufsize);
4798 defsubr (&Spoint_max);
4799 defsubr (&Spoint_min);
4800 defsubr (&Spoint_min_marker);
4801 defsubr (&Spoint_max_marker);
4802 defsubr (&Sgap_position);
4803 defsubr (&Sgap_size);
4804 defsubr (&Sposition_bytes);
4805 defsubr (&Sbyte_to_position);
4807 defsubr (&Sbobp);
4808 defsubr (&Seobp);
4809 defsubr (&Sbolp);
4810 defsubr (&Seolp);
4811 defsubr (&Sfollowing_char);
4812 defsubr (&Sprevious_char);
4813 defsubr (&Schar_after);
4814 defsubr (&Schar_before);
4815 defsubr (&Sinsert);
4816 defsubr (&Sinsert_before_markers);
4817 defsubr (&Sinsert_and_inherit);
4818 defsubr (&Sinsert_and_inherit_before_markers);
4819 defsubr (&Sinsert_char);
4820 defsubr (&Sinsert_byte);
4822 defsubr (&Suser_login_name);
4823 defsubr (&Suser_real_login_name);
4824 defsubr (&Suser_uid);
4825 defsubr (&Suser_real_uid);
4826 defsubr (&Suser_full_name);
4827 defsubr (&Semacs_pid);
4828 defsubr (&Scurrent_time);
4829 defsubr (&Sget_internal_run_time);
4830 defsubr (&Sformat_time_string);
4831 defsubr (&Sfloat_time);
4832 defsubr (&Sdecode_time);
4833 defsubr (&Sencode_time);
4834 defsubr (&Scurrent_time_string);
4835 defsubr (&Scurrent_time_zone);
4836 defsubr (&Sset_time_zone_rule);
4837 defsubr (&Ssystem_name);
4838 defsubr (&Smessage);
4839 defsubr (&Smessage_box);
4840 defsubr (&Smessage_or_box);
4841 defsubr (&Scurrent_message);
4842 defsubr (&Sformat);
4844 defsubr (&Sinsert_buffer_substring);
4845 defsubr (&Scompare_buffer_substrings);
4846 defsubr (&Ssubst_char_in_region);
4847 defsubr (&Stranslate_region_internal);
4848 defsubr (&Sdelete_region);
4849 defsubr (&Sdelete_and_extract_region);
4850 defsubr (&Swiden);
4851 defsubr (&Snarrow_to_region);
4852 defsubr (&Ssave_restriction);
4853 defsubr (&Stranspose_regions);