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