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