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