(easy-mmode-define-global-mode, define-minor-mode): Call
[emacs.git] / src / editfns.c
blobf77b9cafc80574a6318ed18dd0f4175bbfae3fb4
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985, 1986, 1987, 1989, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <sys/types.h>
25 #include <stdio.h>
27 #ifdef VMS
28 #include "vms-pwd.h"
29 #else
30 #include <pwd.h>
31 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #ifdef HAVE_SYS_UTSNAME_H
38 #include <sys/utsname.h>
39 #endif
41 /* systime.h includes <sys/time.h> which, on some systems, is required
42 for <sys/resource.h>; thus systime.h must be included before
43 <sys/resource.h> */
44 #include "systime.h"
46 #if defined HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
48 #endif
50 #include <ctype.h>
52 #include "lisp.h"
53 #include "intervals.h"
54 #include "buffer.h"
55 #include "charset.h"
56 #include "coding.h"
57 #include "frame.h"
58 #include "window.h"
60 #ifdef STDC_HEADERS
61 #include <float.h>
62 #define MAX_10_EXP DBL_MAX_10_EXP
63 #else
64 #define MAX_10_EXP 310
65 #endif
67 #ifndef NULL
68 #define NULL 0
69 #endif
71 #ifndef USE_CRT_DLL
72 extern char **environ;
73 #endif
75 extern Lisp_Object make_time P_ ((time_t));
76 extern size_t emacs_strftimeu P_ ((char *, size_t, const char *,
77 const struct tm *, int));
78 static int tm_diff P_ ((struct tm *, struct tm *));
79 static void find_field P_ ((Lisp_Object, Lisp_Object, Lisp_Object, int *, Lisp_Object, int *));
80 static void update_buffer_properties P_ ((int, int));
81 static Lisp_Object region_limit P_ ((int));
82 int lisp_time_argument P_ ((Lisp_Object, time_t *, int *));
83 static size_t emacs_memftimeu P_ ((char *, size_t, const char *,
84 size_t, const struct tm *, int));
85 static void general_insert_function P_ ((void (*) (const unsigned char *, int),
86 void (*) (Lisp_Object, int, int, int,
87 int, int),
88 int, int, Lisp_Object *));
89 static Lisp_Object subst_char_in_region_unwind P_ ((Lisp_Object));
90 static Lisp_Object subst_char_in_region_unwind_1 P_ ((Lisp_Object));
91 static void transpose_markers P_ ((int, int, int, int, int, int, int, int));
93 #ifdef HAVE_INDEX
94 extern char *index P_ ((const char *, int));
95 #endif
97 Lisp_Object Vbuffer_access_fontify_functions;
98 Lisp_Object Qbuffer_access_fontify_functions;
99 Lisp_Object Vbuffer_access_fontified_property;
101 Lisp_Object Fuser_full_name P_ ((Lisp_Object));
103 /* Non-nil means don't stop at field boundary in text motion commands. */
105 Lisp_Object Vinhibit_field_text_motion;
107 /* Some static data, and a function to initialize it for each run */
109 Lisp_Object Vsystem_name;
110 Lisp_Object Vuser_real_login_name; /* login name of current user ID */
111 Lisp_Object Vuser_full_name; /* full name of current user */
112 Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
113 Lisp_Object Voperating_system_release; /* Operating System Release */
115 /* Symbol for the text property used to mark fields. */
117 Lisp_Object Qfield;
119 /* A special value for Qfield properties. */
121 Lisp_Object Qboundary;
124 void
125 init_editfns ()
127 char *user_name;
128 register unsigned char *p;
129 struct passwd *pw; /* password entry for the current user */
130 Lisp_Object tem;
132 /* Set up system_name even when dumping. */
133 init_system_name ();
135 #ifndef CANNOT_DUMP
136 /* Don't bother with this on initial start when just dumping out */
137 if (!initialized)
138 return;
139 #endif /* not CANNOT_DUMP */
141 pw = (struct passwd *) getpwuid (getuid ());
142 #ifdef MSDOS
143 /* We let the real user name default to "root" because that's quite
144 accurate on MSDOG and because it lets Emacs find the init file.
145 (The DVX libraries override the Djgpp libraries here.) */
146 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
147 #else
148 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
149 #endif
151 /* Get the effective user name, by consulting environment variables,
152 or the effective uid if those are unset. */
153 user_name = (char *) getenv ("LOGNAME");
154 if (!user_name)
155 #ifdef WINDOWSNT
156 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
157 #else /* WINDOWSNT */
158 user_name = (char *) getenv ("USER");
159 #endif /* WINDOWSNT */
160 if (!user_name)
162 pw = (struct passwd *) getpwuid (geteuid ());
163 user_name = (char *) (pw ? pw->pw_name : "unknown");
165 Vuser_login_name = build_string (user_name);
167 /* If the user name claimed in the environment vars differs from
168 the real uid, use the claimed name to find the full name. */
169 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
170 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
171 : Vuser_login_name);
173 p = (unsigned char *) getenv ("NAME");
174 if (p)
175 Vuser_full_name = build_string (p);
176 else if (NILP (Vuser_full_name))
177 Vuser_full_name = build_string ("unknown");
179 #ifdef HAVE_SYS_UTSNAME_H
181 struct utsname uts;
182 uname (&uts);
183 Voperating_system_release = build_string (uts.release);
185 #else
186 Voperating_system_release = Qnil;
187 #endif
190 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
191 doc: /* Convert arg CHAR to a string containing that character.
192 usage: (char-to-string CHAR) */)
193 (character)
194 Lisp_Object character;
196 int len;
197 unsigned char str[MAX_MULTIBYTE_LENGTH];
199 CHECK_NUMBER (character);
201 len = (SINGLE_BYTE_CHAR_P (XFASTINT (character))
202 ? (*str = (unsigned char)(XFASTINT (character)), 1)
203 : char_to_string (XFASTINT (character), str));
204 return make_string_from_bytes (str, 1, len);
207 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
208 doc: /* Convert arg STRING to a character, the first character of that string.
209 A multibyte character is handled correctly. */)
210 (string)
211 register Lisp_Object string;
213 register Lisp_Object val;
214 CHECK_STRING (string);
215 if (SCHARS (string))
217 if (STRING_MULTIBYTE (string))
218 XSETFASTINT (val, STRING_CHAR (SDATA (string), SBYTES (string)));
219 else
220 XSETFASTINT (val, SREF (string, 0));
222 else
223 XSETFASTINT (val, 0);
224 return val;
227 static Lisp_Object
228 buildmark (charpos, bytepos)
229 int charpos, bytepos;
231 register Lisp_Object mark;
232 mark = Fmake_marker ();
233 set_marker_both (mark, Qnil, charpos, bytepos);
234 return mark;
237 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
238 doc: /* Return value of point, as an integer.
239 Beginning of buffer is position (point-min). */)
242 Lisp_Object temp;
243 XSETFASTINT (temp, PT);
244 return temp;
247 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
248 doc: /* Return value of point, as a marker object. */)
251 return buildmark (PT, PT_BYTE);
255 clip_to_bounds (lower, num, upper)
256 int lower, num, upper;
258 if (num < lower)
259 return lower;
260 else if (num > upper)
261 return upper;
262 else
263 return num;
266 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
267 doc: /* Set point to POSITION, a number or marker.
268 Beginning of buffer is position (point-min), end is (point-max). */)
269 (position)
270 register Lisp_Object position;
272 int pos;
274 if (MARKERP (position)
275 && current_buffer == XMARKER (position)->buffer)
277 pos = marker_position (position);
278 if (pos < BEGV)
279 SET_PT_BOTH (BEGV, BEGV_BYTE);
280 else if (pos > ZV)
281 SET_PT_BOTH (ZV, ZV_BYTE);
282 else
283 SET_PT_BOTH (pos, marker_byte_position (position));
285 return position;
288 CHECK_NUMBER_COERCE_MARKER (position);
290 pos = clip_to_bounds (BEGV, XINT (position), ZV);
291 SET_PT (pos);
292 return position;
296 /* Return the start or end position of the region.
297 BEGINNINGP non-zero means return the start.
298 If there is no region active, signal an error. */
300 static Lisp_Object
301 region_limit (beginningp)
302 int beginningp;
304 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
305 Lisp_Object m;
307 if (!NILP (Vtransient_mark_mode)
308 && NILP (Vmark_even_if_inactive)
309 && NILP (current_buffer->mark_active))
310 Fsignal (Qmark_inactive, Qnil);
312 m = Fmarker_position (current_buffer->mark);
313 if (NILP (m))
314 error ("The mark is not set now, so there is no region");
316 if ((PT < XFASTINT (m)) == (beginningp != 0))
317 m = make_number (PT);
318 return m;
321 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
322 doc: /* Return position of beginning of region, as an integer. */)
325 return region_limit (1);
328 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
329 doc: /* Return position of end of region, as an integer. */)
332 return region_limit (0);
335 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
336 doc: /* Return this buffer's mark, as a marker object.
337 Watch out! Moving this marker changes the mark position.
338 If you set the marker not to point anywhere, the buffer will have no mark. */)
341 return current_buffer->mark;
345 /* Find all the overlays in the current buffer that touch position POS.
346 Return the number found, and store them in a vector in VEC
347 of length LEN. */
349 static int
350 overlays_around (pos, vec, len)
351 int pos;
352 Lisp_Object *vec;
353 int len;
355 Lisp_Object overlay, start, end;
356 struct Lisp_Overlay *tail;
357 int startpos, endpos;
358 int idx = 0;
360 for (tail = current_buffer->overlays_before; tail; tail = tail->next)
362 XSETMISC (overlay, tail);
364 end = OVERLAY_END (overlay);
365 endpos = OVERLAY_POSITION (end);
366 if (endpos < pos)
367 break;
368 start = OVERLAY_START (overlay);
369 startpos = OVERLAY_POSITION (start);
370 if (startpos <= pos)
372 if (idx < len)
373 vec[idx] = overlay;
374 /* Keep counting overlays even if we can't return them all. */
375 idx++;
379 for (tail = current_buffer->overlays_after; tail; tail = tail->next)
381 XSETMISC (overlay, tail);
383 start = OVERLAY_START (overlay);
384 startpos = OVERLAY_POSITION (start);
385 if (pos < startpos)
386 break;
387 end = OVERLAY_END (overlay);
388 endpos = OVERLAY_POSITION (end);
389 if (pos <= endpos)
391 if (idx < len)
392 vec[idx] = overlay;
393 idx++;
397 return idx;
400 /* Return the value of property PROP, in OBJECT at POSITION.
401 It's the value of PROP that a char inserted at POSITION would get.
402 OBJECT is optional and defaults to the current buffer.
403 If OBJECT is a buffer, then overlay properties are considered as well as
404 text properties.
405 If OBJECT is a window, then that window's buffer is used, but
406 window-specific overlays are considered only if they are associated
407 with OBJECT. */
408 Lisp_Object
409 get_pos_property (position, prop, object)
410 Lisp_Object position, object;
411 register Lisp_Object prop;
413 CHECK_NUMBER_COERCE_MARKER (position);
415 if (NILP (object))
416 XSETBUFFER (object, current_buffer);
417 else if (WINDOWP (object))
418 object = XWINDOW (object)->buffer;
420 if (!BUFFERP (object))
421 /* pos-property only makes sense in buffers right now, since strings
422 have no overlays and no notion of insertion for which stickiness
423 could be obeyed. */
424 return Fget_text_property (position, prop, object);
425 else
427 int posn = XINT (position);
428 int noverlays;
429 Lisp_Object *overlay_vec, tem;
430 struct buffer *obuf = current_buffer;
432 set_buffer_temp (XBUFFER (object));
434 /* First try with room for 40 overlays. */
435 noverlays = 40;
436 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
437 noverlays = overlays_around (posn, overlay_vec, noverlays);
439 /* If there are more than 40,
440 make enough space for all, and try again. */
441 if (noverlays > 40)
443 overlay_vec = (Lisp_Object *) alloca (noverlays * sizeof (Lisp_Object));
444 noverlays = overlays_around (posn, overlay_vec, noverlays);
446 noverlays = sort_overlays (overlay_vec, noverlays, NULL);
448 set_buffer_temp (obuf);
450 /* Now check the overlays in order of decreasing priority. */
451 while (--noverlays >= 0)
453 Lisp_Object ol = overlay_vec[noverlays];
454 tem = Foverlay_get (ol, prop);
455 if (!NILP (tem))
457 /* Check the overlay is indeed active at point. */
458 Lisp_Object start = OVERLAY_START (ol), finish = OVERLAY_END (ol);
459 if ((OVERLAY_POSITION (start) == posn
460 && XMARKER (start)->insertion_type == 1)
461 || (OVERLAY_POSITION (finish) == posn
462 && XMARKER (finish)->insertion_type == 0))
463 ; /* The overlay will not cover a char inserted at point. */
464 else
466 return tem;
471 { /* Now check the text-properties. */
472 int stickiness = text_property_stickiness (prop, position, object);
473 if (stickiness > 0)
474 return Fget_text_property (position, prop, object);
475 else if (stickiness < 0
476 && XINT (position) > BUF_BEGV (XBUFFER (object)))
477 return Fget_text_property (make_number (XINT (position) - 1),
478 prop, object);
479 else
480 return Qnil;
485 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
486 the value of point is used instead. If BEG or END null,
487 means don't store the beginning or end of the field.
489 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
490 results; they do not effect boundary behavior.
492 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
493 position of a field, then the beginning of the previous field is
494 returned instead of the beginning of POS's field (since the end of a
495 field is actually also the beginning of the next input field, this
496 behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
497 true case, if two fields are separated by a field with the special
498 value `boundary', and POS lies within it, then the two separated
499 fields are considered to be adjacent, and POS between them, when
500 finding the beginning and ending of the "merged" field.
502 Either BEG or END may be 0, in which case the corresponding value
503 is not stored. */
505 static void
506 find_field (pos, merge_at_boundary, beg_limit, beg, end_limit, end)
507 Lisp_Object pos;
508 Lisp_Object merge_at_boundary;
509 Lisp_Object beg_limit, end_limit;
510 int *beg, *end;
512 /* Fields right before and after the point. */
513 Lisp_Object before_field, after_field;
514 /* 1 if POS counts as the start of a field. */
515 int at_field_start = 0;
516 /* 1 if POS counts as the end of a field. */
517 int at_field_end = 0;
519 if (NILP (pos))
520 XSETFASTINT (pos, PT);
521 else
522 CHECK_NUMBER_COERCE_MARKER (pos);
524 after_field
525 = get_char_property_and_overlay (pos, Qfield, Qnil, NULL);
526 before_field
527 = (XFASTINT (pos) > BEGV
528 ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
529 Qfield, Qnil, NULL)
530 : Qnil);
532 /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil
533 and POS is at beginning of a field, which can also be interpreted
534 as the end of the previous field. Note that the case where if
535 MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
536 more natural one; then we avoid treating the beginning of a field
537 specially. */
538 if (NILP (merge_at_boundary))
540 Lisp_Object field = get_pos_property (pos, Qfield, Qnil);
541 if (!EQ (field, after_field))
542 at_field_end = 1;
543 if (!EQ (field, before_field))
544 at_field_start = 1;
545 if (NILP (field) && at_field_start && at_field_end)
546 /* If an inserted char would have a nil field while the surrounding
547 text is non-nil, we're probably not looking at a
548 zero-length field, but instead at a non-nil field that's
549 not intended for editing (such as comint's prompts). */
550 at_field_end = at_field_start = 0;
553 /* Note about special `boundary' fields:
555 Consider the case where the point (`.') is between the fields `x' and `y':
557 xxxx.yyyy
559 In this situation, if merge_at_boundary is true, we consider the
560 `x' and `y' fields as forming one big merged field, and so the end
561 of the field is the end of `y'.
563 However, if `x' and `y' are separated by a special `boundary' field
564 (a field with a `field' char-property of 'boundary), then we ignore
565 this special field when merging adjacent fields. Here's the same
566 situation, but with a `boundary' field between the `x' and `y' fields:
568 xxx.BBBByyyy
570 Here, if point is at the end of `x', the beginning of `y', or
571 anywhere in-between (within the `boundary' field), we merge all
572 three fields and consider the beginning as being the beginning of
573 the `x' field, and the end as being the end of the `y' field. */
575 if (beg)
577 if (at_field_start)
578 /* POS is at the edge of a field, and we should consider it as
579 the beginning of the following field. */
580 *beg = XFASTINT (pos);
581 else
582 /* Find the previous field boundary. */
584 Lisp_Object p = pos;
585 if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary))
586 /* Skip a `boundary' field. */
587 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
588 beg_limit);
590 p = Fprevious_single_char_property_change (p, Qfield, Qnil,
591 beg_limit);
592 *beg = NILP (p) ? BEGV : XFASTINT (p);
596 if (end)
598 if (at_field_end)
599 /* POS is at the edge of a field, and we should consider it as
600 the end of the previous field. */
601 *end = XFASTINT (pos);
602 else
603 /* Find the next field boundary. */
605 if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary))
606 /* Skip a `boundary' field. */
607 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
608 end_limit);
610 pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
611 end_limit);
612 *end = NILP (pos) ? ZV : XFASTINT (pos);
618 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
619 doc: /* Delete the field surrounding POS.
620 A field is a region of text with the same `field' property.
621 If POS is nil, the value of point is used for POS. */)
622 (pos)
623 Lisp_Object pos;
625 int beg, end;
626 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
627 if (beg != end)
628 del_range (beg, end);
629 return Qnil;
632 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
633 doc: /* Return the contents of the field surrounding POS as a string.
634 A field is a region of text with the same `field' property.
635 If POS is nil, the value of point is used for POS. */)
636 (pos)
637 Lisp_Object pos;
639 int beg, end;
640 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
641 return make_buffer_string (beg, end, 1);
644 DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
645 doc: /* Return the contents of the field around POS, without text-properties.
646 A field is a region of text with the same `field' property.
647 If POS is nil, the value of point is used for POS. */)
648 (pos)
649 Lisp_Object pos;
651 int beg, end;
652 find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
653 return make_buffer_string (beg, end, 0);
656 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
657 doc: /* Return the beginning of the field surrounding POS.
658 A field is a region of text with the same `field' property.
659 If POS is nil, the value of point is used for POS.
660 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
661 field, then the beginning of the *previous* field is returned.
662 If LIMIT is non-nil, it is a buffer position; if the beginning of the field
663 is before LIMIT, then LIMIT will be returned instead. */)
664 (pos, escape_from_edge, limit)
665 Lisp_Object pos, escape_from_edge, limit;
667 int beg;
668 find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
669 return make_number (beg);
672 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
673 doc: /* Return the end of the field surrounding POS.
674 A field is a region of text with the same `field' property.
675 If POS is nil, the value of point is used for POS.
676 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
677 then the end of the *following* field is returned.
678 If LIMIT is non-nil, it is a buffer position; if the end of the field
679 is after LIMIT, then LIMIT will be returned instead. */)
680 (pos, escape_from_edge, limit)
681 Lisp_Object pos, escape_from_edge, limit;
683 int end;
684 find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
685 return make_number (end);
688 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
689 doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
691 A field is a region of text with the same `field' property.
692 If NEW-POS is nil, then the current point is used instead, and set to the
693 constrained position if that is different.
695 If OLD-POS is at the boundary of two fields, then the allowable
696 positions for NEW-POS depends on the value of the optional argument
697 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
698 constrained to the field that has the same `field' char-property
699 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
700 is non-nil, NEW-POS is constrained to the union of the two adjacent
701 fields. Additionally, if two fields are separated by another field with
702 the special value `boundary', then any point within this special field is
703 also considered to be `on the boundary'.
705 If the optional argument ONLY-IN-LINE is non-nil and constraining
706 NEW-POS would move it to a different line, NEW-POS is returned
707 unconstrained. This useful for commands that move by line, like
708 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
709 only in the case where they can still move to the right line.
711 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
712 a non-nil property of that name, then any field boundaries are ignored.
714 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */)
715 (new_pos, old_pos, escape_from_edge, only_in_line, inhibit_capture_property)
716 Lisp_Object new_pos, old_pos;
717 Lisp_Object escape_from_edge, only_in_line, inhibit_capture_property;
719 /* If non-zero, then the original point, before re-positioning. */
720 int orig_point = 0;
722 if (NILP (new_pos))
723 /* Use the current point, and afterwards, set it. */
725 orig_point = PT;
726 XSETFASTINT (new_pos, PT);
729 if (NILP (Vinhibit_field_text_motion)
730 && !EQ (new_pos, old_pos)
731 && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
732 || !NILP (Fget_char_property (old_pos, Qfield, Qnil)))
733 && (NILP (inhibit_capture_property)
734 || NILP (Fget_char_property(old_pos, inhibit_capture_property, Qnil))))
735 /* NEW_POS is not within the same field as OLD_POS; try to
736 move NEW_POS so that it is. */
738 int fwd, shortage;
739 Lisp_Object field_bound;
741 CHECK_NUMBER_COERCE_MARKER (new_pos);
742 CHECK_NUMBER_COERCE_MARKER (old_pos);
744 fwd = (XFASTINT (new_pos) > XFASTINT (old_pos));
746 if (fwd)
747 field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
748 else
749 field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
751 if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
752 other side of NEW_POS, which would mean that NEW_POS is
753 already acceptable, and it's not necessary to constrain it
754 to FIELD_BOUND. */
755 ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd)
756 /* NEW_POS should be constrained, but only if either
757 ONLY_IN_LINE is nil (in which case any constraint is OK),
758 or NEW_POS and FIELD_BOUND are on the same line (in which
759 case the constraint is OK even if ONLY_IN_LINE is non-nil). */
760 && (NILP (only_in_line)
761 /* This is the ONLY_IN_LINE case, check that NEW_POS and
762 FIELD_BOUND are on the same line by seeing whether
763 there's an intervening newline or not. */
764 || (scan_buffer ('\n',
765 XFASTINT (new_pos), XFASTINT (field_bound),
766 fwd ? -1 : 1, &shortage, 1),
767 shortage != 0)))
768 /* Constrain NEW_POS to FIELD_BOUND. */
769 new_pos = field_bound;
771 if (orig_point && XFASTINT (new_pos) != orig_point)
772 /* The NEW_POS argument was originally nil, so automatically set PT. */
773 SET_PT (XFASTINT (new_pos));
776 return new_pos;
780 DEFUN ("line-beginning-position",
781 Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
782 doc: /* Return the character position of the first character on the current line.
783 With argument N not nil or 1, move forward N - 1 lines first.
784 If scan reaches end of buffer, return that position.
786 The scan does not cross a field boundary unless doing so would move
787 beyond there to a different line; if N is nil or 1, and scan starts at a
788 field boundary, the scan stops as soon as it starts. To ignore field
789 boundaries bind `inhibit-field-text-motion' to t.
791 This function does not move point. */)
793 Lisp_Object n;
795 int orig, orig_byte, end;
797 if (NILP (n))
798 XSETFASTINT (n, 1);
799 else
800 CHECK_NUMBER (n);
802 orig = PT;
803 orig_byte = PT_BYTE;
804 Fforward_line (make_number (XINT (n) - 1));
805 end = PT;
807 SET_PT_BOTH (orig, orig_byte);
809 /* Return END constrained to the current input field. */
810 return Fconstrain_to_field (make_number (end), make_number (orig),
811 XINT (n) != 1 ? Qt : Qnil,
812 Qt, Qnil);
815 DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0,
816 doc: /* Return the character position of the last character on the current line.
817 With argument N not nil or 1, move forward N - 1 lines first.
818 If scan reaches end of buffer, return that position.
820 The scan does not cross a field boundary unless doing so would move
821 beyond there to a different line; if N is nil or 1, and scan starts at a
822 field boundary, the scan stops as soon as it starts. To ignore field
823 boundaries bind `inhibit-field-text-motion' to t.
825 This function does not move point. */)
827 Lisp_Object n;
829 int end_pos;
830 int orig = PT;
832 if (NILP (n))
833 XSETFASTINT (n, 1);
834 else
835 CHECK_NUMBER (n);
837 end_pos = find_before_next_newline (orig, 0, XINT (n) - (XINT (n) <= 0));
839 /* Return END_POS constrained to the current input field. */
840 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
841 Qnil, Qt, Qnil);
845 Lisp_Object
846 save_excursion_save ()
848 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
849 == current_buffer);
851 return Fcons (Fpoint_marker (),
852 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
853 Fcons (visible ? Qt : Qnil,
854 Fcons (current_buffer->mark_active,
855 selected_window))));
858 Lisp_Object
859 save_excursion_restore (info)
860 Lisp_Object info;
862 Lisp_Object tem, tem1, omark, nmark;
863 struct gcpro gcpro1, gcpro2, gcpro3;
864 int visible_p;
866 tem = Fmarker_buffer (XCAR (info));
867 /* If buffer being returned to is now deleted, avoid error */
868 /* Otherwise could get error here while unwinding to top level
869 and crash */
870 /* In that case, Fmarker_buffer returns nil now. */
871 if (NILP (tem))
872 return Qnil;
874 omark = nmark = Qnil;
875 GCPRO3 (info, omark, nmark);
877 Fset_buffer (tem);
879 /* Point marker. */
880 tem = XCAR (info);
881 Fgoto_char (tem);
882 unchain_marker (XMARKER (tem));
884 /* Mark marker. */
885 info = XCDR (info);
886 tem = XCAR (info);
887 omark = Fmarker_position (current_buffer->mark);
888 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
889 nmark = Fmarker_position (tem);
890 unchain_marker (XMARKER (tem));
892 /* visible */
893 info = XCDR (info);
894 visible_p = !NILP (XCAR (info));
896 #if 0 /* We used to make the current buffer visible in the selected window
897 if that was true previously. That avoids some anomalies.
898 But it creates others, and it wasn't documented, and it is simpler
899 and cleaner never to alter the window/buffer connections. */
900 tem1 = Fcar (tem);
901 if (!NILP (tem1)
902 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
903 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
904 #endif /* 0 */
906 /* Mark active */
907 info = XCDR (info);
908 tem = XCAR (info);
909 tem1 = current_buffer->mark_active;
910 current_buffer->mark_active = tem;
912 if (!NILP (Vrun_hooks))
914 /* If mark is active now, and either was not active
915 or was at a different place, run the activate hook. */
916 if (! NILP (current_buffer->mark_active))
918 if (! EQ (omark, nmark))
919 call1 (Vrun_hooks, intern ("activate-mark-hook"));
921 /* If mark has ceased to be active, run deactivate hook. */
922 else if (! NILP (tem1))
923 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
926 /* If buffer was visible in a window, and a different window was
927 selected, and the old selected window is still showing this
928 buffer, restore point in that window. */
929 tem = XCDR (info);
930 if (visible_p
931 && !EQ (tem, selected_window)
932 && (tem1 = XWINDOW (tem)->buffer,
933 (/* Window is live... */
934 BUFFERP (tem1)
935 /* ...and it shows the current buffer. */
936 && XBUFFER (tem1) == current_buffer)))
937 Fset_window_point (tem, make_number (PT));
939 UNGCPRO;
940 return Qnil;
943 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
944 doc: /* Save point, mark, and current buffer; execute BODY; restore those things.
945 Executes BODY just like `progn'.
946 The values of point, mark and the current buffer are restored
947 even in case of abnormal exit (throw or error).
948 The state of activation of the mark is also restored.
950 This construct does not save `deactivate-mark', and therefore
951 functions that change the buffer will still cause deactivation
952 of the mark at the end of the command. To prevent that, bind
953 `deactivate-mark' with `let'.
955 usage: (save-excursion &rest BODY) */)
956 (args)
957 Lisp_Object args;
959 register Lisp_Object val;
960 int count = SPECPDL_INDEX ();
962 record_unwind_protect (save_excursion_restore, save_excursion_save ());
964 val = Fprogn (args);
965 return unbind_to (count, val);
968 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
969 doc: /* Save the current buffer; execute BODY; restore the current buffer.
970 Executes BODY just like `progn'.
971 usage: (save-current-buffer &rest BODY) */)
972 (args)
973 Lisp_Object args;
975 Lisp_Object val;
976 int count = SPECPDL_INDEX ();
978 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
980 val = Fprogn (args);
981 return unbind_to (count, val);
984 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
985 doc: /* Return the number of characters in the current buffer.
986 If BUFFER, return the number of characters in that buffer instead. */)
987 (buffer)
988 Lisp_Object buffer;
990 if (NILP (buffer))
991 return make_number (Z - BEG);
992 else
994 CHECK_BUFFER (buffer);
995 return make_number (BUF_Z (XBUFFER (buffer))
996 - BUF_BEG (XBUFFER (buffer)));
1000 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
1001 doc: /* Return the minimum permissible value of point in the current buffer.
1002 This is 1, unless narrowing (a buffer restriction) is in effect. */)
1005 Lisp_Object temp;
1006 XSETFASTINT (temp, BEGV);
1007 return temp;
1010 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
1011 doc: /* Return a marker to the minimum permissible value of point in this buffer.
1012 This is the beginning, unless narrowing (a buffer restriction) is in effect. */)
1015 return buildmark (BEGV, BEGV_BYTE);
1018 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
1019 doc: /* Return the maximum permissible value of point in the current buffer.
1020 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1021 is in effect, in which case it is less. */)
1024 Lisp_Object temp;
1025 XSETFASTINT (temp, ZV);
1026 return temp;
1029 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
1030 doc: /* Return a marker to the maximum permissible value of point in this buffer.
1031 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)
1032 is in effect, in which case it is less. */)
1035 return buildmark (ZV, ZV_BYTE);
1038 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
1039 doc: /* Return the position of the gap, in the current buffer.
1040 See also `gap-size'. */)
1043 Lisp_Object temp;
1044 XSETFASTINT (temp, GPT);
1045 return temp;
1048 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
1049 doc: /* Return the size of the current buffer's gap.
1050 See also `gap-position'. */)
1053 Lisp_Object temp;
1054 XSETFASTINT (temp, GAP_SIZE);
1055 return temp;
1058 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
1059 doc: /* Return the byte position for character position POSITION.
1060 If POSITION is out of range, the value is nil. */)
1061 (position)
1062 Lisp_Object position;
1064 CHECK_NUMBER_COERCE_MARKER (position);
1065 if (XINT (position) < BEG || XINT (position) > Z)
1066 return Qnil;
1067 return make_number (CHAR_TO_BYTE (XINT (position)));
1070 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
1071 doc: /* Return the character position for byte position BYTEPOS.
1072 If BYTEPOS is out of range, the value is nil. */)
1073 (bytepos)
1074 Lisp_Object bytepos;
1076 CHECK_NUMBER (bytepos);
1077 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
1078 return Qnil;
1079 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
1082 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
1083 doc: /* Return the character following point, as a number.
1084 At the end of the buffer or accessible region, return 0. */)
1087 Lisp_Object temp;
1088 if (PT >= ZV)
1089 XSETFASTINT (temp, 0);
1090 else
1091 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
1092 return temp;
1095 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
1096 doc: /* Return the character preceding point, as a number.
1097 At the beginning of the buffer or accessible region, return 0. */)
1100 Lisp_Object temp;
1101 if (PT <= BEGV)
1102 XSETFASTINT (temp, 0);
1103 else if (!NILP (current_buffer->enable_multibyte_characters))
1105 int pos = PT_BYTE;
1106 DEC_POS (pos);
1107 XSETFASTINT (temp, FETCH_CHAR (pos));
1109 else
1110 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
1111 return temp;
1114 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
1115 doc: /* Return t if point is at the beginning of the buffer.
1116 If the buffer is narrowed, this means the beginning of the narrowed part. */)
1119 if (PT == BEGV)
1120 return Qt;
1121 return Qnil;
1124 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
1125 doc: /* Return t if point is at the end of the buffer.
1126 If the buffer is narrowed, this means the end of the narrowed part. */)
1129 if (PT == ZV)
1130 return Qt;
1131 return Qnil;
1134 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
1135 doc: /* Return t if point is at the beginning of a line. */)
1138 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
1139 return Qt;
1140 return Qnil;
1143 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
1144 doc: /* Return t if point is at the end of a line.
1145 `End of a line' includes point being at the end of the buffer. */)
1148 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
1149 return Qt;
1150 return Qnil;
1153 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
1154 doc: /* Return character in current buffer at position POS.
1155 POS is an integer or a marker and defaults to point.
1156 If POS is out of range, the value is nil. */)
1157 (pos)
1158 Lisp_Object pos;
1160 register int pos_byte;
1162 if (NILP (pos))
1164 pos_byte = PT_BYTE;
1165 XSETFASTINT (pos, PT);
1168 if (MARKERP (pos))
1170 pos_byte = marker_byte_position (pos);
1171 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1172 return Qnil;
1174 else
1176 CHECK_NUMBER_COERCE_MARKER (pos);
1177 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1178 return Qnil;
1180 pos_byte = CHAR_TO_BYTE (XINT (pos));
1183 return make_number (FETCH_CHAR (pos_byte));
1186 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1187 doc: /* Return character in current buffer preceding position POS.
1188 POS is an integer or a marker and defaults to point.
1189 If POS is out of range, the value is nil. */)
1190 (pos)
1191 Lisp_Object pos;
1193 register Lisp_Object val;
1194 register int pos_byte;
1196 if (NILP (pos))
1198 pos_byte = PT_BYTE;
1199 XSETFASTINT (pos, PT);
1202 if (MARKERP (pos))
1204 pos_byte = marker_byte_position (pos);
1206 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1207 return Qnil;
1209 else
1211 CHECK_NUMBER_COERCE_MARKER (pos);
1213 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1214 return Qnil;
1216 pos_byte = CHAR_TO_BYTE (XINT (pos));
1219 if (!NILP (current_buffer->enable_multibyte_characters))
1221 DEC_POS (pos_byte);
1222 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1224 else
1226 pos_byte--;
1227 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1229 return val;
1232 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1233 doc: /* Return the name under which the user logged in, as a string.
1234 This is based on the effective uid, not the real uid.
1235 Also, if the environment variables LOGNAME or USER are set,
1236 that determines the value of this function.
1238 If optional argument UID is an integer, return the login name of the user
1239 with that uid, or nil if there is no such user. */)
1240 (uid)
1241 Lisp_Object uid;
1243 struct passwd *pw;
1245 /* Set up the user name info if we didn't do it before.
1246 (That can happen if Emacs is dumpable
1247 but you decide to run `temacs -l loadup' and not dump. */
1248 if (INTEGERP (Vuser_login_name))
1249 init_editfns ();
1251 if (NILP (uid))
1252 return Vuser_login_name;
1254 CHECK_NUMBER (uid);
1255 pw = (struct passwd *) getpwuid (XINT (uid));
1256 return (pw ? build_string (pw->pw_name) : Qnil);
1259 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1260 0, 0, 0,
1261 doc: /* Return the name of the user's real uid, as a string.
1262 This ignores the environment variables LOGNAME and USER, so it differs from
1263 `user-login-name' when running under `su'. */)
1266 /* Set up the user name info if we didn't do it before.
1267 (That can happen if Emacs is dumpable
1268 but you decide to run `temacs -l loadup' and not dump. */
1269 if (INTEGERP (Vuser_login_name))
1270 init_editfns ();
1271 return Vuser_real_login_name;
1274 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1275 doc: /* Return the effective uid of Emacs.
1276 Value is an integer or float, depending on the value. */)
1279 return make_fixnum_or_float (geteuid ());
1282 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1283 doc: /* Return the real uid of Emacs.
1284 Value is an integer or float, depending on the value. */)
1287 return make_fixnum_or_float (getuid ());
1290 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1291 doc: /* Return the full name of the user logged in, as a string.
1292 If the full name corresponding to Emacs's userid is not known,
1293 return "unknown".
1295 If optional argument UID is an integer or float, return the full name
1296 of the user with that uid, or nil if there is no such user.
1297 If UID is a string, return the full name of the user with that login
1298 name, or nil if there is no such user. */)
1299 (uid)
1300 Lisp_Object uid;
1302 struct passwd *pw;
1303 register unsigned char *p, *q;
1304 Lisp_Object full;
1306 if (NILP (uid))
1307 return Vuser_full_name;
1308 else if (NUMBERP (uid))
1309 pw = (struct passwd *) getpwuid ((uid_t) XFLOATINT (uid));
1310 else if (STRINGP (uid))
1311 pw = (struct passwd *) getpwnam (SDATA (uid));
1312 else
1313 error ("Invalid UID specification");
1315 if (!pw)
1316 return Qnil;
1318 p = (unsigned char *) USER_FULL_NAME;
1319 /* Chop off everything after the first comma. */
1320 q = (unsigned char *) index (p, ',');
1321 full = make_string (p, q ? q - p : strlen (p));
1323 #ifdef AMPERSAND_FULL_NAME
1324 p = SDATA (full);
1325 q = (unsigned char *) index (p, '&');
1326 /* Substitute the login name for the &, upcasing the first character. */
1327 if (q)
1329 register unsigned char *r;
1330 Lisp_Object login;
1332 login = Fuser_login_name (make_number (pw->pw_uid));
1333 r = (unsigned char *) alloca (strlen (p) + SCHARS (login) + 1);
1334 bcopy (p, r, q - p);
1335 r[q - p] = 0;
1336 strcat (r, SDATA (login));
1337 r[q - p] = UPCASE (r[q - p]);
1338 strcat (r, q + 1);
1339 full = build_string (r);
1341 #endif /* AMPERSAND_FULL_NAME */
1343 return full;
1346 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1347 doc: /* Return the name of the machine you are running on, as a string. */)
1350 return Vsystem_name;
1353 /* For the benefit of callers who don't want to include lisp.h */
1355 char *
1356 get_system_name ()
1358 if (STRINGP (Vsystem_name))
1359 return (char *) SDATA (Vsystem_name);
1360 else
1361 return "";
1364 char *
1365 get_operating_system_release()
1367 if (STRINGP (Voperating_system_release))
1368 return (char *) SDATA (Voperating_system_release);
1369 else
1370 return "";
1373 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1374 doc: /* Return the process ID of Emacs, as an integer. */)
1377 return make_number (getpid ());
1380 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1381 doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
1382 The time is returned as a list of three integers. The first has the
1383 most significant 16 bits of the seconds, while the second has the
1384 least significant 16 bits. The third integer gives the microsecond
1385 count.
1387 The microsecond count is zero on systems that do not provide
1388 resolution finer than a second. */)
1391 EMACS_TIME t;
1392 Lisp_Object result[3];
1394 EMACS_GET_TIME (t);
1395 XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff);
1396 XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff);
1397 XSETINT (result[2], EMACS_USECS (t));
1399 return Flist (3, result);
1402 DEFUN ("get-internal-run-time", Fget_internal_run_time, Sget_internal_run_time,
1403 0, 0, 0,
1404 doc: /* Return the current run time used by Emacs.
1405 The time is returned as a list of three integers. The first has the
1406 most significant 16 bits of the seconds, while the second has the
1407 least significant 16 bits. The third integer gives the microsecond
1408 count.
1410 On systems that can't determine the run time, get-internal-run-time
1411 does the same thing as current-time. The microsecond count is zero on
1412 systems that do not provide resolution finer than a second. */)
1415 #ifdef HAVE_GETRUSAGE
1416 struct rusage usage;
1417 Lisp_Object result[3];
1418 int secs, usecs;
1420 if (getrusage (RUSAGE_SELF, &usage) < 0)
1421 /* This shouldn't happen. What action is appropriate? */
1422 Fsignal (Qerror, Qnil);
1424 /* Sum up user time and system time. */
1425 secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
1426 usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
1427 if (usecs >= 1000000)
1429 usecs -= 1000000;
1430 secs++;
1433 XSETINT (result[0], (secs >> 16) & 0xffff);
1434 XSETINT (result[1], (secs >> 0) & 0xffff);
1435 XSETINT (result[2], usecs);
1437 return Flist (3, result);
1438 #else
1439 return Fcurrent_time ();
1440 #endif
1445 lisp_time_argument (specified_time, result, usec)
1446 Lisp_Object specified_time;
1447 time_t *result;
1448 int *usec;
1450 if (NILP (specified_time))
1452 if (usec)
1454 EMACS_TIME t;
1456 EMACS_GET_TIME (t);
1457 *usec = EMACS_USECS (t);
1458 *result = EMACS_SECS (t);
1459 return 1;
1461 else
1462 return time (result) != -1;
1464 else
1466 Lisp_Object high, low;
1467 high = Fcar (specified_time);
1468 CHECK_NUMBER (high);
1469 low = Fcdr (specified_time);
1470 if (CONSP (low))
1472 if (usec)
1474 Lisp_Object usec_l = Fcdr (low);
1475 if (CONSP (usec_l))
1476 usec_l = Fcar (usec_l);
1477 if (NILP (usec_l))
1478 *usec = 0;
1479 else
1481 CHECK_NUMBER (usec_l);
1482 *usec = XINT (usec_l);
1485 low = Fcar (low);
1487 else if (usec)
1488 *usec = 0;
1489 CHECK_NUMBER (low);
1490 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
1491 return *result >> 16 == XINT (high);
1495 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1496 doc: /* Return the current time, as a float number of seconds since the epoch.
1497 If SPECIFIED-TIME is given, it is the time to convert to float
1498 instead of the current time. The argument should have the form
1499 (HIGH LOW . IGNORED). Thus, you can use times obtained from
1500 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1501 have the form (HIGH . LOW), but this is considered obsolete.
1503 WARNING: Since the result is floating point, it may not be exact.
1504 Do not use this function if precise time stamps are required. */)
1505 (specified_time)
1506 Lisp_Object specified_time;
1508 time_t sec;
1509 int usec;
1511 if (! lisp_time_argument (specified_time, &sec, &usec))
1512 error ("Invalid time specification");
1514 return make_float ((sec * 1e6 + usec) / 1e6);
1517 /* Write information into buffer S of size MAXSIZE, according to the
1518 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1519 Default to Universal Time if UT is nonzero, local time otherwise.
1520 Return the number of bytes written, not including the terminating
1521 '\0'. If S is NULL, nothing will be written anywhere; so to
1522 determine how many bytes would be written, use NULL for S and
1523 ((size_t) -1) for MAXSIZE.
1525 This function behaves like emacs_strftimeu, except it allows null
1526 bytes in FORMAT. */
1527 static size_t
1528 emacs_memftimeu (s, maxsize, format, format_len, tp, ut)
1529 char *s;
1530 size_t maxsize;
1531 const char *format;
1532 size_t format_len;
1533 const struct tm *tp;
1534 int ut;
1536 size_t total = 0;
1538 /* Loop through all the null-terminated strings in the format
1539 argument. Normally there's just one null-terminated string, but
1540 there can be arbitrarily many, concatenated together, if the
1541 format contains '\0' bytes. emacs_strftimeu stops at the first
1542 '\0' byte so we must invoke it separately for each such string. */
1543 for (;;)
1545 size_t len;
1546 size_t result;
1548 if (s)
1549 s[0] = '\1';
1551 result = emacs_strftimeu (s, maxsize, format, tp, ut);
1553 if (s)
1555 if (result == 0 && s[0] != '\0')
1556 return 0;
1557 s += result + 1;
1560 maxsize -= result + 1;
1561 total += result;
1562 len = strlen (format);
1563 if (len == format_len)
1564 return total;
1565 total++;
1566 format += len + 1;
1567 format_len -= len + 1;
1571 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1572 doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted.
1573 TIME is specified as (HIGH LOW . IGNORED), as returned by
1574 `current-time' or `file-attributes'. The obsolete form (HIGH . LOW)
1575 is also still accepted.
1576 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME
1577 as Universal Time; nil means describe TIME in the local time zone.
1578 The value is a copy of FORMAT-STRING, but with certain constructs replaced
1579 by text that describes the specified date and time in TIME:
1581 %Y is the year, %y within the century, %C the century.
1582 %G is the year corresponding to the ISO week, %g within the century.
1583 %m is the numeric month.
1584 %b and %h are the locale's abbreviated month name, %B the full name.
1585 %d is the day of the month, zero-padded, %e is blank-padded.
1586 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
1587 %a is the locale's abbreviated name of the day of week, %A the full name.
1588 %U is the week number starting on Sunday, %W starting on Monday,
1589 %V according to ISO 8601.
1590 %j is the day of the year.
1592 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
1593 only blank-padded, %l is like %I blank-padded.
1594 %p is the locale's equivalent of either AM or PM.
1595 %M is the minute.
1596 %S is the second.
1597 %Z is the time zone name, %z is the numeric form.
1598 %s is the number of seconds since 1970-01-01 00:00:00 +0000.
1600 %c is the locale's date and time format.
1601 %x is the locale's "preferred" date format.
1602 %D is like "%m/%d/%y".
1604 %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p".
1605 %X is the locale's "preferred" time format.
1607 Finally, %n is a newline, %t is a tab, %% is a literal %.
1609 Certain flags and modifiers are available with some format controls.
1610 The flags are `_', `-', `^' and `#'. For certain characters X,
1611 %_X is like %X, but padded with blanks; %-X is like %X,
1612 but without padding. %^X is like %X, but with all textual
1613 characters up-cased; %#X is like %X, but with letter-case of
1614 all textual characters reversed.
1615 %NX (where N stands for an integer) is like %X,
1616 but takes up at least N (a number) positions.
1617 The modifiers are `E' and `O'. For certain characters X,
1618 %EX is a locale's alternative version of %X;
1619 %OX is like %X, but uses the locale's number symbols.
1621 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z". */)
1622 (format_string, time, universal)
1623 Lisp_Object format_string, time, universal;
1625 time_t value;
1626 int size;
1627 struct tm *tm;
1628 int ut = ! NILP (universal);
1630 CHECK_STRING (format_string);
1632 if (! lisp_time_argument (time, &value, NULL))
1633 error ("Invalid time specification");
1635 format_string = code_convert_string_norecord (format_string,
1636 Vlocale_coding_system, 1);
1638 /* This is probably enough. */
1639 size = SBYTES (format_string) * 6 + 50;
1641 tm = ut ? gmtime (&value) : localtime (&value);
1642 if (! tm)
1643 error ("Specified time is not representable");
1645 synchronize_system_time_locale ();
1647 while (1)
1649 char *buf = (char *) alloca (size + 1);
1650 int result;
1652 buf[0] = '\1';
1653 result = emacs_memftimeu (buf, size, SDATA (format_string),
1654 SBYTES (format_string),
1655 tm, ut);
1656 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
1657 return code_convert_string_norecord (make_string (buf, result),
1658 Vlocale_coding_system, 0);
1660 /* If buffer was too small, make it bigger and try again. */
1661 result = emacs_memftimeu (NULL, (size_t) -1,
1662 SDATA (format_string),
1663 SBYTES (format_string),
1664 tm, ut);
1665 size = result + 1;
1669 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1670 doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
1671 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED),
1672 as from `current-time' and `file-attributes', or `nil' to use the
1673 current time. The obsolete form (HIGH . LOW) is also still accepted.
1674 The list has the following nine members: SEC is an integer between 0
1675 and 60; SEC is 60 for a leap second, which only some operating systems
1676 support. MINUTE is an integer between 0 and 59. HOUR is an integer
1677 between 0 and 23. DAY is an integer between 1 and 31. MONTH is an
1678 integer between 1 and 12. YEAR is an integer indicating the
1679 four-digit year. DOW is the day of week, an integer between 0 and 6,
1680 where 0 is Sunday. DST is t if daylight savings time is effect,
1681 otherwise nil. ZONE is an integer indicating the number of seconds
1682 east of Greenwich. (Note that Common Lisp has different meanings for
1683 DOW and ZONE.) */)
1684 (specified_time)
1685 Lisp_Object specified_time;
1687 time_t time_spec;
1688 struct tm save_tm;
1689 struct tm *decoded_time;
1690 Lisp_Object list_args[9];
1692 if (! lisp_time_argument (specified_time, &time_spec, NULL))
1693 error ("Invalid time specification");
1695 decoded_time = localtime (&time_spec);
1696 if (! decoded_time)
1697 error ("Specified time is not representable");
1698 XSETFASTINT (list_args[0], decoded_time->tm_sec);
1699 XSETFASTINT (list_args[1], decoded_time->tm_min);
1700 XSETFASTINT (list_args[2], decoded_time->tm_hour);
1701 XSETFASTINT (list_args[3], decoded_time->tm_mday);
1702 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
1703 XSETINT (list_args[5], decoded_time->tm_year + 1900);
1704 XSETFASTINT (list_args[6], decoded_time->tm_wday);
1705 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
1707 /* Make a copy, in case gmtime modifies the struct. */
1708 save_tm = *decoded_time;
1709 decoded_time = gmtime (&time_spec);
1710 if (decoded_time == 0)
1711 list_args[8] = Qnil;
1712 else
1713 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1714 return Flist (9, list_args);
1717 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1718 doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
1719 This is the reverse operation of `decode-time', which see.
1720 ZONE defaults to the current time zone rule. This can
1721 be a string or t (as from `set-time-zone-rule'), or it can be a list
1722 \(as from `current-time-zone') or an integer (as from `decode-time')
1723 applied without consideration for daylight savings time.
1725 You can pass more than 7 arguments; then the first six arguments
1726 are used as SECOND through YEAR, and the *last* argument is used as ZONE.
1727 The intervening arguments are ignored.
1728 This feature lets (apply 'encode-time (decode-time ...)) work.
1730 Out-of-range values for SECOND, MINUTE, HOUR, DAY, or MONTH are allowed;
1731 for example, a DAY of 0 means the day preceding the given month.
1732 Year numbers less than 100 are treated just like other year numbers.
1733 If you want them to stand for years in this century, you must do that yourself.
1735 Years before 1970 are not guaranteed to work. On some systems,
1736 year values as low as 1901 do work.
1738 usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */)
1739 (nargs, args)
1740 int nargs;
1741 register Lisp_Object *args;
1743 time_t time;
1744 struct tm tm;
1745 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1747 CHECK_NUMBER (args[0]); /* second */
1748 CHECK_NUMBER (args[1]); /* minute */
1749 CHECK_NUMBER (args[2]); /* hour */
1750 CHECK_NUMBER (args[3]); /* day */
1751 CHECK_NUMBER (args[4]); /* month */
1752 CHECK_NUMBER (args[5]); /* year */
1754 tm.tm_sec = XINT (args[0]);
1755 tm.tm_min = XINT (args[1]);
1756 tm.tm_hour = XINT (args[2]);
1757 tm.tm_mday = XINT (args[3]);
1758 tm.tm_mon = XINT (args[4]) - 1;
1759 tm.tm_year = XINT (args[5]) - 1900;
1760 tm.tm_isdst = -1;
1762 if (CONSP (zone))
1763 zone = Fcar (zone);
1764 if (NILP (zone))
1765 time = mktime (&tm);
1766 else
1768 char tzbuf[100];
1769 char *tzstring;
1770 char **oldenv = environ, **newenv;
1772 if (EQ (zone, Qt))
1773 tzstring = "UTC0";
1774 else if (STRINGP (zone))
1775 tzstring = (char *) SDATA (zone);
1776 else if (INTEGERP (zone))
1778 int abszone = abs (XINT (zone));
1779 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
1780 abszone / (60*60), (abszone/60) % 60, abszone % 60);
1781 tzstring = tzbuf;
1783 else
1784 error ("Invalid time zone specification");
1786 /* Set TZ before calling mktime; merely adjusting mktime's returned
1787 value doesn't suffice, since that would mishandle leap seconds. */
1788 set_time_zone_rule (tzstring);
1790 time = mktime (&tm);
1792 /* Restore TZ to previous value. */
1793 newenv = environ;
1794 environ = oldenv;
1795 xfree (newenv);
1796 #ifdef LOCALTIME_CACHE
1797 tzset ();
1798 #endif
1801 if (time == (time_t) -1)
1802 error ("Specified time is not representable");
1804 return make_time (time);
1807 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1808 doc: /* Return the current time, as a human-readable string.
1809 Programs can use this function to decode a time,
1810 since the number of columns in each field is fixed.
1811 The format is `Sun Sep 16 01:03:52 1973'.
1812 However, see also the functions `decode-time' and `format-time-string'
1813 which provide a much more powerful and general facility.
1815 If SPECIFIED-TIME is given, it is a time to format instead of the
1816 current time. The argument should have the form (HIGH LOW . IGNORED).
1817 Thus, you can use times obtained from `current-time' and from
1818 `file-attributes'. SPECIFIED-TIME can also have the form (HIGH . LOW),
1819 but this is considered obsolete. */)
1820 (specified_time)
1821 Lisp_Object specified_time;
1823 time_t value;
1824 char buf[30];
1825 register char *tem;
1827 if (! lisp_time_argument (specified_time, &value, NULL))
1828 value = -1;
1829 tem = (char *) ctime (&value);
1831 strncpy (buf, tem, 24);
1832 buf[24] = 0;
1834 return build_string (buf);
1837 #define TM_YEAR_BASE 1900
1839 /* Yield A - B, measured in seconds.
1840 This function is copied from the GNU C Library. */
1841 static int
1842 tm_diff (a, b)
1843 struct tm *a, *b;
1845 /* Compute intervening leap days correctly even if year is negative.
1846 Take care to avoid int overflow in leap day calculations,
1847 but it's OK to assume that A and B are close to each other. */
1848 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1849 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1850 int a100 = a4 / 25 - (a4 % 25 < 0);
1851 int b100 = b4 / 25 - (b4 % 25 < 0);
1852 int a400 = a100 >> 2;
1853 int b400 = b100 >> 2;
1854 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1855 int years = a->tm_year - b->tm_year;
1856 int days = (365 * years + intervening_leap_days
1857 + (a->tm_yday - b->tm_yday));
1858 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1859 + (a->tm_min - b->tm_min))
1860 + (a->tm_sec - b->tm_sec));
1863 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1864 doc: /* Return the offset and name for the local time zone.
1865 This returns a list of the form (OFFSET NAME).
1866 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).
1867 A negative value means west of Greenwich.
1868 NAME is a string giving the name of the time zone.
1869 If SPECIFIED-TIME is given, the time zone offset is determined from it
1870 instead of using the current time. The argument should have the form
1871 (HIGH LOW . IGNORED). Thus, you can use times obtained from
1872 `current-time' and from `file-attributes'. SPECIFIED-TIME can also
1873 have the form (HIGH . LOW), but this is considered obsolete.
1875 Some operating systems cannot provide all this information to Emacs;
1876 in this case, `current-time-zone' returns a list containing nil for
1877 the data it can't find. */)
1878 (specified_time)
1879 Lisp_Object specified_time;
1881 time_t value;
1882 struct tm *t;
1883 struct tm gmt;
1885 if (lisp_time_argument (specified_time, &value, NULL)
1886 && (t = gmtime (&value)) != 0
1887 && (gmt = *t, t = localtime (&value)) != 0)
1889 int offset = tm_diff (t, &gmt);
1890 char *s = 0;
1891 char buf[6];
1892 #ifdef HAVE_TM_ZONE
1893 if (t->tm_zone)
1894 s = (char *)t->tm_zone;
1895 #else /* not HAVE_TM_ZONE */
1896 #ifdef HAVE_TZNAME
1897 if (t->tm_isdst == 0 || t->tm_isdst == 1)
1898 s = tzname[t->tm_isdst];
1899 #endif
1900 #endif /* not HAVE_TM_ZONE */
1902 #if defined HAVE_TM_ZONE || defined HAVE_TZNAME
1903 if (s)
1905 /* On Japanese w32, we can get a Japanese string as time
1906 zone name. Don't accept that. */
1907 char *p;
1908 for (p = s; *p && (isalnum ((unsigned char)*p) || *p == ' '); ++p)
1910 if (p == s || *p)
1911 s = NULL;
1913 #endif
1915 if (!s)
1917 /* No local time zone name is available; use "+-NNNN" instead. */
1918 int am = (offset < 0 ? -offset : offset) / 60;
1919 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
1920 s = buf;
1922 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
1924 else
1925 return Fmake_list (make_number (2), Qnil);
1928 /* This holds the value of `environ' produced by the previous
1929 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1930 has never been called. */
1931 static char **environbuf;
1933 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1934 doc: /* Set the local time zone using TZ, a string specifying a time zone rule.
1935 If TZ is nil, use implementation-defined default time zone information.
1936 If TZ is t, use Universal Time. */)
1937 (tz)
1938 Lisp_Object tz;
1940 char *tzstring;
1942 if (NILP (tz))
1943 tzstring = 0;
1944 else if (EQ (tz, Qt))
1945 tzstring = "UTC0";
1946 else
1948 CHECK_STRING (tz);
1949 tzstring = (char *) SDATA (tz);
1952 set_time_zone_rule (tzstring);
1953 if (environbuf)
1954 free (environbuf);
1955 environbuf = environ;
1957 return Qnil;
1960 #ifdef LOCALTIME_CACHE
1962 /* These two values are known to load tz files in buggy implementations,
1963 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1964 Their values shouldn't matter in non-buggy implementations.
1965 We don't use string literals for these strings,
1966 since if a string in the environment is in readonly
1967 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1968 See Sun bugs 1113095 and 1114114, ``Timezone routines
1969 improperly modify environment''. */
1971 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1972 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1974 #endif
1976 /* Set the local time zone rule to TZSTRING.
1977 This allocates memory into `environ', which it is the caller's
1978 responsibility to free. */
1980 void
1981 set_time_zone_rule (tzstring)
1982 char *tzstring;
1984 int envptrs;
1985 char **from, **to, **newenv;
1987 /* Make the ENVIRON vector longer with room for TZSTRING. */
1988 for (from = environ; *from; from++)
1989 continue;
1990 envptrs = from - environ + 2;
1991 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
1992 + (tzstring ? strlen (tzstring) + 4 : 0));
1994 /* Add TZSTRING to the end of environ, as a value for TZ. */
1995 if (tzstring)
1997 char *t = (char *) (to + envptrs);
1998 strcpy (t, "TZ=");
1999 strcat (t, tzstring);
2000 *to++ = t;
2003 /* Copy the old environ vector elements into NEWENV,
2004 but don't copy the TZ variable.
2005 So we have only one definition of TZ, which came from TZSTRING. */
2006 for (from = environ; *from; from++)
2007 if (strncmp (*from, "TZ=", 3) != 0)
2008 *to++ = *from;
2009 *to = 0;
2011 environ = newenv;
2013 /* If we do have a TZSTRING, NEWENV points to the vector slot where
2014 the TZ variable is stored. If we do not have a TZSTRING,
2015 TO points to the vector slot which has the terminating null. */
2017 #ifdef LOCALTIME_CACHE
2019 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
2020 "US/Pacific" that loads a tz file, then changes to a value like
2021 "XXX0" that does not load a tz file, and then changes back to
2022 its original value, the last change is (incorrectly) ignored.
2023 Also, if TZ changes twice in succession to values that do
2024 not load a tz file, tzset can dump core (see Sun bug#1225179).
2025 The following code works around these bugs. */
2027 if (tzstring)
2029 /* Temporarily set TZ to a value that loads a tz file
2030 and that differs from tzstring. */
2031 char *tz = *newenv;
2032 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
2033 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
2034 tzset ();
2035 *newenv = tz;
2037 else
2039 /* The implied tzstring is unknown, so temporarily set TZ to
2040 two different values that each load a tz file. */
2041 *to = set_time_zone_rule_tz1;
2042 to[1] = 0;
2043 tzset ();
2044 *to = set_time_zone_rule_tz2;
2045 tzset ();
2046 *to = 0;
2049 /* Now TZ has the desired value, and tzset can be invoked safely. */
2052 tzset ();
2053 #endif
2056 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
2057 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
2058 type of object is Lisp_String). INHERIT is passed to
2059 INSERT_FROM_STRING_FUNC as the last argument. */
2061 static void
2062 general_insert_function (insert_func, insert_from_string_func,
2063 inherit, nargs, args)
2064 void (*insert_func) P_ ((const unsigned char *, int));
2065 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
2066 int inherit, nargs;
2067 register Lisp_Object *args;
2069 register int argnum;
2070 register Lisp_Object val;
2072 for (argnum = 0; argnum < nargs; argnum++)
2074 val = args[argnum];
2075 retry:
2076 if (INTEGERP (val))
2078 unsigned char str[MAX_MULTIBYTE_LENGTH];
2079 int len;
2081 if (!NILP (current_buffer->enable_multibyte_characters))
2082 len = CHAR_STRING (XFASTINT (val), str);
2083 else
2085 str[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
2086 ? XINT (val)
2087 : multibyte_char_to_unibyte (XINT (val), Qnil));
2088 len = 1;
2090 (*insert_func) (str, len);
2092 else if (STRINGP (val))
2094 (*insert_from_string_func) (val, 0, 0,
2095 SCHARS (val),
2096 SBYTES (val),
2097 inherit);
2099 else
2101 val = wrong_type_argument (Qchar_or_string_p, val);
2102 goto retry;
2107 void
2108 insert1 (arg)
2109 Lisp_Object arg;
2111 Finsert (1, &arg);
2115 /* Callers passing one argument to Finsert need not gcpro the
2116 argument "array", since the only element of the array will
2117 not be used after calling insert or insert_from_string, so
2118 we don't care if it gets trashed. */
2120 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
2121 doc: /* Insert the arguments, either strings or characters, at point.
2122 Point and before-insertion markers move forward to end up
2123 after the inserted text.
2124 Any other markers at the point of insertion remain before the text.
2126 If the current buffer is multibyte, unibyte strings are converted
2127 to multibyte for insertion (see `string-make-multibyte').
2128 If the current buffer is unibyte, multibyte strings are converted
2129 to unibyte for insertion (see `string-make-unibyte').
2131 When operating on binary data, it may be necessary to preserve the
2132 original bytes of a unibyte string when inserting it into a multibyte
2133 buffer; to accomplish this, apply `string-as-multibyte' to the string
2134 and insert the result.
2136 usage: (insert &rest ARGS) */)
2137 (nargs, args)
2138 int nargs;
2139 register Lisp_Object *args;
2141 general_insert_function (insert, insert_from_string, 0, nargs, args);
2142 return Qnil;
2145 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
2146 0, MANY, 0,
2147 doc: /* Insert the arguments at point, inheriting properties from adjoining text.
2148 Point and before-insertion markers move forward to end up
2149 after the inserted text.
2150 Any other markers at the point of insertion remain before the text.
2152 If the current buffer is multibyte, unibyte strings are converted
2153 to multibyte for insertion (see `unibyte-char-to-multibyte').
2154 If the current buffer is unibyte, multibyte strings are converted
2155 to unibyte for insertion.
2157 usage: (insert-and-inherit &rest ARGS) */)
2158 (nargs, args)
2159 int nargs;
2160 register Lisp_Object *args;
2162 general_insert_function (insert_and_inherit, insert_from_string, 1,
2163 nargs, args);
2164 return Qnil;
2167 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
2168 doc: /* Insert strings or characters at point, relocating markers after the text.
2169 Point and markers move forward to end up after the inserted text.
2171 If the current buffer is multibyte, unibyte strings are converted
2172 to multibyte for insertion (see `unibyte-char-to-multibyte').
2173 If the current buffer is unibyte, multibyte strings are converted
2174 to unibyte for insertion.
2176 usage: (insert-before-markers &rest ARGS) */)
2177 (nargs, args)
2178 int nargs;
2179 register Lisp_Object *args;
2181 general_insert_function (insert_before_markers,
2182 insert_from_string_before_markers, 0,
2183 nargs, args);
2184 return Qnil;
2187 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
2188 Sinsert_and_inherit_before_markers, 0, MANY, 0,
2189 doc: /* Insert text at point, relocating markers and inheriting properties.
2190 Point and markers move forward to end up after the inserted text.
2192 If the current buffer is multibyte, unibyte strings are converted
2193 to multibyte for insertion (see `unibyte-char-to-multibyte').
2194 If the current buffer is unibyte, multibyte strings are converted
2195 to unibyte for insertion.
2197 usage: (insert-before-markers-and-inherit &rest ARGS) */)
2198 (nargs, args)
2199 int nargs;
2200 register Lisp_Object *args;
2202 general_insert_function (insert_before_markers_and_inherit,
2203 insert_from_string_before_markers, 1,
2204 nargs, args);
2205 return Qnil;
2208 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2209 doc: /* Insert COUNT (second arg) copies of CHARACTER (first arg).
2210 Both arguments are required.
2211 Point, and before-insertion markers, are relocated as in the function `insert'.
2212 The optional third arg INHERIT, if non-nil, says to inherit text properties
2213 from adjoining text, if those properties are sticky. */)
2214 (character, count, inherit)
2215 Lisp_Object character, count, inherit;
2217 register unsigned char *string;
2218 register int strlen;
2219 register int i, n;
2220 int len;
2221 unsigned char str[MAX_MULTIBYTE_LENGTH];
2223 CHECK_NUMBER (character);
2224 CHECK_NUMBER (count);
2226 if (!NILP (current_buffer->enable_multibyte_characters))
2227 len = CHAR_STRING (XFASTINT (character), str);
2228 else
2229 str[0] = XFASTINT (character), len = 1;
2230 n = XINT (count) * len;
2231 if (n <= 0)
2232 return Qnil;
2233 strlen = min (n, 256 * len);
2234 string = (unsigned char *) alloca (strlen);
2235 for (i = 0; i < strlen; i++)
2236 string[i] = str[i % len];
2237 while (n >= strlen)
2239 QUIT;
2240 if (!NILP (inherit))
2241 insert_and_inherit (string, strlen);
2242 else
2243 insert (string, strlen);
2244 n -= strlen;
2246 if (n > 0)
2248 if (!NILP (inherit))
2249 insert_and_inherit (string, n);
2250 else
2251 insert (string, n);
2253 return Qnil;
2257 /* Making strings from buffer contents. */
2259 /* Return a Lisp_String containing the text of the current buffer from
2260 START to END. If text properties are in use and the current buffer
2261 has properties in the range specified, the resulting string will also
2262 have them, if PROPS is nonzero.
2264 We don't want to use plain old make_string here, because it calls
2265 make_uninit_string, which can cause the buffer arena to be
2266 compacted. make_string has no way of knowing that the data has
2267 been moved, and thus copies the wrong data into the string. This
2268 doesn't effect most of the other users of make_string, so it should
2269 be left as is. But we should use this function when conjuring
2270 buffer substrings. */
2272 Lisp_Object
2273 make_buffer_string (start, end, props)
2274 int start, end;
2275 int props;
2277 int start_byte = CHAR_TO_BYTE (start);
2278 int end_byte = CHAR_TO_BYTE (end);
2280 return make_buffer_string_both (start, start_byte, end, end_byte, props);
2283 /* Return a Lisp_String containing the text of the current buffer from
2284 START / START_BYTE to END / END_BYTE.
2286 If text properties are in use and the current buffer
2287 has properties in the range specified, the resulting string will also
2288 have them, if PROPS is nonzero.
2290 We don't want to use plain old make_string here, because it calls
2291 make_uninit_string, which can cause the buffer arena to be
2292 compacted. make_string has no way of knowing that the data has
2293 been moved, and thus copies the wrong data into the string. This
2294 doesn't effect most of the other users of make_string, so it should
2295 be left as is. But we should use this function when conjuring
2296 buffer substrings. */
2298 Lisp_Object
2299 make_buffer_string_both (start, start_byte, end, end_byte, props)
2300 int start, start_byte, end, end_byte;
2301 int props;
2303 Lisp_Object result, tem, tem1;
2305 if (start < GPT && GPT < end)
2306 move_gap (start);
2308 if (! NILP (current_buffer->enable_multibyte_characters))
2309 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
2310 else
2311 result = make_uninit_string (end - start);
2312 bcopy (BYTE_POS_ADDR (start_byte), SDATA (result),
2313 end_byte - start_byte);
2315 /* If desired, update and copy the text properties. */
2316 if (props)
2318 update_buffer_properties (start, end);
2320 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
2321 tem1 = Ftext_properties_at (make_number (start), Qnil);
2323 if (XINT (tem) != end || !NILP (tem1))
2324 copy_intervals_to_string (result, current_buffer, start,
2325 end - start);
2328 return result;
2331 /* Call Vbuffer_access_fontify_functions for the range START ... END
2332 in the current buffer, if necessary. */
2334 static void
2335 update_buffer_properties (start, end)
2336 int start, end;
2338 /* If this buffer has some access functions,
2339 call them, specifying the range of the buffer being accessed. */
2340 if (!NILP (Vbuffer_access_fontify_functions))
2342 Lisp_Object args[3];
2343 Lisp_Object tem;
2345 args[0] = Qbuffer_access_fontify_functions;
2346 XSETINT (args[1], start);
2347 XSETINT (args[2], end);
2349 /* But don't call them if we can tell that the work
2350 has already been done. */
2351 if (!NILP (Vbuffer_access_fontified_property))
2353 tem = Ftext_property_any (args[1], args[2],
2354 Vbuffer_access_fontified_property,
2355 Qnil, Qnil);
2356 if (! NILP (tem))
2357 Frun_hook_with_args (3, args);
2359 else
2360 Frun_hook_with_args (3, args);
2364 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2365 doc: /* Return the contents of part of the current buffer as a string.
2366 The two arguments START and END are character positions;
2367 they can be in either order.
2368 The string returned is multibyte if the buffer is multibyte.
2370 This function copies the text properties of that part of the buffer
2371 into the result string; if you don't want the text properties,
2372 use `buffer-substring-no-properties' instead. */)
2373 (start, end)
2374 Lisp_Object start, end;
2376 register int b, e;
2378 validate_region (&start, &end);
2379 b = XINT (start);
2380 e = XINT (end);
2382 return make_buffer_string (b, e, 1);
2385 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2386 Sbuffer_substring_no_properties, 2, 2, 0,
2387 doc: /* Return the characters of part of the buffer, without the text properties.
2388 The two arguments START and END are character positions;
2389 they can be in either order. */)
2390 (start, end)
2391 Lisp_Object start, end;
2393 register int b, e;
2395 validate_region (&start, &end);
2396 b = XINT (start);
2397 e = XINT (end);
2399 return make_buffer_string (b, e, 0);
2402 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2403 doc: /* Return the contents of the current buffer as a string.
2404 If narrowing is in effect, this function returns only the visible part
2405 of the buffer. */)
2408 return make_buffer_string (BEGV, ZV, 1);
2411 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2412 1, 3, 0,
2413 doc: /* Insert before point a substring of the contents of BUFFER.
2414 BUFFER may be a buffer or a buffer name.
2415 Arguments START and END are character positions specifying the substring.
2416 They default to the values of (point-min) and (point-max) in BUFFER. */)
2417 (buffer, start, end)
2418 Lisp_Object buffer, start, end;
2420 register int b, e, temp;
2421 register struct buffer *bp, *obuf;
2422 Lisp_Object buf;
2424 buf = Fget_buffer (buffer);
2425 if (NILP (buf))
2426 nsberror (buffer);
2427 bp = XBUFFER (buf);
2428 if (NILP (bp->name))
2429 error ("Selecting deleted buffer");
2431 if (NILP (start))
2432 b = BUF_BEGV (bp);
2433 else
2435 CHECK_NUMBER_COERCE_MARKER (start);
2436 b = XINT (start);
2438 if (NILP (end))
2439 e = BUF_ZV (bp);
2440 else
2442 CHECK_NUMBER_COERCE_MARKER (end);
2443 e = XINT (end);
2446 if (b > e)
2447 temp = b, b = e, e = temp;
2449 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2450 args_out_of_range (start, end);
2452 obuf = current_buffer;
2453 set_buffer_internal_1 (bp);
2454 update_buffer_properties (b, e);
2455 set_buffer_internal_1 (obuf);
2457 insert_from_buffer (bp, b, e - b, 0);
2458 return Qnil;
2461 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2462 6, 6, 0,
2463 doc: /* Compare two substrings of two buffers; return result as number.
2464 the value is -N if first string is less after N-1 chars,
2465 +N if first string is greater after N-1 chars, or 0 if strings match.
2466 Each substring is represented as three arguments: BUFFER, START and END.
2467 That makes six args in all, three for each substring.
2469 The value of `case-fold-search' in the current buffer
2470 determines whether case is significant or ignored. */)
2471 (buffer1, start1, end1, buffer2, start2, end2)
2472 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
2474 register int begp1, endp1, begp2, endp2, temp;
2475 register struct buffer *bp1, *bp2;
2476 register Lisp_Object *trt
2477 = (!NILP (current_buffer->case_fold_search)
2478 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
2479 int chars = 0;
2480 int i1, i2, i1_byte, i2_byte;
2482 /* Find the first buffer and its substring. */
2484 if (NILP (buffer1))
2485 bp1 = current_buffer;
2486 else
2488 Lisp_Object buf1;
2489 buf1 = Fget_buffer (buffer1);
2490 if (NILP (buf1))
2491 nsberror (buffer1);
2492 bp1 = XBUFFER (buf1);
2493 if (NILP (bp1->name))
2494 error ("Selecting deleted buffer");
2497 if (NILP (start1))
2498 begp1 = BUF_BEGV (bp1);
2499 else
2501 CHECK_NUMBER_COERCE_MARKER (start1);
2502 begp1 = XINT (start1);
2504 if (NILP (end1))
2505 endp1 = BUF_ZV (bp1);
2506 else
2508 CHECK_NUMBER_COERCE_MARKER (end1);
2509 endp1 = XINT (end1);
2512 if (begp1 > endp1)
2513 temp = begp1, begp1 = endp1, endp1 = temp;
2515 if (!(BUF_BEGV (bp1) <= begp1
2516 && begp1 <= endp1
2517 && endp1 <= BUF_ZV (bp1)))
2518 args_out_of_range (start1, end1);
2520 /* Likewise for second substring. */
2522 if (NILP (buffer2))
2523 bp2 = current_buffer;
2524 else
2526 Lisp_Object buf2;
2527 buf2 = Fget_buffer (buffer2);
2528 if (NILP (buf2))
2529 nsberror (buffer2);
2530 bp2 = XBUFFER (buf2);
2531 if (NILP (bp2->name))
2532 error ("Selecting deleted buffer");
2535 if (NILP (start2))
2536 begp2 = BUF_BEGV (bp2);
2537 else
2539 CHECK_NUMBER_COERCE_MARKER (start2);
2540 begp2 = XINT (start2);
2542 if (NILP (end2))
2543 endp2 = BUF_ZV (bp2);
2544 else
2546 CHECK_NUMBER_COERCE_MARKER (end2);
2547 endp2 = XINT (end2);
2550 if (begp2 > endp2)
2551 temp = begp2, begp2 = endp2, endp2 = temp;
2553 if (!(BUF_BEGV (bp2) <= begp2
2554 && begp2 <= endp2
2555 && endp2 <= BUF_ZV (bp2)))
2556 args_out_of_range (start2, end2);
2558 i1 = begp1;
2559 i2 = begp2;
2560 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2561 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2563 while (i1 < endp1 && i2 < endp2)
2565 /* When we find a mismatch, we must compare the
2566 characters, not just the bytes. */
2567 int c1, c2;
2569 QUIT;
2571 if (! NILP (bp1->enable_multibyte_characters))
2573 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2574 BUF_INC_POS (bp1, i1_byte);
2575 i1++;
2577 else
2579 c1 = BUF_FETCH_BYTE (bp1, i1);
2580 c1 = unibyte_char_to_multibyte (c1);
2581 i1++;
2584 if (! NILP (bp2->enable_multibyte_characters))
2586 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2587 BUF_INC_POS (bp2, i2_byte);
2588 i2++;
2590 else
2592 c2 = BUF_FETCH_BYTE (bp2, i2);
2593 c2 = unibyte_char_to_multibyte (c2);
2594 i2++;
2597 if (trt)
2599 c1 = XINT (trt[c1]);
2600 c2 = XINT (trt[c2]);
2602 if (c1 < c2)
2603 return make_number (- 1 - chars);
2604 if (c1 > c2)
2605 return make_number (chars + 1);
2607 chars++;
2610 /* The strings match as far as they go.
2611 If one is shorter, that one is less. */
2612 if (chars < endp1 - begp1)
2613 return make_number (chars + 1);
2614 else if (chars < endp2 - begp2)
2615 return make_number (- chars - 1);
2617 /* Same length too => they are equal. */
2618 return make_number (0);
2621 static Lisp_Object
2622 subst_char_in_region_unwind (arg)
2623 Lisp_Object arg;
2625 return current_buffer->undo_list = arg;
2628 static Lisp_Object
2629 subst_char_in_region_unwind_1 (arg)
2630 Lisp_Object arg;
2632 return current_buffer->filename = arg;
2635 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2636 Ssubst_char_in_region, 4, 5, 0,
2637 doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs.
2638 If optional arg NOUNDO is non-nil, don't record this change for undo
2639 and don't mark the buffer as really changed.
2640 Both characters must have the same length of multi-byte form. */)
2641 (start, end, fromchar, tochar, noundo)
2642 Lisp_Object start, end, fromchar, tochar, noundo;
2644 register int pos, pos_byte, stop, i, len, end_byte;
2645 int changed = 0;
2646 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2647 unsigned char *p;
2648 int count = SPECPDL_INDEX ();
2649 #define COMBINING_NO 0
2650 #define COMBINING_BEFORE 1
2651 #define COMBINING_AFTER 2
2652 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2653 int maybe_byte_combining = COMBINING_NO;
2654 int last_changed = 0;
2655 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2657 validate_region (&start, &end);
2658 CHECK_NUMBER (fromchar);
2659 CHECK_NUMBER (tochar);
2661 if (multibyte_p)
2663 len = CHAR_STRING (XFASTINT (fromchar), fromstr);
2664 if (CHAR_STRING (XFASTINT (tochar), tostr) != len)
2665 error ("Characters in subst-char-in-region have different byte-lengths");
2666 if (!ASCII_BYTE_P (*tostr))
2668 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2669 complete multibyte character, it may be combined with the
2670 after bytes. If it is in the range 0xA0..0xFF, it may be
2671 combined with the before and after bytes. */
2672 if (!CHAR_HEAD_P (*tostr))
2673 maybe_byte_combining = COMBINING_BOTH;
2674 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2675 maybe_byte_combining = COMBINING_AFTER;
2678 else
2680 len = 1;
2681 fromstr[0] = XFASTINT (fromchar);
2682 tostr[0] = XFASTINT (tochar);
2685 pos = XINT (start);
2686 pos_byte = CHAR_TO_BYTE (pos);
2687 stop = CHAR_TO_BYTE (XINT (end));
2688 end_byte = stop;
2690 /* If we don't want undo, turn off putting stuff on the list.
2691 That's faster than getting rid of things,
2692 and it prevents even the entry for a first change.
2693 Also inhibit locking the file. */
2694 if (!NILP (noundo))
2696 record_unwind_protect (subst_char_in_region_unwind,
2697 current_buffer->undo_list);
2698 current_buffer->undo_list = Qt;
2699 /* Don't do file-locking. */
2700 record_unwind_protect (subst_char_in_region_unwind_1,
2701 current_buffer->filename);
2702 current_buffer->filename = Qnil;
2705 if (pos_byte < GPT_BYTE)
2706 stop = min (stop, GPT_BYTE);
2707 while (1)
2709 int pos_byte_next = pos_byte;
2711 if (pos_byte >= stop)
2713 if (pos_byte >= end_byte) break;
2714 stop = end_byte;
2716 p = BYTE_POS_ADDR (pos_byte);
2717 if (multibyte_p)
2718 INC_POS (pos_byte_next);
2719 else
2720 ++pos_byte_next;
2721 if (pos_byte_next - pos_byte == len
2722 && p[0] == fromstr[0]
2723 && (len == 1
2724 || (p[1] == fromstr[1]
2725 && (len == 2 || (p[2] == fromstr[2]
2726 && (len == 3 || p[3] == fromstr[3]))))))
2728 if (! changed)
2730 changed = pos;
2731 modify_region (current_buffer, changed, XINT (end));
2733 if (! NILP (noundo))
2735 if (MODIFF - 1 == SAVE_MODIFF)
2736 SAVE_MODIFF++;
2737 if (MODIFF - 1 == current_buffer->auto_save_modified)
2738 current_buffer->auto_save_modified++;
2742 /* Take care of the case where the new character
2743 combines with neighboring bytes. */
2744 if (maybe_byte_combining
2745 && (maybe_byte_combining == COMBINING_AFTER
2746 ? (pos_byte_next < Z_BYTE
2747 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2748 : ((pos_byte_next < Z_BYTE
2749 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2750 || (pos_byte > BEG_BYTE
2751 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2753 Lisp_Object tem, string;
2755 struct gcpro gcpro1;
2757 tem = current_buffer->undo_list;
2758 GCPRO1 (tem);
2760 /* Make a multibyte string containing this single character. */
2761 string = make_multibyte_string (tostr, 1, len);
2762 /* replace_range is less efficient, because it moves the gap,
2763 but it handles combining correctly. */
2764 replace_range (pos, pos + 1, string,
2765 0, 0, 1);
2766 pos_byte_next = CHAR_TO_BYTE (pos);
2767 if (pos_byte_next > pos_byte)
2768 /* Before combining happened. We should not increment
2769 POS. So, to cancel the later increment of POS,
2770 decrease it now. */
2771 pos--;
2772 else
2773 INC_POS (pos_byte_next);
2775 if (! NILP (noundo))
2776 current_buffer->undo_list = tem;
2778 UNGCPRO;
2780 else
2782 if (NILP (noundo))
2783 record_change (pos, 1);
2784 for (i = 0; i < len; i++) *p++ = tostr[i];
2786 last_changed = pos + 1;
2788 pos_byte = pos_byte_next;
2789 pos++;
2792 if (changed)
2794 signal_after_change (changed,
2795 last_changed - changed, last_changed - changed);
2796 update_compositions (changed, last_changed, CHECK_ALL);
2799 unbind_to (count, Qnil);
2800 return Qnil;
2803 DEFUN ("translate-region-internal", Ftranslate_region_internal,
2804 Stranslate_region_internal, 3, 3, 0,
2805 doc: /* Internal use only.
2806 From START to END, translate characters according to TABLE.
2807 TABLE is a string; the Nth character in it is the mapping
2808 for the character with code N.
2809 It returns the number of characters changed. */)
2810 (start, end, table)
2811 Lisp_Object start;
2812 Lisp_Object end;
2813 register Lisp_Object table;
2815 register unsigned char *tt; /* Trans table. */
2816 register int nc; /* New character. */
2817 int cnt; /* Number of changes made. */
2818 int size; /* Size of translate table. */
2819 int pos, pos_byte, end_pos;
2820 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2821 int string_multibyte;
2823 validate_region (&start, &end);
2824 if (CHAR_TABLE_P (table))
2826 size = MAX_CHAR;
2827 tt = NULL;
2829 else
2831 CHECK_STRING (table);
2833 if (! multibyte && (SCHARS (table) < SBYTES (table)))
2834 table = string_make_unibyte (table);
2835 string_multibyte = SCHARS (table) < SBYTES (table);
2836 size = SCHARS (table);
2837 tt = SDATA (table);
2840 pos = XINT (start);
2841 pos_byte = CHAR_TO_BYTE (pos);
2842 end_pos = XINT (end);
2843 modify_region (current_buffer, pos, XINT (end));
2845 cnt = 0;
2846 for (; pos < end_pos; )
2848 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
2849 unsigned char *str, buf[MAX_MULTIBYTE_LENGTH];
2850 int len, str_len;
2851 int oc;
2853 if (multibyte)
2854 oc = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, len);
2855 else
2856 oc = *p, len = 1;
2857 if (oc < size)
2859 if (tt)
2861 if (string_multibyte)
2863 str = tt + string_char_to_byte (table, oc);
2864 nc = STRING_CHAR_AND_LENGTH (str, MAX_MULTIBYTE_LENGTH,
2865 str_len);
2867 else
2869 nc = tt[oc];
2870 if (! ASCII_BYTE_P (nc) && multibyte)
2872 str_len = CHAR_STRING (nc, buf);
2873 str = buf;
2875 else
2877 str_len = 1;
2878 str = tt + oc;
2882 else
2884 Lisp_Object val;
2885 int c;
2887 nc = oc;
2888 val = CHAR_TABLE_REF (table, oc);
2889 if (INTEGERP (val)
2890 && (c = XINT (val), CHAR_VALID_P (c, 0)))
2892 nc = c;
2893 str_len = CHAR_STRING (nc, buf);
2894 str = buf;
2898 if (nc != oc)
2900 if (len != str_len)
2902 Lisp_Object string;
2904 /* This is less efficient, because it moves the gap,
2905 but it should multibyte characters correctly. */
2906 string = make_multibyte_string (str, 1, str_len);
2907 replace_range (pos, pos + 1, string, 1, 0, 1);
2908 len = str_len;
2910 else
2912 record_change (pos, 1);
2913 while (str_len-- > 0)
2914 *p++ = *str++;
2915 signal_after_change (pos, 1, 1);
2916 update_compositions (pos, pos + 1, CHECK_BORDER);
2918 ++cnt;
2921 pos_byte += len;
2922 pos++;
2925 return make_number (cnt);
2928 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
2929 doc: /* Delete the text between point and mark.
2931 When called from a program, expects two arguments,
2932 positions (integers or markers) specifying the stretch to be deleted. */)
2933 (start, end)
2934 Lisp_Object start, end;
2936 validate_region (&start, &end);
2937 del_range (XINT (start), XINT (end));
2938 return Qnil;
2941 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
2942 Sdelete_and_extract_region, 2, 2, 0,
2943 doc: /* Delete the text between START and END and return it. */)
2944 (start, end)
2945 Lisp_Object start, end;
2947 validate_region (&start, &end);
2948 if (XINT (start) == XINT (end))
2949 return build_string ("");
2950 return del_range_1 (XINT (start), XINT (end), 1, 1);
2953 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
2954 doc: /* Remove restrictions (narrowing) from current buffer.
2955 This allows the buffer's full text to be seen and edited. */)
2958 if (BEG != BEGV || Z != ZV)
2959 current_buffer->clip_changed = 1;
2960 BEGV = BEG;
2961 BEGV_BYTE = BEG_BYTE;
2962 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
2963 /* Changing the buffer bounds invalidates any recorded current column. */
2964 invalidate_current_column ();
2965 return Qnil;
2968 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
2969 doc: /* Restrict editing in this buffer to the current region.
2970 The rest of the text becomes temporarily invisible and untouchable
2971 but is not deleted; if you save the buffer in a file, the invisible
2972 text is included in the file. \\[widen] makes all visible again.
2973 See also `save-restriction'.
2975 When calling from a program, pass two arguments; positions (integers
2976 or markers) bounding the text that should remain visible. */)
2977 (start, end)
2978 register Lisp_Object start, end;
2980 CHECK_NUMBER_COERCE_MARKER (start);
2981 CHECK_NUMBER_COERCE_MARKER (end);
2983 if (XINT (start) > XINT (end))
2985 Lisp_Object tem;
2986 tem = start; start = end; end = tem;
2989 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
2990 args_out_of_range (start, end);
2992 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
2993 current_buffer->clip_changed = 1;
2995 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2996 SET_BUF_ZV (current_buffer, XFASTINT (end));
2997 if (PT < XFASTINT (start))
2998 SET_PT (XFASTINT (start));
2999 if (PT > XFASTINT (end))
3000 SET_PT (XFASTINT (end));
3001 /* Changing the buffer bounds invalidates any recorded current column. */
3002 invalidate_current_column ();
3003 return Qnil;
3006 Lisp_Object
3007 save_restriction_save ()
3009 if (BEGV == BEG && ZV == Z)
3010 /* The common case that the buffer isn't narrowed.
3011 We return just the buffer object, which save_restriction_restore
3012 recognizes as meaning `no restriction'. */
3013 return Fcurrent_buffer ();
3014 else
3015 /* We have to save a restriction, so return a pair of markers, one
3016 for the beginning and one for the end. */
3018 Lisp_Object beg, end;
3020 beg = buildmark (BEGV, BEGV_BYTE);
3021 end = buildmark (ZV, ZV_BYTE);
3023 /* END must move forward if text is inserted at its exact location. */
3024 XMARKER(end)->insertion_type = 1;
3026 return Fcons (beg, end);
3030 Lisp_Object
3031 save_restriction_restore (data)
3032 Lisp_Object data;
3034 if (CONSP (data))
3035 /* A pair of marks bounding a saved restriction. */
3037 struct Lisp_Marker *beg = XMARKER (XCAR (data));
3038 struct Lisp_Marker *end = XMARKER (XCDR (data));
3039 struct buffer *buf = beg->buffer; /* END should have the same buffer. */
3041 if (buf /* Verify marker still points to a buffer. */
3042 && (beg->charpos != BUF_BEGV (buf) || end->charpos != BUF_ZV (buf)))
3043 /* The restriction has changed from the saved one, so restore
3044 the saved restriction. */
3046 int pt = BUF_PT (buf);
3048 SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
3049 SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
3051 if (pt < beg->charpos || pt > end->charpos)
3052 /* The point is outside the new visible range, move it inside. */
3053 SET_BUF_PT_BOTH (buf,
3054 clip_to_bounds (beg->charpos, pt, end->charpos),
3055 clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf),
3056 end->bytepos));
3058 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3061 else
3062 /* A buffer, which means that there was no old restriction. */
3064 struct buffer *buf = XBUFFER (data);
3066 if (buf /* Verify marker still points to a buffer. */
3067 && (BUF_BEGV (buf) != BUF_BEG (buf) || BUF_ZV (buf) != BUF_Z (buf)))
3068 /* The buffer has been narrowed, get rid of the narrowing. */
3070 SET_BUF_BEGV_BOTH (buf, BUF_BEG (buf), BUF_BEG_BYTE (buf));
3071 SET_BUF_ZV_BOTH (buf, BUF_Z (buf), BUF_Z_BYTE (buf));
3073 buf->clip_changed = 1; /* Remember that the narrowing changed. */
3077 return Qnil;
3080 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
3081 doc: /* Execute BODY, saving and restoring current buffer's restrictions.
3082 The buffer's restrictions make parts of the beginning and end invisible.
3083 (They are set up with `narrow-to-region' and eliminated with `widen'.)
3084 This special form, `save-restriction', saves the current buffer's restrictions
3085 when it is entered, and restores them when it is exited.
3086 So any `narrow-to-region' within BODY lasts only until the end of the form.
3087 The old restrictions settings are restored
3088 even in case of abnormal exit (throw or error).
3090 The value returned is the value of the last form in BODY.
3092 Note: if you are using both `save-excursion' and `save-restriction',
3093 use `save-excursion' outermost:
3094 (save-excursion (save-restriction ...))
3096 usage: (save-restriction &rest BODY) */)
3097 (body)
3098 Lisp_Object body;
3100 register Lisp_Object val;
3101 int count = SPECPDL_INDEX ();
3103 record_unwind_protect (save_restriction_restore, save_restriction_save ());
3104 val = Fprogn (body);
3105 return unbind_to (count, val);
3108 /* Buffer for the most recent text displayed by Fmessage_box. */
3109 static char *message_text;
3111 /* Allocated length of that buffer. */
3112 static int message_length;
3114 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
3115 doc: /* Print a one-line message at the bottom of the screen.
3116 The message also goes into the `*Messages*' buffer.
3117 \(In keyboard macros, that's all it does.)
3119 The first argument is a format control string, and the rest are data
3120 to be formatted under control of the string. See `format' for details.
3122 If the first argument is nil, the function clears any existing message;
3123 this lets the minibuffer contents show. See also `current-message'.
3125 usage: (message STRING &rest ARGS) */)
3126 (nargs, args)
3127 int nargs;
3128 Lisp_Object *args;
3130 if (NILP (args[0])
3131 || (STRINGP (args[0])
3132 && SBYTES (args[0]) == 0))
3134 message (0);
3135 return args[0];
3137 else
3139 register Lisp_Object val;
3140 val = Fformat (nargs, args);
3141 message3 (val, SBYTES (val), STRING_MULTIBYTE (val));
3142 return val;
3146 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
3147 doc: /* Display a message, in a dialog box if possible.
3148 If a dialog box is not available, use the echo area.
3149 The first argument is a format control string, and the rest are data
3150 to be formatted under control of the string. See `format' for details.
3152 If the first argument is nil, clear any existing message; let the
3153 minibuffer contents show.
3155 usage: (message-box STRING &rest ARGS) */)
3156 (nargs, args)
3157 int nargs;
3158 Lisp_Object *args;
3160 if (NILP (args[0]))
3162 message (0);
3163 return Qnil;
3165 else
3167 register Lisp_Object val;
3168 val = Fformat (nargs, args);
3169 #ifdef HAVE_MENUS
3170 /* The MS-DOS frames support popup menus even though they are
3171 not FRAME_WINDOW_P. */
3172 if (FRAME_WINDOW_P (XFRAME (selected_frame))
3173 || FRAME_MSDOS_P (XFRAME (selected_frame)))
3175 Lisp_Object pane, menu, obj;
3176 struct gcpro gcpro1;
3177 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
3178 GCPRO1 (pane);
3179 menu = Fcons (val, pane);
3180 obj = Fx_popup_dialog (Qt, menu);
3181 UNGCPRO;
3182 return val;
3184 #endif /* HAVE_MENUS */
3185 /* Copy the data so that it won't move when we GC. */
3186 if (! message_text)
3188 message_text = (char *)xmalloc (80);
3189 message_length = 80;
3191 if (SBYTES (val) > message_length)
3193 message_length = SBYTES (val);
3194 message_text = (char *)xrealloc (message_text, message_length);
3196 bcopy (SDATA (val), message_text, SBYTES (val));
3197 message2 (message_text, SBYTES (val),
3198 STRING_MULTIBYTE (val));
3199 return val;
3202 #ifdef HAVE_MENUS
3203 extern Lisp_Object last_nonmenu_event;
3204 #endif
3206 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
3207 doc: /* Display a message in a dialog box or in the echo area.
3208 If this command was invoked with the mouse, use a dialog box if
3209 `use-dialog-box' is non-nil.
3210 Otherwise, use the echo area.
3211 The first argument is a format control string, and the rest are data
3212 to be formatted under control of the string. See `format' for details.
3214 If the first argument is nil, clear any existing message; let the
3215 minibuffer contents show.
3217 usage: (message-or-box STRING &rest ARGS) */)
3218 (nargs, args)
3219 int nargs;
3220 Lisp_Object *args;
3222 #ifdef HAVE_MENUS
3223 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3224 && use_dialog_box)
3225 return Fmessage_box (nargs, args);
3226 #endif
3227 return Fmessage (nargs, args);
3230 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
3231 doc: /* Return the string currently displayed in the echo area, or nil if none. */)
3234 return current_message ();
3238 DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0,
3239 doc: /* Return a copy of STRING with text properties added.
3240 First argument is the string to copy.
3241 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
3242 properties to add to the result.
3243 usage: (propertize STRING &rest PROPERTIES) */)
3244 (nargs, args)
3245 int nargs;
3246 Lisp_Object *args;
3248 Lisp_Object properties, string;
3249 struct gcpro gcpro1, gcpro2;
3250 int i;
3252 /* Number of args must be odd. */
3253 if ((nargs & 1) == 0 || nargs < 1)
3254 error ("Wrong number of arguments");
3256 properties = string = Qnil;
3257 GCPRO2 (properties, string);
3259 /* First argument must be a string. */
3260 CHECK_STRING (args[0]);
3261 string = Fcopy_sequence (args[0]);
3263 for (i = 1; i < nargs; i += 2)
3265 CHECK_SYMBOL (args[i]);
3266 properties = Fcons (args[i], Fcons (args[i + 1], properties));
3269 Fadd_text_properties (make_number (0),
3270 make_number (SCHARS (string)),
3271 properties, string);
3272 RETURN_UNGCPRO (string);
3276 /* Number of bytes that STRING will occupy when put into the result.
3277 MULTIBYTE is nonzero if the result should be multibyte. */
3279 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
3280 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
3281 ? count_size_as_multibyte (SDATA (STRING), SBYTES (STRING)) \
3282 : SBYTES (STRING))
3284 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3285 doc: /* Format a string out of a control-string and arguments.
3286 The first argument is a control string.
3287 The other arguments are substituted into it to make the result, a string.
3288 It may contain %-sequences meaning to substitute the next argument.
3289 %s means print a string argument. Actually, prints any object, with `princ'.
3290 %d means print as number in decimal (%o octal, %x hex).
3291 %X is like %x, but uses upper case.
3292 %e means print a number in exponential notation.
3293 %f means print a number in decimal-point notation.
3294 %g means print a number in exponential notation
3295 or decimal-point notation, whichever uses fewer characters.
3296 %c means print a number as a single character.
3297 %S means print any object as an s-expression (using `prin1').
3298 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.
3299 Use %% to put a single % into the output.
3301 The basic structure of a %-sequence is
3302 % <flags> <width> <precision> character
3303 where flags is [- #0]+, width is [0-9]+, and precision is .[0-9]+
3305 usage: (format STRING &rest OBJECTS) */)
3306 (nargs, args)
3307 int nargs;
3308 register Lisp_Object *args;
3310 register int n; /* The number of the next arg to substitute */
3311 register int total; /* An estimate of the final length */
3312 char *buf, *p;
3313 register unsigned char *format, *end, *format_start;
3314 int nchars;
3315 /* Nonzero if the output should be a multibyte string,
3316 which is true if any of the inputs is one. */
3317 int multibyte = 0;
3318 /* When we make a multibyte string, we must pay attention to the
3319 byte combining problem, i.e., a byte may be combined with a
3320 multibyte charcter of the previous string. This flag tells if we
3321 must consider such a situation or not. */
3322 int maybe_combine_byte;
3323 unsigned char *this_format;
3324 /* Precision for each spec, or -1, a flag value meaning no precision
3325 was given in that spec. Element 0, corresonding to the format
3326 string itself, will not be used. Element NARGS, corresponding to
3327 no argument, *will* be assigned to in the case that a `%' and `.'
3328 occur after the final format specifier. */
3329 int *precision = (int *) (alloca((nargs + 1) * sizeof (int)));
3330 int longest_format;
3331 Lisp_Object val;
3332 int arg_intervals = 0;
3333 USE_SAFE_ALLOCA;
3335 /* discarded[I] is 1 if byte I of the format
3336 string was not copied into the output.
3337 It is 2 if byte I was not the first byte of its character. */
3338 char *discarded = 0;
3340 /* Each element records, for one argument,
3341 the start and end bytepos in the output string,
3342 and whether the argument is a string with intervals.
3343 info[0] is unused. Unused elements have -1 for start. */
3344 struct info
3346 int start, end, intervals;
3347 } *info = 0;
3349 /* It should not be necessary to GCPRO ARGS, because
3350 the caller in the interpreter should take care of that. */
3352 /* Try to determine whether the result should be multibyte.
3353 This is not always right; sometimes the result needs to be multibyte
3354 because of an object that we will pass through prin1,
3355 and in that case, we won't know it here. */
3356 for (n = 0; n < nargs; n++)
3358 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3359 multibyte = 1;
3360 /* Piggyback on this loop to initialize precision[N]. */
3361 precision[n] = -1;
3363 precision[nargs] = -1;
3365 CHECK_STRING (args[0]);
3366 /* We may have to change "%S" to "%s". */
3367 args[0] = Fcopy_sequence (args[0]);
3369 /* GC should never happen here, so abort if it does. */
3370 abort_on_gc++;
3372 /* If we start out planning a unibyte result,
3373 then discover it has to be multibyte, we jump back to retry.
3374 That can only happen from the first large while loop below. */
3375 retry:
3377 format = SDATA (args[0]);
3378 format_start = format;
3379 end = format + SBYTES (args[0]);
3380 longest_format = 0;
3382 /* Make room in result for all the non-%-codes in the control string. */
3383 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]) + 1;
3385 /* Allocate the info and discarded tables. */
3387 int nbytes = (nargs+1) * sizeof *info;
3388 int i;
3389 if (!info)
3390 info = (struct info *) alloca (nbytes);
3391 bzero (info, nbytes);
3392 for (i = 0; i <= nargs; i++)
3393 info[i].start = -1;
3394 if (!discarded)
3395 SAFE_ALLOCA (discarded, char *, SBYTES (args[0]));
3396 bzero (discarded, SBYTES (args[0]));
3399 /* Add to TOTAL enough space to hold the converted arguments. */
3401 n = 0;
3402 while (format != end)
3403 if (*format++ == '%')
3405 int thissize = 0;
3406 int actual_width = 0;
3407 unsigned char *this_format_start = format - 1;
3408 int field_width = 0;
3410 /* General format specifications look like
3412 '%' [flags] [field-width] [precision] format
3414 where
3416 flags ::= [- #0]+
3417 field-width ::= [0-9]+
3418 precision ::= '.' [0-9]*
3420 If a field-width is specified, it specifies to which width
3421 the output should be padded with blanks, iff the output
3422 string is shorter than field-width.
3424 If precision is specified, it specifies the number of
3425 digits to print after the '.' for floats, or the max.
3426 number of chars to print from a string. */
3428 while (index ("-0# ", *format))
3429 ++format;
3431 if (*format >= '0' && *format <= '9')
3433 for (field_width = 0; *format >= '0' && *format <= '9'; ++format)
3434 field_width = 10 * field_width + *format - '0';
3437 /* N is not incremented for another few lines below, so refer to
3438 element N+1 (which might be precision[NARGS]). */
3439 if (*format == '.')
3441 ++format;
3442 for (precision[n+1] = 0; *format >= '0' && *format <= '9'; ++format)
3443 precision[n+1] = 10 * precision[n+1] + *format - '0';
3446 if (format - this_format_start + 1 > longest_format)
3447 longest_format = format - this_format_start + 1;
3449 if (format == end)
3450 error ("Format string ends in middle of format specifier");
3451 if (*format == '%')
3452 format++;
3453 else if (++n >= nargs)
3454 error ("Not enough arguments for format string");
3455 else if (*format == 'S')
3457 /* For `S', prin1 the argument and then treat like a string. */
3458 register Lisp_Object tem;
3459 tem = Fprin1_to_string (args[n], Qnil);
3460 if (STRING_MULTIBYTE (tem) && ! multibyte)
3462 multibyte = 1;
3463 goto retry;
3465 args[n] = tem;
3466 /* If we restart the loop, we should not come here again
3467 because args[n] is now a string and calling
3468 Fprin1_to_string on it produces superflous double
3469 quotes. So, change "%S" to "%s" now. */
3470 *format = 's';
3471 goto string;
3473 else if (SYMBOLP (args[n]))
3475 args[n] = SYMBOL_NAME (args[n]);
3476 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3478 multibyte = 1;
3479 goto retry;
3481 goto string;
3483 else if (STRINGP (args[n]))
3485 string:
3486 if (*format != 's' && *format != 'S')
3487 error ("Format specifier doesn't match argument type");
3488 /* In the case (PRECISION[N] > 0), THISSIZE may not need
3489 to be as large as is calculated here. Easy check for
3490 the case PRECISION = 0. */
3491 thissize = precision[n] ? CONVERTED_BYTE_SIZE (multibyte, args[n]) : 0;
3492 actual_width = lisp_string_width (args[n], -1, NULL, NULL);
3494 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
3495 else if (INTEGERP (args[n]) && *format != 's')
3497 /* The following loop assumes the Lisp type indicates
3498 the proper way to pass the argument.
3499 So make sure we have a flonum if the argument should
3500 be a double. */
3501 if (*format == 'e' || *format == 'f' || *format == 'g')
3502 args[n] = Ffloat (args[n]);
3503 else
3504 if (*format != 'd' && *format != 'o' && *format != 'x'
3505 && *format != 'i' && *format != 'X' && *format != 'c')
3506 error ("Invalid format operation %%%c", *format);
3508 thissize = 30;
3509 if (*format == 'c')
3511 if (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
3512 /* Note: No one can remember why we have to treat
3513 the character 0 as a multibyte character here.
3514 But, until it causes a real problem, let's
3515 don't change it. */
3516 || XINT (args[n]) == 0)
3518 if (! multibyte)
3520 multibyte = 1;
3521 goto retry;
3523 args[n] = Fchar_to_string (args[n]);
3524 thissize = SBYTES (args[n]);
3526 else if (! ASCII_BYTE_P (XINT (args[n])) && multibyte)
3528 args[n]
3529 = Fchar_to_string (Funibyte_char_to_multibyte (args[n]));
3530 thissize = SBYTES (args[n]);
3534 else if (FLOATP (args[n]) && *format != 's')
3536 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
3538 if (*format != 'd' && *format != 'o' && *format != 'x'
3539 && *format != 'i' && *format != 'X' && *format != 'c')
3540 error ("Invalid format operation %%%c", *format);
3541 args[n] = Ftruncate (args[n], Qnil);
3544 /* Note that we're using sprintf to print floats,
3545 so we have to take into account what that function
3546 prints. */
3547 /* Filter out flag value of -1. */
3548 thissize = (MAX_10_EXP + 100
3549 + (precision[n] > 0 ? precision[n] : 0));
3551 else
3553 /* Anything but a string, convert to a string using princ. */
3554 register Lisp_Object tem;
3555 tem = Fprin1_to_string (args[n], Qt);
3556 if (STRING_MULTIBYTE (tem) && ! multibyte)
3558 multibyte = 1;
3559 goto retry;
3561 args[n] = tem;
3562 goto string;
3565 thissize += max (0, field_width - actual_width);
3566 total += thissize + 4;
3569 abort_on_gc--;
3571 /* Now we can no longer jump to retry.
3572 TOTAL and LONGEST_FORMAT are known for certain. */
3574 this_format = (unsigned char *) alloca (longest_format + 1);
3576 /* Allocate the space for the result.
3577 Note that TOTAL is an overestimate. */
3578 SAFE_ALLOCA (buf, char *, total);
3580 p = buf;
3581 nchars = 0;
3582 n = 0;
3584 /* Scan the format and store result in BUF. */
3585 format = SDATA (args[0]);
3586 format_start = format;
3587 end = format + SBYTES (args[0]);
3588 maybe_combine_byte = 0;
3589 while (format != end)
3591 if (*format == '%')
3593 int minlen;
3594 int negative = 0;
3595 unsigned char *this_format_start = format;
3597 discarded[format - format_start] = 1;
3598 format++;
3600 while (index("-0# ", *format))
3602 if (*format == '-')
3604 negative = 1;
3606 discarded[format - format_start] = 1;
3607 ++format;
3610 minlen = atoi (format);
3612 while ((*format >= '0' && *format <= '9') || *format == '.')
3614 discarded[format - format_start] = 1;
3615 format++;
3618 if (*format++ == '%')
3620 *p++ = '%';
3621 nchars++;
3622 continue;
3625 ++n;
3627 discarded[format - format_start - 1] = 1;
3628 info[n].start = nchars;
3630 if (STRINGP (args[n]))
3632 /* handle case (precision[n] >= 0) */
3634 int width, padding;
3635 int nbytes, start, end;
3636 int nchars_string;
3638 /* lisp_string_width ignores a precision of 0, but GNU
3639 libc functions print 0 characters when the precision
3640 is 0. Imitate libc behavior here. Changing
3641 lisp_string_width is the right thing, and will be
3642 done, but meanwhile we work with it. */
3644 if (precision[n] == 0)
3645 width = nchars_string = nbytes = 0;
3646 else if (precision[n] > 0)
3647 width = lisp_string_width (args[n], precision[n], &nchars_string, &nbytes);
3648 else
3649 { /* no precision spec given for this argument */
3650 width = lisp_string_width (args[n], -1, NULL, NULL);
3651 nbytes = SBYTES (args[n]);
3652 nchars_string = SCHARS (args[n]);
3655 /* If spec requires it, pad on right with spaces. */
3656 padding = minlen - width;
3657 if (! negative)
3658 while (padding-- > 0)
3660 *p++ = ' ';
3661 ++nchars;
3664 start = nchars;
3665 nchars += nchars_string;
3666 end = nchars;
3668 if (p > buf
3669 && multibyte
3670 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3671 && STRING_MULTIBYTE (args[n])
3672 && !CHAR_HEAD_P (SREF (args[n], 0)))
3673 maybe_combine_byte = 1;
3675 p += copy_text (SDATA (args[n]), p,
3676 nbytes,
3677 STRING_MULTIBYTE (args[n]), multibyte);
3679 if (negative)
3680 while (padding-- > 0)
3682 *p++ = ' ';
3683 nchars++;
3686 /* If this argument has text properties, record where
3687 in the result string it appears. */
3688 if (STRING_INTERVALS (args[n]))
3689 info[n].intervals = arg_intervals = 1;
3691 else if (INTEGERP (args[n]) || FLOATP (args[n]))
3693 int this_nchars;
3695 bcopy (this_format_start, this_format,
3696 format - this_format_start);
3697 this_format[format - this_format_start] = 0;
3699 if (INTEGERP (args[n]))
3700 sprintf (p, this_format, XINT (args[n]));
3701 else
3702 sprintf (p, this_format, XFLOAT_DATA (args[n]));
3704 if (p > buf
3705 && multibyte
3706 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3707 && !CHAR_HEAD_P (*((unsigned char *) p)))
3708 maybe_combine_byte = 1;
3709 this_nchars = strlen (p);
3710 if (multibyte)
3711 p += str_to_multibyte (p, buf + total - 1 - p, this_nchars);
3712 else
3713 p += this_nchars;
3714 nchars += this_nchars;
3717 info[n].end = nchars;
3719 else if (STRING_MULTIBYTE (args[0]))
3721 /* Copy a whole multibyte character. */
3722 if (p > buf
3723 && multibyte
3724 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3725 && !CHAR_HEAD_P (*format))
3726 maybe_combine_byte = 1;
3727 *p++ = *format++;
3728 while (! CHAR_HEAD_P (*format))
3730 discarded[format - format_start] = 2;
3731 *p++ = *format++;
3733 nchars++;
3735 else if (multibyte)
3737 /* Convert a single-byte character to multibyte. */
3738 int len = copy_text (format, p, 1, 0, 1);
3740 p += len;
3741 format++;
3742 nchars++;
3744 else
3745 *p++ = *format++, nchars++;
3748 if (p > buf + total)
3749 abort ();
3751 if (maybe_combine_byte)
3752 nchars = multibyte_chars_in_text (buf, p - buf);
3753 val = make_specified_string (buf, nchars, p - buf, multibyte);
3755 /* If we allocated BUF with malloc, free it too. */
3756 SAFE_FREE ();
3758 /* If the format string has text properties, or any of the string
3759 arguments has text properties, set up text properties of the
3760 result string. */
3762 if (STRING_INTERVALS (args[0]) || arg_intervals)
3764 Lisp_Object len, new_len, props;
3765 struct gcpro gcpro1;
3767 /* Add text properties from the format string. */
3768 len = make_number (SCHARS (args[0]));
3769 props = text_property_list (args[0], make_number (0), len, Qnil);
3770 GCPRO1 (props);
3772 if (CONSP (props))
3774 int bytepos = 0, position = 0, translated = 0, argn = 1;
3775 Lisp_Object list;
3777 /* Adjust the bounds of each text property
3778 to the proper start and end in the output string. */
3780 /* Put the positions in PROPS in increasing order, so that
3781 we can do (effectively) one scan through the position
3782 space of the format string. */
3783 props = Fnreverse (props);
3785 /* BYTEPOS is the byte position in the format string,
3786 POSITION is the untranslated char position in it,
3787 TRANSLATED is the translated char position in BUF,
3788 and ARGN is the number of the next arg we will come to. */
3789 for (list = props; CONSP (list); list = XCDR (list))
3791 Lisp_Object item;
3792 int pos;
3794 item = XCAR (list);
3796 /* First adjust the property start position. */
3797 pos = XINT (XCAR (item));
3799 /* Advance BYTEPOS, POSITION, TRANSLATED and ARGN
3800 up to this position. */
3801 for (; position < pos; bytepos++)
3803 if (! discarded[bytepos])
3804 position++, translated++;
3805 else if (discarded[bytepos] == 1)
3807 position++;
3808 if (translated == info[argn].start)
3810 translated += info[argn].end - info[argn].start;
3811 argn++;
3816 XSETCAR (item, make_number (translated));
3818 /* Likewise adjust the property end position. */
3819 pos = XINT (XCAR (XCDR (item)));
3821 for (; bytepos < pos; bytepos++)
3823 if (! discarded[bytepos])
3824 position++, translated++;
3825 else if (discarded[bytepos] == 1)
3827 position++;
3828 if (translated == info[argn].start)
3830 translated += info[argn].end - info[argn].start;
3831 argn++;
3836 XSETCAR (XCDR (item), make_number (translated));
3839 add_text_properties_from_list (val, props, make_number (0));
3842 /* Add text properties from arguments. */
3843 if (arg_intervals)
3844 for (n = 1; n < nargs; ++n)
3845 if (info[n].intervals)
3847 len = make_number (SCHARS (args[n]));
3848 new_len = make_number (info[n].end - info[n].start);
3849 props = text_property_list (args[n], make_number (0), len, Qnil);
3850 extend_property_ranges (props, len, new_len);
3851 /* If successive arguments have properites, be sure that
3852 the value of `composition' property be the copy. */
3853 if (n > 1 && info[n - 1].end)
3854 make_composition_value_copy (props);
3855 add_text_properties_from_list (val, props,
3856 make_number (info[n].start));
3859 UNGCPRO;
3862 return val;
3865 Lisp_Object
3866 format2 (string1, arg0, arg1)
3867 char *string1;
3868 Lisp_Object arg0, arg1;
3870 Lisp_Object args[3];
3871 args[0] = build_string (string1);
3872 args[1] = arg0;
3873 args[2] = arg1;
3874 return Fformat (3, args);
3877 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
3878 doc: /* Return t if two characters match, optionally ignoring case.
3879 Both arguments must be characters (i.e. integers).
3880 Case is ignored if `case-fold-search' is non-nil in the current buffer. */)
3881 (c1, c2)
3882 register Lisp_Object c1, c2;
3884 int i1, i2;
3885 CHECK_NUMBER (c1);
3886 CHECK_NUMBER (c2);
3888 if (XINT (c1) == XINT (c2))
3889 return Qt;
3890 if (NILP (current_buffer->case_fold_search))
3891 return Qnil;
3893 /* Do these in separate statements,
3894 then compare the variables.
3895 because of the way DOWNCASE uses temp variables. */
3896 i1 = DOWNCASE (XFASTINT (c1));
3897 i2 = DOWNCASE (XFASTINT (c2));
3898 return (i1 == i2 ? Qt : Qnil);
3901 /* Transpose the markers in two regions of the current buffer, and
3902 adjust the ones between them if necessary (i.e.: if the regions
3903 differ in size).
3905 START1, END1 are the character positions of the first region.
3906 START1_BYTE, END1_BYTE are the byte positions.
3907 START2, END2 are the character positions of the second region.
3908 START2_BYTE, END2_BYTE are the byte positions.
3910 Traverses the entire marker list of the buffer to do so, adding an
3911 appropriate amount to some, subtracting from some, and leaving the
3912 rest untouched. Most of this is copied from adjust_markers in insdel.c.
3914 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
3916 static void
3917 transpose_markers (start1, end1, start2, end2,
3918 start1_byte, end1_byte, start2_byte, end2_byte)
3919 register int start1, end1, start2, end2;
3920 register int start1_byte, end1_byte, start2_byte, end2_byte;
3922 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
3923 register struct Lisp_Marker *marker;
3925 /* Update point as if it were a marker. */
3926 if (PT < start1)
3928 else if (PT < end1)
3929 TEMP_SET_PT_BOTH (PT + (end2 - end1),
3930 PT_BYTE + (end2_byte - end1_byte));
3931 else if (PT < start2)
3932 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
3933 (PT_BYTE + (end2_byte - start2_byte)
3934 - (end1_byte - start1_byte)));
3935 else if (PT < end2)
3936 TEMP_SET_PT_BOTH (PT - (start2 - start1),
3937 PT_BYTE - (start2_byte - start1_byte));
3939 /* We used to adjust the endpoints here to account for the gap, but that
3940 isn't good enough. Even if we assume the caller has tried to move the
3941 gap out of our way, it might still be at start1 exactly, for example;
3942 and that places it `inside' the interval, for our purposes. The amount
3943 of adjustment is nontrivial if there's a `denormalized' marker whose
3944 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
3945 the dirty work to Fmarker_position, below. */
3947 /* The difference between the region's lengths */
3948 diff = (end2 - start2) - (end1 - start1);
3949 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
3951 /* For shifting each marker in a region by the length of the other
3952 region plus the distance between the regions. */
3953 amt1 = (end2 - start2) + (start2 - end1);
3954 amt2 = (end1 - start1) + (start2 - end1);
3955 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
3956 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
3958 for (marker = BUF_MARKERS (current_buffer); marker; marker = marker->next)
3960 mpos = marker->bytepos;
3961 if (mpos >= start1_byte && mpos < end2_byte)
3963 if (mpos < end1_byte)
3964 mpos += amt1_byte;
3965 else if (mpos < start2_byte)
3966 mpos += diff_byte;
3967 else
3968 mpos -= amt2_byte;
3969 marker->bytepos = mpos;
3971 mpos = marker->charpos;
3972 if (mpos >= start1 && mpos < end2)
3974 if (mpos < end1)
3975 mpos += amt1;
3976 else if (mpos < start2)
3977 mpos += diff;
3978 else
3979 mpos -= amt2;
3981 marker->charpos = mpos;
3985 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
3986 doc: /* Transpose region STARTR1 to ENDR1 with STARTR2 to ENDR2.
3987 The regions may not be overlapping, because the size of the buffer is
3988 never changed in a transposition.
3990 Optional fifth arg LEAVE-MARKERS, if non-nil, means don't update
3991 any markers that happen to be located in the regions.
3993 Transposing beyond buffer boundaries is an error. */)
3994 (startr1, endr1, startr2, endr2, leave_markers)
3995 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
3997 register int start1, end1, start2, end2;
3998 int start1_byte, start2_byte, len1_byte, len2_byte;
3999 int gap, len1, len_mid, len2;
4000 unsigned char *start1_addr, *start2_addr, *temp;
4002 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
4003 cur_intv = BUF_INTERVALS (current_buffer);
4005 validate_region (&startr1, &endr1);
4006 validate_region (&startr2, &endr2);
4008 start1 = XFASTINT (startr1);
4009 end1 = XFASTINT (endr1);
4010 start2 = XFASTINT (startr2);
4011 end2 = XFASTINT (endr2);
4012 gap = GPT;
4014 /* Swap the regions if they're reversed. */
4015 if (start2 < end1)
4017 register int glumph = start1;
4018 start1 = start2;
4019 start2 = glumph;
4020 glumph = end1;
4021 end1 = end2;
4022 end2 = glumph;
4025 len1 = end1 - start1;
4026 len2 = end2 - start2;
4028 if (start2 < end1)
4029 error ("Transposed regions overlap");
4030 else if (start1 == end1 || start2 == end2)
4031 error ("Transposed region has length 0");
4033 /* The possibilities are:
4034 1. Adjacent (contiguous) regions, or separate but equal regions
4035 (no, really equal, in this case!), or
4036 2. Separate regions of unequal size.
4038 The worst case is usually No. 2. It means that (aside from
4039 potential need for getting the gap out of the way), there also
4040 needs to be a shifting of the text between the two regions. So
4041 if they are spread far apart, we are that much slower... sigh. */
4043 /* It must be pointed out that the really studly thing to do would
4044 be not to move the gap at all, but to leave it in place and work
4045 around it if necessary. This would be extremely efficient,
4046 especially considering that people are likely to do
4047 transpositions near where they are working interactively, which
4048 is exactly where the gap would be found. However, such code
4049 would be much harder to write and to read. So, if you are
4050 reading this comment and are feeling squirrely, by all means have
4051 a go! I just didn't feel like doing it, so I will simply move
4052 the gap the minimum distance to get it out of the way, and then
4053 deal with an unbroken array. */
4055 /* Make sure the gap won't interfere, by moving it out of the text
4056 we will operate on. */
4057 if (start1 < gap && gap < end2)
4059 if (gap - start1 < end2 - gap)
4060 move_gap (start1);
4061 else
4062 move_gap (end2);
4065 start1_byte = CHAR_TO_BYTE (start1);
4066 start2_byte = CHAR_TO_BYTE (start2);
4067 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
4068 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
4070 #ifdef BYTE_COMBINING_DEBUG
4071 if (end1 == start2)
4073 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4074 len2_byte, start1, start1_byte)
4075 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4076 len1_byte, end2, start2_byte + len2_byte)
4077 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4078 len1_byte, end2, start2_byte + len2_byte))
4079 abort ();
4081 else
4083 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
4084 len2_byte, start1, start1_byte)
4085 || count_combining_before (BYTE_POS_ADDR (start1_byte),
4086 len1_byte, start2, start2_byte)
4087 || count_combining_after (BYTE_POS_ADDR (start2_byte),
4088 len2_byte, end1, start1_byte + len1_byte)
4089 || count_combining_after (BYTE_POS_ADDR (start1_byte),
4090 len1_byte, end2, start2_byte + len2_byte))
4091 abort ();
4093 #endif
4095 /* Hmmm... how about checking to see if the gap is large
4096 enough to use as the temporary storage? That would avoid an
4097 allocation... interesting. Later, don't fool with it now. */
4099 /* Working without memmove, for portability (sigh), so must be
4100 careful of overlapping subsections of the array... */
4102 if (end1 == start2) /* adjacent regions */
4104 modify_region (current_buffer, start1, end2);
4105 record_change (start1, len1 + len2);
4107 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4108 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4109 Fset_text_properties (make_number (start1), make_number (end2),
4110 Qnil, Qnil);
4112 /* First region smaller than second. */
4113 if (len1_byte < len2_byte)
4115 USE_SAFE_ALLOCA;
4117 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4119 /* Don't precompute these addresses. We have to compute them
4120 at the last minute, because the relocating allocator might
4121 have moved the buffer around during the xmalloc. */
4122 start1_addr = BYTE_POS_ADDR (start1_byte);
4123 start2_addr = BYTE_POS_ADDR (start2_byte);
4125 bcopy (start2_addr, temp, len2_byte);
4126 bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
4127 bcopy (temp, start1_addr, len2_byte);
4128 SAFE_FREE ();
4130 else
4131 /* First region not smaller than second. */
4133 USE_SAFE_ALLOCA;
4135 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4136 start1_addr = BYTE_POS_ADDR (start1_byte);
4137 start2_addr = BYTE_POS_ADDR (start2_byte);
4138 bcopy (start1_addr, temp, len1_byte);
4139 bcopy (start2_addr, start1_addr, len2_byte);
4140 bcopy (temp, start1_addr + len2_byte, len1_byte);
4141 SAFE_FREE ();
4143 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
4144 len1, current_buffer, 0);
4145 graft_intervals_into_buffer (tmp_interval2, start1,
4146 len2, current_buffer, 0);
4147 update_compositions (start1, start1 + len2, CHECK_BORDER);
4148 update_compositions (start1 + len2, end2, CHECK_TAIL);
4150 /* Non-adjacent regions, because end1 != start2, bleagh... */
4151 else
4153 len_mid = start2_byte - (start1_byte + len1_byte);
4155 if (len1_byte == len2_byte)
4156 /* Regions are same size, though, how nice. */
4158 USE_SAFE_ALLOCA;
4160 modify_region (current_buffer, start1, end1);
4161 modify_region (current_buffer, start2, end2);
4162 record_change (start1, len1);
4163 record_change (start2, len2);
4164 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4165 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4166 Fset_text_properties (make_number (start1), make_number (end1),
4167 Qnil, Qnil);
4168 Fset_text_properties (make_number (start2), make_number (end2),
4169 Qnil, Qnil);
4171 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4172 start1_addr = BYTE_POS_ADDR (start1_byte);
4173 start2_addr = BYTE_POS_ADDR (start2_byte);
4174 bcopy (start1_addr, temp, len1_byte);
4175 bcopy (start2_addr, start1_addr, len2_byte);
4176 bcopy (temp, start2_addr, len1_byte);
4177 SAFE_FREE ();
4179 graft_intervals_into_buffer (tmp_interval1, start2,
4180 len1, current_buffer, 0);
4181 graft_intervals_into_buffer (tmp_interval2, start1,
4182 len2, current_buffer, 0);
4185 else if (len1_byte < len2_byte) /* Second region larger than first */
4186 /* Non-adjacent & unequal size, area between must also be shifted. */
4188 USE_SAFE_ALLOCA;
4190 modify_region (current_buffer, start1, end2);
4191 record_change (start1, (end2 - start1));
4192 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4193 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4194 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4195 Fset_text_properties (make_number (start1), make_number (end2),
4196 Qnil, Qnil);
4198 /* holds region 2 */
4199 SAFE_ALLOCA (temp, unsigned char *, len2_byte);
4200 start1_addr = BYTE_POS_ADDR (start1_byte);
4201 start2_addr = BYTE_POS_ADDR (start2_byte);
4202 bcopy (start2_addr, temp, len2_byte);
4203 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
4204 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
4205 bcopy (temp, start1_addr, len2_byte);
4206 SAFE_FREE ();
4208 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4209 len1, current_buffer, 0);
4210 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4211 len_mid, current_buffer, 0);
4212 graft_intervals_into_buffer (tmp_interval2, start1,
4213 len2, current_buffer, 0);
4215 else
4216 /* Second region smaller than first. */
4218 USE_SAFE_ALLOCA;
4220 record_change (start1, (end2 - start1));
4221 modify_region (current_buffer, start1, end2);
4223 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
4224 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
4225 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
4226 Fset_text_properties (make_number (start1), make_number (end2),
4227 Qnil, Qnil);
4229 /* holds region 1 */
4230 SAFE_ALLOCA (temp, unsigned char *, len1_byte);
4231 start1_addr = BYTE_POS_ADDR (start1_byte);
4232 start2_addr = BYTE_POS_ADDR (start2_byte);
4233 bcopy (start1_addr, temp, len1_byte);
4234 bcopy (start2_addr, start1_addr, len2_byte);
4235 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
4236 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte);
4237 SAFE_FREE ();
4239 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
4240 len1, current_buffer, 0);
4241 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
4242 len_mid, current_buffer, 0);
4243 graft_intervals_into_buffer (tmp_interval2, start1,
4244 len2, current_buffer, 0);
4247 update_compositions (start1, start1 + len2, CHECK_BORDER);
4248 update_compositions (end2 - len1, end2, CHECK_BORDER);
4251 /* When doing multiple transpositions, it might be nice
4252 to optimize this. Perhaps the markers in any one buffer
4253 should be organized in some sorted data tree. */
4254 if (NILP (leave_markers))
4256 transpose_markers (start1, end1, start2, end2,
4257 start1_byte, start1_byte + len1_byte,
4258 start2_byte, start2_byte + len2_byte);
4259 fix_start_end_in_overlays (start1, end2);
4262 return Qnil;
4266 void
4267 syms_of_editfns ()
4269 environbuf = 0;
4271 Qbuffer_access_fontify_functions
4272 = intern ("buffer-access-fontify-functions");
4273 staticpro (&Qbuffer_access_fontify_functions);
4275 DEFVAR_LISP ("inhibit-field-text-motion", &Vinhibit_field_text_motion,
4276 doc: /* Non-nil means text motion commands don't notice fields. */);
4277 Vinhibit_field_text_motion = Qnil;
4279 DEFVAR_LISP ("buffer-access-fontify-functions",
4280 &Vbuffer_access_fontify_functions,
4281 doc: /* List of functions called by `buffer-substring' to fontify if necessary.
4282 Each function is called with two arguments which specify the range
4283 of the buffer being accessed. */);
4284 Vbuffer_access_fontify_functions = Qnil;
4287 Lisp_Object obuf;
4288 extern Lisp_Object Vprin1_to_string_buffer;
4289 obuf = Fcurrent_buffer ();
4290 /* Do this here, because init_buffer_once is too early--it won't work. */
4291 Fset_buffer (Vprin1_to_string_buffer);
4292 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
4293 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
4294 Qnil);
4295 Fset_buffer (obuf);
4298 DEFVAR_LISP ("buffer-access-fontified-property",
4299 &Vbuffer_access_fontified_property,
4300 doc: /* Property which (if non-nil) indicates text has been fontified.
4301 `buffer-substring' need not call the `buffer-access-fontify-functions'
4302 functions if all the text being accessed has this property. */);
4303 Vbuffer_access_fontified_property = Qnil;
4305 DEFVAR_LISP ("system-name", &Vsystem_name,
4306 doc: /* The name of the machine Emacs is running on. */);
4308 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
4309 doc: /* The full name of the user logged in. */);
4311 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
4312 doc: /* The user's name, taken from environment variables if possible. */);
4314 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
4315 doc: /* The user's name, based upon the real uid only. */);
4317 DEFVAR_LISP ("operating-system-release", &Voperating_system_release,
4318 doc: /* The release of the operating system Emacs is running on. */);
4320 defsubr (&Spropertize);
4321 defsubr (&Schar_equal);
4322 defsubr (&Sgoto_char);
4323 defsubr (&Sstring_to_char);
4324 defsubr (&Schar_to_string);
4325 defsubr (&Sbuffer_substring);
4326 defsubr (&Sbuffer_substring_no_properties);
4327 defsubr (&Sbuffer_string);
4329 defsubr (&Spoint_marker);
4330 defsubr (&Smark_marker);
4331 defsubr (&Spoint);
4332 defsubr (&Sregion_beginning);
4333 defsubr (&Sregion_end);
4335 staticpro (&Qfield);
4336 Qfield = intern ("field");
4337 staticpro (&Qboundary);
4338 Qboundary = intern ("boundary");
4339 defsubr (&Sfield_beginning);
4340 defsubr (&Sfield_end);
4341 defsubr (&Sfield_string);
4342 defsubr (&Sfield_string_no_properties);
4343 defsubr (&Sdelete_field);
4344 defsubr (&Sconstrain_to_field);
4346 defsubr (&Sline_beginning_position);
4347 defsubr (&Sline_end_position);
4349 /* defsubr (&Smark); */
4350 /* defsubr (&Sset_mark); */
4351 defsubr (&Ssave_excursion);
4352 defsubr (&Ssave_current_buffer);
4354 defsubr (&Sbufsize);
4355 defsubr (&Spoint_max);
4356 defsubr (&Spoint_min);
4357 defsubr (&Spoint_min_marker);
4358 defsubr (&Spoint_max_marker);
4359 defsubr (&Sgap_position);
4360 defsubr (&Sgap_size);
4361 defsubr (&Sposition_bytes);
4362 defsubr (&Sbyte_to_position);
4364 defsubr (&Sbobp);
4365 defsubr (&Seobp);
4366 defsubr (&Sbolp);
4367 defsubr (&Seolp);
4368 defsubr (&Sfollowing_char);
4369 defsubr (&Sprevious_char);
4370 defsubr (&Schar_after);
4371 defsubr (&Schar_before);
4372 defsubr (&Sinsert);
4373 defsubr (&Sinsert_before_markers);
4374 defsubr (&Sinsert_and_inherit);
4375 defsubr (&Sinsert_and_inherit_before_markers);
4376 defsubr (&Sinsert_char);
4378 defsubr (&Suser_login_name);
4379 defsubr (&Suser_real_login_name);
4380 defsubr (&Suser_uid);
4381 defsubr (&Suser_real_uid);
4382 defsubr (&Suser_full_name);
4383 defsubr (&Semacs_pid);
4384 defsubr (&Scurrent_time);
4385 defsubr (&Sget_internal_run_time);
4386 defsubr (&Sformat_time_string);
4387 defsubr (&Sfloat_time);
4388 defsubr (&Sdecode_time);
4389 defsubr (&Sencode_time);
4390 defsubr (&Scurrent_time_string);
4391 defsubr (&Scurrent_time_zone);
4392 defsubr (&Sset_time_zone_rule);
4393 defsubr (&Ssystem_name);
4394 defsubr (&Smessage);
4395 defsubr (&Smessage_box);
4396 defsubr (&Smessage_or_box);
4397 defsubr (&Scurrent_message);
4398 defsubr (&Sformat);
4400 defsubr (&Sinsert_buffer_substring);
4401 defsubr (&Scompare_buffer_substrings);
4402 defsubr (&Ssubst_char_in_region);
4403 defsubr (&Stranslate_region_internal);
4404 defsubr (&Sdelete_region);
4405 defsubr (&Sdelete_and_extract_region);
4406 defsubr (&Swiden);
4407 defsubr (&Snarrow_to_region);
4408 defsubr (&Ssave_restriction);
4409 defsubr (&Stranspose_regions);
4412 /* arch-tag: fc3827d8-6f60-4067-b11e-c3218031b018
4413 (do not change this comment) */