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