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