*** empty log message ***
[bpt/emacs.git] / src / editfns.c
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <sys/types.h>
25
26 #ifdef VMS
27 #include "vms-pwd.h"
28 #else
29 #include <pwd.h>
30 #endif
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include "lisp.h"
37 #include "intervals.h"
38 #include "buffer.h"
39 #include "charset.h"
40 #include "coding.h"
41 #include "window.h"
42
43 #include "systime.h"
44
45 #define min(a, b) ((a) < (b) ? (a) : (b))
46 #define max(a, b) ((a) > (b) ? (a) : (b))
47
48 #ifndef NULL
49 #define NULL 0
50 #endif
51
52 #ifndef USE_CRT_DLL
53 extern char **environ;
54 #endif
55
56 extern Lisp_Object make_time P_ ((time_t));
57 extern size_t emacs_strftimeu P_ ((char *, size_t, const char *,
58 const struct tm *, int));
59 static int tm_diff P_ ((struct tm *, struct tm *));
60 static void find_field P_ ((Lisp_Object, Lisp_Object, int *, int *));
61 static void update_buffer_properties P_ ((int, int));
62 static Lisp_Object region_limit P_ ((int));
63 static int lisp_time_argument P_ ((Lisp_Object, time_t *, int *));
64 static size_t emacs_memftimeu P_ ((char *, size_t, const char *,
65 size_t, const struct tm *, int));
66 static void general_insert_function P_ ((void (*) (unsigned char *, int),
67 void (*) (Lisp_Object, int, int, int,
68 int, int),
69 int, int, Lisp_Object *));
70 static Lisp_Object subst_char_in_region_unwind P_ ((Lisp_Object));
71 static Lisp_Object subst_char_in_region_unwind_1 P_ ((Lisp_Object));
72 static void transpose_markers P_ ((int, int, int, int, int, int, int, int));
73
74 #ifdef HAVE_INDEX
75 extern char *index P_ ((const char *, int));
76 #endif
77
78 Lisp_Object Vbuffer_access_fontify_functions;
79 Lisp_Object Qbuffer_access_fontify_functions;
80 Lisp_Object Vbuffer_access_fontified_property;
81
82 Lisp_Object Fuser_full_name P_ ((Lisp_Object));
83
84 /* Non-nil means don't stop at field boundary in text motion commands. */
85
86 Lisp_Object Vinhibit_field_text_motion;
87
88 /* Some static data, and a function to initialize it for each run */
89
90 Lisp_Object Vsystem_name;
91 Lisp_Object Vuser_real_login_name; /* login name of current user ID */
92 Lisp_Object Vuser_full_name; /* full name of current user */
93 Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
94
95 /* Symbol for the text property used to mark fields. */
96
97 Lisp_Object Qfield;
98
99 /* A special value for Qfield properties. */
100
101 Lisp_Object Qboundary;
102
103
104 void
105 init_editfns ()
106 {
107 char *user_name;
108 register unsigned char *p;
109 struct passwd *pw; /* password entry for the current user */
110 Lisp_Object tem;
111
112 /* Set up system_name even when dumping. */
113 init_system_name ();
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 ());
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.) */
126 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
127 #else
128 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
129 #endif
130
131 /* Get the effective user name, by consulting environment variables,
132 or the effective uid if those are unset. */
133 user_name = (char *) getenv ("LOGNAME");
134 if (!user_name)
135 #ifdef WINDOWSNT
136 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
137 #else /* WINDOWSNT */
138 user_name = (char *) getenv ("USER");
139 #endif /* WINDOWSNT */
140 if (!user_name)
141 {
142 pw = (struct passwd *) getpwuid (geteuid ());
143 user_name = (char *) (pw ? pw->pw_name : "unknown");
144 }
145 Vuser_login_name = build_string (user_name);
146
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. */
149 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
150 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
151 : Vuser_login_name);
152
153 p = (unsigned char *) getenv ("NAME");
154 if (p)
155 Vuser_full_name = build_string (p);
156 else if (NILP (Vuser_full_name))
157 Vuser_full_name = build_string ("unknown");
158 }
159 \f
160 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
161 "Convert arg CHAR to a string containing that character.")
162 (character)
163 Lisp_Object character;
164 {
165 int len;
166 unsigned char str[MAX_MULTIBYTE_LENGTH];
167
168 CHECK_NUMBER (character, 0);
169
170 len = CHAR_STRING (XFASTINT (character), str);
171 return make_string_from_bytes (str, 1, len);
172 }
173
174 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
175 "Convert arg STRING to a character, the first character of that string.\n\
176 A multibyte character is handled correctly.")
177 (string)
178 register Lisp_Object string;
179 {
180 register Lisp_Object val;
181 register struct Lisp_String *p;
182 CHECK_STRING (string, 0);
183 p = XSTRING (string);
184 if (p->size)
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 }
191 else
192 XSETFASTINT (val, 0);
193 return val;
194 }
195 \f
196 static Lisp_Object
197 buildmark (charpos, bytepos)
198 int charpos, bytepos;
199 {
200 register Lisp_Object mark;
201 mark = Fmake_marker ();
202 set_marker_both (mark, Qnil, charpos, bytepos);
203 return mark;
204 }
205
206 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
207 "Return value of point, as an integer.\n\
208 Beginning of buffer is position (point-min)")
209 ()
210 {
211 Lisp_Object temp;
212 XSETFASTINT (temp, PT);
213 return temp;
214 }
215
216 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
217 "Return value of point, as a marker object.")
218 ()
219 {
220 return buildmark (PT, PT_BYTE);
221 }
222
223 int
224 clip_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
235 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
236 "Set point to POSITION, a number or marker.\n\
237 Beginning of buffer is position (point-min), end is (point-max).\n\
238 If the position is in the middle of a multibyte form,\n\
239 the actual point is set at the head of the multibyte form\n\
240 except in the case that `enable-multibyte-characters' is nil.")
241 (position)
242 register Lisp_Object position;
243 {
244 int pos;
245
246 if (MARKERP (position)
247 && current_buffer == XMARKER (position)->buffer)
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
260 CHECK_NUMBER_COERCE_MARKER (position, 0);
261
262 pos = clip_to_bounds (BEGV, XINT (position), ZV);
263 SET_PT (pos);
264 return position;
265 }
266
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
272 static Lisp_Object
273 region_limit (beginningp)
274 int beginningp;
275 {
276 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
277 Lisp_Object m;
278
279 if (!NILP (Vtransient_mark_mode)
280 && NILP (Vmark_even_if_inactive)
281 && NILP (current_buffer->mark_active))
282 Fsignal (Qmark_inactive, Qnil);
283
284 m = Fmarker_position (current_buffer->mark);
285 if (NILP (m))
286 error ("There is no region now");
287
288 if ((PT < XFASTINT (m)) == beginningp)
289 m = make_number (PT);
290 return m;
291 }
292
293 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
294 "Return position of beginning of region, as an integer.")
295 ()
296 {
297 return region_limit (1);
298 }
299
300 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
301 "Return position of end of region, as an integer.")
302 ()
303 {
304 return region_limit (0);
305 }
306
307 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
308 "Return this buffer's mark, as a marker object.\n\
309 Watch out! Moving this marker changes the mark position.\n\
310 If you set the marker not to point anywhere, the buffer will have no mark.")
311 ()
312 {
313 return current_buffer->mark;
314 }
315
316 \f
317 /* Return nonzero if POS1 and POS2 have the same value
318 for the text property PROP. */
319
320 static int
321 char_property_eq (prop, pos1, pos2)
322 Lisp_Object prop;
323 Lisp_Object pos1, pos2;
324 {
325 Lisp_Object pval1, pval2;
326
327 pval1 = Fget_char_property (pos1, prop, Qnil);
328 pval2 = Fget_char_property (pos2, prop, Qnil);
329
330 return EQ (pval1, pval2);
331 }
332
333 /* Return the direction from which the char-property PROP would be
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
338 static int
339 char_property_stickiness (prop, pos)
340 Lisp_Object prop;
341 Lisp_Object pos;
342 {
343 Lisp_Object front_sticky;
344
345 if (XINT (pos) > BEGV)
346 /* Consider previous character. */
347 {
348 Lisp_Object prev_pos, rear_non_sticky;
349
350 prev_pos = make_number (XINT (pos) - 1);
351 rear_non_sticky = Fget_char_property (prev_pos, Qrear_nonsticky, Qnil);
352
353 if (EQ (rear_non_sticky, Qnil)
354 || (CONSP (rear_non_sticky)
355 && NILP (Fmemq (prop, rear_non_sticky))))
356 /* PROP is not rear-non-sticky, and since this takes precedence over
357 any front-stickiness, PROP is inherited from before. */
358 return -1;
359 }
360
361 /* Consider following character. */
362 front_sticky = Fget_char_property (pos, Qfront_sticky, Qnil);
363
364 if (EQ (front_sticky, Qt)
365 || (CONSP (front_sticky)
366 && !NILP (Fmemq (prop, front_sticky))))
367 /* PROP is inherited from after. */
368 return 1;
369
370 /* PROP is not inherited from either side. */
371 return 0;
372 }
373
374 \f
375 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
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.
378
379 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
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.
388
389 Either BEG or END may be 0, in which case the corresponding value
390 is not stored. */
391
392 static void
393 find_field (pos, merge_at_boundary, beg, end)
394 Lisp_Object pos;
395 Lisp_Object merge_at_boundary;
396 int *beg, *end;
397 {
398 /* Fields right before and after the point. */
399 Lisp_Object before_field, after_field;
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;
404
405 if (NILP (pos))
406 XSETFASTINT (pos, PT);
407 else
408 CHECK_NUMBER_COERCE_MARKER (pos, 0);
409
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);
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"). */
438 {
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. */
443 }
444 }
445
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
468 if (beg)
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);
480
481 pos = Fprevious_single_char_property_change (pos, Qfield, Qnil, Qnil);
482 *beg = NILP (pos) ? BEGV : XFASTINT (pos);
483 }
484 }
485
486 if (end)
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);
498
499 pos = Fnext_single_char_property_change (pos, Qfield, Qnil, Qnil);
500 *end = NILP (pos) ? ZV : XFASTINT (pos);
501 }
502 }
503 }
504
505 \f
506 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
507 "Delete the field surrounding POS.\n\
508 A field is a region of text with the same `field' property.\n\
509 If POS is nil, the value of point is used for POS.")
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);
517 return Qnil;
518 }
519
520 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
521 "Return the contents of the field surrounding POS as a string.\n\
522 A field is a region of text with the same `field' property.\n\
523 If POS is nil, the value of point is used for POS.")
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
532 DEFUN ("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\
534 A field is a region of text with the same `field' property.\n\
535 If POS is nil, the value of point is used for POS.")
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
544 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 2, 0,
545 "Return the beginning of the field surrounding POS.\n\
546 A field is a region of text with the same `field' property.\n\
547 If POS is nil, the value of point is used for POS.\n\
548 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its\n\
549 field, 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
558 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 2, 0,
559 "Return the end of the field surrounding POS.\n\
560 A field is a region of text with the same `field' property.\n\
561 If POS is nil, the value of point is used for POS.\n\
562 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,\n\
563 then 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
572 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
573 "Return the position closest to NEW-POS that is in the same field as OLD-POS.\n\
574 \n\
575 A field is a region of text with the same `field' property.\n\
576 If NEW-POS is nil, then the current point is used instead, and set to the\n\
577 constrained position if that is is different.\n\
578 \n\
579 If OLD-POS is at the boundary of two fields, then the allowable\n\
580 positions for NEW-POS depends on the value of the optional argument\n\
581 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is\n\
582 constrained to the field that has the same `field' char-property\n\
583 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE\n\
584 is non-nil, NEW-POS is constrained to the union of the two adjacent\n\
585 fields. Additionally, if two fields are separated by another field with\n\
586 the special value `boundary', then any point within this special field is\n\
587 also considered to be `on the boundary'.\n\
588 \n\
589 If the optional argument ONLY-IN-LINE is non-nil and constraining\n\
590 NEW-POS would move it to a different line, NEW-POS is returned\n\
591 unconstrained. This useful for commands that move by line, like\n\
592 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries\n\
593 only in the case where they can still move to the right line.\n\
594 \n\
595 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has\n\
596 a non-nil property of that name, then any field boundaries are ignored.\n\
597 \n\
598 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil.")
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;
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
613 if (NILP (Vinhibit_field_text_motion)
614 && !EQ (new_pos, old_pos)
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))))
618 /* NEW_POS is not within the same field as OLD_POS; try to
619 move NEW_POS so that it is. */
620 {
621 int fwd, shortage;
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
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)))
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 }
661
662 \f
663 DEFUN ("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\
666 With argument N not nil or 1, move forward N - 1 lines first.\n\
667 If scan reaches end of buffer, return that position.\n\
668 The scan does not cross a field boundary unless it would move\n\
669 beyond 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\
671 and scan starts at a field boundary, the scan stops as soon as it starts.\n\
672 \n\
673 This function does not move point.")
674 (n)
675 Lisp_Object n;
676 {
677 int orig, orig_byte, end;
678
679 if (NILP (n))
680 XSETFASTINT (n, 1);
681 else
682 CHECK_NUMBER (n, 0);
683
684 orig = PT;
685 orig_byte = PT_BYTE;
686 Fforward_line (make_number (XINT (n) - 1));
687 end = PT;
688
689 SET_PT_BOTH (orig, orig_byte);
690
691 /* Return END constrained to the current input field. */
692 return Fconstrain_to_field (make_number (end), make_number (orig),
693 XINT (n) != 1 ? Qt : Qnil,
694 Qt, Qnil);
695 }
696
697 DEFUN ("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\
700 With argument N not nil or 1, move forward N - 1 lines first.\n\
701 If scan reaches end of buffer, return that position.\n\
702 This function does not move point.")
703 (n)
704 Lisp_Object n;
705 {
706 int end_pos;
707 int orig = PT;
708
709 if (NILP (n))
710 XSETFASTINT (n, 1);
711 else
712 CHECK_NUMBER (n, 0);
713
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. */
717 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
718 Qnil, Qt, Qnil);
719 }
720 \f
721 Lisp_Object
722 save_excursion_save ()
723 {
724 int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
725 == current_buffer);
726
727 return Fcons (Fpoint_marker (),
728 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
729 Fcons (visible ? Qt : Qnil,
730 current_buffer->mark_active)));
731 }
732
733 Lisp_Object
734 save_excursion_restore (info)
735 Lisp_Object info;
736 {
737 Lisp_Object tem, tem1, omark, nmark;
738 struct gcpro gcpro1, gcpro2, gcpro3;
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. */
745 if (NILP (tem))
746 return Qnil;
747
748 omark = nmark = Qnil;
749 GCPRO3 (info, omark, nmark);
750
751 Fset_buffer (tem);
752 tem = Fcar (info);
753 Fgoto_char (tem);
754 unchain_marker (tem);
755 tem = Fcar (Fcdr (info));
756 omark = Fmarker_position (current_buffer->mark);
757 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
758 nmark = Fmarker_position (tem);
759 unchain_marker (tem);
760 tem = Fcdr (Fcdr (info));
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. */
765 tem1 = Fcar (tem);
766 if (!NILP (tem1)
767 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
768 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
769 #endif /* 0 */
770
771 tem1 = current_buffer->mark_active;
772 current_buffer->mark_active = Fcdr (tem);
773 if (!NILP (Vrun_hooks))
774 {
775 /* If mark is active now, and either was not active
776 or was at a different place, run the activate hook. */
777 if (! NILP (current_buffer->mark_active))
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. */
783 else if (! NILP (tem1))
784 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
785 }
786 UNGCPRO;
787 return Qnil;
788 }
789
790 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
791 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
792 Executes BODY just like `progn'.\n\
793 The values of point, mark and the current buffer are restored\n\
794 even in case of abnormal exit (throw or error).\n\
795 The state of activation of the mark is also restored.\n\
796 \n\
797 This construct does not save `deactivate-mark', and therefore\n\
798 functions that change the buffer will still cause deactivation\n\
799 of the mark at the end of the command. To prevent that, bind\n\
800 `deactivate-mark' with `let'.")
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 ());
808
809 val = Fprogn (args);
810 return unbind_to (count, val);
811 }
812
813 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
814 "Save the current buffer; execute BODY; restore the current buffer.\n\
815 Executes BODY just like `progn'.")
816 (args)
817 Lisp_Object args;
818 {
819 Lisp_Object val;
820 int count = specpdl_ptr - specpdl;
821
822 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
823
824 val = Fprogn (args);
825 return unbind_to (count, val);
826 }
827 \f
828 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
829 "Return the number of characters in the current buffer.\n\
830 If BUFFER, return the number of characters in that buffer instead.")
831 (buffer)
832 Lisp_Object buffer;
833 {
834 if (NILP (buffer))
835 return make_number (Z - BEG);
836 else
837 {
838 CHECK_BUFFER (buffer, 1);
839 return make_number (BUF_Z (XBUFFER (buffer))
840 - BUF_BEG (XBUFFER (buffer)));
841 }
842 }
843
844 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
845 "Return the minimum permissible value of point in the current buffer.\n\
846 This is 1, unless narrowing (a buffer restriction) is in effect.")
847 ()
848 {
849 Lisp_Object temp;
850 XSETFASTINT (temp, BEGV);
851 return temp;
852 }
853
854 DEFUN ("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\
856 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
857 ()
858 {
859 return buildmark (BEGV, BEGV_BYTE);
860 }
861
862 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
863 "Return the maximum permissible value of point in the current buffer.\n\
864 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
865 is in effect, in which case it is less.")
866 ()
867 {
868 Lisp_Object temp;
869 XSETFASTINT (temp, ZV);
870 return temp;
871 }
872
873 DEFUN ("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\
875 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
876 is in effect, in which case it is less.")
877 ()
878 {
879 return buildmark (ZV, ZV_BYTE);
880 }
881
882 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
883 "Return the position of the gap, in the current buffer.\n\
884 See also `gap-size'.")
885 ()
886 {
887 Lisp_Object temp;
888 XSETFASTINT (temp, GPT);
889 return temp;
890 }
891
892 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
893 "Return the size of the current buffer's gap.\n\
894 See also `gap-position'.")
895 ()
896 {
897 Lisp_Object temp;
898 XSETFASTINT (temp, GAP_SIZE);
899 return temp;
900 }
901
902 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
903 "Return the byte position for character position POSITION.\n\
904 If POSITION is out of range, the value is nil.")
905 (position)
906 Lisp_Object position;
907 {
908 CHECK_NUMBER_COERCE_MARKER (position, 1);
909 if (XINT (position) < BEG || XINT (position) > Z)
910 return Qnil;
911 return make_number (CHAR_TO_BYTE (XINT (position)));
912 }
913
914 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
915 "Return the character position for byte position BYTEPOS.\n\
916 If BYTEPOS is out of range, the value is nil.")
917 (bytepos)
918 Lisp_Object bytepos;
919 {
920 CHECK_NUMBER (bytepos, 1);
921 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
922 return Qnil;
923 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
924 }
925 \f
926 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
927 "Return the character following point, as a number.\n\
928 At the end of the buffer or accessible region, return 0.")
929 ()
930 {
931 Lisp_Object temp;
932 if (PT >= ZV)
933 XSETFASTINT (temp, 0);
934 else
935 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
936 return temp;
937 }
938
939 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
940 "Return the character preceding point, as a number.\n\
941 At the beginning of the buffer or accessible region, return 0.")
942 ()
943 {
944 Lisp_Object temp;
945 if (PT <= BEGV)
946 XSETFASTINT (temp, 0);
947 else if (!NILP (current_buffer->enable_multibyte_characters))
948 {
949 int pos = PT_BYTE;
950 DEC_POS (pos);
951 XSETFASTINT (temp, FETCH_CHAR (pos));
952 }
953 else
954 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
955 return temp;
956 }
957
958 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
959 "Return t if point is at the beginning of the buffer.\n\
960 If the buffer is narrowed, this means the beginning of the narrowed part.")
961 ()
962 {
963 if (PT == BEGV)
964 return Qt;
965 return Qnil;
966 }
967
968 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
969 "Return t if point is at the end of the buffer.\n\
970 If the buffer is narrowed, this means the end of the narrowed part.")
971 ()
972 {
973 if (PT == ZV)
974 return Qt;
975 return Qnil;
976 }
977
978 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
979 "Return t if point is at the beginning of a line.")
980 ()
981 {
982 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
983 return Qt;
984 return Qnil;
985 }
986
987 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
988 "Return t if point is at the end of a line.\n\
989 `End of a line' includes point being at the end of the buffer.")
990 ()
991 {
992 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
993 return Qt;
994 return Qnil;
995 }
996
997 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
998 "Return character in current buffer at position POS.\n\
999 POS is an integer or a marker.\n\
1000 If POS is out of range, the value is nil.")
1001 (pos)
1002 Lisp_Object pos;
1003 {
1004 register int pos_byte;
1005
1006 if (NILP (pos))
1007 {
1008 pos_byte = PT_BYTE;
1009 XSETFASTINT (pos, PT);
1010 }
1011
1012 if (MARKERP (pos))
1013 {
1014 pos_byte = marker_byte_position (pos);
1015 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
1016 return Qnil;
1017 }
1018 else
1019 {
1020 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1021 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
1022 return Qnil;
1023
1024 pos_byte = CHAR_TO_BYTE (XINT (pos));
1025 }
1026
1027 return make_number (FETCH_CHAR (pos_byte));
1028 }
1029
1030 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
1031 "Return character in current buffer preceding position POS.\n\
1032 POS is an integer or a marker.\n\
1033 If POS is out of range, the value is nil.")
1034 (pos)
1035 Lisp_Object pos;
1036 {
1037 register Lisp_Object val;
1038 register int pos_byte;
1039
1040 if (NILP (pos))
1041 {
1042 pos_byte = PT_BYTE;
1043 XSETFASTINT (pos, PT);
1044 }
1045
1046 if (MARKERP (pos))
1047 {
1048 pos_byte = marker_byte_position (pos);
1049
1050 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
1051 return Qnil;
1052 }
1053 else
1054 {
1055 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1056
1057 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
1058 return Qnil;
1059
1060 pos_byte = CHAR_TO_BYTE (XINT (pos));
1061 }
1062
1063 if (!NILP (current_buffer->enable_multibyte_characters))
1064 {
1065 DEC_POS (pos_byte);
1066 XSETFASTINT (val, FETCH_CHAR (pos_byte));
1067 }
1068 else
1069 {
1070 pos_byte--;
1071 XSETFASTINT (val, FETCH_BYTE (pos_byte));
1072 }
1073 return val;
1074 }
1075 \f
1076 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
1077 "Return the name under which the user logged in, as a string.\n\
1078 This is based on the effective uid, not the real uid.\n\
1079 Also, if the environment variable LOGNAME or USER is set,\n\
1080 that determines the value of this function.\n\n\
1081 If optional argument UID is an integer, return the login name of the user\n\
1082 with that uid, or nil if there is no such user.")
1083 (uid)
1084 Lisp_Object uid;
1085 {
1086 struct passwd *pw;
1087
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. */
1091 if (INTEGERP (Vuser_login_name))
1092 init_editfns ();
1093
1094 if (NILP (uid))
1095 return Vuser_login_name;
1096
1097 CHECK_NUMBER (uid, 0);
1098 pw = (struct passwd *) getpwuid (XINT (uid));
1099 return (pw ? build_string (pw->pw_name) : Qnil);
1100 }
1101
1102 DEFUN ("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\
1105 This ignores the environment variables LOGNAME and USER, so it differs from\n\
1106 `user-login-name' when running under `su'.")
1107 ()
1108 {
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. */
1112 if (INTEGERP (Vuser_login_name))
1113 init_editfns ();
1114 return Vuser_real_login_name;
1115 }
1116
1117 DEFUN ("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
1124 DEFUN ("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
1131 DEFUN ("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\
1133 If the full name corresponding to Emacs's userid is not known,\n\
1134 return \"unknown\".\n\
1135 \n\
1136 If optional argument UID is an integer, return the full name of the user\n\
1137 with that uid, or nil if there is no such user.\n\
1138 If UID is a string, return the full name of the user with that login\n\
1139 name, or nil if there is no such user.")
1140 (uid)
1141 Lisp_Object uid;
1142 {
1143 struct passwd *pw;
1144 register unsigned char *p, *q;
1145 Lisp_Object full;
1146
1147 if (NILP (uid))
1148 return Vuser_full_name;
1149 else if (NUMBERP (uid))
1150 pw = (struct passwd *) getpwuid (XINT (uid));
1151 else if (STRINGP (uid))
1152 pw = (struct passwd *) getpwnam (XSTRING (uid)->data);
1153 else
1154 error ("Invalid UID specification");
1155
1156 if (!pw)
1157 return Qnil;
1158
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));
1163
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 {
1170 register unsigned char *r;
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;
1185 }
1186
1187 DEFUN ("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
1194 /* For the benefit of callers who don't want to include lisp.h */
1195
1196 char *
1197 get_system_name ()
1198 {
1199 if (STRINGP (Vsystem_name))
1200 return (char *) XSTRING (Vsystem_name)->data;
1201 else
1202 return "";
1203 }
1204
1205 DEFUN ("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
1212 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1213 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
1214 The time is returned as a list of three integers. The first has the\n\
1215 most significant 16 bits of the seconds, while the second has the\n\
1216 least significant 16 bits. The third integer gives the microsecond\n\
1217 count.\n\
1218 \n\
1219 The microsecond count is zero on systems that do not provide\n\
1220 resolution finer than a second.")
1221 ()
1222 {
1223 EMACS_TIME t;
1224 Lisp_Object result[3];
1225
1226 EMACS_GET_TIME (t);
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));
1230
1231 return Flist (3, result);
1232 }
1233 \f
1234
1235 static int
1236 lisp_time_argument (specified_time, result, usec)
1237 Lisp_Object specified_time;
1238 time_t *result;
1239 int *usec;
1240 {
1241 if (NILP (specified_time))
1242 {
1243 if (usec)
1244 {
1245 EMACS_TIME t;
1246
1247 EMACS_GET_TIME (t);
1248 *usec = EMACS_USECS (t);
1249 *result = EMACS_SECS (t);
1250 return 1;
1251 }
1252 else
1253 return time (result) != -1;
1254 }
1255 else
1256 {
1257 Lisp_Object high, low;
1258 high = Fcar (specified_time);
1259 CHECK_NUMBER (high, 0);
1260 low = Fcdr (specified_time);
1261 if (CONSP (low))
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;
1280 CHECK_NUMBER (low, 0);
1281 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
1282 return *result >> 16 == XINT (high);
1283 }
1284 }
1285
1286 DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
1287 "Return the current time, as a float number of seconds since the epoch.\n\
1288 If an argument is given, it specifies a time to convert to float\n\
1289 instead of the current time. The argument should have the forms:\n\
1290 (HIGH . LOW) or (HIGH LOW USEC) or (HIGH LOW . USEC).\n\
1291 Thus, you can use times obtained from `current-time'\n\
1292 and 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
1305 /* Write information into buffer S of size MAXSIZE, according to the
1306 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1307 Default to Universal Time if UT is nonzero, local time otherwise.
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
1313 This function behaves like emacs_strftimeu, except it allows null
1314 bytes in FORMAT. */
1315 static size_t
1316 emacs_memftimeu (s, maxsize, format, format_len, tp, ut)
1317 char *s;
1318 size_t maxsize;
1319 const char *format;
1320 size_t format_len;
1321 const struct tm *tp;
1322 int ut;
1323 {
1324 size_t total = 0;
1325
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
1329 format contains '\0' bytes. emacs_strftimeu stops at the first
1330 '\0' byte so we must invoke it separately for each such string. */
1331 for (;;)
1332 {
1333 size_t len;
1334 size_t result;
1335
1336 if (s)
1337 s[0] = '\1';
1338
1339 result = emacs_strftimeu (s, maxsize, format, tp, ut);
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
1359 /*
1360 DEFUN ("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\
1362 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
1363 `current-time' or `file-attributes'.\n\
1364 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
1365 as Universal Time; nil means describe TIME in the local time zone.\n\
1366 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
1367 by text that describes the specified date and time in TIME:\n\
1368 \n\
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\
1371 %m is the numeric month.\n\
1372 %b and %h are the locale's abbreviated month name, %B the full name.\n\
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\
1375 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
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\
1382 %p is the locale's equivalent of either AM or PM.\n\
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\
1395 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
1396 \n\
1397 Certain flags and modifiers are available with some format controls.\n\
1398 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
1399 but padded with blanks; %-X is like %X, but without padding.\n\
1400 %NX (where N stands for an integer) is like %X,\n\
1401 but takes up at least N (a number) positions.\n\
1402 The 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\
1406 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
1407 (format_string, time, universal)
1408 */
1409
1410 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1411 0 /* See immediately above */)
1412 (format_string, time, universal)
1413 Lisp_Object format_string, time, universal;
1414 {
1415 time_t value;
1416 int size;
1417 struct tm *tm;
1418 int ut = ! NILP (universal);
1419
1420 CHECK_STRING (format_string, 1);
1421
1422 if (! lisp_time_argument (time, &value, NULL))
1423 error ("Invalid time specification");
1424
1425 format_string = code_convert_string_norecord (format_string,
1426 Vlocale_coding_system, 1);
1427
1428 /* This is probably enough. */
1429 size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
1430
1431 tm = ut ? gmtime (&value) : localtime (&value);
1432 if (! tm)
1433 error ("Specified time is not representable");
1434
1435 synchronize_system_time_locale ();
1436
1437 while (1)
1438 {
1439 char *buf = (char *) alloca (size + 1);
1440 int result;
1441
1442 buf[0] = '\1';
1443 result = emacs_memftimeu (buf, size, XSTRING (format_string)->data,
1444 STRING_BYTES (XSTRING (format_string)),
1445 tm, ut);
1446 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
1447 return code_convert_string_norecord (make_string (buf, result),
1448 Vlocale_coding_system, 0);
1449
1450 /* If buffer was too small, make it bigger and try again. */
1451 result = emacs_memftimeu (NULL, (size_t) -1,
1452 XSTRING (format_string)->data,
1453 STRING_BYTES (XSTRING (format_string)),
1454 tm, ut);
1455 size = result + 1;
1456 }
1457 }
1458
1459 DEFUN ("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\
1461 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
1462 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
1463 to use the current time. The list has the following nine members:\n\
1464 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
1465 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
1466 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
1467 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
1468 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
1469 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
1470 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
1471 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
1472 (specified_time)
1473 Lisp_Object specified_time;
1474 {
1475 time_t time_spec;
1476 struct tm save_tm;
1477 struct tm *decoded_time;
1478 Lisp_Object list_args[9];
1479
1480 if (! lisp_time_argument (specified_time, &time_spec, NULL))
1481 error ("Invalid time specification");
1482
1483 decoded_time = localtime (&time_spec);
1484 if (! decoded_time)
1485 error ("Specified time is not representable");
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);
1491 XSETINT (list_args[5], decoded_time->tm_year + 1900);
1492 XSETFASTINT (list_args[6], decoded_time->tm_wday);
1493 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
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
1501 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1502 return Flist (9, list_args);
1503 }
1504
1505 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1506 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
1507 This is the reverse operation of `decode-time', which see.\n\
1508 ZONE defaults to the current time zone rule. This can\n\
1509 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
1510 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
1511 applied without consideration for daylight savings time.\n\
1512 \n\
1513 You can pass more than 7 arguments; then the first six arguments\n\
1514 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
1515 The intervening arguments are ignored.\n\
1516 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
1517 \n\
1518 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
1519 for example, a DAY of 0 means the day preceding the given month.\n\
1520 Year numbers less than 100 are treated just like other year numbers.\n\
1521 If you want them to stand for years in this century, you must do that yourself.")
1522 (nargs, args)
1523 int nargs;
1524 register Lisp_Object *args;
1525 {
1526 time_t time;
1527 struct tm tm;
1528 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
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;
1543 tm.tm_isdst = -1;
1544
1545 if (CONSP (zone))
1546 zone = Fcar (zone);
1547 if (NILP (zone))
1548 time = mktime (&tm);
1549 else
1550 {
1551 char tzbuf[100];
1552 char *tzstring;
1553 char **oldenv = environ, **newenv;
1554
1555 if (EQ (zone, Qt))
1556 tzstring = "UTC0";
1557 else if (STRINGP (zone))
1558 tzstring = (char *) XSTRING (zone)->data;
1559 else if (INTEGERP (zone))
1560 {
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;
1565 }
1566 else
1567 error ("Invalid time zone specification");
1568
1569 /* Set TZ before calling mktime; merely adjusting mktime's returned
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;
1578 xfree (newenv);
1579 #ifdef LOCALTIME_CACHE
1580 tzset ();
1581 #endif
1582 }
1583
1584 if (time == (time_t) -1)
1585 error ("Specified time is not representable");
1586
1587 return make_time (time);
1588 }
1589
1590 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1591 "Return the current time, as a human-readable string.\n\
1592 Programs can use this function to decode a time,\n\
1593 since the number of columns in each field is fixed.\n\
1594 The format is `Sun Sep 16 01:03:52 1973'.\n\
1595 However, see also the functions `decode-time' and `format-time-string'\n\
1596 which provide a much more powerful and general facility.\n\
1597 \n\
1598 If an argument is given, it specifies a time to format\n\
1599 instead of the current time. The argument should have the form:\n\
1600 (HIGH . LOW)\n\
1601 or the form:\n\
1602 (HIGH LOW . IGNORED).\n\
1603 Thus, you can use times obtained from `current-time'\n\
1604 and from `file-attributes'.")
1605 (specified_time)
1606 Lisp_Object specified_time;
1607 {
1608 time_t value;
1609 char buf[30];
1610 register char *tem;
1611
1612 if (! lisp_time_argument (specified_time, &value, NULL))
1613 value = -1;
1614 tem = (char *) ctime (&value);
1615
1616 strncpy (buf, tem, 24);
1617 buf[24] = 0;
1618
1619 return build_string (buf);
1620 }
1621
1622 #define TM_YEAR_BASE 1900
1623
1624 /* Yield A - B, measured in seconds.
1625 This function is copied from the GNU C Library. */
1626 static int
1627 tm_diff (a, b)
1628 struct tm *a, *b;
1629 {
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))
1645 + (a->tm_sec - b->tm_sec));
1646 }
1647
1648 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1649 "Return the offset and name for the local time zone.\n\
1650 This returns a list of the form (OFFSET NAME).\n\
1651 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1652 A negative value means west of Greenwich.\n\
1653 NAME is a string giving the name of the time zone.\n\
1654 If an argument is given, it specifies when the time zone offset is determined\n\
1655 instead of using the current time. The argument should have the form:\n\
1656 (HIGH . LOW)\n\
1657 or the form:\n\
1658 (HIGH LOW . IGNORED).\n\
1659 Thus, you can use times obtained from `current-time'\n\
1660 and from `file-attributes'.\n\
1661 \n\
1662 Some operating systems cannot provide all this information to Emacs;\n\
1663 in this case, `current-time-zone' returns a list containing nil for\n\
1664 the data it can't find.")
1665 (specified_time)
1666 Lisp_Object specified_time;
1667 {
1668 time_t value;
1669 struct tm *t;
1670 struct tm gmt;
1671
1672 if (lisp_time_argument (specified_time, &value, NULL)
1673 && (t = gmtime (&value)) != 0
1674 && (gmt = *t, t = localtime (&value)) != 0)
1675 {
1676 int offset = tm_diff (t, &gmt);
1677 char *s = 0;
1678 char buf[6];
1679 #ifdef HAVE_TM_ZONE
1680 if (t->tm_zone)
1681 s = (char *)t->tm_zone;
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];
1686 #endif
1687 #endif /* not HAVE_TM_ZONE */
1688 if (!s)
1689 {
1690 /* No local time zone name is available; use "+-NNNN" instead. */
1691 int am = (offset < 0 ? -offset : offset) / 60;
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
1698 return Fmake_list (make_number (2), Qnil);
1699 }
1700
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. */
1704 static char **environbuf;
1705
1706 DEFUN ("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\
1708 If TZ is nil, use implementation-defined default time zone information.\n\
1709 If TZ is t, use Universal Time.")
1710 (tz)
1711 Lisp_Object tz;
1712 {
1713 char *tzstring;
1714
1715 if (NILP (tz))
1716 tzstring = 0;
1717 else if (EQ (tz, Qt))
1718 tzstring = "UTC0";
1719 else
1720 {
1721 CHECK_STRING (tz, 0);
1722 tzstring = (char *) XSTRING (tz)->data;
1723 }
1724
1725 set_time_zone_rule (tzstring);
1726 if (environbuf)
1727 free (environbuf);
1728 environbuf = environ;
1729
1730 return Qnil;
1731 }
1732
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.
1737 Their values shouldn't matter in non-buggy implementations.
1738 We don't use string literals for these strings,
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
1744 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1745 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1746
1747 #endif
1748
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. */
1752
1753 void
1754 set_time_zone_rule (tzstring)
1755 char *tzstring;
1756 {
1757 int envptrs;
1758 char **from, **to, **newenv;
1759
1760 /* Make the ENVIRON vector longer with room for TZSTRING. */
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));
1766
1767 /* Add TZSTRING to the end of environ, as a value for TZ. */
1768 if (tzstring)
1769 {
1770 char *t = (char *) (to + envptrs);
1771 strcpy (t, "TZ=");
1772 strcat (t, tzstring);
1773 *to++ = t;
1774 }
1775
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. */
1779 for (from = environ; *from; from++)
1780 if (strncmp (*from, "TZ=", 3) != 0)
1781 *to++ = *from;
1782 *to = 0;
1783
1784 environ = newenv;
1785
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
1790 #ifdef LOCALTIME_CACHE
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
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;
1805 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1806 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
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. */
1814 *to = set_time_zone_rule_tz1;
1815 to[1] = 0;
1816 tzset ();
1817 *to = set_time_zone_rule_tz2;
1818 tzset ();
1819 *to = 0;
1820 }
1821
1822 /* Now TZ has the desired value, and tzset can be invoked safely. */
1823 }
1824
1825 tzset ();
1826 #endif
1827 }
1828 \f
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
1834 static void
1835 general_insert_function (insert_func, insert_from_string_func,
1836 inherit, nargs, args)
1837 void (*insert_func) P_ ((unsigned char *, int));
1838 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
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 {
1851 unsigned char str[MAX_MULTIBYTE_LENGTH];
1852 int len;
1853
1854 if (!NILP (current_buffer->enable_multibyte_characters))
1855 len = CHAR_STRING (XFASTINT (val), str);
1856 else
1857 {
1858 str[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
1859 ? XINT (val)
1860 : multibyte_char_to_unibyte (XINT (val), Qnil));
1861 len = 1;
1862 }
1863 (*insert_func) (str, len);
1864 }
1865 else if (STRINGP (val))
1866 {
1867 (*insert_from_string_func) (val, 0, 0,
1868 XSTRING (val)->size,
1869 STRING_BYTES (XSTRING (val)),
1870 inherit);
1871 }
1872 else
1873 {
1874 val = wrong_type_argument (Qchar_or_string_p, val);
1875 goto retry;
1876 }
1877 }
1878 }
1879
1880 void
1881 insert1 (arg)
1882 Lisp_Object arg;
1883 {
1884 Finsert (1, &arg);
1885 }
1886
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
1893 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1894 "Insert the arguments, either strings or characters, at point.\n\
1895 Point and before-insertion markers move forward to end up\n\
1896 after the inserted text.\n\
1897 Any other markers at the point of insertion remain before the text.\n\
1898 \n\
1899 If the current buffer is multibyte, unibyte strings are converted\n\
1900 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1901 If the current buffer is unibyte, multibyte strings are converted\n\
1902 to unibyte for insertion.")
1903 (nargs, args)
1904 int nargs;
1905 register Lisp_Object *args;
1906 {
1907 general_insert_function (insert, insert_from_string, 0, nargs, args);
1908 return Qnil;
1909 }
1910
1911 DEFUN ("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\
1914 Point and before-insertion markers move forward to end up\n\
1915 after the inserted text.\n\
1916 Any other markers at the point of insertion remain before the text.\n\
1917 \n\
1918 If the current buffer is multibyte, unibyte strings are converted\n\
1919 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1920 If the current buffer is unibyte, multibyte strings are converted\n\
1921 to unibyte for insertion.")
1922 (nargs, args)
1923 int nargs;
1924 register Lisp_Object *args;
1925 {
1926 general_insert_function (insert_and_inherit, insert_from_string, 1,
1927 nargs, args);
1928 return Qnil;
1929 }
1930
1931 DEFUN ("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\
1933 Point and markers move forward to end up after the inserted text.\n\
1934 \n\
1935 If the current buffer is multibyte, unibyte strings are converted\n\
1936 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1937 If the current buffer is unibyte, multibyte strings are converted\n\
1938 to unibyte for insertion.")
1939 (nargs, args)
1940 int nargs;
1941 register Lisp_Object *args;
1942 {
1943 general_insert_function (insert_before_markers,
1944 insert_from_string_before_markers, 0,
1945 nargs, args);
1946 return Qnil;
1947 }
1948
1949 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
1950 Sinsert_and_inherit_before_markers, 0, MANY, 0,
1951 "Insert text at point, relocating markers and inheriting properties.\n\
1952 Point and markers move forward to end up after the inserted text.\n\
1953 \n\
1954 If the current buffer is multibyte, unibyte strings are converted\n\
1955 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1956 If the current buffer is unibyte, multibyte strings are converted\n\
1957 to unibyte for insertion.")
1958 (nargs, args)
1959 int nargs;
1960 register Lisp_Object *args;
1961 {
1962 general_insert_function (insert_before_markers_and_inherit,
1963 insert_from_string_before_markers, 1,
1964 nargs, args);
1965 return Qnil;
1966 }
1967 \f
1968 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
1969 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1970 Both arguments are required.\n\
1971 Point, and before-insertion markers, are relocated as in the function `insert'.\n\
1972 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1973 from adjoining text, if those properties are sticky.")
1974 (character, count, inherit)
1975 Lisp_Object character, count, inherit;
1976 {
1977 register unsigned char *string;
1978 register int strlen;
1979 register int i, n;
1980 int len;
1981 unsigned char str[MAX_MULTIBYTE_LENGTH];
1982
1983 CHECK_NUMBER (character, 0);
1984 CHECK_NUMBER (count, 1);
1985
1986 if (!NILP (current_buffer->enable_multibyte_characters))
1987 len = CHAR_STRING (XFASTINT (character), str);
1988 else
1989 str[0] = XFASTINT (character), len = 1;
1990 n = XINT (count) * len;
1991 if (n <= 0)
1992 return Qnil;
1993 strlen = min (n, 256 * len);
1994 string = (unsigned char *) alloca (strlen);
1995 for (i = 0; i < strlen; i++)
1996 string[i] = str[i % len];
1997 while (n >= strlen)
1998 {
1999 QUIT;
2000 if (!NILP (inherit))
2001 insert_and_inherit (string, strlen);
2002 else
2003 insert (string, strlen);
2004 n -= strlen;
2005 }
2006 if (n > 0)
2007 {
2008 if (!NILP (inherit))
2009 insert_and_inherit (string, n);
2010 else
2011 insert (string, n);
2012 }
2013 return Qnil;
2014 }
2015
2016 \f
2017 /* Making strings from buffer contents. */
2018
2019 /* Return a Lisp_String containing the text of the current buffer from
2020 START to END. If text properties are in use and the current buffer
2021 has properties in the range specified, the resulting string will also
2022 have them, if PROPS is nonzero.
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. */
2031
2032 Lisp_Object
2033 make_buffer_string (start, end, props)
2034 int start, end;
2035 int props;
2036 {
2037 int start_byte = CHAR_TO_BYTE (start);
2038 int end_byte = CHAR_TO_BYTE (end);
2039
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
2058 Lisp_Object
2059 make_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
2065 if (start < GPT && GPT < end)
2066 move_gap (start);
2067
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);
2072 bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data,
2073 end_byte - start_byte);
2074
2075 /* If desired, update and copy the text properties. */
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))
2084 copy_intervals_to_string (result, current_buffer, start,
2085 end - start);
2086 }
2087
2088 return result;
2089 }
2090
2091 /* Call Vbuffer_access_fontify_functions for the range START ... END
2092 in the current buffer, if necessary. */
2093
2094 static void
2095 update_buffer_properties (start, end)
2096 int start, end;
2097 {
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))
2117 Frun_hook_with_args (3, args);
2118 }
2119 else
2120 Frun_hook_with_args (3, args);
2121 }
2122 }
2123
2124 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
2125 "Return the contents of part of the current buffer as a string.\n\
2126 The two arguments START and END are character positions;\n\
2127 they can be in either order.\n\
2128 The string returned is multibyte if the buffer is multibyte.")
2129 (start, end)
2130 Lisp_Object start, end;
2131 {
2132 register int b, e;
2133
2134 validate_region (&start, &end);
2135 b = XINT (start);
2136 e = XINT (end);
2137
2138 return make_buffer_string (b, e, 1);
2139 }
2140
2141 DEFUN ("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\
2144 The two arguments START and END are character positions;\n\
2145 they can be in either order.")
2146 (start, end)
2147 Lisp_Object start, end;
2148 {
2149 register int b, e;
2150
2151 validate_region (&start, &end);
2152 b = XINT (start);
2153 e = XINT (end);
2154
2155 return make_buffer_string (b, e, 0);
2156 }
2157
2158 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2159 "Return the contents of the current buffer as a string.\n\
2160 If narrowing is in effect, this function returns only the visible part\n\
2161 of the buffer.")
2162 ()
2163 {
2164 return make_buffer_string (BEGV, ZV, 1);
2165 }
2166
2167 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2168 1, 3, 0,
2169 "Insert before point a substring of the contents of buffer BUFFER.\n\
2170 BUFFER may be a buffer or a buffer name.\n\
2171 Arguments START and END are character numbers specifying the substring.\n\
2172 They default to the beginning and the end of BUFFER.")
2173 (buf, start, end)
2174 Lisp_Object buf, start, end;
2175 {
2176 register int b, e, temp;
2177 register struct buffer *bp, *obuf;
2178 Lisp_Object buffer;
2179
2180 buffer = Fget_buffer (buf);
2181 if (NILP (buffer))
2182 nsberror (buf);
2183 bp = XBUFFER (buffer);
2184 if (NILP (bp->name))
2185 error ("Selecting deleted buffer");
2186
2187 if (NILP (start))
2188 b = BUF_BEGV (bp);
2189 else
2190 {
2191 CHECK_NUMBER_COERCE_MARKER (start, 0);
2192 b = XINT (start);
2193 }
2194 if (NILP (end))
2195 e = BUF_ZV (bp);
2196 else
2197 {
2198 CHECK_NUMBER_COERCE_MARKER (end, 1);
2199 e = XINT (end);
2200 }
2201
2202 if (b > e)
2203 temp = b, b = e, e = temp;
2204
2205 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2206 args_out_of_range (start, end);
2207
2208 obuf = current_buffer;
2209 set_buffer_internal_1 (bp);
2210 update_buffer_properties (b, e);
2211 set_buffer_internal_1 (obuf);
2212
2213 insert_from_buffer (bp, b, e - b, 0);
2214 return Qnil;
2215 }
2216
2217 DEFUN ("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\
2220 the 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\
2222 Each substring is represented as three arguments: BUFFER, START and END.\n\
2223 That makes six args in all, three for each substring.\n\n\
2224 The value of `case-fold-search' in the current buffer\n\
2225 determines whether case is significant or ignored.")
2226 (buffer1, start1, end1, buffer2, start2, end2)
2227 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
2228 {
2229 register int begp1, endp1, begp2, endp2, temp;
2230 register struct buffer *bp1, *bp2;
2231 register Lisp_Object *trt
2232 = (!NILP (current_buffer->case_fold_search)
2233 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
2234 int chars = 0;
2235 int i1, i2, i1_byte, i2_byte;
2236
2237 /* Find the first buffer and its substring. */
2238
2239 if (NILP (buffer1))
2240 bp1 = current_buffer;
2241 else
2242 {
2243 Lisp_Object buf1;
2244 buf1 = Fget_buffer (buffer1);
2245 if (NILP (buf1))
2246 nsberror (buffer1);
2247 bp1 = XBUFFER (buf1);
2248 if (NILP (bp1->name))
2249 error ("Selecting deleted buffer");
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 {
2281 Lisp_Object buf2;
2282 buf2 = Fget_buffer (buffer2);
2283 if (NILP (buf2))
2284 nsberror (buffer2);
2285 bp2 = XBUFFER (buf2);
2286 if (NILP (bp2->name))
2287 error ("Selecting deleted buffer");
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
2313 i1 = begp1;
2314 i2 = begp2;
2315 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2316 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2317
2318 while (i1 < endp1 && i2 < endp2)
2319 {
2320 /* When we find a mismatch, we must compare the
2321 characters, not just the bytes. */
2322 int c1, c2;
2323
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 }
2349
2350 if (trt)
2351 {
2352 c1 = XINT (trt[c1]);
2353 c2 = XINT (trt[c2]);
2354 }
2355 if (c1 < c2)
2356 return make_number (- 1 - chars);
2357 if (c1 > c2)
2358 return make_number (chars + 1);
2359
2360 chars++;
2361 }
2362
2363 /* The strings match as far as they go.
2364 If one is shorter, that one is less. */
2365 if (chars < endp1 - begp1)
2366 return make_number (chars + 1);
2367 else if (chars < endp2 - begp2)
2368 return make_number (- chars - 1);
2369
2370 /* Same length too => they are equal. */
2371 return make_number (0);
2372 }
2373 \f
2374 static Lisp_Object
2375 subst_char_in_region_unwind (arg)
2376 Lisp_Object arg;
2377 {
2378 return current_buffer->undo_list = arg;
2379 }
2380
2381 static Lisp_Object
2382 subst_char_in_region_unwind_1 (arg)
2383 Lisp_Object arg;
2384 {
2385 return current_buffer->filename = arg;
2386 }
2387
2388 DEFUN ("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\
2391 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
2392 and don't mark the buffer as really changed.\n\
2393 Both characters must have the same length of multi-byte form.")
2394 (start, end, fromchar, tochar, noundo)
2395 Lisp_Object start, end, fromchar, tochar, noundo;
2396 {
2397 register int pos, pos_byte, stop, i, len, end_byte;
2398 int changed = 0;
2399 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2400 unsigned char *p;
2401 int count = specpdl_ptr - specpdl;
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;
2407 int last_changed;
2408 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2409
2410 validate_region (&start, &end);
2411 CHECK_NUMBER (fromchar, 2);
2412 CHECK_NUMBER (tochar, 3);
2413
2414 if (multibyte_p)
2415 {
2416 len = CHAR_STRING (XFASTINT (fromchar), fromstr);
2417 if (CHAR_STRING (XFASTINT (tochar), tostr) != len)
2418 error ("Characters in subst-char-in-region have different byte-lengths");
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 }
2430 }
2431 else
2432 {
2433 len = 1;
2434 fromstr[0] = XFASTINT (fromchar);
2435 tostr[0] = XFASTINT (tochar);
2436 }
2437
2438 pos = XINT (start);
2439 pos_byte = CHAR_TO_BYTE (pos);
2440 stop = CHAR_TO_BYTE (XINT (end));
2441 end_byte = stop;
2442
2443 /* If we don't want undo, turn off putting stuff on the list.
2444 That's faster than getting rid of things,
2445 and it prevents even the entry for a first change.
2446 Also inhibit locking the file. */
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;
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;
2456 }
2457
2458 if (pos_byte < GPT_BYTE)
2459 stop = min (stop, GPT_BYTE);
2460 while (1)
2461 {
2462 int pos_byte_next = pos_byte;
2463
2464 if (pos_byte >= stop)
2465 {
2466 if (pos_byte >= end_byte) break;
2467 stop = end_byte;
2468 }
2469 p = BYTE_POS_ADDR (pos_byte);
2470 if (multibyte_p)
2471 INC_POS (pos_byte_next);
2472 else
2473 ++pos_byte_next;
2474 if (pos_byte_next - pos_byte == len
2475 && p[0] == fromstr[0]
2476 && (len == 1
2477 || (p[1] == fromstr[1]
2478 && (len == 2 || (p[2] == fromstr[2]
2479 && (len == 3 || p[3] == fromstr[3]))))))
2480 {
2481 if (! changed)
2482 {
2483 changed = pos;
2484 modify_region (current_buffer, changed, XINT (end));
2485
2486 if (! NILP (noundo))
2487 {
2488 if (MODIFF - 1 == SAVE_MODIFF)
2489 SAVE_MODIFF++;
2490 if (MODIFF - 1 == current_buffer->auto_save_modified)
2491 current_buffer->auto_save_modified++;
2492 }
2493 }
2494
2495 /* Take care of the case where the new character
2496 combines with neighboring bytes. */
2497 if (maybe_byte_combining
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))))))
2505 {
2506 Lisp_Object tem, string;
2507
2508 struct gcpro gcpro1;
2509
2510 tem = current_buffer->undo_list;
2511 GCPRO1 (tem);
2512
2513 /* Make a multibyte string containing this single character. */
2514 string = make_multibyte_string (tostr, 1, len);
2515 /* replace_range is less efficient, because it moves the gap,
2516 but it handles combining correctly. */
2517 replace_range (pos, pos + 1, string,
2518 0, 0, 1);
2519 pos_byte_next = CHAR_TO_BYTE (pos);
2520 if (pos_byte_next > pos_byte)
2521 /* Before combining happened. We should not increment
2522 POS. So, to cancel the later increment of POS,
2523 decrease it now. */
2524 pos--;
2525 else
2526 INC_POS (pos_byte_next);
2527
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 }
2539 last_changed = pos + 1;
2540 }
2541 pos_byte = pos_byte_next;
2542 pos++;
2543 }
2544
2545 if (changed)
2546 {
2547 signal_after_change (changed,
2548 last_changed - changed, last_changed - changed);
2549 update_compositions (changed, last_changed, CHECK_ALL);
2550 }
2551
2552 unbind_to (count, Qnil);
2553 return Qnil;
2554 }
2555
2556 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
2557 "From START to END, translate characters according to TABLE.\n\
2558 TABLE is a string; the Nth character in it is the mapping\n\
2559 for the character with code N.\n\
2560 This function does not alter multibyte characters.\n\
2561 It returns the number of characters changed.")
2562 (start, end, table)
2563 Lisp_Object start;
2564 Lisp_Object end;
2565 register Lisp_Object table;
2566 {
2567 register int pos_byte, stop; /* Limits of the region. */
2568 register unsigned char *tt; /* Trans table. */
2569 register int nc; /* New character. */
2570 int cnt; /* Number of changes made. */
2571 int size; /* Size of translate table. */
2572 int pos;
2573 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2574
2575 validate_region (&start, &end);
2576 CHECK_STRING (table, 2);
2577
2578 size = STRING_BYTES (XSTRING (table));
2579 tt = XSTRING (table)->data;
2580
2581 pos_byte = CHAR_TO_BYTE (XINT (start));
2582 stop = CHAR_TO_BYTE (XINT (end));
2583 modify_region (current_buffer, XINT (start), XINT (end));
2584 pos = XINT (start);
2585
2586 cnt = 0;
2587 for (; pos_byte < stop; )
2588 {
2589 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
2590 int len;
2591 int oc;
2592 int pos_byte_next;
2593
2594 if (multibyte)
2595 oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len);
2596 else
2597 oc = *p, len = 1;
2598 pos_byte_next = pos_byte + len;
2599 if (oc < size && len == 1)
2600 {
2601 nc = tt[oc];
2602 if (nc != oc)
2603 {
2604 /* Take care of the case where the new character
2605 combines with neighboring bytes. */
2606 if (!ASCII_BYTE_P (nc)
2607 && (CHAR_HEAD_P (nc)
2608 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1))
2609 : (pos_byte > BEG_BYTE
2610 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))
2611 {
2612 Lisp_Object string;
2613
2614 string = make_multibyte_string (tt + oc, 1, 1);
2615 /* This is less efficient, because it moves the gap,
2616 but it handles combining correctly. */
2617 replace_range (pos, pos + 1, string,
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
2622 increment POS. So, to cancel the later
2623 increment of POS, we decrease it now. */
2624 pos--;
2625 else
2626 INC_POS (pos_byte_next);
2627 }
2628 else
2629 {
2630 record_change (pos, 1);
2631 *p = nc;
2632 signal_after_change (pos, 1, 1);
2633 update_compositions (pos, pos + 1, CHECK_BORDER);
2634 }
2635 ++cnt;
2636 }
2637 }
2638 pos_byte = pos_byte_next;
2639 pos++;
2640 }
2641
2642 return make_number (cnt);
2643 }
2644
2645 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
2646 "Delete the text between point and mark.\n\
2647 When called from a program, expects two arguments,\n\
2648 positions (integers or markers) specifying the stretch to be deleted.")
2649 (start, end)
2650 Lisp_Object start, end;
2651 {
2652 validate_region (&start, &end);
2653 del_range (XINT (start), XINT (end));
2654 return Qnil;
2655 }
2656
2657 DEFUN ("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 }
2666 \f
2667 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
2668 "Remove restrictions (narrowing) from current buffer.\n\
2669 This allows the buffer's full text to be seen and edited.")
2670 ()
2671 {
2672 if (BEG != BEGV || Z != ZV)
2673 current_buffer->clip_changed = 1;
2674 BEGV = BEG;
2675 BEGV_BYTE = BEG_BYTE;
2676 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
2677 /* Changing the buffer bounds invalidates any recorded current column. */
2678 invalidate_current_column ();
2679 return Qnil;
2680 }
2681
2682 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
2683 "Restrict editing in this buffer to the current region.\n\
2684 The rest of the text becomes temporarily invisible and untouchable\n\
2685 but is not deleted; if you save the buffer in a file, the invisible\n\
2686 text is included in the file. \\[widen] makes all visible again.\n\
2687 See also `save-restriction'.\n\
2688 \n\
2689 When calling from a program, pass two arguments; positions (integers\n\
2690 or markers) bounding the text that should remain visible.")
2691 (start, end)
2692 register Lisp_Object start, end;
2693 {
2694 CHECK_NUMBER_COERCE_MARKER (start, 0);
2695 CHECK_NUMBER_COERCE_MARKER (end, 1);
2696
2697 if (XINT (start) > XINT (end))
2698 {
2699 Lisp_Object tem;
2700 tem = start; start = end; end = tem;
2701 }
2702
2703 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
2704 args_out_of_range (start, end);
2705
2706 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
2707 current_buffer->clip_changed = 1;
2708
2709 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2710 SET_BUF_ZV (current_buffer, XFASTINT (end));
2711 if (PT < XFASTINT (start))
2712 SET_PT (XFASTINT (start));
2713 if (PT > XFASTINT (end))
2714 SET_PT (XFASTINT (end));
2715 /* Changing the buffer bounds invalidates any recorded current column. */
2716 invalidate_current_column ();
2717 return Qnil;
2718 }
2719
2720 Lisp_Object
2721 save_restriction_save ()
2722 {
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);
2736
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 }
2742 }
2743
2744 Lisp_Object
2745 save_restriction_restore (data)
2746 Lisp_Object data;
2747 {
2748 if (CONSP (data))
2749 /* A pair of marks bounding a saved restriction. */
2750 {
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. */
2754
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);
2778
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));
2784
2785 buf->clip_changed = 1; /* Remember that the narrowing changed. */
2786 }
2787 }
2788
2789 return Qnil;
2790 }
2791
2792 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2793 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2794 The 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\
2796 This special form, `save-restriction', saves the current buffer's restrictions\n\
2797 when it is entered, and restores them when it is exited.\n\
2798 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2799 The old restrictions settings are restored\n\
2800 even in case of abnormal exit (throw or error).\n\
2801 \n\
2802 The value returned is the value of the last form in BODY.\n\
2803 \n\
2804 Note: if you are using both `save-excursion' and `save-restriction',\n\
2805 use `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
2818 #ifndef HAVE_MENUS
2819
2820 /* Buffer for the most recent text displayed by Fmessage. */
2821 static char *message_text;
2822
2823 /* Allocated length of that buffer. */
2824 static int message_length;
2825
2826 #endif /* not HAVE_MENUS */
2827
2828 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2829 "Print a one-line message at the bottom of the screen.\n\
2830 The first argument is a format control string, and the rest are data\n\
2831 to be formatted under control of the string. See `format' for details.\n\
2832 \n\
2833 If the first argument is nil, clear any existing message; let the\n\
2834 minibuffer contents show.")
2835 (nargs, args)
2836 int nargs;
2837 Lisp_Object *args;
2838 {
2839 if (NILP (args[0]))
2840 {
2841 message (0);
2842 return Qnil;
2843 }
2844 else
2845 {
2846 register Lisp_Object val;
2847 val = Fformat (nargs, args);
2848 message3 (val, STRING_BYTES (XSTRING (val)), STRING_MULTIBYTE (val));
2849 return val;
2850 }
2851 }
2852
2853 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
2854 "Display a message, in a dialog box if possible.\n\
2855 If a dialog box is not available, use the echo area.\n\
2856 The first argument is a format control string, and the rest are data\n\
2857 to be formatted under control of the string. See `format' for details.\n\
2858 \n\
2859 If the first argument is nil, clear any existing message; let the\n\
2860 minibuffer 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);
2874 #ifdef HAVE_MENUS
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 }
2885 #else /* not HAVE_MENUS */
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 }
2892 if (STRING_BYTES (XSTRING (val)) > message_length)
2893 {
2894 message_length = STRING_BYTES (XSTRING (val));
2895 message_text = (char *)xrealloc (message_text, message_length);
2896 }
2897 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
2898 message2 (message_text, STRING_BYTES (XSTRING (val)),
2899 STRING_MULTIBYTE (val));
2900 return val;
2901 #endif /* not HAVE_MENUS */
2902 }
2903 }
2904 #ifdef HAVE_MENUS
2905 extern Lisp_Object last_nonmenu_event;
2906 #endif
2907
2908 DEFUN ("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\
2910 If this command was invoked with the mouse, use a dialog box.\n\
2911 Otherwise, use the echo area.\n\
2912 The first argument is a format control string, and the rest are data\n\
2913 to be formatted under control of the string. See `format' for details.\n\
2914 \n\
2915 If the first argument is nil, clear any existing message; let the\n\
2916 minibuffer contents show.")
2917 (nargs, args)
2918 int nargs;
2919 Lisp_Object *args;
2920 {
2921 #ifdef HAVE_MENUS
2922 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2923 && use_dialog_box)
2924 return Fmessage_box (nargs, args);
2925 #endif
2926 return Fmessage (nargs, args);
2927 }
2928
2929 DEFUN ("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 {
2933 return current_message ();
2934 }
2935
2936
2937 DEFUN ("propertize", Fpropertize, Spropertize, 3, MANY, 0,
2938 "Return a copy of STRING with text properties added.\n\
2939 First argument is the string to copy.\n\
2940 Remaining arguments form a sequence of PROPERTY VALUE pairs for text\n\
2941 properties 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);
2956
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
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)) \
2979 ? count_size_as_multibyte (XSTRING (STRING)->data, \
2980 STRING_BYTES (XSTRING (STRING))) \
2981 : STRING_BYTES (XSTRING (STRING)))
2982
2983 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
2984 "Format a string out of a control-string and arguments.\n\
2985 The first argument is a control string.\n\
2986 The other arguments are substituted into it to make the result, a string.\n\
2987 It 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\
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\
2994 %c means print a number as a single character.\n\
2995 %S means print any object as an s-expression (using `prin1').\n\
2996 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2997 Use %% to put a single % into the output.")
2998 (nargs, args)
2999 int nargs;
3000 register Lisp_Object *args;
3001 {
3002 register int n; /* The number of the next arg to substitute */
3003 register int total; /* An estimate of the final length */
3004 char *buf, *p;
3005 register unsigned char *format, *end;
3006 int nchars;
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;
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;
3015 unsigned char *this_format;
3016 int longest_format;
3017 Lisp_Object val;
3018 struct info
3019 {
3020 int start, end;
3021 } *info = 0;
3022
3023 /* It should not be necessary to GCPRO ARGS, because
3024 the caller in the interpreter should take care of that. */
3025
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. */
3030 for (n = 0; n < nargs; n++)
3031 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
3032 multibyte = 1;
3033
3034 CHECK_STRING (args[0], 0);
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
3040 format = XSTRING (args[0])->data;
3041 end = format + STRING_BYTES (XSTRING (args[0]));
3042 longest_format = 0;
3043
3044 /* Make room in result for all the non-%-codes in the control string. */
3045 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]);
3046
3047 /* Add to TOTAL enough space to hold the converted arguments. */
3048
3049 n = 0;
3050 while (format != end)
3051 if (*format++ == '%')
3052 {
3053 int minlen, thissize = 0;
3054 unsigned char *this_format_start = format - 1;
3055
3056 /* Process a numeric arg and skip it. */
3057 minlen = atoi (format);
3058 if (minlen < 0)
3059 minlen = - minlen;
3060
3061 while ((*format >= '0' && *format <= '9')
3062 || *format == '-' || *format == ' ' || *format == '.')
3063 format++;
3064
3065 if (format - this_format_start + 1 > longest_format)
3066 longest_format = format - this_format_start + 1;
3067
3068 if (format == end)
3069 error ("Format string ends in middle of format specifier");
3070 if (*format == '%')
3071 format++;
3072 else if (++n >= nargs)
3073 error ("Not enough arguments for format string");
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);
3079 if (STRING_MULTIBYTE (tem) && ! multibyte)
3080 {
3081 multibyte = 1;
3082 goto retry;
3083 }
3084 args[n] = tem;
3085 goto string;
3086 }
3087 else if (SYMBOLP (args[n]))
3088 {
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);
3093 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
3094 {
3095 multibyte = 1;
3096 goto retry;
3097 }
3098 goto string;
3099 }
3100 else if (STRINGP (args[n]))
3101 {
3102 string:
3103 if (*format != 's' && *format != 'S')
3104 error ("Format specifier doesn't match argument type");
3105 thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
3106 }
3107 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
3108 else if (INTEGERP (args[n]) && *format != 's')
3109 {
3110 /* The following loop assumes the Lisp type indicates
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]);
3116 else
3117 if (*format != 'd' && *format != 'o' && *format != 'x'
3118 && *format != 'i' && *format != 'X' && *format != 'c')
3119 error ("Invalid format operation %%%c", *format);
3120
3121 thissize = 30;
3122 if (*format == 'c'
3123 && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
3124 || XINT (args[n]) == 0))
3125 {
3126 if (! multibyte)
3127 {
3128 multibyte = 1;
3129 goto retry;
3130 }
3131 args[n] = Fchar_to_string (args[n]);
3132 thissize = STRING_BYTES (XSTRING (args[n]));
3133 }
3134 }
3135 else if (FLOATP (args[n]) && *format != 's')
3136 {
3137 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
3138 args[n] = Ftruncate (args[n], Qnil);
3139 thissize = 200;
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);
3146 if (STRING_MULTIBYTE (tem) & ! multibyte)
3147 {
3148 multibyte = 1;
3149 goto retry;
3150 }
3151 args[n] = tem;
3152 goto string;
3153 }
3154
3155 if (thissize < minlen)
3156 thissize = minlen;
3157
3158 total += thissize + 4;
3159 }
3160
3161 /* Now we can no longer jump to retry.
3162 TOTAL and LONGEST_FORMAT are known for certain. */
3163
3164 this_format = (unsigned char *) alloca (longest_format + 1);
3165
3166 /* Allocate the space for the result.
3167 Note that TOTAL is an overestimate. */
3168 if (total < 1000)
3169 buf = (char *) alloca (total + 1);
3170 else
3171 buf = (char *) xmalloc (total + 1);
3172
3173 p = buf;
3174 nchars = 0;
3175 n = 0;
3176
3177 /* Scan the format and store result in BUF. */
3178 format = XSTRING (args[0])->data;
3179 maybe_combine_byte = 0;
3180 while (format != end)
3181 {
3182 if (*format == '%')
3183 {
3184 int minlen;
3185 int negative = 0;
3186 unsigned char *this_format_start = format;
3187
3188 format++;
3189
3190 /* Process a numeric arg and skip it. */
3191 minlen = atoi (format);
3192 if (minlen < 0)
3193 minlen = - minlen, negative = 1;
3194
3195 while ((*format >= '0' && *format <= '9')
3196 || *format == '-' || *format == ' ' || *format == '.')
3197 format++;
3198
3199 if (*format++ == '%')
3200 {
3201 *p++ = '%';
3202 nchars++;
3203 continue;
3204 }
3205
3206 ++n;
3207
3208 if (STRINGP (args[n]))
3209 {
3210 int padding, nbytes;
3211 int width = strwidth (XSTRING (args[n])->data,
3212 STRING_BYTES (XSTRING (args[n])));
3213 int start = nchars;
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 }
3223
3224 if (p > buf
3225 && multibyte
3226 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3227 && STRING_MULTIBYTE (args[n])
3228 && !CHAR_HEAD_P (XSTRING (args[n])->data[0]))
3229 maybe_combine_byte = 1;
3230 nbytes = copy_text (XSTRING (args[n])->data, p,
3231 STRING_BYTES (XSTRING (args[n])),
3232 STRING_MULTIBYTE (args[n]), multibyte);
3233 p += nbytes;
3234 nchars += XSTRING (args[n])->size;
3235
3236 if (negative)
3237 while (padding-- > 0)
3238 {
3239 *p++ = ' ';
3240 nchars++;
3241 }
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 }
3253
3254 info[n].start = start;
3255 info[n].end = nchars;
3256 }
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
3266 if (INTEGERP (args[n]))
3267 sprintf (p, this_format, XINT (args[n]));
3268 else
3269 sprintf (p, this_format, XFLOAT_DATA (args[n]));
3270
3271 if (p > buf
3272 && multibyte
3273 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3274 && !CHAR_HEAD_P (*((unsigned char *) p)))
3275 maybe_combine_byte = 1;
3276 this_nchars = strlen (p);
3277 if (multibyte)
3278 p += str_to_multibyte (p, buf + total - p, this_nchars);
3279 else
3280 p += this_nchars;
3281 nchars += this_nchars;
3282 }
3283 }
3284 else if (STRING_MULTIBYTE (args[0]))
3285 {
3286 /* Copy a whole multibyte character. */
3287 if (p > buf
3288 && multibyte
3289 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3290 && !CHAR_HEAD_P (*format))
3291 maybe_combine_byte = 1;
3292 *p++ = *format++;
3293 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
3294 nchars++;
3295 }
3296 else if (multibyte)
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
3309 if (maybe_combine_byte)
3310 nchars = multibyte_chars_in_text (buf, p - buf);
3311 val = make_specified_string (buf, nchars, p - buf, multibyte);
3312
3313 /* If we allocated BUF with malloc, free it too. */
3314 if (total >= 1000)
3315 xfree (buf);
3316
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. */
3320
3321 if (XSTRING (args[0])->intervals || info)
3322 {
3323 Lisp_Object len, new_len, props;
3324 struct gcpro gcpro1;
3325
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);
3330
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);
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);
3351 add_text_properties_from_list (val, props,
3352 make_number (info[n].start));
3353 }
3354
3355 UNGCPRO;
3356 }
3357
3358 return val;
3359 }
3360
3361
3362 /* VARARGS 1 */
3363 Lisp_Object
3364 #ifdef NO_ARG_ARRAY
3365 format1 (string1, arg0, arg1, arg2, arg3, arg4)
3366 EMACS_INT arg0, arg1, arg2, arg3, arg4;
3367 #else
3368 format1 (string1)
3369 #endif
3370 char *string1;
3371 {
3372 char buf[100];
3373 #ifdef NO_ARG_ARRAY
3374 EMACS_INT args[5];
3375 args[0] = arg0;
3376 args[1] = arg1;
3377 args[2] = arg2;
3378 args[3] = arg3;
3379 args[4] = arg4;
3380 doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
3381 #else
3382 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
3383 #endif
3384 return build_string (buf);
3385 }
3386 \f
3387 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
3388 "Return t if two characters match, optionally ignoring case.\n\
3389 Both arguments must be characters (i.e. integers).\n\
3390 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
3391 (c1, c2)
3392 register Lisp_Object c1, c2;
3393 {
3394 int i1, i2;
3395 CHECK_NUMBER (c1, 0);
3396 CHECK_NUMBER (c2, 1);
3397
3398 if (XINT (c1) == XINT (c2))
3399 return Qt;
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);
3409 }
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
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
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.
3423
3424 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
3425
3426 static void
3427 transpose_markers (start1, end1, start2, end2,
3428 start1_byte, end1_byte, start2_byte, end2_byte)
3429 register int start1, end1, start2, end2;
3430 register int start1_byte, end1_byte, start2_byte, end2_byte;
3431 {
3432 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
3433 register Lisp_Object marker;
3434
3435 /* Update point as if it were a marker. */
3436 if (PT < start1)
3437 ;
3438 else if (PT < end1)
3439 TEMP_SET_PT_BOTH (PT + (end2 - end1),
3440 PT_BYTE + (end2_byte - end1_byte));
3441 else if (PT < start2)
3442 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
3443 (PT_BYTE + (end2_byte - start2_byte)
3444 - (end1_byte - start1_byte)));
3445 else if (PT < end2)
3446 TEMP_SET_PT_BOTH (PT - (start2 - start1),
3447 PT_BYTE - (start2_byte - start1_byte));
3448
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. */
3456
3457 /* The difference between the region's lengths */
3458 diff = (end2 - start2) - (end1 - start1);
3459 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
3460
3461 /* For shifting each marker in a region by the length of the other
3462 region plus the distance between the regions. */
3463 amt1 = (end2 - start2) + (start2 - end1);
3464 amt2 = (end1 - start1) + (start2 - end1);
3465 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
3466 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
3467
3468 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
3469 marker = XMARKER (marker)->chain)
3470 {
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;
3480 XMARKER (marker)->bytepos = mpos;
3481 }
3482 mpos = XMARKER (marker)->charpos;
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;
3491 }
3492 XMARKER (marker)->charpos = mpos;
3493 }
3494 }
3495
3496 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
3497 "Transpose region START1 to END1 with START2 to END2.\n\
3498 The regions may not be overlapping, because the size of the buffer is\n\
3499 never changed in a transposition.\n\
3500 \n\
3501 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
3502 any markers that happen to be located in the regions.\n\
3503 \n\
3504 Transposing beyond buffer boundaries is an error.")
3505 (startr1, endr1, startr2, endr2, leave_markers)
3506 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
3507 {
3508 register int start1, end1, start2, end2;
3509 int start1_byte, start2_byte, len1_byte, len2_byte;
3510 int gap, len1, len_mid, len2;
3511 unsigned char *start1_addr, *start2_addr, *temp;
3512 struct gcpro gcpro1, gcpro2;
3513
3514 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
3515 cur_intv = BUF_INTERVALS (current_buffer);
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
3537 len1 = end1 - start1;
3538 len2 = end2 - start2;
3539
3540 if (start2 < end1)
3541 error ("Transposed regions overlap");
3542 else if (start1 == end1 || start2 == end2)
3543 error ("Transposed region has length 0");
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.
3549
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. */
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 }
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;
3581
3582 #ifdef BYTE_COMBINING_DEBUG
3583 if (end1 == start2)
3584 {
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 ();
3592 }
3593 else
3594 {
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 ();
3604 }
3605 #endif
3606
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 {
3616 modify_region (current_buffer, start1, end2);
3617 record_change (start1, len1 + len2);
3618
3619 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3620 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3621 Fset_text_properties (make_number (start1), make_number (end2),
3622 Qnil, Qnil);
3623
3624 /* First region smaller than second. */
3625 if (len1_byte < len2_byte)
3626 {
3627 /* We use alloca only if it is small,
3628 because we want to avoid stack overflow. */
3629 if (len2_byte > 20000)
3630 temp = (unsigned char *) xmalloc (len2_byte);
3631 else
3632 temp = (unsigned char *) alloca (len2_byte);
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. */
3637 start1_addr = BYTE_POS_ADDR (start1_byte);
3638 start2_addr = BYTE_POS_ADDR (start2_byte);
3639
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)
3644 xfree (temp);
3645 }
3646 else
3647 /* First region not smaller than second. */
3648 {
3649 if (len1_byte > 20000)
3650 temp = (unsigned char *) xmalloc (len1_byte);
3651 else
3652 temp = (unsigned char *) alloca (len1_byte);
3653 start1_addr = BYTE_POS_ADDR (start1_byte);
3654 start2_addr = BYTE_POS_ADDR (start2_byte);
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)
3659 xfree (temp);
3660 }
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);
3665 update_compositions (start1, start1 + len2, CHECK_BORDER);
3666 update_compositions (start1 + len2, end2, CHECK_TAIL);
3667 }
3668 /* Non-adjacent regions, because end1 != start2, bleagh... */
3669 else
3670 {
3671 len_mid = start2_byte - (start1_byte + len1_byte);
3672
3673 if (len1_byte == len2_byte)
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);
3680 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3681 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
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);
3686
3687 if (len1_byte > 20000)
3688 temp = (unsigned char *) xmalloc (len1_byte);
3689 else
3690 temp = (unsigned char *) alloca (len1_byte);
3691 start1_addr = BYTE_POS_ADDR (start1_byte);
3692 start2_addr = BYTE_POS_ADDR (start2_byte);
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)
3697 xfree (temp);
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);
3702 }
3703
3704 else if (len1_byte < len2_byte) /* Second region larger than first */
3705 /* Non-adjacent & unequal size, area between must also be shifted. */
3706 {
3707 modify_region (current_buffer, start1, end2);
3708 record_change (start1, (end2 - start1));
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);
3712 Fset_text_properties (make_number (start1), make_number (end2),
3713 Qnil, Qnil);
3714
3715 /* holds region 2 */
3716 if (len2_byte > 20000)
3717 temp = (unsigned char *) xmalloc (len2_byte);
3718 else
3719 temp = (unsigned char *) alloca (len2_byte);
3720 start1_addr = BYTE_POS_ADDR (start1_byte);
3721 start2_addr = BYTE_POS_ADDR (start2_byte);
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)
3727 xfree (temp);
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);
3734 }
3735 else
3736 /* Second region smaller than first. */
3737 {
3738 record_change (start1, (end2 - start1));
3739 modify_region (current_buffer, start1, end2);
3740
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);
3744 Fset_text_properties (make_number (start1), make_number (end2),
3745 Qnil, Qnil);
3746
3747 /* holds region 1 */
3748 if (len1_byte > 20000)
3749 temp = (unsigned char *) xmalloc (len1_byte);
3750 else
3751 temp = (unsigned char *) alloca (len1_byte);
3752 start1_addr = BYTE_POS_ADDR (start1_byte);
3753 start2_addr = BYTE_POS_ADDR (start2_byte);
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)
3759 xfree (temp);
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);
3766 }
3767
3768 update_compositions (start1, start1 + len2, CHECK_BORDER);
3769 update_compositions (end2 - len1, end2, CHECK_BORDER);
3770 }
3771
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. */
3775 if (NILP (leave_markers))
3776 {
3777 transpose_markers (start1, end1, start2, end2,
3778 start1_byte, start1_byte + len1_byte,
3779 start2_byte, start2_byte + len2_byte);
3780 fix_overlays_in_range (start1, end2);
3781 }
3782
3783 return Qnil;
3784 }
3785
3786 \f
3787 void
3788 syms_of_editfns ()
3789 {
3790 environbuf = 0;
3791
3792 Qbuffer_access_fontify_functions
3793 = intern ("buffer-access-fontify-functions");
3794 staticpro (&Qbuffer_access_fontify_functions);
3795
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
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\
3803 Each function is called with two arguments which specify the range\n\
3804 of the buffer being accessed.");
3805 Vbuffer_access_fontify_functions = Qnil;
3806
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
3819 DEFVAR_LISP ("buffer-access-fontified-property",
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\
3823 functions if all the text being accessed has this property.");
3824 Vbuffer_access_fontified_property = Qnil;
3825
3826 DEFVAR_LISP ("system-name", &Vsystem_name,
3827 "The name of the machine Emacs is running on.");
3828
3829 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
3830 "The full name of the user logged in.");
3831
3832 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
3833 "The user's name, taken from environment variables if possible.");
3834
3835 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
3836 "The user's name, based upon the real uid only.");
3837
3838 defsubr (&Spropertize);
3839 defsubr (&Schar_equal);
3840 defsubr (&Sgoto_char);
3841 defsubr (&Sstring_to_char);
3842 defsubr (&Schar_to_string);
3843 defsubr (&Sbuffer_substring);
3844 defsubr (&Sbuffer_substring_no_properties);
3845 defsubr (&Sbuffer_string);
3846
3847 defsubr (&Spoint_marker);
3848 defsubr (&Smark_marker);
3849 defsubr (&Spoint);
3850 defsubr (&Sregion_beginning);
3851 defsubr (&Sregion_end);
3852
3853 staticpro (&Qfield);
3854 Qfield = intern ("field");
3855 staticpro (&Qboundary);
3856 Qboundary = intern ("boundary");
3857 defsubr (&Sfield_beginning);
3858 defsubr (&Sfield_end);
3859 defsubr (&Sfield_string);
3860 defsubr (&Sfield_string_no_properties);
3861 defsubr (&Sdelete_field);
3862 defsubr (&Sconstrain_to_field);
3863
3864 defsubr (&Sline_beginning_position);
3865 defsubr (&Sline_end_position);
3866
3867 /* defsubr (&Smark); */
3868 /* defsubr (&Sset_mark); */
3869 defsubr (&Ssave_excursion);
3870 defsubr (&Ssave_current_buffer);
3871
3872 defsubr (&Sbufsize);
3873 defsubr (&Spoint_max);
3874 defsubr (&Spoint_min);
3875 defsubr (&Spoint_min_marker);
3876 defsubr (&Spoint_max_marker);
3877 defsubr (&Sgap_position);
3878 defsubr (&Sgap_size);
3879 defsubr (&Sposition_bytes);
3880 defsubr (&Sbyte_to_position);
3881
3882 defsubr (&Sbobp);
3883 defsubr (&Seobp);
3884 defsubr (&Sbolp);
3885 defsubr (&Seolp);
3886 defsubr (&Sfollowing_char);
3887 defsubr (&Sprevious_char);
3888 defsubr (&Schar_after);
3889 defsubr (&Schar_before);
3890 defsubr (&Sinsert);
3891 defsubr (&Sinsert_before_markers);
3892 defsubr (&Sinsert_and_inherit);
3893 defsubr (&Sinsert_and_inherit_before_markers);
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);
3901 defsubr (&Semacs_pid);
3902 defsubr (&Scurrent_time);
3903 defsubr (&Sformat_time_string);
3904 defsubr (&Sfloat_time);
3905 defsubr (&Sdecode_time);
3906 defsubr (&Sencode_time);
3907 defsubr (&Scurrent_time_string);
3908 defsubr (&Scurrent_time_zone);
3909 defsubr (&Sset_time_zone_rule);
3910 defsubr (&Ssystem_name);
3911 defsubr (&Smessage);
3912 defsubr (&Smessage_box);
3913 defsubr (&Smessage_or_box);
3914 defsubr (&Scurrent_message);
3915 defsubr (&Sformat);
3916
3917 defsubr (&Sinsert_buffer_substring);
3918 defsubr (&Scompare_buffer_substrings);
3919 defsubr (&Ssubst_char_in_region);
3920 defsubr (&Stranslate_region);
3921 defsubr (&Sdelete_region);
3922 defsubr (&Sdelete_and_extract_region);
3923 defsubr (&Swiden);
3924 defsubr (&Snarrow_to_region);
3925 defsubr (&Ssave_restriction);
3926 defsubr (&Stranspose_regions);
3927 }