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