* lisp/emacs-lisp/pcase.el (pcase-UPAT, pcase-QPAT): New edebug specs.
[emacs.git] / src / editfns.c
blob8f7b2aee76c80f5f8ae1b51d865ac66c08845ac3
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 USER_FULL_NAME
63 #define USER_FULL_NAME pw->pw_gecos
64 #endif
66 #ifndef USE_CRT_DLL
67 extern char **environ;
68 #endif
70 #define TM_YEAR_BASE 1900
72 #ifdef WINDOWSNT
73 extern Lisp_Object w32_get_internal_run_time (void);
74 #endif
76 static void time_overflow (void) NO_RETURN;
77 static Lisp_Object format_time_string (char const *, ptrdiff_t, Lisp_Object,
78 int, time_t *, struct tm *);
79 static int tm_diff (struct tm *, struct tm *);
80 static void update_buffer_properties (ptrdiff_t, ptrdiff_t);
82 static Lisp_Object Qbuffer_access_fontify_functions;
83 static Lisp_Object Fuser_full_name (Lisp_Object);
85 /* Symbol for the text property used to mark fields. */
87 Lisp_Object Qfield;
89 /* A special value for Qfield properties. */
91 static Lisp_Object Qboundary;
94 void
95 init_editfns (void)
97 const char *user_name;
98 register char *p;
99 struct passwd *pw; /* password entry for the current user */
100 Lisp_Object tem;
102 /* Set up system_name even when dumping. */
103 init_system_name ();
105 #ifndef CANNOT_DUMP
106 /* Don't bother with this on initial start when just dumping out */
107 if (!initialized)
108 return;
109 #endif /* not CANNOT_DUMP */
111 pw = getpwuid (getuid ());
112 #ifdef MSDOS
113 /* We let the real user name default to "root" because that's quite
114 accurate on MSDOG and because it lets Emacs find the init file.
115 (The DVX libraries override the Djgpp libraries here.) */
116 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
117 #else
118 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
119 #endif
121 /* Get the effective user name, by consulting environment variables,
122 or the effective uid if those are unset. */
123 user_name = getenv ("LOGNAME");
124 if (!user_name)
125 #ifdef WINDOWSNT
126 user_name = getenv ("USERNAME"); /* it's USERNAME on NT */
127 #else /* WINDOWSNT */
128 user_name = getenv ("USER");
129 #endif /* WINDOWSNT */
130 if (!user_name)
132 pw = getpwuid (geteuid ());
133 user_name = pw ? pw->pw_name : "unknown";
135 Vuser_login_name = build_string (user_name);
137 /* If the user name claimed in the environment vars differs from
138 the real uid, use the claimed name to find the full name. */
139 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
140 if (! NILP (tem))
141 tem = Vuser_login_name;
142 else
144 uid_t euid = geteuid ();
145 tem = make_fixnum_or_float (euid);
147 Vuser_full_name = Fuser_full_name (tem);
149 p = getenv ("NAME");
150 if (p)
151 Vuser_full_name = build_string (p);
152 else if (NILP (Vuser_full_name))
153 Vuser_full_name = build_string ("unknown");
155 #ifdef HAVE_SYS_UTSNAME_H
157 struct utsname uts;
158 uname (&uts);
159 Voperating_system_release = build_string (uts.release);
161 #else
162 Voperating_system_release = Qnil;
163 #endif
166 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
167 doc: /* Convert arg CHAR to a string containing that character.
168 usage: (char-to-string CHAR) */)
169 (Lisp_Object character)
171 int c, len;
172 unsigned char str[MAX_MULTIBYTE_LENGTH];
174 CHECK_CHARACTER (character);
175 c = XFASTINT (character);
177 len = CHAR_STRING (c, str);
178 return make_string_from_bytes ((char *) str, 1, len);
181 DEFUN ("byte-to-string", Fbyte_to_string, Sbyte_to_string, 1, 1, 0,
182 doc: /* Convert arg BYTE to a unibyte string containing that byte. */)
183 (Lisp_Object byte)
185 unsigned char b;
186 CHECK_NUMBER (byte);
187 if (XINT (byte) < 0 || XINT (byte) > 255)
188 error ("Invalid byte");
189 b = XINT (byte);
190 return make_string_from_bytes ((char *) &b, 1, 1);
193 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
194 doc: /* Return the first character in STRING. */)
195 (register Lisp_Object string)
197 register Lisp_Object val;
198 CHECK_STRING (string);
199 if (SCHARS (string))
201 if (STRING_MULTIBYTE (string))
202 XSETFASTINT (val, STRING_CHAR (SDATA (string)));
203 else
204 XSETFASTINT (val, SREF (string, 0));
206 else
207 XSETFASTINT (val, 0);
208 return val;
211 static Lisp_Object
212 buildmark (ptrdiff_t charpos, ptrdiff_t bytepos)
214 register Lisp_Object mark;
215 mark = Fmake_marker ();
216 set_marker_both (mark, Qnil, charpos, bytepos);
217 return mark;
220 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
221 doc: /* Return value of point, as an integer.
222 Beginning of buffer is position (point-min). */)
223 (void)
225 Lisp_Object temp;
226 XSETFASTINT (temp, PT);
227 return temp;
230 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
231 doc: /* Return value of point, as a marker object. */)
232 (void)
234 return buildmark (PT, PT_BYTE);
237 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
238 doc: /* Set point to POSITION, a number or marker.
239 Beginning of buffer is position (point-min), end is (point-max).
241 The return value is POSITION. */)
242 (register Lisp_Object position)
244 ptrdiff_t pos;
246 if (MARKERP (position)
247 && current_buffer == XMARKER (position)->buffer)
249 pos = marker_position (position);
250 if (pos < BEGV)
251 SET_PT_BOTH (BEGV, BEGV_BYTE);
252 else if (pos > ZV)
253 SET_PT_BOTH (ZV, ZV_BYTE);
254 else
255 SET_PT_BOTH (pos, marker_byte_position (position));
257 return position;
260 CHECK_NUMBER_COERCE_MARKER (position);
262 pos = clip_to_bounds (BEGV, XINT (position), ZV);
263 SET_PT (pos);
264 return position;
268 /* Return the start or end position of the region.
269 BEGINNINGP non-zero means return the start.
270 If there is no region active, signal an error. */
272 static Lisp_Object
273 region_limit (int beginningp)
275 Lisp_Object m;
277 if (!NILP (Vtransient_mark_mode)
278 && NILP (Vmark_even_if_inactive)
279 && NILP (BVAR (current_buffer, mark_active)))
280 xsignal0 (Qmark_inactive);
282 m = Fmarker_position (BVAR (current_buffer, mark));
283 if (NILP (m))
284 error ("The mark is not set now, so there is no region");
286 if ((PT < XFASTINT (m)) == (beginningp != 0))
287 m = make_number (PT);
288 return m;
291 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
292 doc: /* Return the integer value of point or mark, whichever is smaller. */)
293 (void)
295 return region_limit (1);
298 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
299 doc: /* Return the integer value of point or mark, whichever is larger. */)
300 (void)
302 return region_limit (0);
305 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
306 doc: /* Return this buffer's mark, as a marker object.
307 Watch out! Moving this marker changes the mark position.
308 If you set the marker not to point anywhere, the buffer will have no mark. */)
309 (void)
311 return BVAR (current_buffer, mark);
315 /* Find all the overlays in the current buffer that touch position POS.
316 Return the number found, and store them in a vector in VEC
317 of length LEN. */
319 static ptrdiff_t
320 overlays_around (EMACS_INT pos, Lisp_Object *vec, ptrdiff_t len)
322 Lisp_Object overlay, start, end;
323 struct Lisp_Overlay *tail;
324 ptrdiff_t startpos, endpos;
325 ptrdiff_t idx = 0;
327 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
329 XSETMISC (overlay, tail);
331 end = OVERLAY_END (overlay);
332 endpos = OVERLAY_POSITION (end);
333 if (endpos < pos)
334 break;
335 start = OVERLAY_START (overlay);
336 startpos = OVERLAY_POSITION (start);
337 if (startpos <= pos)
339 if (idx < len)
340 vec[idx] = overlay;
341 /* Keep counting overlays even if we can't return them all. */
342 idx++;
346 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
348 XSETMISC (overlay, tail);
350 start = OVERLAY_START (overlay);
351 startpos = OVERLAY_POSITION (start);
352 if (pos < startpos)
353 break;
354 end = OVERLAY_END (overlay);
355 endpos = OVERLAY_POSITION (end);
356 if (pos <= endpos)
358 if (idx < len)
359 vec[idx] = overlay;
360 idx++;
364 return idx;
367 /* Return the value of property PROP, in OBJECT at POSITION.
368 It's the value of PROP that a char inserted at POSITION would get.
369 OBJECT is optional and defaults to the current buffer.
370 If OBJECT is a buffer, then overlay properties are considered as well as
371 text properties.
372 If OBJECT is a window, then that window's buffer is used, but
373 window-specific overlays are considered only if they are associated
374 with OBJECT. */
375 Lisp_Object
376 get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object object)
378 CHECK_NUMBER_COERCE_MARKER (position);
380 if (NILP (object))
381 XSETBUFFER (object, current_buffer);
382 else if (WINDOWP (object))
383 object = XWINDOW (object)->buffer;
385 if (!BUFFERP (object))
386 /* pos-property only makes sense in buffers right now, since strings
387 have no overlays and no notion of insertion for which stickiness
388 could be obeyed. */
389 return Fget_text_property (position, prop, object);
390 else
392 EMACS_INT posn = XINT (position);
393 ptrdiff_t noverlays;
394 Lisp_Object *overlay_vec, tem;
395 struct buffer *obuf = current_buffer;
397 set_buffer_temp (XBUFFER (object));
399 /* First try with room for 40 overlays. */
400 noverlays = 40;
401 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
402 noverlays = overlays_around (posn, overlay_vec, noverlays);
404 /* If there are more than 40,
405 make enough space for all, and try again. */
406 if (noverlays > 40)
408 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
409 noverlays = overlays_around (posn, overlay_vec, noverlays);
411 noverlays = sort_overlays (overlay_vec, noverlays, NULL);
413 set_buffer_temp (obuf);
415 /* Now check the overlays in order of decreasing priority. */
416 while (--noverlays >= 0)
418 Lisp_Object ol = overlay_vec[noverlays];
419 tem = Foverlay_get (ol, prop);
420 if (!NILP (tem))
422 /* Check the overlay is indeed active at point. */
423 Lisp_Object start = OVERLAY_START (ol), finish = OVERLAY_END (ol);
424 if ((OVERLAY_POSITION (start) == posn
425 && XMARKER (start)->insertion_type == 1)
426 || (OVERLAY_POSITION (finish) == posn
427 && XMARKER (finish)->insertion_type == 0))
428 ; /* The overlay will not cover a char inserted at point. */
429 else
431 return tem;
436 { /* Now check the text properties. */
437 int stickiness = text_property_stickiness (prop, position, object);
438 if (stickiness > 0)
439 return Fget_text_property (position, prop, object);
440 else if (stickiness < 0
441 && XINT (position) > BUF_BEGV (XBUFFER (object)))
442 return Fget_text_property (make_number (XINT (position) - 1),
443 prop, object);
444 else
445 return Qnil;
450 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
451 the value of point is used instead. If BEG or END is null,
452 means don't store the beginning or end of the field.
454 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
455 results; they do not effect boundary behavior.
457 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
458 position of a field, then the beginning of the previous field is
459 returned instead of the beginning of POS's field (since the end of a
460 field is actually also the beginning of the next input field, this
461 behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
462 true case, if two fields are separated by a field with the special
463 value `boundary', and POS lies within it, then the two separated
464 fields are considered to be adjacent, and POS between them, when
465 finding the beginning and ending of the "merged" field.
467 Either BEG or END may be 0, in which case the corresponding value
468 is not stored. */
470 static void
471 find_field (Lisp_Object pos, Lisp_Object merge_at_boundary,
472 Lisp_Object beg_limit,
473 ptrdiff_t *beg, Lisp_Object end_limit, ptrdiff_t *end)
475 /* Fields right before and after the point. */
476 Lisp_Object before_field, after_field;
477 /* 1 if POS counts as the start of a field. */
478 int at_field_start = 0;
479 /* 1 if POS counts as the end of a field. */
480 int at_field_end = 0;
482 if (NILP (pos))
483 XSETFASTINT (pos, PT);
484 else
485 CHECK_NUMBER_COERCE_MARKER (pos);
487 after_field
488 = get_char_property_and_overlay (pos, Qfield, Qnil, NULL);
489 before_field
490 = (XFASTINT (pos) > BEGV
491 ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
492 Qfield, Qnil, NULL)
493 /* Using nil here would be a more obvious choice, but it would
494 fail when the buffer starts with a non-sticky field. */
495 : after_field);
497 /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil
498 and POS is at beginning of a field, which can also be interpreted
499 as the end of the previous field. Note that the case where if
500 MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
501 more natural one; then we avoid treating the beginning of a field
502 specially. */
503 if (NILP (merge_at_boundary))
505 Lisp_Object field = get_pos_property (pos, Qfield, Qnil);
506 if (!EQ (field, after_field))
507 at_field_end = 1;
508 if (!EQ (field, before_field))
509 at_field_start = 1;
510 if (NILP (field) && at_field_start && at_field_end)
511 /* If an inserted char would have a nil field while the surrounding
512 text is non-nil, we're probably not looking at a
513 zero-length field, but instead at a non-nil field that's
514 not intended for editing (such as comint's prompts). */
515 at_field_end = at_field_start = 0;
518 /* Note about special `boundary' fields:
520 Consider the case where the point (`.') is between the fields `x' and `y':
522 xxxx.yyyy
524 In this situation, if merge_at_boundary is true, we consider the
525 `x' and `y' fields as forming one big merged field, and so the end
526 of the field is the end of `y'.
528 However, if `x' and `y' are separated by a special `boundary' field
529 (a field with a `field' char-property of 'boundary), then we ignore
530 this special field when merging adjacent fields. Here's the same
531 situation, but with a `boundary' field between the `x' and `y' fields:
533 xxx.BBBByyyy
535 Here, if point is at the end of `x', the beginning of `y', or
536 anywhere in-between (within the `boundary' field), we merge all
537 three fields and consider the beginning as being the beginning of
538 the `x' field, and the end as being the end of the `y' field. */
540 if (beg)
542 if (at_field_start)
543 /* POS is at the edge of a field, and we should consider it as
544 the beginning of the following field. */
545 *beg = XFASTINT (pos);
546 else
547 /* Find the previous field boundary. */
549 Lisp_Object p = pos;
550 if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary))
551 /* Skip a `boundary' field. */
552 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
553 beg_limit);
555 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
556 beg_limit);
557 *beg = NILP (p) ? BEGV : XFASTINT (p);
561 if (end)
563 if (at_field_end)
564 /* POS is at the edge of a field, and we should consider it as
565 the end of the previous field. */
566 *end = XFASTINT (pos);
567 else
568 /* Find the next field boundary. */
570 if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary))
571 /* Skip a `boundary' field. */
572 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
573 end_limit);
575 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
576 end_limit);
577 *end = NILP (pos) ? ZV : XFASTINT (pos);
583 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
584 doc: /* Delete the field surrounding POS.
585 A field is a region of text with the same `field' property.
586 If POS is nil, the value of point is used for POS. */)
587 (Lisp_Object pos)
589 ptrdiff_t beg, end;
590 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
591 if (beg != end)
592 del_range (beg, end);
593 return Qnil;
596 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
597 doc: /* Return the contents of the field surrounding POS as a string.
598 A field is a region of text with the same `field' property.
599 If POS is nil, the value of point is used for POS. */)
600 (Lisp_Object pos)
602 ptrdiff_t beg, end;
603 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
604 return make_buffer_string (beg, end, 1);
607 DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
608 doc: /* Return the contents of the field around POS, without text properties.
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, 0);
618 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
619 doc: /* Return the beginning of the field surrounding POS.
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 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
623 field, then the beginning of the *previous* field is returned.
624 If LIMIT is non-nil, it is a buffer position; if the beginning of the field
625 is before LIMIT, then LIMIT will be returned instead. */)
626 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
628 ptrdiff_t beg;
629 find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
630 return make_number (beg);
633 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
634 doc: /* Return the end of the field surrounding POS.
635 A field is a region of text with the same `field' property.
636 If POS is nil, the value of point is used for POS.
637 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
638 then the end of the *following* field is returned.
639 If LIMIT is non-nil, it is a buffer position; if the end of the field
640 is after LIMIT, then LIMIT will be returned instead. */)
641 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
643 ptrdiff_t end;
644 find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
645 return make_number (end);
648 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
649 doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
650 A field is a region of text with the same `field' property.
652 If NEW-POS is nil, then use the current point instead, and move point
653 to the resulting constrained position, in addition to returning that
654 position.
656 If OLD-POS is at the boundary of two fields, then the allowable
657 positions for NEW-POS depends on the value of the optional argument
658 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
659 constrained to the field that has the same `field' char-property
660 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
661 is non-nil, NEW-POS is constrained to the union of the two adjacent
662 fields. Additionally, if two fields are separated by another field with
663 the special value `boundary', then any point within this special field is
664 also considered to be `on the boundary'.
666 If the optional argument ONLY-IN-LINE is non-nil and constraining
667 NEW-POS would move it to a different line, NEW-POS is returned
668 unconstrained. This useful for commands that move by line, like
669 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
670 only in the case where they can still move to the right line.
672 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
673 a non-nil property of that name, then any field boundaries are ignored.
675 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */)
676 (Lisp_Object new_pos, Lisp_Object old_pos, Lisp_Object escape_from_edge, Lisp_Object only_in_line, Lisp_Object inhibit_capture_property)
678 /* If non-zero, then the original point, before re-positioning. */
679 ptrdiff_t orig_point = 0;
680 int fwd;
681 Lisp_Object prev_old, prev_new;
683 if (NILP (new_pos))
684 /* Use the current point, and afterwards, set it. */
686 orig_point = PT;
687 XSETFASTINT (new_pos, PT);
690 CHECK_NUMBER_COERCE_MARKER (new_pos);
691 CHECK_NUMBER_COERCE_MARKER (old_pos);
693 fwd = (XINT (new_pos) > XINT (old_pos));
695 prev_old = make_number (XINT (old_pos) - 1);
696 prev_new = make_number (XINT (new_pos) - 1);
698 if (NILP (Vinhibit_field_text_motion)
699 && !EQ (new_pos, old_pos)
700 && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
701 || !NILP (Fget_char_property (old_pos, Qfield, Qnil))
702 /* To recognize field boundaries, we must also look at the
703 previous positions; we could use `get_pos_property'
704 instead, but in itself that would fail inside non-sticky
705 fields (like comint prompts). */
706 || (XFASTINT (new_pos) > BEGV
707 && !NILP (Fget_char_property (prev_new, Qfield, Qnil)))
708 || (XFASTINT (old_pos) > BEGV
709 && !NILP (Fget_char_property (prev_old, Qfield, Qnil))))
710 && (NILP (inhibit_capture_property)
711 /* Field boundaries are again a problem; but now we must
712 decide the case exactly, so we need to call
713 `get_pos_property' as well. */
714 || (NILP (get_pos_property (old_pos, inhibit_capture_property, Qnil))
715 && (XFASTINT (old_pos) <= BEGV
716 || NILP (Fget_char_property (old_pos, inhibit_capture_property, Qnil))
717 || NILP (Fget_char_property (prev_old, inhibit_capture_property, Qnil))))))
718 /* It is possible that NEW_POS is not within the same field as
719 OLD_POS; try to move NEW_POS so that it is. */
721 ptrdiff_t shortage;
722 Lisp_Object field_bound;
724 if (fwd)
725 field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
726 else
727 field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
729 if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
730 other side of NEW_POS, which would mean that NEW_POS is
731 already acceptable, and it's not necessary to constrain it
732 to FIELD_BOUND. */
733 ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd)
734 /* NEW_POS should be constrained, but only if either
735 ONLY_IN_LINE is nil (in which case any constraint is OK),
736 or NEW_POS and FIELD_BOUND are on the same line (in which
737 case the constraint is OK even if ONLY_IN_LINE is non-nil). */
738 && (NILP (only_in_line)
739 /* This is the ONLY_IN_LINE case, check that NEW_POS and
740 FIELD_BOUND are on the same line by seeing whether
741 there's an intervening newline or not. */
742 || (scan_buffer ('\n',
743 XFASTINT (new_pos), XFASTINT (field_bound),
744 fwd ? -1 : 1, &shortage, 1),
745 shortage != 0)))
746 /* Constrain NEW_POS to FIELD_BOUND. */
747 new_pos = field_bound;
749 if (orig_point && XFASTINT (new_pos) != orig_point)
750 /* The NEW_POS argument was originally nil, so automatically set PT. */
751 SET_PT (XFASTINT (new_pos));
754 return new_pos;
758 DEFUN ("line-beginning-position",
759 Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
760 doc: /* Return the character position of the first character on the current line.
761 With argument N not nil or 1, move forward N - 1 lines first.
762 If scan reaches end of buffer, return that position.
764 The returned position is of the first character in the logical order,
765 i.e. the one that has the smallest character position.
767 This function constrains the returned position to the current field
768 unless that would be on a different line than the original,
769 unconstrained result. If N is nil or 1, and a front-sticky field
770 starts at point, the scan stops as soon as it starts. To ignore field
771 boundaries bind `inhibit-field-text-motion' to t.
773 This function does not move point. */)
774 (Lisp_Object n)
776 ptrdiff_t orig, orig_byte, end;
777 ptrdiff_t count = SPECPDL_INDEX ();
778 specbind (Qinhibit_point_motion_hooks, Qt);
780 if (NILP (n))
781 XSETFASTINT (n, 1);
782 else
783 CHECK_NUMBER (n);
785 orig = PT;
786 orig_byte = PT_BYTE;
787 Fforward_line (make_number (XINT (n) - 1));
788 end = PT;
790 SET_PT_BOTH (orig, orig_byte);
792 unbind_to (count, Qnil);
794 /* Return END constrained to the current input field. */
795 return Fconstrain_to_field (make_number (end), make_number (orig),
796 XINT (n) != 1 ? Qt : Qnil,
797 Qt, Qnil);
800 DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0,
801 doc: /* Return the character position of the last character on the current line.
802 With argument N not nil or 1, move forward N - 1 lines first.
803 If scan reaches end of buffer, return that position.
805 The returned position is of the last character in the logical order,
806 i.e. the character whose buffer position is the largest one.
808 This function constrains the returned position to the current field
809 unless that would be on a different line than the original,
810 unconstrained result. If N is nil or 1, and a rear-sticky field ends
811 at point, the scan stops as soon as it starts. To ignore field
812 boundaries bind `inhibit-field-text-motion' to t.
814 This function does not move point. */)
815 (Lisp_Object n)
817 ptrdiff_t clipped_n;
818 ptrdiff_t end_pos;
819 ptrdiff_t orig = PT;
821 if (NILP (n))
822 XSETFASTINT (n, 1);
823 else
824 CHECK_NUMBER (n);
826 clipped_n = clip_to_bounds (PTRDIFF_MIN + 1, XINT (n), PTRDIFF_MAX);
827 end_pos = find_before_next_newline (orig, 0, clipped_n - (clipped_n <= 0));
829 /* Return END_POS constrained to the current input field. */
830 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
831 Qnil, Qt, Qnil);
835 Lisp_Object
836 save_excursion_save (void)
838 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
839 == current_buffer);
841 return Fcons (Fpoint_marker (),
842 Fcons (Fcopy_marker (BVAR (current_buffer, mark), Qnil),
843 Fcons (visible ? Qt : Qnil,
844 Fcons (BVAR (current_buffer, mark_active),
845 selected_window))));
848 Lisp_Object
849 save_excursion_restore (Lisp_Object info)
851 Lisp_Object tem, tem1, omark, nmark;
852 struct gcpro gcpro1, gcpro2, gcpro3;
853 int visible_p;
855 tem = Fmarker_buffer (XCAR (info));
856 /* If buffer being returned to is now deleted, avoid error */
857 /* Otherwise could get error here while unwinding to top level
858 and crash */
859 /* In that case, Fmarker_buffer returns nil now. */
860 if (NILP (tem))
861 return Qnil;
863 omark = nmark = Qnil;
864 GCPRO3 (info, omark, nmark);
866 Fset_buffer (tem);
868 /* Point marker. */
869 tem = XCAR (info);
870 Fgoto_char (tem);
871 unchain_marker (XMARKER (tem));
873 /* Mark marker. */
874 info = XCDR (info);
875 tem = XCAR (info);
876 omark = Fmarker_position (BVAR (current_buffer, mark));
877 Fset_marker (BVAR (current_buffer, mark), tem, Fcurrent_buffer ());
878 nmark = Fmarker_position (tem);
879 unchain_marker (XMARKER (tem));
881 /* visible */
882 info = XCDR (info);
883 visible_p = !NILP (XCAR (info));
885 #if 0 /* We used to make the current buffer visible in the selected window
886 if that was true previously. That avoids some anomalies.
887 But it creates others, and it wasn't documented, and it is simpler
888 and cleaner never to alter the window/buffer connections. */
889 tem1 = Fcar (tem);
890 if (!NILP (tem1)
891 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
892 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
893 #endif /* 0 */
895 /* Mark active */
896 info = XCDR (info);
897 tem = XCAR (info);
898 tem1 = BVAR (current_buffer, mark_active);
899 BVAR (current_buffer, mark_active) = tem;
901 /* If mark is active now, and either was not active
902 or was at a different place, run the activate hook. */
903 if (! NILP (tem))
905 if (! EQ (omark, nmark))
907 tem = intern ("activate-mark-hook");
908 Frun_hooks (1, &tem);
911 /* If mark has ceased to be active, run deactivate hook. */
912 else if (! NILP (tem1))
914 tem = intern ("deactivate-mark-hook");
915 Frun_hooks (1, &tem);
918 /* If buffer was visible in a window, and a different window was
919 selected, and the old selected window is still showing this
920 buffer, restore point in that window. */
921 tem = XCDR (info);
922 if (visible_p
923 && !EQ (tem, selected_window)
924 && (tem1 = XWINDOW (tem)->buffer,
925 (/* Window is live... */
926 BUFFERP (tem1)
927 /* ...and it shows the current buffer. */
928 && XBUFFER (tem1) == current_buffer)))
929 Fset_window_point (tem, make_number (PT));
931 UNGCPRO;
932 return Qnil;
935 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
936 doc: /* Save point, mark, and current buffer; execute BODY; restore those things.
937 Executes BODY just like `progn'.
938 The values of point, mark and the current buffer are restored
939 even in case of abnormal exit (throw or error).
940 The state of activation of the mark is also restored.
942 This construct does not save `deactivate-mark', and therefore
943 functions that change the buffer will still cause deactivation
944 of the mark at the end of the command. To prevent that, bind
945 `deactivate-mark' with `let'.
947 If you only want to save the current buffer but not point nor mark,
948 then just use `save-current-buffer', or even `with-current-buffer'.
950 usage: (save-excursion &rest BODY) */)
951 (Lisp_Object args)
953 register Lisp_Object val;
954 ptrdiff_t count = SPECPDL_INDEX ();
956 record_unwind_protect (save_excursion_restore, save_excursion_save ());
958 val = Fprogn (args);
959 return unbind_to (count, val);
962 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
963 doc: /* Save the current buffer; execute BODY; restore the current buffer.
964 Executes BODY just like `progn'.
965 usage: (save-current-buffer &rest BODY) */)
966 (Lisp_Object args)
968 Lisp_Object val;
969 ptrdiff_t count = SPECPDL_INDEX ();
971 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
973 val = Fprogn (args);
974 return unbind_to (count, val);
977 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
978 doc: /* Return the number of characters in the current buffer.
979 If BUFFER, return the number of characters in that buffer instead. */)
980 (Lisp_Object buffer)
982 if (NILP (buffer))
983 return make_number (Z - BEG);
984 else
986 CHECK_BUFFER (buffer);
987 return make_number (BUF_Z (XBUFFER (buffer))
988 - BUF_BEG (XBUFFER (buffer)));
992 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
993 doc: /* Return the minimum permissible value of point in the current buffer.
994 This is 1, unless narrowing (a buffer restriction) is in effect. */)
995 (void)
997 Lisp_Object temp;
998 XSETFASTINT (temp, BEGV);
999 return temp;
1002 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
1003 doc: /* Return a marker to the minimum permissible value of point in this buffer.
1004 This is the beginning, unless narrowing (a buffer restriction) is in effect. */)
1005 (void)
1007 return buildmark (BEGV, BEGV_BYTE);
1010 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
1011 doc: /* Return the maximum permissible value of point in the current buffer.
1012 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1013 is in effect, in which case it is less. */)
1014 (void)
1016 Lisp_Object temp;
1017 XSETFASTINT (temp, ZV);
1018 return temp;
1021 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
1022 doc: /* Return a marker to the maximum permissible value of point in this buffer.
1023 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1024 is in effect, in which case it is less. */)
1025 (void)
1027 return buildmark (ZV, ZV_BYTE);
1030 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
1031 doc: /* Return the position of the gap, in the current buffer.
1032 See also `gap-size'. */)
1033 (void)
1035 Lisp_Object temp;
1036 XSETFASTINT (temp, GPT);
1037 return temp;
1040 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
1041 doc: /* Return the size of the current buffer's gap.
1042 See also `gap-position'. */)
1043 (void)
1045 Lisp_Object temp;
1046 XSETFASTINT (temp, GAP_SIZE);
1047 return temp;
1050 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
1051 doc: /* Return the byte position for character position POSITION.
1052 If POSITION is out of range, the value is nil. */)
1053 (Lisp_Object position)
1055 CHECK_NUMBER_COERCE_MARKER (position);
1056 if (XINT (position) < BEG || XINT (position) > Z)
1057 return Qnil;
1058 return make_number (CHAR_TO_BYTE (XINT (position)));
1061 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
1062 doc: /* Return the character position for byte position BYTEPOS.
1063 If BYTEPOS is out of range, the value is nil. */)
1064 (Lisp_Object bytepos)
1066 CHECK_NUMBER (bytepos);
1067 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
1068 return Qnil;
1069 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
1072 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
1073 doc: /* Return the character following point, as a number.
1074 At the end of the buffer or accessible region, return 0. */)
1075 (void)
1077 Lisp_Object temp;
1078 if (PT >= ZV)
1079 XSETFASTINT (temp, 0);
1080 else
1081 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
1082 return temp;
1085 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
1086 doc: /* Return the character preceding point, as a number.
1087 At the beginning of the buffer or accessible region, return 0. */)
1088 (void)
1090 Lisp_Object temp;
1091 if (PT <= BEGV)
1092 XSETFASTINT (temp, 0);
1093 else if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1095 ptrdiff_t pos = PT_BYTE;
1096 DEC_POS (pos);
1097 XSETFASTINT (temp, FETCH_CHAR (pos));
1099 else
1100 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
1101 return temp;
1104 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
1105 doc: /* Return t if point is at the beginning of the buffer.
1106 If the buffer is narrowed, this means the beginning of the narrowed part. */)
1107 (void)
1109 if (PT == BEGV)
1110 return Qt;
1111 return Qnil;
1114 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
1115 doc: /* Return t if point is at the end of the buffer.
1116 If the buffer is narrowed, this means the end of the narrowed part. */)
1117 (void)
1119 if (PT == ZV)
1120 return Qt;
1121 return Qnil;
1124 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
1125 doc: /* Return t if point is at the beginning of a line. */)
1126 (void)
1128 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
1129 return Qt;
1130 return Qnil;
1133 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
1134 doc: /* Return t if point is at the end of a line.
1135 `End of a line' includes point being at the end of the buffer. */)
1136 (void)
1138 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
1139 return Qt;
1140 return Qnil;
1143 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
1144 doc: /* Return character in current buffer at position POS.
1145 POS is an integer or a marker and defaults to point.
1146 If POS is out of range, the value is nil. */)
1147 (Lisp_Object pos)
1149 register ptrdiff_t pos_byte;
1151 if (NILP (pos))
1153 pos_byte = PT_BYTE;
1154 XSETFASTINT (pos, PT);
1157 if (MARKERP (pos))
1159 pos_byte = marker_byte_position (pos);
1160 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1161 return Qnil;
1163 else
1165 CHECK_NUMBER_COERCE_MARKER (pos);
1166 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1167 return Qnil;
1169 pos_byte = CHAR_TO_BYTE (XINT (pos));
1172 return make_number (FETCH_CHAR (pos_byte));
1175 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1176 doc: /* Return character in current buffer preceding position POS.
1177 POS is an integer or a marker and defaults to point.
1178 If POS is out of range, the value is nil. */)
1179 (Lisp_Object pos)
1181 register Lisp_Object val;
1182 register ptrdiff_t pos_byte;
1184 if (NILP (pos))
1186 pos_byte = PT_BYTE;
1187 XSETFASTINT (pos, PT);
1190 if (MARKERP (pos))
1192 pos_byte = marker_byte_position (pos);
1194 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1195 return Qnil;
1197 else
1199 CHECK_NUMBER_COERCE_MARKER (pos);
1201 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1202 return Qnil;
1204 pos_byte = CHAR_TO_BYTE (XINT (pos));
1207 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1209 DEC_POS (pos_byte);
1210 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1212 else
1214 pos_byte--;
1215 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1217 return val;
1220 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1221 doc: /* Return the name under which the user logged in, as a string.
1222 This is based on the effective uid, not the real uid.
1223 Also, if the environment variables LOGNAME or USER are set,
1224 that determines the value of this function.
1226 If optional argument UID is an integer or a float, return the login name
1227 of the user with that uid, or nil if there is no such user. */)
1228 (Lisp_Object uid)
1230 struct passwd *pw;
1231 uid_t id;
1233 /* Set up the user name info if we didn't do it before.
1234 (That can happen if Emacs is dumpable
1235 but you decide to run `temacs -l loadup' and not dump. */
1236 if (INTEGERP (Vuser_login_name))
1237 init_editfns ();
1239 if (NILP (uid))
1240 return Vuser_login_name;
1242 CONS_TO_INTEGER (uid, uid_t, id);
1243 BLOCK_INPUT;
1244 pw = getpwuid (id);
1245 UNBLOCK_INPUT;
1246 return (pw ? build_string (pw->pw_name) : Qnil);
1249 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1250 0, 0, 0,
1251 doc: /* Return the name of the user's real uid, as a string.
1252 This ignores the environment variables LOGNAME and USER, so it differs from
1253 `user-login-name' when running under `su'. */)
1254 (void)
1256 /* Set up the user name info if we didn't do it before.
1257 (That can happen if Emacs is dumpable
1258 but you decide to run `temacs -l loadup' and not dump. */
1259 if (INTEGERP (Vuser_login_name))
1260 init_editfns ();
1261 return Vuser_real_login_name;
1264 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1265 doc: /* Return the effective uid of Emacs.
1266 Value is an integer or a float, depending on the value. */)
1267 (void)
1269 uid_t euid = geteuid ();
1270 return make_fixnum_or_float (euid);
1273 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1274 doc: /* Return the real uid of Emacs.
1275 Value is an integer or a float, depending on the value. */)
1276 (void)
1278 uid_t uid = getuid ();
1279 return make_fixnum_or_float (uid);
1282 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1283 doc: /* Return the full name of the user logged in, as a string.
1284 If the full name corresponding to Emacs's userid is not known,
1285 return "unknown".
1287 If optional argument UID is an integer or float, return the full name
1288 of the user with that uid, or nil if there is no such user.
1289 If UID is a string, return the full name of the user with that login
1290 name, or nil if there is no such user. */)
1291 (Lisp_Object uid)
1293 struct passwd *pw;
1294 register char *p, *q;
1295 Lisp_Object full;
1297 if (NILP (uid))
1298 return Vuser_full_name;
1299 else if (NUMBERP (uid))
1301 uid_t u;
1302 CONS_TO_INTEGER (uid, uid_t, u);
1303 BLOCK_INPUT;
1304 pw = getpwuid (u);
1305 UNBLOCK_INPUT;
1307 else if (STRINGP (uid))
1309 BLOCK_INPUT;
1310 pw = getpwnam (SSDATA (uid));
1311 UNBLOCK_INPUT;
1313 else
1314 error ("Invalid UID specification");
1316 if (!pw)
1317 return Qnil;
1319 p = USER_FULL_NAME;
1320 /* Chop off everything after the first comma. */
1321 q = strchr (p, ',');
1322 full = make_string (p, q ? q - p : strlen (p));
1324 #ifdef AMPERSAND_FULL_NAME
1325 p = SSDATA (full);
1326 q = strchr (p, '&');
1327 /* Substitute the login name for the &, upcasing the first character. */
1328 if (q)
1330 register char *r;
1331 Lisp_Object login;
1333 login = Fuser_login_name (make_number (pw->pw_uid));
1334 r = (char *) alloca (strlen (p) + SCHARS (login) + 1);
1335 memcpy (r, p, q - p);
1336 r[q - p] = 0;
1337 strcat (r, SSDATA (login));
1338 r[q - p] = upcase ((unsigned char) r[q - p]);
1339 strcat (r, q + 1);
1340 full = build_string (r);
1342 #endif /* AMPERSAND_FULL_NAME */
1344 return full;
1347 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1348 doc: /* Return the host name of the machine you are running on, as a string. */)
1349 (void)
1351 return Vsystem_name;
1354 const char *
1355 get_system_name (void)
1357 if (STRINGP (Vsystem_name))
1358 return SSDATA (Vsystem_name);
1359 else
1360 return "";
1363 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1364 doc: /* Return the process ID of Emacs, as a number. */)
1365 (void)
1367 pid_t pid = getpid ();
1368 return make_fixnum_or_float (pid);
1373 #ifndef TIME_T_MIN
1374 # define TIME_T_MIN TYPE_MINIMUM (time_t)
1375 #endif
1376 #ifndef TIME_T_MAX
1377 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
1378 #endif
1380 /* Report that a time value is out of range for Emacs. */
1381 static void
1382 time_overflow (void)
1384 error ("Specified time is not representable");
1387 /* Return the upper part of the time T (everything but the bottom 16 bits),
1388 making sure that it is representable. */
1389 static EMACS_INT
1390 hi_time (time_t t)
1392 time_t hi = t >> 16;
1394 /* Check for overflow, helping the compiler for common cases where
1395 no runtime check is needed, and taking care not to convert
1396 negative numbers to unsigned before comparing them. */
1397 if (! ((! TYPE_SIGNED (time_t)
1398 || MOST_NEGATIVE_FIXNUM <= TIME_T_MIN >> 16
1399 || MOST_NEGATIVE_FIXNUM <= hi)
1400 && (TIME_T_MAX >> 16 <= MOST_POSITIVE_FIXNUM
1401 || hi <= MOST_POSITIVE_FIXNUM)))
1402 time_overflow ();
1404 return hi;
1407 /* Return the bottom 16 bits of the time T. */
1408 static int
1409 lo_time (time_t t)
1411 return t & ((1 << 16) - 1);
1414 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1415 doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
1416 The time is returned as a list of three integers. The first has the
1417 most significant 16 bits of the seconds, while the second has the
1418 least significant 16 bits. The third integer gives the microsecond
1419 count.
1421 The microsecond count is zero on systems that do not provide
1422 resolution finer than a second. */)
1423 (void)
1425 EMACS_TIME t;
1427 EMACS_GET_TIME (t);
1428 return list3 (make_number (hi_time (EMACS_SECS (t))),
1429 make_number (lo_time (EMACS_SECS (t))),
1430 make_number (EMACS_USECS (t)));
1433 DEFUN ("get-internal-run-time", Fget_internal_run_time, Sget_internal_run_time,
1434 0, 0, 0,
1435 doc: /* Return the current run time used by Emacs.
1436 The time is returned as a list of three integers. The first has the
1437 most significant 16 bits of the seconds, while the second has the
1438 least significant 16 bits. The third integer gives the microsecond
1439 count.
1441 On systems that can't determine the run time, `get-internal-run-time'
1442 does the same thing as `current-time'. The microsecond count is zero
1443 on systems that do not provide resolution finer than a second. */)
1444 (void)
1446 #ifdef HAVE_GETRUSAGE
1447 struct rusage usage;
1448 time_t secs;
1449 int usecs;
1451 if (getrusage (RUSAGE_SELF, &usage) < 0)
1452 /* This shouldn't happen. What action is appropriate? */
1453 xsignal0 (Qerror);
1455 /* Sum up user time and system time. */
1456 secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
1457 usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
1458 if (usecs >= 1000000)
1460 usecs -= 1000000;
1461 secs++;
1464 return list3 (make_number (hi_time (secs)),
1465 make_number (lo_time (secs)),
1466 make_number (usecs));
1467 #else /* ! HAVE_GETRUSAGE */
1468 #ifdef WINDOWSNT
1469 return w32_get_internal_run_time ();
1470 #else /* ! WINDOWSNT */
1471 return Fcurrent_time ();
1472 #endif /* WINDOWSNT */
1473 #endif /* HAVE_GETRUSAGE */
1477 /* Make a Lisp list that represents the time T. */
1478 Lisp_Object
1479 make_time (time_t t)
1481 return list2 (make_number (hi_time (t)),
1482 make_number (lo_time (t)));
1485 /* Decode a Lisp list SPECIFIED_TIME that represents a time.
1486 If SPECIFIED_TIME is nil, use the current time.
1487 Set *RESULT to seconds since the Epoch.
1488 If USEC is not null, set *USEC to the microseconds component.
1489 Return nonzero if successful. */
1491 lisp_time_argument (Lisp_Object specified_time, time_t *result, int *usec)
1493 if (NILP (specified_time))
1495 if (usec)
1497 EMACS_TIME t;
1499 EMACS_GET_TIME (t);
1500 *usec = EMACS_USECS (t);
1501 *result = EMACS_SECS (t);
1502 return 1;
1504 else
1505 return time (result) != -1;
1507 else
1509 Lisp_Object high, low;
1510 EMACS_INT hi;
1511 high = Fcar (specified_time);
1512 CHECK_NUMBER (high);
1513 low = Fcdr (specified_time);
1514 if (CONSP (low))
1516 if (usec)
1518 Lisp_Object usec_l = Fcdr (low);
1519 if (CONSP (usec_l))
1520 usec_l = Fcar (usec_l);
1521 if (NILP (usec_l))
1522 *usec = 0;
1523 else
1525 CHECK_NUMBER (usec_l);
1526 if (! (0 <= XINT (usec_l) && XINT (usec_l) < 1000000))
1527 return 0;
1528 *usec = XINT (usec_l);
1531 low = Fcar (low);
1533 else if (usec)
1534 *usec = 0;
1535 CHECK_NUMBER (low);
1536 hi = XINT (high);
1538 /* Check for overflow, helping the compiler for common cases
1539 where no runtime check is needed, and taking care not to
1540 convert negative numbers to unsigned before comparing them. */
1541 if (! ((TYPE_SIGNED (time_t)
1542 ? (TIME_T_MIN >> 16 <= MOST_NEGATIVE_FIXNUM
1543 || TIME_T_MIN >> 16 <= hi)
1544 : 0 <= hi)
1545 && (MOST_POSITIVE_FIXNUM <= TIME_T_MAX >> 16
1546 || hi <= TIME_T_MAX >> 16)))
1547 return 0;
1549 *result = (hi << 16) + (XINT (low) & 0xffff);
1550 return 1;
1554 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1555 doc: /* Return the current time, as a float number of seconds since the epoch.
1556 If SPECIFIED-TIME is given, it is the time to convert to float
1557 instead of the current time. The argument should have the form
1558 (HIGH LOW) or (HIGH LOW USEC). Thus, you can use times obtained from
1559 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1560 have the form (HIGH . LOW), but this is considered obsolete.
1562 WARNING: Since the result is floating point, it may not be exact.
1563 If precise time stamps are required, use either `current-time',
1564 or (if you need time as a string) `format-time-string'. */)
1565 (Lisp_Object specified_time)
1567 time_t sec;
1568 int usec;
1570 if (! lisp_time_argument (specified_time, &sec, &usec))
1571 error ("Invalid time specification");
1573 return make_float ((sec * 1e6 + usec) / 1e6);
1576 /* Write information into buffer S of size MAXSIZE, according to the
1577 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1578 Default to Universal Time if UT is nonzero, local time otherwise.
1579 Use NS as the number of nanoseconds in the %N directive.
1580 Return the number of bytes written, not including the terminating
1581 '\0'. If S is NULL, nothing will be written anywhere; so to
1582 determine how many bytes would be written, use NULL for S and
1583 ((size_t) -1) for MAXSIZE.
1585 This function behaves like nstrftime, except it allows null
1586 bytes in FORMAT and it does not support nanoseconds. */
1587 static size_t
1588 emacs_nmemftime (char *s, size_t maxsize, const char *format,
1589 size_t format_len, const struct tm *tp, int ut, int ns)
1591 size_t total = 0;
1593 /* Loop through all the null-terminated strings in the format
1594 argument. Normally there's just one null-terminated string, but
1595 there can be arbitrarily many, concatenated together, if the
1596 format contains '\0' bytes. nstrftime stops at the first
1597 '\0' byte so we must invoke it separately for each such string. */
1598 for (;;)
1600 size_t len;
1601 size_t result;
1603 if (s)
1604 s[0] = '\1';
1606 result = nstrftime (s, maxsize, format, tp, ut, ns);
1608 if (s)
1610 if (result == 0 && s[0] != '\0')
1611 return 0;
1612 s += result + 1;
1615 maxsize -= result + 1;
1616 total += result;
1617 len = strlen (format);
1618 if (len == format_len)
1619 return total;
1620 total++;
1621 format += len + 1;
1622 format_len -= len + 1;
1626 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1627 doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted.
1628 TIME is specified as (HIGH LOW . IGNORED), as returned by
1629 `current-time' or `file-attributes'. The obsolete form (HIGH . LOW)
1630 is also still accepted.
1631 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME
1632 as Universal Time; nil means describe TIME in the local time zone.
1633 The value is a copy of FORMAT-STRING, but with certain constructs replaced
1634 by text that describes the specified date and time in TIME:
1636 %Y is the year, %y within the century, %C the century.
1637 %G is the year corresponding to the ISO week, %g within the century.
1638 %m is the numeric month.
1639 %b and %h are the locale's abbreviated month name, %B the full name.
1640 %d is the day of the month, zero-padded, %e is blank-padded.
1641 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
1642 %a is the locale's abbreviated name of the day of week, %A the full name.
1643 %U is the week number starting on Sunday, %W starting on Monday,
1644 %V according to ISO 8601.
1645 %j is the day of the year.
1647 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
1648 only blank-padded, %l is like %I blank-padded.
1649 %p is the locale's equivalent of either AM or PM.
1650 %M is the minute.
1651 %S is the second.
1652 %N is the nanosecond, %6N the microsecond, %3N the millisecond, etc.
1653 %Z is the time zone name, %z is the numeric form.
1654 %s is the number of seconds since 1970-01-01 00:00:00 +0000.
1656 %c is the locale's date and time format.
1657 %x is the locale's "preferred" date format.
1658 %D is like "%m/%d/%y".
1660 %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p".
1661 %X is the locale's "preferred" time format.
1663 Finally, %n is a newline, %t is a tab, %% is a literal %.
1665 Certain flags and modifiers are available with some format controls.
1666 The flags are `_', `-', `^' and `#'. For certain characters X,
1667 %_X is like %X, but padded with blanks; %-X is like %X,
1668 but without padding. %^X is like %X, but with all textual
1669 characters up-cased; %#X is like %X, but with letter-case of
1670 all textual characters reversed.
1671 %NX (where N stands for an integer) is like %X,
1672 but takes up at least N (a number) positions.
1673 The modifiers are `E' and `O'. For certain characters X,
1674 %EX is a locale's alternative version of %X;
1675 %OX is like %X, but uses the locale's number symbols.
1677 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".
1679 usage: (format-time-string FORMAT-STRING &optional TIME UNIVERSAL) */)
1680 (Lisp_Object format_string, Lisp_Object timeval, Lisp_Object universal)
1682 time_t t;
1683 struct tm tm;
1685 CHECK_STRING (format_string);
1686 format_string = code_convert_string_norecord (format_string,
1687 Vlocale_coding_system, 1);
1688 return format_time_string (SSDATA (format_string), SBYTES (format_string),
1689 timeval, ! NILP (universal), &t, &tm);
1692 static Lisp_Object
1693 format_time_string (char const *format, ptrdiff_t formatlen,
1694 Lisp_Object timeval, int ut, time_t *tval, struct tm *tmp)
1696 char buffer[4000];
1697 char *buf = buffer;
1698 ptrdiff_t size = sizeof buffer;
1699 size_t len;
1700 Lisp_Object bufstring;
1701 int usec;
1702 int ns;
1703 struct tm *tm;
1704 USE_SAFE_ALLOCA;
1706 if (! lisp_time_argument (timeval, tval, &usec))
1707 error ("Invalid time specification");
1708 ns = usec * 1000;
1710 while (1)
1712 BLOCK_INPUT;
1714 synchronize_system_time_locale ();
1716 tm = ut ? gmtime (tval) : localtime (tval);
1717 if (! tm)
1719 UNBLOCK_INPUT;
1720 time_overflow ();
1722 *tmp = *tm;
1724 buf[0] = '\1';
1725 len = emacs_nmemftime (buf, size, format, formatlen, tm, ut, ns);
1726 if ((0 < len && len < size) || (len == 0 && buf[0] == '\0'))
1727 break;
1729 /* Buffer was too small, so make it bigger and try again. */
1730 len = emacs_nmemftime (NULL, SIZE_MAX, format, formatlen, tm, ut, ns);
1731 UNBLOCK_INPUT;
1732 if (STRING_BYTES_BOUND <= len)
1733 string_overflow ();
1734 size = len + 1;
1735 SAFE_ALLOCA (buf, char *, size);
1738 UNBLOCK_INPUT;
1739 bufstring = make_unibyte_string (buf, len);
1740 SAFE_FREE ();
1741 return code_convert_string_norecord (bufstring, Vlocale_coding_system, 0);
1744 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1745 doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
1746 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED),
1747 as from `current-time' and `file-attributes', or nil to use the
1748 current time. The obsolete form (HIGH . LOW) is also still accepted.
1749 The list has the following nine members: SEC is an integer between 0
1750 and 60; SEC is 60 for a leap second, which only some operating systems
1751 support. MINUTE is an integer between 0 and 59. HOUR is an integer
1752 between 0 and 23. DAY is an integer between 1 and 31. MONTH is an
1753 integer between 1 and 12. YEAR is an integer indicating the
1754 four-digit year. DOW is the day of week, an integer between 0 and 6,
1755 where 0 is Sunday. DST is t if daylight saving time is in effect,
1756 otherwise nil. ZONE is an integer indicating the number of seconds
1757 east of Greenwich. (Note that Common Lisp has different meanings for
1758 DOW and ZONE.) */)
1759 (Lisp_Object specified_time)
1761 time_t time_spec;
1762 struct tm save_tm;
1763 struct tm *decoded_time;
1764 Lisp_Object list_args[9];
1766 if (! lisp_time_argument (specified_time, &time_spec, NULL))
1767 error ("Invalid time specification");
1769 BLOCK_INPUT;
1770 decoded_time = localtime (&time_spec);
1771 /* Make a copy, in case a signal handler modifies TZ or the struct. */
1772 if (decoded_time)
1773 save_tm = *decoded_time;
1774 UNBLOCK_INPUT;
1775 if (! (decoded_time
1776 && MOST_NEGATIVE_FIXNUM - TM_YEAR_BASE <= save_tm.tm_year
1777 && save_tm.tm_year <= MOST_POSITIVE_FIXNUM - TM_YEAR_BASE))
1778 time_overflow ();
1779 XSETFASTINT (list_args[0], save_tm.tm_sec);
1780 XSETFASTINT (list_args[1], save_tm.tm_min);
1781 XSETFASTINT (list_args[2], save_tm.tm_hour);
1782 XSETFASTINT (list_args[3], save_tm.tm_mday);
1783 XSETFASTINT (list_args[4], save_tm.tm_mon + 1);
1784 /* On 64-bit machines an int is narrower than EMACS_INT, thus the
1785 cast below avoids overflow in int arithmetics. */
1786 XSETINT (list_args[5], TM_YEAR_BASE + (EMACS_INT) save_tm.tm_year);
1787 XSETFASTINT (list_args[6], save_tm.tm_wday);
1788 list_args[7] = save_tm.tm_isdst ? Qt : Qnil;
1790 BLOCK_INPUT;
1791 decoded_time = gmtime (&time_spec);
1792 if (decoded_time == 0)
1793 list_args[8] = Qnil;
1794 else
1795 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1796 UNBLOCK_INPUT;
1797 return Flist (9, list_args);
1800 /* Return OBJ - OFFSET, checking that OBJ is a valid fixnum and that
1801 the result is representable as an int. Assume OFFSET is small and
1802 nonnegative. */
1803 static int
1804 check_tm_member (Lisp_Object obj, int offset)
1806 EMACS_INT n;
1807 CHECK_NUMBER (obj);
1808 n = XINT (obj);
1809 if (! (INT_MIN + offset <= n && n - offset <= INT_MAX))
1810 time_overflow ();
1811 return n - offset;
1814 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1815 doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
1816 This is the reverse operation of `decode-time', which see.
1817 ZONE defaults to the current time zone rule. This can
1818 be a string or t (as from `set-time-zone-rule'), or it can be a list
1819 \(as from `current-time-zone') or an integer (as from `decode-time')
1820 applied without consideration for daylight saving time.
1822 You can pass more than 7 arguments; then the first six arguments
1823 are used as SECOND through YEAR, and the *last* argument is used as ZONE.
1824 The intervening arguments are ignored.
1825 This feature lets (apply 'encode-time (decode-time ...)) work.
1827 Out-of-range values for SECOND, MINUTE, HOUR, DAY, or MONTH are allowed;
1828 for example, a DAY of 0 means the day preceding the given month.
1829 Year numbers less than 100 are treated just like other year numbers.
1830 If you want them to stand for years in this century, you must do that yourself.
1832 Years before 1970 are not guaranteed to work. On some systems,
1833 year values as low as 1901 do work.
1835 usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */)
1836 (ptrdiff_t nargs, Lisp_Object *args)
1838 time_t value;
1839 struct tm tm;
1840 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1842 tm.tm_sec = check_tm_member (args[0], 0);
1843 tm.tm_min = check_tm_member (args[1], 0);
1844 tm.tm_hour = check_tm_member (args[2], 0);
1845 tm.tm_mday = check_tm_member (args[3], 0);
1846 tm.tm_mon = check_tm_member (args[4], 1);
1847 tm.tm_year = check_tm_member (args[5], TM_YEAR_BASE);
1848 tm.tm_isdst = -1;
1850 if (CONSP (zone))
1851 zone = Fcar (zone);
1852 if (NILP (zone))
1854 BLOCK_INPUT;
1855 value = mktime (&tm);
1856 UNBLOCK_INPUT;
1858 else
1860 char tzbuf[100];
1861 const char *tzstring;
1862 char **oldenv = environ, **newenv;
1864 if (EQ (zone, Qt))
1865 tzstring = "UTC0";
1866 else if (STRINGP (zone))
1867 tzstring = SSDATA (zone);
1868 else if (INTEGERP (zone))
1870 EMACS_INT abszone = eabs (XINT (zone));
1871 EMACS_INT zone_hr = abszone / (60*60);
1872 int zone_min = (abszone/60) % 60;
1873 int zone_sec = abszone % 60;
1874 sprintf (tzbuf, "XXX%s%"pI"d:%02d:%02d", "-" + (XINT (zone) < 0),
1875 zone_hr, zone_min, zone_sec);
1876 tzstring = tzbuf;
1878 else
1879 error ("Invalid time zone specification");
1881 BLOCK_INPUT;
1883 /* Set TZ before calling mktime; merely adjusting mktime's returned
1884 value doesn't suffice, since that would mishandle leap seconds. */
1885 set_time_zone_rule (tzstring);
1887 value = mktime (&tm);
1889 /* Restore TZ to previous value. */
1890 newenv = environ;
1891 environ = oldenv;
1892 #ifdef LOCALTIME_CACHE
1893 tzset ();
1894 #endif
1895 UNBLOCK_INPUT;
1897 xfree (newenv);
1900 if (value == (time_t) -1)
1901 time_overflow ();
1903 return make_time (value);
1906 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1907 doc: /* Return the current local time, as a human-readable string.
1908 Programs can use this function to decode a time,
1909 since the number of columns in each field is fixed
1910 if the year is in the range 1000-9999.
1911 The format is `Sun Sep 16 01:03:52 1973'.
1912 However, see also the functions `decode-time' and `format-time-string'
1913 which provide a much more powerful and general facility.
1915 If SPECIFIED-TIME is given, it is a time to format instead of the
1916 current time. The argument should have the form (HIGH LOW . IGNORED).
1917 Thus, you can use times obtained from `current-time' and from
1918 `file-attributes'. SPECIFIED-TIME can also have the form (HIGH . LOW),
1919 but this is considered obsolete. */)
1920 (Lisp_Object specified_time)
1922 time_t value;
1923 struct tm *tm;
1924 char buf[sizeof "Mon Apr 30 12:49:17 " + INT_STRLEN_BOUND (int) + 1];
1925 int len IF_LINT (= 0);
1927 if (! lisp_time_argument (specified_time, &value, NULL))
1928 error ("Invalid time specification");
1930 /* Convert to a string in ctime format, except without the trailing
1931 newline, and without the 4-digit year limit. Don't use asctime
1932 or ctime, as they might dump core if the year is outside the
1933 range -999 .. 9999. */
1934 BLOCK_INPUT;
1935 tm = localtime (&value);
1936 if (tm)
1938 static char const wday_name[][4] =
1939 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1940 static char const mon_name[][4] =
1941 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1942 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1943 printmax_t year_base = TM_YEAR_BASE;
1945 len = sprintf (buf, "%s %s%3d %02d:%02d:%02d %"pMd,
1946 wday_name[tm->tm_wday], mon_name[tm->tm_mon], tm->tm_mday,
1947 tm->tm_hour, tm->tm_min, tm->tm_sec,
1948 tm->tm_year + year_base);
1950 UNBLOCK_INPUT;
1951 if (! tm)
1952 time_overflow ();
1954 return make_unibyte_string (buf, len);
1957 /* Yield A - B, measured in seconds.
1958 This function is copied from the GNU C Library. */
1959 static int
1960 tm_diff (struct tm *a, struct tm *b)
1962 /* Compute intervening leap days correctly even if year is negative.
1963 Take care to avoid int overflow in leap day calculations,
1964 but it's OK to assume that A and B are close to each other. */
1965 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1966 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1967 int a100 = a4 / 25 - (a4 % 25 < 0);
1968 int b100 = b4 / 25 - (b4 % 25 < 0);
1969 int a400 = a100 >> 2;
1970 int b400 = b100 >> 2;
1971 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1972 int years = a->tm_year - b->tm_year;
1973 int days = (365 * years + intervening_leap_days
1974 + (a->tm_yday - b->tm_yday));
1975 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1976 + (a->tm_min - b->tm_min))
1977 + (a->tm_sec - b->tm_sec));
1980 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1981 doc: /* Return the offset and name for the local time zone.
1982 This returns a list of the form (OFFSET NAME).
1983 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).
1984 A negative value means west of Greenwich.
1985 NAME is a string giving the name of the time zone.
1986 If SPECIFIED-TIME is given, the time zone offset is determined from it
1987 instead of using the current time. The argument should have the form
1988 (HIGH LOW . IGNORED). Thus, you can use times obtained from
1989 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1990 have the form (HIGH . LOW), but this is considered obsolete.
1992 Some operating systems cannot provide all this information to Emacs;
1993 in this case, `current-time-zone' returns a list containing nil for
1994 the data it can't find. */)
1995 (Lisp_Object specified_time)
1997 time_t value;
1998 int offset;
1999 struct tm *t;
2000 struct tm localtm;
2001 Lisp_Object zone_offset, zone_name;
2003 zone_offset = Qnil;
2004 zone_name = format_time_string ("%Z", sizeof "%Z" - 1, specified_time,
2005 0, &value, &localtm);
2006 BLOCK_INPUT;
2007 t = gmtime (&value);
2008 if (t)
2009 offset = tm_diff (&localtm, t);
2010 UNBLOCK_INPUT;
2012 if (t)
2014 zone_offset = make_number (offset);
2015 if (SCHARS (zone_name) == 0)
2017 /* No local time zone name is available; use "+-NNNN" instead. */
2018 int m = offset / 60;
2019 int am = offset < 0 ? - m : m;
2020 char buf[sizeof "+00" + INT_STRLEN_BOUND (int)];
2021 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
2022 zone_name = build_string (buf);
2026 return list2 (zone_offset, zone_name);
2029 /* This holds the value of `environ' produced by the previous
2030 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
2031 has never been called. */
2032 static char **environbuf;
2034 /* This holds the startup value of the TZ environment variable so it
2035 can be restored if the user calls set-time-zone-rule with a nil
2036 argument. */
2037 static char *initial_tz;
2039 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
2040 doc: /* Set the local time zone using TZ, a string specifying a time zone rule.
2041 If TZ is nil, use implementation-defined default time zone information.
2042 If TZ is t, use Universal Time.
2044 Instead of calling this function, you typically want (setenv "TZ" TZ).
2045 That changes both the environment of the Emacs process and the
2046 variable `process-environment', whereas `set-time-zone-rule' affects
2047 only the former. */)
2048 (Lisp_Object tz)
2050 const char *tzstring;
2051 char **old_environbuf;
2053 if (! (NILP (tz) || EQ (tz, Qt)))
2054 CHECK_STRING (tz);
2056 BLOCK_INPUT;
2058 /* When called for the first time, save the original TZ. */
2059 old_environbuf = environbuf;
2060 if (!old_environbuf)
2061 initial_tz = (char *) getenv ("TZ");
2063 if (NILP (tz))
2064 tzstring = initial_tz;
2065 else if (EQ (tz, Qt))
2066 tzstring = "UTC0";
2067 else
2068 tzstring = SSDATA (tz);
2070 set_time_zone_rule (tzstring);
2071 environbuf = environ;
2073 UNBLOCK_INPUT;
2075 xfree (old_environbuf);
2076 return Qnil;
2079 #ifdef LOCALTIME_CACHE
2081 /* These two values are known to load tz files in buggy implementations,
2082 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
2083 Their values shouldn't matter in non-buggy implementations.
2084 We don't use string literals for these strings,
2085 since if a string in the environment is in readonly
2086 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
2087 See Sun bugs 1113095 and 1114114, ``Timezone routines
2088 improperly modify environment''. */
2090 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
2091 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
2093 #endif
2095 /* Set the local time zone rule to TZSTRING.
2096 This allocates memory into `environ', which it is the caller's
2097 responsibility to free. */
2099 void
2100 set_time_zone_rule (const char *tzstring)
2102 ptrdiff_t envptrs;
2103 char **from, **to, **newenv;
2105 /* Make the ENVIRON vector longer with room for TZSTRING. */
2106 for (from = environ; *from; from++)
2107 continue;
2108 envptrs = from - environ + 2;
2109 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
2110 + (tzstring ? strlen (tzstring) + 4 : 0));
2112 /* Add TZSTRING to the end of environ, as a value for TZ. */
2113 if (tzstring)
2115 char *t = (char *) (to + envptrs);
2116 strcpy (t, "TZ=");
2117 strcat (t, tzstring);
2118 *to++ = t;
2121 /* Copy the old environ vector elements into NEWENV,
2122 but don't copy the TZ variable.
2123 So we have only one definition of TZ, which came from TZSTRING. */
2124 for (from = environ; *from; from++)
2125 if (strncmp (*from, "TZ=", 3) != 0)
2126 *to++ = *from;
2127 *to = 0;
2129 environ = newenv;
2131 /* If we do have a TZSTRING, NEWENV points to the vector slot where
2132 the TZ variable is stored. If we do not have a TZSTRING,
2133 TO points to the vector slot which has the terminating null. */
2135 #ifdef LOCALTIME_CACHE
2137 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
2138 "US/Pacific" that loads a tz file, then changes to a value like
2139 "XXX0" that does not load a tz file, and then changes back to
2140 its original value, the last change is (incorrectly) ignored.
2141 Also, if TZ changes twice in succession to values that do
2142 not load a tz file, tzset can dump core (see Sun bug#1225179).
2143 The following code works around these bugs. */
2145 if (tzstring)
2147 /* Temporarily set TZ to a value that loads a tz file
2148 and that differs from tzstring. */
2149 char *tz = *newenv;
2150 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
2151 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
2152 tzset ();
2153 *newenv = tz;
2155 else
2157 /* The implied tzstring is unknown, so temporarily set TZ to
2158 two different values that each load a tz file. */
2159 *to = set_time_zone_rule_tz1;
2160 to[1] = 0;
2161 tzset ();
2162 *to = set_time_zone_rule_tz2;
2163 tzset ();
2164 *to = 0;
2167 /* Now TZ has the desired value, and tzset can be invoked safely. */
2170 tzset ();
2171 #endif
2174 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
2175 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
2176 type of object is Lisp_String). INHERIT is passed to
2177 INSERT_FROM_STRING_FUNC as the last argument. */
2179 static void
2180 general_insert_function (void (*insert_func)
2181 (const char *, ptrdiff_t),
2182 void (*insert_from_string_func)
2183 (Lisp_Object, ptrdiff_t, ptrdiff_t,
2184 ptrdiff_t, ptrdiff_t, int),
2185 int inherit, ptrdiff_t nargs, Lisp_Object *args)
2187 ptrdiff_t argnum;
2188 register Lisp_Object val;
2190 for (argnum = 0; argnum < nargs; argnum++)
2192 val = args[argnum];
2193 if (CHARACTERP (val))
2195 int c = XFASTINT (val);
2196 unsigned char str[MAX_MULTIBYTE_LENGTH];
2197 int len;
2199 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2200 len = CHAR_STRING (c, str);
2201 else
2203 str[0] = ASCII_CHAR_P (c) ? c : multibyte_char_to_unibyte (c);
2204 len = 1;
2206 (*insert_func) ((char *) str, len);
2208 else if (STRINGP (val))
2210 (*insert_from_string_func) (val, 0, 0,
2211 SCHARS (val),
2212 SBYTES (val),
2213 inherit);
2215 else
2216 wrong_type_argument (Qchar_or_string_p, val);
2220 void
2221 insert1 (Lisp_Object arg)
2223 Finsert (1, &arg);
2227 /* Callers passing one argument to Finsert need not gcpro the
2228 argument "array", since the only element of the array will
2229 not be used after calling insert or insert_from_string, so
2230 we don't care if it gets trashed. */
2232 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
2233 doc: /* Insert the arguments, either strings or characters, at point.
2234 Point and before-insertion markers move forward to end up
2235 after the inserted text.
2236 Any other markers at the point of insertion remain before the text.
2238 If the current buffer is multibyte, unibyte strings are converted
2239 to multibyte for insertion (see `string-make-multibyte').
2240 If the current buffer is unibyte, multibyte strings are converted
2241 to unibyte for insertion (see `string-make-unibyte').
2243 When operating on binary data, it may be necessary to preserve the
2244 original bytes of a unibyte string when inserting it into a multibyte
2245 buffer; to accomplish this, apply `string-as-multibyte' to the string
2246 and insert the result.
2248 usage: (insert &rest ARGS) */)
2249 (ptrdiff_t nargs, Lisp_Object *args)
2251 general_insert_function (insert, insert_from_string, 0, nargs, args);
2252 return Qnil;
2255 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
2256 0, MANY, 0,
2257 doc: /* Insert the arguments at point, inheriting properties from adjoining text.
2258 Point and before-insertion markers move forward to end up
2259 after the inserted text.
2260 Any other markers at the point of insertion remain before the text.
2262 If the current buffer is multibyte, unibyte strings are converted
2263 to multibyte for insertion (see `unibyte-char-to-multibyte').
2264 If the current buffer is unibyte, multibyte strings are converted
2265 to unibyte for insertion.
2267 usage: (insert-and-inherit &rest ARGS) */)
2268 (ptrdiff_t nargs, Lisp_Object *args)
2270 general_insert_function (insert_and_inherit, insert_from_string, 1,
2271 nargs, args);
2272 return Qnil;
2275 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
2276 doc: /* Insert strings or characters at point, relocating markers after the text.
2277 Point and markers move forward to end up after the inserted text.
2279 If the current buffer is multibyte, unibyte strings are converted
2280 to multibyte for insertion (see `unibyte-char-to-multibyte').
2281 If the current buffer is unibyte, multibyte strings are converted
2282 to unibyte for insertion.
2284 usage: (insert-before-markers &rest ARGS) */)
2285 (ptrdiff_t nargs, Lisp_Object *args)
2287 general_insert_function (insert_before_markers,
2288 insert_from_string_before_markers, 0,
2289 nargs, args);
2290 return Qnil;
2293 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
2294 Sinsert_and_inherit_before_markers, 0, MANY, 0,
2295 doc: /* Insert text at point, relocating markers and inheriting properties.
2296 Point and markers move forward to end up after the inserted text.
2298 If the current buffer is multibyte, unibyte strings are converted
2299 to multibyte for insertion (see `unibyte-char-to-multibyte').
2300 If the current buffer is unibyte, multibyte strings are converted
2301 to unibyte for insertion.
2303 usage: (insert-before-markers-and-inherit &rest ARGS) */)
2304 (ptrdiff_t nargs, Lisp_Object *args)
2306 general_insert_function (insert_before_markers_and_inherit,
2307 insert_from_string_before_markers, 1,
2308 nargs, args);
2309 return Qnil;
2312 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2313 doc: /* Insert COUNT copies of CHARACTER.
2314 Point, and before-insertion markers, are relocated as in the function `insert'.
2315 The optional third arg INHERIT, if non-nil, says to inherit text properties
2316 from adjoining text, if those properties are sticky. */)
2317 (Lisp_Object character, Lisp_Object count, Lisp_Object inherit)
2319 int i, stringlen;
2320 register ptrdiff_t n;
2321 int c, len;
2322 unsigned char str[MAX_MULTIBYTE_LENGTH];
2323 char string[4000];
2325 CHECK_CHARACTER (character);
2326 CHECK_NUMBER (count);
2327 c = XFASTINT (character);
2329 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2330 len = CHAR_STRING (c, str);
2331 else
2332 str[0] = c, len = 1;
2333 if (XINT (count) <= 0)
2334 return Qnil;
2335 if (BUF_BYTES_MAX / len < XINT (count))
2336 buffer_overflow ();
2337 n = XINT (count) * len;
2338 stringlen = min (n, sizeof string - sizeof string % len);
2339 for (i = 0; i < stringlen; i++)
2340 string[i] = str[i % len];
2341 while (n > stringlen)
2343 QUIT;
2344 if (!NILP (inherit))
2345 insert_and_inherit (string, stringlen);
2346 else
2347 insert (string, stringlen);
2348 n -= stringlen;
2350 if (!NILP (inherit))
2351 insert_and_inherit (string, n);
2352 else
2353 insert (string, n);
2354 return Qnil;
2357 DEFUN ("insert-byte", Finsert_byte, Sinsert_byte, 2, 3, 0,
2358 doc: /* Insert COUNT (second arg) copies of BYTE (first arg).
2359 Both arguments are required.
2360 BYTE is a number of the range 0..255.
2362 If BYTE is 128..255 and the current buffer is multibyte, the
2363 corresponding eight-bit character is inserted.
2365 Point, and before-insertion markers, are relocated as in the function `insert'.
2366 The optional third arg INHERIT, if non-nil, says to inherit text properties
2367 from adjoining text, if those properties are sticky. */)
2368 (Lisp_Object byte, Lisp_Object count, Lisp_Object inherit)
2370 CHECK_NUMBER (byte);
2371 if (XINT (byte) < 0 || XINT (byte) > 255)
2372 args_out_of_range_3 (byte, make_number (0), make_number (255));
2373 if (XINT (byte) >= 128
2374 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2375 XSETFASTINT (byte, BYTE8_TO_CHAR (XINT (byte)));
2376 return Finsert_char (byte, count, inherit);
2380 /* Making strings from buffer contents. */
2382 /* Return a Lisp_String containing the text of the current buffer from
2383 START to END. If text properties are in use and the current buffer
2384 has properties in the range specified, the resulting string will also
2385 have them, if PROPS is nonzero.
2387 We don't want to use plain old make_string here, because it calls
2388 make_uninit_string, which can cause the buffer arena to be
2389 compacted. make_string has no way of knowing that the data has
2390 been moved, and thus copies the wrong data into the string. This
2391 doesn't effect most of the other users of make_string, so it should
2392 be left as is. But we should use this function when conjuring
2393 buffer substrings. */
2395 Lisp_Object
2396 make_buffer_string (ptrdiff_t start, ptrdiff_t end, int props)
2398 ptrdiff_t start_byte = CHAR_TO_BYTE (start);
2399 ptrdiff_t end_byte = CHAR_TO_BYTE (end);
2401 return make_buffer_string_both (start, start_byte, end, end_byte, props);
2404 /* Return a Lisp_String containing the text of the current buffer from
2405 START / START_BYTE to END / END_BYTE.
2407 If text properties are in use and the current buffer
2408 has properties in the range specified, the resulting string will also
2409 have them, if PROPS is nonzero.
2411 We don't want to use plain old make_string here, because it calls
2412 make_uninit_string, which can cause the buffer arena to be
2413 compacted. make_string has no way of knowing that the data has
2414 been moved, and thus copies the wrong data into the string. This
2415 doesn't effect most of the other users of make_string, so it should
2416 be left as is. But we should use this function when conjuring
2417 buffer substrings. */
2419 Lisp_Object
2420 make_buffer_string_both (ptrdiff_t start, ptrdiff_t start_byte,
2421 ptrdiff_t end, ptrdiff_t end_byte, int props)
2423 Lisp_Object result, tem, tem1;
2425 if (start < GPT && GPT < end)
2426 move_gap (start);
2428 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2429 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
2430 else
2431 result = make_uninit_string (end - start);
2432 memcpy (SDATA (result), BYTE_POS_ADDR (start_byte), end_byte - start_byte);
2434 /* If desired, update and copy the text properties. */
2435 if (props)
2437 update_buffer_properties (start, end);
2439 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
2440 tem1 = Ftext_properties_at (make_number (start), Qnil);
2442 if (XINT (tem) != end || !NILP (tem1))
2443 copy_intervals_to_string (result, current_buffer, start,
2444 end - start);
2447 return result;
2450 /* Call Vbuffer_access_fontify_functions for the range START ... END
2451 in the current buffer, if necessary. */
2453 static void
2454 update_buffer_properties (ptrdiff_t start, ptrdiff_t end)
2456 /* If this buffer has some access functions,
2457 call them, specifying the range of the buffer being accessed. */
2458 if (!NILP (Vbuffer_access_fontify_functions))
2460 Lisp_Object args[3];
2461 Lisp_Object tem;
2463 args[0] = Qbuffer_access_fontify_functions;
2464 XSETINT (args[1], start);
2465 XSETINT (args[2], end);
2467 /* But don't call them if we can tell that the work
2468 has already been done. */
2469 if (!NILP (Vbuffer_access_fontified_property))
2471 tem = Ftext_property_any (args[1], args[2],
2472 Vbuffer_access_fontified_property,
2473 Qnil, Qnil);
2474 if (! NILP (tem))
2475 Frun_hook_with_args (3, args);
2477 else
2478 Frun_hook_with_args (3, args);
2482 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2483 doc: /* Return the contents of part of the current buffer as a string.
2484 The two arguments START and END are character positions;
2485 they can be in either order.
2486 The string returned is multibyte if the buffer is multibyte.
2488 This function copies the text properties of that part of the buffer
2489 into the result string; if you don't want the text properties,
2490 use `buffer-substring-no-properties' instead. */)
2491 (Lisp_Object start, Lisp_Object end)
2493 register ptrdiff_t b, e;
2495 validate_region (&start, &end);
2496 b = XINT (start);
2497 e = XINT (end);
2499 return make_buffer_string (b, e, 1);
2502 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2503 Sbuffer_substring_no_properties, 2, 2, 0,
2504 doc: /* Return the characters of part of the buffer, without the text properties.
2505 The two arguments START and END are character positions;
2506 they can be in either order. */)
2507 (Lisp_Object start, Lisp_Object end)
2509 register ptrdiff_t b, e;
2511 validate_region (&start, &end);
2512 b = XINT (start);
2513 e = XINT (end);
2515 return make_buffer_string (b, e, 0);
2518 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2519 doc: /* Return the contents of the current buffer as a string.
2520 If narrowing is in effect, this function returns only the visible part
2521 of the buffer. */)
2522 (void)
2524 return make_buffer_string (BEGV, ZV, 1);
2527 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2528 1, 3, 0,
2529 doc: /* Insert before point a substring of the contents of BUFFER.
2530 BUFFER may be a buffer or a buffer name.
2531 Arguments START and END are character positions specifying the substring.
2532 They default to the values of (point-min) and (point-max) in BUFFER. */)
2533 (Lisp_Object buffer, Lisp_Object start, Lisp_Object end)
2535 register EMACS_INT b, e, temp;
2536 register struct buffer *bp, *obuf;
2537 Lisp_Object buf;
2539 buf = Fget_buffer (buffer);
2540 if (NILP (buf))
2541 nsberror (buffer);
2542 bp = XBUFFER (buf);
2543 if (NILP (BVAR (bp, name)))
2544 error ("Selecting deleted buffer");
2546 if (NILP (start))
2547 b = BUF_BEGV (bp);
2548 else
2550 CHECK_NUMBER_COERCE_MARKER (start);
2551 b = XINT (start);
2553 if (NILP (end))
2554 e = BUF_ZV (bp);
2555 else
2557 CHECK_NUMBER_COERCE_MARKER (end);
2558 e = XINT (end);
2561 if (b > e)
2562 temp = b, b = e, e = temp;
2564 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2565 args_out_of_range (start, end);
2567 obuf = current_buffer;
2568 set_buffer_internal_1 (bp);
2569 update_buffer_properties (b, e);
2570 set_buffer_internal_1 (obuf);
2572 insert_from_buffer (bp, b, e - b, 0);
2573 return Qnil;
2576 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2577 6, 6, 0,
2578 doc: /* Compare two substrings of two buffers; return result as number.
2579 the value is -N if first string is less after N-1 chars,
2580 +N if first string is greater after N-1 chars, or 0 if strings match.
2581 Each substring is represented as three arguments: BUFFER, START and END.
2582 That makes six args in all, three for each substring.
2584 The value of `case-fold-search' in the current buffer
2585 determines whether case is significant or ignored. */)
2586 (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2)
2588 register EMACS_INT begp1, endp1, begp2, endp2, temp;
2589 register struct buffer *bp1, *bp2;
2590 register Lisp_Object trt
2591 = (!NILP (BVAR (current_buffer, case_fold_search))
2592 ? BVAR (current_buffer, case_canon_table) : Qnil);
2593 ptrdiff_t chars = 0;
2594 ptrdiff_t i1, i2, i1_byte, i2_byte;
2596 /* Find the first buffer and its substring. */
2598 if (NILP (buffer1))
2599 bp1 = current_buffer;
2600 else
2602 Lisp_Object buf1;
2603 buf1 = Fget_buffer (buffer1);
2604 if (NILP (buf1))
2605 nsberror (buffer1);
2606 bp1 = XBUFFER (buf1);
2607 if (NILP (BVAR (bp1, name)))
2608 error ("Selecting deleted buffer");
2611 if (NILP (start1))
2612 begp1 = BUF_BEGV (bp1);
2613 else
2615 CHECK_NUMBER_COERCE_MARKER (start1);
2616 begp1 = XINT (start1);
2618 if (NILP (end1))
2619 endp1 = BUF_ZV (bp1);
2620 else
2622 CHECK_NUMBER_COERCE_MARKER (end1);
2623 endp1 = XINT (end1);
2626 if (begp1 > endp1)
2627 temp = begp1, begp1 = endp1, endp1 = temp;
2629 if (!(BUF_BEGV (bp1) <= begp1
2630 && begp1 <= endp1
2631 && endp1 <= BUF_ZV (bp1)))
2632 args_out_of_range (start1, end1);
2634 /* Likewise for second substring. */
2636 if (NILP (buffer2))
2637 bp2 = current_buffer;
2638 else
2640 Lisp_Object buf2;
2641 buf2 = Fget_buffer (buffer2);
2642 if (NILP (buf2))
2643 nsberror (buffer2);
2644 bp2 = XBUFFER (buf2);
2645 if (NILP (BVAR (bp2, name)))
2646 error ("Selecting deleted buffer");
2649 if (NILP (start2))
2650 begp2 = BUF_BEGV (bp2);
2651 else
2653 CHECK_NUMBER_COERCE_MARKER (start2);
2654 begp2 = XINT (start2);
2656 if (NILP (end2))
2657 endp2 = BUF_ZV (bp2);
2658 else
2660 CHECK_NUMBER_COERCE_MARKER (end2);
2661 endp2 = XINT (end2);
2664 if (begp2 > endp2)
2665 temp = begp2, begp2 = endp2, endp2 = temp;
2667 if (!(BUF_BEGV (bp2) <= begp2
2668 && begp2 <= endp2
2669 && endp2 <= BUF_ZV (bp2)))
2670 args_out_of_range (start2, end2);
2672 i1 = begp1;
2673 i2 = begp2;
2674 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2675 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2677 while (i1 < endp1 && i2 < endp2)
2679 /* When we find a mismatch, we must compare the
2680 characters, not just the bytes. */
2681 int c1, c2;
2683 QUIT;
2685 if (! NILP (BVAR (bp1, enable_multibyte_characters)))
2687 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2688 BUF_INC_POS (bp1, i1_byte);
2689 i1++;
2691 else
2693 c1 = BUF_FETCH_BYTE (bp1, i1);
2694 MAKE_CHAR_MULTIBYTE (c1);
2695 i1++;
2698 if (! NILP (BVAR (bp2, enable_multibyte_characters)))
2700 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2701 BUF_INC_POS (bp2, i2_byte);
2702 i2++;
2704 else
2706 c2 = BUF_FETCH_BYTE (bp2, i2);
2707 MAKE_CHAR_MULTIBYTE (c2);
2708 i2++;
2711 if (!NILP (trt))
2713 c1 = CHAR_TABLE_TRANSLATE (trt, c1);
2714 c2 = CHAR_TABLE_TRANSLATE (trt, c2);
2716 if (c1 < c2)
2717 return make_number (- 1 - chars);
2718 if (c1 > c2)
2719 return make_number (chars + 1);
2721 chars++;
2724 /* The strings match as far as they go.
2725 If one is shorter, that one is less. */
2726 if (chars < endp1 - begp1)
2727 return make_number (chars + 1);
2728 else if (chars < endp2 - begp2)
2729 return make_number (- chars - 1);
2731 /* Same length too => they are equal. */
2732 return make_number (0);
2735 static Lisp_Object
2736 subst_char_in_region_unwind (Lisp_Object arg)
2738 return BVAR (current_buffer, undo_list) = arg;
2741 static Lisp_Object
2742 subst_char_in_region_unwind_1 (Lisp_Object arg)
2744 return BVAR (current_buffer, filename) = arg;
2747 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2748 Ssubst_char_in_region, 4, 5, 0,
2749 doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs.
2750 If optional arg NOUNDO is non-nil, don't record this change for undo
2751 and don't mark the buffer as really changed.
2752 Both characters must have the same length of multi-byte form. */)
2753 (Lisp_Object start, Lisp_Object end, Lisp_Object fromchar, Lisp_Object tochar, Lisp_Object noundo)
2755 register ptrdiff_t pos, pos_byte, stop, i, len, end_byte;
2756 /* Keep track of the first change in the buffer:
2757 if 0 we haven't found it yet.
2758 if < 0 we've found it and we've run the before-change-function.
2759 if > 0 we've actually performed it and the value is its position. */
2760 ptrdiff_t changed = 0;
2761 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2762 unsigned char *p;
2763 ptrdiff_t count = SPECPDL_INDEX ();
2764 #define COMBINING_NO 0
2765 #define COMBINING_BEFORE 1
2766 #define COMBINING_AFTER 2
2767 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2768 int maybe_byte_combining = COMBINING_NO;
2769 ptrdiff_t last_changed = 0;
2770 int multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2771 int fromc, toc;
2773 restart:
2775 validate_region (&start, &end);
2776 CHECK_CHARACTER (fromchar);
2777 CHECK_CHARACTER (tochar);
2778 fromc = XFASTINT (fromchar);
2779 toc = XFASTINT (tochar);
2781 if (multibyte_p)
2783 len = CHAR_STRING (fromc, fromstr);
2784 if (CHAR_STRING (toc, tostr) != len)
2785 error ("Characters in `subst-char-in-region' have different byte-lengths");
2786 if (!ASCII_BYTE_P (*tostr))
2788 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2789 complete multibyte character, it may be combined with the
2790 after bytes. If it is in the range 0xA0..0xFF, it may be
2791 combined with the before and after bytes. */
2792 if (!CHAR_HEAD_P (*tostr))
2793 maybe_byte_combining = COMBINING_BOTH;
2794 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2795 maybe_byte_combining = COMBINING_AFTER;
2798 else
2800 len = 1;
2801 fromstr[0] = fromc;
2802 tostr[0] = toc;
2805 pos = XINT (start);
2806 pos_byte = CHAR_TO_BYTE (pos);
2807 stop = CHAR_TO_BYTE (XINT (end));
2808 end_byte = stop;
2810 /* If we don't want undo, turn off putting stuff on the list.
2811 That's faster than getting rid of things,
2812 and it prevents even the entry for a first change.
2813 Also inhibit locking the file. */
2814 if (!changed && !NILP (noundo))
2816 record_unwind_protect (subst_char_in_region_unwind,
2817 BVAR (current_buffer, undo_list));
2818 BVAR (current_buffer, undo_list) = Qt;
2819 /* Don't do file-locking. */
2820 record_unwind_protect (subst_char_in_region_unwind_1,
2821 BVAR (current_buffer, filename));
2822 BVAR (current_buffer, filename) = Qnil;
2825 if (pos_byte < GPT_BYTE)
2826 stop = min (stop, GPT_BYTE);
2827 while (1)
2829 ptrdiff_t pos_byte_next = pos_byte;
2831 if (pos_byte >= stop)
2833 if (pos_byte >= end_byte) break;
2834 stop = end_byte;
2836 p = BYTE_POS_ADDR (pos_byte);
2837 if (multibyte_p)
2838 INC_POS (pos_byte_next);
2839 else
2840 ++pos_byte_next;
2841 if (pos_byte_next - pos_byte == len
2842 && p[0] == fromstr[0]
2843 && (len == 1
2844 || (p[1] == fromstr[1]
2845 && (len == 2 || (p[2] == fromstr[2]
2846 && (len == 3 || p[3] == fromstr[3]))))))
2848 if (changed < 0)
2849 /* We've already seen this and run the before-change-function;
2850 this time we only need to record the actual position. */
2851 changed = pos;
2852 else if (!changed)
2854 changed = -1;
2855 modify_region (current_buffer, pos, XINT (end), 0);
2857 if (! NILP (noundo))
2859 if (MODIFF - 1 == SAVE_MODIFF)
2860 SAVE_MODIFF++;
2861 if (MODIFF - 1 == BUF_AUTOSAVE_MODIFF (current_buffer))
2862 BUF_AUTOSAVE_MODIFF (current_buffer)++;
2865 /* The before-change-function may have moved the gap
2866 or even modified the buffer so we should start over. */
2867 goto restart;
2870 /* Take care of the case where the new character
2871 combines with neighboring bytes. */
2872 if (maybe_byte_combining
2873 && (maybe_byte_combining == COMBINING_AFTER
2874 ? (pos_byte_next < Z_BYTE
2875 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2876 : ((pos_byte_next < Z_BYTE
2877 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2878 || (pos_byte > BEG_BYTE
2879 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2881 Lisp_Object tem, string;
2883 struct gcpro gcpro1;
2885 tem = BVAR (current_buffer, undo_list);
2886 GCPRO1 (tem);
2888 /* Make a multibyte string containing this single character. */
2889 string = make_multibyte_string ((char *) tostr, 1, len);
2890 /* replace_range is less efficient, because it moves the gap,
2891 but it handles combining correctly. */
2892 replace_range (pos, pos + 1, string,
2893 0, 0, 1);
2894 pos_byte_next = CHAR_TO_BYTE (pos);
2895 if (pos_byte_next > pos_byte)
2896 /* Before combining happened. We should not increment
2897 POS. So, to cancel the later increment of POS,
2898 decrease it now. */
2899 pos--;
2900 else
2901 INC_POS (pos_byte_next);
2903 if (! NILP (noundo))
2904 BVAR (current_buffer, undo_list) = tem;
2906 UNGCPRO;
2908 else
2910 if (NILP (noundo))
2911 record_change (pos, 1);
2912 for (i = 0; i < len; i++) *p++ = tostr[i];
2914 last_changed = pos + 1;
2916 pos_byte = pos_byte_next;
2917 pos++;
2920 if (changed > 0)
2922 signal_after_change (changed,
2923 last_changed - changed, last_changed - changed);
2924 update_compositions (changed, last_changed, CHECK_ALL);
2927 unbind_to (count, Qnil);
2928 return Qnil;
2932 static Lisp_Object check_translation (ptrdiff_t, ptrdiff_t, ptrdiff_t,
2933 Lisp_Object);
2935 /* Helper function for Ftranslate_region_internal.
2937 Check if a character sequence at POS (POS_BYTE) matches an element
2938 of VAL. VAL is a list (([FROM-CHAR ...] . TO) ...). If a matching
2939 element is found, return it. Otherwise return Qnil. */
2941 static Lisp_Object
2942 check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end,
2943 Lisp_Object val)
2945 int buf_size = 16, buf_used = 0;
2946 int *buf = alloca (sizeof (int) * buf_size);
2948 for (; CONSP (val); val = XCDR (val))
2950 Lisp_Object elt;
2951 ptrdiff_t len, i;
2953 elt = XCAR (val);
2954 if (! CONSP (elt))
2955 continue;
2956 elt = XCAR (elt);
2957 if (! VECTORP (elt))
2958 continue;
2959 len = ASIZE (elt);
2960 if (len <= end - pos)
2962 for (i = 0; i < len; i++)
2964 if (buf_used <= i)
2966 unsigned char *p = BYTE_POS_ADDR (pos_byte);
2967 int len1;
2969 if (buf_used == buf_size)
2971 int *newbuf;
2973 buf_size += 16;
2974 newbuf = alloca (sizeof (int) * buf_size);
2975 memcpy (newbuf, buf, sizeof (int) * buf_used);
2976 buf = newbuf;
2978 buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1);
2979 pos_byte += len1;
2981 if (XINT (AREF (elt, i)) != buf[i])
2982 break;
2984 if (i == len)
2985 return XCAR (val);
2988 return Qnil;
2992 DEFUN ("translate-region-internal", Ftranslate_region_internal,
2993 Stranslate_region_internal, 3, 3, 0,
2994 doc: /* Internal use only.
2995 From START to END, translate characters according to TABLE.
2996 TABLE is a string or a char-table; the Nth character in it is the
2997 mapping for the character with code N.
2998 It returns the number of characters changed. */)
2999 (Lisp_Object start, Lisp_Object end, register Lisp_Object table)
3001 register unsigned char *tt; /* Trans table. */
3002 register int nc; /* New character. */
3003 int cnt; /* Number of changes made. */
3004 ptrdiff_t size; /* Size of translate table. */
3005 ptrdiff_t pos, pos_byte, end_pos;
3006 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
3007 int string_multibyte IF_LINT (= 0);
3009 validate_region (&start, &end);
3010 if (CHAR_TABLE_P (table))
3012 if (! EQ (XCHAR_TABLE (table)->purpose, Qtranslation_table))
3013 error ("Not a translation table");
3014 size = MAX_CHAR;
3015 tt = NULL;
3017 else
3019 CHECK_STRING (table);
3021 if (! multibyte && (SCHARS (table) < SBYTES (table)))
3022 table = string_make_unibyte (table);
3023 string_multibyte = SCHARS (table) < SBYTES (table);
3024 size = SBYTES (table);
3025 tt = SDATA (table);
3028 pos = XINT (start);
3029 pos_byte = CHAR_TO_BYTE (pos);
3030 end_pos = XINT (end);
3031 modify_region (current_buffer, pos, end_pos, 0);
3033 cnt = 0;
3034 for (; pos < end_pos; )
3036 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
3037 unsigned char *str, buf[MAX_MULTIBYTE_LENGTH];
3038 int len, str_len;
3039 int oc;
3040 Lisp_Object val;
3042 if (multibyte)
3043 oc = STRING_CHAR_AND_LENGTH (p, len);
3044 else
3045 oc = *p, len = 1;
3046 if (oc < size)
3048 if (tt)
3050 /* Reload as signal_after_change in last iteration may GC. */
3051 tt = SDATA (table);
3052 if (string_multibyte)
3054 str = tt + string_char_to_byte (table, oc);
3055 nc = STRING_CHAR_AND_LENGTH (str, str_len);
3057 else
3059 nc = tt[oc];
3060 if (! ASCII_BYTE_P (nc) && multibyte)
3062 str_len = BYTE8_STRING (nc, buf);
3063 str = buf;
3065 else
3067 str_len = 1;
3068 str = tt + oc;
3072 else
3074 nc = oc;
3075 val = CHAR_TABLE_REF (table, oc);
3076 if (CHARACTERP (val))
3078 nc = XFASTINT (val);
3079 str_len = CHAR_STRING (nc, buf);
3080 str = buf;
3082 else if (VECTORP (val) || (CONSP (val)))
3084 /* VAL is [TO_CHAR ...] or (([FROM-CHAR ...] . TO) ...)
3085 where TO is TO-CHAR or [TO-CHAR ...]. */
3086 nc = -1;
3090 if (nc != oc && nc >= 0)
3092 /* Simple one char to one char translation. */
3093 if (len != str_len)
3095 Lisp_Object string;
3097 /* This is less efficient, because it moves the gap,
3098 but it should handle multibyte characters correctly. */
3099 string = make_multibyte_string ((char *) str, 1, str_len);
3100 replace_range (pos, pos + 1, string, 1, 0, 1);
3101 len = str_len;
3103 else
3105 record_change (pos, 1);
3106 while (str_len-- > 0)
3107 *p++ = *str++;
3108 signal_after_change (pos, 1, 1);
3109 update_compositions (pos, pos + 1, CHECK_BORDER);
3111 ++cnt;
3113 else if (nc < 0)
3115 Lisp_Object string;
3117 if (CONSP (val))
3119 val = check_translation (pos, pos_byte, end_pos, val);
3120 if (NILP (val))
3122 pos_byte += len;
3123 pos++;
3124 continue;
3126 /* VAL is ([FROM-CHAR ...] . TO). */
3127 len = ASIZE (XCAR (val));
3128 val = XCDR (val);
3130 else
3131 len = 1;
3133 if (VECTORP (val))
3135 string = Fconcat (1, &val);
3137 else
3139 string = Fmake_string (make_number (1), val);
3141 replace_range (pos, pos + len, string, 1, 0, 1);
3142 pos_byte += SBYTES (string);
3143 pos += SCHARS (string);
3144 cnt += SCHARS (string);
3145 end_pos += SCHARS (string) - len;
3146 continue;
3149 pos_byte += len;
3150 pos++;
3153 return make_number (cnt);
3156 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
3157 doc: /* Delete the text between START and END.
3158 If called interactively, delete the region between point and mark.
3159 This command deletes buffer text without modifying the kill ring. */)
3160 (Lisp_Object start, Lisp_Object end)
3162 validate_region (&start, &end);
3163 del_range (XINT (start), XINT (end));
3164 return Qnil;
3167 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
3168 Sdelete_and_extract_region, 2, 2, 0,
3169 doc: /* Delete the text between START and END and return it. */)
3170 (Lisp_Object start, Lisp_Object end)
3172 validate_region (&start, &end);
3173 if (XINT (start) == XINT (end))
3174 return empty_unibyte_string;
3175 return del_range_1 (XINT (start), XINT (end), 1, 1);
3178 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
3179 doc: /* Remove restrictions (narrowing) from current buffer.
3180 This allows the buffer's full text to be seen and edited. */)
3181 (void)
3183 if (BEG != BEGV || Z != ZV)
3184 current_buffer->clip_changed = 1;
3185 BEGV = BEG;
3186 BEGV_BYTE = BEG_BYTE;
3187 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
3188 /* Changing the buffer bounds invalidates any recorded current column. */
3189 invalidate_current_column ();
3190 return Qnil;
3193 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
3194 doc: /* Restrict editing in this buffer to the current region.
3195 The rest of the text becomes temporarily invisible and untouchable
3196 but is not deleted; if you save the buffer in a file, the invisible
3197 text is included in the file. \\[widen] makes all visible again.
3198 See also `save-restriction'.
3200 When calling from a program, pass two arguments; positions (integers
3201 or markers) bounding the text that should remain visible. */)
3202 (register Lisp_Object start, Lisp_Object end)
3204 CHECK_NUMBER_COERCE_MARKER (start);
3205 CHECK_NUMBER_COERCE_MARKER (end);
3207 if (XINT (start) > XINT (end))
3209 Lisp_Object tem;
3210 tem = start; start = end; end = tem;
3213 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
3214 args_out_of_range (start, end);
3216 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
3217 current_buffer->clip_changed = 1;
3219 SET_BUF_BEGV (current_buffer, XFASTINT (start));
3220 SET_BUF_ZV (current_buffer, XFASTINT (end));
3221 if (PT < XFASTINT (start))
3222 SET_PT (XFASTINT (start));
3223 if (PT > XFASTINT (end))
3224 SET_PT (XFASTINT (end));
3225 /* Changing the buffer bounds invalidates any recorded current column. */
3226 invalidate_current_column ();
3227 return Qnil;
3230 Lisp_Object
3231 save_restriction_save (void)
3233 if (BEGV == BEG && ZV == Z)
3234 /* The common case that the buffer isn't narrowed.
3235 We return just the buffer object, which save_restriction_restore
3236 recognizes as meaning `no restriction'. */
3237 return Fcurrent_buffer ();
3238 else
3239 /* We have to save a restriction, so return a pair of markers, one
3240 for the beginning and one for the end. */
3242 Lisp_Object beg, end;
3244 beg = buildmark (BEGV, BEGV_BYTE);
3245 end = buildmark (ZV, ZV_BYTE);
3247 /* END must move forward if text is inserted at its exact location. */
3248 XMARKER (end)->insertion_type = 1;
3250 return Fcons (beg, end);
3254 Lisp_Object
3255 save_restriction_restore (Lisp_Object data)
3257 struct buffer *cur = NULL;
3258 struct buffer *buf = (CONSP (data)
3259 ? XMARKER (XCAR (data))->buffer
3260 : XBUFFER (data));
3262 if (buf && buf != current_buffer && !NILP (BVAR (buf, pt_marker)))
3263 { /* If `buf' uses markers to keep track of PT, BEGV, and ZV (as
3264 is the case if it is or has an indirect buffer), then make
3265 sure it is current before we update BEGV, so
3266 set_buffer_internal takes care of managing those markers. */
3267 cur = current_buffer;
3268 set_buffer_internal (buf);
3271 if (CONSP (data))
3272 /* A pair of marks bounding a saved restriction. */
3274 struct Lisp_Marker *beg = XMARKER (XCAR (data));
3275 struct Lisp_Marker *end = XMARKER (XCDR (data));
3276 eassert (buf == end->buffer);
3278 if (buf /* Verify marker still points to a buffer. */
3279 && (beg->charpos != BUF_BEGV (buf) || end->charpos != BUF_ZV (buf)))
3280 /* The restriction has changed from the saved one, so restore
3281 the saved restriction. */
3283 ptrdiff_t pt = BUF_PT (buf);
3285 SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
3286 SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
3288 if (pt < beg->charpos || pt > end->charpos)
3289 /* The point is outside the new visible range, move it inside. */
3290 SET_BUF_PT_BOTH (buf,
3291 clip_to_bounds (beg->charpos, pt, end->charpos),
3292 clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf),
3293 end->bytepos));
3295 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3298 else
3299 /* A buffer, which means that there was no old restriction. */
3301 if (buf /* Verify marker still points to a buffer. */
3302 && (BUF_BEGV (buf) != BUF_BEG (buf) || BUF_ZV (buf) != BUF_Z (buf)))
3303 /* The buffer has been narrowed, get rid of the narrowing. */
3305 SET_BUF_BEGV_BOTH (buf, BUF_BEG (buf), BUF_BEG_BYTE (buf));
3306 SET_BUF_ZV_BOTH (buf, BUF_Z (buf), BUF_Z_BYTE (buf));
3308 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3312 /* Changing the buffer bounds invalidates any recorded current column. */
3313 invalidate_current_column ();
3315 if (cur)
3316 set_buffer_internal (cur);
3318 return Qnil;
3321 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
3322 doc: /* Execute BODY, saving and restoring current buffer's restrictions.
3323 The buffer's restrictions make parts of the beginning and end invisible.
3324 \(They are set up with `narrow-to-region' and eliminated with `widen'.)
3325 This special form, `save-restriction', saves the current buffer's restrictions
3326 when it is entered, and restores them when it is exited.
3327 So any `narrow-to-region' within BODY lasts only until the end of the form.
3328 The old restrictions settings are restored
3329 even in case of abnormal exit (throw or error).
3331 The value returned is the value of the last form in BODY.
3333 Note: if you are using both `save-excursion' and `save-restriction',
3334 use `save-excursion' outermost:
3335 (save-excursion (save-restriction ...))
3337 usage: (save-restriction &rest BODY) */)
3338 (Lisp_Object body)
3340 register Lisp_Object val;
3341 ptrdiff_t count = SPECPDL_INDEX ();
3343 record_unwind_protect (save_restriction_restore, save_restriction_save ());
3344 val = Fprogn (body);
3345 return unbind_to (count, val);
3348 /* Buffer for the most recent text displayed by Fmessage_box. */
3349 static char *message_text;
3351 /* Allocated length of that buffer. */
3352 static ptrdiff_t message_length;
3354 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
3355 doc: /* Display a message at the bottom of the screen.
3356 The message also goes into the `*Messages*' buffer.
3357 \(In keyboard macros, that's all it does.)
3358 Return the message.
3360 The first argument is a format control string, and the rest are data
3361 to be formatted under control of the string. See `format' for details.
3363 Note: Use (message "%s" VALUE) to print the value of expressions and
3364 variables to avoid accidentally interpreting `%' as format specifiers.
3366 If the first argument is nil or the empty string, the function clears
3367 any existing message; this lets the minibuffer contents show. See
3368 also `current-message'.
3370 usage: (message FORMAT-STRING &rest ARGS) */)
3371 (ptrdiff_t nargs, Lisp_Object *args)
3373 if (NILP (args[0])
3374 || (STRINGP (args[0])
3375 && SBYTES (args[0]) == 0))
3377 message (0);
3378 return args[0];
3380 else
3382 register Lisp_Object val;
3383 val = Fformat (nargs, args);
3384 message3 (val, SBYTES (val), STRING_MULTIBYTE (val));
3385 return val;
3389 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
3390 doc: /* Display a message, in a dialog box if possible.
3391 If a dialog box is not available, use the echo area.
3392 The first argument is a format control string, and the rest are data
3393 to be formatted under control of the string. See `format' for details.
3395 If the first argument is nil or the empty string, clear any existing
3396 message; let the minibuffer contents show.
3398 usage: (message-box FORMAT-STRING &rest ARGS) */)
3399 (ptrdiff_t nargs, Lisp_Object *args)
3401 if (NILP (args[0]))
3403 message (0);
3404 return Qnil;
3406 else
3408 register Lisp_Object val;
3409 val = Fformat (nargs, args);
3410 #ifdef HAVE_MENUS
3411 /* The MS-DOS frames support popup menus even though they are
3412 not FRAME_WINDOW_P. */
3413 if (FRAME_WINDOW_P (XFRAME (selected_frame))
3414 || FRAME_MSDOS_P (XFRAME (selected_frame)))
3416 Lisp_Object pane, menu;
3417 struct gcpro gcpro1;
3418 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
3419 GCPRO1 (pane);
3420 menu = Fcons (val, pane);
3421 Fx_popup_dialog (Qt, menu, Qt);
3422 UNGCPRO;
3423 return val;
3425 #endif /* HAVE_MENUS */
3426 /* Copy the data so that it won't move when we GC. */
3427 if (! message_text)
3429 message_text = (char *)xmalloc (80);
3430 message_length = 80;
3432 if (SBYTES (val) > message_length)
3434 message_text = (char *) xrealloc (message_text, SBYTES (val));
3435 message_length = SBYTES (val);
3437 memcpy (message_text, SDATA (val), SBYTES (val));
3438 message2 (message_text, SBYTES (val),
3439 STRING_MULTIBYTE (val));
3440 return val;
3444 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
3445 doc: /* Display a message in a dialog box or in the echo area.
3446 If this command was invoked with the mouse, use a dialog box if
3447 `use-dialog-box' is non-nil.
3448 Otherwise, use the echo area.
3449 The first argument is a format control string, and the rest are data
3450 to be formatted under control of the string. See `format' for details.
3452 If the first argument is nil or the empty string, clear any existing
3453 message; let the minibuffer contents show.
3455 usage: (message-or-box FORMAT-STRING &rest ARGS) */)
3456 (ptrdiff_t nargs, Lisp_Object *args)
3458 #ifdef HAVE_MENUS
3459 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3460 && use_dialog_box)
3461 return Fmessage_box (nargs, args);
3462 #endif
3463 return Fmessage (nargs, args);
3466 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
3467 doc: /* Return the string currently displayed in the echo area, or nil if none. */)
3468 (void)
3470 return current_message ();
3474 DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0,
3475 doc: /* Return a copy of STRING with text properties added.
3476 First argument is the string to copy.
3477 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
3478 properties to add to the result.
3479 usage: (propertize STRING &rest PROPERTIES) */)
3480 (ptrdiff_t nargs, Lisp_Object *args)
3482 Lisp_Object properties, string;
3483 struct gcpro gcpro1, gcpro2;
3484 ptrdiff_t i;
3486 /* Number of args must be odd. */
3487 if ((nargs & 1) == 0)
3488 error ("Wrong number of arguments");
3490 properties = string = Qnil;
3491 GCPRO2 (properties, string);
3493 /* First argument must be a string. */
3494 CHECK_STRING (args[0]);
3495 string = Fcopy_sequence (args[0]);
3497 for (i = 1; i < nargs; i += 2)
3498 properties = Fcons (args[i], Fcons (args[i + 1], properties));
3500 Fadd_text_properties (make_number (0),
3501 make_number (SCHARS (string)),
3502 properties, string);
3503 RETURN_UNGCPRO (string);
3506 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3507 doc: /* Format a string out of a format-string and arguments.
3508 The first argument is a format control string.
3509 The other arguments are substituted into it to make the result, a string.
3511 The format control string may contain %-sequences meaning to substitute
3512 the next available argument:
3514 %s means print a string argument. Actually, prints any object, with `princ'.
3515 %d means print as number in decimal (%o octal, %x hex).
3516 %X is like %x, but uses upper case.
3517 %e means print a number in exponential notation.
3518 %f means print a number in decimal-point notation.
3519 %g means print a number in exponential notation
3520 or decimal-point notation, whichever uses fewer characters.
3521 %c means print a number as a single character.
3522 %S means print any object as an s-expression (using `prin1').
3524 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.
3525 Use %% to put a single % into the output.
3527 A %-sequence may contain optional flag, width, and precision
3528 specifiers, as follows:
3530 %<flags><width><precision>character
3532 where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+
3534 The + flag character inserts a + before any positive number, while a
3535 space inserts a space before any positive number; these flags only
3536 affect %d, %e, %f, and %g sequences, and the + flag takes precedence.
3537 The # flag means to use an alternate display form for %o, %x, %X, %e,
3538 %f, and %g sequences. The - and 0 flags affect the width specifier,
3539 as described below.
3541 The width specifier supplies a lower limit for the length of the
3542 printed representation. The padding, if any, normally goes on the
3543 left, but it goes on the right if the - flag is present. The padding
3544 character is normally a space, but it is 0 if the 0 flag is present.
3545 The 0 flag is ignored if the - flag is present, or the format sequence
3546 is something other than %d, %e, %f, and %g.
3548 For %e, %f, and %g sequences, the number after the "." in the
3549 precision specifier says how many decimal places to show; if zero, the
3550 decimal point itself is omitted. For %s and %S, the precision
3551 specifier truncates the string to the given width.
3553 usage: (format STRING &rest OBJECTS) */)
3554 (ptrdiff_t nargs, Lisp_Object *args)
3556 ptrdiff_t n; /* The number of the next arg to substitute */
3557 char initial_buffer[4000];
3558 char *buf = initial_buffer;
3559 ptrdiff_t bufsize = sizeof initial_buffer;
3560 ptrdiff_t max_bufsize = STRING_BYTES_BOUND + 1;
3561 char *p;
3562 Lisp_Object buf_save_value IF_LINT (= {0});
3563 register char *format, *end, *format_start;
3564 ptrdiff_t formatlen, nchars;
3565 /* Nonzero if the format is multibyte. */
3566 int multibyte_format = 0;
3567 /* Nonzero if the output should be a multibyte string,
3568 which is true if any of the inputs is one. */
3569 int multibyte = 0;
3570 /* When we make a multibyte string, we must pay attention to the
3571 byte combining problem, i.e., a byte may be combined with a
3572 multibyte character of the previous string. This flag tells if we
3573 must consider such a situation or not. */
3574 int maybe_combine_byte;
3575 Lisp_Object val;
3576 int arg_intervals = 0;
3577 USE_SAFE_ALLOCA;
3579 /* discarded[I] is 1 if byte I of the format
3580 string was not copied into the output.
3581 It is 2 if byte I was not the first byte of its character. */
3582 char *discarded;
3584 /* Each element records, for one argument,
3585 the start and end bytepos in the output string,
3586 whether the argument has been converted to string (e.g., due to "%S"),
3587 and whether the argument is a string with intervals.
3588 info[0] is unused. Unused elements have -1 for start. */
3589 struct info
3591 ptrdiff_t start, end;
3592 int converted_to_string;
3593 int intervals;
3594 } *info = 0;
3596 /* It should not be necessary to GCPRO ARGS, because
3597 the caller in the interpreter should take care of that. */
3599 CHECK_STRING (args[0]);
3600 format_start = SSDATA (args[0]);
3601 formatlen = SBYTES (args[0]);
3603 /* Allocate the info and discarded tables. */
3605 ptrdiff_t i;
3606 if ((SIZE_MAX - formatlen) / sizeof (struct info) <= nargs)
3607 memory_full (SIZE_MAX);
3608 SAFE_ALLOCA (info, struct info *, (nargs + 1) * sizeof *info + formatlen);
3609 discarded = (char *) &info[nargs + 1];
3610 for (i = 0; i < nargs + 1; i++)
3612 info[i].start = -1;
3613 info[i].intervals = info[i].converted_to_string = 0;
3615 memset (discarded, 0, formatlen);
3618 /* Try to determine whether the result should be multibyte.
3619 This is not always right; sometimes the result needs to be multibyte
3620 because of an object that we will pass through prin1,
3621 and in that case, we won't know it here. */
3622 multibyte_format = STRING_MULTIBYTE (args[0]);
3623 multibyte = multibyte_format;
3624 for (n = 1; !multibyte && n < nargs; n++)
3625 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3626 multibyte = 1;
3628 /* If we start out planning a unibyte result,
3629 then discover it has to be multibyte, we jump back to retry. */
3630 retry:
3632 p = buf;
3633 nchars = 0;
3634 n = 0;
3636 /* Scan the format and store result in BUF. */
3637 format = format_start;
3638 end = format + formatlen;
3639 maybe_combine_byte = 0;
3641 while (format != end)
3643 /* The values of N and FORMAT when the loop body is entered. */
3644 ptrdiff_t n0 = n;
3645 char *format0 = format;
3647 /* Bytes needed to represent the output of this conversion. */
3648 ptrdiff_t convbytes;
3650 if (*format == '%')
3652 /* General format specifications look like
3654 '%' [flags] [field-width] [precision] format
3656 where
3658 flags ::= [-+0# ]+
3659 field-width ::= [0-9]+
3660 precision ::= '.' [0-9]*
3662 If a field-width is specified, it specifies to which width
3663 the output should be padded with blanks, if the output
3664 string is shorter than field-width.
3666 If precision is specified, it specifies the number of
3667 digits to print after the '.' for floats, or the max.
3668 number of chars to print from a string. */
3670 int minus_flag = 0;
3671 int plus_flag = 0;
3672 int space_flag = 0;
3673 int sharp_flag = 0;
3674 int zero_flag = 0;
3675 ptrdiff_t field_width;
3676 int precision_given;
3677 uintmax_t precision = UINTMAX_MAX;
3678 char *num_end;
3679 char conversion;
3681 while (1)
3683 switch (*++format)
3685 case '-': minus_flag = 1; continue;
3686 case '+': plus_flag = 1; continue;
3687 case ' ': space_flag = 1; continue;
3688 case '#': sharp_flag = 1; continue;
3689 case '0': zero_flag = 1; continue;
3691 break;
3694 /* Ignore flags when sprintf ignores them. */
3695 space_flag &= ~ plus_flag;
3696 zero_flag &= ~ minus_flag;
3699 uintmax_t w = strtoumax (format, &num_end, 10);
3700 if (max_bufsize <= w)
3701 string_overflow ();
3702 field_width = w;
3704 precision_given = *num_end == '.';
3705 if (precision_given)
3706 precision = strtoumax (num_end + 1, &num_end, 10);
3707 format = num_end;
3709 if (format == end)
3710 error ("Format string ends in middle of format specifier");
3712 memset (&discarded[format0 - format_start], 1, format - format0);
3713 conversion = *format;
3714 if (conversion == '%')
3715 goto copy_char;
3716 discarded[format - format_start] = 1;
3717 format++;
3719 ++n;
3720 if (! (n < nargs))
3721 error ("Not enough arguments for format string");
3723 /* For 'S', prin1 the argument, and then treat like 's'.
3724 For 's', princ any argument that is not a string or
3725 symbol. But don't do this conversion twice, which might
3726 happen after retrying. */
3727 if ((conversion == 'S'
3728 || (conversion == 's'
3729 && ! STRINGP (args[n]) && ! SYMBOLP (args[n]))))
3731 if (! info[n].converted_to_string)
3733 Lisp_Object noescape = conversion == 'S' ? Qnil : Qt;
3734 args[n] = Fprin1_to_string (args[n], noescape);
3735 info[n].converted_to_string = 1;
3736 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3738 multibyte = 1;
3739 goto retry;
3742 conversion = 's';
3744 else if (conversion == 'c')
3746 if (FLOATP (args[n]))
3748 double d = XFLOAT_DATA (args[n]);
3749 args[n] = make_number (FIXNUM_OVERFLOW_P (d) ? -1 : d);
3752 if (INTEGERP (args[n]) && ! ASCII_CHAR_P (XINT (args[n])))
3754 if (!multibyte)
3756 multibyte = 1;
3757 goto retry;
3759 args[n] = Fchar_to_string (args[n]);
3760 info[n].converted_to_string = 1;
3763 if (info[n].converted_to_string)
3764 conversion = 's';
3765 zero_flag = 0;
3768 if (SYMBOLP (args[n]))
3770 args[n] = SYMBOL_NAME (args[n]);
3771 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3773 multibyte = 1;
3774 goto retry;
3778 if (conversion == 's')
3780 /* handle case (precision[n] >= 0) */
3782 ptrdiff_t width, padding, nbytes;
3783 ptrdiff_t nchars_string;
3785 ptrdiff_t prec = -1;
3786 if (precision_given && precision <= TYPE_MAXIMUM (ptrdiff_t))
3787 prec = precision;
3789 /* lisp_string_width ignores a precision of 0, but GNU
3790 libc functions print 0 characters when the precision
3791 is 0. Imitate libc behavior here. Changing
3792 lisp_string_width is the right thing, and will be
3793 done, but meanwhile we work with it. */
3795 if (prec == 0)
3796 width = nchars_string = nbytes = 0;
3797 else
3799 ptrdiff_t nch, nby;
3800 width = lisp_string_width (args[n], prec, &nch, &nby);
3801 if (prec < 0)
3803 nchars_string = SCHARS (args[n]);
3804 nbytes = SBYTES (args[n]);
3806 else
3808 nchars_string = nch;
3809 nbytes = nby;
3813 convbytes = nbytes;
3814 if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n]))
3815 convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes);
3817 padding = width < field_width ? field_width - width : 0;
3819 if (max_bufsize - padding <= convbytes)
3820 string_overflow ();
3821 convbytes += padding;
3822 if (convbytes <= buf + bufsize - p)
3824 if (! minus_flag)
3826 memset (p, ' ', padding);
3827 p += padding;
3828 nchars += padding;
3831 if (p > buf
3832 && multibyte
3833 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3834 && STRING_MULTIBYTE (args[n])
3835 && !CHAR_HEAD_P (SREF (args[n], 0)))
3836 maybe_combine_byte = 1;
3838 p += copy_text (SDATA (args[n]), (unsigned char *) p,
3839 nbytes,
3840 STRING_MULTIBYTE (args[n]), multibyte);
3842 info[n].start = nchars;
3843 nchars += nchars_string;
3844 info[n].end = nchars;
3846 if (minus_flag)
3848 memset (p, ' ', padding);
3849 p += padding;
3850 nchars += padding;
3853 /* If this argument has text properties, record where
3854 in the result string it appears. */
3855 if (STRING_INTERVALS (args[n]))
3856 info[n].intervals = arg_intervals = 1;
3858 continue;
3861 else if (! (conversion == 'c' || conversion == 'd'
3862 || conversion == 'e' || conversion == 'f'
3863 || conversion == 'g' || conversion == 'i'
3864 || conversion == 'o' || conversion == 'x'
3865 || conversion == 'X'))
3866 error ("Invalid format operation %%%c",
3867 STRING_CHAR ((unsigned char *) format - 1));
3868 else if (! (INTEGERP (args[n]) || FLOATP (args[n])))
3869 error ("Format specifier doesn't match argument type");
3870 else
3872 enum
3874 /* Maximum precision for a %f conversion such that the
3875 trailing output digit might be nonzero. Any precision
3876 larger than this will not yield useful information. */
3877 USEFUL_PRECISION_MAX =
3878 ((1 - DBL_MIN_EXP)
3879 * (FLT_RADIX == 2 || FLT_RADIX == 10 ? 1
3880 : FLT_RADIX == 16 ? 4
3881 : -1)),
3883 /* Maximum number of bytes generated by any format, if
3884 precision is no more than USEFUL_PRECISION_MAX.
3885 On all practical hosts, %f is the worst case. */
3886 SPRINTF_BUFSIZE =
3887 sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX,
3889 /* Length of pM (that is, of pMd without the
3890 trailing "d"). */
3891 pMlen = sizeof pMd - 2
3893 verify (0 < USEFUL_PRECISION_MAX);
3895 int prec;
3896 ptrdiff_t padding, sprintf_bytes;
3897 uintmax_t excess_precision, numwidth;
3898 uintmax_t leading_zeros = 0, trailing_zeros = 0;
3900 char sprintf_buf[SPRINTF_BUFSIZE];
3902 /* Copy of conversion specification, modified somewhat.
3903 At most three flags F can be specified at once. */
3904 char convspec[sizeof "%FFF.*d" + pMlen];
3906 /* Avoid undefined behavior in underlying sprintf. */
3907 if (conversion == 'd' || conversion == 'i')
3908 sharp_flag = 0;
3910 /* Create the copy of the conversion specification, with
3911 any width and precision removed, with ".*" inserted,
3912 and with pM inserted for integer formats. */
3914 char *f = convspec;
3915 *f++ = '%';
3916 *f = '-'; f += minus_flag;
3917 *f = '+'; f += plus_flag;
3918 *f = ' '; f += space_flag;
3919 *f = '#'; f += sharp_flag;
3920 *f = '0'; f += zero_flag;
3921 *f++ = '.';
3922 *f++ = '*';
3923 if (conversion == 'd' || conversion == 'i'
3924 || conversion == 'o' || conversion == 'x'
3925 || conversion == 'X')
3927 memcpy (f, pMd, pMlen);
3928 f += pMlen;
3929 zero_flag &= ~ precision_given;
3931 *f++ = conversion;
3932 *f = '\0';
3935 prec = -1;
3936 if (precision_given)
3937 prec = min (precision, USEFUL_PRECISION_MAX);
3939 /* Use sprintf to format this number into sprintf_buf. Omit
3940 padding and excess precision, though, because sprintf limits
3941 output length to INT_MAX.
3943 There are four types of conversion: double, unsigned
3944 char (passed as int), wide signed int, and wide
3945 unsigned int. Treat them separately because the
3946 sprintf ABI is sensitive to which type is passed. Be
3947 careful about integer overflow, NaNs, infinities, and
3948 conversions; for example, the min and max macros are
3949 not suitable here. */
3950 if (conversion == 'e' || conversion == 'f' || conversion == 'g')
3952 double x = (INTEGERP (args[n])
3953 ? XINT (args[n])
3954 : XFLOAT_DATA (args[n]));
3955 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
3957 else if (conversion == 'c')
3959 /* Don't use sprintf here, as it might mishandle prec. */
3960 sprintf_buf[0] = XINT (args[n]);
3961 sprintf_bytes = prec != 0;
3963 else if (conversion == 'd')
3965 /* For float, maybe we should use "%1.0f"
3966 instead so it also works for values outside
3967 the integer range. */
3968 printmax_t x;
3969 if (INTEGERP (args[n]))
3970 x = XINT (args[n]);
3971 else
3973 double d = XFLOAT_DATA (args[n]);
3974 if (d < 0)
3976 x = TYPE_MINIMUM (printmax_t);
3977 if (x < d)
3978 x = d;
3980 else
3982 x = TYPE_MAXIMUM (printmax_t);
3983 if (d < x)
3984 x = d;
3987 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
3989 else
3991 /* Don't sign-extend for octal or hex printing. */
3992 uprintmax_t x;
3993 if (INTEGERP (args[n]))
3994 x = XUINT (args[n]);
3995 else
3997 double d = XFLOAT_DATA (args[n]);
3998 if (d < 0)
3999 x = 0;
4000 else
4002 x = TYPE_MAXIMUM (uprintmax_t);
4003 if (d < x)
4004 x = d;
4007 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
4010 /* Now the length of the formatted item is known, except it omits
4011 padding and excess precision. Deal with excess precision
4012 first. This happens only when the format specifies
4013 ridiculously large precision. */
4014 excess_precision = precision - prec;
4015 if (excess_precision)
4017 if (conversion == 'e' || conversion == 'f'
4018 || conversion == 'g')
4020 if ((conversion == 'g' && ! sharp_flag)
4021 || ! ('0' <= sprintf_buf[sprintf_bytes - 1]
4022 && sprintf_buf[sprintf_bytes - 1] <= '9'))
4023 excess_precision = 0;
4024 else
4026 if (conversion == 'g')
4028 char *dot = strchr (sprintf_buf, '.');
4029 if (!dot)
4030 excess_precision = 0;
4033 trailing_zeros = excess_precision;
4035 else
4036 leading_zeros = excess_precision;
4039 /* Compute the total bytes needed for this item, including
4040 excess precision and padding. */
4041 numwidth = sprintf_bytes + excess_precision;
4042 padding = numwidth < field_width ? field_width - numwidth : 0;
4043 if (max_bufsize - sprintf_bytes <= excess_precision
4044 || max_bufsize - padding <= numwidth)
4045 string_overflow ();
4046 convbytes = numwidth + padding;
4048 if (convbytes <= buf + bufsize - p)
4050 /* Copy the formatted item from sprintf_buf into buf,
4051 inserting padding and excess-precision zeros. */
4053 char *src = sprintf_buf;
4054 char src0 = src[0];
4055 int exponent_bytes = 0;
4056 int signedp = src0 == '-' || src0 == '+' || src0 == ' ';
4057 int significand_bytes;
4058 if (zero_flag
4059 && ((src[signedp] >= '0' && src[signedp] <= '9')
4060 || (src[signedp] >= 'a' && src[signedp] <= 'f')
4061 || (src[signedp] >= 'A' && src[signedp] <= 'F')))
4063 leading_zeros += padding;
4064 padding = 0;
4067 if (excess_precision
4068 && (conversion == 'e' || conversion == 'g'))
4070 char *e = strchr (src, 'e');
4071 if (e)
4072 exponent_bytes = src + sprintf_bytes - e;
4075 if (! minus_flag)
4077 memset (p, ' ', padding);
4078 p += padding;
4079 nchars += padding;
4082 *p = src0;
4083 src += signedp;
4084 p += signedp;
4085 memset (p, '0', leading_zeros);
4086 p += leading_zeros;
4087 significand_bytes = sprintf_bytes - signedp - exponent_bytes;
4088 memcpy (p, src, significand_bytes);
4089 p += significand_bytes;
4090 src += significand_bytes;
4091 memset (p, '0', trailing_zeros);
4092 p += trailing_zeros;
4093 memcpy (p, src, exponent_bytes);
4094 p += exponent_bytes;
4096 info[n].start = nchars;
4097 nchars += leading_zeros + sprintf_bytes + trailing_zeros;
4098 info[n].end = nchars;
4100 if (minus_flag)
4102 memset (p, ' ', padding);
4103 p += padding;
4104 nchars += padding;
4107 continue;
4111 else
4112 copy_char:
4114 /* Copy a single character from format to buf. */
4116 char *src = format;
4117 unsigned char str[MAX_MULTIBYTE_LENGTH];
4119 if (multibyte_format)
4121 /* Copy a whole multibyte character. */
4122 if (p > buf
4123 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
4124 && !CHAR_HEAD_P (*format))
4125 maybe_combine_byte = 1;
4128 format++;
4129 while (! CHAR_HEAD_P (*format));
4131 convbytes = format - src;
4132 memset (&discarded[src + 1 - format_start], 2, convbytes - 1);
4134 else
4136 unsigned char uc = *format++;
4137 if (! multibyte || ASCII_BYTE_P (uc))
4138 convbytes = 1;
4139 else
4141 int c = BYTE8_TO_CHAR (uc);
4142 convbytes = CHAR_STRING (c, str);
4143 src = (char *) str;
4147 if (convbytes <= buf + bufsize - p)
4149 memcpy (p, src, convbytes);
4150 p += convbytes;
4151 nchars++;
4152 continue;
4156 /* There wasn't enough room to store this conversion or single
4157 character. CONVBYTES says how much room is needed. Allocate
4158 enough room (and then some) and do it again. */
4160 ptrdiff_t used = p - buf;
4162 if (max_bufsize - used < convbytes)
4163 string_overflow ();
4164 bufsize = used + convbytes;
4165 bufsize = bufsize < max_bufsize / 2 ? bufsize * 2 : max_bufsize;
4167 if (buf == initial_buffer)
4169 buf = xmalloc (bufsize);
4170 sa_must_free = 1;
4171 buf_save_value = make_save_value (buf, 0);
4172 record_unwind_protect (safe_alloca_unwind, buf_save_value);
4173 memcpy (buf, initial_buffer, used);
4175 else
4176 XSAVE_VALUE (buf_save_value)->pointer = buf = xrealloc (buf, bufsize);
4178 p = buf + used;
4181 format = format0;
4182 n = n0;
4185 if (bufsize < p - buf)
4186 abort ();
4188 if (maybe_combine_byte)
4189 nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf);
4190 val = make_specified_string (buf, nchars, p - buf, multibyte);
4192 /* If we allocated BUF with malloc, free it too. */
4193 SAFE_FREE ();
4195 /* If the format string has text properties, or any of the string
4196 arguments has text properties, set up text properties of the
4197 result string. */
4199 if (STRING_INTERVALS (args[0]) || arg_intervals)
4201 Lisp_Object len, new_len, props;
4202 struct gcpro gcpro1;
4204 /* Add text properties from the format string. */
4205 len = make_number (SCHARS (args[0]));
4206 props = text_property_list (args[0], make_number (0), len, Qnil);
4207 GCPRO1 (props);
4209 if (CONSP (props))
4211 ptrdiff_t bytepos = 0, position = 0, translated = 0;
4212 ptrdiff_t argn = 1;
4213 Lisp_Object list;
4215 /* Adjust the bounds of each text property
4216 to the proper start and end in the output string. */
4218 /* Put the positions in PROPS in increasing order, so that
4219 we can do (effectively) one scan through the position
4220 space of the format string. */
4221 props = Fnreverse (props);
4223 /* BYTEPOS is the byte position in the format string,
4224 POSITION is the untranslated char position in it,
4225 TRANSLATED is the translated char position in BUF,
4226 and ARGN is the number of the next arg we will come to. */
4227 for (list = props; CONSP (list); list = XCDR (list))
4229 Lisp_Object item;
4230 ptrdiff_t pos;
4232 item = XCAR (list);
4234 /* First adjust the property start position. */
4235 pos = XINT (XCAR (item));
4237 /* Advance BYTEPOS, POSITION, TRANSLATED and ARGN
4238 up to this position. */
4239 for (; position < pos; bytepos++)
4241 if (! discarded[bytepos])
4242 position++, translated++;
4243 else if (discarded[bytepos] == 1)
4245 position++;
4246 if (translated == info[argn].start)
4248 translated += info[argn].end - info[argn].start;
4249 argn++;
4254 XSETCAR (item, make_number (translated));
4256 /* Likewise adjust the property end position. */
4257 pos = XINT (XCAR (XCDR (item)));
4259 for (; position < pos; bytepos++)
4261 if (! discarded[bytepos])
4262 position++, translated++;
4263 else if (discarded[bytepos] == 1)
4265 position++;
4266 if (translated == info[argn].start)
4268 translated += info[argn].end - info[argn].start;
4269 argn++;
4274 XSETCAR (XCDR (item), make_number (translated));
4277 add_text_properties_from_list (val, props, make_number (0));
4280 /* Add text properties from arguments. */
4281 if (arg_intervals)
4282 for (n = 1; n < nargs; ++n)
4283 if (info[n].intervals)
4285 len = make_number (SCHARS (args[n]));
4286 new_len = make_number (info[n].end - info[n].start);
4287 props = text_property_list (args[n], make_number (0), len, Qnil);
4288 props = extend_property_ranges (props, new_len);
4289 /* If successive arguments have properties, be sure that
4290 the value of `composition' property be the copy. */
4291 if (n > 1 && info[n - 1].end)
4292 make_composition_value_copy (props);
4293 add_text_properties_from_list (val, props,
4294 make_number (info[n].start));
4297 UNGCPRO;
4300 return val;
4303 Lisp_Object
4304 format2 (const char *string1, Lisp_Object arg0, Lisp_Object arg1)
4306 Lisp_Object args[3];
4307 args[0] = build_string (string1);
4308 args[1] = arg0;
4309 args[2] = arg1;
4310 return Fformat (3, args);
4313 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
4314 doc: /* Return t if two characters match, optionally ignoring case.
4315 Both arguments must be characters (i.e. integers).
4316 Case is ignored if `case-fold-search' is non-nil in the current buffer. */)
4317 (register Lisp_Object c1, Lisp_Object c2)
4319 int i1, i2;
4320 /* Check they're chars, not just integers, otherwise we could get array
4321 bounds violations in downcase. */
4322 CHECK_CHARACTER (c1);
4323 CHECK_CHARACTER (c2);
4325 if (XINT (c1) == XINT (c2))
4326 return Qt;
4327 if (NILP (BVAR (current_buffer, case_fold_search)))
4328 return Qnil;
4330 i1 = XFASTINT (c1);
4331 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4332 && ! ASCII_CHAR_P (i1))
4334 MAKE_CHAR_MULTIBYTE (i1);
4336 i2 = XFASTINT (c2);
4337 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4338 && ! ASCII_CHAR_P (i2))
4340 MAKE_CHAR_MULTIBYTE (i2);
4342 return (downcase (i1) == downcase (i2) ? Qt : Qnil);
4345 /* Transpose the markers in two regions of the current buffer, and
4346 adjust the ones between them if necessary (i.e.: if the regions
4347 differ in size).
4349 START1, END1 are the character positions of the first region.
4350 START1_BYTE, END1_BYTE are the byte positions.
4351 START2, END2 are the character positions of the second region.
4352 START2_BYTE, END2_BYTE are the byte positions.
4354 Traverses the entire marker list of the buffer to do so, adding an
4355 appropriate amount to some, subtracting from some, and leaving the
4356 rest untouched. Most of this is copied from adjust_markers in insdel.c.
4358 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
4360 static void
4361 transpose_markers (ptrdiff_t start1, ptrdiff_t end1,
4362 ptrdiff_t start2, ptrdiff_t end2,
4363 ptrdiff_t start1_byte, ptrdiff_t end1_byte,
4364 ptrdiff_t start2_byte, ptrdiff_t end2_byte)
4366 register ptrdiff_t amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
4367 register struct Lisp_Marker *marker;
4369 /* Update point as if it were a marker. */
4370 if (PT < start1)
4372 else if (PT < end1)
4373 TEMP_SET_PT_BOTH (PT + (end2 - end1),
4374 PT_BYTE + (end2_byte - end1_byte));
4375 else if (PT < start2)
4376 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
4377 (PT_BYTE + (end2_byte - start2_byte)
4378 - (end1_byte - start1_byte)));
4379 else if (PT < end2)
4380 TEMP_SET_PT_BOTH (PT - (start2 - start1),
4381 PT_BYTE - (start2_byte - start1_byte));
4383 /* We used to adjust the endpoints here to account for the gap, but that
4384 isn't good enough. Even if we assume the caller has tried to move the
4385 gap out of our way, it might still be at start1 exactly, for example;
4386 and that places it `inside' the interval, for our purposes. The amount
4387 of adjustment is nontrivial if there's a `denormalized' marker whose
4388 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
4389 the dirty work to Fmarker_position, below. */
4391 /* The difference between the region's lengths */
4392 diff = (end2 - start2) - (end1 - start1);
4393 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
4395 /* For shifting each marker in a region by the length of the other
4396 region plus the distance between the regions. */
4397 amt1 = (end2 - start2) + (start2 - end1);
4398 amt2 = (end1 - start1) + (start2 - end1);
4399 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
4400 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
4402 for (marker = BUF_MARKERS (current_buffer); marker; marker = marker->next)
4404 mpos = marker->bytepos;
4405 if (mpos >= start1_byte && mpos < end2_byte)
4407 if (mpos < end1_byte)
4408 mpos += amt1_byte;
4409 else if (mpos < start2_byte)
4410 mpos += diff_byte;
4411 else
4412 mpos -= amt2_byte;
4413 marker->bytepos = mpos;
4415 mpos = marker->charpos;
4416 if (mpos >= start1 && mpos < end2)
4418 if (mpos < end1)
4419 mpos += amt1;
4420 else if (mpos < start2)
4421 mpos += diff;
4422 else
4423 mpos -= amt2;
4425 marker->charpos = mpos;
4429 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
4430 doc: /* Transpose region STARTR1 to ENDR1 with STARTR2 to ENDR2.
4431 The regions should not be overlapping, because the size of the buffer is
4432 never changed in a transposition.
4434 Optional fifth arg LEAVE-MARKERS, if non-nil, means don't update
4435 any markers that happen to be located in the regions.
4437 Transposing beyond buffer boundaries is an error. */)
4438 (Lisp_Object startr1, Lisp_Object endr1, Lisp_Object startr2, Lisp_Object endr2, Lisp_Object leave_markers)
4440 register ptrdiff_t start1, end1, start2, end2;
4441 ptrdiff_t start1_byte, start2_byte, len1_byte, len2_byte;
4442 ptrdiff_t gap, len1, len_mid, len2;
4443 unsigned char *start1_addr, *start2_addr, *temp;
4445 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2, tmp_interval3;
4446 Lisp_Object buf;
4448 XSETBUFFER (buf, current_buffer);
4449 cur_intv = BUF_INTERVALS (current_buffer);
4451 validate_region (&startr1, &endr1);
4452 validate_region (&startr2, &endr2);
4454 start1 = XFASTINT (startr1);
4455 end1 = XFASTINT (endr1);
4456 start2 = XFASTINT (startr2);
4457 end2 = XFASTINT (endr2);
4458 gap = GPT;
4460 /* Swap the regions if they're reversed. */
4461 if (start2 < end1)
4463 register ptrdiff_t glumph = start1;
4464 start1 = start2;
4465 start2 = glumph;
4466 glumph = end1;
4467 end1 = end2;
4468 end2 = glumph;
4471 len1 = end1 - start1;
4472 len2 = end2 - start2;
4474 if (start2 < end1)
4475 error ("Transposed regions overlap");
4476 /* Nothing to change for adjacent regions with one being empty */
4477 else if ((start1 == end1 || start2 == end2) && end1 == start2)
4478 return Qnil;
4480 /* The possibilities are:
4481 1. Adjacent (contiguous) regions, or separate but equal regions
4482 (no, really equal, in this case!), or
4483 2. Separate regions of unequal size.
4485 The worst case is usually No. 2. It means that (aside from
4486 potential need for getting the gap out of the way), there also
4487 needs to be a shifting of the text between the two regions. So
4488 if they are spread far apart, we are that much slower... sigh. */
4490 /* It must be pointed out that the really studly thing to do would
4491 be not to move the gap at all, but to leave it in place and work
4492 around it if necessary. This would be extremely efficient,
4493 especially considering that people are likely to do
4494 transpositions near where they are working interactively, which
4495 is exactly where the gap would be found. However, such code
4496 would be much harder to write and to read. So, if you are
4497 reading this comment and are feeling squirrely, by all means have
4498 a go! I just didn't feel like doing it, so I will simply move
4499 the gap the minimum distance to get it out of the way, and then
4500 deal with an unbroken array. */
4502 /* Make sure the gap won't interfere, by moving it out of the text
4503 we will operate on. */
4504 if (start1 < gap && gap < end2)
4506 if (gap - start1 < end2 - gap)
4507 move_gap (start1);
4508 else
4509 move_gap (end2);
4512 start1_byte = CHAR_TO_BYTE (start1);
4513 start2_byte = CHAR_TO_BYTE (start2);
4514 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
4515 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
4517 #ifdef BYTE_COMBINING_DEBUG
4518 if (end1 == start2)
4520 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4521 len2_byte, start1, start1_byte)
4522 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4523 len1_byte, end2, start2_byte + len2_byte)
4524 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4525 len1_byte, end2, start2_byte + len2_byte))
4526 abort ();
4528 else
4530 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4531 len2_byte, start1, start1_byte)
4532 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4533 len1_byte, start2, start2_byte)
4534 || count_combining_after (BYTE_POS_ADDR (start2_byte),
4535 len2_byte, end1, start1_byte + len1_byte)
4536 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4537 len1_byte, end2, start2_byte + len2_byte))
4538 abort ();
4540 #endif
4542 /* Hmmm... how about checking to see if the gap is large
4543 enough to use as the temporary storage? That would avoid an
4544 allocation... interesting. Later, don't fool with it now. */
4546 /* Working without memmove, for portability (sigh), so must be
4547 careful of overlapping subsections of the array... */
4549 if (end1 == start2) /* adjacent regions */
4551 modify_region (current_buffer, start1, end2, 0);
4552 record_change (start1, len1 + len2);
4554 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4555 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4556 /* Don't use Fset_text_properties: that can cause GC, which can
4557 clobber objects stored in the tmp_intervals. */
4558 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4559 if (!NULL_INTERVAL_P (tmp_interval3))
4560 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4562 /* First region smaller than second. */
4563 if (len1_byte < len2_byte)
4565 USE_SAFE_ALLOCA;
4567 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4569 /* Don't precompute these addresses. We have to compute them
4570 at the last minute, because the relocating allocator might
4571 have moved the buffer around during the xmalloc. */
4572 start1_addr = BYTE_POS_ADDR (start1_byte);
4573 start2_addr = BYTE_POS_ADDR (start2_byte);
4575 memcpy (temp, start2_addr, len2_byte);
4576 memcpy (start1_addr + len2_byte, start1_addr, len1_byte);
4577 memcpy (start1_addr, temp, len2_byte);
4578 SAFE_FREE ();
4580 else
4581 /* First region not smaller than second. */
4583 USE_SAFE_ALLOCA;
4585 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4586 start1_addr = BYTE_POS_ADDR (start1_byte);
4587 start2_addr = BYTE_POS_ADDR (start2_byte);
4588 memcpy (temp, start1_addr, len1_byte);
4589 memcpy (start1_addr, start2_addr, len2_byte);
4590 memcpy (start1_addr + len2_byte, temp, len1_byte);
4591 SAFE_FREE ();
4593 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
4594 len1, current_buffer, 0);
4595 graft_intervals_into_buffer (tmp_interval2, start1,
4596 len2, current_buffer, 0);
4597 update_compositions (start1, start1 + len2, CHECK_BORDER);
4598 update_compositions (start1 + len2, end2, CHECK_TAIL);
4600 /* Non-adjacent regions, because end1 != start2, bleagh... */
4601 else
4603 len_mid = start2_byte - (start1_byte + len1_byte);
4605 if (len1_byte == len2_byte)
4606 /* Regions are same size, though, how nice. */
4608 USE_SAFE_ALLOCA;
4610 modify_region (current_buffer, start1, end1, 0);
4611 modify_region (current_buffer, start2, end2, 0);
4612 record_change (start1, len1);
4613 record_change (start2, len2);
4614 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4615 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4617 tmp_interval3 = validate_interval_range (buf, &startr1, &endr1, 0);
4618 if (!NULL_INTERVAL_P (tmp_interval3))
4619 set_text_properties_1 (startr1, endr1, Qnil, buf, tmp_interval3);
4621 tmp_interval3 = validate_interval_range (buf, &startr2, &endr2, 0);
4622 if (!NULL_INTERVAL_P (tmp_interval3))
4623 set_text_properties_1 (startr2, endr2, Qnil, buf, tmp_interval3);
4625 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4626 start1_addr = BYTE_POS_ADDR (start1_byte);
4627 start2_addr = BYTE_POS_ADDR (start2_byte);
4628 memcpy (temp, start1_addr, len1_byte);
4629 memcpy (start1_addr, start2_addr, len2_byte);
4630 memcpy (start2_addr, temp, len1_byte);
4631 SAFE_FREE ();
4633 graft_intervals_into_buffer (tmp_interval1, start2,
4634 len1, current_buffer, 0);
4635 graft_intervals_into_buffer (tmp_interval2, start1,
4636 len2, current_buffer, 0);
4639 else if (len1_byte < len2_byte) /* Second region larger than first */
4640 /* Non-adjacent & unequal size, area between must also be shifted. */
4642 USE_SAFE_ALLOCA;
4644 modify_region (current_buffer, start1, end2, 0);
4645 record_change (start1, (end2 - start1));
4646 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4647 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4648 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4650 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4651 if (!NULL_INTERVAL_P (tmp_interval3))
4652 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4654 /* holds region 2 */
4655 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4656 start1_addr = BYTE_POS_ADDR (start1_byte);
4657 start2_addr = BYTE_POS_ADDR (start2_byte);
4658 memcpy (temp, start2_addr, len2_byte);
4659 memcpy (start1_addr + len_mid + len2_byte, start1_addr, len1_byte);
4660 memmove (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4661 memcpy (start1_addr, temp, len2_byte);
4662 SAFE_FREE ();
4664 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4665 len1, current_buffer, 0);
4666 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4667 len_mid, current_buffer, 0);
4668 graft_intervals_into_buffer (tmp_interval2, start1,
4669 len2, current_buffer, 0);
4671 else
4672 /* Second region smaller than first. */
4674 USE_SAFE_ALLOCA;
4676 record_change (start1, (end2 - start1));
4677 modify_region (current_buffer, start1, end2, 0);
4679 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4680 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4681 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4683 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4684 if (!NULL_INTERVAL_P (tmp_interval3))
4685 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4687 /* holds region 1 */
4688 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4689 start1_addr = BYTE_POS_ADDR (start1_byte);
4690 start2_addr = BYTE_POS_ADDR (start2_byte);
4691 memcpy (temp, start1_addr, len1_byte);
4692 memcpy (start1_addr, start2_addr, len2_byte);
4693 memcpy (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4694 memcpy (start1_addr + len2_byte + len_mid, temp, len1_byte);
4695 SAFE_FREE ();
4697 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4698 len1, current_buffer, 0);
4699 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4700 len_mid, current_buffer, 0);
4701 graft_intervals_into_buffer (tmp_interval2, start1,
4702 len2, current_buffer, 0);
4705 update_compositions (start1, start1 + len2, CHECK_BORDER);
4706 update_compositions (end2 - len1, end2, CHECK_BORDER);
4709 /* When doing multiple transpositions, it might be nice
4710 to optimize this. Perhaps the markers in any one buffer
4711 should be organized in some sorted data tree. */
4712 if (NILP (leave_markers))
4714 transpose_markers (start1, end1, start2, end2,
4715 start1_byte, start1_byte + len1_byte,
4716 start2_byte, start2_byte + len2_byte);
4717 fix_start_end_in_overlays (start1, end2);
4720 signal_after_change (start1, end2 - start1, end2 - start1);
4721 return Qnil;
4725 void
4726 syms_of_editfns (void)
4728 environbuf = 0;
4729 initial_tz = 0;
4731 DEFSYM (Qbuffer_access_fontify_functions, "buffer-access-fontify-functions");
4733 DEFVAR_LISP ("inhibit-field-text-motion", Vinhibit_field_text_motion,
4734 doc: /* Non-nil means text motion commands don't notice fields. */);
4735 Vinhibit_field_text_motion = Qnil;
4737 DEFVAR_LISP ("buffer-access-fontify-functions",
4738 Vbuffer_access_fontify_functions,
4739 doc: /* List of functions called by `buffer-substring' to fontify if necessary.
4740 Each function is called with two arguments which specify the range
4741 of the buffer being accessed. */);
4742 Vbuffer_access_fontify_functions = Qnil;
4745 Lisp_Object obuf;
4746 obuf = Fcurrent_buffer ();
4747 /* Do this here, because init_buffer_once is too early--it won't work. */
4748 Fset_buffer (Vprin1_to_string_buffer);
4749 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
4750 Fset (Fmake_local_variable (intern_c_string ("buffer-access-fontify-functions")),
4751 Qnil);
4752 Fset_buffer (obuf);
4755 DEFVAR_LISP ("buffer-access-fontified-property",
4756 Vbuffer_access_fontified_property,
4757 doc: /* Property which (if non-nil) indicates text has been fontified.
4758 `buffer-substring' need not call the `buffer-access-fontify-functions'
4759 functions if all the text being accessed has this property. */);
4760 Vbuffer_access_fontified_property = Qnil;
4762 DEFVAR_LISP ("system-name", Vsystem_name,
4763 doc: /* The host name of the machine Emacs is running on. */);
4765 DEFVAR_LISP ("user-full-name", Vuser_full_name,
4766 doc: /* The full name of the user logged in. */);
4768 DEFVAR_LISP ("user-login-name", Vuser_login_name,
4769 doc: /* The user's name, taken from environment variables if possible. */);
4771 DEFVAR_LISP ("user-real-login-name", Vuser_real_login_name,
4772 doc: /* The user's name, based upon the real uid only. */);
4774 DEFVAR_LISP ("operating-system-release", Voperating_system_release,
4775 doc: /* The release of the operating system Emacs is running on. */);
4777 defsubr (&Spropertize);
4778 defsubr (&Schar_equal);
4779 defsubr (&Sgoto_char);
4780 defsubr (&Sstring_to_char);
4781 defsubr (&Schar_to_string);
4782 defsubr (&Sbyte_to_string);
4783 defsubr (&Sbuffer_substring);
4784 defsubr (&Sbuffer_substring_no_properties);
4785 defsubr (&Sbuffer_string);
4787 defsubr (&Spoint_marker);
4788 defsubr (&Smark_marker);
4789 defsubr (&Spoint);
4790 defsubr (&Sregion_beginning);
4791 defsubr (&Sregion_end);
4793 DEFSYM (Qfield, "field");
4794 DEFSYM (Qboundary, "boundary");
4795 defsubr (&Sfield_beginning);
4796 defsubr (&Sfield_end);
4797 defsubr (&Sfield_string);
4798 defsubr (&Sfield_string_no_properties);
4799 defsubr (&Sdelete_field);
4800 defsubr (&Sconstrain_to_field);
4802 defsubr (&Sline_beginning_position);
4803 defsubr (&Sline_end_position);
4805 /* defsubr (&Smark); */
4806 /* defsubr (&Sset_mark); */
4807 defsubr (&Ssave_excursion);
4808 defsubr (&Ssave_current_buffer);
4810 defsubr (&Sbufsize);
4811 defsubr (&Spoint_max);
4812 defsubr (&Spoint_min);
4813 defsubr (&Spoint_min_marker);
4814 defsubr (&Spoint_max_marker);
4815 defsubr (&Sgap_position);
4816 defsubr (&Sgap_size);
4817 defsubr (&Sposition_bytes);
4818 defsubr (&Sbyte_to_position);
4820 defsubr (&Sbobp);
4821 defsubr (&Seobp);
4822 defsubr (&Sbolp);
4823 defsubr (&Seolp);
4824 defsubr (&Sfollowing_char);
4825 defsubr (&Sprevious_char);
4826 defsubr (&Schar_after);
4827 defsubr (&Schar_before);
4828 defsubr (&Sinsert);
4829 defsubr (&Sinsert_before_markers);
4830 defsubr (&Sinsert_and_inherit);
4831 defsubr (&Sinsert_and_inherit_before_markers);
4832 defsubr (&Sinsert_char);
4833 defsubr (&Sinsert_byte);
4835 defsubr (&Suser_login_name);
4836 defsubr (&Suser_real_login_name);
4837 defsubr (&Suser_uid);
4838 defsubr (&Suser_real_uid);
4839 defsubr (&Suser_full_name);
4840 defsubr (&Semacs_pid);
4841 defsubr (&Scurrent_time);
4842 defsubr (&Sget_internal_run_time);
4843 defsubr (&Sformat_time_string);
4844 defsubr (&Sfloat_time);
4845 defsubr (&Sdecode_time);
4846 defsubr (&Sencode_time);
4847 defsubr (&Scurrent_time_string);
4848 defsubr (&Scurrent_time_zone);
4849 defsubr (&Sset_time_zone_rule);
4850 defsubr (&Ssystem_name);
4851 defsubr (&Smessage);
4852 defsubr (&Smessage_box);
4853 defsubr (&Smessage_or_box);
4854 defsubr (&Scurrent_message);
4855 defsubr (&Sformat);
4857 defsubr (&Sinsert_buffer_substring);
4858 defsubr (&Scompare_buffer_substrings);
4859 defsubr (&Ssubst_char_in_region);
4860 defsubr (&Stranslate_region_internal);
4861 defsubr (&Sdelete_region);
4862 defsubr (&Sdelete_and_extract_region);
4863 defsubr (&Swiden);
4864 defsubr (&Snarrow_to_region);
4865 defsubr (&Ssave_restriction);
4866 defsubr (&Stranspose_regions);