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