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