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