lib-src/makefile.w32-in (CTAGS_CFLAGS): Remove EMACS_NAME.
[emacs/old-mirror.git] / src / editfns.c
blob4c3b80be14a225cb61fcc5c5a52248584e7b0011
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 "character.h"
56 #include "buffer.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 Lisp_Object format_time_string (char const *, ptrdiff_t, EMACS_TIME,
77 int, struct tm *);
78 static int tm_diff (struct tm *, struct tm *);
79 static void update_buffer_properties (ptrdiff_t, ptrdiff_t);
81 static Lisp_Object Qbuffer_access_fontify_functions;
82 static Lisp_Object Fuser_full_name (Lisp_Object);
84 /* Symbol for the text property used to mark fields. */
86 Lisp_Object Qfield;
88 /* A special value for Qfield properties. */
90 static Lisp_Object Qboundary;
93 void
94 init_editfns (void)
96 const char *user_name;
97 register char *p;
98 struct passwd *pw; /* password entry for the current user */
99 Lisp_Object tem;
101 /* Set up system_name even when dumping. */
102 init_system_name ();
104 #ifndef CANNOT_DUMP
105 /* Don't bother with this on initial start when just dumping out */
106 if (!initialized)
107 return;
108 #endif /* not CANNOT_DUMP */
110 pw = getpwuid (getuid ());
111 #ifdef MSDOS
112 /* We let the real user name default to "root" because that's quite
113 accurate on MSDOG and because it lets Emacs find the init file.
114 (The DVX libraries override the Djgpp libraries here.) */
115 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
116 #else
117 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
118 #endif
120 /* Get the effective user name, by consulting environment variables,
121 or the effective uid if those are unset. */
122 user_name = getenv ("LOGNAME");
123 if (!user_name)
124 #ifdef WINDOWSNT
125 user_name = getenv ("USERNAME"); /* it's USERNAME on NT */
126 #else /* WINDOWSNT */
127 user_name = getenv ("USER");
128 #endif /* WINDOWSNT */
129 if (!user_name)
131 pw = getpwuid (geteuid ());
132 user_name = pw ? pw->pw_name : "unknown";
134 Vuser_login_name = build_string (user_name);
136 /* If the user name claimed in the environment vars differs from
137 the real uid, use the claimed name to find the full name. */
138 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
139 if (! NILP (tem))
140 tem = Vuser_login_name;
141 else
143 uid_t euid = geteuid ();
144 tem = make_fixnum_or_float (euid);
146 Vuser_full_name = Fuser_full_name (tem);
148 p = getenv ("NAME");
149 if (p)
150 Vuser_full_name = build_string (p);
151 else if (NILP (Vuser_full_name))
152 Vuser_full_name = build_string ("unknown");
154 #ifdef HAVE_SYS_UTSNAME_H
156 struct utsname uts;
157 uname (&uts);
158 Voperating_system_release = build_string (uts.release);
160 #else
161 Voperating_system_release = Qnil;
162 #endif
165 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
166 doc: /* Convert arg CHAR to a string containing that character.
167 usage: (char-to-string CHAR) */)
168 (Lisp_Object character)
170 int c, len;
171 unsigned char str[MAX_MULTIBYTE_LENGTH];
173 CHECK_CHARACTER (character);
174 c = XFASTINT (character);
176 len = CHAR_STRING (c, str);
177 return make_string_from_bytes ((char *) str, 1, len);
180 DEFUN ("byte-to-string", Fbyte_to_string, Sbyte_to_string, 1, 1, 0,
181 doc: /* Convert arg BYTE to a unibyte string containing that byte. */)
182 (Lisp_Object byte)
184 unsigned char b;
185 CHECK_NUMBER (byte);
186 if (XINT (byte) < 0 || XINT (byte) > 255)
187 error ("Invalid byte");
188 b = XINT (byte);
189 return make_string_from_bytes ((char *) &b, 1, 1);
192 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
193 doc: /* Return the first character in STRING. */)
194 (register Lisp_Object string)
196 register Lisp_Object val;
197 CHECK_STRING (string);
198 if (SCHARS (string))
200 if (STRING_MULTIBYTE (string))
201 XSETFASTINT (val, STRING_CHAR (SDATA (string)));
202 else
203 XSETFASTINT (val, SREF (string, 0));
205 else
206 XSETFASTINT (val, 0);
207 return val;
210 static Lisp_Object
211 buildmark (ptrdiff_t charpos, ptrdiff_t bytepos)
213 register Lisp_Object mark;
214 mark = Fmake_marker ();
215 set_marker_both (mark, Qnil, charpos, bytepos);
216 return mark;
219 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
220 doc: /* Return value of point, as an integer.
221 Beginning of buffer is position (point-min). */)
222 (void)
224 Lisp_Object temp;
225 XSETFASTINT (temp, PT);
226 return temp;
229 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
230 doc: /* Return value of point, as a marker object. */)
231 (void)
233 return buildmark (PT, PT_BYTE);
236 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
237 doc: /* Set point to POSITION, a number or marker.
238 Beginning of buffer is position (point-min), end is (point-max).
240 The return value is POSITION. */)
241 (register Lisp_Object position)
243 ptrdiff_t pos;
245 if (MARKERP (position)
246 && current_buffer == XMARKER (position)->buffer)
248 pos = marker_position (position);
249 if (pos < BEGV)
250 SET_PT_BOTH (BEGV, BEGV_BYTE);
251 else if (pos > ZV)
252 SET_PT_BOTH (ZV, ZV_BYTE);
253 else
254 SET_PT_BOTH (pos, marker_byte_position (position));
256 return position;
259 CHECK_NUMBER_COERCE_MARKER (position);
261 pos = clip_to_bounds (BEGV, XINT (position), ZV);
262 SET_PT (pos);
263 return position;
267 /* Return the start or end position of the region.
268 BEGINNINGP non-zero means return the start.
269 If there is no region active, signal an error. */
271 static Lisp_Object
272 region_limit (int beginningp)
274 Lisp_Object m;
276 if (!NILP (Vtransient_mark_mode)
277 && NILP (Vmark_even_if_inactive)
278 && NILP (BVAR (current_buffer, mark_active)))
279 xsignal0 (Qmark_inactive);
281 m = Fmarker_position (BVAR (current_buffer, mark));
282 if (NILP (m))
283 error ("The mark is not set now, so there is no region");
285 if ((PT < XFASTINT (m)) == (beginningp != 0))
286 return make_number (PT);
287 else
288 { /* Clip to the current narrowing (bug#11770). */
289 ptrdiff_t mark = XFASTINT (m);
290 return make_number (mark < BEGV ? BEGV : mark > ZV ? ZV : mark);
294 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
295 doc: /* Return the integer value of point or mark, whichever is smaller. */)
296 (void)
298 return region_limit (1);
301 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
302 doc: /* Return the integer value of point or mark, whichever is larger. */)
303 (void)
305 return region_limit (0);
308 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
309 doc: /* Return this buffer's mark, as a marker object.
310 Watch out! Moving this marker changes the mark position.
311 If you set the marker not to point anywhere, the buffer will have no mark. */)
312 (void)
314 return BVAR (current_buffer, mark);
318 /* Find all the overlays in the current buffer that touch position POS.
319 Return the number found, and store them in a vector in VEC
320 of length LEN. */
322 static ptrdiff_t
323 overlays_around (EMACS_INT pos, Lisp_Object *vec, ptrdiff_t len)
325 Lisp_Object overlay, start, end;
326 struct Lisp_Overlay *tail;
327 ptrdiff_t startpos, endpos;
328 ptrdiff_t idx = 0;
330 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
332 XSETMISC (overlay, tail);
334 end = OVERLAY_END (overlay);
335 endpos = OVERLAY_POSITION (end);
336 if (endpos < pos)
337 break;
338 start = OVERLAY_START (overlay);
339 startpos = OVERLAY_POSITION (start);
340 if (startpos <= pos)
342 if (idx < len)
343 vec[idx] = overlay;
344 /* Keep counting overlays even if we can't return them all. */
345 idx++;
349 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
351 XSETMISC (overlay, tail);
353 start = OVERLAY_START (overlay);
354 startpos = OVERLAY_POSITION (start);
355 if (pos < startpos)
356 break;
357 end = OVERLAY_END (overlay);
358 endpos = OVERLAY_POSITION (end);
359 if (pos <= endpos)
361 if (idx < len)
362 vec[idx] = overlay;
363 idx++;
367 return idx;
370 /* Return the value of property PROP, in OBJECT at POSITION.
371 It's the value of PROP that a char inserted at POSITION would get.
372 OBJECT is optional and defaults to the current buffer.
373 If OBJECT is a buffer, then overlay properties are considered as well as
374 text properties.
375 If OBJECT is a window, then that window's buffer is used, but
376 window-specific overlays are considered only if they are associated
377 with OBJECT. */
378 Lisp_Object
379 get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object object)
381 CHECK_NUMBER_COERCE_MARKER (position);
383 if (NILP (object))
384 XSETBUFFER (object, current_buffer);
385 else if (WINDOWP (object))
386 object = XWINDOW (object)->buffer;
388 if (!BUFFERP (object))
389 /* pos-property only makes sense in buffers right now, since strings
390 have no overlays and no notion of insertion for which stickiness
391 could be obeyed. */
392 return Fget_text_property (position, prop, object);
393 else
395 EMACS_INT posn = XINT (position);
396 ptrdiff_t noverlays;
397 Lisp_Object *overlay_vec, tem;
398 struct buffer *obuf = current_buffer;
400 set_buffer_temp (XBUFFER (object));
402 /* First try with room for 40 overlays. */
403 noverlays = 40;
404 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
405 noverlays = overlays_around (posn, overlay_vec, noverlays);
407 /* If there are more than 40,
408 make enough space for all, and try again. */
409 if (noverlays > 40)
411 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
412 noverlays = overlays_around (posn, overlay_vec, noverlays);
414 noverlays = sort_overlays (overlay_vec, noverlays, NULL);
416 set_buffer_temp (obuf);
418 /* Now check the overlays in order of decreasing priority. */
419 while (--noverlays >= 0)
421 Lisp_Object ol = overlay_vec[noverlays];
422 tem = Foverlay_get (ol, prop);
423 if (!NILP (tem))
425 /* Check the overlay is indeed active at point. */
426 Lisp_Object start = OVERLAY_START (ol), finish = OVERLAY_END (ol);
427 if ((OVERLAY_POSITION (start) == posn
428 && XMARKER (start)->insertion_type == 1)
429 || (OVERLAY_POSITION (finish) == posn
430 && XMARKER (finish)->insertion_type == 0))
431 ; /* The overlay will not cover a char inserted at point. */
432 else
434 return tem;
439 { /* Now check the text properties. */
440 int stickiness = text_property_stickiness (prop, position, object);
441 if (stickiness > 0)
442 return Fget_text_property (position, prop, object);
443 else if (stickiness < 0
444 && XINT (position) > BUF_BEGV (XBUFFER (object)))
445 return Fget_text_property (make_number (XINT (position) - 1),
446 prop, object);
447 else
448 return Qnil;
453 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
454 the value of point is used instead. If BEG or END is null,
455 means don't store the beginning or end of the field.
457 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
458 results; they do not effect boundary behavior.
460 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
461 position of a field, then the beginning of the previous field is
462 returned instead of the beginning of POS's field (since the end of a
463 field is actually also the beginning of the next input field, this
464 behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
465 true case, if two fields are separated by a field with the special
466 value `boundary', and POS lies within it, then the two separated
467 fields are considered to be adjacent, and POS between them, when
468 finding the beginning and ending of the "merged" field.
470 Either BEG or END may be 0, in which case the corresponding value
471 is not stored. */
473 static void
474 find_field (Lisp_Object pos, Lisp_Object merge_at_boundary,
475 Lisp_Object beg_limit,
476 ptrdiff_t *beg, Lisp_Object end_limit, ptrdiff_t *end)
478 /* Fields right before and after the point. */
479 Lisp_Object before_field, after_field;
480 /* 1 if POS counts as the start of a field. */
481 int at_field_start = 0;
482 /* 1 if POS counts as the end of a field. */
483 int at_field_end = 0;
485 if (NILP (pos))
486 XSETFASTINT (pos, PT);
487 else
488 CHECK_NUMBER_COERCE_MARKER (pos);
490 after_field
491 = get_char_property_and_overlay (pos, Qfield, Qnil, NULL);
492 before_field
493 = (XFASTINT (pos) > BEGV
494 ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
495 Qfield, Qnil, NULL)
496 /* Using nil here would be a more obvious choice, but it would
497 fail when the buffer starts with a non-sticky field. */
498 : after_field);
500 /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil
501 and POS is at beginning of a field, which can also be interpreted
502 as the end of the previous field. Note that the case where if
503 MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
504 more natural one; then we avoid treating the beginning of a field
505 specially. */
506 if (NILP (merge_at_boundary))
508 Lisp_Object field = get_pos_property (pos, Qfield, Qnil);
509 if (!EQ (field, after_field))
510 at_field_end = 1;
511 if (!EQ (field, before_field))
512 at_field_start = 1;
513 if (NILP (field) && at_field_start && at_field_end)
514 /* If an inserted char would have a nil field while the surrounding
515 text is non-nil, we're probably not looking at a
516 zero-length field, but instead at a non-nil field that's
517 not intended for editing (such as comint's prompts). */
518 at_field_end = at_field_start = 0;
521 /* Note about special `boundary' fields:
523 Consider the case where the point (`.') is between the fields `x' and `y':
525 xxxx.yyyy
527 In this situation, if merge_at_boundary is true, we consider the
528 `x' and `y' fields as forming one big merged field, and so the end
529 of the field is the end of `y'.
531 However, if `x' and `y' are separated by a special `boundary' field
532 (a field with a `field' char-property of 'boundary), then we ignore
533 this special field when merging adjacent fields. Here's the same
534 situation, but with a `boundary' field between the `x' and `y' fields:
536 xxx.BBBByyyy
538 Here, if point is at the end of `x', the beginning of `y', or
539 anywhere in-between (within the `boundary' field), we merge all
540 three fields and consider the beginning as being the beginning of
541 the `x' field, and the end as being the end of the `y' field. */
543 if (beg)
545 if (at_field_start)
546 /* POS is at the edge of a field, and we should consider it as
547 the beginning of the following field. */
548 *beg = XFASTINT (pos);
549 else
550 /* Find the previous field boundary. */
552 Lisp_Object p = pos;
553 if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary))
554 /* Skip a `boundary' field. */
555 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
556 beg_limit);
558 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
559 beg_limit);
560 *beg = NILP (p) ? BEGV : XFASTINT (p);
564 if (end)
566 if (at_field_end)
567 /* POS is at the edge of a field, and we should consider it as
568 the end of the previous field. */
569 *end = XFASTINT (pos);
570 else
571 /* Find the next field boundary. */
573 if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary))
574 /* Skip a `boundary' field. */
575 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
576 end_limit);
578 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
579 end_limit);
580 *end = NILP (pos) ? ZV : XFASTINT (pos);
586 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
587 doc: /* Delete the field surrounding POS.
588 A field is a region of text with the same `field' property.
589 If POS is nil, the value of point is used for POS. */)
590 (Lisp_Object pos)
592 ptrdiff_t beg, end;
593 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
594 if (beg != end)
595 del_range (beg, end);
596 return Qnil;
599 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
600 doc: /* Return the contents of the field surrounding POS as a string.
601 A field is a region of text with the same `field' property.
602 If POS is nil, the value of point is used for POS. */)
603 (Lisp_Object pos)
605 ptrdiff_t beg, end;
606 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
607 return make_buffer_string (beg, end, 1);
610 DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
611 doc: /* Return the contents of the field around POS, without text properties.
612 A field is a region of text with the same `field' property.
613 If POS is nil, the value of point is used for POS. */)
614 (Lisp_Object pos)
616 ptrdiff_t beg, end;
617 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
618 return make_buffer_string (beg, end, 0);
621 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
622 doc: /* Return the beginning of the field surrounding POS.
623 A field is a region of text with the same `field' property.
624 If POS is nil, the value of point is used for POS.
625 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
626 field, then the beginning of the *previous* field is returned.
627 If LIMIT is non-nil, it is a buffer position; if the beginning of the field
628 is before LIMIT, then LIMIT will be returned instead. */)
629 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
631 ptrdiff_t beg;
632 find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
633 return make_number (beg);
636 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
637 doc: /* Return the end of the field surrounding POS.
638 A field is a region of text with the same `field' property.
639 If POS is nil, the value of point is used for POS.
640 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
641 then the end of the *following* field is returned.
642 If LIMIT is non-nil, it is a buffer position; if the end of the field
643 is after LIMIT, then LIMIT will be returned instead. */)
644 (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
646 ptrdiff_t end;
647 find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
648 return make_number (end);
651 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
652 doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
653 A field is a region of text with the same `field' property.
655 If NEW-POS is nil, then use the current point instead, and move point
656 to the resulting constrained position, in addition to returning that
657 position.
659 If OLD-POS is at the boundary of two fields, then the allowable
660 positions for NEW-POS depends on the value of the optional argument
661 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
662 constrained to the field that has the same `field' char-property
663 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
664 is non-nil, NEW-POS is constrained to the union of the two adjacent
665 fields. Additionally, if two fields are separated by another field with
666 the special value `boundary', then any point within this special field is
667 also considered to be `on the boundary'.
669 If the optional argument ONLY-IN-LINE is non-nil and constraining
670 NEW-POS would move it to a different line, NEW-POS is returned
671 unconstrained. This useful for commands that move by line, like
672 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
673 only in the case where they can still move to the right line.
675 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
676 a non-nil property of that name, then any field boundaries are ignored.
678 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */)
679 (Lisp_Object new_pos, Lisp_Object old_pos, Lisp_Object escape_from_edge, Lisp_Object only_in_line, Lisp_Object inhibit_capture_property)
681 /* If non-zero, then the original point, before re-positioning. */
682 ptrdiff_t orig_point = 0;
683 int fwd;
684 Lisp_Object prev_old, prev_new;
686 if (NILP (new_pos))
687 /* Use the current point, and afterwards, set it. */
689 orig_point = PT;
690 XSETFASTINT (new_pos, PT);
693 CHECK_NUMBER_COERCE_MARKER (new_pos);
694 CHECK_NUMBER_COERCE_MARKER (old_pos);
696 fwd = (XINT (new_pos) > XINT (old_pos));
698 prev_old = make_number (XINT (old_pos) - 1);
699 prev_new = make_number (XINT (new_pos) - 1);
701 if (NILP (Vinhibit_field_text_motion)
702 && !EQ (new_pos, old_pos)
703 && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
704 || !NILP (Fget_char_property (old_pos, Qfield, Qnil))
705 /* To recognize field boundaries, we must also look at the
706 previous positions; we could use `get_pos_property'
707 instead, but in itself that would fail inside non-sticky
708 fields (like comint prompts). */
709 || (XFASTINT (new_pos) > BEGV
710 && !NILP (Fget_char_property (prev_new, Qfield, Qnil)))
711 || (XFASTINT (old_pos) > BEGV
712 && !NILP (Fget_char_property (prev_old, Qfield, Qnil))))
713 && (NILP (inhibit_capture_property)
714 /* Field boundaries are again a problem; but now we must
715 decide the case exactly, so we need to call
716 `get_pos_property' as well. */
717 || (NILP (get_pos_property (old_pos, inhibit_capture_property, Qnil))
718 && (XFASTINT (old_pos) <= BEGV
719 || NILP (Fget_char_property (old_pos, inhibit_capture_property, Qnil))
720 || NILP (Fget_char_property (prev_old, inhibit_capture_property, Qnil))))))
721 /* It is possible that NEW_POS is not within the same field as
722 OLD_POS; try to move NEW_POS so that it is. */
724 ptrdiff_t shortage;
725 Lisp_Object field_bound;
727 if (fwd)
728 field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
729 else
730 field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
732 if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
733 other side of NEW_POS, which would mean that NEW_POS is
734 already acceptable, and it's not necessary to constrain it
735 to FIELD_BOUND. */
736 ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd)
737 /* NEW_POS should be constrained, but only if either
738 ONLY_IN_LINE is nil (in which case any constraint is OK),
739 or NEW_POS and FIELD_BOUND are on the same line (in which
740 case the constraint is OK even if ONLY_IN_LINE is non-nil). */
741 && (NILP (only_in_line)
742 /* This is the ONLY_IN_LINE case, check that NEW_POS and
743 FIELD_BOUND are on the same line by seeing whether
744 there's an intervening newline or not. */
745 || (scan_buffer ('\n',
746 XFASTINT (new_pos), XFASTINT (field_bound),
747 fwd ? -1 : 1, &shortage, 1),
748 shortage != 0)))
749 /* Constrain NEW_POS to FIELD_BOUND. */
750 new_pos = field_bound;
752 if (orig_point && XFASTINT (new_pos) != orig_point)
753 /* The NEW_POS argument was originally nil, so automatically set PT. */
754 SET_PT (XFASTINT (new_pos));
757 return new_pos;
761 DEFUN ("line-beginning-position",
762 Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
763 doc: /* Return the character position of the first character on the current line.
764 With argument N not nil or 1, move forward N - 1 lines first.
765 If scan reaches end of buffer, return that position.
767 The returned position is of the first character in the logical order,
768 i.e. the one that has the smallest character position.
770 This function constrains the returned position to the current field
771 unless that would be on a different line than the original,
772 unconstrained result. If N is nil or 1, and a front-sticky field
773 starts at point, the scan stops as soon as it starts. To ignore field
774 boundaries bind `inhibit-field-text-motion' to t.
776 This function does not move point. */)
777 (Lisp_Object n)
779 ptrdiff_t orig, orig_byte, end;
780 ptrdiff_t count = SPECPDL_INDEX ();
781 specbind (Qinhibit_point_motion_hooks, Qt);
783 if (NILP (n))
784 XSETFASTINT (n, 1);
785 else
786 CHECK_NUMBER (n);
788 orig = PT;
789 orig_byte = PT_BYTE;
790 Fforward_line (make_number (XINT (n) - 1));
791 end = PT;
793 SET_PT_BOTH (orig, orig_byte);
795 unbind_to (count, Qnil);
797 /* Return END constrained to the current input field. */
798 return Fconstrain_to_field (make_number (end), make_number (orig),
799 XINT (n) != 1 ? Qt : Qnil,
800 Qt, Qnil);
803 DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0,
804 doc: /* Return the character position of the last character on the current line.
805 With argument N not nil or 1, move forward N - 1 lines first.
806 If scan reaches end of buffer, return that position.
808 The returned position is of the last character in the logical order,
809 i.e. the character whose buffer position is the largest one.
811 This function constrains the returned position to the current field
812 unless that would be on a different line than the original,
813 unconstrained result. If N is nil or 1, and a rear-sticky field ends
814 at point, the scan stops as soon as it starts. To ignore field
815 boundaries bind `inhibit-field-text-motion' to t.
817 This function does not move point. */)
818 (Lisp_Object n)
820 ptrdiff_t clipped_n;
821 ptrdiff_t end_pos;
822 ptrdiff_t orig = PT;
824 if (NILP (n))
825 XSETFASTINT (n, 1);
826 else
827 CHECK_NUMBER (n);
829 clipped_n = clip_to_bounds (PTRDIFF_MIN + 1, XINT (n), PTRDIFF_MAX);
830 end_pos = find_before_next_newline (orig, 0, clipped_n - (clipped_n <= 0));
832 /* Return END_POS constrained to the current input field. */
833 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
834 Qnil, Qt, Qnil);
838 Lisp_Object
839 save_excursion_save (void)
841 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
842 == current_buffer);
844 return Fcons (Fpoint_marker (),
845 Fcons (Fcopy_marker (BVAR (current_buffer, mark), Qnil),
846 Fcons (visible ? Qt : Qnil,
847 Fcons (BVAR (current_buffer, mark_active),
848 selected_window))));
851 Lisp_Object
852 save_excursion_restore (Lisp_Object info)
854 Lisp_Object tem, tem1, omark, nmark;
855 struct gcpro gcpro1, gcpro2, gcpro3;
856 int visible_p;
858 tem = Fmarker_buffer (XCAR (info));
859 /* If buffer being returned to is now deleted, avoid error */
860 /* Otherwise could get error here while unwinding to top level
861 and crash */
862 /* In that case, Fmarker_buffer returns nil now. */
863 if (NILP (tem))
864 return Qnil;
866 omark = nmark = Qnil;
867 GCPRO3 (info, omark, nmark);
869 Fset_buffer (tem);
871 /* Point marker. */
872 tem = XCAR (info);
873 Fgoto_char (tem);
874 unchain_marker (XMARKER (tem));
876 /* Mark marker. */
877 info = XCDR (info);
878 tem = XCAR (info);
879 omark = Fmarker_position (BVAR (current_buffer, mark));
880 Fset_marker (BVAR (current_buffer, mark), tem, Fcurrent_buffer ());
881 nmark = Fmarker_position (tem);
882 unchain_marker (XMARKER (tem));
884 /* visible */
885 info = XCDR (info);
886 visible_p = !NILP (XCAR (info));
888 #if 0 /* We used to make the current buffer visible in the selected window
889 if that was true previously. That avoids some anomalies.
890 But it creates others, and it wasn't documented, and it is simpler
891 and cleaner never to alter the window/buffer connections. */
892 tem1 = Fcar (tem);
893 if (!NILP (tem1)
894 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
895 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
896 #endif /* 0 */
898 /* Mark active */
899 info = XCDR (info);
900 tem = XCAR (info);
901 tem1 = BVAR (current_buffer, mark_active);
902 BVAR (current_buffer, mark_active) = tem;
904 /* If mark is active now, and either was not active
905 or was at a different place, run the activate hook. */
906 if (! NILP (tem))
908 if (! EQ (omark, nmark))
910 tem = intern ("activate-mark-hook");
911 Frun_hooks (1, &tem);
914 /* If mark has ceased to be active, run deactivate hook. */
915 else if (! NILP (tem1))
917 tem = intern ("deactivate-mark-hook");
918 Frun_hooks (1, &tem);
921 /* If buffer was visible in a window, and a different window was
922 selected, and the old selected window is still showing this
923 buffer, restore point in that window. */
924 tem = XCDR (info);
925 if (visible_p
926 && !EQ (tem, selected_window)
927 && (tem1 = XWINDOW (tem)->buffer,
928 (/* Window is live... */
929 BUFFERP (tem1)
930 /* ...and it shows the current buffer. */
931 && XBUFFER (tem1) == current_buffer)))
932 Fset_window_point (tem, make_number (PT));
934 UNGCPRO;
935 return Qnil;
938 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
939 doc: /* Save point, mark, and current buffer; execute BODY; restore those things.
940 Executes BODY just like `progn'.
941 The values of point, mark and the current buffer are restored
942 even in case of abnormal exit (throw or error).
943 The state of activation of the mark is also restored.
945 This construct does not save `deactivate-mark', and therefore
946 functions that change the buffer will still cause deactivation
947 of the mark at the end of the command. To prevent that, bind
948 `deactivate-mark' with `let'.
950 If you only want to save the current buffer but not point nor mark,
951 then just use `save-current-buffer', or even `with-current-buffer'.
953 usage: (save-excursion &rest BODY) */)
954 (Lisp_Object args)
956 register Lisp_Object val;
957 ptrdiff_t count = SPECPDL_INDEX ();
959 record_unwind_protect (save_excursion_restore, save_excursion_save ());
961 val = Fprogn (args);
962 return unbind_to (count, val);
965 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
966 doc: /* Save the current buffer; execute BODY; restore the current buffer.
967 Executes BODY just like `progn'.
968 usage: (save-current-buffer &rest BODY) */)
969 (Lisp_Object args)
971 Lisp_Object val;
972 ptrdiff_t count = SPECPDL_INDEX ();
974 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
976 val = Fprogn (args);
977 return unbind_to (count, val);
980 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
981 doc: /* Return the number of characters in the current buffer.
982 If BUFFER, return the number of characters in that buffer instead. */)
983 (Lisp_Object buffer)
985 if (NILP (buffer))
986 return make_number (Z - BEG);
987 else
989 CHECK_BUFFER (buffer);
990 return make_number (BUF_Z (XBUFFER (buffer))
991 - BUF_BEG (XBUFFER (buffer)));
995 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
996 doc: /* Return the minimum permissible value of point in the current buffer.
997 This is 1, unless narrowing (a buffer restriction) is in effect. */)
998 (void)
1000 Lisp_Object temp;
1001 XSETFASTINT (temp, BEGV);
1002 return temp;
1005 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
1006 doc: /* Return a marker to the minimum permissible value of point in this buffer.
1007 This is the beginning, unless narrowing (a buffer restriction) is in effect. */)
1008 (void)
1010 return buildmark (BEGV, BEGV_BYTE);
1013 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
1014 doc: /* Return the maximum permissible value of point in the current buffer.
1015 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1016 is in effect, in which case it is less. */)
1017 (void)
1019 Lisp_Object temp;
1020 XSETFASTINT (temp, ZV);
1021 return temp;
1024 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
1025 doc: /* Return a marker to the maximum permissible value of point in this buffer.
1026 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1027 is in effect, in which case it is less. */)
1028 (void)
1030 return buildmark (ZV, ZV_BYTE);
1033 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
1034 doc: /* Return the position of the gap, in the current buffer.
1035 See also `gap-size'. */)
1036 (void)
1038 Lisp_Object temp;
1039 XSETFASTINT (temp, GPT);
1040 return temp;
1043 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
1044 doc: /* Return the size of the current buffer's gap.
1045 See also `gap-position'. */)
1046 (void)
1048 Lisp_Object temp;
1049 XSETFASTINT (temp, GAP_SIZE);
1050 return temp;
1053 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
1054 doc: /* Return the byte position for character position POSITION.
1055 If POSITION is out of range, the value is nil. */)
1056 (Lisp_Object position)
1058 CHECK_NUMBER_COERCE_MARKER (position);
1059 if (XINT (position) < BEG || XINT (position) > Z)
1060 return Qnil;
1061 return make_number (CHAR_TO_BYTE (XINT (position)));
1064 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
1065 doc: /* Return the character position for byte position BYTEPOS.
1066 If BYTEPOS is out of range, the value is nil. */)
1067 (Lisp_Object bytepos)
1069 CHECK_NUMBER (bytepos);
1070 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
1071 return Qnil;
1072 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
1075 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
1076 doc: /* Return the character following point, as a number.
1077 At the end of the buffer or accessible region, return 0. */)
1078 (void)
1080 Lisp_Object temp;
1081 if (PT >= ZV)
1082 XSETFASTINT (temp, 0);
1083 else
1084 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
1085 return temp;
1088 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
1089 doc: /* Return the character preceding point, as a number.
1090 At the beginning of the buffer or accessible region, return 0. */)
1091 (void)
1093 Lisp_Object temp;
1094 if (PT <= BEGV)
1095 XSETFASTINT (temp, 0);
1096 else if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1098 ptrdiff_t pos = PT_BYTE;
1099 DEC_POS (pos);
1100 XSETFASTINT (temp, FETCH_CHAR (pos));
1102 else
1103 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
1104 return temp;
1107 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
1108 doc: /* Return t if point is at the beginning of the buffer.
1109 If the buffer is narrowed, this means the beginning of the narrowed part. */)
1110 (void)
1112 if (PT == BEGV)
1113 return Qt;
1114 return Qnil;
1117 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
1118 doc: /* Return t if point is at the end of the buffer.
1119 If the buffer is narrowed, this means the end of the narrowed part. */)
1120 (void)
1122 if (PT == ZV)
1123 return Qt;
1124 return Qnil;
1127 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
1128 doc: /* Return t if point is at the beginning of a line. */)
1129 (void)
1131 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
1132 return Qt;
1133 return Qnil;
1136 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
1137 doc: /* Return t if point is at the end of a line.
1138 `End of a line' includes point being at the end of the buffer. */)
1139 (void)
1141 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
1142 return Qt;
1143 return Qnil;
1146 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
1147 doc: /* Return character in current buffer at position POS.
1148 POS is an integer or a marker and defaults to point.
1149 If POS is out of range, the value is nil. */)
1150 (Lisp_Object pos)
1152 register ptrdiff_t pos_byte;
1154 if (NILP (pos))
1156 pos_byte = PT_BYTE;
1157 XSETFASTINT (pos, PT);
1160 if (MARKERP (pos))
1162 pos_byte = marker_byte_position (pos);
1163 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1164 return Qnil;
1166 else
1168 CHECK_NUMBER_COERCE_MARKER (pos);
1169 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1170 return Qnil;
1172 pos_byte = CHAR_TO_BYTE (XINT (pos));
1175 return make_number (FETCH_CHAR (pos_byte));
1178 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1179 doc: /* Return character in current buffer preceding position POS.
1180 POS is an integer or a marker and defaults to point.
1181 If POS is out of range, the value is nil. */)
1182 (Lisp_Object pos)
1184 register Lisp_Object val;
1185 register ptrdiff_t pos_byte;
1187 if (NILP (pos))
1189 pos_byte = PT_BYTE;
1190 XSETFASTINT (pos, PT);
1193 if (MARKERP (pos))
1195 pos_byte = marker_byte_position (pos);
1197 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1198 return Qnil;
1200 else
1202 CHECK_NUMBER_COERCE_MARKER (pos);
1204 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1205 return Qnil;
1207 pos_byte = CHAR_TO_BYTE (XINT (pos));
1210 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
1212 DEC_POS (pos_byte);
1213 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1215 else
1217 pos_byte--;
1218 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1220 return val;
1223 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1224 doc: /* Return the name under which the user logged in, as a string.
1225 This is based on the effective uid, not the real uid.
1226 Also, if the environment variables LOGNAME or USER are set,
1227 that determines the value of this function.
1229 If optional argument UID is an integer or a float, return the login name
1230 of the user with that uid, or nil if there is no such user. */)
1231 (Lisp_Object uid)
1233 struct passwd *pw;
1234 uid_t id;
1236 /* Set up the user name info if we didn't do it before.
1237 (That can happen if Emacs is dumpable
1238 but you decide to run `temacs -l loadup' and not dump. */
1239 if (INTEGERP (Vuser_login_name))
1240 init_editfns ();
1242 if (NILP (uid))
1243 return Vuser_login_name;
1245 CONS_TO_INTEGER (uid, uid_t, id);
1246 BLOCK_INPUT;
1247 pw = getpwuid (id);
1248 UNBLOCK_INPUT;
1249 return (pw ? build_string (pw->pw_name) : Qnil);
1252 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1253 0, 0, 0,
1254 doc: /* Return the name of the user's real uid, as a string.
1255 This ignores the environment variables LOGNAME and USER, so it differs from
1256 `user-login-name' when running under `su'. */)
1257 (void)
1259 /* Set up the user name info if we didn't do it before.
1260 (That can happen if Emacs is dumpable
1261 but you decide to run `temacs -l loadup' and not dump. */
1262 if (INTEGERP (Vuser_login_name))
1263 init_editfns ();
1264 return Vuser_real_login_name;
1267 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1268 doc: /* Return the effective uid of Emacs.
1269 Value is an integer or a float, depending on the value. */)
1270 (void)
1272 uid_t euid = geteuid ();
1273 return make_fixnum_or_float (euid);
1276 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1277 doc: /* Return the real uid of Emacs.
1278 Value is an integer or a float, depending on the value. */)
1279 (void)
1281 uid_t uid = getuid ();
1282 return make_fixnum_or_float (uid);
1285 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1286 doc: /* Return the full name of the user logged in, as a string.
1287 If the full name corresponding to Emacs's userid is not known,
1288 return "unknown".
1290 If optional argument UID is an integer or float, return the full name
1291 of the user with that uid, or nil if there is no such user.
1292 If UID is a string, return the full name of the user with that login
1293 name, or nil if there is no such user. */)
1294 (Lisp_Object uid)
1296 struct passwd *pw;
1297 register char *p, *q;
1298 Lisp_Object full;
1300 if (NILP (uid))
1301 return Vuser_full_name;
1302 else if (NUMBERP (uid))
1304 uid_t u;
1305 CONS_TO_INTEGER (uid, uid_t, u);
1306 BLOCK_INPUT;
1307 pw = getpwuid (u);
1308 UNBLOCK_INPUT;
1310 else if (STRINGP (uid))
1312 BLOCK_INPUT;
1313 pw = getpwnam (SSDATA (uid));
1314 UNBLOCK_INPUT;
1316 else
1317 error ("Invalid UID specification");
1319 if (!pw)
1320 return Qnil;
1322 p = USER_FULL_NAME;
1323 /* Chop off everything after the first comma. */
1324 q = strchr (p, ',');
1325 full = make_string (p, q ? q - p : strlen (p));
1327 #ifdef AMPERSAND_FULL_NAME
1328 p = SSDATA (full);
1329 q = strchr (p, '&');
1330 /* Substitute the login name for the &, upcasing the first character. */
1331 if (q)
1333 register char *r;
1334 Lisp_Object login;
1336 login = Fuser_login_name (make_number (pw->pw_uid));
1337 r = (char *) alloca (strlen (p) + SCHARS (login) + 1);
1338 memcpy (r, p, q - p);
1339 r[q - p] = 0;
1340 strcat (r, SSDATA (login));
1341 r[q - p] = upcase ((unsigned char) r[q - p]);
1342 strcat (r, q + 1);
1343 full = build_string (r);
1345 #endif /* AMPERSAND_FULL_NAME */
1347 return full;
1350 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1351 doc: /* Return the host name of the machine you are running on, as a string. */)
1352 (void)
1354 return Vsystem_name;
1357 const char *
1358 get_system_name (void)
1360 if (STRINGP (Vsystem_name))
1361 return SSDATA (Vsystem_name);
1362 else
1363 return "";
1366 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1367 doc: /* Return the process ID of Emacs, as a number. */)
1368 (void)
1370 pid_t pid = getpid ();
1371 return make_fixnum_or_float (pid);
1376 #ifndef TIME_T_MIN
1377 # define TIME_T_MIN TYPE_MINIMUM (time_t)
1378 #endif
1379 #ifndef TIME_T_MAX
1380 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
1381 #endif
1383 /* Report that a time value is out of range for Emacs. */
1384 void
1385 time_overflow (void)
1387 error ("Specified time is not representable");
1390 /* Return the upper part of the time T (everything but the bottom 16 bits). */
1391 static EMACS_INT
1392 hi_time (time_t t)
1394 time_t hi = t >> 16;
1396 /* Check for overflow, helping the compiler for common cases where
1397 no runtime check is needed, and taking care not to convert
1398 negative numbers to unsigned before comparing them. */
1399 if (! ((! TYPE_SIGNED (time_t)
1400 || MOST_NEGATIVE_FIXNUM <= TIME_T_MIN >> 16
1401 || MOST_NEGATIVE_FIXNUM <= hi)
1402 && (TIME_T_MAX >> 16 <= MOST_POSITIVE_FIXNUM
1403 || hi <= MOST_POSITIVE_FIXNUM)))
1404 time_overflow ();
1406 return hi;
1409 /* Return the bottom 16 bits of the time T. */
1410 static int
1411 lo_time (time_t t)
1413 return t & ((1 << 16) - 1);
1416 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1417 doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
1418 The time is returned as a list of integers (HIGH LOW USEC PSEC).
1419 HIGH has the most significant bits of the seconds, while LOW has the
1420 least significant 16 bits. USEC and PSEC are the microsecond and
1421 picosecond counts. */)
1422 (void)
1424 EMACS_TIME t;
1426 EMACS_GET_TIME (t);
1427 return make_lisp_time (t);
1430 DEFUN ("get-internal-run-time", Fget_internal_run_time, Sget_internal_run_time,
1431 0, 0, 0,
1432 doc: /* Return the current run time used by Emacs.
1433 The time is returned as a list (HIGH LOW USEC PSEC), using the same
1434 style as (current-time).
1436 On systems that can't determine the run time, `get-internal-run-time'
1437 does the same thing as `current-time'. */)
1438 (void)
1440 #ifdef HAVE_GETRUSAGE
1441 struct rusage usage;
1442 time_t secs;
1443 int usecs;
1444 EMACS_TIME t;
1446 if (getrusage (RUSAGE_SELF, &usage) < 0)
1447 /* This shouldn't happen. What action is appropriate? */
1448 xsignal0 (Qerror);
1450 /* Sum up user time and system time. */
1451 secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
1452 usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
1453 if (usecs >= 1000000)
1455 usecs -= 1000000;
1456 secs++;
1458 EMACS_SET_SECS_USECS (t, secs, usecs);
1459 return make_lisp_time (t);
1460 #else /* ! HAVE_GETRUSAGE */
1461 #ifdef WINDOWSNT
1462 return w32_get_internal_run_time ();
1463 #else /* ! WINDOWSNT */
1464 return Fcurrent_time ();
1465 #endif /* WINDOWSNT */
1466 #endif /* HAVE_GETRUSAGE */
1470 /* Make a Lisp list that represents the time T with fraction TAIL. */
1471 static Lisp_Object
1472 make_time_tail (time_t t, Lisp_Object tail)
1474 return Fcons (make_number (hi_time (t)),
1475 Fcons (make_number (lo_time (t)), tail));
1478 /* Make a Lisp list that represents the system time T. */
1479 static Lisp_Object
1480 make_time (time_t t)
1482 return make_time_tail (t, Qnil);
1485 /* Make a Lisp list that represents the Emacs time T. T may be an
1486 invalid time, with a slightly negative tv_nsec value such as
1487 UNKNOWN_MODTIME_NSECS; in that case, the Lisp list contains a
1488 correspondingly negative picosecond count. */
1489 Lisp_Object
1490 make_lisp_time (EMACS_TIME t)
1492 int ns = EMACS_NSECS (t);
1493 return make_time_tail (EMACS_SECS (t),
1494 list2 (make_number (ns / 1000),
1495 make_number (ns % 1000 * 1000)));
1498 /* Decode a Lisp list SPECIFIED_TIME that represents a time.
1499 Set *PHIGH, *PLOW, *PUSEC, *PPSEC to its parts; do not check their values.
1500 Return nonzero if successful. */
1501 static int
1502 disassemble_lisp_time (Lisp_Object specified_time, Lisp_Object *phigh,
1503 Lisp_Object *plow, Lisp_Object *pusec,
1504 Lisp_Object *ppsec)
1506 if (CONSP (specified_time))
1508 Lisp_Object low = XCDR (specified_time);
1509 Lisp_Object usec = make_number (0);
1510 Lisp_Object psec = make_number (0);
1511 if (CONSP (low))
1513 Lisp_Object low_tail = XCDR (low);
1514 low = XCAR (low);
1515 if (CONSP (low_tail))
1517 usec = XCAR (low_tail);
1518 low_tail = XCDR (low_tail);
1519 if (CONSP (low_tail))
1520 psec = XCAR (low_tail);
1522 else if (!NILP (low_tail))
1523 usec = low_tail;
1526 *phigh = XCAR (specified_time);
1527 *plow = low;
1528 *pusec = usec;
1529 *ppsec = psec;
1530 return 1;
1533 return 0;
1536 /* From the time components HIGH, LOW, USEC and PSEC taken from a Lisp
1537 list, generate the corresponding EMACS_TIME value *RESULT, and
1538 if RESULT_PSEC is not null store into *RESULT_PSEC the
1539 (nonnegative) difference in picoseconds between the input time and
1540 the returned time. Return nonzero if successful. */
1542 decode_time_components (Lisp_Object high, Lisp_Object low, Lisp_Object usec,
1543 Lisp_Object psec, EMACS_TIME *result, int *result_psec)
1545 EMACS_INT hi, lo, us, ps;
1546 time_t sec;
1547 if (! (INTEGERP (high) && INTEGERP (low)
1548 && INTEGERP (usec) && INTEGERP (psec)))
1549 return 0;
1550 hi = XINT (high);
1551 lo = XINT (low);
1552 us = XINT (usec);
1553 ps = XINT (psec);
1555 /* Normalize out-of-range lower-order components by carrying
1556 each overflow into the next higher-order component. */
1557 us += ps / 1000000 - (ps % 1000000 < 0);
1558 lo += us / 1000000 - (us % 1000000 < 0);
1559 hi += lo >> 16;
1560 ps = ps % 1000000 + 1000000 * (ps % 1000000 < 0);
1561 us = us % 1000000 + 1000000 * (us % 1000000 < 0);
1562 lo &= (1 << 16) - 1;
1564 /* Check for overflow in the highest-order component. */
1565 if (! ((TYPE_SIGNED (time_t) ? TIME_T_MIN >> 16 <= hi : 0 <= hi)
1566 && hi <= TIME_T_MAX >> 16))
1567 return 0;
1569 sec = hi;
1570 EMACS_SET_SECS_NSECS (*result, (sec << 16) + lo, us * 1000 + ps / 1000);
1571 if (result_psec)
1572 *result_psec = ps % 1000;
1573 return 1;
1576 /* Decode a Lisp list SPECIFIED_TIME that represents a time.
1577 If SPECIFIED_TIME is nil, use the current time.
1578 Round the time down to the nearest EMACS_TIME value, and
1579 if PPSEC is not null store into *PPSEC the (nonnegative) difference in
1580 picoseconds between the input time and the returned time.
1581 Return seconds since the Epoch.
1582 Signal an error if unsuccessful. */
1583 EMACS_TIME
1584 lisp_time_argument (Lisp_Object specified_time, int *ppsec)
1586 EMACS_TIME t;
1587 if (NILP (specified_time))
1588 EMACS_GET_TIME (t);
1589 else
1591 Lisp_Object high, low, usec, psec;
1592 if (! (disassemble_lisp_time (specified_time, &high, &low, &usec, &psec)
1593 && decode_time_components (high, low, usec, psec, &t, ppsec)))
1594 error ("Invalid time specification");
1596 return t;
1599 /* Like lisp_time_argument, except decode only the seconds part,
1600 and do not check the subseconds part, and always round down. */
1601 static time_t
1602 lisp_seconds_argument (Lisp_Object specified_time)
1604 if (NILP (specified_time))
1605 return time (NULL);
1606 else
1608 Lisp_Object high, low, usec, psec;
1609 EMACS_TIME t;
1610 if (! (disassemble_lisp_time (specified_time, &high, &low, &usec, &psec)
1611 && decode_time_components (high, low, make_number (0),
1612 make_number (0), &t, 0)))
1613 error ("Invalid time specification");
1614 return EMACS_SECS (t);
1618 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1619 doc: /* Return the current time, as a float number of seconds since the epoch.
1620 If SPECIFIED-TIME is given, it is the time to convert to float
1621 instead of the current time. The argument should have the form
1622 (HIGH LOW) or (HIGH LOW USEC) or (HIGH LOW USEC PSEC). Thus,
1623 you can use times from `current-time' and from `file-attributes'.
1624 SPECIFIED-TIME can also have the form (HIGH . LOW), but this is
1625 considered obsolete.
1627 WARNING: Since the result is floating point, it may not be exact.
1628 If precise time stamps are required, use either `current-time',
1629 or (if you need time as a string) `format-time-string'. */)
1630 (Lisp_Object specified_time)
1632 int psec;
1633 EMACS_TIME t = lisp_time_argument (specified_time, &psec);
1634 double ps = (1000 * 1000 * 1000 <= INTMAX_MAX / 1000
1635 ? EMACS_NSECS (t) * (intmax_t) 1000 + psec
1636 : EMACS_NSECS (t) * 1e3 + psec);
1637 return make_float (EMACS_SECS (t) + ps / 1e12);
1640 /* Write information into buffer S of size MAXSIZE, according to the
1641 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1642 Default to Universal Time if UT is nonzero, local time otherwise.
1643 Use NS as the number of nanoseconds in the %N directive.
1644 Return the number of bytes written, not including the terminating
1645 '\0'. If S is NULL, nothing will be written anywhere; so to
1646 determine how many bytes would be written, use NULL for S and
1647 ((size_t) -1) for MAXSIZE.
1649 This function behaves like nstrftime, except it allows null
1650 bytes in FORMAT and it does not support nanoseconds. */
1651 static size_t
1652 emacs_nmemftime (char *s, size_t maxsize, const char *format,
1653 size_t format_len, const struct tm *tp, int ut, int ns)
1655 size_t total = 0;
1657 /* Loop through all the null-terminated strings in the format
1658 argument. Normally there's just one null-terminated string, but
1659 there can be arbitrarily many, concatenated together, if the
1660 format contains '\0' bytes. nstrftime stops at the first
1661 '\0' byte so we must invoke it separately for each such string. */
1662 for (;;)
1664 size_t len;
1665 size_t result;
1667 if (s)
1668 s[0] = '\1';
1670 result = nstrftime (s, maxsize, format, tp, ut, ns);
1672 if (s)
1674 if (result == 0 && s[0] != '\0')
1675 return 0;
1676 s += result + 1;
1679 maxsize -= result + 1;
1680 total += result;
1681 len = strlen (format);
1682 if (len == format_len)
1683 return total;
1684 total++;
1685 format += len + 1;
1686 format_len -= len + 1;
1690 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1691 doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted.
1692 TIME is specified as (HIGH LOW USEC PSEC), as returned by
1693 `current-time' or `file-attributes'. The obsolete form (HIGH . LOW)
1694 is also still accepted.
1695 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME
1696 as Universal Time; nil means describe TIME in the local time zone.
1697 The value is a copy of FORMAT-STRING, but with certain constructs replaced
1698 by text that describes the specified date and time in TIME:
1700 %Y is the year, %y within the century, %C the century.
1701 %G is the year corresponding to the ISO week, %g within the century.
1702 %m is the numeric month.
1703 %b and %h are the locale's abbreviated month name, %B the full name.
1704 %d is the day of the month, zero-padded, %e is blank-padded.
1705 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
1706 %a is the locale's abbreviated name of the day of week, %A the full name.
1707 %U is the week number starting on Sunday, %W starting on Monday,
1708 %V according to ISO 8601.
1709 %j is the day of the year.
1711 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
1712 only blank-padded, %l is like %I blank-padded.
1713 %p is the locale's equivalent of either AM or PM.
1714 %M is the minute.
1715 %S is the second.
1716 %N is the nanosecond, %6N the microsecond, %3N the millisecond, etc.
1717 %Z is the time zone name, %z is the numeric form.
1718 %s is the number of seconds since 1970-01-01 00:00:00 +0000.
1720 %c is the locale's date and time format.
1721 %x is the locale's "preferred" date format.
1722 %D is like "%m/%d/%y".
1724 %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p".
1725 %X is the locale's "preferred" time format.
1727 Finally, %n is a newline, %t is a tab, %% is a literal %.
1729 Certain flags and modifiers are available with some format controls.
1730 The flags are `_', `-', `^' and `#'. For certain characters X,
1731 %_X is like %X, but padded with blanks; %-X is like %X,
1732 but without padding. %^X is like %X, but with all textual
1733 characters up-cased; %#X is like %X, but with letter-case of
1734 all textual characters reversed.
1735 %NX (where N stands for an integer) is like %X,
1736 but takes up at least N (a number) positions.
1737 The modifiers are `E' and `O'. For certain characters X,
1738 %EX is a locale's alternative version of %X;
1739 %OX is like %X, but uses the locale's number symbols.
1741 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".
1743 usage: (format-time-string FORMAT-STRING &optional TIME UNIVERSAL) */)
1744 (Lisp_Object format_string, Lisp_Object timeval, Lisp_Object universal)
1746 EMACS_TIME t = lisp_time_argument (timeval, 0);
1747 struct tm tm;
1749 CHECK_STRING (format_string);
1750 format_string = code_convert_string_norecord (format_string,
1751 Vlocale_coding_system, 1);
1752 return format_time_string (SSDATA (format_string), SBYTES (format_string),
1753 t, ! NILP (universal), &tm);
1756 static Lisp_Object
1757 format_time_string (char const *format, ptrdiff_t formatlen,
1758 EMACS_TIME t, int ut, struct tm *tmp)
1760 char buffer[4000];
1761 char *buf = buffer;
1762 ptrdiff_t size = sizeof buffer;
1763 size_t len;
1764 Lisp_Object bufstring;
1765 int ns = EMACS_NSECS (t);
1766 struct tm *tm;
1767 USE_SAFE_ALLOCA;
1769 while (1)
1771 BLOCK_INPUT;
1773 synchronize_system_time_locale ();
1775 tm = ut ? gmtime (EMACS_SECS_ADDR (t)) : localtime (EMACS_SECS_ADDR (t));
1776 if (! tm)
1778 UNBLOCK_INPUT;
1779 time_overflow ();
1781 *tmp = *tm;
1783 buf[0] = '\1';
1784 len = emacs_nmemftime (buf, size, format, formatlen, tm, ut, ns);
1785 if ((0 < len && len < size) || (len == 0 && buf[0] == '\0'))
1786 break;
1788 /* Buffer was too small, so make it bigger and try again. */
1789 len = emacs_nmemftime (NULL, SIZE_MAX, format, formatlen, tm, ut, ns);
1790 UNBLOCK_INPUT;
1791 if (STRING_BYTES_BOUND <= len)
1792 string_overflow ();
1793 size = len + 1;
1794 SAFE_ALLOCA (buf, char *, size);
1797 UNBLOCK_INPUT;
1798 bufstring = make_unibyte_string (buf, len);
1799 SAFE_FREE ();
1800 return code_convert_string_norecord (bufstring, Vlocale_coding_system, 0);
1803 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1804 doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
1805 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED),
1806 as from `current-time' and `file-attributes', or nil to use the
1807 current time. The obsolete form (HIGH . LOW) is also still accepted.
1808 The list has the following nine members: SEC is an integer between 0
1809 and 60; SEC is 60 for a leap second, which only some operating systems
1810 support. MINUTE is an integer between 0 and 59. HOUR is an integer
1811 between 0 and 23. DAY is an integer between 1 and 31. MONTH is an
1812 integer between 1 and 12. YEAR is an integer indicating the
1813 four-digit year. DOW is the day of week, an integer between 0 and 6,
1814 where 0 is Sunday. DST is t if daylight saving time is in effect,
1815 otherwise nil. ZONE is an integer indicating the number of seconds
1816 east of Greenwich. (Note that Common Lisp has different meanings for
1817 DOW and ZONE.) */)
1818 (Lisp_Object specified_time)
1820 time_t time_spec = lisp_seconds_argument (specified_time);
1821 struct tm save_tm;
1822 struct tm *decoded_time;
1823 Lisp_Object list_args[9];
1825 BLOCK_INPUT;
1826 decoded_time = localtime (&time_spec);
1827 if (decoded_time)
1828 save_tm = *decoded_time;
1829 UNBLOCK_INPUT;
1830 if (! (decoded_time
1831 && MOST_NEGATIVE_FIXNUM - TM_YEAR_BASE <= save_tm.tm_year
1832 && save_tm.tm_year <= MOST_POSITIVE_FIXNUM - TM_YEAR_BASE))
1833 time_overflow ();
1834 XSETFASTINT (list_args[0], save_tm.tm_sec);
1835 XSETFASTINT (list_args[1], save_tm.tm_min);
1836 XSETFASTINT (list_args[2], save_tm.tm_hour);
1837 XSETFASTINT (list_args[3], save_tm.tm_mday);
1838 XSETFASTINT (list_args[4], save_tm.tm_mon + 1);
1839 /* On 64-bit machines an int is narrower than EMACS_INT, thus the
1840 cast below avoids overflow in int arithmetics. */
1841 XSETINT (list_args[5], TM_YEAR_BASE + (EMACS_INT) save_tm.tm_year);
1842 XSETFASTINT (list_args[6], save_tm.tm_wday);
1843 list_args[7] = save_tm.tm_isdst ? Qt : Qnil;
1845 BLOCK_INPUT;
1846 decoded_time = gmtime (&time_spec);
1847 if (decoded_time == 0)
1848 list_args[8] = Qnil;
1849 else
1850 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1851 UNBLOCK_INPUT;
1852 return Flist (9, list_args);
1855 /* Return OBJ - OFFSET, checking that OBJ is a valid fixnum and that
1856 the result is representable as an int. Assume OFFSET is small and
1857 nonnegative. */
1858 static int
1859 check_tm_member (Lisp_Object obj, int offset)
1861 EMACS_INT n;
1862 CHECK_NUMBER (obj);
1863 n = XINT (obj);
1864 if (! (INT_MIN + offset <= n && n - offset <= INT_MAX))
1865 time_overflow ();
1866 return n - offset;
1869 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1870 doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
1871 This is the reverse operation of `decode-time', which see.
1872 ZONE defaults to the current time zone rule. This can
1873 be a string or t (as from `set-time-zone-rule'), or it can be a list
1874 \(as from `current-time-zone') or an integer (as from `decode-time')
1875 applied without consideration for daylight saving time.
1877 You can pass more than 7 arguments; then the first six arguments
1878 are used as SECOND through YEAR, and the *last* argument is used as ZONE.
1879 The intervening arguments are ignored.
1880 This feature lets (apply 'encode-time (decode-time ...)) work.
1882 Out-of-range values for SECOND, MINUTE, HOUR, DAY, or MONTH are allowed;
1883 for example, a DAY of 0 means the day preceding the given month.
1884 Year numbers less than 100 are treated just like other year numbers.
1885 If you want them to stand for years in this century, you must do that yourself.
1887 Years before 1970 are not guaranteed to work. On some systems,
1888 year values as low as 1901 do work.
1890 usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */)
1891 (ptrdiff_t nargs, Lisp_Object *args)
1893 time_t value;
1894 struct tm tm;
1895 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1897 tm.tm_sec = check_tm_member (args[0], 0);
1898 tm.tm_min = check_tm_member (args[1], 0);
1899 tm.tm_hour = check_tm_member (args[2], 0);
1900 tm.tm_mday = check_tm_member (args[3], 0);
1901 tm.tm_mon = check_tm_member (args[4], 1);
1902 tm.tm_year = check_tm_member (args[5], TM_YEAR_BASE);
1903 tm.tm_isdst = -1;
1905 if (CONSP (zone))
1906 zone = Fcar (zone);
1907 if (NILP (zone))
1909 BLOCK_INPUT;
1910 value = mktime (&tm);
1911 UNBLOCK_INPUT;
1913 else
1915 char tzbuf[100];
1916 const char *tzstring;
1917 char **oldenv = environ, **newenv;
1919 if (EQ (zone, Qt))
1920 tzstring = "UTC0";
1921 else if (STRINGP (zone))
1922 tzstring = SSDATA (zone);
1923 else if (INTEGERP (zone))
1925 EMACS_INT abszone = eabs (XINT (zone));
1926 EMACS_INT zone_hr = abszone / (60*60);
1927 int zone_min = (abszone/60) % 60;
1928 int zone_sec = abszone % 60;
1929 sprintf (tzbuf, "XXX%s%"pI"d:%02d:%02d", "-" + (XINT (zone) < 0),
1930 zone_hr, zone_min, zone_sec);
1931 tzstring = tzbuf;
1933 else
1934 error ("Invalid time zone specification");
1936 BLOCK_INPUT;
1938 /* Set TZ before calling mktime; merely adjusting mktime's returned
1939 value doesn't suffice, since that would mishandle leap seconds. */
1940 set_time_zone_rule (tzstring);
1942 value = mktime (&tm);
1944 /* Restore TZ to previous value. */
1945 newenv = environ;
1946 environ = oldenv;
1947 #ifdef LOCALTIME_CACHE
1948 tzset ();
1949 #endif
1950 UNBLOCK_INPUT;
1952 xfree (newenv);
1955 if (value == (time_t) -1)
1956 time_overflow ();
1958 return make_time (value);
1961 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1962 doc: /* Return the current local time, as a human-readable string.
1963 Programs can use this function to decode a time,
1964 since the number of columns in each field is fixed
1965 if the year is in the range 1000-9999.
1966 The format is `Sun Sep 16 01:03:52 1973'.
1967 However, see also the functions `decode-time' and `format-time-string'
1968 which provide a much more powerful and general facility.
1970 If SPECIFIED-TIME is given, it is a time to format instead of the
1971 current time. The argument should have the form (HIGH LOW . IGNORED).
1972 Thus, you can use times obtained from `current-time' and from
1973 `file-attributes'. SPECIFIED-TIME can also have the form (HIGH . LOW),
1974 but this is considered obsolete. */)
1975 (Lisp_Object specified_time)
1977 time_t value = lisp_seconds_argument (specified_time);
1978 struct tm *tm;
1979 char buf[sizeof "Mon Apr 30 12:49:17 " + INT_STRLEN_BOUND (int) + 1];
1980 int len IF_LINT (= 0);
1982 /* Convert to a string in ctime format, except without the trailing
1983 newline, and without the 4-digit year limit. Don't use asctime
1984 or ctime, as they might dump core if the year is outside the
1985 range -999 .. 9999. */
1986 BLOCK_INPUT;
1987 tm = localtime (&value);
1988 if (tm)
1990 static char const wday_name[][4] =
1991 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1992 static char const mon_name[][4] =
1993 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1994 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1995 printmax_t year_base = TM_YEAR_BASE;
1997 len = sprintf (buf, "%s %s%3d %02d:%02d:%02d %"pMd,
1998 wday_name[tm->tm_wday], mon_name[tm->tm_mon], tm->tm_mday,
1999 tm->tm_hour, tm->tm_min, tm->tm_sec,
2000 tm->tm_year + year_base);
2002 UNBLOCK_INPUT;
2003 if (! tm)
2004 time_overflow ();
2006 return make_unibyte_string (buf, len);
2009 /* Yield A - B, measured in seconds.
2010 This function is copied from the GNU C Library. */
2011 static int
2012 tm_diff (struct tm *a, struct tm *b)
2014 /* Compute intervening leap days correctly even if year is negative.
2015 Take care to avoid int overflow in leap day calculations,
2016 but it's OK to assume that A and B are close to each other. */
2017 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
2018 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
2019 int a100 = a4 / 25 - (a4 % 25 < 0);
2020 int b100 = b4 / 25 - (b4 % 25 < 0);
2021 int a400 = a100 >> 2;
2022 int b400 = b100 >> 2;
2023 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
2024 int years = a->tm_year - b->tm_year;
2025 int days = (365 * years + intervening_leap_days
2026 + (a->tm_yday - b->tm_yday));
2027 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
2028 + (a->tm_min - b->tm_min))
2029 + (a->tm_sec - b->tm_sec));
2032 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
2033 doc: /* Return the offset and name for the local time zone.
2034 This returns a list of the form (OFFSET NAME).
2035 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).
2036 A negative value means west of Greenwich.
2037 NAME is a string giving the name of the time zone.
2038 If SPECIFIED-TIME is given, the time zone offset is determined from it
2039 instead of using the current time. The argument should have the form
2040 (HIGH LOW . IGNORED). Thus, you can use times obtained from
2041 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
2042 have the form (HIGH . LOW), but this is considered obsolete.
2044 Some operating systems cannot provide all this information to Emacs;
2045 in this case, `current-time-zone' returns a list containing nil for
2046 the data it can't find. */)
2047 (Lisp_Object specified_time)
2049 EMACS_TIME value;
2050 int offset;
2051 struct tm *t;
2052 struct tm localtm;
2053 Lisp_Object zone_offset, zone_name;
2055 zone_offset = Qnil;
2056 EMACS_SET_SECS_NSECS (value, lisp_seconds_argument (specified_time), 0);
2057 zone_name = format_time_string ("%Z", sizeof "%Z" - 1, value, 0, &localtm);
2058 BLOCK_INPUT;
2059 t = gmtime (EMACS_SECS_ADDR (value));
2060 if (t)
2061 offset = tm_diff (&localtm, t);
2062 UNBLOCK_INPUT;
2064 if (t)
2066 zone_offset = make_number (offset);
2067 if (SCHARS (zone_name) == 0)
2069 /* No local time zone name is available; use "+-NNNN" instead. */
2070 int m = offset / 60;
2071 int am = offset < 0 ? - m : m;
2072 char buf[sizeof "+00" + INT_STRLEN_BOUND (int)];
2073 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
2074 zone_name = build_string (buf);
2078 return list2 (zone_offset, zone_name);
2081 /* This holds the value of `environ' produced by the previous
2082 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
2083 has never been called. */
2084 static char **environbuf;
2086 /* This holds the startup value of the TZ environment variable so it
2087 can be restored if the user calls set-time-zone-rule with a nil
2088 argument. */
2089 static char *initial_tz;
2091 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
2092 doc: /* Set the local time zone using TZ, a string specifying a time zone rule.
2093 If TZ is nil, use implementation-defined default time zone information.
2094 If TZ is t, use Universal Time.
2096 Instead of calling this function, you typically want (setenv "TZ" TZ).
2097 That changes both the environment of the Emacs process and the
2098 variable `process-environment', whereas `set-time-zone-rule' affects
2099 only the former. */)
2100 (Lisp_Object tz)
2102 const char *tzstring;
2103 char **old_environbuf;
2105 if (! (NILP (tz) || EQ (tz, Qt)))
2106 CHECK_STRING (tz);
2108 BLOCK_INPUT;
2110 /* When called for the first time, save the original TZ. */
2111 old_environbuf = environbuf;
2112 if (!old_environbuf)
2113 initial_tz = (char *) getenv ("TZ");
2115 if (NILP (tz))
2116 tzstring = initial_tz;
2117 else if (EQ (tz, Qt))
2118 tzstring = "UTC0";
2119 else
2120 tzstring = SSDATA (tz);
2122 set_time_zone_rule (tzstring);
2123 environbuf = environ;
2125 UNBLOCK_INPUT;
2127 xfree (old_environbuf);
2128 return Qnil;
2131 #ifdef LOCALTIME_CACHE
2133 /* These two values are known to load tz files in buggy implementations,
2134 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
2135 Their values shouldn't matter in non-buggy implementations.
2136 We don't use string literals for these strings,
2137 since if a string in the environment is in readonly
2138 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
2139 See Sun bugs 1113095 and 1114114, ``Timezone routines
2140 improperly modify environment''. */
2142 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
2143 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
2145 #endif
2147 /* Set the local time zone rule to TZSTRING.
2148 This allocates memory into `environ', which it is the caller's
2149 responsibility to free. */
2151 void
2152 set_time_zone_rule (const char *tzstring)
2154 ptrdiff_t envptrs;
2155 char **from, **to, **newenv;
2157 /* Make the ENVIRON vector longer with room for TZSTRING. */
2158 for (from = environ; *from; from++)
2159 continue;
2160 envptrs = from - environ + 2;
2161 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
2162 + (tzstring ? strlen (tzstring) + 4 : 0));
2164 /* Add TZSTRING to the end of environ, as a value for TZ. */
2165 if (tzstring)
2167 char *t = (char *) (to + envptrs);
2168 strcpy (t, "TZ=");
2169 strcat (t, tzstring);
2170 *to++ = t;
2173 /* Copy the old environ vector elements into NEWENV,
2174 but don't copy the TZ variable.
2175 So we have only one definition of TZ, which came from TZSTRING. */
2176 for (from = environ; *from; from++)
2177 if (strncmp (*from, "TZ=", 3) != 0)
2178 *to++ = *from;
2179 *to = 0;
2181 environ = newenv;
2183 /* If we do have a TZSTRING, NEWENV points to the vector slot where
2184 the TZ variable is stored. If we do not have a TZSTRING,
2185 TO points to the vector slot which has the terminating null. */
2187 #ifdef LOCALTIME_CACHE
2189 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
2190 "US/Pacific" that loads a tz file, then changes to a value like
2191 "XXX0" that does not load a tz file, and then changes back to
2192 its original value, the last change is (incorrectly) ignored.
2193 Also, if TZ changes twice in succession to values that do
2194 not load a tz file, tzset can dump core (see Sun bug#1225179).
2195 The following code works around these bugs. */
2197 if (tzstring)
2199 /* Temporarily set TZ to a value that loads a tz file
2200 and that differs from tzstring. */
2201 char *tz = *newenv;
2202 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
2203 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
2204 tzset ();
2205 *newenv = tz;
2207 else
2209 /* The implied tzstring is unknown, so temporarily set TZ to
2210 two different values that each load a tz file. */
2211 *to = set_time_zone_rule_tz1;
2212 to[1] = 0;
2213 tzset ();
2214 *to = set_time_zone_rule_tz2;
2215 tzset ();
2216 *to = 0;
2219 /* Now TZ has the desired value, and tzset can be invoked safely. */
2222 tzset ();
2223 #endif
2226 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
2227 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
2228 type of object is Lisp_String). INHERIT is passed to
2229 INSERT_FROM_STRING_FUNC as the last argument. */
2231 static void
2232 general_insert_function (void (*insert_func)
2233 (const char *, ptrdiff_t),
2234 void (*insert_from_string_func)
2235 (Lisp_Object, ptrdiff_t, ptrdiff_t,
2236 ptrdiff_t, ptrdiff_t, int),
2237 int inherit, ptrdiff_t nargs, Lisp_Object *args)
2239 ptrdiff_t argnum;
2240 register Lisp_Object val;
2242 for (argnum = 0; argnum < nargs; argnum++)
2244 val = args[argnum];
2245 if (CHARACTERP (val))
2247 int c = XFASTINT (val);
2248 unsigned char str[MAX_MULTIBYTE_LENGTH];
2249 int len;
2251 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2252 len = CHAR_STRING (c, str);
2253 else
2255 str[0] = ASCII_CHAR_P (c) ? c : multibyte_char_to_unibyte (c);
2256 len = 1;
2258 (*insert_func) ((char *) str, len);
2260 else if (STRINGP (val))
2262 (*insert_from_string_func) (val, 0, 0,
2263 SCHARS (val),
2264 SBYTES (val),
2265 inherit);
2267 else
2268 wrong_type_argument (Qchar_or_string_p, val);
2272 void
2273 insert1 (Lisp_Object arg)
2275 Finsert (1, &arg);
2279 /* Callers passing one argument to Finsert need not gcpro the
2280 argument "array", since the only element of the array will
2281 not be used after calling insert or insert_from_string, so
2282 we don't care if it gets trashed. */
2284 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
2285 doc: /* Insert the arguments, either strings or characters, at point.
2286 Point and before-insertion markers move forward to end up
2287 after the inserted text.
2288 Any other markers at the point of insertion remain before the text.
2290 If the current buffer is multibyte, unibyte strings are converted
2291 to multibyte for insertion (see `string-make-multibyte').
2292 If the current buffer is unibyte, multibyte strings are converted
2293 to unibyte for insertion (see `string-make-unibyte').
2295 When operating on binary data, it may be necessary to preserve the
2296 original bytes of a unibyte string when inserting it into a multibyte
2297 buffer; to accomplish this, apply `string-as-multibyte' to the string
2298 and insert the result.
2300 usage: (insert &rest ARGS) */)
2301 (ptrdiff_t nargs, Lisp_Object *args)
2303 general_insert_function (insert, insert_from_string, 0, nargs, args);
2304 return Qnil;
2307 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
2308 0, MANY, 0,
2309 doc: /* Insert the arguments at point, inheriting properties from adjoining text.
2310 Point and before-insertion markers move forward to end up
2311 after the inserted text.
2312 Any other markers at the point of insertion remain before the text.
2314 If the current buffer is multibyte, unibyte strings are converted
2315 to multibyte for insertion (see `unibyte-char-to-multibyte').
2316 If the current buffer is unibyte, multibyte strings are converted
2317 to unibyte for insertion.
2319 usage: (insert-and-inherit &rest ARGS) */)
2320 (ptrdiff_t nargs, Lisp_Object *args)
2322 general_insert_function (insert_and_inherit, insert_from_string, 1,
2323 nargs, args);
2324 return Qnil;
2327 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
2328 doc: /* Insert strings or characters at point, relocating markers after the text.
2329 Point and markers move forward to end up after the inserted text.
2331 If the current buffer is multibyte, unibyte strings are converted
2332 to multibyte for insertion (see `unibyte-char-to-multibyte').
2333 If the current buffer is unibyte, multibyte strings are converted
2334 to unibyte for insertion.
2336 usage: (insert-before-markers &rest ARGS) */)
2337 (ptrdiff_t nargs, Lisp_Object *args)
2339 general_insert_function (insert_before_markers,
2340 insert_from_string_before_markers, 0,
2341 nargs, args);
2342 return Qnil;
2345 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
2346 Sinsert_and_inherit_before_markers, 0, MANY, 0,
2347 doc: /* Insert text at point, relocating markers and inheriting properties.
2348 Point and markers move forward to end up after the inserted text.
2350 If the current buffer is multibyte, unibyte strings are converted
2351 to multibyte for insertion (see `unibyte-char-to-multibyte').
2352 If the current buffer is unibyte, multibyte strings are converted
2353 to unibyte for insertion.
2355 usage: (insert-before-markers-and-inherit &rest ARGS) */)
2356 (ptrdiff_t nargs, Lisp_Object *args)
2358 general_insert_function (insert_before_markers_and_inherit,
2359 insert_from_string_before_markers, 1,
2360 nargs, args);
2361 return Qnil;
2364 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2365 doc: /* Insert COUNT copies of CHARACTER.
2366 Point, and before-insertion markers, are relocated as in the function `insert'.
2367 The optional third arg INHERIT, if non-nil, says to inherit text properties
2368 from adjoining text, if those properties are sticky. */)
2369 (Lisp_Object character, Lisp_Object count, Lisp_Object inherit)
2371 int i, stringlen;
2372 register ptrdiff_t n;
2373 int c, len;
2374 unsigned char str[MAX_MULTIBYTE_LENGTH];
2375 char string[4000];
2377 CHECK_CHARACTER (character);
2378 CHECK_NUMBER (count);
2379 c = XFASTINT (character);
2381 if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
2382 len = CHAR_STRING (c, str);
2383 else
2384 str[0] = c, len = 1;
2385 if (XINT (count) <= 0)
2386 return Qnil;
2387 if (BUF_BYTES_MAX / len < XINT (count))
2388 buffer_overflow ();
2389 n = XINT (count) * len;
2390 stringlen = min (n, sizeof string - sizeof string % len);
2391 for (i = 0; i < stringlen; i++)
2392 string[i] = str[i % len];
2393 while (n > stringlen)
2395 QUIT;
2396 if (!NILP (inherit))
2397 insert_and_inherit (string, stringlen);
2398 else
2399 insert (string, stringlen);
2400 n -= stringlen;
2402 if (!NILP (inherit))
2403 insert_and_inherit (string, n);
2404 else
2405 insert (string, n);
2406 return Qnil;
2409 DEFUN ("insert-byte", Finsert_byte, Sinsert_byte, 2, 3, 0,
2410 doc: /* Insert COUNT (second arg) copies of BYTE (first arg).
2411 Both arguments are required.
2412 BYTE is a number of the range 0..255.
2414 If BYTE is 128..255 and the current buffer is multibyte, the
2415 corresponding eight-bit character is inserted.
2417 Point, and before-insertion markers, are relocated as in the function `insert'.
2418 The optional third arg INHERIT, if non-nil, says to inherit text properties
2419 from adjoining text, if those properties are sticky. */)
2420 (Lisp_Object byte, Lisp_Object count, Lisp_Object inherit)
2422 CHECK_NUMBER (byte);
2423 if (XINT (byte) < 0 || XINT (byte) > 255)
2424 args_out_of_range_3 (byte, make_number (0), make_number (255));
2425 if (XINT (byte) >= 128
2426 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2427 XSETFASTINT (byte, BYTE8_TO_CHAR (XINT (byte)));
2428 return Finsert_char (byte, count, inherit);
2432 /* Making strings from buffer contents. */
2434 /* Return a Lisp_String containing the text of the current buffer from
2435 START to END. If text properties are in use and the current buffer
2436 has properties in the range specified, the resulting string will also
2437 have them, if PROPS is nonzero.
2439 We don't want to use plain old make_string here, because it calls
2440 make_uninit_string, which can cause the buffer arena to be
2441 compacted. make_string has no way of knowing that the data has
2442 been moved, and thus copies the wrong data into the string. This
2443 doesn't effect most of the other users of make_string, so it should
2444 be left as is. But we should use this function when conjuring
2445 buffer substrings. */
2447 Lisp_Object
2448 make_buffer_string (ptrdiff_t start, ptrdiff_t end, int props)
2450 ptrdiff_t start_byte = CHAR_TO_BYTE (start);
2451 ptrdiff_t end_byte = CHAR_TO_BYTE (end);
2453 return make_buffer_string_both (start, start_byte, end, end_byte, props);
2456 /* Return a Lisp_String containing the text of the current buffer from
2457 START / START_BYTE to END / END_BYTE.
2459 If text properties are in use and the current buffer
2460 has properties in the range specified, the resulting string will also
2461 have them, if PROPS is nonzero.
2463 We don't want to use plain old make_string here, because it calls
2464 make_uninit_string, which can cause the buffer arena to be
2465 compacted. make_string has no way of knowing that the data has
2466 been moved, and thus copies the wrong data into the string. This
2467 doesn't effect most of the other users of make_string, so it should
2468 be left as is. But we should use this function when conjuring
2469 buffer substrings. */
2471 Lisp_Object
2472 make_buffer_string_both (ptrdiff_t start, ptrdiff_t start_byte,
2473 ptrdiff_t end, ptrdiff_t end_byte, int props)
2475 Lisp_Object result, tem, tem1;
2477 if (start < GPT && GPT < end)
2478 move_gap (start);
2480 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2481 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
2482 else
2483 result = make_uninit_string (end - start);
2484 memcpy (SDATA (result), BYTE_POS_ADDR (start_byte), end_byte - start_byte);
2486 /* If desired, update and copy the text properties. */
2487 if (props)
2489 update_buffer_properties (start, end);
2491 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
2492 tem1 = Ftext_properties_at (make_number (start), Qnil);
2494 if (XINT (tem) != end || !NILP (tem1))
2495 copy_intervals_to_string (result, current_buffer, start,
2496 end - start);
2499 return result;
2502 /* Call Vbuffer_access_fontify_functions for the range START ... END
2503 in the current buffer, if necessary. */
2505 static void
2506 update_buffer_properties (ptrdiff_t start, ptrdiff_t end)
2508 /* If this buffer has some access functions,
2509 call them, specifying the range of the buffer being accessed. */
2510 if (!NILP (Vbuffer_access_fontify_functions))
2512 Lisp_Object args[3];
2513 Lisp_Object tem;
2515 args[0] = Qbuffer_access_fontify_functions;
2516 XSETINT (args[1], start);
2517 XSETINT (args[2], end);
2519 /* But don't call them if we can tell that the work
2520 has already been done. */
2521 if (!NILP (Vbuffer_access_fontified_property))
2523 tem = Ftext_property_any (args[1], args[2],
2524 Vbuffer_access_fontified_property,
2525 Qnil, Qnil);
2526 if (! NILP (tem))
2527 Frun_hook_with_args (3, args);
2529 else
2530 Frun_hook_with_args (3, args);
2534 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2535 doc: /* Return the contents of part of the current buffer as a string.
2536 The two arguments START and END are character positions;
2537 they can be in either order.
2538 The string returned is multibyte if the buffer is multibyte.
2540 This function copies the text properties of that part of the buffer
2541 into the result string; if you don't want the text properties,
2542 use `buffer-substring-no-properties' instead. */)
2543 (Lisp_Object start, Lisp_Object end)
2545 register ptrdiff_t b, e;
2547 validate_region (&start, &end);
2548 b = XINT (start);
2549 e = XINT (end);
2551 return make_buffer_string (b, e, 1);
2554 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2555 Sbuffer_substring_no_properties, 2, 2, 0,
2556 doc: /* Return the characters of part of the buffer, without the text properties.
2557 The two arguments START and END are character positions;
2558 they can be in either order. */)
2559 (Lisp_Object start, Lisp_Object end)
2561 register ptrdiff_t b, e;
2563 validate_region (&start, &end);
2564 b = XINT (start);
2565 e = XINT (end);
2567 return make_buffer_string (b, e, 0);
2570 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2571 doc: /* Return the contents of the current buffer as a string.
2572 If narrowing is in effect, this function returns only the visible part
2573 of the buffer. */)
2574 (void)
2576 return make_buffer_string (BEGV, ZV, 1);
2579 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2580 1, 3, 0,
2581 doc: /* Insert before point a substring of the contents of BUFFER.
2582 BUFFER may be a buffer or a buffer name.
2583 Arguments START and END are character positions specifying the substring.
2584 They default to the values of (point-min) and (point-max) in BUFFER. */)
2585 (Lisp_Object buffer, Lisp_Object start, Lisp_Object end)
2587 register EMACS_INT b, e, temp;
2588 register struct buffer *bp, *obuf;
2589 Lisp_Object buf;
2591 buf = Fget_buffer (buffer);
2592 if (NILP (buf))
2593 nsberror (buffer);
2594 bp = XBUFFER (buf);
2595 if (NILP (BVAR (bp, name)))
2596 error ("Selecting deleted buffer");
2598 if (NILP (start))
2599 b = BUF_BEGV (bp);
2600 else
2602 CHECK_NUMBER_COERCE_MARKER (start);
2603 b = XINT (start);
2605 if (NILP (end))
2606 e = BUF_ZV (bp);
2607 else
2609 CHECK_NUMBER_COERCE_MARKER (end);
2610 e = XINT (end);
2613 if (b > e)
2614 temp = b, b = e, e = temp;
2616 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2617 args_out_of_range (start, end);
2619 obuf = current_buffer;
2620 set_buffer_internal_1 (bp);
2621 update_buffer_properties (b, e);
2622 set_buffer_internal_1 (obuf);
2624 insert_from_buffer (bp, b, e - b, 0);
2625 return Qnil;
2628 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2629 6, 6, 0,
2630 doc: /* Compare two substrings of two buffers; return result as number.
2631 the value is -N if first string is less after N-1 chars,
2632 +N if first string is greater after N-1 chars, or 0 if strings match.
2633 Each substring is represented as three arguments: BUFFER, START and END.
2634 That makes six args in all, three for each substring.
2636 The value of `case-fold-search' in the current buffer
2637 determines whether case is significant or ignored. */)
2638 (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2)
2640 register EMACS_INT begp1, endp1, begp2, endp2, temp;
2641 register struct buffer *bp1, *bp2;
2642 register Lisp_Object trt
2643 = (!NILP (BVAR (current_buffer, case_fold_search))
2644 ? BVAR (current_buffer, case_canon_table) : Qnil);
2645 ptrdiff_t chars = 0;
2646 ptrdiff_t i1, i2, i1_byte, i2_byte;
2648 /* Find the first buffer and its substring. */
2650 if (NILP (buffer1))
2651 bp1 = current_buffer;
2652 else
2654 Lisp_Object buf1;
2655 buf1 = Fget_buffer (buffer1);
2656 if (NILP (buf1))
2657 nsberror (buffer1);
2658 bp1 = XBUFFER (buf1);
2659 if (NILP (BVAR (bp1, name)))
2660 error ("Selecting deleted buffer");
2663 if (NILP (start1))
2664 begp1 = BUF_BEGV (bp1);
2665 else
2667 CHECK_NUMBER_COERCE_MARKER (start1);
2668 begp1 = XINT (start1);
2670 if (NILP (end1))
2671 endp1 = BUF_ZV (bp1);
2672 else
2674 CHECK_NUMBER_COERCE_MARKER (end1);
2675 endp1 = XINT (end1);
2678 if (begp1 > endp1)
2679 temp = begp1, begp1 = endp1, endp1 = temp;
2681 if (!(BUF_BEGV (bp1) <= begp1
2682 && begp1 <= endp1
2683 && endp1 <= BUF_ZV (bp1)))
2684 args_out_of_range (start1, end1);
2686 /* Likewise for second substring. */
2688 if (NILP (buffer2))
2689 bp2 = current_buffer;
2690 else
2692 Lisp_Object buf2;
2693 buf2 = Fget_buffer (buffer2);
2694 if (NILP (buf2))
2695 nsberror (buffer2);
2696 bp2 = XBUFFER (buf2);
2697 if (NILP (BVAR (bp2, name)))
2698 error ("Selecting deleted buffer");
2701 if (NILP (start2))
2702 begp2 = BUF_BEGV (bp2);
2703 else
2705 CHECK_NUMBER_COERCE_MARKER (start2);
2706 begp2 = XINT (start2);
2708 if (NILP (end2))
2709 endp2 = BUF_ZV (bp2);
2710 else
2712 CHECK_NUMBER_COERCE_MARKER (end2);
2713 endp2 = XINT (end2);
2716 if (begp2 > endp2)
2717 temp = begp2, begp2 = endp2, endp2 = temp;
2719 if (!(BUF_BEGV (bp2) <= begp2
2720 && begp2 <= endp2
2721 && endp2 <= BUF_ZV (bp2)))
2722 args_out_of_range (start2, end2);
2724 i1 = begp1;
2725 i2 = begp2;
2726 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2727 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2729 while (i1 < endp1 && i2 < endp2)
2731 /* When we find a mismatch, we must compare the
2732 characters, not just the bytes. */
2733 int c1, c2;
2735 QUIT;
2737 if (! NILP (BVAR (bp1, enable_multibyte_characters)))
2739 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2740 BUF_INC_POS (bp1, i1_byte);
2741 i1++;
2743 else
2745 c1 = BUF_FETCH_BYTE (bp1, i1);
2746 MAKE_CHAR_MULTIBYTE (c1);
2747 i1++;
2750 if (! NILP (BVAR (bp2, enable_multibyte_characters)))
2752 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2753 BUF_INC_POS (bp2, i2_byte);
2754 i2++;
2756 else
2758 c2 = BUF_FETCH_BYTE (bp2, i2);
2759 MAKE_CHAR_MULTIBYTE (c2);
2760 i2++;
2763 if (!NILP (trt))
2765 c1 = CHAR_TABLE_TRANSLATE (trt, c1);
2766 c2 = CHAR_TABLE_TRANSLATE (trt, c2);
2768 if (c1 < c2)
2769 return make_number (- 1 - chars);
2770 if (c1 > c2)
2771 return make_number (chars + 1);
2773 chars++;
2776 /* The strings match as far as they go.
2777 If one is shorter, that one is less. */
2778 if (chars < endp1 - begp1)
2779 return make_number (chars + 1);
2780 else if (chars < endp2 - begp2)
2781 return make_number (- chars - 1);
2783 /* Same length too => they are equal. */
2784 return make_number (0);
2787 static Lisp_Object
2788 subst_char_in_region_unwind (Lisp_Object arg)
2790 return BVAR (current_buffer, undo_list) = arg;
2793 static Lisp_Object
2794 subst_char_in_region_unwind_1 (Lisp_Object arg)
2796 return BVAR (current_buffer, filename) = arg;
2799 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2800 Ssubst_char_in_region, 4, 5, 0,
2801 doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs.
2802 If optional arg NOUNDO is non-nil, don't record this change for undo
2803 and don't mark the buffer as really changed.
2804 Both characters must have the same length of multi-byte form. */)
2805 (Lisp_Object start, Lisp_Object end, Lisp_Object fromchar, Lisp_Object tochar, Lisp_Object noundo)
2807 register ptrdiff_t pos, pos_byte, stop, i, len, end_byte;
2808 /* Keep track of the first change in the buffer:
2809 if 0 we haven't found it yet.
2810 if < 0 we've found it and we've run the before-change-function.
2811 if > 0 we've actually performed it and the value is its position. */
2812 ptrdiff_t changed = 0;
2813 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2814 unsigned char *p;
2815 ptrdiff_t count = SPECPDL_INDEX ();
2816 #define COMBINING_NO 0
2817 #define COMBINING_BEFORE 1
2818 #define COMBINING_AFTER 2
2819 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2820 int maybe_byte_combining = COMBINING_NO;
2821 ptrdiff_t last_changed = 0;
2822 int multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2823 int fromc, toc;
2825 restart:
2827 validate_region (&start, &end);
2828 CHECK_CHARACTER (fromchar);
2829 CHECK_CHARACTER (tochar);
2830 fromc = XFASTINT (fromchar);
2831 toc = XFASTINT (tochar);
2833 if (multibyte_p)
2835 len = CHAR_STRING (fromc, fromstr);
2836 if (CHAR_STRING (toc, tostr) != len)
2837 error ("Characters in `subst-char-in-region' have different byte-lengths");
2838 if (!ASCII_BYTE_P (*tostr))
2840 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2841 complete multibyte character, it may be combined with the
2842 after bytes. If it is in the range 0xA0..0xFF, it may be
2843 combined with the before and after bytes. */
2844 if (!CHAR_HEAD_P (*tostr))
2845 maybe_byte_combining = COMBINING_BOTH;
2846 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2847 maybe_byte_combining = COMBINING_AFTER;
2850 else
2852 len = 1;
2853 fromstr[0] = fromc;
2854 tostr[0] = toc;
2857 pos = XINT (start);
2858 pos_byte = CHAR_TO_BYTE (pos);
2859 stop = CHAR_TO_BYTE (XINT (end));
2860 end_byte = stop;
2862 /* If we don't want undo, turn off putting stuff on the list.
2863 That's faster than getting rid of things,
2864 and it prevents even the entry for a first change.
2865 Also inhibit locking the file. */
2866 if (!changed && !NILP (noundo))
2868 record_unwind_protect (subst_char_in_region_unwind,
2869 BVAR (current_buffer, undo_list));
2870 BVAR (current_buffer, undo_list) = Qt;
2871 /* Don't do file-locking. */
2872 record_unwind_protect (subst_char_in_region_unwind_1,
2873 BVAR (current_buffer, filename));
2874 BVAR (current_buffer, filename) = Qnil;
2877 if (pos_byte < GPT_BYTE)
2878 stop = min (stop, GPT_BYTE);
2879 while (1)
2881 ptrdiff_t pos_byte_next = pos_byte;
2883 if (pos_byte >= stop)
2885 if (pos_byte >= end_byte) break;
2886 stop = end_byte;
2888 p = BYTE_POS_ADDR (pos_byte);
2889 if (multibyte_p)
2890 INC_POS (pos_byte_next);
2891 else
2892 ++pos_byte_next;
2893 if (pos_byte_next - pos_byte == len
2894 && p[0] == fromstr[0]
2895 && (len == 1
2896 || (p[1] == fromstr[1]
2897 && (len == 2 || (p[2] == fromstr[2]
2898 && (len == 3 || p[3] == fromstr[3]))))))
2900 if (changed < 0)
2901 /* We've already seen this and run the before-change-function;
2902 this time we only need to record the actual position. */
2903 changed = pos;
2904 else if (!changed)
2906 changed = -1;
2907 modify_region (current_buffer, pos, XINT (end), 0);
2909 if (! NILP (noundo))
2911 if (MODIFF - 1 == SAVE_MODIFF)
2912 SAVE_MODIFF++;
2913 if (MODIFF - 1 == BUF_AUTOSAVE_MODIFF (current_buffer))
2914 BUF_AUTOSAVE_MODIFF (current_buffer)++;
2917 /* The before-change-function may have moved the gap
2918 or even modified the buffer so we should start over. */
2919 goto restart;
2922 /* Take care of the case where the new character
2923 combines with neighboring bytes. */
2924 if (maybe_byte_combining
2925 && (maybe_byte_combining == COMBINING_AFTER
2926 ? (pos_byte_next < Z_BYTE
2927 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2928 : ((pos_byte_next < Z_BYTE
2929 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2930 || (pos_byte > BEG_BYTE
2931 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2933 Lisp_Object tem, string;
2935 struct gcpro gcpro1;
2937 tem = BVAR (current_buffer, undo_list);
2938 GCPRO1 (tem);
2940 /* Make a multibyte string containing this single character. */
2941 string = make_multibyte_string ((char *) tostr, 1, len);
2942 /* replace_range is less efficient, because it moves the gap,
2943 but it handles combining correctly. */
2944 replace_range (pos, pos + 1, string,
2945 0, 0, 1);
2946 pos_byte_next = CHAR_TO_BYTE (pos);
2947 if (pos_byte_next > pos_byte)
2948 /* Before combining happened. We should not increment
2949 POS. So, to cancel the later increment of POS,
2950 decrease it now. */
2951 pos--;
2952 else
2953 INC_POS (pos_byte_next);
2955 if (! NILP (noundo))
2956 BVAR (current_buffer, undo_list) = tem;
2958 UNGCPRO;
2960 else
2962 if (NILP (noundo))
2963 record_change (pos, 1);
2964 for (i = 0; i < len; i++) *p++ = tostr[i];
2966 last_changed = pos + 1;
2968 pos_byte = pos_byte_next;
2969 pos++;
2972 if (changed > 0)
2974 signal_after_change (changed,
2975 last_changed - changed, last_changed - changed);
2976 update_compositions (changed, last_changed, CHECK_ALL);
2979 unbind_to (count, Qnil);
2980 return Qnil;
2984 static Lisp_Object check_translation (ptrdiff_t, ptrdiff_t, ptrdiff_t,
2985 Lisp_Object);
2987 /* Helper function for Ftranslate_region_internal.
2989 Check if a character sequence at POS (POS_BYTE) matches an element
2990 of VAL. VAL is a list (([FROM-CHAR ...] . TO) ...). If a matching
2991 element is found, return it. Otherwise return Qnil. */
2993 static Lisp_Object
2994 check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end,
2995 Lisp_Object val)
2997 int buf_size = 16, buf_used = 0;
2998 int *buf = alloca (sizeof (int) * buf_size);
3000 for (; CONSP (val); val = XCDR (val))
3002 Lisp_Object elt;
3003 ptrdiff_t len, i;
3005 elt = XCAR (val);
3006 if (! CONSP (elt))
3007 continue;
3008 elt = XCAR (elt);
3009 if (! VECTORP (elt))
3010 continue;
3011 len = ASIZE (elt);
3012 if (len <= end - pos)
3014 for (i = 0; i < len; i++)
3016 if (buf_used <= i)
3018 unsigned char *p = BYTE_POS_ADDR (pos_byte);
3019 int len1;
3021 if (buf_used == buf_size)
3023 int *newbuf;
3025 buf_size += 16;
3026 newbuf = alloca (sizeof (int) * buf_size);
3027 memcpy (newbuf, buf, sizeof (int) * buf_used);
3028 buf = newbuf;
3030 buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1);
3031 pos_byte += len1;
3033 if (XINT (AREF (elt, i)) != buf[i])
3034 break;
3036 if (i == len)
3037 return XCAR (val);
3040 return Qnil;
3044 DEFUN ("translate-region-internal", Ftranslate_region_internal,
3045 Stranslate_region_internal, 3, 3, 0,
3046 doc: /* Internal use only.
3047 From START to END, translate characters according to TABLE.
3048 TABLE is a string or a char-table; the Nth character in it is the
3049 mapping for the character with code N.
3050 It returns the number of characters changed. */)
3051 (Lisp_Object start, Lisp_Object end, register Lisp_Object table)
3053 register unsigned char *tt; /* Trans table. */
3054 register int nc; /* New character. */
3055 int cnt; /* Number of changes made. */
3056 ptrdiff_t size; /* Size of translate table. */
3057 ptrdiff_t pos, pos_byte, end_pos;
3058 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
3059 int string_multibyte IF_LINT (= 0);
3061 validate_region (&start, &end);
3062 if (CHAR_TABLE_P (table))
3064 if (! EQ (XCHAR_TABLE (table)->purpose, Qtranslation_table))
3065 error ("Not a translation table");
3066 size = MAX_CHAR;
3067 tt = NULL;
3069 else
3071 CHECK_STRING (table);
3073 if (! multibyte && (SCHARS (table) < SBYTES (table)))
3074 table = string_make_unibyte (table);
3075 string_multibyte = SCHARS (table) < SBYTES (table);
3076 size = SBYTES (table);
3077 tt = SDATA (table);
3080 pos = XINT (start);
3081 pos_byte = CHAR_TO_BYTE (pos);
3082 end_pos = XINT (end);
3083 modify_region (current_buffer, pos, end_pos, 0);
3085 cnt = 0;
3086 for (; pos < end_pos; )
3088 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
3089 unsigned char *str, buf[MAX_MULTIBYTE_LENGTH];
3090 int len, str_len;
3091 int oc;
3092 Lisp_Object val;
3094 if (multibyte)
3095 oc = STRING_CHAR_AND_LENGTH (p, len);
3096 else
3097 oc = *p, len = 1;
3098 if (oc < size)
3100 if (tt)
3102 /* Reload as signal_after_change in last iteration may GC. */
3103 tt = SDATA (table);
3104 if (string_multibyte)
3106 str = tt + string_char_to_byte (table, oc);
3107 nc = STRING_CHAR_AND_LENGTH (str, str_len);
3109 else
3111 nc = tt[oc];
3112 if (! ASCII_BYTE_P (nc) && multibyte)
3114 str_len = BYTE8_STRING (nc, buf);
3115 str = buf;
3117 else
3119 str_len = 1;
3120 str = tt + oc;
3124 else
3126 nc = oc;
3127 val = CHAR_TABLE_REF (table, oc);
3128 if (CHARACTERP (val))
3130 nc = XFASTINT (val);
3131 str_len = CHAR_STRING (nc, buf);
3132 str = buf;
3134 else if (VECTORP (val) || (CONSP (val)))
3136 /* VAL is [TO_CHAR ...] or (([FROM-CHAR ...] . TO) ...)
3137 where TO is TO-CHAR or [TO-CHAR ...]. */
3138 nc = -1;
3142 if (nc != oc && nc >= 0)
3144 /* Simple one char to one char translation. */
3145 if (len != str_len)
3147 Lisp_Object string;
3149 /* This is less efficient, because it moves the gap,
3150 but it should handle multibyte characters correctly. */
3151 string = make_multibyte_string ((char *) str, 1, str_len);
3152 replace_range (pos, pos + 1, string, 1, 0, 1);
3153 len = str_len;
3155 else
3157 record_change (pos, 1);
3158 while (str_len-- > 0)
3159 *p++ = *str++;
3160 signal_after_change (pos, 1, 1);
3161 update_compositions (pos, pos + 1, CHECK_BORDER);
3163 ++cnt;
3165 else if (nc < 0)
3167 Lisp_Object string;
3169 if (CONSP (val))
3171 val = check_translation (pos, pos_byte, end_pos, val);
3172 if (NILP (val))
3174 pos_byte += len;
3175 pos++;
3176 continue;
3178 /* VAL is ([FROM-CHAR ...] . TO). */
3179 len = ASIZE (XCAR (val));
3180 val = XCDR (val);
3182 else
3183 len = 1;
3185 if (VECTORP (val))
3187 string = Fconcat (1, &val);
3189 else
3191 string = Fmake_string (make_number (1), val);
3193 replace_range (pos, pos + len, string, 1, 0, 1);
3194 pos_byte += SBYTES (string);
3195 pos += SCHARS (string);
3196 cnt += SCHARS (string);
3197 end_pos += SCHARS (string) - len;
3198 continue;
3201 pos_byte += len;
3202 pos++;
3205 return make_number (cnt);
3208 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
3209 doc: /* Delete the text between START and END.
3210 If called interactively, delete the region between point and mark.
3211 This command deletes buffer text without modifying the kill ring. */)
3212 (Lisp_Object start, Lisp_Object end)
3214 validate_region (&start, &end);
3215 del_range (XINT (start), XINT (end));
3216 return Qnil;
3219 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
3220 Sdelete_and_extract_region, 2, 2, 0,
3221 doc: /* Delete the text between START and END and return it. */)
3222 (Lisp_Object start, Lisp_Object end)
3224 validate_region (&start, &end);
3225 if (XINT (start) == XINT (end))
3226 return empty_unibyte_string;
3227 return del_range_1 (XINT (start), XINT (end), 1, 1);
3230 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
3231 doc: /* Remove restrictions (narrowing) from current buffer.
3232 This allows the buffer's full text to be seen and edited. */)
3233 (void)
3235 if (BEG != BEGV || Z != ZV)
3236 current_buffer->clip_changed = 1;
3237 BEGV = BEG;
3238 BEGV_BYTE = BEG_BYTE;
3239 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
3240 /* Changing the buffer bounds invalidates any recorded current column. */
3241 invalidate_current_column ();
3242 return Qnil;
3245 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
3246 doc: /* Restrict editing in this buffer to the current region.
3247 The rest of the text becomes temporarily invisible and untouchable
3248 but is not deleted; if you save the buffer in a file, the invisible
3249 text is included in the file. \\[widen] makes all visible again.
3250 See also `save-restriction'.
3252 When calling from a program, pass two arguments; positions (integers
3253 or markers) bounding the text that should remain visible. */)
3254 (register Lisp_Object start, Lisp_Object end)
3256 CHECK_NUMBER_COERCE_MARKER (start);
3257 CHECK_NUMBER_COERCE_MARKER (end);
3259 if (XINT (start) > XINT (end))
3261 Lisp_Object tem;
3262 tem = start; start = end; end = tem;
3265 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
3266 args_out_of_range (start, end);
3268 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
3269 current_buffer->clip_changed = 1;
3271 SET_BUF_BEGV (current_buffer, XFASTINT (start));
3272 SET_BUF_ZV (current_buffer, XFASTINT (end));
3273 if (PT < XFASTINT (start))
3274 SET_PT (XFASTINT (start));
3275 if (PT > XFASTINT (end))
3276 SET_PT (XFASTINT (end));
3277 /* Changing the buffer bounds invalidates any recorded current column. */
3278 invalidate_current_column ();
3279 return Qnil;
3282 Lisp_Object
3283 save_restriction_save (void)
3285 if (BEGV == BEG && ZV == Z)
3286 /* The common case that the buffer isn't narrowed.
3287 We return just the buffer object, which save_restriction_restore
3288 recognizes as meaning `no restriction'. */
3289 return Fcurrent_buffer ();
3290 else
3291 /* We have to save a restriction, so return a pair of markers, one
3292 for the beginning and one for the end. */
3294 Lisp_Object beg, end;
3296 beg = buildmark (BEGV, BEGV_BYTE);
3297 end = buildmark (ZV, ZV_BYTE);
3299 /* END must move forward if text is inserted at its exact location. */
3300 XMARKER (end)->insertion_type = 1;
3302 return Fcons (beg, end);
3306 Lisp_Object
3307 save_restriction_restore (Lisp_Object data)
3309 struct buffer *cur = NULL;
3310 struct buffer *buf = (CONSP (data)
3311 ? XMARKER (XCAR (data))->buffer
3312 : XBUFFER (data));
3314 if (buf && buf != current_buffer && !NILP (BVAR (buf, pt_marker)))
3315 { /* If `buf' uses markers to keep track of PT, BEGV, and ZV (as
3316 is the case if it is or has an indirect buffer), then make
3317 sure it is current before we update BEGV, so
3318 set_buffer_internal takes care of managing those markers. */
3319 cur = current_buffer;
3320 set_buffer_internal (buf);
3323 if (CONSP (data))
3324 /* A pair of marks bounding a saved restriction. */
3326 struct Lisp_Marker *beg = XMARKER (XCAR (data));
3327 struct Lisp_Marker *end = XMARKER (XCDR (data));
3328 eassert (buf == end->buffer);
3330 if (buf /* Verify marker still points to a buffer. */
3331 && (beg->charpos != BUF_BEGV (buf) || end->charpos != BUF_ZV (buf)))
3332 /* The restriction has changed from the saved one, so restore
3333 the saved restriction. */
3335 ptrdiff_t pt = BUF_PT (buf);
3337 SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
3338 SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
3340 if (pt < beg->charpos || pt > end->charpos)
3341 /* The point is outside the new visible range, move it inside. */
3342 SET_BUF_PT_BOTH (buf,
3343 clip_to_bounds (beg->charpos, pt, end->charpos),
3344 clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf),
3345 end->bytepos));
3347 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3350 else
3351 /* A buffer, which means that there was no old restriction. */
3353 if (buf /* Verify marker still points to a buffer. */
3354 && (BUF_BEGV (buf) != BUF_BEG (buf) || BUF_ZV (buf) != BUF_Z (buf)))
3355 /* The buffer has been narrowed, get rid of the narrowing. */
3357 SET_BUF_BEGV_BOTH (buf, BUF_BEG (buf), BUF_BEG_BYTE (buf));
3358 SET_BUF_ZV_BOTH (buf, BUF_Z (buf), BUF_Z_BYTE (buf));
3360 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3364 /* Changing the buffer bounds invalidates any recorded current column. */
3365 invalidate_current_column ();
3367 if (cur)
3368 set_buffer_internal (cur);
3370 return Qnil;
3373 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
3374 doc: /* Execute BODY, saving and restoring current buffer's restrictions.
3375 The buffer's restrictions make parts of the beginning and end invisible.
3376 \(They are set up with `narrow-to-region' and eliminated with `widen'.)
3377 This special form, `save-restriction', saves the current buffer's restrictions
3378 when it is entered, and restores them when it is exited.
3379 So any `narrow-to-region' within BODY lasts only until the end of the form.
3380 The old restrictions settings are restored
3381 even in case of abnormal exit (throw or error).
3383 The value returned is the value of the last form in BODY.
3385 Note: if you are using both `save-excursion' and `save-restriction',
3386 use `save-excursion' outermost:
3387 (save-excursion (save-restriction ...))
3389 usage: (save-restriction &rest BODY) */)
3390 (Lisp_Object body)
3392 register Lisp_Object val;
3393 ptrdiff_t count = SPECPDL_INDEX ();
3395 record_unwind_protect (save_restriction_restore, save_restriction_save ());
3396 val = Fprogn (body);
3397 return unbind_to (count, val);
3400 /* Buffer for the most recent text displayed by Fmessage_box. */
3401 static char *message_text;
3403 /* Allocated length of that buffer. */
3404 static ptrdiff_t message_length;
3406 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
3407 doc: /* Display a message at the bottom of the screen.
3408 The message also goes into the `*Messages*' buffer.
3409 \(In keyboard macros, that's all it does.)
3410 Return the message.
3412 The first argument is a format control string, and the rest are data
3413 to be formatted under control of the string. See `format' for details.
3415 Note: Use (message "%s" VALUE) to print the value of expressions and
3416 variables to avoid accidentally interpreting `%' as format specifiers.
3418 If the first argument is nil or the empty string, the function clears
3419 any existing message; this lets the minibuffer contents show. See
3420 also `current-message'.
3422 usage: (message FORMAT-STRING &rest ARGS) */)
3423 (ptrdiff_t nargs, Lisp_Object *args)
3425 if (NILP (args[0])
3426 || (STRINGP (args[0])
3427 && SBYTES (args[0]) == 0))
3429 message (0);
3430 return args[0];
3432 else
3434 register Lisp_Object val;
3435 val = Fformat (nargs, args);
3436 message3 (val, SBYTES (val), STRING_MULTIBYTE (val));
3437 return val;
3441 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
3442 doc: /* Display a message, in a dialog box if possible.
3443 If a dialog box is not available, use the echo area.
3444 The first argument is a format control string, and the rest are data
3445 to be formatted under control of the string. See `format' for details.
3447 If the first argument is nil or the empty string, clear any existing
3448 message; let the minibuffer contents show.
3450 usage: (message-box FORMAT-STRING &rest ARGS) */)
3451 (ptrdiff_t nargs, Lisp_Object *args)
3453 if (NILP (args[0]))
3455 message (0);
3456 return Qnil;
3458 else
3460 register Lisp_Object val;
3461 val = Fformat (nargs, args);
3462 #ifdef HAVE_MENUS
3463 /* The MS-DOS frames support popup menus even though they are
3464 not FRAME_WINDOW_P. */
3465 if (FRAME_WINDOW_P (XFRAME (selected_frame))
3466 || FRAME_MSDOS_P (XFRAME (selected_frame)))
3468 Lisp_Object pane, menu;
3469 struct gcpro gcpro1;
3470 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
3471 GCPRO1 (pane);
3472 menu = Fcons (val, pane);
3473 Fx_popup_dialog (Qt, menu, Qt);
3474 UNGCPRO;
3475 return val;
3477 #endif /* HAVE_MENUS */
3478 /* Copy the data so that it won't move when we GC. */
3479 if (! message_text)
3481 message_text = (char *)xmalloc (80);
3482 message_length = 80;
3484 if (SBYTES (val) > message_length)
3486 message_text = (char *) xrealloc (message_text, SBYTES (val));
3487 message_length = SBYTES (val);
3489 memcpy (message_text, SDATA (val), SBYTES (val));
3490 message2 (message_text, SBYTES (val),
3491 STRING_MULTIBYTE (val));
3492 return val;
3496 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
3497 doc: /* Display a message in a dialog box or in the echo area.
3498 If this command was invoked with the mouse, use a dialog box if
3499 `use-dialog-box' is non-nil.
3500 Otherwise, use the echo area.
3501 The first argument is a format control string, and the rest are data
3502 to be formatted under control of the string. See `format' for details.
3504 If the first argument is nil or the empty string, clear any existing
3505 message; let the minibuffer contents show.
3507 usage: (message-or-box FORMAT-STRING &rest ARGS) */)
3508 (ptrdiff_t nargs, Lisp_Object *args)
3510 #ifdef HAVE_MENUS
3511 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3512 && use_dialog_box)
3513 return Fmessage_box (nargs, args);
3514 #endif
3515 return Fmessage (nargs, args);
3518 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
3519 doc: /* Return the string currently displayed in the echo area, or nil if none. */)
3520 (void)
3522 return current_message ();
3526 DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0,
3527 doc: /* Return a copy of STRING with text properties added.
3528 First argument is the string to copy.
3529 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
3530 properties to add to the result.
3531 usage: (propertize STRING &rest PROPERTIES) */)
3532 (ptrdiff_t nargs, Lisp_Object *args)
3534 Lisp_Object properties, string;
3535 struct gcpro gcpro1, gcpro2;
3536 ptrdiff_t i;
3538 /* Number of args must be odd. */
3539 if ((nargs & 1) == 0)
3540 error ("Wrong number of arguments");
3542 properties = string = Qnil;
3543 GCPRO2 (properties, string);
3545 /* First argument must be a string. */
3546 CHECK_STRING (args[0]);
3547 string = Fcopy_sequence (args[0]);
3549 for (i = 1; i < nargs; i += 2)
3550 properties = Fcons (args[i], Fcons (args[i + 1], properties));
3552 Fadd_text_properties (make_number (0),
3553 make_number (SCHARS (string)),
3554 properties, string);
3555 RETURN_UNGCPRO (string);
3558 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3559 doc: /* Format a string out of a format-string and arguments.
3560 The first argument is a format control string.
3561 The other arguments are substituted into it to make the result, a string.
3563 The format control string may contain %-sequences meaning to substitute
3564 the next available argument:
3566 %s means print a string argument. Actually, prints any object, with `princ'.
3567 %d means print as number in decimal (%o octal, %x hex).
3568 %X is like %x, but uses upper case.
3569 %e means print a number in exponential notation.
3570 %f means print a number in decimal-point notation.
3571 %g means print a number in exponential notation
3572 or decimal-point notation, whichever uses fewer characters.
3573 %c means print a number as a single character.
3574 %S means print any object as an s-expression (using `prin1').
3576 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.
3577 Use %% to put a single % into the output.
3579 A %-sequence may contain optional flag, width, and precision
3580 specifiers, as follows:
3582 %<flags><width><precision>character
3584 where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+
3586 The + flag character inserts a + before any positive number, while a
3587 space inserts a space before any positive number; these flags only
3588 affect %d, %e, %f, and %g sequences, and the + flag takes precedence.
3589 The # flag means to use an alternate display form for %o, %x, %X, %e,
3590 %f, and %g sequences. The - and 0 flags affect the width specifier,
3591 as described below.
3593 The width specifier supplies a lower limit for the length of the
3594 printed representation. The padding, if any, normally goes on the
3595 left, but it goes on the right if the - flag is present. The padding
3596 character is normally a space, but it is 0 if the 0 flag is present.
3597 The 0 flag is ignored if the - flag is present, or the format sequence
3598 is something other than %d, %e, %f, and %g.
3600 For %e, %f, and %g sequences, the number after the "." in the
3601 precision specifier says how many decimal places to show; if zero, the
3602 decimal point itself is omitted. For %s and %S, the precision
3603 specifier truncates the string to the given width.
3605 usage: (format STRING &rest OBJECTS) */)
3606 (ptrdiff_t nargs, Lisp_Object *args)
3608 ptrdiff_t n; /* The number of the next arg to substitute */
3609 char initial_buffer[4000];
3610 char *buf = initial_buffer;
3611 ptrdiff_t bufsize = sizeof initial_buffer;
3612 ptrdiff_t max_bufsize = STRING_BYTES_BOUND + 1;
3613 char *p;
3614 Lisp_Object buf_save_value IF_LINT (= {0});
3615 register char *format, *end, *format_start;
3616 ptrdiff_t formatlen, nchars;
3617 /* Nonzero if the format is multibyte. */
3618 int multibyte_format = 0;
3619 /* Nonzero if the output should be a multibyte string,
3620 which is true if any of the inputs is one. */
3621 int multibyte = 0;
3622 /* When we make a multibyte string, we must pay attention to the
3623 byte combining problem, i.e., a byte may be combined with a
3624 multibyte character of the previous string. This flag tells if we
3625 must consider such a situation or not. */
3626 int maybe_combine_byte;
3627 Lisp_Object val;
3628 int arg_intervals = 0;
3629 USE_SAFE_ALLOCA;
3631 /* discarded[I] is 1 if byte I of the format
3632 string was not copied into the output.
3633 It is 2 if byte I was not the first byte of its character. */
3634 char *discarded;
3636 /* Each element records, for one argument,
3637 the start and end bytepos in the output string,
3638 whether the argument has been converted to string (e.g., due to "%S"),
3639 and whether the argument is a string with intervals.
3640 info[0] is unused. Unused elements have -1 for start. */
3641 struct info
3643 ptrdiff_t start, end;
3644 int converted_to_string;
3645 int intervals;
3646 } *info = 0;
3648 /* It should not be necessary to GCPRO ARGS, because
3649 the caller in the interpreter should take care of that. */
3651 CHECK_STRING (args[0]);
3652 format_start = SSDATA (args[0]);
3653 formatlen = SBYTES (args[0]);
3655 /* Allocate the info and discarded tables. */
3657 ptrdiff_t i;
3658 if ((SIZE_MAX - formatlen) / sizeof (struct info) <= nargs)
3659 memory_full (SIZE_MAX);
3660 SAFE_ALLOCA (info, struct info *, (nargs + 1) * sizeof *info + formatlen);
3661 discarded = (char *) &info[nargs + 1];
3662 for (i = 0; i < nargs + 1; i++)
3664 info[i].start = -1;
3665 info[i].intervals = info[i].converted_to_string = 0;
3667 memset (discarded, 0, formatlen);
3670 /* Try to determine whether the result should be multibyte.
3671 This is not always right; sometimes the result needs to be multibyte
3672 because of an object that we will pass through prin1,
3673 and in that case, we won't know it here. */
3674 multibyte_format = STRING_MULTIBYTE (args[0]);
3675 multibyte = multibyte_format;
3676 for (n = 1; !multibyte && n < nargs; n++)
3677 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3678 multibyte = 1;
3680 /* If we start out planning a unibyte result,
3681 then discover it has to be multibyte, we jump back to retry. */
3682 retry:
3684 p = buf;
3685 nchars = 0;
3686 n = 0;
3688 /* Scan the format and store result in BUF. */
3689 format = format_start;
3690 end = format + formatlen;
3691 maybe_combine_byte = 0;
3693 while (format != end)
3695 /* The values of N and FORMAT when the loop body is entered. */
3696 ptrdiff_t n0 = n;
3697 char *format0 = format;
3699 /* Bytes needed to represent the output of this conversion. */
3700 ptrdiff_t convbytes;
3702 if (*format == '%')
3704 /* General format specifications look like
3706 '%' [flags] [field-width] [precision] format
3708 where
3710 flags ::= [-+0# ]+
3711 field-width ::= [0-9]+
3712 precision ::= '.' [0-9]*
3714 If a field-width is specified, it specifies to which width
3715 the output should be padded with blanks, if the output
3716 string is shorter than field-width.
3718 If precision is specified, it specifies the number of
3719 digits to print after the '.' for floats, or the max.
3720 number of chars to print from a string. */
3722 int minus_flag = 0;
3723 int plus_flag = 0;
3724 int space_flag = 0;
3725 int sharp_flag = 0;
3726 int zero_flag = 0;
3727 ptrdiff_t field_width;
3728 int precision_given;
3729 uintmax_t precision = UINTMAX_MAX;
3730 char *num_end;
3731 char conversion;
3733 while (1)
3735 switch (*++format)
3737 case '-': minus_flag = 1; continue;
3738 case '+': plus_flag = 1; continue;
3739 case ' ': space_flag = 1; continue;
3740 case '#': sharp_flag = 1; continue;
3741 case '0': zero_flag = 1; continue;
3743 break;
3746 /* Ignore flags when sprintf ignores them. */
3747 space_flag &= ~ plus_flag;
3748 zero_flag &= ~ minus_flag;
3751 uintmax_t w = strtoumax (format, &num_end, 10);
3752 if (max_bufsize <= w)
3753 string_overflow ();
3754 field_width = w;
3756 precision_given = *num_end == '.';
3757 if (precision_given)
3758 precision = strtoumax (num_end + 1, &num_end, 10);
3759 format = num_end;
3761 if (format == end)
3762 error ("Format string ends in middle of format specifier");
3764 memset (&discarded[format0 - format_start], 1, format - format0);
3765 conversion = *format;
3766 if (conversion == '%')
3767 goto copy_char;
3768 discarded[format - format_start] = 1;
3769 format++;
3771 ++n;
3772 if (! (n < nargs))
3773 error ("Not enough arguments for format string");
3775 /* For 'S', prin1 the argument, and then treat like 's'.
3776 For 's', princ any argument that is not a string or
3777 symbol. But don't do this conversion twice, which might
3778 happen after retrying. */
3779 if ((conversion == 'S'
3780 || (conversion == 's'
3781 && ! STRINGP (args[n]) && ! SYMBOLP (args[n]))))
3783 if (! info[n].converted_to_string)
3785 Lisp_Object noescape = conversion == 'S' ? Qnil : Qt;
3786 args[n] = Fprin1_to_string (args[n], noescape);
3787 info[n].converted_to_string = 1;
3788 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3790 multibyte = 1;
3791 goto retry;
3794 conversion = 's';
3796 else if (conversion == 'c')
3798 if (FLOATP (args[n]))
3800 double d = XFLOAT_DATA (args[n]);
3801 args[n] = make_number (FIXNUM_OVERFLOW_P (d) ? -1 : d);
3804 if (INTEGERP (args[n]) && ! ASCII_CHAR_P (XINT (args[n])))
3806 if (!multibyte)
3808 multibyte = 1;
3809 goto retry;
3811 args[n] = Fchar_to_string (args[n]);
3812 info[n].converted_to_string = 1;
3815 if (info[n].converted_to_string)
3816 conversion = 's';
3817 zero_flag = 0;
3820 if (SYMBOLP (args[n]))
3822 args[n] = SYMBOL_NAME (args[n]);
3823 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3825 multibyte = 1;
3826 goto retry;
3830 if (conversion == 's')
3832 /* handle case (precision[n] >= 0) */
3834 ptrdiff_t width, padding, nbytes;
3835 ptrdiff_t nchars_string;
3837 ptrdiff_t prec = -1;
3838 if (precision_given && precision <= TYPE_MAXIMUM (ptrdiff_t))
3839 prec = precision;
3841 /* lisp_string_width ignores a precision of 0, but GNU
3842 libc functions print 0 characters when the precision
3843 is 0. Imitate libc behavior here. Changing
3844 lisp_string_width is the right thing, and will be
3845 done, but meanwhile we work with it. */
3847 if (prec == 0)
3848 width = nchars_string = nbytes = 0;
3849 else
3851 ptrdiff_t nch, nby;
3852 width = lisp_string_width (args[n], prec, &nch, &nby);
3853 if (prec < 0)
3855 nchars_string = SCHARS (args[n]);
3856 nbytes = SBYTES (args[n]);
3858 else
3860 nchars_string = nch;
3861 nbytes = nby;
3865 convbytes = nbytes;
3866 if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n]))
3867 convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes);
3869 padding = width < field_width ? field_width - width : 0;
3871 if (max_bufsize - padding <= convbytes)
3872 string_overflow ();
3873 convbytes += padding;
3874 if (convbytes <= buf + bufsize - p)
3876 if (! minus_flag)
3878 memset (p, ' ', padding);
3879 p += padding;
3880 nchars += padding;
3883 if (p > buf
3884 && multibyte
3885 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3886 && STRING_MULTIBYTE (args[n])
3887 && !CHAR_HEAD_P (SREF (args[n], 0)))
3888 maybe_combine_byte = 1;
3890 p += copy_text (SDATA (args[n]), (unsigned char *) p,
3891 nbytes,
3892 STRING_MULTIBYTE (args[n]), multibyte);
3894 info[n].start = nchars;
3895 nchars += nchars_string;
3896 info[n].end = nchars;
3898 if (minus_flag)
3900 memset (p, ' ', padding);
3901 p += padding;
3902 nchars += padding;
3905 /* If this argument has text properties, record where
3906 in the result string it appears. */
3907 if (STRING_INTERVALS (args[n]))
3908 info[n].intervals = arg_intervals = 1;
3910 continue;
3913 else if (! (conversion == 'c' || conversion == 'd'
3914 || conversion == 'e' || conversion == 'f'
3915 || conversion == 'g' || conversion == 'i'
3916 || conversion == 'o' || conversion == 'x'
3917 || conversion == 'X'))
3918 error ("Invalid format operation %%%c",
3919 STRING_CHAR ((unsigned char *) format - 1));
3920 else if (! (INTEGERP (args[n]) || FLOATP (args[n])))
3921 error ("Format specifier doesn't match argument type");
3922 else
3924 enum
3926 /* Maximum precision for a %f conversion such that the
3927 trailing output digit might be nonzero. Any precision
3928 larger than this will not yield useful information. */
3929 USEFUL_PRECISION_MAX =
3930 ((1 - DBL_MIN_EXP)
3931 * (FLT_RADIX == 2 || FLT_RADIX == 10 ? 1
3932 : FLT_RADIX == 16 ? 4
3933 : -1)),
3935 /* Maximum number of bytes generated by any format, if
3936 precision is no more than USEFUL_PRECISION_MAX.
3937 On all practical hosts, %f is the worst case. */
3938 SPRINTF_BUFSIZE =
3939 sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX,
3941 /* Length of pM (that is, of pMd without the
3942 trailing "d"). */
3943 pMlen = sizeof pMd - 2
3945 verify (0 < USEFUL_PRECISION_MAX);
3947 int prec;
3948 ptrdiff_t padding, sprintf_bytes;
3949 uintmax_t excess_precision, numwidth;
3950 uintmax_t leading_zeros = 0, trailing_zeros = 0;
3952 char sprintf_buf[SPRINTF_BUFSIZE];
3954 /* Copy of conversion specification, modified somewhat.
3955 At most three flags F can be specified at once. */
3956 char convspec[sizeof "%FFF.*d" + pMlen];
3958 /* Avoid undefined behavior in underlying sprintf. */
3959 if (conversion == 'd' || conversion == 'i')
3960 sharp_flag = 0;
3962 /* Create the copy of the conversion specification, with
3963 any width and precision removed, with ".*" inserted,
3964 and with pM inserted for integer formats. */
3966 char *f = convspec;
3967 *f++ = '%';
3968 *f = '-'; f += minus_flag;
3969 *f = '+'; f += plus_flag;
3970 *f = ' '; f += space_flag;
3971 *f = '#'; f += sharp_flag;
3972 *f = '0'; f += zero_flag;
3973 *f++ = '.';
3974 *f++ = '*';
3975 if (conversion == 'd' || conversion == 'i'
3976 || conversion == 'o' || conversion == 'x'
3977 || conversion == 'X')
3979 memcpy (f, pMd, pMlen);
3980 f += pMlen;
3981 zero_flag &= ~ precision_given;
3983 *f++ = conversion;
3984 *f = '\0';
3987 prec = -1;
3988 if (precision_given)
3989 prec = min (precision, USEFUL_PRECISION_MAX);
3991 /* Use sprintf to format this number into sprintf_buf. Omit
3992 padding and excess precision, though, because sprintf limits
3993 output length to INT_MAX.
3995 There are four types of conversion: double, unsigned
3996 char (passed as int), wide signed int, and wide
3997 unsigned int. Treat them separately because the
3998 sprintf ABI is sensitive to which type is passed. Be
3999 careful about integer overflow, NaNs, infinities, and
4000 conversions; for example, the min and max macros are
4001 not suitable here. */
4002 if (conversion == 'e' || conversion == 'f' || conversion == 'g')
4004 double x = (INTEGERP (args[n])
4005 ? XINT (args[n])
4006 : XFLOAT_DATA (args[n]));
4007 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
4009 else if (conversion == 'c')
4011 /* Don't use sprintf here, as it might mishandle prec. */
4012 sprintf_buf[0] = XINT (args[n]);
4013 sprintf_bytes = prec != 0;
4015 else if (conversion == 'd')
4017 /* For float, maybe we should use "%1.0f"
4018 instead so it also works for values outside
4019 the integer range. */
4020 printmax_t x;
4021 if (INTEGERP (args[n]))
4022 x = XINT (args[n]);
4023 else
4025 double d = XFLOAT_DATA (args[n]);
4026 if (d < 0)
4028 x = TYPE_MINIMUM (printmax_t);
4029 if (x < d)
4030 x = d;
4032 else
4034 x = TYPE_MAXIMUM (printmax_t);
4035 if (d < x)
4036 x = d;
4039 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
4041 else
4043 /* Don't sign-extend for octal or hex printing. */
4044 uprintmax_t x;
4045 if (INTEGERP (args[n]))
4046 x = XUINT (args[n]);
4047 else
4049 double d = XFLOAT_DATA (args[n]);
4050 if (d < 0)
4051 x = 0;
4052 else
4054 x = TYPE_MAXIMUM (uprintmax_t);
4055 if (d < x)
4056 x = d;
4059 sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x);
4062 /* Now the length of the formatted item is known, except it omits
4063 padding and excess precision. Deal with excess precision
4064 first. This happens only when the format specifies
4065 ridiculously large precision. */
4066 excess_precision = precision - prec;
4067 if (excess_precision)
4069 if (conversion == 'e' || conversion == 'f'
4070 || conversion == 'g')
4072 if ((conversion == 'g' && ! sharp_flag)
4073 || ! ('0' <= sprintf_buf[sprintf_bytes - 1]
4074 && sprintf_buf[sprintf_bytes - 1] <= '9'))
4075 excess_precision = 0;
4076 else
4078 if (conversion == 'g')
4080 char *dot = strchr (sprintf_buf, '.');
4081 if (!dot)
4082 excess_precision = 0;
4085 trailing_zeros = excess_precision;
4087 else
4088 leading_zeros = excess_precision;
4091 /* Compute the total bytes needed for this item, including
4092 excess precision and padding. */
4093 numwidth = sprintf_bytes + excess_precision;
4094 padding = numwidth < field_width ? field_width - numwidth : 0;
4095 if (max_bufsize - sprintf_bytes <= excess_precision
4096 || max_bufsize - padding <= numwidth)
4097 string_overflow ();
4098 convbytes = numwidth + padding;
4100 if (convbytes <= buf + bufsize - p)
4102 /* Copy the formatted item from sprintf_buf into buf,
4103 inserting padding and excess-precision zeros. */
4105 char *src = sprintf_buf;
4106 char src0 = src[0];
4107 int exponent_bytes = 0;
4108 int signedp = src0 == '-' || src0 == '+' || src0 == ' ';
4109 int significand_bytes;
4110 if (zero_flag
4111 && ((src[signedp] >= '0' && src[signedp] <= '9')
4112 || (src[signedp] >= 'a' && src[signedp] <= 'f')
4113 || (src[signedp] >= 'A' && src[signedp] <= 'F')))
4115 leading_zeros += padding;
4116 padding = 0;
4119 if (excess_precision
4120 && (conversion == 'e' || conversion == 'g'))
4122 char *e = strchr (src, 'e');
4123 if (e)
4124 exponent_bytes = src + sprintf_bytes - e;
4127 if (! minus_flag)
4129 memset (p, ' ', padding);
4130 p += padding;
4131 nchars += padding;
4134 *p = src0;
4135 src += signedp;
4136 p += signedp;
4137 memset (p, '0', leading_zeros);
4138 p += leading_zeros;
4139 significand_bytes = sprintf_bytes - signedp - exponent_bytes;
4140 memcpy (p, src, significand_bytes);
4141 p += significand_bytes;
4142 src += significand_bytes;
4143 memset (p, '0', trailing_zeros);
4144 p += trailing_zeros;
4145 memcpy (p, src, exponent_bytes);
4146 p += exponent_bytes;
4148 info[n].start = nchars;
4149 nchars += leading_zeros + sprintf_bytes + trailing_zeros;
4150 info[n].end = nchars;
4152 if (minus_flag)
4154 memset (p, ' ', padding);
4155 p += padding;
4156 nchars += padding;
4159 continue;
4163 else
4164 copy_char:
4166 /* Copy a single character from format to buf. */
4168 char *src = format;
4169 unsigned char str[MAX_MULTIBYTE_LENGTH];
4171 if (multibyte_format)
4173 /* Copy a whole multibyte character. */
4174 if (p > buf
4175 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
4176 && !CHAR_HEAD_P (*format))
4177 maybe_combine_byte = 1;
4180 format++;
4181 while (! CHAR_HEAD_P (*format));
4183 convbytes = format - src;
4184 memset (&discarded[src + 1 - format_start], 2, convbytes - 1);
4186 else
4188 unsigned char uc = *format++;
4189 if (! multibyte || ASCII_BYTE_P (uc))
4190 convbytes = 1;
4191 else
4193 int c = BYTE8_TO_CHAR (uc);
4194 convbytes = CHAR_STRING (c, str);
4195 src = (char *) str;
4199 if (convbytes <= buf + bufsize - p)
4201 memcpy (p, src, convbytes);
4202 p += convbytes;
4203 nchars++;
4204 continue;
4208 /* There wasn't enough room to store this conversion or single
4209 character. CONVBYTES says how much room is needed. Allocate
4210 enough room (and then some) and do it again. */
4212 ptrdiff_t used = p - buf;
4214 if (max_bufsize - used < convbytes)
4215 string_overflow ();
4216 bufsize = used + convbytes;
4217 bufsize = bufsize < max_bufsize / 2 ? bufsize * 2 : max_bufsize;
4219 if (buf == initial_buffer)
4221 buf = xmalloc (bufsize);
4222 sa_must_free = 1;
4223 buf_save_value = make_save_value (buf, 0);
4224 record_unwind_protect (safe_alloca_unwind, buf_save_value);
4225 memcpy (buf, initial_buffer, used);
4227 else
4228 XSAVE_VALUE (buf_save_value)->pointer = buf = xrealloc (buf, bufsize);
4230 p = buf + used;
4233 format = format0;
4234 n = n0;
4237 if (bufsize < p - buf)
4238 abort ();
4240 if (maybe_combine_byte)
4241 nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf);
4242 val = make_specified_string (buf, nchars, p - buf, multibyte);
4244 /* If we allocated BUF with malloc, free it too. */
4245 SAFE_FREE ();
4247 /* If the format string has text properties, or any of the string
4248 arguments has text properties, set up text properties of the
4249 result string. */
4251 if (STRING_INTERVALS (args[0]) || arg_intervals)
4253 Lisp_Object len, new_len, props;
4254 struct gcpro gcpro1;
4256 /* Add text properties from the format string. */
4257 len = make_number (SCHARS (args[0]));
4258 props = text_property_list (args[0], make_number (0), len, Qnil);
4259 GCPRO1 (props);
4261 if (CONSP (props))
4263 ptrdiff_t bytepos = 0, position = 0, translated = 0;
4264 ptrdiff_t argn = 1;
4265 Lisp_Object list;
4267 /* Adjust the bounds of each text property
4268 to the proper start and end in the output string. */
4270 /* Put the positions in PROPS in increasing order, so that
4271 we can do (effectively) one scan through the position
4272 space of the format string. */
4273 props = Fnreverse (props);
4275 /* BYTEPOS is the byte position in the format string,
4276 POSITION is the untranslated char position in it,
4277 TRANSLATED is the translated char position in BUF,
4278 and ARGN is the number of the next arg we will come to. */
4279 for (list = props; CONSP (list); list = XCDR (list))
4281 Lisp_Object item;
4282 ptrdiff_t pos;
4284 item = XCAR (list);
4286 /* First adjust the property start position. */
4287 pos = XINT (XCAR (item));
4289 /* Advance BYTEPOS, POSITION, TRANSLATED and ARGN
4290 up to this position. */
4291 for (; position < pos; bytepos++)
4293 if (! discarded[bytepos])
4294 position++, translated++;
4295 else if (discarded[bytepos] == 1)
4297 position++;
4298 if (translated == info[argn].start)
4300 translated += info[argn].end - info[argn].start;
4301 argn++;
4306 XSETCAR (item, make_number (translated));
4308 /* Likewise adjust the property end position. */
4309 pos = XINT (XCAR (XCDR (item)));
4311 for (; position < pos; bytepos++)
4313 if (! discarded[bytepos])
4314 position++, translated++;
4315 else if (discarded[bytepos] == 1)
4317 position++;
4318 if (translated == info[argn].start)
4320 translated += info[argn].end - info[argn].start;
4321 argn++;
4326 XSETCAR (XCDR (item), make_number (translated));
4329 add_text_properties_from_list (val, props, make_number (0));
4332 /* Add text properties from arguments. */
4333 if (arg_intervals)
4334 for (n = 1; n < nargs; ++n)
4335 if (info[n].intervals)
4337 len = make_number (SCHARS (args[n]));
4338 new_len = make_number (info[n].end - info[n].start);
4339 props = text_property_list (args[n], make_number (0), len, Qnil);
4340 props = extend_property_ranges (props, new_len);
4341 /* If successive arguments have properties, be sure that
4342 the value of `composition' property be the copy. */
4343 if (n > 1 && info[n - 1].end)
4344 make_composition_value_copy (props);
4345 add_text_properties_from_list (val, props,
4346 make_number (info[n].start));
4349 UNGCPRO;
4352 return val;
4355 Lisp_Object
4356 format2 (const char *string1, Lisp_Object arg0, Lisp_Object arg1)
4358 Lisp_Object args[3];
4359 args[0] = build_string (string1);
4360 args[1] = arg0;
4361 args[2] = arg1;
4362 return Fformat (3, args);
4365 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
4366 doc: /* Return t if two characters match, optionally ignoring case.
4367 Both arguments must be characters (i.e. integers).
4368 Case is ignored if `case-fold-search' is non-nil in the current buffer. */)
4369 (register Lisp_Object c1, Lisp_Object c2)
4371 int i1, i2;
4372 /* Check they're chars, not just integers, otherwise we could get array
4373 bounds violations in downcase. */
4374 CHECK_CHARACTER (c1);
4375 CHECK_CHARACTER (c2);
4377 if (XINT (c1) == XINT (c2))
4378 return Qt;
4379 if (NILP (BVAR (current_buffer, case_fold_search)))
4380 return Qnil;
4382 i1 = XFASTINT (c1);
4383 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4384 && ! ASCII_CHAR_P (i1))
4386 MAKE_CHAR_MULTIBYTE (i1);
4388 i2 = XFASTINT (c2);
4389 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
4390 && ! ASCII_CHAR_P (i2))
4392 MAKE_CHAR_MULTIBYTE (i2);
4394 return (downcase (i1) == downcase (i2) ? Qt : Qnil);
4397 /* Transpose the markers in two regions of the current buffer, and
4398 adjust the ones between them if necessary (i.e.: if the regions
4399 differ in size).
4401 START1, END1 are the character positions of the first region.
4402 START1_BYTE, END1_BYTE are the byte positions.
4403 START2, END2 are the character positions of the second region.
4404 START2_BYTE, END2_BYTE are the byte positions.
4406 Traverses the entire marker list of the buffer to do so, adding an
4407 appropriate amount to some, subtracting from some, and leaving the
4408 rest untouched. Most of this is copied from adjust_markers in insdel.c.
4410 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
4412 static void
4413 transpose_markers (ptrdiff_t start1, ptrdiff_t end1,
4414 ptrdiff_t start2, ptrdiff_t end2,
4415 ptrdiff_t start1_byte, ptrdiff_t end1_byte,
4416 ptrdiff_t start2_byte, ptrdiff_t end2_byte)
4418 register ptrdiff_t amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
4419 register struct Lisp_Marker *marker;
4421 /* Update point as if it were a marker. */
4422 if (PT < start1)
4424 else if (PT < end1)
4425 TEMP_SET_PT_BOTH (PT + (end2 - end1),
4426 PT_BYTE + (end2_byte - end1_byte));
4427 else if (PT < start2)
4428 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
4429 (PT_BYTE + (end2_byte - start2_byte)
4430 - (end1_byte - start1_byte)));
4431 else if (PT < end2)
4432 TEMP_SET_PT_BOTH (PT - (start2 - start1),
4433 PT_BYTE - (start2_byte - start1_byte));
4435 /* We used to adjust the endpoints here to account for the gap, but that
4436 isn't good enough. Even if we assume the caller has tried to move the
4437 gap out of our way, it might still be at start1 exactly, for example;
4438 and that places it `inside' the interval, for our purposes. The amount
4439 of adjustment is nontrivial if there's a `denormalized' marker whose
4440 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
4441 the dirty work to Fmarker_position, below. */
4443 /* The difference between the region's lengths */
4444 diff = (end2 - start2) - (end1 - start1);
4445 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
4447 /* For shifting each marker in a region by the length of the other
4448 region plus the distance between the regions. */
4449 amt1 = (end2 - start2) + (start2 - end1);
4450 amt2 = (end1 - start1) + (start2 - end1);
4451 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
4452 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
4454 for (marker = BUF_MARKERS (current_buffer); marker; marker = marker->next)
4456 mpos = marker->bytepos;
4457 if (mpos >= start1_byte && mpos < end2_byte)
4459 if (mpos < end1_byte)
4460 mpos += amt1_byte;
4461 else if (mpos < start2_byte)
4462 mpos += diff_byte;
4463 else
4464 mpos -= amt2_byte;
4465 marker->bytepos = mpos;
4467 mpos = marker->charpos;
4468 if (mpos >= start1 && mpos < end2)
4470 if (mpos < end1)
4471 mpos += amt1;
4472 else if (mpos < start2)
4473 mpos += diff;
4474 else
4475 mpos -= amt2;
4477 marker->charpos = mpos;
4481 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
4482 doc: /* Transpose region STARTR1 to ENDR1 with STARTR2 to ENDR2.
4483 The regions should not be overlapping, because the size of the buffer is
4484 never changed in a transposition.
4486 Optional fifth arg LEAVE-MARKERS, if non-nil, means don't update
4487 any markers that happen to be located in the regions.
4489 Transposing beyond buffer boundaries is an error. */)
4490 (Lisp_Object startr1, Lisp_Object endr1, Lisp_Object startr2, Lisp_Object endr2, Lisp_Object leave_markers)
4492 register ptrdiff_t start1, end1, start2, end2;
4493 ptrdiff_t start1_byte, start2_byte, len1_byte, len2_byte;
4494 ptrdiff_t gap, len1, len_mid, len2;
4495 unsigned char *start1_addr, *start2_addr, *temp;
4497 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2, tmp_interval3;
4498 Lisp_Object buf;
4500 XSETBUFFER (buf, current_buffer);
4501 cur_intv = BUF_INTERVALS (current_buffer);
4503 validate_region (&startr1, &endr1);
4504 validate_region (&startr2, &endr2);
4506 start1 = XFASTINT (startr1);
4507 end1 = XFASTINT (endr1);
4508 start2 = XFASTINT (startr2);
4509 end2 = XFASTINT (endr2);
4510 gap = GPT;
4512 /* Swap the regions if they're reversed. */
4513 if (start2 < end1)
4515 register ptrdiff_t glumph = start1;
4516 start1 = start2;
4517 start2 = glumph;
4518 glumph = end1;
4519 end1 = end2;
4520 end2 = glumph;
4523 len1 = end1 - start1;
4524 len2 = end2 - start2;
4526 if (start2 < end1)
4527 error ("Transposed regions overlap");
4528 /* Nothing to change for adjacent regions with one being empty */
4529 else if ((start1 == end1 || start2 == end2) && end1 == start2)
4530 return Qnil;
4532 /* The possibilities are:
4533 1. Adjacent (contiguous) regions, or separate but equal regions
4534 (no, really equal, in this case!), or
4535 2. Separate regions of unequal size.
4537 The worst case is usually No. 2. It means that (aside from
4538 potential need for getting the gap out of the way), there also
4539 needs to be a shifting of the text between the two regions. So
4540 if they are spread far apart, we are that much slower... sigh. */
4542 /* It must be pointed out that the really studly thing to do would
4543 be not to move the gap at all, but to leave it in place and work
4544 around it if necessary. This would be extremely efficient,
4545 especially considering that people are likely to do
4546 transpositions near where they are working interactively, which
4547 is exactly where the gap would be found. However, such code
4548 would be much harder to write and to read. So, if you are
4549 reading this comment and are feeling squirrely, by all means have
4550 a go! I just didn't feel like doing it, so I will simply move
4551 the gap the minimum distance to get it out of the way, and then
4552 deal with an unbroken array. */
4554 /* Make sure the gap won't interfere, by moving it out of the text
4555 we will operate on. */
4556 if (start1 < gap && gap < end2)
4558 if (gap - start1 < end2 - gap)
4559 move_gap (start1);
4560 else
4561 move_gap (end2);
4564 start1_byte = CHAR_TO_BYTE (start1);
4565 start2_byte = CHAR_TO_BYTE (start2);
4566 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
4567 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
4569 #ifdef BYTE_COMBINING_DEBUG
4570 if (end1 == start2)
4572 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4573 len2_byte, start1, start1_byte)
4574 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4575 len1_byte, end2, start2_byte + len2_byte)
4576 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4577 len1_byte, end2, start2_byte + len2_byte))
4578 abort ();
4580 else
4582 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4583 len2_byte, start1, start1_byte)
4584 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4585 len1_byte, start2, start2_byte)
4586 || count_combining_after (BYTE_POS_ADDR (start2_byte),
4587 len2_byte, end1, start1_byte + len1_byte)
4588 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4589 len1_byte, end2, start2_byte + len2_byte))
4590 abort ();
4592 #endif
4594 /* Hmmm... how about checking to see if the gap is large
4595 enough to use as the temporary storage? That would avoid an
4596 allocation... interesting. Later, don't fool with it now. */
4598 /* Working without memmove, for portability (sigh), so must be
4599 careful of overlapping subsections of the array... */
4601 if (end1 == start2) /* adjacent regions */
4603 modify_region (current_buffer, start1, end2, 0);
4604 record_change (start1, len1 + len2);
4606 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4607 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4608 /* Don't use Fset_text_properties: that can cause GC, which can
4609 clobber objects stored in the tmp_intervals. */
4610 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4611 if (!NULL_INTERVAL_P (tmp_interval3))
4612 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4614 /* First region smaller than second. */
4615 if (len1_byte < len2_byte)
4617 USE_SAFE_ALLOCA;
4619 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4621 /* Don't precompute these addresses. We have to compute them
4622 at the last minute, because the relocating allocator might
4623 have moved the buffer around during the xmalloc. */
4624 start1_addr = BYTE_POS_ADDR (start1_byte);
4625 start2_addr = BYTE_POS_ADDR (start2_byte);
4627 memcpy (temp, start2_addr, len2_byte);
4628 memcpy (start1_addr + len2_byte, start1_addr, len1_byte);
4629 memcpy (start1_addr, temp, len2_byte);
4630 SAFE_FREE ();
4632 else
4633 /* First region not smaller than second. */
4635 USE_SAFE_ALLOCA;
4637 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4638 start1_addr = BYTE_POS_ADDR (start1_byte);
4639 start2_addr = BYTE_POS_ADDR (start2_byte);
4640 memcpy (temp, start1_addr, len1_byte);
4641 memcpy (start1_addr, start2_addr, len2_byte);
4642 memcpy (start1_addr + len2_byte, temp, len1_byte);
4643 SAFE_FREE ();
4645 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
4646 len1, current_buffer, 0);
4647 graft_intervals_into_buffer (tmp_interval2, start1,
4648 len2, current_buffer, 0);
4649 update_compositions (start1, start1 + len2, CHECK_BORDER);
4650 update_compositions (start1 + len2, end2, CHECK_TAIL);
4652 /* Non-adjacent regions, because end1 != start2, bleagh... */
4653 else
4655 len_mid = start2_byte - (start1_byte + len1_byte);
4657 if (len1_byte == len2_byte)
4658 /* Regions are same size, though, how nice. */
4660 USE_SAFE_ALLOCA;
4662 modify_region (current_buffer, start1, end1, 0);
4663 modify_region (current_buffer, start2, end2, 0);
4664 record_change (start1, len1);
4665 record_change (start2, len2);
4666 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4667 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4669 tmp_interval3 = validate_interval_range (buf, &startr1, &endr1, 0);
4670 if (!NULL_INTERVAL_P (tmp_interval3))
4671 set_text_properties_1 (startr1, endr1, Qnil, buf, tmp_interval3);
4673 tmp_interval3 = validate_interval_range (buf, &startr2, &endr2, 0);
4674 if (!NULL_INTERVAL_P (tmp_interval3))
4675 set_text_properties_1 (startr2, endr2, Qnil, buf, tmp_interval3);
4677 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4678 start1_addr = BYTE_POS_ADDR (start1_byte);
4679 start2_addr = BYTE_POS_ADDR (start2_byte);
4680 memcpy (temp, start1_addr, len1_byte);
4681 memcpy (start1_addr, start2_addr, len2_byte);
4682 memcpy (start2_addr, temp, len1_byte);
4683 SAFE_FREE ();
4685 graft_intervals_into_buffer (tmp_interval1, start2,
4686 len1, current_buffer, 0);
4687 graft_intervals_into_buffer (tmp_interval2, start1,
4688 len2, current_buffer, 0);
4691 else if (len1_byte < len2_byte) /* Second region larger than first */
4692 /* Non-adjacent & unequal size, area between must also be shifted. */
4694 USE_SAFE_ALLOCA;
4696 modify_region (current_buffer, start1, end2, 0);
4697 record_change (start1, (end2 - start1));
4698 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4699 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4700 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4702 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4703 if (!NULL_INTERVAL_P (tmp_interval3))
4704 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4706 /* holds region 2 */
4707 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4708 start1_addr = BYTE_POS_ADDR (start1_byte);
4709 start2_addr = BYTE_POS_ADDR (start2_byte);
4710 memcpy (temp, start2_addr, len2_byte);
4711 memcpy (start1_addr + len_mid + len2_byte, start1_addr, len1_byte);
4712 memmove (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4713 memcpy (start1_addr, temp, len2_byte);
4714 SAFE_FREE ();
4716 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4717 len1, current_buffer, 0);
4718 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4719 len_mid, current_buffer, 0);
4720 graft_intervals_into_buffer (tmp_interval2, start1,
4721 len2, current_buffer, 0);
4723 else
4724 /* Second region smaller than first. */
4726 USE_SAFE_ALLOCA;
4728 record_change (start1, (end2 - start1));
4729 modify_region (current_buffer, start1, end2, 0);
4731 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4732 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4733 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4735 tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0);
4736 if (!NULL_INTERVAL_P (tmp_interval3))
4737 set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3);
4739 /* holds region 1 */
4740 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4741 start1_addr = BYTE_POS_ADDR (start1_byte);
4742 start2_addr = BYTE_POS_ADDR (start2_byte);
4743 memcpy (temp, start1_addr, len1_byte);
4744 memcpy (start1_addr, start2_addr, len2_byte);
4745 memcpy (start1_addr + len2_byte, start1_addr + len1_byte, len_mid);
4746 memcpy (start1_addr + len2_byte + len_mid, temp, len1_byte);
4747 SAFE_FREE ();
4749 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4750 len1, current_buffer, 0);
4751 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4752 len_mid, current_buffer, 0);
4753 graft_intervals_into_buffer (tmp_interval2, start1,
4754 len2, current_buffer, 0);
4757 update_compositions (start1, start1 + len2, CHECK_BORDER);
4758 update_compositions (end2 - len1, end2, CHECK_BORDER);
4761 /* When doing multiple transpositions, it might be nice
4762 to optimize this. Perhaps the markers in any one buffer
4763 should be organized in some sorted data tree. */
4764 if (NILP (leave_markers))
4766 transpose_markers (start1, end1, start2, end2,
4767 start1_byte, start1_byte + len1_byte,
4768 start2_byte, start2_byte + len2_byte);
4769 fix_start_end_in_overlays (start1, end2);
4772 signal_after_change (start1, end2 - start1, end2 - start1);
4773 return Qnil;
4777 void
4778 syms_of_editfns (void)
4780 environbuf = 0;
4781 initial_tz = 0;
4783 DEFSYM (Qbuffer_access_fontify_functions, "buffer-access-fontify-functions");
4785 DEFVAR_LISP ("inhibit-field-text-motion", Vinhibit_field_text_motion,
4786 doc: /* Non-nil means text motion commands don't notice fields. */);
4787 Vinhibit_field_text_motion = Qnil;
4789 DEFVAR_LISP ("buffer-access-fontify-functions",
4790 Vbuffer_access_fontify_functions,
4791 doc: /* List of functions called by `buffer-substring' to fontify if necessary.
4792 Each function is called with two arguments which specify the range
4793 of the buffer being accessed. */);
4794 Vbuffer_access_fontify_functions = Qnil;
4797 Lisp_Object obuf;
4798 obuf = Fcurrent_buffer ();
4799 /* Do this here, because init_buffer_once is too early--it won't work. */
4800 Fset_buffer (Vprin1_to_string_buffer);
4801 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
4802 Fset (Fmake_local_variable (intern_c_string ("buffer-access-fontify-functions")),
4803 Qnil);
4804 Fset_buffer (obuf);
4807 DEFVAR_LISP ("buffer-access-fontified-property",
4808 Vbuffer_access_fontified_property,
4809 doc: /* Property which (if non-nil) indicates text has been fontified.
4810 `buffer-substring' need not call the `buffer-access-fontify-functions'
4811 functions if all the text being accessed has this property. */);
4812 Vbuffer_access_fontified_property = Qnil;
4814 DEFVAR_LISP ("system-name", Vsystem_name,
4815 doc: /* The host name of the machine Emacs is running on. */);
4817 DEFVAR_LISP ("user-full-name", Vuser_full_name,
4818 doc: /* The full name of the user logged in. */);
4820 DEFVAR_LISP ("user-login-name", Vuser_login_name,
4821 doc: /* The user's name, taken from environment variables if possible. */);
4823 DEFVAR_LISP ("user-real-login-name", Vuser_real_login_name,
4824 doc: /* The user's name, based upon the real uid only. */);
4826 DEFVAR_LISP ("operating-system-release", Voperating_system_release,
4827 doc: /* The release of the operating system Emacs is running on. */);
4829 defsubr (&Spropertize);
4830 defsubr (&Schar_equal);
4831 defsubr (&Sgoto_char);
4832 defsubr (&Sstring_to_char);
4833 defsubr (&Schar_to_string);
4834 defsubr (&Sbyte_to_string);
4835 defsubr (&Sbuffer_substring);
4836 defsubr (&Sbuffer_substring_no_properties);
4837 defsubr (&Sbuffer_string);
4839 defsubr (&Spoint_marker);
4840 defsubr (&Smark_marker);
4841 defsubr (&Spoint);
4842 defsubr (&Sregion_beginning);
4843 defsubr (&Sregion_end);
4845 DEFSYM (Qfield, "field");
4846 DEFSYM (Qboundary, "boundary");
4847 defsubr (&Sfield_beginning);
4848 defsubr (&Sfield_end);
4849 defsubr (&Sfield_string);
4850 defsubr (&Sfield_string_no_properties);
4851 defsubr (&Sdelete_field);
4852 defsubr (&Sconstrain_to_field);
4854 defsubr (&Sline_beginning_position);
4855 defsubr (&Sline_end_position);
4857 /* defsubr (&Smark); */
4858 /* defsubr (&Sset_mark); */
4859 defsubr (&Ssave_excursion);
4860 defsubr (&Ssave_current_buffer);
4862 defsubr (&Sbufsize);
4863 defsubr (&Spoint_max);
4864 defsubr (&Spoint_min);
4865 defsubr (&Spoint_min_marker);
4866 defsubr (&Spoint_max_marker);
4867 defsubr (&Sgap_position);
4868 defsubr (&Sgap_size);
4869 defsubr (&Sposition_bytes);
4870 defsubr (&Sbyte_to_position);
4872 defsubr (&Sbobp);
4873 defsubr (&Seobp);
4874 defsubr (&Sbolp);
4875 defsubr (&Seolp);
4876 defsubr (&Sfollowing_char);
4877 defsubr (&Sprevious_char);
4878 defsubr (&Schar_after);
4879 defsubr (&Schar_before);
4880 defsubr (&Sinsert);
4881 defsubr (&Sinsert_before_markers);
4882 defsubr (&Sinsert_and_inherit);
4883 defsubr (&Sinsert_and_inherit_before_markers);
4884 defsubr (&Sinsert_char);
4885 defsubr (&Sinsert_byte);
4887 defsubr (&Suser_login_name);
4888 defsubr (&Suser_real_login_name);
4889 defsubr (&Suser_uid);
4890 defsubr (&Suser_real_uid);
4891 defsubr (&Suser_full_name);
4892 defsubr (&Semacs_pid);
4893 defsubr (&Scurrent_time);
4894 defsubr (&Sget_internal_run_time);
4895 defsubr (&Sformat_time_string);
4896 defsubr (&Sfloat_time);
4897 defsubr (&Sdecode_time);
4898 defsubr (&Sencode_time);
4899 defsubr (&Scurrent_time_string);
4900 defsubr (&Scurrent_time_zone);
4901 defsubr (&Sset_time_zone_rule);
4902 defsubr (&Ssystem_name);
4903 defsubr (&Smessage);
4904 defsubr (&Smessage_box);
4905 defsubr (&Smessage_or_box);
4906 defsubr (&Scurrent_message);
4907 defsubr (&Sformat);
4909 defsubr (&Sinsert_buffer_substring);
4910 defsubr (&Scompare_buffer_substrings);
4911 defsubr (&Ssubst_char_in_region);
4912 defsubr (&Stranslate_region_internal);
4913 defsubr (&Sdelete_region);
4914 defsubr (&Sdelete_and_extract_region);
4915 defsubr (&Swiden);
4916 defsubr (&Snarrow_to_region);
4917 defsubr (&Ssave_restriction);
4918 defsubr (&Stranspose_regions);