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